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