raspisms/controllers/internals/Received.php

217 lines
7.2 KiB
PHP
Raw Normal View History

2019-10-29 14:57:13 +01:00
<?php
/*
2019-11-10 17:48:54 +01:00
* This file is part of RaspiSMS.
*
2019-11-10 17:48:54 +01:00
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
2019-11-10 17:48:54 +01:00
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/
2019-10-29 14:57:13 +01:00
namespace controllers\internals;
2019-10-29 18:36:25 +01:00
class Received extends StandardController
2019-10-29 14:57:13 +01:00
{
2019-11-14 21:44:31 +01:00
protected $model = null;
2019-11-12 17:37:20 +01:00
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
2019-11-14 22:33:00 +01:00
$this->model = $this->model ?? new \models\Received($this->bdd);
return $this->model;
}
2019-12-02 01:55:06 +01:00
/**
* 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
* @param $at : Reception date
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $destination : Number of the receiver
2019-12-02 01:55:06 +01:00
* @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
*/
2019-12-02 01:55:06 +01:00
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,
2019-12-02 01:55:06 +01:00
'status' => $status,
'command' => $command,
];
return (bool) $this->get_model()->insert($received);
}
2019-11-12 17:37:20 +01:00
2019-10-29 14:57:13 +01:00
/**
* Update a received for a user
* @param int $id_user : user id
* @param int $id_received : received id
* @param $at : Reception date
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $destination : Number of the receiver
2019-12-02 01:55:06 +01:00
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
* @return bool : false on error, true on success
*/
2019-12-02 01:55:06 +01:00
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,
2019-12-02 01:55:06 +01:00
'status' => $status,
'command' => $command,
];
return (bool) $this->get_model()->update_for_user($id_user, $id_received, $received);
}
2019-11-12 17:37:20 +01:00
2019-12-02 01:55:06 +01:00
/**
* 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
* @param int $nb_entry : Number of receiveds messages to return
* @return array
*/
public function get_lasts_by_date_for_user(int $id_user, int $nb_entry)
{
return $this->get_model()->get_lasts_by_date_for_user($id_user, $nb_entry);
}
2019-10-29 14:57:13 +01:00
/**
* Return receiveds for an origin and a user
* @param int $id_user : User id
* @param string $origin : Number who sent the message
* @return array
*/
public function gets_by_origin_and_user(int $id_user, string $origin)
{
return $this->get_model()->gets_by_origin_and_user($id_user, $origin);
}
2019-10-29 14:57:13 +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
*/
public function count_by_day_since_for_user(int $id_user, $date)
{
2019-11-14 22:33:00 +01:00
$counts_by_day = $this->get_model()->count_by_day_since_for_user($id_user, $date);
$return = [];
foreach ($counts_by_day as $count_by_day)
{
$return[$count_by_day['at_ymd']] = $count_by_day['nb'];
}
return $return;
}
2019-10-29 14:57:13 +01:00
/**
* Return all discussions (ie : numbers we have a message received from or sended to) for a user
* @param int $id_user : User id
* @return array
*/
public function get_discussions_for_user(int $id_user)
{
return $this->get_model()->get_discussions_for_user($id_user);
}
2019-10-29 14:57:13 +01:00
/**
* Get SMS received since a date for a user
* @param int $id_user : User id
2019-11-14 22:33:00 +01:00
* @param $date : La date depuis laquelle on veux les SMS (au format 2014-10-25 20:10:05)
* @return array : Tableau avec tous les SMS depuis la date
*/
public function get_since_by_date_for_user(int $id_user, $date)
{
return $this->get_model()->get_since_by_date_for_user($id_user, $date);
2019-10-29 14:57:13 +01:00
}
/**
* Find messages received since a date for a certain origin and user
* @param int $id_user : User id
* @param $date : Date we want messages sinces
* @param string $origin : Origin number
* @return array
*/
public function get_since_by_date_for_origin_and_user(int $id_user, $date, string $origin)
{
return $this->get_model()->get_since_by_date_for_origin_and_user($id_user, $date, $origin);
}
2019-10-29 14:57:13 +01:00
}