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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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 sendedes.
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
class Sended extends StandardModel
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2020-04-02 01:55:55 +02:00
|
|
|
const STATUS_UNKNOWN = 'unknown';
|
|
|
|
const STATUS_DELIVERED = 'delivered';
|
|
|
|
const STATUS_FAILED = 'failed';
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-09-23 03:02:13 +02:00
|
|
|
/**
|
|
|
|
* Return a list of sended 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
|
|
|
|
*/
|
2021-07-14 04:38:52 +02:00
|
|
|
public function datatable_list_for_user(int $id_user, ?int $limit = null, ?int $offset = null, ?string $search = null, ?array $search_columns = [], ?string $order_column = null, bool $order_desc = false, ?bool $count = false)
|
2020-09-23 03:02:13 +02:00
|
|
|
{
|
2021-07-14 04:38:52 +02:00
|
|
|
$params = [
|
|
|
|
'id_user' => $id_user,
|
|
|
|
];
|
2020-09-23 03:02:13 +02:00
|
|
|
|
2021-07-14 04:38:52 +02:00
|
|
|
$query = $count ? 'SELECT COUNT(*) as nb' : 'SELECT * ';
|
|
|
|
$query .= '
|
|
|
|
FROM (
|
|
|
|
SELECT sended.*, contact.name as contact_name, phone.name as phone_name, IF(contact.name IS NULL, sended.destination, CONCAT(sended.destination, " (", contact.name, ")")) as searchable_destination
|
|
|
|
FROM sended
|
|
|
|
LEFT JOIN contact
|
|
|
|
ON contact.number = sended.destination
|
|
|
|
AND contact.id_user = sended.id_user
|
|
|
|
LEFT JOIN phone
|
|
|
|
ON phone.id = sended.id_phone
|
|
|
|
WHERE sended.id_user = :id_user
|
|
|
|
) as results
|
2021-07-19 17:32:23 +02:00
|
|
|
';
|
2021-07-14 04:38:52 +02:00
|
|
|
|
|
|
|
if ($search && $search_columns)
|
|
|
|
{
|
|
|
|
$like_search = '%' . str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], $search) . '%';
|
|
|
|
$params[':like_search'] = $like_search;
|
2021-07-19 17:32:23 +02:00
|
|
|
|
2021-07-14 04:38:52 +02:00
|
|
|
$query .= ' WHERE (0';
|
2021-07-19 17:32:23 +02:00
|
|
|
|
2021-07-14 04:38:52 +02:00
|
|
|
foreach ($search_columns as $column)
|
|
|
|
{
|
|
|
|
$query .= ' OR ' . $column . ' LIKE :like_search';
|
|
|
|
}
|
|
|
|
|
|
|
|
$query .= ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order_column)
|
|
|
|
{
|
|
|
|
$query .= ' ORDER BY ' . $order_column . ($order_desc ? ' DESC' : ' ASC');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $limit && !$count)
|
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
|
|
|
|
2021-07-19 17:32:23 +02:00
|
|
|
return $count ? $this->_run_query($query, $params)[0]['nb'] ?? 0 : $this->_run_query($query, $params);
|
2020-09-23 03:02:13 +02:00
|
|
|
}
|
2020-04-02 01:55:55 +02:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return x last sendeds 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 sendeds messages to return
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
public function get_lasts_by_date_for_user($id_user, $nb_entry)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-13 03:24:22 +01:00
|
|
|
$nb_entry = (int) $nb_entry;
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
SELECT *
|
|
|
|
FROM sended
|
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);
|
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
|
|
|
* Return sendeds for an destination and a user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
2019-11-13 03:24:22 +01:00
|
|
|
* @param string $destination : Number who sent the message
|
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-15 06:30:23 +01:00
|
|
|
public function gets_by_destination_and_user(int $id_user, string $destination)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-13 03:24:22 +01:00
|
|
|
$query = '
|
|
|
|
SELECT *
|
|
|
|
FROM sended
|
2020-03-05 23:07:07 +01:00
|
|
|
WHERE id_user = :id_user
|
2019-11-13 03:24:22 +01:00
|
|
|
AND destination = :destination
|
|
|
|
';
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'id_user' => $id_user,
|
|
|
|
'destination' => $destination,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-04-13 02:01:03 +02:00
|
|
|
/**
|
|
|
|
* Return sendeds for an destination and a user since a date.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
2021-06-17 00:51:33 +02:00
|
|
|
* @param string $since : Date we want messages since
|
2021-04-13 02:01:03 +02:00
|
|
|
* @param string $destination : Number who sent the message
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function gets_since_date_by_destination_and_user(int $id_user, string $since, string $destination)
|
|
|
|
{
|
|
|
|
$query = '
|
|
|
|
SELECT *
|
|
|
|
FROM sended
|
|
|
|
WHERE id_user = :id_user
|
|
|
|
AND destination = :destination
|
|
|
|
AND at > :since
|
|
|
|
';
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'id_user' => $id_user,
|
|
|
|
'destination' => $destination,
|
|
|
|
'since' => $since,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return sended for an uid and an adapter.
|
|
|
|
*
|
2020-06-23 21:06:13 +02:00
|
|
|
* @param int $id_user : Id of the user
|
2020-01-17 18:19:25 +01:00
|
|
|
* @param string $uid : Uid of the sended
|
2020-01-08 02:14:38 +01:00
|
|
|
* @param string $adapter : Adapter used to send the message
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-01-08 02:14:38 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-03-05 23:07:07 +01:00
|
|
|
public function get_by_uid_and_adapter_for_user(int $id_user, string $uid, string $adapter)
|
2020-01-08 02:14:38 +01:00
|
|
|
{
|
2020-03-05 23:07:07 +01:00
|
|
|
return $this->_select_one('sended', ['id_user' => $id_user, 'uid' => $uid, 'adapter' => $adapter]);
|
2020-01-08 02:14:38 +01:00
|
|
|
}
|
2019-11-13 03:24:22 +01:00
|
|
|
|
2023-02-06 03:42:03 +01:00
|
|
|
/**
|
|
|
|
* Get number of sended SMS since a date for a phone
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param int $id_phone : Phone id we want the number of sended message for
|
2023-02-24 16:29:10 +01:00
|
|
|
* @param ?\DateTime $since : Date since which we want sended Number. Default to null.
|
|
|
|
* @param ?\DateTime $before : Date up to which we want sended number. Default to null.
|
|
|
|
* @param ?string $tag_like : Tag to filter sms by, this is not a = but a LIKE operator
|
2023-02-06 03:42:03 +01:00
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2023-02-24 16:29:10 +01:00
|
|
|
public function count_since_for_phone_and_user(int $id_user, int $id_phone, ?\DateTime $since = null, ?\DateTime $before = null, ?string $tag_like = null) : int
|
2023-02-06 03:42:03 +01:00
|
|
|
{
|
2023-02-24 16:29:10 +01:00
|
|
|
$data = [
|
|
|
|
'id_user' => $id_user,
|
|
|
|
'id_phone' => $id_phone,
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($since)
|
|
|
|
{
|
|
|
|
$data['>=at'] = $since->format('c');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($before)
|
|
|
|
{
|
|
|
|
$data['<=at'] = $before->format('c');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($tag_like)
|
|
|
|
{
|
|
|
|
$data['%tag'] = $tag_like;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_count('sended', $data);
|
2023-02-06 03:42:03 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 14:57:13 +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
|
|
|
*/
|
2023-05-30 18:05:38 +02:00
|
|
|
public function count_by_day_and_status_since_for_user($id_user, $date)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2021-01-14 03:32:17 +01:00
|
|
|
$query = "
|
2023-05-30 18:05:38 +02:00
|
|
|
SELECT COUNT(id) as nb, status, DATE_FORMAT(at, '%Y-%m-%d') as at_ymd
|
2019-10-29 14:57:13 +01:00
|
|
|
FROM sended
|
|
|
|
WHERE at > :date
|
2020-03-05 23:07:07 +01:00
|
|
|
AND id_user = :id_user
|
2023-05-30 18:05:38 +02:00
|
|
|
GROUP BY at_ymd, status
|
2019-10-29 14:57:13 +01:00
|
|
|
";
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
$params = [
|
2019-10-29 14:57:13 +01:00
|
|
|
'date' => $date,
|
2019-11-13 03:24:22 +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-11-07 16:17:18 +01:00
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Get SMS sended since a date for a user.
|
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
* @param $date : La date depuis laquelle on veux les SMS (au format 2014-10-25 20:10:05)
|
|
|
|
* @param int $id_user : User id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
* @return array : Tableau avec tous les SMS depuis la date
|
|
|
|
*/
|
|
|
|
public function get_since_by_date_for_user($date, $id_user)
|
|
|
|
{
|
2021-01-14 03:32:17 +01:00
|
|
|
$query = "
|
2019-11-13 03:24:22 +01:00
|
|
|
SELECT *
|
|
|
|
FROM sended
|
|
|
|
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-13 03:24:22 +01:00
|
|
|
ORDER BY at ASC";
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
$params = [
|
|
|
|
'date' => $date,
|
|
|
|
'id_user' => $id_user,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-11 17:27:07 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Find last sended message for a destination and user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
2020-01-11 17:27:07 +01:00
|
|
|
* @param string $destination : Destination number
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-01-11 17:27:07 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function get_last_for_destination_and_user(int $id_user, string $destination)
|
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 sended
|
|
|
|
WHERE destination = :destination
|
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 = [
|
|
|
|
'destination' => $destination,
|
|
|
|
'id_user' => $id_user,
|
|
|
|
];
|
|
|
|
|
|
|
|
$result = $this->_run_query($query, $params);
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
return $result[0] ?? [];
|
2020-01-11 17:27:07 +01:00
|
|
|
}
|
2019-11-13 03:24:22 +01:00
|
|
|
|
2024-06-25 21:12:48 +02:00
|
|
|
/**
|
|
|
|
* Get number of sended SMS by day and status between two dates, possibly by sending phone.
|
|
|
|
*
|
|
|
|
* @param int $id_user : user id
|
|
|
|
* @param \DateTime $start_date : Date since which we want the messages
|
|
|
|
* @param \DateTime $end_date : Date until which we want the messages
|
|
|
|
* @param ?int $id_phone : Id of the phone to search sended for, null by default get all phones
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_sended_status_stats ($id_user, $start_date, $end_date, ?int $id_phone = null)
|
|
|
|
{
|
|
|
|
$params = [
|
|
|
|
'start_date' => $start_date->format('y-m-d H:i:s'),
|
|
|
|
'end_date' => $end_date->format('y-m-d H:i:s'),
|
|
|
|
'id_user' => $id_user,
|
|
|
|
];
|
|
|
|
|
|
|
|
$query = "
|
|
|
|
SELECT DATE_FORMAT(at, '%Y-%m-%d') as at_ymd, id_phone, status, COUNT(id) as nb
|
|
|
|
FROM sended
|
|
|
|
WHERE id_user = :id_user
|
|
|
|
AND id_phone IS NOT NULL
|
|
|
|
AND at >= :start_date
|
|
|
|
AND at <= :end_date
|
|
|
|
";
|
|
|
|
|
|
|
|
if ($id_phone)
|
|
|
|
{
|
|
|
|
$params['id_phone'] = $id_phone;
|
|
|
|
$query .= "
|
|
|
|
AND id_phone = :id_phone
|
|
|
|
";
|
|
|
|
}
|
|
|
|
|
|
|
|
$query .= "
|
|
|
|
GROUP BY at_ymd, status, id_phone
|
|
|
|
ORDER BY at_ymd, id_phone, status
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
|
|
|
}
|
|
|
|
|
2024-10-28 21:35:01 +01:00
|
|
|
/**
|
|
|
|
* Get list of invalid phone number we've sent message to
|
|
|
|
*
|
|
|
|
* @param int $id_user : user id
|
|
|
|
* @param int $volume : Minimum number of sms sent to the number
|
|
|
|
* @param float $percent_failed : Minimum ratio of failed message
|
|
|
|
* @param float $percent_unknown : Minimum ratio of unknown message
|
|
|
|
* @param int $limit : Limit of results
|
|
|
|
* @param int $page : Page of results (offset = page * limit)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function get_invalid_numbers (int $id_user, int $volume, float $percent_failed, float $percent_unknown, int $limit, int $page)
|
|
|
|
{
|
|
|
|
$query = "
|
|
|
|
SELECT
|
|
|
|
destination,
|
|
|
|
COUNT(*) AS total_sms_sent,
|
|
|
|
ROUND(SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) / COUNT(*), 2) AS failed_percentage,
|
|
|
|
ROUND(SUM(CASE WHEN status = 'unknown' THEN 1 ELSE 0 END) / COUNT(*), 2) AS unknown_percentage
|
|
|
|
FROM
|
|
|
|
sended
|
|
|
|
GROUP BY
|
|
|
|
destination
|
|
|
|
HAVING
|
|
|
|
total_sms_sent >= :volume
|
|
|
|
AND failed_percentage >= :percent_failed
|
|
|
|
AND unknown_percentage >= :percent_unknown
|
|
|
|
LIMIT " . intval($page * $limit) . "," . intval($limit) . "
|
|
|
|
";
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'volume' => $volume,
|
|
|
|
'percent_failed' => $percent_failed,
|
|
|
|
'percent_unknown' => $percent_unknown
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
|
|
|
}
|
|
|
|
|
2024-10-26 18:02:11 +02:00
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
/**
|
|
|
|
* Return table name.
|
|
|
|
*/
|
|
|
|
protected function get_table_name(): string
|
|
|
|
{
|
|
|
|
return 'sended';
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|