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