2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
/*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This file is part of RaspiSMS.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
2019-10-30 00:30:39 +01:00
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
namespace controllers\internals;
|
|
|
|
|
2019-11-13 06:13:55 +01:00
|
|
|
class Contact extends StandardController
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
protected $model;
|
2019-10-30 00:17:10 +01:00
|
|
|
|
2019-11-12 19:19:55 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return a contact for a user by a number.
|
|
|
|
*
|
|
|
|
* @param int $id_user : user id
|
|
|
|
* @param string $number : Contact number
|
|
|
|
*
|
2019-11-12 17:37:20 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-11-14 21:44:31 +01:00
|
|
|
public function get_by_number_and_user(int $id_user, string $number)
|
2019-11-12 17:37:20 +01:00
|
|
|
{
|
2019-11-14 02:02:50 +01:00
|
|
|
return $this->get_model()->get_by_number_and_user($id_user, $number);
|
2019-11-12 17:37:20 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return a contact by his name for a user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param string $name : Contact name
|
|
|
|
*
|
2019-11-13 06:13:55 +01:00
|
|
|
* @return array
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
2019-11-13 06:13:55 +01:00
|
|
|
public function get_by_name_and_user(int $id_user, string $name)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-14 02:02:50 +01:00
|
|
|
return $this->get_model()->get_by_name_and_user($id_user, $name);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-20 02:45:00 +01:00
|
|
|
/**
|
|
|
|
* Return all contacts of a user.
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-20 02:45:00 +01:00
|
|
|
* @param int $id_user : user id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-20 02:45:00 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function gets_for_user(int $id_user)
|
2019-11-20 02:45:00 +01:00
|
|
|
{
|
|
|
|
return $this->get_model()->gets_for_user($id_user);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Create a new contact.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param string $number : Contact number
|
|
|
|
* @param string $name : Contact name
|
|
|
|
* @param string $datas : Contact datas
|
|
|
|
*
|
2019-11-13 06:13:55 +01:00
|
|
|
* @return mixed bool|int : False if cannot create contact, id of the new contact else
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2019-11-28 01:55:11 +01:00
|
|
|
public function create($id_user, $number, $name, $datas)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$contact = [
|
2019-11-12 19:19:55 +01:00
|
|
|
'id_user' => $id_user,
|
2019-10-29 14:57:13 +01:00
|
|
|
'number' => $number,
|
|
|
|
'name' => $name,
|
2019-11-20 02:45:00 +01:00
|
|
|
'datas' => $datas,
|
2019-10-29 14:57:13 +01:00
|
|
|
];
|
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
$result = $this->get_model()->insert($contact);
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$result)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-11-15 06:30:23 +01:00
|
|
|
$internal_event = new Event($this->bdd);
|
2020-01-17 18:47:08 +01:00
|
|
|
$internal_event->create($id_user, 'CONTACT_ADD', 'Ajout contact : ' . $name . ' (' . \controllers\internals\Tool::phone_format($number) . ')');
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
return $result;
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Update a contact.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param int $id : Contact id
|
|
|
|
* @param string $number : Contact number
|
|
|
|
* @param string $name : Contact name
|
|
|
|
* @param ?string $datas : Contact datas
|
|
|
|
*
|
2019-11-13 06:13:55 +01:00
|
|
|
* @return int : number of modified rows
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2019-11-28 01:55:11 +01:00
|
|
|
public function update_for_user(int $id_user, int $id, string $number, string $name, string $datas)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-20 02:45:00 +01:00
|
|
|
$contact = [
|
2019-10-29 14:57:13 +01:00
|
|
|
'number' => $number,
|
|
|
|
'name' => $name,
|
2019-11-20 02:45:00 +01:00
|
|
|
'datas' => $datas,
|
2019-10-29 14:57:13 +01:00
|
|
|
];
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-11-20 02:45:00 +01:00
|
|
|
return $this->get_model()->update_for_user($id_user, $id, $contact);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-11-30 05:31:20 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Import a list of contacts as csv.
|
|
|
|
*
|
2019-11-30 05:31:20 +01:00
|
|
|
* @param resource $file_handler : File handler to import contacts from
|
2020-01-17 18:19:25 +01:00
|
|
|
* @param int $id_user : User id
|
|
|
|
*
|
2019-11-30 05:31:20 +01:00
|
|
|
* @return mixed : False on error, number of inserted contacts else
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function import_csv(int $id_user, $file_handler)
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
if (!\is_resource($file_handler))
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$nb_insert = 0;
|
|
|
|
|
|
|
|
$head = null;
|
|
|
|
while ($line = fgetcsv($file_handler))
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
if (null === $head)
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
|
|
|
$head = $line;
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-30 05:31:20 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$line = array_combine($head, $line);
|
2020-01-17 18:19:25 +01:00
|
|
|
if (false === $line)
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-13 19:49:17 +02:00
|
|
|
if (!isset($line[array_keys($line)[0]], $line[array_keys($line)[1]]))
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$datas = [];
|
2020-06-13 19:49:17 +02:00
|
|
|
$i = 0;
|
2019-11-30 05:31:20 +01:00
|
|
|
foreach ($line as $key => $value)
|
|
|
|
{
|
2020-06-13 19:49:17 +02:00
|
|
|
$i++;
|
|
|
|
if ($i < 3)
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
if ('' === $value)
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = mb_ereg_replace('[\W]', '', $key);
|
|
|
|
$datas[$key] = $value;
|
|
|
|
}
|
|
|
|
$datas = json_encode($datas);
|
|
|
|
|
2020-06-13 19:10:55 +02:00
|
|
|
try
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
2020-06-13 19:49:17 +02:00
|
|
|
$success = $this->create($id_user, $line[array_keys($line)[1]], $line[array_keys($line)[0]], $datas);
|
2020-06-13 19:10:55 +02:00
|
|
|
if ($success)
|
|
|
|
{
|
|
|
|
++$nb_insert;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
continue;
|
2019-11-30 05:31:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $nb_insert;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Import a list of contacts as json.
|
|
|
|
*
|
2019-11-30 05:31:20 +01:00
|
|
|
* @param resource $file_handler : File handler to import contacts from
|
2020-01-17 18:19:25 +01:00
|
|
|
* @param int $id_user : User id
|
|
|
|
*
|
2019-11-30 05:31:20 +01:00
|
|
|
* @return mixed : False on error, number of inserted contacts else
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function import_json(int $id_user, $file_handler)
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
if (!\is_resource($file_handler))
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_content = '';
|
|
|
|
while ($line = fgets($file_handler))
|
|
|
|
{
|
|
|
|
$file_content .= $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$contacts = json_decode($file_content, true);
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
if (!\is_array($contacts))
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$nb_insert = 0;
|
|
|
|
foreach ($contacts as $contact)
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
if (!\is_array($contact))
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($contact['name'], $contact['number']))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$datas = $contact['datas'] ?? [];
|
|
|
|
$datas = json_encode($datas);
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-06-13 19:10:55 +02:00
|
|
|
try
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
2020-06-13 19:10:55 +02:00
|
|
|
$success = $this->create($id_user, $contact['number'], $contact['name'], $datas);
|
|
|
|
if ($success)
|
|
|
|
{
|
|
|
|
++$nb_insert;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
continue;
|
2019-11-30 05:31:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $nb_insert;
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-30 05:31:20 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Export the contacts of a user as csv.
|
|
|
|
*
|
2019-11-30 05:31:20 +01:00
|
|
|
* @param int $id_user : User id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-30 05:31:20 +01:00
|
|
|
* @return array : ['headers' => array of headers to return, 'content' => the generated file]
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function export_csv(int $id_user): array
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
|
|
|
$contacts = $this->get_model()->gets_for_user($id_user);
|
|
|
|
|
2020-06-14 00:46:51 +02:00
|
|
|
$columns = [0, 1];
|
2019-11-30 05:31:20 +01:00
|
|
|
|
|
|
|
foreach ($contacts as $contact)
|
|
|
|
{
|
|
|
|
$datas = json_decode($contact['datas'], true);
|
|
|
|
foreach ($datas as $key => $value)
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$columns[] = $key;
|
2019-11-30 05:31:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$columns = array_unique($columns);
|
|
|
|
|
|
|
|
$lines = [];
|
|
|
|
foreach ($contacts as $contact)
|
|
|
|
{
|
|
|
|
$datas = json_decode($contact['datas'], true);
|
|
|
|
|
2020-06-14 00:46:51 +02:00
|
|
|
$line = [$contact['name'], $contact['number']];
|
2019-11-30 05:31:20 +01:00
|
|
|
foreach ($columns as $column)
|
|
|
|
{
|
|
|
|
if (isset($datas[$column]))
|
|
|
|
{
|
|
|
|
$line[] = $datas[$column];
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-30 05:31:20 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$line[] = null;
|
|
|
|
}
|
|
|
|
$lines[] = $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Php only support csv formatting to file. To get it in string we need to create a tmp in memory file, write in it, and then read the file into a var
|
|
|
|
// output up to 5MB is kept in memory, if it becomes bigger it will automatically be written to a temporary file
|
2020-01-17 18:47:08 +01:00
|
|
|
$csv_tmp_file = fopen('php://temp/maxmemory:' . (5 * 1024 * 1024), 'r+');
|
2019-11-30 05:31:20 +01:00
|
|
|
fputcsv($csv_tmp_file, $columns);
|
|
|
|
foreach ($lines as $line)
|
|
|
|
{
|
|
|
|
fputcsv($csv_tmp_file, $line);
|
|
|
|
}
|
|
|
|
rewind($csv_tmp_file);
|
|
|
|
|
|
|
|
$csv_string = stream_get_contents($csv_tmp_file);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'headers' => [
|
|
|
|
'Content-Disposition: attachment; filename=contacts.csv',
|
|
|
|
'Content-Type: text/csv',
|
2020-01-17 18:47:08 +01:00
|
|
|
'Content-Length: ' . mb_strlen($csv_string),
|
2019-11-30 05:31:20 +01:00
|
|
|
],
|
|
|
|
'content' => $csv_string,
|
|
|
|
];
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-30 05:31:20 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Export the contacts of a user as json.
|
|
|
|
*
|
2019-11-30 05:31:20 +01:00
|
|
|
* @param int $id_user : User id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-30 05:31:20 +01:00
|
|
|
* @return array : ['headers' => array of headers to return, 'content' => the generated file]
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function export_json(int $id_user): array
|
2019-11-30 05:31:20 +01:00
|
|
|
{
|
|
|
|
$contacts = $this->get_model()->gets_for_user($id_user);
|
|
|
|
|
|
|
|
foreach ($contacts as &$contact)
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
unset($contact['id'], $contact['id_user']);
|
|
|
|
|
2019-11-30 05:31:20 +01:00
|
|
|
$contact['datas'] = json_decode($contact['datas']);
|
|
|
|
}
|
|
|
|
$content = json_encode($contacts);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'headers' => [
|
|
|
|
'Content-Disposition: attachment; filename=contacts.json',
|
|
|
|
'Content-Type: application/json',
|
2020-01-17 18:47:08 +01:00
|
|
|
'Content-Length: ' . mb_strlen($content),
|
2019-11-30 05:31:20 +01:00
|
|
|
],
|
|
|
|
'content' => $content,
|
|
|
|
];
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the model for the Controller.
|
|
|
|
*
|
|
|
|
* @return \descartes\Model
|
|
|
|
*/
|
|
|
|
protected function get_model(): \descartes\Model
|
|
|
|
{
|
|
|
|
$this->model = $this->model ?? new \models\Contact($this->bdd);
|
|
|
|
|
|
|
|
return $this->model;
|
|
|
|
}
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|