2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-11-06 20:34:26 +01:00
|
|
|
/*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This file is part of RaspiSMS.
|
2019-11-06 20:34:26 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
2019-11-06 20:34:26 +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-11-06 20:34:26 +01:00
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
namespace models;
|
2019-10-29 18:33:49 +01:00
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette classe gère les accès bdd pour les receivedes.
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
class Received extends StandardModel
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2020-04-02 18:39:40 +02:00
|
|
|
const STATUS_UNREAD = 'unread';
|
|
|
|
const STATUS_READ = 'read';
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2019-12-02 01:55:06 +01:00
|
|
|
/**
|
2020-09-23 03:02:13 +02:00
|
|
|
* Return a list of received messages for a user.
|
2021-01-14 03:32:17 +01:00
|
|
|
* Add a column contact_name and phone_name when available.
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2021-01-14 03:32:17 +01:00
|
|
|
* @param int $id_user : user id
|
2020-09-23 03:02:13 +02:00
|
|
|
* @param ?int $limit : Number of entry to return or null
|
|
|
|
* @param ?int $offset : Number of entry to ignore or null
|
|
|
|
*
|
|
|
|
* @return array
|
2019-12-02 01:55:06 +01:00
|
|
|
*/
|
2020-09-23 03:02:13 +02:00
|
|
|
public function list_for_user(int $id_user, $limit, $offset)
|
2019-12-02 01:55:06 +01:00
|
|
|
{
|
2020-09-23 03:02:13 +02:00
|
|
|
$query = '
|
|
|
|
SELECT received.*, contact.name as contact_name, phone.name as phone_name
|
|
|
|
FROM received
|
|
|
|
LEFT JOIN contact
|
|
|
|
ON contact.number = received.origin
|
|
|
|
AND contact.id_user = received.id_user
|
|
|
|
LEFT JOIN phone
|
|
|
|
ON phone.id = received.id_phone
|
|
|
|
WHERE received.id_user = :id_user
|
|
|
|
';
|
2019-12-02 01:55:06 +01:00
|
|
|
|
2021-01-14 03:32:17 +01:00
|
|
|
if (null !== $limit)
|
2020-09-23 03:02:13 +02:00
|
|
|
{
|
|
|
|
$limit = (int) $limit;
|
|
|
|
|
|
|
|
$query .= ' LIMIT ' . $limit;
|
2021-01-14 03:32:17 +01:00
|
|
|
if (null !== $offset)
|
2020-09-23 03:02:13 +02:00
|
|
|
{
|
|
|
|
$offset = (int) $offset;
|
|
|
|
$query .= ' OFFSET ' . $offset;
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-09-23 03:02:13 +02:00
|
|
|
$params = [
|
|
|
|
'id_user' => $id_user,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
|
|
|
}
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-09-23 03:02:13 +02:00
|
|
|
/**
|
|
|
|
* Return a list of unread received messages for a user.
|
2021-01-14 03:32:17 +01:00
|
|
|
* Add a column contact_name and phone_name when available.
|
2020-09-23 03:02:13 +02:00
|
|
|
*
|
2021-01-14 03:32:17 +01:00
|
|
|
* @param int $id_user : user id
|
2020-09-23 03:02:13 +02:00
|
|
|
* @param ?int $limit : Number of entry to return or null
|
|
|
|
* @param ?int $offset : Number of entry to ignore or null
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function list_unread_for_user(int $id_user, $limit, $offset)
|
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
SELECT received.*, contact.name as contact_name, phone.name as phone_name
|
|
|
|
FROM received
|
|
|
|
LEFT JOIN contact
|
|
|
|
ON contact.number = received.origin
|
|
|
|
AND contact.id_user = received.id_user
|
|
|
|
LEFT JOIN phone
|
|
|
|
ON phone.id = received.id_phone
|
|
|
|
WHERE received.id_user = :id_user
|
|
|
|
AND status = :status
|
|
|
|
';
|
2019-12-02 01:55:06 +01:00
|
|
|
|
2021-01-14 03:32:17 +01:00
|
|
|
if (null !== $limit)
|
2020-09-23 03:02:13 +02:00
|
|
|
{
|
|
|
|
$limit = (int) $limit;
|
|
|
|
|
|
|
|
$query .= ' LIMIT ' . $limit;
|
2021-01-14 03:32:17 +01:00
|
|
|
if (null !== $offset)
|
2020-09-23 03:02:13 +02:00
|
|
|
{
|
|
|
|
$offset = (int) $offset;
|
|
|
|
$query .= ' OFFSET ' . $offset;
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2019-12-02 01:55:06 +01:00
|
|
|
$params = [
|
|
|
|
'id_user' => $id_user,
|
2020-09-23 03:02:13 +02:00
|
|
|
'status' => self::STATUS_UNREAD,
|
2019-12-02 01:55:06 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-12-02 01:55:06 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Count number of unread received sms for user.
|
|
|
|
*
|
2019-12-02 01:55:06 +01:00
|
|
|
* @param int $id_user : user id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-12-02 01:55:06 +01:00
|
|
|
* @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
|
2020-03-05 23:07:07 +01:00
|
|
|
WHERE id_user = :id_user
|
2019-12-02 01:55:06 +01:00
|
|
|
AND status = \'unread\'
|
|
|
|
';
|
|
|
|
|
2019-11-12 17:37:20 +01:00
|
|
|
$params = [
|
|
|
|
'id_user' => $id_user,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params)[0]['nb'] ?? 0;
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return x last receiveds message for a user, order by date.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
2019-11-13 03:24:22 +01:00
|
|
|
* @param int $nb_entry : Number of receiveds messages to return
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2019-11-13 03:24:22 +01:00
|
|
|
*/
|
2019-11-14 02:02:50 +01:00
|
|
|
public function get_lasts_by_date_for_user(int $id_user, int $nb_entry)
|
2019-11-13 03:24:22 +01:00
|
|
|
{
|
|
|
|
$nb_entry = (int) $nb_entry;
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
SELECT *
|
|
|
|
FROM received
|
2020-03-05 23:07:07 +01:00
|
|
|
WHERE id_user = :id_user
|
2019-11-13 03:24:22 +01:00
|
|
|
ORDER BY at ASC
|
2020-01-17 18:47:08 +01:00
|
|
|
LIMIT ' . $nb_entry;
|
2019-11-13 03:24:22 +01:00
|
|
|
|
|
|
|
$params = [
|
|
|
|
'id_user' => $id_user,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return receiveds for an origin and a user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param string $origin : Number who sent the message
|
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-11-15 06:30:23 +01:00
|
|
|
public function gets_by_origin_and_user(int $id_user, string $origin)
|
2019-11-13 03:24:22 +01:00
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
SELECT *
|
|
|
|
FROM received
|
2020-03-05 23:07:07 +01:00
|
|
|
WHERE id_user = :id_user
|
2019-11-13 03:24:22 +01:00
|
|
|
AND origin = :origin
|
|
|
|
';
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'id_user' => $id_user,
|
|
|
|
'origin' => $origin,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
|
|
|
}
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-04-13 02:01:03 +02:00
|
|
|
/**
|
|
|
|
* Return sendeds for an origin and a user since a date.
|
|
|
|
*
|
2021-06-17 00:51:33 +02:00
|
|
|
* @param int $id_user : User id
|
2021-04-13 02:01:03 +02:00
|
|
|
* @param string $since : Date we want messages since
|
2021-06-17 00:51:33 +02:00
|
|
|
* @param string $origin : Number who sent the message
|
2021-04-13 02:01:03 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function gets_since_date_by_origin_and_user(int $id_user, string $since, string $origin)
|
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
SELECT *
|
|
|
|
FROM received
|
|
|
|
WHERE id_user = :id_user
|
|
|
|
AND origin = :origin
|
|
|
|
AND at > :since
|
|
|
|
';
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'id_user' => $id_user,
|
|
|
|
'origin' => $origin,
|
|
|
|
'since' => $since,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
|
|
|
}
|
2019-11-13 03:24:22 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Get number of sended SMS for every date since a date for a specific user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : user id
|
|
|
|
* @param \DateTime $date : Date since which we want the messages
|
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
* @return array
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
2019-11-14 02:02:50 +01:00
|
|
|
public function count_by_day_since_for_user(int $id_user, $date)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2021-01-14 03:32:17 +01:00
|
|
|
$query = "
|
2019-10-29 14:57:13 +01:00
|
|
|
SELECT COUNT(id) as nb, DATE_FORMAT(at, '%Y-%m-%d') as at_ymd
|
|
|
|
FROM received
|
|
|
|
WHERE at > :date
|
2020-03-05 23:07:07 +01:00
|
|
|
AND id_user = :id_user
|
2019-10-29 14:57:13 +01:00
|
|
|
GROUP BY at_ymd
|
|
|
|
";
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
$params = [
|
2019-10-29 14:57:13 +01:00
|
|
|
'date' => $date,
|
2019-11-12 17:37:20 +01:00
|
|
|
'id_user' => $id_user,
|
2019-10-30 00:30:39 +01:00
|
|
|
];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_run_query($query, $params);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return all discussions (ie : numbers we have a message received from or sended to) for a user.
|
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
* @param int $id_user : User id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
* @return array
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
public function get_discussions_for_user(int $id_user)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2021-01-14 03:32:17 +01:00
|
|
|
$query = '
|
2020-09-23 03:02:13 +02:00
|
|
|
SELECT discussions.at, discussions.number, contact.name as contact_name
|
2019-11-13 03:24:22 +01:00
|
|
|
FROM (
|
|
|
|
SELECT at, destination as number FROM sended
|
2020-03-05 23:07:07 +01:00
|
|
|
WHERE id_user = :id_user
|
2019-11-13 03:24:22 +01:00
|
|
|
UNION (
|
|
|
|
SELECT at, origin as number FROM received
|
2020-03-05 23:07:07 +01:00
|
|
|
WHERE id_user = :id_user
|
2019-11-13 03:24:22 +01:00
|
|
|
)
|
|
|
|
) as discussions
|
2020-09-23 03:02:13 +02:00
|
|
|
LEFT JOIN contact
|
|
|
|
ON discussions.number = contact.number AND id_user = :id_user
|
2019-11-10 00:27:42 +01:00
|
|
|
GROUP BY number
|
2019-10-29 14:57:13 +01:00
|
|
|
ORDER BY at DESC
|
2019-10-30 00:30:39 +01:00
|
|
|
';
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
$params = ['id_user' => $id_user];
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
return $this->_run_query($query, $params);
|
2019-10-29 14:57:13 +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
|
|
|
* Get SMS received since a date for a user.
|
|
|
|
*
|
2019-11-12 17:37:20 +01:00
|
|
|
* @param int $id_user : User id
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param $date : La date depuis laquelle on veux les SMS (au format 2014-10-25 20:10:05)
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return array : Tableau avec tous les SMS depuis la date
|
|
|
|
*/
|
2019-11-14 02:02:50 +01:00
|
|
|
public function get_since_by_date_for_user(int $id_user, $date)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2021-01-14 03:32:17 +01:00
|
|
|
$query = "
|
2019-10-29 14:57:13 +01:00
|
|
|
SELECT *
|
|
|
|
FROM received
|
|
|
|
WHERE at > STR_TO_DATE(:date, '%Y-%m-%d %h:%i:%s')
|
2020-03-05 23:07:07 +01:00
|
|
|
AND id_user = :id_user
|
2019-11-12 17:37:20 +01:00
|
|
|
ORDER BY at ASC";
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
$params = [
|
2019-10-29 14:57:13 +01:00
|
|
|
'date' => $date,
|
2019-11-12 17:37:20 +01:00
|
|
|
'id_user' => $id_user,
|
2019-10-30 00:30:39 +01:00
|
|
|
];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_run_query($query, $params);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Find messages received since a date for a certain origin and user.
|
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param $date : Date we want messages sinces
|
|
|
|
* @param string $origin : Origin number
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
* @return array
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
public function get_since_by_date_for_origin_and_user(int $id_user, $date, string $origin)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2021-01-14 03:32:17 +01:00
|
|
|
$query = "
|
2019-10-29 14:57:13 +01:00
|
|
|
SELECT *
|
|
|
|
FROM received
|
|
|
|
WHERE at > STR_TO_DATE(:date, '%Y-%m-%d %h:%i:%s')
|
|
|
|
AND origin = :origin
|
2020-03-05 23:07:07 +01:00
|
|
|
AND id_user = :id_user
|
2019-10-29 14:57:13 +01:00
|
|
|
ORDER BY at ASC
|
|
|
|
";
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
$params = [
|
2019-11-14 22:33:00 +01:00
|
|
|
'id_user' => $id_user,
|
2019-10-29 14:57:13 +01:00
|
|
|
'date' => $date,
|
2019-10-30 00:30:39 +01:00
|
|
|
'origin' => $origin,
|
|
|
|
];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_run_query($query, $params);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2020-01-11 17:27:07 +01:00
|
|
|
|
|
|
|
/**
|
2020-03-31 01:53:07 +02:00
|
|
|
* Find last received message for an origin and user.
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param string $origin : Origin number
|
|
|
|
*
|
2020-01-11 17:27:07 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function get_last_for_origin_and_user(int $id_user, string $origin)
|
2020-01-11 17:27:07 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$query = '
|
2020-01-11 17:27:07 +01:00
|
|
|
SELECT *
|
|
|
|
FROM received
|
|
|
|
WHERE origin = :origin
|
2020-03-05 23:07:07 +01:00
|
|
|
AND id_user = :id_user
|
2020-01-11 17:27:07 +01:00
|
|
|
ORDER BY at DESC
|
|
|
|
LIMIT 0,1
|
2020-01-17 18:19:25 +01:00
|
|
|
';
|
2020-01-11 17:27:07 +01:00
|
|
|
|
|
|
|
$params = [
|
|
|
|
'origin' => $origin,
|
|
|
|
'id_user' => $id_user,
|
|
|
|
];
|
|
|
|
|
|
|
|
$result = $this->_run_query($query, $params);
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
return $result[0] ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return table name.
|
|
|
|
*/
|
|
|
|
protected function get_table_name(): string
|
|
|
|
{
|
|
|
|
return 'received';
|
2020-01-11 17:27:07 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|