2 if(class_exists('Item'))
5 require_once('object/BaseObject.php');
6 require_once('include/text.php');
7 require_once('boot.php');
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'
19 private $comment_box_template = 'comment_item.tpl';
20 private $toplevel = false;
21 private $writable = false;
22 private $children = array();
23 private $parent = null;
24 private $conversation = null;
25 private $redirect_url = null;
26 private $owner_url = '';
27 private $owner_photo = '';
28 private $owner_name = '';
29 private $wall_to_wall = false;
30 private $threaded = false;
31 private $visiting = false;
33 public function __construct($data) {
34 $a = $this->get_app();
37 $this->set_template('wall');
38 $this->toplevel = ($this->get_id() == $this->get_data_value('parent'));
40 if(is_array($_SESSION['remote'])) {
41 foreach($_SESSION['remote'] as $visitor) {
42 if($visitor['cid'] == $this->get_data_value('contact-id')) {
43 $this->visiting = true;
49 $this->writable = ($this->get_data_value('writable') || $this->get_data_value('self'));
51 $ssl_state = ((local_user()) ? true : false);
52 $this->redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $this->get_data_value('cid') ;
54 if(get_config('system','thread_allow') && $a->theme_thread_allow && !$this->is_toplevel())
55 $this->threaded = true;
57 // Prepare the children
58 if(count($data['children'])) {
59 foreach($data['children'] as $item) {
61 * Only add will be displayed
63 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
66 if($item['verb'] === ACTIVITY_LIKE || $item['verb'] === ACTIVITY_DISLIKE) {
69 $child = new Item($item);
70 $this->add_child($child);
76 * Get data in a form usable by a conversation template
79 * _ The data requested on success
82 public function get_template_data($alike, $dlike, $thread_level=1) {
85 $a = $this->get_app();
87 $item = $this->get_data();
94 $isstarred = "unstarred";
97 $total_children = $this->count_descendants();
99 $conv = $this->get_conversation();
101 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
102 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
103 ? t('Private Message')
105 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
106 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
107 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
110 if(($this->get_data_value('uid') == local_user()) || $this->is_visiting())
114 'dropping' => $dropping,
115 'select' => t('Select'),
116 'delete' => t('Delete'),
119 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
121 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
122 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
123 if($item['author-link'] && (! $item['author-name']))
124 $profile_name = $item['author-link'];
127 $profile_link = best_link_url($item,$sp);
128 if($profile_link === 'mailbox')
131 $sparkle = ' sparkle';
133 $profile_link = zrl($profile_link);
135 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
136 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
137 $profile_avatar = $a->contacts[$normalised]['thumb'];
139 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_data_value('thumb')));
141 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
142 call_hooks('render_location',$locate);
143 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
148 foreach(explode(',',$item['tag']) as $tag){
161 $like = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
162 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
165 * We should avoid doing this all the time, but it depends on the conversation mode
166 * And the conv mode may change when we change the conv, or it changes its mode
167 * Maybe we should establish a way to be notified about conversation changes
169 $this->check_wall_to_wall();
171 if($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url()))
172 $osparkle = ' sparkle';
174 if($this->is_toplevel()) {
175 if($conv->get_profile_owner() == local_user()) {
176 $isstarred = (($item['starred']) ? "starred" : "unstarred");
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"),
193 if($conv->is_writable()) {
195 'like' => array( t("I like this \x28toggle\x29"), t("like")),
196 'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
198 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
201 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
204 localize_item($item);
206 $body = prepare_body($item,true);
208 list($categories, $folders) = get_cats_and_terms($item);
211 'template' => $this->get_template(),
213 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
215 'hashtags' => $hashtags,
216 'mentions' => $mentions,
217 'txt_cats' => t('Categories:'),
218 'txt_folders' => t('Filed under:'),
219 'has_cats' => ((count($categories)) ? 'true' : ''),
220 'has_folders' => ((count($folders)) ? 'true' : ''),
221 'categories' => $categories,
222 'folders' => $folders,
223 'body' => template_escape($body),
224 'text' => strip_tags(template_escape($body)),
225 'id' => $this->get_id(),
226 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
227 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $this->get_owner_name(), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
229 'wall' => t('Wall-to-Wall'),
230 'vwall' => t('via Wall-To-Wall:'),
231 'profile_url' => $profile_link,
232 'item_photo_menu' => item_photo_menu($item),
233 'name' => template_escape($profile_name),
234 'thumb' => $profile_avatar,
235 'osparkle' => $osparkle,
236 'sparkle' => $sparkle,
237 'title' => template_escape($item['title']),
238 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
239 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
241 'location' => template_escape($location),
243 'owner_url' => $this->get_owner_url(),
244 'owner_photo' => $this->get_owner_photo(),
245 'owner_name' => template_escape($this->get_owner_name()),
246 'plink' => get_plink($item),
248 'isstarred' => $isstarred,
254 'dislike' => $dislike,
255 'comment' => $this->get_comment_box($indent),
256 'previewing' => ($conv->is_preview() ? ' preview ' : ''),
257 'wait' => t('Please wait'),
258 'thread_level' => $thread_level
261 $arr = array('item' => $item, 'output' => $tmp_item);
262 call_hooks('display_item', $arr);
264 $result = $arr['output'];
266 $result['children'] = array();
267 $children = $this->get_children();
268 $nb_children = count($children);
269 if($nb_children > 0) {
270 foreach($children as $child) {
271 $result['children'][] = $child->get_template_data($alike, $dlike, $thread_level + 1);
274 if(($nb_children > 2) || ($thread_level > 1)) {
275 $result['children'][0]['comment_firstcollapsed'] = true;
276 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
277 $result['children'][0]['hidden_comments_num'] = $total_children;
278 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
279 $result['children'][0]['hide_text'] = t('show more');
280 if($thread_level > 1) {
281 $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
284 $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
289 if ($this->is_toplevel()) {
290 $result['total_comments_num'] = $total_children;
291 $result['total_comments_text'] = tt('comment', 'comments', $total_children);
294 $result['private'] = $item['private'];
295 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
297 if($this->is_threaded()) {
298 $result['flatten'] = false;
299 $result['threaded'] = true;
302 $result['flatten'] = true;
303 $result['threaded'] = false;
309 public function get_id() {
310 return $this->get_data_value('id');
313 public function is_threaded() {
314 return $this->threaded;
320 public function add_child($item) {
321 $item_id = $item->get_id();
323 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
326 if($this->get_child($item->get_id())) {
327 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
331 * Only add what will be displayed
333 if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
334 logger('[WARN] Item::add_child : Item is a mail ('. $item->get_id() .').', LOGGER_DEBUG);
337 if($item->get_data_value('verb') === ACTIVITY_LIKE || $item->get_data_value('verb') === ACTIVITY_DISLIKE) {
338 logger('[WARN] Item::add_child : Item is a (dis)like ('. $item->get_id() .').', LOGGER_DEBUG);
342 $item->set_parent($this);
343 $this->children[] = $item;
344 return end($this->children);
348 * Get a child by its ID
350 public function get_child($id) {
351 foreach($this->get_children() as $child) {
352 if($child->get_id() == $id)
359 * Get all ou children
361 public function get_children() {
362 return $this->children;
368 protected function set_parent($item) {
369 $parent = $this->get_parent();
371 $parent->remove_child($this);
373 $this->parent = $item;
374 $this->set_conversation($item->get_conversation());
380 protected function remove_parent() {
381 $this->parent = null;
382 $this->conversation = null;
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 $child->remove_parent();
393 unset($this->children[$key]);
394 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
395 $this->children = array_values($this->children);
399 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
406 protected function get_parent() {
407 return $this->parent;
413 public function set_conversation($conv) {
414 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
416 $this->conversation = $conv;
418 // Set it on our children too
419 foreach($this->get_children() as $child)
420 $child->set_conversation($conv);
426 public function get_conversation() {
427 return $this->conversation;
433 * We shouldn't need this
435 public function get_data() {
446 public function get_data_value($name) {
447 if(!isset($this->data[$name])) {
448 logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
452 return $this->data[$name];
458 private function set_template($name) {
459 if(!x($this->available_templates, $name)) {
460 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
463 $this->template = $this->available_templates[$name];
469 private function get_template() {
470 return $this->template;
474 * Check if this is a toplevel post
476 private function is_toplevel() {
477 return $this->toplevel;
481 * Check if this is writable
483 private function is_writable() {
484 $conv = $this->get_conversation();
487 // This will allow us to comment on wall-to-wall items owned by our friends
488 // and community forums even if somebody else wrote the post.
489 return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
491 return $this->writable;
495 * Count the total of our descendants
497 private function count_descendants() {
498 $children = $this->get_children();
499 $total = count($children);
501 foreach($children as $child) {
502 $total += $child->count_descendants();
509 * Get the template for the comment box
511 private function get_comment_box_template() {
512 return $this->comment_box_template;
516 * Get the comment box
519 * _ The comment box string (empty if no comment box)
522 private function get_comment_box($indent) {
523 if(!$this->is_toplevel() && !get_config('system','thread_allow')) {
528 $conv = $this->get_conversation();
529 $template = get_markup_template($this->get_comment_box_template());
531 if( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
534 if($conv->is_writable() && $this->is_writable()) {
535 $a = $this->get_app();
536 $qc = $qcomment = null;
539 * Hmmm, code depending on the presence of a particular plugin?
540 * This should be better if done by a hook
542 if(in_array('qcomment',$a->plugins)) {
543 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
544 $qcomment = (($qc) ? explode("\n",$qc) : null);
546 $comment_box = replace_macros($template,array(
547 '$return_path' => '',
548 '$threaded' => $this->is_threaded(),
549 '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
550 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
551 '$id' => $this->get_id(),
552 '$parent' => $this->get_id(),
553 '$qcomment' => $qcomment,
554 '$profile_uid' => $conv->get_profile_owner(),
555 '$mylink' => $a->contact['url'],
556 '$mytitle' => t('This is you'),
557 '$myphoto' => $a->contact['thumb'],
558 '$comment' => t('Comment'),
559 '$submit' => t('Submit'),
560 '$edbold' => t('Bold'),
561 '$editalic' => t('Italic'),
562 '$eduline' => t('Underline'),
563 '$edquote' => t('Quote'),
564 '$edcode' => t('Code'),
565 '$edimg' => t('Image'),
566 '$edurl' => t('Link'),
567 '$edvideo' => t('Video'),
568 '$preview' => t('Preview'),
569 '$indent' => $indent,
570 '$sourceapp' => t($a->sourcename),
571 '$ww' => (($conv->get_mode() === 'network') ? $ww : '')
578 private function get_redirect_url() {
579 return $this->redirect_url;
583 * Check if we are a wall to wall item and set the relevant properties
585 protected function check_wall_to_wall() {
586 $a = $this->get_app();
587 $conv = $this->get_conversation();
588 $this->wall_to_wall = false;
590 if($this->is_toplevel()) {
591 if( (! $this->get_data_value('self')) && ($conv->get_mode() !== 'profile')) {
592 if($this->get_data_value('wall')) {
594 // On the network page, I am the owner. On the display page it will be the profile owner.
595 // This will have been stored in $a->page_contact by our calling page.
596 // Put this person as the wall owner of the wall-to-wall notice.
598 $this->owner_url = zrl($a->page_contact['url']);
599 $this->owner_photo = $a->page_contact['thumb'];
600 $this->owner_name = $a->page_contact['name'];
601 $this->wall_to_wall = true;
603 else if($this->get_data_value('owner-link')) {
605 $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
606 $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
607 $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
608 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
610 // The author url doesn't match the owner (typically the contact)
611 // and also doesn't match the contact alias.
612 // The name match is a hack to catch several weird cases where URLs are
613 // all over the park. It can be tricked, but this prevents you from
614 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
615 // well that it's the same Bob Smith.
617 // But it could be somebody else with the same name. It just isn't highly likely.
620 $this->owner_photo = $this->get_data_value('owner-avatar');
621 $this->owner_name = $this->get_data_value('owner-name');
622 $this->wall_to_wall = true;
623 // If it is our contact, use a friendly redirect link
624 if((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url')))
625 && ($this->get_data_value('network') === NETWORK_DFRN)) {
626 $this->owner_url = $this->get_redirect_url();
629 $this->owner_url = zrl($this->get_data_value('owner-link'));
635 if(!$this->wall_to_wall) {
636 $this->set_template('wall');
637 $this->owner_url = '';
638 $this->owner_photo = '';
639 $this->owner_name = '';
643 private function is_wall_to_wall() {
644 return $this->wall_to_wall;
647 private function get_owner_url() {
648 return $this->owner_url;
651 private function get_owner_photo() {
652 return $this->owner_photo;
655 private function get_owner_name() {
656 return $this->owner_name;
659 private function is_visiting() {
660 return $this->visiting;