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
|
|
|
|
{
|
|
|
|
|
/**
|
2019-11-13 03:24:22 +01:00
|
|
|
|
* Return table name
|
|
|
|
|
* @return string
|
2019-10-29 14:57:13 +01:00
|
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
|
protected function get_table_name() : string { return 'sended'; }
|
|
|
|
|
|
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
|
/**
|
2019-11-13 03:24:22 +01:00
|
|
|
|
* Return a list of sended for a user
|
2019-11-12 17:58:07 +01:00
|
|
|
|
* @param int $id_user : User id
|
|
|
|
|
* @param int $limit : Max results to return
|
|
|
|
|
* @param int $offset : Number of results to ignore
|
2019-10-29 18:33:49 +01:00
|
|
|
|
*/
|
2019-11-12 17:58:07 +01:00
|
|
|
|
public function list_for_user($id_user, $limit, $offset)
|
2019-10-29 14:57:13 +01:00
|
|
|
|
{
|
2019-11-12 17:58:07 +01:00
|
|
|
|
$limit = (int) $limit;
|
|
|
|
|
$offset = (int) $offset;
|
|
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
|
SELECT * FROM sended
|
|
|
|
|
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
|
|
|
|
LIMIT ' . $limit . ' OFFSET ' . $offset;
|
|
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
|
'id_user' => $id_user,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
2019-10-29 18:33:49 +01:00
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
|
/**
|
2019-11-13 03:24:22 +01:00
|
|
|
|
* Return a list of sendeds in a group of ids and for a user
|
|
|
|
|
* @param int $id_user : user id
|
|
|
|
|
* @param array $ids : ids of sendeds to find
|
|
|
|
|
* @return array
|
2019-10-29 18:33:49 +01:00
|
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
|
public function gets_in_for_user(int $id_user, $ids)
|
2019-10-29 14:57:13 +01:00
|
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
|
$query = '
|
2019-10-29 14:57:13 +01:00
|
|
|
|
SELECT * FROM sended
|
2019-11-13 03:24:22 +01:00
|
|
|
|
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
|
|
|
|
AND id ';
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
|
//On génère la clause IN et les paramètres adaptés depuis le tableau des id
|
|
|
|
|
$generated_in = $this->_generate_in_from_array($ids);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
$query .= $generated_in['QUERY'];
|
|
|
|
|
$params = $generated_in['PARAMS'];
|
2019-11-13 03:24:22 +01:00
|
|
|
|
$params['id_user'] = $id_user;
|
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-13 03:24:22 +01:00
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
|
/**
|
2019-11-13 03:24:22 +01:00
|
|
|
|
* Delete a entry by his id for a user
|
|
|
|
|
* @param int $id_user : User id
|
|
|
|
|
* @param int $id : Entry id
|
|
|
|
|
* @return int : Number of removed rows
|
2019-10-29 14:57:13 +01:00
|
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
|
public function delete_for_user(int $id_user, int $id)
|
2019-10-29 14:57:13 +01:00
|
|
|
|
{
|
2019-11-13 03:24:22 +01:00
|
|
|
|
$query = '
|
|
|
|
|
DELETE FROM sended
|
|
|
|
|
WHERE id = :id
|
|
|
|
|
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
|
|
|
|
';
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
|
$params = ['id_user' => $id_user, 'id' => $id];
|
|
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params, self::ROWCOUNT);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
|
/**
|
2019-11-13 03:24:22 +01:00
|
|
|
|
* Update a sended sms for a user
|
|
|
|
|
* @param int $id_user : User id
|
|
|
|
|
* @param int $id : Entry id
|
|
|
|
|
* @param array $datas : datas to update
|
2019-10-30 00:30:39 +01:00
|
|
|
|
*
|
2019-11-13 03:24:22 +01:00
|
|
|
|
* @return int : number of modified rows
|
2019-10-29 18:33:49 +01:00
|
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
|
public function update_for_user (int $id_user, int $id, array $datas)
|
2019-10-29 14:57:13 +01:00
|
|
|
|
{
|
2019-11-13 03:24:22 +01:00
|
|
|
|
$params = [];
|
|
|
|
|
$sets = [];
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
|
foreach ($datas as $label => $value)
|
|
|
|
|
{
|
|
|
|
|
$label = preg_replace('#[^a-zA-Z0-9_]#', '', $label);
|
|
|
|
|
$params['set_' . $label] = $value;
|
|
|
|
|
$sets[] = '`' . $label . '` = :set_' . $label . ' ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
|
UPDATE `sended`
|
|
|
|
|
SET ' . implode(', ', $sets) . '
|
|
|
|
|
WHERE id = :id
|
|
|
|
|
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
//If try to update origin, also check it does belong to user
|
|
|
|
|
if ($sets['set_origin'] ?? false)
|
|
|
|
|
{
|
|
|
|
|
$query .= ' AND :set_origin IN (SELECT number FROM phone WHERE id_user = :id_user)'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$params['id'] = $id;
|
|
|
|
|
$params['id_user'] = $id_user;
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
|
return $this->_run_query($query, $params, self::ROWCOUNT);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
}
|
2019-11-13 03:24:22 +01:00
|
|
|
|
|
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
|
/**
|
2019-11-13 03:24:22 +01:00
|
|
|
|
* Count number of sended sms for user
|
|
|
|
|
* @param int $id_user : user id
|
|
|
|
|
* @return int : Number of sended SMS for user
|
2019-10-29 14:57:13 +01:00
|
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
|
public function count_for_user($id_user)
|
2019-10-29 14:57:13 +01:00
|
|
|
|
{
|
2019-11-13 03:24:22 +01:00
|
|
|
|
$query = '
|
|
|
|
|
SELECT COUNT(id) as nb
|
|
|
|
|
FROM sended
|
|
|
|
|
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
|
|
|
|
';
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
|
$params = [
|
|
|
|
|
'id_user' => $id_user,
|
|
|
|
|
];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
|
return $this->_run_query($query, $params)[0]['nb'] ?? 0;
|
2019-10-29 14:57:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
|
/**
|
2019-11-13 03:24:22 +01:00
|
|
|
|
* Return x last sendeds message for a user, order by date
|
|
|
|
|
* @param int $id_user : User id
|
|
|
|
|
* @param int $nb_entry : Number of sendeds messages to return
|
|
|
|
|
* @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
|
|
|
|
|
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
|
|
|
|
ORDER BY at ASC
|
|
|
|
|
LIMIT ' . $nb_entry;
|
|
|
|
|
|
|
|
|
|
$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-11-13 03:24:22 +01:00
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
|
/**
|
2019-11-13 03:24:22 +01:00
|
|
|
|
* Return sendeds for an destination and a user
|
|
|
|
|
* @param int $id_user : User id
|
|
|
|
|
* @param string $destination : Number who sent the message
|
|
|
|
|
* @return array
|
2019-10-29 14:57:13 +01:00
|
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
|
public function gets_by_destination_for_user(int $id_user, string $destination)
|
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
|
|
|
|
|
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
|
|
|
|
AND destination = :destination
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
|
'id_user' => $id_user,
|
|
|
|
|
'destination' => $destination,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
|
/**
|
2019-11-13 03:24:22 +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
|
|
|
|
|
* @return array
|
2019-10-29 18:33:49 +01:00
|
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
|
public function count_by_day_since_for_user($id_user, $date)
|
2019-10-29 14:57:13 +01:00
|
|
|
|
{
|
|
|
|
|
$query = "
|
|
|
|
|
SELECT COUNT(id) as nb, DATE_FORMAT(at, '%Y-%m-%d') as at_ymd
|
|
|
|
|
FROM sended
|
|
|
|
|
WHERE at > :date
|
2019-11-13 03:24:22 +01:00
|
|
|
|
AND origin IN (SELECT number FROM phone WHERE 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-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
|
|
|
|
* Return all discussions (ie : numbers we have a message sended from or sended to) for a user
|
|
|
|
|
* @param int $id_user : User id
|
|
|
|
|
* @return array
|
2019-11-07 16:17:18 +01:00
|
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
|
public function get_discussions_for_user(int $id_user)
|
2019-11-07 16:17:18 +01:00
|
|
|
|
{
|
|
|
|
|
$query = '
|
2019-11-13 03:24:22 +01:00
|
|
|
|
SELECT at, number
|
|
|
|
|
FROM (
|
|
|
|
|
SELECT at, origin as number FROM sended
|
|
|
|
|
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
|
|
|
|
UNION (
|
|
|
|
|
SELECT at, destination as number FROM sended
|
|
|
|
|
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
|
|
|
|
)
|
|
|
|
|
) as discussions
|
|
|
|
|
GROUP BY number
|
|
|
|
|
ORDER BY at DESC
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
$params = ['id_user' => $id_user];
|
|
|
|
|
return $this->_run_query($query, $params);
|
|
|
|
|
}
|
2019-11-07 16:17:18 +01:00
|
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
|
/**
|
|
|
|
|
* Get SMS sended since a date for a user
|
|
|
|
|
* @param $date : La date depuis laquelle on veux les SMS (au format 2014-10-25 20:10:05)
|
|
|
|
|
* @param int $id_user : User id
|
|
|
|
|
* @return array : Tableau avec tous les SMS depuis la date
|
|
|
|
|
*/
|
|
|
|
|
public function get_since_by_date_for_user($date, $id_user)
|
|
|
|
|
{
|
|
|
|
|
$query = "
|
|
|
|
|
SELECT *
|
|
|
|
|
FROM sended
|
|
|
|
|
WHERE at > STR_TO_DATE(:date, '%Y-%m-%d %h:%i:%s')
|
|
|
|
|
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
|
|
|
|
ORDER BY at ASC";
|
|
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
|
'date' => $date,
|
|
|
|
|
'id_user' => $id_user,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find messages sended since a date for a certain destination and user
|
|
|
|
|
* @param int $id_user : User id
|
|
|
|
|
* @param $date : Date we want messages sinces
|
|
|
|
|
* @param string $destination : Origin number
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function get_since_by_date_for_destination_and_user(int $id_user, $date, string $destination)
|
|
|
|
|
{
|
|
|
|
|
$query = "
|
|
|
|
|
SELECT *
|
|
|
|
|
FROM sended
|
|
|
|
|
WHERE at > STR_TO_DATE(:date, '%Y-%m-%d %h:%i:%s')
|
|
|
|
|
AND destination = :destination
|
|
|
|
|
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
|
|
|
|
ORDER BY at ASC
|
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
|
'id_user' => $id_user
|
|
|
|
|
'date' => $date,
|
|
|
|
|
'destination' => $destination,
|
|
|
|
|
];
|
2019-11-07 16:17:18 +01:00
|
|
|
|
|
|
|
|
|
return $this->_run_query($query, $params);
|
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
}
|