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