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