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) {
84 require_once("mod/proxy.php");
88 $a = $this->get_app();
90 $item = $this->get_data();
92 if (strcmp($item['created'], $item['edited'])<>0) {
94 'label' => t('This entry was edited'),
95 'date' => datetime_convert('UTC', date_default_timezone_get(), $item['edited'], 'r'),
96 'relative' => relative_date($item['edited'])
104 $isstarred = "unstarred";
108 $total_children = $this->count_descendants();
110 $conv = $this->get_conversation();
113 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
114 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
115 ? t('Private Message')
117 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
118 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
119 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
122 if(($this->get_data_value('uid') == local_user()) || $this->is_visiting())
126 'dropping' => $dropping,
127 'pagedrop' => ((feature_enabled($conv->get_profile_owner(),'multi_delete')) ? $item['pagedrop'] : ''),
128 'select' => t('Select'),
129 'delete' => t('Delete'),
132 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
134 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
135 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
136 if($item['author-link'] && (! $item['author-name']))
137 $profile_name = $item['author-link'];
140 $profile_link = best_link_url($item,$sp);
141 if($profile_link === 'mailbox')
144 $sparkle = ' sparkle';
146 $profile_link = zrl($profile_link);
148 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
149 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
150 $profile_avatar = $a->contacts[$normalised]['thumb'];
152 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_data_value('thumb')));
154 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
155 call_hooks('render_location',$locate);
156 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
158 $searchpath = $a->get_baseurl()."/search?tag=";
164 /*foreach(explode(',',$item['tag']) as $tag){
176 $like = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
177 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
180 * We should avoid doing this all the time, but it depends on the conversation mode
181 * And the conv mode may change when we change the conv, or it changes its mode
182 * Maybe we should establish a way to be notified about conversation changes
184 $this->check_wall_to_wall();
186 if($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url()))
187 $osparkle = ' sparkle';
189 if($this->is_toplevel()) {
190 if($conv->get_profile_owner() == local_user()) {
191 $isstarred = (($item['starred']) ? "starred" : "unstarred");
194 'do' => t("add star"),
195 'undo' => t("remove star"),
196 'toggle' => t("toggle star status"),
197 'classdo' => (($item['starred']) ? "hidden" : ""),
198 'classundo' => (($item['starred']) ? "" : "hidden"),
199 'starred' => t('starred'),
202 if(feature_enabled($conv->get_profile_owner(),'commtag')) {
204 'add' => t("add tag"),
213 if($conv->is_writable()) {
215 'like' => array( t("I like this \x28toggle\x29"), t("like")),
216 'dislike' => ((feature_enabled($conv->get_profile_owner(),'dislike')) ? array( t("I don't like this \x28toggle\x29"), t("dislike")) : ''),
218 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
221 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0){
225 localize_item($item);
227 if ($item["postopts"] and !get_config("system", "suppress_language")) {
228 //$langdata = explode(";", $item["postopts"]);
229 //$langstr = substr($langdata[0], 5)." (".round($langdata[1]*100, 1)."%)";
231 if (substr($item["postopts"], 0, 5) == "lang=") {
232 $postopts = substr($item["postopts"], 5);
234 $languages = explode(":", $postopts);
236 if (sizeof($languages) == 1) {
237 $languages = array();
238 $languages[] = $postopts;
241 foreach ($languages as $language) {
242 $langdata = explode(";", $language);
246 //$langstr .= $langdata[0]." (".round($langdata[1]*100, 1)."%)";
247 $langstr .= round($langdata[1]*100, 1)."% ".$langdata[0];
252 $body = prepare_body($item,true);
254 list($categories, $folders) = get_cats_and_terms($item);
256 if($a->theme['template_engine'] === 'internal') {
257 $body_e = template_escape($body);
258 $text_e = strip_tags(template_escape($body));
259 $name_e = template_escape($profile_name);
260 $title_e = template_escape($item['title']);
261 $location_e = template_escape($location);
262 $owner_name_e = template_escape($this->get_owner_name());
266 $text_e = strip_tags($body);
267 $name_e = $profile_name;
268 $title_e = $item['title'];
269 $location_e = $location;
270 $owner_name_e = $this->get_owner_name();
273 // Disable features that aren't available in several networks
274 if (($item["item_network"] != "dfrn") AND isset($buttons["dislike"])) {
275 unset($buttons["dislike"]);
279 if (($item["item_network"] == "feed") AND isset($buttons["like"]))
280 unset($buttons["like"]);
282 if (($item["item_network"] == "mail") AND isset($buttons["like"]))
283 unset($buttons["like"]);
285 if (($item["item_network"] == "dspr") AND ($indent == 'comment') AND isset($buttons["like"]))
286 unset($buttons["like"]);
288 // Facebook can like comments - but it isn't programmed in the connector yet.
289 if (($item["item_network"] == "face") AND ($indent == 'comment') AND isset($buttons["like"]))
290 unset($buttons["like"]);
294 'template' => $this->get_template(),
296 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
297 'tags' => $item['tags'],
298 'hashtags' => $item['hashtags'],
299 'mentions' => $item['mentions'],
300 'txt_cats' => t('Categories:'),
301 'txt_folders' => t('Filed under:'),
302 'has_cats' => ((count($categories)) ? 'true' : ''),
303 'has_folders' => ((count($folders)) ? 'true' : ''),
304 'categories' => $categories,
305 'folders' => $folders,
308 'id' => $this->get_id(),
309 'guid' => $item['guid'],
310 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
311 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $this->get_owner_name(), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
314 'wall' => t('Wall-to-Wall'),
315 'vwall' => t('via Wall-To-Wall:'),
316 'profile_url' => $profile_link,
317 'item_photo_menu' => item_photo_menu($item),
319 'thumb' => proxy_url($profile_avatar),
320 'osparkle' => $osparkle,
321 'sparkle' => $sparkle,
323 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
324 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
325 'app' => $item['app'],
326 'created' => relative_date($item['created']),
328 'location' => $location_e,
331 'owner_url' => $this->get_owner_url(),
332 'owner_photo' => proxy_url($this->get_owner_photo()),
333 'owner_name' => $owner_name_e,
334 'plink' => get_plink($item),
335 'edpost' => ((feature_enabled($conv->get_profile_owner(),'edit_posts')) ? $edpost : ''),
336 'isstarred' => $isstarred,
337 'star' => ((feature_enabled($conv->get_profile_owner(),'star_posts')) ? $star : ''),
339 'filer' => ((feature_enabled($conv->get_profile_owner(),'filing')) ? $filer : ''),
343 'dislike' => $dislike,
344 'switchcomment' => t('Comment'),
345 'comment' => $this->get_comment_box($indent),
346 'previewing' => ($conv->is_preview() ? ' preview ' : ''),
347 'wait' => t('Please wait'),
348 'thread_level' => $thread_level,
349 'postopts' => $langstr,
351 'network' => $item["item_network"],
352 'network_name' => network_to_name($item['item_network']),
355 $arr = array('item' => $item, 'output' => $tmp_item);
356 call_hooks('display_item', $arr);
358 $result = $arr['output'];
360 $result['children'] = array();
361 $children = $this->get_children();
362 $nb_children = count($children);
363 if($nb_children > 0) {
364 foreach($children as $child) {
365 $result['children'][] = $child->get_template_data($alike, $dlike, $thread_level + 1);
368 if(($nb_children > 2) || ($thread_level > 1)) {
369 $result['children'][0]['comment_firstcollapsed'] = true;
370 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
371 $result['children'][0]['hidden_comments_num'] = $total_children;
372 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
373 $result['children'][0]['hide_text'] = t('show more');
374 if($thread_level > 1) {
375 $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
378 $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
383 if ($this->is_toplevel()) {
384 $result['total_comments_num'] = "$total_children";
385 $result['total_comments_text'] = tt('comment', 'comments', $total_children);
388 $result['private'] = $item['private'];
389 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
391 if($this->is_threaded()) {
392 $result['flatten'] = false;
393 $result['threaded'] = true;
396 $result['flatten'] = true;
397 $result['threaded'] = false;
403 public function get_id() {
404 return $this->get_data_value('id');
407 public function is_threaded() {
408 return $this->threaded;
414 public function add_child($item) {
415 $item_id = $item->get_id();
417 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
420 if($this->get_child($item->get_id())) {
421 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
425 * Only add what will be displayed
427 if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
430 if(activity_match($item->get_data_value('verb'),ACTIVITY_LIKE) || activity_match($item->get_data_value('verb'),ACTIVITY_DISLIKE)) {
434 $item->set_parent($this);
435 $this->children[] = $item;
436 return end($this->children);
440 * Get a child by its ID
442 public function get_child($id) {
443 foreach($this->get_children() as $child) {
444 if($child->get_id() == $id)
451 * Get all ou children
453 public function get_children() {
454 return $this->children;
460 protected function set_parent($item) {
461 $parent = $this->get_parent();
463 $parent->remove_child($this);
465 $this->parent = $item;
466 $this->set_conversation($item->get_conversation());
472 protected function remove_parent() {
473 $this->parent = null;
474 $this->conversation = null;
480 public function remove_child($item) {
481 $id = $item->get_id();
482 foreach($this->get_children() as $key => $child) {
483 if($child->get_id() == $id) {
484 $child->remove_parent();
485 unset($this->children[$key]);
486 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
487 $this->children = array_values($this->children);
491 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
498 protected function get_parent() {
499 return $this->parent;
505 public function set_conversation($conv) {
506 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
508 $this->conversation = $conv;
510 // Set it on our children too
511 foreach($this->get_children() as $child)
512 $child->set_conversation($conv);
518 public function get_conversation() {
519 return $this->conversation;
525 * We shouldn't need this
527 public function get_data() {
538 public function get_data_value($name) {
539 if(!isset($this->data[$name])) {
540 // logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
544 return $this->data[$name];
550 private function set_template($name) {
553 if(!x($this->available_templates, $name)) {
554 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
558 $this->template = $this->available_templates[$name];
564 private function get_template() {
565 return $this->template;
569 * Check if this is a toplevel post
571 private function is_toplevel() {
572 return $this->toplevel;
576 * Check if this is writable
578 private function is_writable() {
579 $conv = $this->get_conversation();
582 // This will allow us to comment on wall-to-wall items owned by our friends
583 // and community forums even if somebody else wrote the post.
585 // bug #517 - this fixes for conversation owner
586 if($conv->get_mode() == 'profile' && $conv->get_profile_owner() == local_user())
589 // this fixes for visitors
590 return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
592 return $this->writable;
596 * Count the total of our descendants
598 private function count_descendants() {
599 $children = $this->get_children();
600 $total = count($children);
602 foreach($children as $child) {
603 $total += $child->count_descendants();
610 * Get the template for the comment box
612 private function get_comment_box_template() {
613 return $this->comment_box_template;
617 * Get the comment box
620 * _ The comment box string (empty if no comment box)
623 private function get_comment_box($indent) {
624 $a = $this->get_app();
625 if(!$this->is_toplevel() && !(get_config('system','thread_allow') && $a->theme_thread_allow)) {
630 $conv = $this->get_conversation();
631 $template = get_markup_template($this->get_comment_box_template());
633 if( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
636 if($conv->is_writable() && $this->is_writable()) {
637 $qc = $qcomment = null;
640 * Hmmm, code depending on the presence of a particular plugin?
641 * This should be better if done by a hook
643 if(in_array('qcomment',$a->plugins)) {
644 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
645 $qcomment = (($qc) ? explode("\n",$qc) : null);
647 $comment_box = replace_macros($template,array(
648 '$return_path' => $a->query_string,
649 '$threaded' => $this->is_threaded(),
650 // '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
652 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
653 '$id' => $this->get_id(),
654 '$parent' => $this->get_id(),
655 '$qcomment' => $qcomment,
656 '$profile_uid' => $conv->get_profile_owner(),
657 '$mylink' => $a->contact['url'],
658 '$mytitle' => t('This is you'),
659 '$myphoto' => $a->contact['thumb'],
660 '$comment' => t('Comment'),
661 '$submit' => t('Submit'),
662 '$edbold' => t('Bold'),
663 '$editalic' => t('Italic'),
664 '$eduline' => t('Underline'),
665 '$edquote' => t('Quote'),
666 '$edcode' => t('Code'),
667 '$edimg' => t('Image'),
668 '$edurl' => t('Link'),
669 '$edvideo' => t('Video'),
670 '$preview' => ((feature_enabled($conv->get_profile_owner(),'preview')) ? t('Preview') : ''),
671 '$indent' => $indent,
672 '$sourceapp' => t($a->sourcename),
673 '$ww' => (($conv->get_mode() === 'network') ? $ww : ''),
674 '$rand_num' => random_digits(12)
681 private function get_redirect_url() {
682 return $this->redirect_url;
686 * Check if we are a wall to wall item and set the relevant properties
688 protected function check_wall_to_wall() {
689 $a = $this->get_app();
690 $conv = $this->get_conversation();
691 $this->wall_to_wall = false;
693 if($this->is_toplevel()) {
694 if( (! $this->get_data_value('self')) && ($conv->get_mode() !== 'profile')) {
695 if($this->get_data_value('wall')) {
697 // On the network page, I am the owner. On the display page it will be the profile owner.
698 // This will have been stored in $a->page_contact by our calling page.
699 // Put this person as the wall owner of the wall-to-wall notice.
701 $this->owner_url = zrl($a->page_contact['url']);
702 $this->owner_photo = $a->page_contact['thumb'];
703 $this->owner_name = $a->page_contact['name'];
704 $this->wall_to_wall = true;
706 else if($this->get_data_value('owner-link')) {
708 $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
709 $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
710 $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
711 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
713 // The author url doesn't match the owner (typically the contact)
714 // and also doesn't match the contact alias.
715 // The name match is a hack to catch several weird cases where URLs are
716 // all over the park. It can be tricked, but this prevents you from
717 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
718 // well that it's the same Bob Smith.
720 // But it could be somebody else with the same name. It just isn't highly likely.
723 $this->owner_photo = $this->get_data_value('owner-avatar');
724 $this->owner_name = $this->get_data_value('owner-name');
725 $this->wall_to_wall = true;
726 // If it is our contact, use a friendly redirect link
727 if((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url')))
728 && ($this->get_data_value('network') === NETWORK_DFRN)) {
729 $this->owner_url = $this->get_redirect_url();
732 $this->owner_url = zrl($this->get_data_value('owner-link'));
738 if(!$this->wall_to_wall) {
739 $this->set_template('wall');
740 $this->owner_url = '';
741 $this->owner_photo = '';
742 $this->owner_name = '';
746 private function is_wall_to_wall() {
747 return $this->wall_to_wall;
750 private function get_owner_url() {
751 return $this->owner_url;
754 private function get_owner_photo() {
755 return $this->owner_photo;
758 private function get_owner_name() {
759 return $this->owner_name;
762 private function is_visiting() {
763 return $this->visiting;