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(! visible_activity($item)) {
69 $item['pagedrop'] = $data['pagedrop'];
70 $child = new Item($item);
71 $this->add_child($child);
77 * Get data in a form usable by a conversation template
80 * _ The data requested on success
83 public function get_template_data($alike, $dlike, $thread_level=1) {
86 $a = $this->get_app();
88 $item = $this->get_data();
90 if (strcmp($item['created'], $item['edited'])<>0) {
92 'label' => t('This entry was edited'),
93 'date' => datetime_convert('UTC', date_default_timezone_get(), $item['edited'], 'r'),
94 'relative' => relative_date($item['edited'])
102 $isstarred = "unstarred";
106 $total_children = $this->count_descendants();
108 $conv = $this->get_conversation();
111 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
112 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
113 ? t('Private Message')
115 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
116 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
117 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
120 if(($this->get_data_value('uid') == local_user()) || $this->is_visiting())
124 'dropping' => $dropping,
125 'pagedrop' => ((feature_enabled($conv->get_profile_owner(),'multi_delete')) ? $item['pagedrop'] : ''),
126 'select' => t('Select'),
127 'delete' => t('Delete'),
130 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
132 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
133 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
134 if($item['author-link'] && (! $item['author-name']))
135 $profile_name = $item['author-link'];
138 $profile_link = best_link_url($item,$sp);
139 if($profile_link === 'mailbox')
142 $sparkle = ' sparkle';
144 $profile_link = zrl($profile_link);
146 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
147 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
148 $profile_avatar = $a->contacts[$normalised]['thumb'];
150 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_data_value('thumb')));
152 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
153 call_hooks('render_location',$locate);
154 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
156 $searchpath = $a->get_baseurl()."/search?tag=";
162 /*foreach(explode(',',$item['tag']) as $tag){
174 $like = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
175 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
178 * We should avoid doing this all the time, but it depends on the conversation mode
179 * And the conv mode may change when we change the conv, or it changes its mode
180 * Maybe we should establish a way to be notified about conversation changes
182 $this->check_wall_to_wall();
184 if($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url()))
185 $osparkle = ' sparkle';
187 if($this->is_toplevel()) {
188 if($conv->get_profile_owner() == local_user()) {
189 $isstarred = (($item['starred']) ? "starred" : "unstarred");
192 'do' => t("add star"),
193 'undo' => t("remove star"),
194 'toggle' => t("toggle star status"),
195 'classdo' => (($item['starred']) ? "hidden" : ""),
196 'classundo' => (($item['starred']) ? "" : "hidden"),
197 'starred' => t('starred'),
200 if(feature_enabled($conv->get_profile_owner(),'commtag')) {
202 'add' => t("add tag"),
211 if($conv->is_writable()) {
213 'like' => array( t("I like this \x28toggle\x29"), t("like")),
214 'dislike' => ((feature_enabled($conv->get_profile_owner(),'dislike')) ? array( t("I don't like this \x28toggle\x29"), t("dislike")) : ''),
216 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
219 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0){
223 localize_item($item);
225 if ($item["postopts"] and !get_config("system", "suppress_language")) {
226 //$langdata = explode(";", $item["postopts"]);
227 //$langstr = substr($langdata[0], 5)." (".round($langdata[1]*100, 1)."%)";
229 if (substr($item["postopts"], 0, 5) == "lang=") {
230 $postopts = substr($item["postopts"], 5);
232 $languages = explode(":", $postopts);
234 if (sizeof($languages) == 1) {
235 $languages = array();
236 $languages[] = $postopts;
239 foreach ($languages as $language) {
240 $langdata = explode(";", $language);
244 //$langstr .= $langdata[0]." (".round($langdata[1]*100, 1)."%)";
245 $langstr .= round($langdata[1]*100, 1)."% ".$langdata[0];
250 $body = prepare_body($item,true);
252 list($categories, $folders) = get_cats_and_terms($item);
254 if($a->theme['template_engine'] === 'internal') {
255 $body_e = template_escape($body);
256 $text_e = strip_tags(template_escape($body));
257 $name_e = template_escape($profile_name);
258 $title_e = template_escape($item['title']);
259 $location_e = template_escape($location);
260 $owner_name_e = template_escape($this->get_owner_name());
264 $text_e = strip_tags($body);
265 $name_e = $profile_name;
266 $title_e = $item['title'];
267 $location_e = $location;
268 $owner_name_e = $this->get_owner_name();
271 // Disable features that aren't available in several networks
272 if (($item["item_network"] != "dfrn") AND isset($buttons["dislike"])) {
273 unset($buttons["dislike"]);
277 if (($item["item_network"] == "feed") AND isset($buttons["like"]))
278 unset($buttons["like"]);
280 if (($item["item_network"] == "mail") AND isset($buttons["like"]))
281 unset($buttons["like"]);
283 if (($item["item_network"] == "dspr") AND ($indent == 'comment') AND isset($buttons["like"]))
284 unset($buttons["like"]);
286 // Facebook can like comments - but it isn't programmed in the connector yet.
287 if (($item["item_network"] == "face") AND ($indent == 'comment') AND isset($buttons["like"]))
288 unset($buttons["like"]);
292 'template' => $this->get_template(),
294 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
295 'tags' => $item['tags'],
296 'hashtags' => $item['hashtags'],
297 'mentions' => $item['mentions'],
298 'txt_cats' => t('Categories:'),
299 'txt_folders' => t('Filed under:'),
300 'has_cats' => ((count($categories)) ? 'true' : ''),
301 'has_folders' => ((count($folders)) ? 'true' : ''),
302 'categories' => $categories,
303 'folders' => $folders,
306 'id' => $this->get_id(),
307 'guid' => $item['guid'],
308 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
309 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $this->get_owner_name(), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
312 'wall' => t('Wall-to-Wall'),
313 'vwall' => t('via Wall-To-Wall:'),
314 'profile_url' => $profile_link,
315 'item_photo_menu' => item_photo_menu($item),
317 'thumb' => $profile_avatar,
318 'osparkle' => $osparkle,
319 'sparkle' => $sparkle,
321 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
322 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
323 'app' => $item['app'],
324 'created' => relative_date($item['created']),
326 'location' => $location_e,
329 'owner_url' => $this->get_owner_url(),
330 'owner_photo' => $this->get_owner_photo(),
331 'owner_name' => $owner_name_e,
332 'plink' => get_plink($item),
333 'edpost' => ((feature_enabled($conv->get_profile_owner(),'edit_posts')) ? $edpost : ''),
334 'isstarred' => $isstarred,
335 'star' => ((feature_enabled($conv->get_profile_owner(),'star_posts')) ? $star : ''),
337 'filer' => ((feature_enabled($conv->get_profile_owner(),'filing')) ? $filer : ''),
341 'dislike' => $dislike,
342 'switchcomment' => t('Comment'),
343 'comment' => $this->get_comment_box($indent),
344 'previewing' => ($conv->is_preview() ? ' preview ' : ''),
345 'wait' => t('Please wait'),
346 'thread_level' => $thread_level,
347 'postopts' => $langstr,
349 'network' => $item["item_network"],
350 'network_name' => network_to_name($item['item_network']),
353 $arr = array('item' => $item, 'output' => $tmp_item);
354 call_hooks('display_item', $arr);
356 $result = $arr['output'];
358 $result['children'] = array();
359 $children = $this->get_children();
360 $nb_children = count($children);
361 if($nb_children > 0) {
362 foreach($children as $child) {
363 $result['children'][] = $child->get_template_data($alike, $dlike, $thread_level + 1);
366 if(($nb_children > 2) || ($thread_level > 1)) {
367 $result['children'][0]['comment_firstcollapsed'] = true;
368 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
369 $result['children'][0]['hidden_comments_num'] = $total_children;
370 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
371 $result['children'][0]['hide_text'] = t('show more');
372 if($thread_level > 1) {
373 $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
376 $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
381 if ($this->is_toplevel()) {
382 $result['total_comments_num'] = "$total_children";
383 $result['total_comments_text'] = tt('comment', 'comments', $total_children);
386 $result['private'] = $item['private'];
387 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
389 if($this->is_threaded()) {
390 $result['flatten'] = false;
391 $result['threaded'] = true;
394 $result['flatten'] = true;
395 $result['threaded'] = false;
401 public function get_id() {
402 return $this->get_data_value('id');
405 public function is_threaded() {
406 return $this->threaded;
412 public function add_child($item) {
413 $item_id = $item->get_id();
415 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
418 if($this->get_child($item->get_id())) {
419 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
423 * Only add what will be displayed
425 if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
428 if(activity_match($item->get_data_value('verb'),ACTIVITY_LIKE) || activity_match($item->get_data_value('verb'),ACTIVITY_DISLIKE)) {
432 $item->set_parent($this);
433 $this->children[] = $item;
434 return end($this->children);
438 * Get a child by its ID
440 public function get_child($id) {
441 foreach($this->get_children() as $child) {
442 if($child->get_id() == $id)
449 * Get all ou children
451 public function get_children() {
452 return $this->children;
458 protected function set_parent($item) {
459 $parent = $this->get_parent();
461 $parent->remove_child($this);
463 $this->parent = $item;
464 $this->set_conversation($item->get_conversation());
470 protected function remove_parent() {
471 $this->parent = null;
472 $this->conversation = null;
478 public function remove_child($item) {
479 $id = $item->get_id();
480 foreach($this->get_children() as $key => $child) {
481 if($child->get_id() == $id) {
482 $child->remove_parent();
483 unset($this->children[$key]);
484 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
485 $this->children = array_values($this->children);
489 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
496 protected function get_parent() {
497 return $this->parent;
503 public function set_conversation($conv) {
504 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
506 $this->conversation = $conv;
508 // Set it on our children too
509 foreach($this->get_children() as $child)
510 $child->set_conversation($conv);
516 public function get_conversation() {
517 return $this->conversation;
523 * We shouldn't need this
525 public function get_data() {
536 public function get_data_value($name) {
537 if(!isset($this->data[$name])) {
538 // logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
542 return $this->data[$name];
548 private function set_template($name) {
551 if(!x($this->available_templates, $name)) {
552 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
556 $this->template = $this->available_templates[$name];
562 private function get_template() {
563 return $this->template;
567 * Check if this is a toplevel post
569 private function is_toplevel() {
570 return $this->toplevel;
574 * Check if this is writable
576 private function is_writable() {
577 $conv = $this->get_conversation();
580 // This will allow us to comment on wall-to-wall items owned by our friends
581 // and community forums even if somebody else wrote the post.
583 // bug #517 - this fixes for conversation owner
584 if($conv->get_mode() == 'profile' && $conv->get_profile_owner() == local_user())
587 // this fixes for visitors
588 return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
590 return $this->writable;
594 * Count the total of our descendants
596 private function count_descendants() {
597 $children = $this->get_children();
598 $total = count($children);
600 foreach($children as $child) {
601 $total += $child->count_descendants();
608 * Get the template for the comment box
610 private function get_comment_box_template() {
611 return $this->comment_box_template;
615 * Get the comment box
618 * _ The comment box string (empty if no comment box)
621 private function get_comment_box($indent) {
622 $a = $this->get_app();
623 if(!$this->is_toplevel() && !(get_config('system','thread_allow') && $a->theme_thread_allow)) {
628 $conv = $this->get_conversation();
629 $template = get_markup_template($this->get_comment_box_template());
631 if( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
634 if($conv->is_writable() && $this->is_writable()) {
635 $qc = $qcomment = null;
638 * Hmmm, code depending on the presence of a particular plugin?
639 * This should be better if done by a hook
641 if(in_array('qcomment',$a->plugins)) {
642 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
643 $qcomment = (($qc) ? explode("\n",$qc) : null);
645 $comment_box = replace_macros($template,array(
646 '$return_path' => $a->query_string,
647 '$threaded' => $this->is_threaded(),
648 // '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
650 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
651 '$id' => $this->get_id(),
652 '$parent' => $this->get_id(),
653 '$qcomment' => $qcomment,
654 '$profile_uid' => $conv->get_profile_owner(),
655 '$mylink' => $a->contact['url'],
656 '$mytitle' => t('This is you'),
657 '$myphoto' => $a->contact['thumb'],
658 '$comment' => t('Comment'),
659 '$submit' => t('Submit'),
660 '$edbold' => t('Bold'),
661 '$editalic' => t('Italic'),
662 '$eduline' => t('Underline'),
663 '$edquote' => t('Quote'),
664 '$edcode' => t('Code'),
665 '$edimg' => t('Image'),
666 '$edurl' => t('Link'),
667 '$edvideo' => t('Video'),
668 '$preview' => ((feature_enabled($conv->get_profile_owner(),'preview')) ? t('Preview') : ''),
669 '$indent' => $indent,
670 '$sourceapp' => t($a->sourcename),
671 '$ww' => (($conv->get_mode() === 'network') ? $ww : ''),
672 '$rand_num' => random_digits(12)
679 private function get_redirect_url() {
680 return $this->redirect_url;
684 * Check if we are a wall to wall item and set the relevant properties
686 protected function check_wall_to_wall() {
687 $a = $this->get_app();
688 $conv = $this->get_conversation();
689 $this->wall_to_wall = false;
691 if($this->is_toplevel()) {
692 if( (! $this->get_data_value('self')) && ($conv->get_mode() !== 'profile')) {
693 if($this->get_data_value('wall')) {
695 // On the network page, I am the owner. On the display page it will be the profile owner.
696 // This will have been stored in $a->page_contact by our calling page.
697 // Put this person as the wall owner of the wall-to-wall notice.
699 $this->owner_url = zrl($a->page_contact['url']);
700 $this->owner_photo = $a->page_contact['thumb'];
701 $this->owner_name = $a->page_contact['name'];
702 $this->wall_to_wall = true;
704 else if($this->get_data_value('owner-link')) {
706 $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
707 $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
708 $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
709 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
711 // The author url doesn't match the owner (typically the contact)
712 // and also doesn't match the contact alias.
713 // The name match is a hack to catch several weird cases where URLs are
714 // all over the park. It can be tricked, but this prevents you from
715 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
716 // well that it's the same Bob Smith.
718 // But it could be somebody else with the same name. It just isn't highly likely.
721 $this->owner_photo = $this->get_data_value('owner-avatar');
722 $this->owner_name = $this->get_data_value('owner-name');
723 $this->wall_to_wall = true;
724 // If it is our contact, use a friendly redirect link
725 if((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url')))
726 && ($this->get_data_value('network') === NETWORK_DFRN)) {
727 $this->owner_url = $this->get_redirect_url();
730 $this->owner_url = zrl($this->get_data_value('owner-link'));
736 if(!$this->wall_to_wall) {
737 $this->set_template('wall');
738 $this->owner_url = '';
739 $this->owner_photo = '';
740 $this->owner_name = '';
744 private function is_wall_to_wall() {
745 return $this->wall_to_wall;
748 private function get_owner_url() {
749 return $this->owner_url;
752 private function get_owner_photo() {
753 return $this->owner_photo;
756 private function get_owner_name() {
757 return $this->owner_name;
760 private function is_visiting() {
761 return $this->visiting;