3 * @file include/NotificationsManager.php
5 require_once("include/datetime.php");
6 require_once("include/bbcode.php");
9 * @brief Read and write notifications from/to database
11 class NotificationsManager {
14 public function __construct() {
19 * @brief set some extra note properties
21 * @param array $notes array of note arrays from db
22 * @return array Copy of input array with added properties
24 * Set some extra properties to note array from db:
25 * - timestamp as int in default TZ
26 * - date_rel : relative date string
27 * - msg_html: message as html string
28 * - msg_plain: message as plain text string
30 private function _set_extra($notes) {
32 foreach($notes as $n) {
33 $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
34 $n['timestamp'] = strtotime($local_time);
35 $n['date_rel'] = relative_date($n['date']);
36 $n['msg_html'] = bbcode($n['msg'], false, false, false, false);
37 $n['msg_plain'] = explode("\n",trim(html2plain($n['msg_html'], 0)))[0];
46 * @brief get all notifications for local_user()
48 * @param array $filter optional Array "column name"=>value: filter query by columns values
49 * @param string $order optional Space separated list of column to sort by. prepend name with "+" to sort ASC, "-" to sort DESC. Default to "-date"
50 * @param string $limit optional Query limits
52 * @return array of results or false on errors
54 public function getAll($filter = array(), $order="-date", $limit="") {
55 $filter_str = array();
57 foreach($filter as $column => $value) {
58 $filter_str[] = sprintf("`%s` = '%s'", $column, dbesc($value));
60 if (count($filter_str)>0) {
61 $filter_sql = "AND ".implode(" AND ", $filter_str);
64 $aOrder = explode(" ", $order);
66 foreach($aOrder as $o) {
76 $asOrder[] = "$o $dir";
78 $order_sql = implode(", ", $asOrder);
80 if ($limit!="") $limit = " LIMIT ".$limit;
82 $r = q("SELECT * FROM notify WHERE uid = %d $filter_sql ORDER BY $order_sql $limit",
85 if ($r!==false && count($r)>0) return $this->_set_extra($r);
90 * @brief get one note for local_user() by $id value
93 * @return array note values or null if not found
95 public function getByID($id) {
96 $r = q("SELECT * FROM notify WHERE id = %d AND uid = %d LIMIT 1",
100 if($r!==false && count($r)>0) {
101 return $this->_set_extra($r)[0];
107 * @brief set seen state of $note of local_user()
110 * @param bool $seen optional true or false, default true
111 * @return bool true on success, false on errors
113 public function setSeen($note, $seen = true) {
114 return q("UPDATE notify SET seen = %d WHERE ( link = '%s' OR ( parent != 0 AND parent = %d AND otype = '%s' )) AND uid = %d",
116 dbesc($note['link']),
117 intval($note['parent']),
118 dbesc($note['otype']),
124 * @brief set seen state of all notifications of local_user()
126 * @param bool $seen optional true or false. default true
127 * @return bool true on success, false on error
129 public function setAllSeen($seen = true) {
130 return q("UPDATE notify SET seen = %d WHERE uid = %d",