]> git.mxchange.org Git - friendica.git/blob - include/NotificationsManager.php
362e3f53264a39a81eb5beb56d3a29f620dc3bf1
[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 require_once("include/dbm.php");
9
10 /**
11  * @brief Read and write notifications from/to database
12  */
13 class NotificationsManager {
14         private $a;
15
16         public function __construct() {
17                 $this->a = get_app();
18         }
19
20         /**
21          * @brief set some extra note properties
22          *
23          * @param array $notes array of note arrays from db
24          * @return array Copy of input array with added properties
25          * 
26          * Set some extra properties to note array from db:
27          *  - timestamp as int in default TZ
28          *  - date_rel : relative date string
29          *  - msg_html: message as html string
30          *  - msg_plain: message as plain text string
31          */
32         private function _set_extra($notes) {
33                 $rets = array();
34                 foreach($notes as $n) {
35                         $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
36                         $n['timestamp'] = strtotime($local_time);
37                         $n['date_rel'] = relative_date($n['date']);
38                                 $n['msg_html'] = bbcode($n['msg'], false, false, false, false);
39                                 $n['msg_plain'] = explode("\n",trim(html2plain($n['msg_html'], 0)))[0];
40
41                         $rets[] = $n;
42                 }
43                 return $rets;
44         }
45
46
47         /**
48          * @brief get all notifications for local_user()
49          *
50          * @param array $filter optional Array "column name"=>value: filter query by columns values
51          * @param string $order optional Space separated list of column to sort by. prepend name with "+" to sort ASC, "-" to sort DESC. Default to "-date"
52          * @param string $limit optional Query limits
53          *
54          * @return array of results or false on errors
55          */
56         public function getAll($filter = array(), $order="-date", $limit="") {
57                 $filter_str = array();
58                 $filter_sql = "";
59                 foreach($filter as $column => $value) {
60                         $filter_str[] = sprintf("`%s` = '%s'", $column, dbesc($value));
61                 }
62                 if (count($filter_str)>0) {
63                         $filter_sql = "AND ".implode(" AND ", $filter_str);
64                 }
65
66                 $aOrder = explode(" ", $order);
67                 $asOrder = array();
68                 foreach($aOrder as $o) {
69                         $dir = "asc";
70                         if ($o[0]==="-") {
71                                 $dir = "desc";
72                                 $o = substr($o,1);
73                         }
74                         if ($o[0]==="+") {
75                                 $dir = "asc";
76                                 $o = substr($o,1);
77                         }
78                         $asOrder[] = "$o $dir";
79                 }
80                 $order_sql = implode(", ", $asOrder);
81
82                 if ($limit!="") $limit = " LIMIT ".$limit;
83
84                         $r = q("SELECT * FROM `notify` WHERE `uid` = %d $filter_sql ORDER BY $order_sql $limit",
85                                 intval(local_user())
86                         );
87                 if ($r!==false && count($r)>0) return $this->_set_extra($r);
88                 return false;
89         }
90
91         /**
92          * @brief get one note for local_user() by $id value
93          *
94          * @param int $id
95          * @return array note values or null if not found
96          */
97         public function getByID($id) {
98                 $r = q("SELECT * FROM `notify` WHERE `id` = %d AND `uid` = %d LIMIT 1",
99                         intval($id),
100                         intval(local_user())
101                 );
102                 if($r!==false && count($r)>0) {
103                         return $this->_set_extra($r)[0];
104                 }
105                 return null;
106         }
107
108         /**
109          * @brief set seen state of $note of local_user()
110          *
111          * @param array $note
112          * @param bool $seen optional true or false, default true
113          * @return bool true on success, false on errors
114          */
115         public function setSeen($note, $seen = true) {
116                 return q("UPDATE `notify` SET `seen` = %d WHERE ( `link` = '%s' OR ( `parent` != 0 AND `parent` = %d AND `otype` = '%s' )) AND `uid` = %d",
117                         intval($seen),
118                         dbesc($note['link']),
119                         intval($note['parent']),
120                         dbesc($note['otype']),
121                         intval(local_user())
122                 );
123         }
124
125         /**
126          * @brief set seen state of all notifications of local_user()
127          *
128          * @param bool $seen optional true or false. default true
129          * @return bool true on success, false on error
130          */
131         public function setAllSeen($seen = true) {
132                 return q("UPDATE `notify` SET `seen` = %d WHERE `uid` = %d",
133                         intval($seen),
134                         intval(local_user())
135                 );
136         }
137
138         /**
139          * @brief List of pages for the Notifications TabBar
140          * 
141          * @param app $a The 
142          * @return array with with notifications TabBar data
143          */
144         public function getTabs() {
145                 $tabs = array(
146                         array(
147                                 'label' => t('System'),
148                                 'url'=>'notifications/system',
149                                 'sel'=> (($this->a->argv[1] == 'system') ? 'active' : ''),
150                                 'id' => 'system-tab',
151                                 'accesskey' => 'y',
152                         ),
153                         array(
154                                 'label' => t('Network'),
155                                 'url'=>'notifications/network',
156                                 'sel'=> (($this->a->argv[1] == 'network') ? 'active' : ''),
157                                 'id' => 'network-tab',
158                                 'accesskey' => 'w',
159                         ),
160                         array(
161                                 'label' => t('Personal'),
162                                 'url'=>'notifications/personal',
163                                 'sel'=> (($this->a->argv[1] == 'personal') ? 'active' : ''),
164                                 'id' => 'personal-tab',
165                                 'accesskey' => 'r',
166                         ),
167                         array(
168                                 'label' => t('Home'),
169                                 'url' => 'notifications/home',
170                                 'sel'=> (($this->a->argv[1] == 'home') ? 'active' : ''),
171                                 'id' => 'home-tab',
172                                 'accesskey' => 'h',
173                         ),
174                         array(
175                                 'label' => t('Introductions'),
176                                 'url' => 'notifications/intros',
177                                 'sel'=> (($this->a->argv[1] == 'intros') ? 'active' : ''),
178                                 'id' => 'intro-tab',
179                                 'accesskey' => 'i',
180                         ),
181                 );
182
183                 return $tabs;
184         }
185
186         /**
187          * @brief Format the notification query in an usable array
188          * 
189          * @param array $notifs The array from the db query
190          * @param string $ident The notifications identifier (e.g. network)
191          * @return array
192          *      string 'label' => The type of the notification
193          *      string 'link' => URL to the source
194          *      string 'image' => The avatar image
195          *      string 'text' => The notification text
196          *      string 'when' => Relative date of the notification
197          *      bool 'seen' => Is the notification marked as "seen"
198          */
199         private function format($notifs, $ident = "") {
200
201                 $notif = array();
202                 $arr = array();
203
204                 if (dbm::is_result($notifs)) {
205
206                         foreach ($notifs as $it) {
207                                 // Because we use different db tables for the notification query
208                                 // we have sometimes $it['unseen'] and sometimes $it['seen].
209                                 // So we will have to transform $it['unseen']
210                                 if($it['unseen'])
211                                         $it['seen'] = ($it['unseen'] > 0 ? false : true);
212
213                                 // Depending on the identifier of the notification we need to use different defaults
214                                 switch ($ident) {
215                                         case 'system':
216                                                 $default_item_label = 'notify';
217                                                 $default_item_link = $this->a->get_baseurl(true).'/notify/view/'. $it['id'];
218                                                 $default_item_image = proxy_url($it['photo'], false, PROXY_SIZE_MICRO);
219                                                 $default_item_text = strip_tags(bbcode($it['msg']));
220                                                 $default_item_when = relative_date($it['date']);
221                                                 $default_tpl = $tpl_notify;
222                                                 break;
223
224                                         case 'home':
225                                                 $default_item_label = 'comment';
226                                                 $default_item_link = $this->a->get_baseurl(true).'/display/'.$it['pguid'];
227                                                 $default_item_image = proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO);
228                                                 $default_item_text = sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']);
229                                                 $default_item_when = relative_date($it['created']);
230                                                 $default_tpl = $tpl_item_comments;
231                                                 break;
232
233                                         default:
234                                                 $default_item_label = (($it['id'] == $it['parent']) ? 'post' : 'comment');
235                                                 $default_item_link = $this->a->get_baseurl(true).'/display/'.$it['pguid'];
236                                                 $default_item_image = proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO);
237                                                 $default_item_text = (($it['id'] == $it['parent'])
238                                                                         ? sprintf( t("%s created a new post"), $it['author-name'])
239                                                                         : sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']));
240                                                 $default_item_when = relative_date($it['created']);
241                                                 $default_tpl = (($it['id'] == $it['parent']) ? $tpl_item_posts : $tpl_item_comments);
242
243                                 }
244
245                                 // Transform the different types of notification in an usable array
246                                 switch($it['verb']){
247                                         case ACTIVITY_LIKE:
248                                                 $notif = array(
249                                                         'label' => 'like',
250                                                         'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
251                                                         '$image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
252                                                         'text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
253                                                         'when' => relative_date($it['created']),
254                                                         'seen' => $it['seen']
255                                                 );
256                                                 break;
257
258                                         case ACTIVITY_DISLIKE:
259                                                 $notif = array(
260                                                         'label' => 'dislike',
261                                                         'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
262                                                         'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
263                                                         'text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
264                                                         'when' => relative_date($it['created']),
265                                                         'seen' => $it['seen']
266                                                 );
267                                                 break;
268
269                                         case ACTIVITY_ATTEND:
270                                                 $notif = array(
271                                                         'label' => 'attend',
272                                                         'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
273                                                         'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
274                                                         'text' => sprintf( t("%s is attending %s's event"), $it['author-name'], $it['pname']),
275                                                         'when' => relative_date($it['created']),
276                                                         'seen' => $it['seen']
277                                                 );
278                                                 break;
279
280                                         case ACTIVITY_ATTENDNO:
281                                                 $notif = array(
282                                                         'label' => 'attendno',
283                                                         'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
284                                                         'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
285                                                         'text' => sprintf( t("%s is not attending %s's event"), $it['author-name'], $it['pname']),
286                                                         'when' => relative_date($it['created']),
287                                                         'seen' => $it['seen']
288                                                 );
289                                                 break;
290
291                                         case ACTIVITY_ATTENDMAYBE:
292                                                 $notif = array(
293                                                         'label' => 'attendmaybe',
294                                                         'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
295                                                         'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
296                                                         'text' => sprintf( t("%s may attend %s's event"), $it['author-name'], $it['pname']),
297                                                         'when' => relative_date($it['created']),
298                                                         'seen' => $it['seen']
299                                                 );
300                                                 break;
301
302                                         case ACTIVITY_FRIEND:
303                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
304                                                 $obj = parse_xml_string($xmlhead.$it['object']);
305                                                 $it['fname'] = $obj->title;
306
307                                                 $notif = array(
308                                                         'label' => 'friend',
309                                                         'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
310                                                         'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
311                                                         'text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
312                                                         'when' => relative_date($it['created']),
313                                                         'seen' => $it['seen']
314                                                 );
315                                                 break;
316
317                                         default:
318                                                 $notif = array(
319                                                         'label' => $default_item_label,
320                                                         'link' => $default_item_link,
321                                                         'image' => $default_item_image,
322                                                         'text' => $default_item_text,
323                                                         'when' => $default_item_when,
324                                                         'seen' => $it['seen']
325                                                 );
326                                 }
327
328                                 $arr[] = $notif;
329                         }
330                 }
331
332                 return $arr;
333
334         }
335
336         /**
337          * @brief Total number of network notifications 
338          * @param int $seen
339          *      If 0 only include notifications into the query
340          *      which arn't marked as "seen" into
341          * @return int Number of network notifications
342          */
343         private function networkTotal($seen = 0) {
344                 if($seen === 0)
345                         $sql_seen = " AND `item`.`unseen` = 1 ";
346
347                 $r = q("SELECT COUNT(*) AS `total`
348                                 FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent`
349                                 WHERE `item`.`visible` = 1 AND `pitem`.`parent` != 0 AND
350                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0
351                                 $sql_seen",
352                         intval(local_user())
353                 );
354
355                 if(dbm::is_result($r))
356                         return $r[0]['total'];
357
358                 return 0;
359         }
360
361         /**
362          * @brief Get network notifications
363          * 
364          * @param type $seen
365          *      If 0 only include notifications into the query
366          *      which arn't marked as "seen" into
367          * @param int $start Start the query at this point
368          * @param int $limit Maximum number of query results
369          * 
370          * @return array Query output
371          */
372         public function networkNotifs($seen = 0, $start = 0, $limit = 20) {
373                 $ident = 'network';
374                 $total = $this->networkTotal($seen);
375
376                 if($seen === 0)
377                         $sql_seen = " AND `item`.`unseen` = 1 ";
378
379
380                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, `item`.`unseen`,
381                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` AS `object`,
382                                 `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`, `pitem`.`guid` AS `pguid`
383                                 FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent`
384                                 WHERE `item`.`visible` = 1 AND `pitem`.`parent` != 0 AND
385                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0
386                                 $sql_seen
387                                 ORDER BY `item`.`created` DESC LIMIT %d, %d ",
388                         intval(local_user()),
389                         intval($start),
390                         intval($limit)
391                 );
392
393                 if(dbm::is_result($r)) {
394                         $notifs = $this->format($r, $ident);
395                         $arr = array (
396                                 'notifications' => $notifs,
397                                 'ident' => $ident,
398                                 'total' => $total,
399                         );
400
401                         return $arr;
402                 }
403         }
404
405         /**
406          * @brief Total number of system notifications 
407          * @param int $seen
408          *      If 0 only include notifications into the query
409          *      which arn't marked as "seen" into
410          * @return int Number of system notifications
411          */
412         private function systemTotal($seen = 0) {
413                 if($seen === 0)
414                         $sql_seen = " AND `seen` = 0 ";
415
416                 $r = q("SELECT COUNT(*) AS `total` FROM `notify` WHERE `uid` = %d $sql_seen",
417                         intval(local_user())
418                 );
419
420                 if(dbm::is_result($r))
421                         return $r[0]['total'];
422
423                 return 0;
424         }
425
426                 /**
427          * @brief Get system notifications
428          * 
429          * @param type $seen
430          *      If 0 only include notifications into the query
431          *      which arn't marked as "seen" into
432          * @param int $start Start the query at this point
433          * @param int $limit Maximum number of query results
434          * 
435          * @return array Query output
436          */
437         public function systemNotifs($seen = 0, $start = 0, $limit = 20) {
438                 $ident = 'system';
439                 $total = $this->systemTotal($seen);
440
441                 if($seen === 0)
442                         $sql_seen = " AND `seen` = 0 ";
443
444                 $r = q("SELECT `id`, `photo`, `msg`, `date`, `seen` FROM `notify`
445                                 WHERE `uid` = %d $sql_seen ORDER BY `date` DESC LIMIT %d, %d ",
446                         intval(local_user()),
447                         intval($start),
448                         intval($limit)
449                 );
450
451                 if(dbm::is_result($r)) {
452                         $notifs = $this->format($r, $ident);
453                         $arr = array (
454                                 'notifications' => $notifs,
455                                 'ident' => $ident,
456                                 'total' => $total,
457                         );
458
459                         return $arr;
460                 }
461         }
462
463         /**
464          * @brief Addional SQL query string for the personal notifications
465          * 
466          * @return string The additional sql query
467          */
468         private function _personal_sql_extra() {
469                 $myurl = $this->a->get_baseurl(true) . '/profile/'. $this->a->user['nickname'];
470                 $myurl = substr($myurl,strpos($myurl,'://')+3);
471                 $myurl = str_replace(array('www.','.'),array('','\\.'),$myurl);
472                 $diasp_url = str_replace('/profile/','/u/',$myurl);
473                 $sql_extra .= sprintf(" AND ( `item`.`author-link` regexp '%s' or `item`.`tag` regexp '%s' or `item`.`tag` regexp '%s' ) ",
474                         dbesc($myurl . '$'),
475                         dbesc($myurl . '\\]'),
476                         dbesc($diasp_url . '\\]')
477                 );
478
479                 return $sql_extra;
480         }
481
482         /**
483          * @brief Total number of personal notifications 
484          * @param int $seen
485          *      If 0 only include notifications into the query
486          *      which arn't marked as "seen" into
487          * @return int Number of personal notifications
488          */
489         private function personalTotal($seen = 0) {
490                 $sql_extra .= $this->_personal_sql_extra();
491
492                 if($seen === 0)
493                         $sql_seen = " AND `item`.`unseen` = 1 ";
494
495                 $r = q("SELECT COUNT(*) AS `total`
496                                 FROM `item` INNER JOIN `item` AS `pitem` ON  `pitem`.`id`=`item`.`parent`
497                                 WHERE `item`.`visible` = 1
498                                 $sql_extra
499                                 $sql_seen
500                                 AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 " ,
501                         intval(local_user())
502                 );
503
504                 if(dbm::is_result($r))
505                         return $r[0]['total'];
506
507                 return 0;
508         }
509
510         /**
511          * @brief Get personal notifications
512          * 
513          * @param type $seen
514          *      If 0 only include notifications into the query
515          *      which arn't marked as "seen" into
516          * @param int $start Start the query at this point
517          * @param int $limit Maximum number of query results
518          * 
519          * @return array Query output
520          */
521         public function personalNotifs($seen = 0, $start = 0, $limit = 20) {
522                 $ident = 'personal';
523                 $total = 0;
524                 $sql_extra .= $this->_personal_sql_extra();
525
526                 if($seen === 0)
527                         $sql_seen = " AND `item`.`unseen` = 1 ";
528
529                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, `item`.`unseen`,
530                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` AS `object`, 
531                                 `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`, `pitem`.`guid` AS `pguid`, 
532                                 FROM `item` INNER JOIN `item` AS `pitem` ON  `pitem`.`id`=`item`.`parent`
533                                 WHERE `item`.`visible` = 1
534                                 $sql_extra
535                                 $sql_seen
536                                 AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 
537                                 ORDER BY `item`.`created` DESC LIMIT %d, %d " ,
538                         intval(local_user()),
539                         intval($start),
540                         intval($limit)
541                 );
542
543                 if(dbm::is_result($r)) {
544                         $notifs = $this->format($r, $ident);
545                         $arr = array (
546                                 'notifications' => $notifs,
547                                 'ident' => $ident,
548                                 'total' => $total,
549                         );
550
551                         return $arr;
552                 }
553         }
554
555         /**
556          * @brief Total number of home notifications 
557          * @param int $seen
558          *      If 0 only include notifications into the query
559          *      which arn't marked as "seen" into
560          * @return int Number of home notifications
561          */
562         private function homeTotal($seen = 0) {
563                 if($seen === 0)
564                         $sql_seen = " AND `item`.`unseen` = 1 ";
565
566                 $r = q("SELECT COUNT(*) AS `total` FROM `item`
567                                 WHERE `item`.`visible` = 1 AND
568                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1
569                                 $sql_seen",
570                         intval(local_user())
571                 );
572
573                 if(dbm::is_result($r))
574                         return $r[0]['total'];
575
576                 return 0;
577         }
578
579         /**
580          * @brief Get home notifications
581          * 
582          * @param type $seen
583          *      If 0 only include notifications into the query
584          *      which arn't marked as "seen" into
585          * @param int $start Start the query at this point
586          * @param int $limit Maximum number of query results
587          * 
588          * @return array Query output
589          */
590         public function homeNotifs($seen = 0, $start = 0, $limit = 20) {
591                 $ident = 'home';
592                 $total = $this->homeTotal($seen);
593
594                 if($seen === 0)
595                         $sql_seen = " AND `item`.`unseen` = 1 ";
596
597                 $total = $this->homeTotal($seen);
598
599                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, `item`.`unseen`,
600                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`,
601                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`, `pitem`.`guid` as `pguid`
602                                 FROM `item` INNER JOIN `item` as `pitem` ON `pitem`.`id`=`item`.`parent`
603                                 WHERE `item`.`visible` = 1 AND
604                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1
605                                 $sql_seen
606                                 ORDER BY `item`.`created` DESC LIMIT %d, %d ",
607                         intval(local_user()),
608                         intval($start),
609                         intval($limit)
610                 );
611
612                 if(dbm::is_result($r)) {
613                         $notifs = $this->format($r, $ident);
614                         $arr = array (
615                                 'notifications' => $notifs,
616                                 'ident' => $ident,
617                                 'total' => $total,
618                         );
619
620                         return $arr;
621                 }
622         }
623 }