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