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