3 * @file include/NotificationsManager.php
5 require_once('include/html2plain.php');
6 require_once("include/datetime.php");
7 require_once("include/bbcode.php");
10 * @brief Read and write notifications from/to database
12 class NotificationsManager {
15 public function __construct() {
20 * @brief set some extra note properties
22 * @param array $notes array of note arrays from db
23 * @return array Copy of input array with added properties
25 * Set some extra properties to note array from db:
26 * - timestamp as int in default TZ
27 * - date_rel : relative date string
28 * - msg_html: message as html string
29 * - msg_plain: message as plain text string
31 private function _set_extra($notes) {
33 foreach($notes as $n) {
34 $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
35 $n['timestamp'] = strtotime($local_time);
36 $n['date_rel'] = relative_date($n['date']);
37 $n['msg_html'] = bbcode($n['msg'], false, false, false, false);
38 $n['msg_plain'] = explode("\n",trim(html2plain($n['msg_html'], 0)))[0];
47 * @brief get all notifications for local_user()
49 * @param array $filter optional Array "column name"=>value: filter query by columns values
50 * @param string $order optional Space separated list of column to sort by. prepend name with "+" to sort ASC, "-" to sort DESC. Default to "-date"
51 * @param string $limit optional Query limits
53 * @return array of results or false on errors
55 public function getAll($filter = array(), $order="-date", $limit="") {
56 $filter_str = array();
58 foreach($filter as $column => $value) {
59 $filter_str[] = sprintf("`%s` = '%s'", $column, dbesc($value));
61 if (count($filter_str)>0) {
62 $filter_sql = "AND ".implode(" AND ", $filter_str);
65 $aOrder = explode(" ", $order);
67 foreach($aOrder as $o) {
77 $asOrder[] = "$o $dir";
79 $order_sql = implode(", ", $asOrder);
81 if ($limit!="") $limit = " LIMIT ".$limit;
83 $r = q("SELECT * FROM `notify` WHERE `uid` = %d $filter_sql ORDER BY $order_sql $limit",
86 if ($r!==false && count($r)>0) return $this->_set_extra($r);
91 * @brief get one note for local_user() by $id value
94 * @return array note values or null if not found
96 public function getByID($id) {
97 $r = q("SELECT * FROM `notify` WHERE `id` = %d AND `uid` = %d LIMIT 1",
101 if($r!==false && count($r)>0) {
102 return $this->_set_extra($r)[0];
108 * @brief set seen state of $note of local_user()
111 * @param bool $seen optional true or false, default true
112 * @return bool true on success, false on errors
114 public function setSeen($note, $seen = true) {
115 return q("UPDATE `notify` SET `seen` = %d WHERE ( `link` = '%s' OR ( `parent` != 0 AND `parent` = %d AND `otype` = '%s' )) AND `uid` = %d",
117 dbesc($note['link']),
118 intval($note['parent']),
119 dbesc($note['otype']),
125 * @brief set seen state of all notifications of local_user()
127 * @param bool $seen optional true or false. default true
128 * @return bool true on success, false on error
130 public function setAllSeen($seen = true) {
131 return q("UPDATE `notify` SET `seen` = %d WHERE `uid` = %d",