]> git.mxchange.org Git - friendica.git/blob - object/Item.php
allow hidden comments phrase to be accessed separately as number and text
[friendica.git] / object / Item.php
1 <?php
2 if(class_exists('Item'))
3         return;
4
5 require_once('object/BaseObject.php');
6 require_once('include/text.php');
7 require_once('boot.php');
8
9 /**
10  * An item
11  */
12 class Item extends BaseObject {
13         private $data = array();
14         private $template = null;
15         private $available_templates = array(
16                 'wall' => 'wall_thread.tpl',
17                 'wall2wall' => 'wallwall_thread.tpl'
18         );
19         private $comment_box_template = 'comment_item.tpl';
20         private $toplevel = false;
21         private $writable = false;
22         private $children = array();
23         private $parent = null;
24         private $conversation = null;
25         private $redirect_url = null;
26         private $owner_url = '';
27         private $owner_photo = '';
28         private $owner_name = '';
29         private $wall_to_wall = false;
30         private $threaded = false;
31         private $visiting = false;
32
33         public function __construct($data) {
34                 $a = $this->get_app();
35                 
36                 $this->data = $data;
37                 $this->set_template('wall');
38                 $this->toplevel = ($this->get_id() == $this->get_data_value('parent'));
39
40                 if(is_array($_SESSION['remote'])) {
41                         foreach($_SESSION['remote'] as $visitor) {
42                                 if($visitor['cid'] == $this->get_data_value('contact-id')) {
43                                         $this->visiting = true;
44                                         break;
45                                 }
46                         }
47                 }
48                 
49                 $this->writable = ($this->get_data_value('writable') || $this->get_data_value('self'));
50
51                 $ssl_state = ((local_user()) ? true : false);
52                 $this->redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $this->get_data_value('cid') ;
53
54                 if(get_config('system','thread_allow') && $a->theme_thread_allow && !$this->is_toplevel())
55                         $this->threaded = true;
56
57                 // Prepare the children
58                 if(count($data['children'])) {
59                         foreach($data['children'] as $item) {
60                                 /*
61                                  * Only add will be displayed
62                                  */
63                                 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
64                                         continue;
65                                 }
66                                 if($item['verb'] === ACTIVITY_LIKE || $item['verb'] === ACTIVITY_DISLIKE) {
67                                         continue;
68                                 }
69                                 $child = new Item($item);
70                                 $this->add_child($child);
71                         }
72                 }
73         }
74
75         /**
76          * Get data in a form usable by a conversation template
77          *
78          * Returns:
79          *      _ The data requested on success
80          *      _ false on failure
81          */
82         public function get_template_data($alike, $dlike, $thread_level=1) {
83                 $result = array();
84
85                 $a = $this->get_app();
86
87                 $item = $this->get_data();
88
89                 $commentww = '';
90                 $sparkle = '';
91                 $buttons = '';
92                 $dropping = false;
93                 $star = false;
94                 $isstarred = "unstarred";
95                 $indent = '';
96                 $osparkle = '';
97                 $total_children = $this->count_descendants();
98
99                 $conv = $this->get_conversation();
100
101                 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
102                         || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
103                         ? t('Private Message')
104                         : false);
105                 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
106                 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
107                         $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
108                 else
109                         $edpost = false;
110                 if(($this->get_data_value('uid') == local_user()) || $this->is_visiting())
111                         $dropping = true;
112
113                 $drop = array(
114                         'dropping' => $dropping,
115                         'select' => t('Select'), 
116                         'delete' => t('Delete'),
117                 );
118                 
119                 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
120
121                 $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
122                 $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
123                 if($item['author-link'] && (! $item['author-name']))
124                         $profile_name = $item['author-link'];
125
126                 $sp = false;
127                 $profile_link = best_link_url($item,$sp);
128                 if($profile_link === 'mailbox')
129                         $profile_link = '';
130                 if($sp)
131                         $sparkle = ' sparkle';
132                 else
133                         $profile_link = zrl($profile_link);                 
134
135                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
136                 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
137                         $profile_avatar = $a->contacts[$normalised]['thumb'];
138                 else
139                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_data_value('thumb')));
140
141                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
142                 call_hooks('render_location',$locate);
143                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
144
145                 $tags=array();
146                 foreach(explode(',',$item['tag']) as $tag){
147                         $tag = trim($tag);
148                         if ($tag!="") $tags[] = bbcode($tag);
149                 }
150
151                 $like    = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
152                 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
153
154                 /*
155                  * We should avoid doing this all the time, but it depends on the conversation mode
156                  * And the conv mode may change when we change the conv, or it changes its mode
157                  * Maybe we should establish a way to be notified about conversation changes
158                  */
159                 $this->check_wall_to_wall();
160                 
161                 if($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url()))
162                         $osparkle = ' sparkle';
163                 
164                 if($this->is_toplevel()) {
165                         if($conv->get_profile_owner() == local_user()) {
166                                 $isstarred = (($item['starred']) ? "starred" : "unstarred");
167
168                                 $star = array(
169                                         'do' => t("add star"),
170                                         'undo' => t("remove star"),
171                                         'toggle' => t("toggle star status"),
172                                         'classdo' => (($item['starred']) ? "hidden" : ""),
173                                         'classundo' => (($item['starred']) ? "" : "hidden"),
174                                         'starred' =>  t('starred'),
175                                         'tagger' => t("add tag"),
176                                         'classtagger' => "",
177                                 );
178                         }
179                 } else {
180                         $indent = 'comment';
181                 }
182
183                 if($conv->is_writable()) {
184                         $buttons = array(
185                                 'like' => array( t("I like this \x28toggle\x29"), t("like")),
186                                 'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
187                         );
188                         if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
189                 }
190
191                 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
192                         $indent .= ' shiny';
193
194                 localize_item($item);
195
196                 $body = prepare_body($item,true);
197
198                 $tmp_item = array(
199                         'template' => $this->get_template(),
200                         
201                         'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
202                         'tags' => $tags,
203                         'body' => template_escape($body),
204                         'text' => strip_tags(template_escape($body)),
205                         'id' => $this->get_id(),
206                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
207                         'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $this->get_owner_name(), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
208                         'to' => t('to'),
209                         'wall' => t('Wall-to-Wall'),
210                         'vwall' => t('via Wall-To-Wall:'),
211                         'profile_url' => $profile_link,
212                         'item_photo_menu' => item_photo_menu($item),
213                         'name' => template_escape($profile_name),
214                         'thumb' => $profile_avatar,
215                         'osparkle' => $osparkle,
216                         'sparkle' => $sparkle,
217                         'title' => template_escape($item['title']),
218                         'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
219                         'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
220                         'lock' => $lock,
221                         'location' => template_escape($location),
222                         'indent' => $indent,
223                         'owner_url' => $this->get_owner_url(),
224                         'owner_photo' => $this->get_owner_photo(),
225                         'owner_name' => template_escape($this->get_owner_name()),
226                         'plink' => get_plink($item),
227                         'edpost' => $edpost,
228                         'isstarred' => $isstarred,
229                         'star' => $star,
230                         'filer' => $filer,
231                         'drop' => $drop,
232                         'vote' => $buttons,
233                         'like' => $like,
234                         'dislike' => $dislike,
235                         'comment' => $this->get_comment_box($indent),
236                         'previewing' => ($conv->is_preview() ? ' preview ' : ''),
237                         'wait' => t('Please wait'),
238                         'thread_level' => $thread_level
239                 );
240
241                 $arr = array('item' => $item, 'output' => $tmp_item);
242                 call_hooks('display_item', $arr);
243
244                 $result = $arr['output'];
245
246                 $result['children'] = array();
247                 $children = $this->get_children();
248                 $nb_children = count($children);
249                 if($nb_children > 0) {
250                         foreach($children as $child) {
251                                 $result['children'][] = $child->get_template_data($alike, $dlike, $thread_level + 1);
252                         }
253                         // Collapse
254                         if(($nb_children > 2) || ($thread_level > 1)) {
255                                 $result['children'][0]['comment_firstcollapsed'] = true;
256                                 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
257                                 $result['children'][0]['hidden_comments_num'] = $total_children;
258                                 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
259                                 $result['children'][0]['hide_text'] = t('show more');
260                                 if($thread_level > 1) {
261                                         $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
262                                 }
263                                 else {
264                                         $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
265                                 }
266                         }
267                 }
268                 
269                 $result['private'] = $item['private'];
270                 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
271
272                 if($this->is_threaded()) {
273                         $result['flatten'] = false;
274                         $result['threaded'] = true;
275                 }
276                 else {
277                         $result['flatten'] = true;
278                         $result['threaded'] = false;
279                 }
280
281                 return $result;
282         }
283         
284         public function get_id() {
285                 return $this->get_data_value('id');
286         }
287
288         public function is_threaded() {
289                 return $this->threaded;
290         }
291
292         /**
293          * Add a child item
294          */
295         public function add_child($item) {
296                 $item_id = $item->get_id();
297                 if(!$item_id) {
298                         logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
299                         return false;
300                 }
301                 if($this->get_child($item->get_id())) {
302                         logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
303                         return false;
304                 }
305                 /*
306                  * Only add what will be displayed
307                  */
308                 if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
309                         logger('[WARN] Item::add_child : Item is a mail ('. $item->get_id() .').', LOGGER_DEBUG);
310                         return false;
311                 }
312                 if($item->get_data_value('verb') === ACTIVITY_LIKE || $item->get_data_value('verb') === ACTIVITY_DISLIKE) {
313                         logger('[WARN] Item::add_child : Item is a (dis)like ('. $item->get_id() .').', LOGGER_DEBUG);
314                         return false;
315                 }
316                 
317                 $item->set_parent($this);
318                 $this->children[] = $item;
319                 return end($this->children);
320         }
321
322         /**
323          * Get a child by its ID
324          */
325         public function get_child($id) {
326                 foreach($this->get_children() as $child) {
327                         if($child->get_id() == $id)
328                                 return $child;
329                 }
330                 return null;
331         }
332
333         /**
334          * Get all ou children
335          */
336         public function get_children() {
337                 return $this->children;
338         }
339
340         /**
341          * Set our parent
342          */
343         protected function set_parent($item) {
344                 $parent = $this->get_parent();
345                 if($parent) {
346                         $parent->remove_child($this);
347                 }
348                 $this->parent = $item;
349                 $this->set_conversation($item->get_conversation());
350         }
351
352         /**
353          * Remove our parent
354          */
355         protected function remove_parent() {
356                 $this->parent = null;
357                 $this->conversation = null;
358         }
359
360         /**
361          * Remove a child
362          */
363         public function remove_child($item) {
364                 $id = $item->get_id();
365                 foreach($this->get_children() as $key => $child) {
366                         if($child->get_id() == $id) {
367                                 $child->remove_parent();
368                                 unset($this->children[$key]);
369                                 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
370                                 $this->children = array_values($this->children);
371                                 return true;
372                         }
373                 }
374                 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
375                 return false;
376         }
377
378         /**
379          * Get parent item
380          */
381         protected function get_parent() {
382                 return $this->parent;
383         }
384
385         /**
386          * set conversation
387          */
388         public function set_conversation($conv) {
389                 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
390                 
391                 $this->conversation = $conv;
392
393                 // Set it on our children too
394                 foreach($this->get_children() as $child)
395                         $child->set_conversation($conv);
396         }
397
398         /**
399          * get conversation
400          */
401         public function get_conversation() {
402                 return $this->conversation;
403         }
404
405         /**
406          * Get raw data
407          *
408          * We shouldn't need this
409          */
410         public function get_data() {
411                 return $this->data;
412         }
413
414         /**
415          * Get a data value
416          *
417          * Returns:
418          *      _ value on success
419          *      _ false on failure
420          */
421         public function get_data_value($name) {
422                 if(!isset($this->data[$name])) {
423                         logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
424                         return false;
425                 }
426
427                 return $this->data[$name];
428         }
429
430         /**
431          * Set template
432          */
433         private function set_template($name) {
434                 if(!x($this->available_templates, $name)) {
435                         logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
436                         return false;
437                 }
438                 $this->template = $this->available_templates[$name];
439         }
440
441         /**
442          * Get template
443          */
444         private function get_template() {
445                 return $this->template;
446         }
447
448         /**
449          * Check if this is a toplevel post
450          */
451         private function is_toplevel() {
452                 return $this->toplevel;
453         }
454
455         /**
456          * Check if this is writable
457          */
458         private function is_writable() {
459                 $conv = $this->get_conversation();
460
461                 if($conv) {
462                         // This will allow us to comment on wall-to-wall items owned by our friends
463                         // and community forums even if somebody else wrote the post.
464                         return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
465                 }
466                 return $this->writable;
467         }
468
469         /**
470          * Count the total of our descendants
471          */
472         private function count_descendants() {
473                 $children = $this->get_children();
474                 $total = count($children);
475                 if($total > 0) {
476                         foreach($children as $child) {
477                                 $total += $child->count_descendants();
478                         }
479                 }
480                 return $total;
481         }
482
483         /**
484          * Get the template for the comment box
485          */
486         private function get_comment_box_template() {
487                 return $this->comment_box_template;
488         }
489
490         /**
491          * Get the comment box
492          *
493          * Returns:
494          *      _ The comment box string (empty if no comment box)
495          *      _ false on failure
496          */
497         private function get_comment_box($indent) {
498                 if(!$this->is_toplevel() && !get_config('system','thread_allow')) {
499                         return '';
500                 }
501                 
502                 $comment_box = '';
503                 $conv = $this->get_conversation();
504                 $template = get_markup_template($this->get_comment_box_template());
505                 $ww = '';
506                 if( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
507                         $ww = 'ww';
508
509                 if($conv->is_writable() && $this->is_writable()) {
510                         $a = $this->get_app();
511                         $qc = $qcomment =  null;
512
513                         /*
514                          * Hmmm, code depending on the presence of a particular plugin?
515                          * This should be better if done by a hook
516                          */
517                         if(in_array('qcomment',$a->plugins)) {
518                                 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
519                                 $qcomment = (($qc) ? explode("\n",$qc) : null);
520                         }
521                         $comment_box = replace_macros($template,array(
522                                 '$return_path' => '',
523                                 '$threaded' => $this->is_threaded(),
524                                 '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
525                                 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
526                                 '$id' => $this->get_id(),
527                                 '$parent' => $this->get_id(),
528                                 '$qcomment' => $qcomment,
529                                 '$profile_uid' =>  $conv->get_profile_owner(),
530                                 '$mylink' => $a->contact['url'],
531                                 '$mytitle' => t('This is you'),
532                                 '$myphoto' => $a->contact['thumb'],
533                                 '$comment' => t('Comment'),
534                                 '$submit' => t('Submit'),
535                                 '$edbold' => t('Bold'),
536                                 '$editalic' => t('Italic'),
537                                 '$eduline' => t('Underline'),
538                                 '$edquote' => t('Quote'),
539                                 '$edcode' => t('Code'),
540                                 '$edimg' => t('Image'),
541                                 '$edurl' => t('Link'),
542                                 '$edvideo' => t('Video'),
543                                 '$preview' => t('Preview'),
544                                 '$indent' => $indent,
545                                 '$sourceapp' => t($a->sourcename),
546                                 '$ww' => (($conv->get_mode() === 'network') ? $ww : '')
547                         ));
548                 }
549
550                 return $comment_box;
551         }
552
553         private function get_redirect_url() {
554                 return $this->redirect_url;
555         }
556
557         /**
558          * Check if we are a wall to wall item and set the relevant properties
559          */
560         protected function check_wall_to_wall() {
561                 $a = $this->get_app();
562                 $conv = $this->get_conversation();
563                 $this->wall_to_wall = false;
564                 
565                 if($this->is_toplevel()) {
566                         if( (! $this->get_data_value('self')) && ($conv->get_mode() !== 'profile')) {
567                                 if($this->get_data_value('wall')) {
568
569                                         // On the network page, I am the owner. On the display page it will be the profile owner.
570                                         // This will have been stored in $a->page_contact by our calling page.
571                                         // Put this person as the wall owner of the wall-to-wall notice.
572
573                                         $this->owner_url = zrl($a->page_contact['url']);
574                                         $this->owner_photo = $a->page_contact['thumb'];
575                                         $this->owner_name = $a->page_contact['name'];
576                                         $this->set_template('wall2wall');
577                                         $this->wall_to_wall = true;
578                                 }
579                                 else if($this->get_data_value('owner-link')) {
580
581                                         $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
582                                         $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
583                                         $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
584                                         if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
585
586                                                 // The author url doesn't match the owner (typically the contact)
587                                                 // and also doesn't match the contact alias. 
588                                                 // The name match is a hack to catch several weird cases where URLs are 
589                                                 // all over the park. It can be tricked, but this prevents you from
590                                                 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
591                                                 // well that it's the same Bob Smith. 
592
593                                                 // But it could be somebody else with the same name. It just isn't highly likely. 
594                                                 
595
596                                                 $this->owner_photo = $this->get_data_value('owner-avatar');
597                                                 $this->owner_name = $this->get_data_value('owner-name');
598                                                 $this->set_template('wall2wall');
599                                                 $this->wall_to_wall = true;
600                                                 // If it is our contact, use a friendly redirect link
601                                                 if((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url'))) 
602                                                         && ($this->get_data_value('network') === NETWORK_DFRN)) {
603                                                         $this->owner_url = $this->get_redirect_url();
604                                                 }
605                                                 else
606                                                         $this->owner_url = zrl($this->get_data_value('owner-link'));
607                                         }
608                                 }
609                         }
610                 }
611
612                 if(!$this->wall_to_wall) {
613                         $this->set_template('wall');
614                         $this->owner_url = '';
615                         $this->owner_photo = '';
616                         $this->owner_name = '';
617                 }
618         }
619
620         private function is_wall_to_wall() {
621                 return $this->wall_to_wall;
622         }
623
624         private function get_owner_url() {
625                 return $this->owner_url;
626         }
627
628         private function get_owner_photo() {
629                 return $this->owner_photo;
630         }
631
632         private function get_owner_name() {
633                 return $this->owner_name;
634         }
635
636         private function is_visiting() {
637                 return $this->visiting;
638         }
639 }
640 ?>