]> git.mxchange.org Git - friendica.git/blob - include/NotificationsManager.php
add docs, rewrite part of the notification api
[friendica.git] / include / NotificationsManager.php
1 <?php
2 /**
3  * @file include/NotificationsManager.php
4  */
5 require_once("include/datetime.php");
6 require_once("include/bbcode.php");
7
8 /**
9  * @brief Read and write notifications from/to database
10  */
11 class NotificationsManager {
12     private $a;
13     
14     public function __construct() {
15         $this->a = get_app();
16     }
17     
18         /**
19          * @brief set some extra note properties
20          *
21          * @param array $notes array of note arrays from db
22          * @return array Copy of input array with added properties
23          * 
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
29          */
30     private function _set_extra($notes) {
31         $rets = array();
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];
38                         
39             $rets[] = $n;
40         }
41         return $rets;
42     }
43
44
45     /**
46      * @brief get all notifications for local_user()
47      *
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
51      *
52      * @return array of results or false on errors
53      */
54     public function getAll($filter = array(), $order="-date", $limit="") {
55         $filter_str = array();
56         $filter_sql = "";
57         foreach($filter as $column => $value) {
58             $filter_str[] = sprintf("`%s` = '%s'", $column, dbesc($value));
59         }
60         if (count($filter_str)>0) {
61             $filter_sql = "AND ".implode(" AND ", $filter_str);
62         }
63         
64         $aOrder = explode(" ", $order);
65         $asOrder = array();
66         foreach($aOrder as $o) {
67             $dir = "asc";
68             if ($o[0]==="-") {
69                 $dir = "desc";
70                 $o = substr($o,1);
71             }
72             if ($o[0]==="+") {
73                 $dir = "asc";
74                 $o = substr($o,1);
75             }
76             $asOrder[] = "$o $dir";
77         }
78         $order_sql = implode(", ", $asOrder);
79         
80         if ($limit!="") $limit = " LIMIT ".$limit;
81         
82                 $r = q("SELECT * FROM notify WHERE uid = %d $filter_sql ORDER BY $order_sql $limit",
83                         intval(local_user())
84                 );
85         if ($r!==false && count($r)>0) return $this->_set_extra($r);
86         return false;
87     }
88     
89     /**
90      * @brief get one note for local_user() by $id value
91      *
92      * @param int $id
93      * @return array note values or null if not found
94      */
95     public function getByID($id) {
96         $r = q("SELECT * FROM notify WHERE id = %d AND uid = %d LIMIT 1",
97                 intval($id),
98                 intval(local_user())
99         );
100         if($r!==false && count($r)>0) {
101             return $this->_set_extra($r)[0];
102         }
103         return null;
104     }
105     
106     /**
107      * @brief set seen state of $note of local_user()
108      *
109      * @param array $note
110      * @param bool $seen optional true or false, default true
111      * @return bool true on success, false on errors
112      */
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",
115             intval($seen),
116             dbesc($note['link']),
117             intval($note['parent']),
118             dbesc($note['otype']),
119             intval(local_user())
120         );
121     }
122        
123     /**
124      * @brief set seen state of all notifications of local_user()
125      *
126      * @param bool $seen optional true or false. default true
127      * @return bool true on success, false on error
128      */
129     public function setAllSeen($seen = true) {
130         return q("UPDATE notify SET seen = %d WHERE uid = %d",
131             intval($seen),
132                         intval(local_user())
133                 );
134     }
135 }