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