Add unread messages support

This commit is contained in:
osaajani 2019-12-02 01:55:06 +01:00
parent cfde77a0c1
commit 88b00e4e9f
12 changed files with 249 additions and 15 deletions

View File

@ -24,6 +24,20 @@ namespace controllers\internals;
$this->model = $this->model ?? new \models\Received($this->bdd);
return $this->model;
}
/**
* Return the list of unread messages for a user
* @param int $id_user : User id
* @param ?int $nb_entry : Number of entry to return
* @param ?int $page : Pagination, used to calcul offset, $nb_entry * $page
* @return array : Entrys list
*/
public function list_unread_for_user (int $id_user, ?int $nb_entry = null, ?int $page = null)
{
return $this->get_model()->list_unread_for_user($id_user, $nb_entry, $nb_entry * $page);
}
/**
* Create a received
@ -31,16 +45,18 @@ namespace controllers\internals;
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $destination : Number of the receiver
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
* @return bool : false on error, new received id else
*/
public function create ($at, string $text, string $origin, string $destination, bool $command = false) : bool
public function create ($at, string $text, string $origin, string $destination, string $status = 'unread', bool $command = false) : bool
{
$received = [
'at' => $at,
'text' => $text,
'origin' => $origin,
'destination' => $destination,
'status' => $status,
'command' => $command,
];
@ -56,16 +72,18 @@ namespace controllers\internals;
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $destination : Number of the receiver
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
* @return bool : false on error, true on success
*/
public function update_for_user (int $id_user, int $id_received, $at, string $text, string $origin, string $destination, bool $command = false) : bool
public function update_for_user (int $id_user, int $id_received, $at, string $text, string $origin, string $destination, string $status = 'unread', bool $command = false) : bool
{
$received = [
'at' => $at,
'text' => $text,
'origin' => $origin,
'destination' => $destination,
'status' => $status,
'command' => $command,
];
@ -73,6 +91,50 @@ namespace controllers\internals;
}
/**
* Update a received message for a user to mark the message as read
* @param int $id_user : user id
* @param int $id_received : received id
* @return bool : false on error, true on success
*/
public function mark_as_read_for_user (int $id_user, int $id_received) : bool
{
$received = [
'status' => 'read',
];
return (bool) $this->get_model()->update_for_user($id_user, $id_received, $received);
}
/**
* Update a received message for a user to mark the message as unread
* @param int $id_user : user id
* @param int $id_received : received id
* @return bool : false on error, true on success
*/
public function mark_as_unread_for_user (int $id_user, int $id_received) : bool
{
$received = [
'status' => 'unread',
];
return (bool) $this->get_model()->update_for_user($id_user, $id_received, $received);
}
/**
* Return number of unread messages for a user
* @param int $id_user : User id
* @return array
*/
public function count_unread_for_user(int $id_user)
{
return $this->get_model()->count_unread_for_user($id_user);
}
/**
* Return x last receiveds message for a user, order by date
* @param int $id_user : User id

View File

@ -21,7 +21,6 @@ namespace controllers\publics;
private $internal_contact;
private $internal_group;
private $internal_scheduled;
private $internal_command;
private $internal_event;
/**
@ -39,7 +38,6 @@ namespace controllers\publics;
$this->internal_contact = new \controllers\internals\Contact($bdd);
$this->internal_group = new \controllers\internals\Group($bdd);
$this->internal_scheduled = new \controllers\internals\Scheduled($bdd);
$this->internal_command = new \controllers\internals\Command($bdd);
$this->internal_event = new \controllers\internals\Event($bdd);
\controllers\internals\Tool::verifyconnect();
@ -58,7 +56,7 @@ namespace controllers\publics;
$nb_contacts = $this->internal_contact->count_for_user($id_user);
$nb_groups = $this->internal_group->count_for_user($id_user);
$nb_scheduleds = $this->internal_scheduled->count_for_user($id_user);
$nb_commands = $this->internal_command->count_for_user($id_user);
$nb_unreads = $this->internal_received->count_unread_for_user($id_user);
$nb_sendeds = $this->internal_sended->count_for_user($id_user);
$nb_receiveds = $this->internal_received->count_for_user($id_user);
@ -124,9 +122,9 @@ namespace controllers\publics;
'nb_contacts' => $nb_contacts,
'nb_groups' => $nb_groups,
'nb_scheduleds' => $nb_scheduleds,
'nb_commands' => $nb_commands,
'nb_sendeds' => $nb_sendeds,
'nb_receiveds' => $nb_receiveds,
'nb_unreads' => $nb_unreads,
'avg_sendeds' => $avg_sendeds,
'avg_receiveds' => $avg_receiveds,
'sendeds' => $sendeds,

View File

@ -107,6 +107,11 @@ namespace controllers\publics;
foreach ($receiveds as $received)
{
if ($received['status'] != 'read')
{
$this->internal_received->mark_as_read_for_user($id_user, $received['id']);
}
$messages[] = [
'date' => htmlspecialchars($received['at']),
'text' => htmlspecialchars($received['text']),

View File

@ -49,6 +49,11 @@ namespace controllers\publics;
foreach ($receiveds as $key => $received)
{
if ($received['status'] != 'read')
{
$this->internal_received->mark_as_read_for_user($_SESSION['user']['id'], $received['id']);
}
if (!$contact = $this->internal_contact->get_by_number_and_user($_SESSION['user']['id'], $received['origin']))
{
continue;
@ -59,6 +64,32 @@ namespace controllers\publics;
$this->render('received/list', ['receiveds' => $receiveds, 'page' => $page, 'limit' => $limit, 'nb_results' => \count($receiveds)]);
}
/**
* Return all unread receiveds messages
* @param mixed $page
*/
public function list_unread($page = 0)
{
$page = (int) $page;
$limit = 25;
$receiveds = $this->internal_received->list_unread_for_user($_SESSION['user']['id'], $limit, $page);
foreach ($receiveds as $key => $received)
{
$this->internal_received->mark_as_read_for_user($_SESSION['user']['id'], $received['id']);
if (!$contact = $this->internal_contact->get_by_number_and_user($_SESSION['user']['id'], $received['origin']))
{
continue;
}
$receiveds[$key]['origin'] = $contact['name'].' ('.$received['origin'].')';
}
$this->render('received/list_unread', ['receiveds' => $receiveds, 'page' => $page, 'limit' => $limit, 'nb_results' => \count($receiveds)]);
}
/**
* Delete Receiveds

View File

@ -45,6 +45,7 @@ CREATE TABLE IF NOT EXISTS received
origin VARCHAR(20) NOT NULL,
destination VARCHAR(20),
command BOOLEAN NOT NULL DEFAULT 0,
status ENUM('read', 'unread') NOT NULL DEFAULT 'unread',
PRIMARY KEY (id)
);

View File

@ -89,6 +89,31 @@ namespace models;
return $this->_run_query($query, $params);
}
/**
* Return a list of unread received for a user
* @param int $id_user : User id
* @param int $limit : Max results to return
* @param int $offset : Number of results to ignore
*/
public function list_unread_for_user($id_user, $limit, $offset)
{
$limit = (int) $limit;
$offset = (int) $offset;
$query = '
SELECT * FROM received
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
AND status = \'unread\'
LIMIT ' . $limit . ' OFFSET ' . $offset;
$params = [
'id_user' => $id_user,
];
return $this->_run_query($query, $params);
}
/**
@ -192,6 +217,28 @@ namespace models;
return $this->_run_query($query, $params)[0]['nb'] ?? 0;
}
/**
* Count number of unread received sms for user
* @param int $id_user : user id
* @return int : Number of received SMS for user
*/
public function count_unread_for_user(int $id_user)
{
$query = '
SELECT COUNT(id) as nb
FROM received
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
AND status = \'unread\'
';
$params = [
'id_user' => $id_user,
];
return $this->_run_query($query, $params)[0]['nb'] ?? 0;
}
/**

View File

@ -99,6 +99,10 @@
'/received/',
'/received/p/{page}/',
],
'list_unread' => [
'/unread/',
'/unread/p/{page}/',
],
'delete' => '/received/delete/{csrf}/',
'popup' => '/received/popup/',
],

View File

@ -96,17 +96,17 @@
<div class="panel-heading">
<div class="row">
<div class="col-xs-3">
<i class="fa fa-terminal fa-5x"></i>
<i class="fa fa-eye-slash fa-5x"></i>
</div>
<div class="col-xs-9 text-right">
<div class="huge"><?php echo $nb_commands; ?></div>
<div>Commandes</div>
<div class="huge"><?php echo $nb_unreads; ?></div>
<div>SMS non lus</div>
</div>
</div>
</div>
<a href="<?php echo \descartes\Router::url('Command', 'list') ?>">
<a href="<?php echo \descartes\Router::url('Received', 'list_unread') ?>">
<div class="panel-footer">
<span class="pull-left">Voir les commandes</span>
<span class="pull-left">Voir les SMS non lus</span>
<span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
<div class="clearfix"></div>
</div>

View File

@ -49,7 +49,7 @@
<?php foreach ($discussions as $discussion) { ?>
<tr class="goto" url="<?php $this->s(\descartes\Router::url('Discussion', 'show', ['number' => $discussion['number']])); ?>">
<td><?php $this->s($discussion['at']); ?></td>
<td><?php $this->s(isset($discussion['contact']) ? $discussion['contact'] . ' (' . \controllers\internals\Tool::phone_format($discussion['number']) . ')' : \controllers\internals\Tool::phone_link($discussion['number'])); ?></td>
<td><?php $this->s(isset($discussion['contact']) ? $discussion['contact'] . ' (' . \controllers\internals\Tool::phone_format($discussion['number']) . ')' : \controllers\internals\Tool::phone_format($discussion['number'])); ?></td>
</tr>
<?php } ?>
</tbody>

View File

@ -48,11 +48,11 @@
<li <?php echo $page == 'receiveds' ? 'class="active"' : ''; ?>>
<a href="<?php echo \descartes\Router::url('Received', 'list'); ?>"><i class="fa fa-fw fa-download"></i> SMS reçus</a>
</li>
<li <?php echo $page == 'receiveds_unread' ? 'class="active"' : ''; ?>>
<a href="<?php echo \descartes\Router::url('Received', 'list_unread'); ?>"><i class="fa fa-fw fa-eye-slash"></i> SMS non lus</a>
</li>
</ul>
</li>
<li <?php echo $page == 'commands' ? 'class="active"' : ''; ?>>
<a href="<?php echo \descartes\Router::url('Command', 'list'); ?>"><i class="fa fa-fw fa-terminal"></i> Commandes</a>
</li>
<li>
<a href="javascript:;" data-toggle="collapse" data-target="#repertoire"><i class="fa fa-fw fa-book"></i> Répertoire <i class="fa fa-fw fa-caret-down"></i></a>
<ul id="repertoire" class="collapse <?php echo in_array($page, array('contacts', 'groupes', 'conditional_groupes')) ? 'in' : ''; ?>">
@ -80,6 +80,9 @@
</li>
</ul>
</li>
<li <?php echo $page == 'commands' ? 'class="active"' : ''; ?>>
<a href="<?php echo \descartes\Router::url('Command', 'list'); ?>"><i class="fa fa-fw fa-terminal"></i> Commandes</a>
</li>
<li <?php echo $page == 'phones' ? 'class="active"' : ''; ?>>
<a href="<?php echo \descartes\Router::url('Phone', 'list'); ?>"><i class="fa fa-fw fa-phone"></i> Téléphones</a>
</li>

View File

@ -47,6 +47,7 @@
<th>À</th>
<th>Message</th>
<th>Date</th>
<th>Status</th>
<th>Commande</th>
<?php if ($_SESSION['user']['admin']) { ?><th>Sélectionner</th><?php } ?>
</tr>
@ -59,6 +60,7 @@
<td><?php echo(\controllers\internals\Tool::phone_link($received['destination'])); ?></td>
<td><?php $this->s($received['text']); ?></td>
<td><?php $this->s($received['at']); ?></td>
<td><?php echo ($received['status'] == 'read' ? 'Lu' : 'Non lu'); ?></td>
<td><?php echo $received['command'] ? 'Oui' : 'Non'; ?></td>
<?php if ($_SESSION['user']['admin']) { ?><td><input name="ids[]" type="checkbox" value="<?php $this->s($received['id']); ?>"></td><?php } ?>
</tr>

View File

@ -0,0 +1,81 @@
<?php
//Template dashboard
$this->render('incs/head', ['title' => 'Receiveds - Unread'])
?>
<div id="wrapper">
<?php
$this->render('incs/nav', ['page' => 'receiveds_unread'])
?>
<div id="page-wrapper">
<div class="container-fluid">
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
Dashboard <small>SMS non lus</small>
</h1>
<ol class="breadcrumb">
<li>
<i class="fa fa-dashboard"></i> <a href="<?php echo \descartes\Router::url('Dashboard', 'show'); ?>">Dashboard</a>
</li>
<li class="active">
<i class="fa fa-eye-slash "></i> SMS non lus
</li>
</ol>
</div>
</div>
<!-- /.row -->
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-eye-slash fa-fw"></i> Liste des SMS non lus</h3>
</div>
<div class="panel-body">
<form method="GET">
<?php if (!$receiveds) { ?>
<p>Aucun SMS non lu à afficher.</p>
<?php } else { ?>
<div class="table-receiveds">
<table class="table table-bordered table-hover table-striped" id="table-receiveds">
<thead>
<tr>
<th>#</th>
<th>De</th>
<th>À</th>
<th>Message</th>
<th>Date</th>
<th>Status</th>
<th>Commande</th>
<?php if ($_SESSION['user']['admin']) { ?><th>Sélectionner</th><?php } ?>
</tr>
</thead>
<tbody>
<?php foreach ($receiveds as $received) { ?>
<tr>
<td><?php $this->s($received['id']); ?></td>
<td><?php echo(\controllers\internals\Tool::phone_link($received['origin'])); ?></td>
<td><?php echo(\controllers\internals\Tool::phone_link($received['destination'])); ?></td>
<td><?php $this->s($received['text']); ?></td>
<td><?php $this->s($received['at']); ?></td>
<td><?php echo ($received['status'] == 'read' ? 'Lu' : 'Non lu'); ?></td>
<td><?php echo $received['command'] ? 'Oui' : 'Non'; ?></td>
<?php if ($_SESSION['user']['admin']) { ?><td><input name="ids[]" type="checkbox" value="<?php $this->s($received['id']); ?>"></td><?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php } ?>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
$this->render('incs/footer');