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\publics;
|
2019-10-29 18:36:25 +01:00
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Page des receiveds.
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
class Received extends \descartes\Controller
|
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
private $internal_received;
|
|
|
|
private $internal_contact;
|
2019-11-12 17:37:20 +01:00
|
|
|
private $internal_phone;
|
2021-03-21 18:29:23 +01:00
|
|
|
private $internal_media;
|
2019-10-30 00:17:10 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
|
|
|
* Cette fonction est appelée avant toute les autres :
|
2019-10-30 00:30:39 +01:00
|
|
|
* Elle vérifie que l'utilisateur est bien connecté.
|
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @return void;
|
|
|
|
*/
|
2019-10-30 00:17:10 +01:00
|
|
|
public function __construct()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
|
|
|
$this->internal_received = new \controllers\internals\Received($bdd);
|
|
|
|
$this->internal_contact = new \controllers\internals\Contact($bdd);
|
2019-11-12 17:37:20 +01:00
|
|
|
$this->internal_phone = new \controllers\internals\Phone($bdd);
|
2021-03-21 18:29:23 +01:00
|
|
|
$this->internal_media = new \controllers\internals\Media($bdd);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
\controllers\internals\Tool::verifyconnect();
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne tous les receiveds, sous forme d'un tableau permettant l'administration de ces receiveds.
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2020-04-07 03:02:33 +02:00
|
|
|
public function list()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2020-09-23 03:02:13 +02:00
|
|
|
$this->render('received/list', ['is_unread' => false]);
|
|
|
|
}
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-09-23 03:02:13 +02:00
|
|
|
/**
|
2021-07-16 22:53:33 +02:00
|
|
|
* Return receiveds as json.
|
|
|
|
*
|
|
|
|
* @param bool $unread : Should we only search for unread messages
|
2020-09-23 03:02:13 +02:00
|
|
|
*/
|
2021-07-16 22:53:33 +02:00
|
|
|
public function list_json(bool $unread = false)
|
2020-09-23 03:02:13 +02:00
|
|
|
{
|
2021-07-16 22:53:33 +02:00
|
|
|
$draw = (int)($_GET['draw'] ?? false);
|
|
|
|
|
|
|
|
$columns = [
|
|
|
|
0 => 'searchable_origin',
|
|
|
|
1 => 'phone_name',
|
|
|
|
2 => 'text',
|
|
|
|
3 => 'at',
|
|
|
|
4 => 'status',
|
|
|
|
5 => 'command',
|
|
|
|
];
|
|
|
|
|
|
|
|
$search = $_GET['search']['value'] ?? null;
|
|
|
|
$order_column = $columns[$_GET['order'][0]['column']] ?? null;
|
|
|
|
$order_desc = ($_GET['order'][0]['dir'] ?? 'asc') == 'desc' ? true : false;
|
|
|
|
$offset = (int) ($_GET['start'] ?? 0);
|
|
|
|
$limit = (int) ($_GET['length'] ?? 25);
|
|
|
|
|
|
|
|
$entities = $this->internal_received->datatable_list_for_user($_SESSION['user']['id'], $limit, $offset, $search, $columns, $order_column, $order_desc, false, $unread);
|
|
|
|
$count_entities = $this->internal_received->datatable_list_for_user($_SESSION['user']['id'], $limit, $offset, $search, $columns, $order_column, $order_desc, true, $unread);
|
2020-09-23 03:02:13 +02:00
|
|
|
foreach ($entities as &$entity)
|
2019-10-30 00:30:39 +01:00
|
|
|
{
|
2020-09-23 03:02:13 +02:00
|
|
|
$entity['origin_formatted'] = \controllers\internals\Tool::phone_link($entity['origin']);
|
2021-03-21 18:29:23 +01:00
|
|
|
if ($entity['mms'])
|
|
|
|
{
|
|
|
|
$entity['medias'] = $this->internal_media->gets_for_received($entity['id']);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:33 +02:00
|
|
|
$records_total = $this->internal_received->count_for_user($_SESSION['user']['id']);
|
|
|
|
|
2020-09-23 03:02:13 +02:00
|
|
|
header('Content-Type: application/json');
|
2021-07-16 22:53:33 +02:00
|
|
|
echo json_encode([
|
|
|
|
'draw' => $draw,
|
|
|
|
'recordsTotal' => $records_total,
|
|
|
|
'recordsFiltered' => $count_entities,
|
|
|
|
'data' => $entities,
|
|
|
|
]);
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-02 01:55:06 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return all unread receiveds messages.
|
2019-12-02 01:55:06 +01:00
|
|
|
*/
|
2020-04-07 03:02:33 +02:00
|
|
|
public function list_unread()
|
2019-12-02 01:55:06 +01:00
|
|
|
{
|
2020-09-23 03:02:13 +02:00
|
|
|
$this->render('received/list', ['is_unread' => true]);
|
|
|
|
}
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-09-23 03:02:13 +02:00
|
|
|
/**
|
2021-01-14 03:32:17 +01:00
|
|
|
* Mark messages as.
|
2020-09-23 03:02:13 +02:00
|
|
|
*
|
2021-01-14 03:32:17 +01:00
|
|
|
* @param string $status : New status of the message, read or unread
|
2020-09-23 03:02:13 +02:00
|
|
|
* @param array int $_GET['ids'] : Ids of receiveds to delete
|
|
|
|
* @param mixed $csrf
|
|
|
|
*
|
|
|
|
* @return boolean;
|
|
|
|
*/
|
2021-01-14 03:32:17 +01:00
|
|
|
public function mark_as($status, $csrf)
|
2020-09-23 03:02:13 +02:00
|
|
|
{
|
|
|
|
if (!$this->verify_csrf($csrf))
|
2019-12-02 01:55:06 +01:00
|
|
|
{
|
2020-09-23 03:02:13 +02:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
|
2019-12-02 01:55:06 +01:00
|
|
|
|
2020-09-23 03:02:13 +02:00
|
|
|
return $this->redirect(\descartes\Router::url('Received', 'list'));
|
|
|
|
}
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-09-23 03:02:13 +02:00
|
|
|
$ids = $_GET['ids'] ?? [];
|
|
|
|
foreach ($ids as $id)
|
|
|
|
{
|
2021-01-14 03:32:17 +01:00
|
|
|
if (\models\Received::STATUS_UNREAD === $status)
|
2019-12-02 01:55:06 +01:00
|
|
|
{
|
2020-09-23 03:02:13 +02:00
|
|
|
$this->internal_received->mark_as_unread_for_user($_SESSION['user']['id'], $id);
|
2019-12-02 01:55:06 +01:00
|
|
|
}
|
2021-01-14 03:32:17 +01:00
|
|
|
elseif (\models\Received::STATUS_READ === $status)
|
2020-03-31 01:53:07 +02:00
|
|
|
{
|
2020-09-23 03:02:13 +02:00
|
|
|
$this->internal_received->mark_as_read_for_user($_SESSION['user']['id'], $id);
|
2020-03-31 01:53:07 +02:00
|
|
|
}
|
2019-12-02 01:55:06 +01:00
|
|
|
}
|
|
|
|
|
2020-09-23 03:02:13 +02:00
|
|
|
return $this->redirect(\descartes\Router::url('Received', 'list'));
|
2019-12-02 01:55:06 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-12 17:37:20 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Delete Receiveds.
|
|
|
|
*
|
2019-11-12 17:37:20 +01:00
|
|
|
* @param array int $_GET['ids'] : Ids of receiveds to delete
|
|
|
|
* @param mixed $csrf
|
|
|
|
*
|
|
|
|
* @return boolean;
|
|
|
|
*/
|
|
|
|
public function delete($csrf)
|
|
|
|
{
|
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-12 17:37:20 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Received', 'list'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$ids = $_GET['ids'] ?? [];
|
|
|
|
foreach ($ids as $id)
|
|
|
|
{
|
2019-11-14 22:33:00 +01:00
|
|
|
$this->internal_received->delete_for_user($_SESSION['user']['id'], $id);
|
2019-11-12 17:37:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->redirect(\descartes\Router::url('Received', 'list'));
|
|
|
|
}
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne tous les Sms reçus aujourd'hui pour la popup.
|
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @return string : A JSON Un tableau des Sms reçus
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
2019-10-29 18:36:25 +01:00
|
|
|
public function popup()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$now = new \DateTime();
|
2019-11-14 22:33:00 +01:00
|
|
|
$receiveds = $this->internal_received->get_since_by_date_for_user($_SESSION['user']['id'], $now->format('Y-m-d'));
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
foreach ($receiveds as $key => $received)
|
|
|
|
{
|
2019-11-15 06:30:23 +01:00
|
|
|
if (!$contact = $this->internal_contact->get_by_number_and_user($_SESSION['user']['id'], $received['origin']))
|
2019-10-30 00:30:39 +01:00
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-17 18:47:08 +01:00
|
|
|
$receiveds[$key]['origin'] = $this->s($contact['name'], false, true, false) . ' (' . \controllers\internals\Tool::phone_link($received['origin']) . ')';
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
$nb_received = \count($receiveds);
|
|
|
|
|
|
|
|
if (!isset($_SESSION['popup_nb_receiveds']) || $_SESSION['popup_nb_receiveds'] > $nb_received)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
$_SESSION['popup_nb_receiveds'] = $nb_received;
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
$newly_receiveds = \array_slice($receiveds, $_SESSION['popup_nb_receiveds']);
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
$_SESSION['popup_nb_receiveds'] = $nb_received;
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
echo json_encode($newly_receiveds);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|