]> git.mxchange.org Git - friendica.git/blob - include/NotificationsManager.php
8b0ca9e13de48ae1c1ac62a543a32c20ce624f0b
[friendica.git] / include / NotificationsManager.php
1 <?php
2 require_once("include/datetime.php");
3
4 class NotificationsManager {
5     private $a;
6     
7     public function __construct() {
8         $this->a = get_app();
9     }
10     
11     private function _set_extra($notes) {
12         $rets = array();
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']);
17             $rets[] = $n;
18         }
19         return $rets;
20     }
21
22
23     /**
24      * @brief get all notifications for local_user()
25      *
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
29      *
30      * @return array of results or false on errors
31      */
32     public function getAll($filter = array(), $order="-date", $limit="") {
33         $filter_str = array();
34         $filter_sql = "";
35         foreach($filter as $column => $value) {
36             $filter_str[] = sprintf("`%s` = '%s'", $column, dbesc($value));
37         }
38         if (count($filter_str)>0) {
39             $filter_sql = "AND ".implode(" AND ", $filter_str);
40         }
41         
42         $aOrder = explode(" ", $order);
43         $asOrder = array();
44         foreach($aOrder as $o) {
45             $dir = "asc";
46             if ($o[0]==="-") {
47                 $dir = "desc";
48                 $o = substr($o,1);
49             }
50             if ($o[0]==="+") {
51                 $dir = "asc";
52                 $o = substr($o,1);
53             }
54             $asOrder[] = "$o $dir";
55         }
56         $order_sql = implode(", ", $asOrder);
57         
58         if ($limit!="") $limit = " LIMIT ".$limit;
59         
60                 $r = q("SELECT * from notify where uid = %d $filter_sql order by $order_sql $limit",
61                         intval(local_user())
62                 );
63         if ($r!==false && count($r)>0) return $this->_set_extra($r);
64         return false;
65     }
66     
67     /**
68      * @brief get one note for local_user() by $id value
69      *
70      * @param int $id
71      * @return array note values or null if not found
72      */
73     public function getByID($id) {
74         $r = q("select * from notify where id = %d and uid = %d limit 1",
75                 intval($id),
76                 intval(local_user())
77         );
78         if($r!==false && count($r)>0) {
79             return $this->_set_extra($r)[0];
80         }
81         return null;
82     }
83     
84     /**
85      * @brief set seen state of $note of local_user()
86      *
87      * @param array $note
88      * @param bool $seen optional true or false, default true
89      * @return bool true on success, false on errors
90      */
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",
93             intval($seen),
94             dbesc($note['link']),
95             intval($note['parent']),
96             dbesc($note['otype']),
97             intval(local_user())
98         );
99     }
100        
101     /**
102      * @brief set seen state of all notifications of local_user()
103      *
104      * @param bool $seen optional true or false. default true
105      * @return bool true on success, false on error
106      */
107     public function setAllSeen($seen = true) {
108         return q("update notify set seen = %d where uid = %d",
109             intval($seen),
110                         intval(local_user())
111                 );
112     }
113 }