2 require_once("include/datetime.php");
4 class NotificationsManager {
7 public function __construct() {
11 private function _set_extra($notes) {
13 foreach($notes as $n) {
14 $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
15 $n['timestamp'] = strtotime($local_time);
16 $n['date_rel'] = relative_date($n['date']);
24 * @brief get all notifications for local_user()
26 * @param array $filter optional Array "column name"=>value: filter query by columns values
27 * @param string $order optional Space separated list of column to sort by. prepend name with "+" to sort ASC, "-" to sort DESC. Default to "-date"
28 * @param string $limit optional Query limits
30 * @return array of results or false on errors
32 public function getAll($filter = array(), $order="-date", $limit="") {
33 $filter_str = array();
35 foreach($filter as $column => $value) {
36 $filter_str[] = sprintf("`%s` = '%s'", $column, dbesc($value));
38 if (count($filter_str)>0) {
39 $filter_sql = "AND ".implode(" AND ", $filter_str);
42 $aOrder = explode(" ", $order);
44 foreach($aOrder as $o) {
54 $asOrder[] = "$o $dir";
56 $order_sql = implode(", ", $asOrder);
58 if ($limit!="") $limit = " LIMIT ".$limit;
60 $r = q("SELECT * from notify where uid = %d $filter_sql order by $order_sql $limit",
63 if ($r!==false && count($r)>0) return $this->_set_extra($r);
68 * @brief get one note for local_user() by $id value
71 * @return array note values or null if not found
73 public function getByID($id) {
74 $r = q("select * from notify where id = %d and uid = %d limit 1",
78 if($r!==false && count($r)>0) {
79 return $this->_set_extra($r)[0];
85 * @brief set seen state of $note of local_user()
88 * @param bool $seen optional true or false, default true
89 * @return bool true on success, false on errors
91 public function setSeen($note, $seen = true) {
92 return q("update notify set seen = %d where ( link = '%s' or ( parent != 0 and parent = %d and otype = '%s' )) and uid = %d",
95 intval($note['parent']),
96 dbesc($note['otype']),
102 * @brief set seen state of all notifications of local_user()
104 * @param bool $seen optional true or false. default true
105 * @return bool true on success, false on error
107 public function setAllSeen($seen = true) {
108 return q("update notify set seen = %d where uid = %d",