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'])
105 $isstarred = "unstarred";
109 $total_children = $this->count_descendants();
111 $conv = $this->get_conversation();
114 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
115 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
116 ? t('Private Message')
118 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
119 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
120 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
123 if(($this->get_data_value('uid') == local_user()) || $this->is_visiting())
127 'dropping' => $dropping,
128 'pagedrop' => ((feature_enabled($conv->get_profile_owner(),'multi_delete')) ? $item['pagedrop'] : ''),
129 'select' => t('Select'),
130 'delete' => t('Delete'),
133 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
135 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
136 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
137 if($item['author-link'] && (! $item['author-name']))
138 $profile_name = $item['author-link'];
141 $profile_link = best_link_url($item,$sp);
142 if($profile_link === 'mailbox')
145 $sparkle = ' sparkle';
147 $profile_link = zrl($profile_link);
149 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
150 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
151 $profile_avatar = $a->contacts[$normalised]['thumb'];
153 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_data_value('thumb')));
155 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
156 call_hooks('render_location',$locate);
157 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
159 $searchpath = $a->get_baseurl()."/search?tag=";
165 /*foreach(explode(',',$item['tag']) as $tag){
177 $like = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
178 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
181 * We should avoid doing this all the time, but it depends on the conversation mode
182 * And the conv mode may change when we change the conv, or it changes its mode
183 * Maybe we should establish a way to be notified about conversation changes
185 $this->check_wall_to_wall();
187 if($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url()))
188 $osparkle = ' sparkle';
190 if($this->is_toplevel()) {
191 if($conv->get_profile_owner() == local_user()) {
192 $isstarred = (($item['starred']) ? "starred" : "unstarred");
195 'do' => t("add star"),
196 'undo' => t("remove star"),
197 'toggle' => t("toggle star status"),
198 'classdo' => (($item['starred']) ? "hidden" : ""),
199 'classundo' => (($item['starred']) ? "" : "hidden"),
200 'starred' => t('starred'),
203 'do' => t("ignore thread"),
204 'undo' => t("unignore thread"),
205 'toggle' => t("toggle ignore status"),
206 'classdo' => (($item['starred']) ? "hidden" : ""),
207 'classundo' => (($item['starred']) ? "" : "hidden"),
208 'ignored' => t('ignored'),
211 if(feature_enabled($conv->get_profile_owner(),'commtag')) {
213 'add' => t("add tag"),
222 if($conv->is_writable()) {
224 'like' => array( t("I like this \x28toggle\x29"), t("like")),
225 'dislike' => ((feature_enabled($conv->get_profile_owner(),'dislike')) ? array( t("I don't like this \x28toggle\x29"), t("dislike")) : ''),
227 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
230 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0){
234 localize_item($item);
236 if ($item["postopts"] and !get_config("system", "suppress_language")) {
237 //$langdata = explode(";", $item["postopts"]);
238 //$langstr = substr($langdata[0], 5)." (".round($langdata[1]*100, 1)."%)";
240 if (substr($item["postopts"], 0, 5) == "lang=") {
241 $postopts = substr($item["postopts"], 5);
243 $languages = explode(":", $postopts);
245 if (sizeof($languages) == 1) {
246 $languages = array();
247 $languages[] = $postopts;
250 foreach ($languages as $language) {
251 $langdata = explode(";", $language);
255 //$langstr .= $langdata[0]." (".round($langdata[1]*100, 1)."%)";
256 $langstr .= round($langdata[1]*100, 1)."% ".$langdata[0];
261 $body = prepare_body($item,true);
263 list($categories, $folders) = get_cats_and_terms($item);
265 if($a->theme['template_engine'] === 'internal') {
266 $body_e = template_escape($body);
267 $text_e = strip_tags(template_escape($body));
268 $name_e = template_escape($profile_name);
269 $title_e = template_escape($item['title']);
270 $location_e = template_escape($location);
271 $owner_name_e = template_escape($this->get_owner_name());
275 $text_e = strip_tags($body);
276 $name_e = $profile_name;
277 $title_e = $item['title'];
278 $location_e = $location;
279 $owner_name_e = $this->get_owner_name();
282 // Disable features that aren't available in several networks
283 if (($item["item_network"] != "dfrn") AND isset($buttons["dislike"])) {
284 unset($buttons["dislike"]);
288 if (($item["item_network"] == "feed") AND isset($buttons["like"]))
289 unset($buttons["like"]);
291 if (($item["item_network"] == "mail") AND isset($buttons["like"]))
292 unset($buttons["like"]);
294 if (($item["item_network"] == "dspr") AND ($indent == 'comment') AND isset($buttons["like"]))
295 unset($buttons["like"]);
297 // Facebook can like comments - but it isn't programmed in the connector yet.
298 if (($item["item_network"] == "face") AND ($indent == 'comment') AND isset($buttons["like"]))
299 unset($buttons["like"]);
303 'template' => $this->get_template(),
305 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
306 'tags' => $item['tags'],
307 'hashtags' => $item['hashtags'],
308 'mentions' => $item['mentions'],
309 'txt_cats' => t('Categories:'),
310 'txt_folders' => t('Filed under:'),
311 'has_cats' => ((count($categories)) ? 'true' : ''),
312 'has_folders' => ((count($folders)) ? 'true' : ''),
313 'categories' => $categories,
314 'folders' => $folders,
317 'id' => $this->get_id(),
318 'guid' => $item['guid'],
319 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
320 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $this->get_owner_name(), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
323 'wall' => t('Wall-to-Wall'),
324 'vwall' => t('via Wall-To-Wall:'),
325 'profile_url' => $profile_link,
326 'item_photo_menu' => item_photo_menu($item),
328 'thumb' => proxy_url($profile_avatar),
329 'osparkle' => $osparkle,
330 'sparkle' => $sparkle,
332 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
333 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
334 'app' => $item['app'],
335 'created' => relative_date($item['created']),
337 'location' => $location_e,
340 'owner_url' => $this->get_owner_url(),
341 'owner_photo' => proxy_url($this->get_owner_photo()),
342 'owner_name' => $owner_name_e,
343 'plink' => get_plink($item),
344 'edpost' => ((feature_enabled($conv->get_profile_owner(),'edit_posts')) ? $edpost : ''),
345 'isstarred' => $isstarred,
346 'star' => ((feature_enabled($conv->get_profile_owner(),'star_posts')) ? $star : ''),
347 /* 'ignore' => ((feature_enabled($conv->get_profile_owner(),'ignore_posts')) ? $ignore : ''), */
350 'filer' => ((feature_enabled($conv->get_profile_owner(),'filing')) ? $filer : ''),
354 'dislike' => $dislike,
355 'switchcomment' => t('Comment'),
356 'comment' => $this->get_comment_box($indent),
357 'previewing' => ($conv->is_preview() ? ' preview ' : ''),
358 'wait' => t('Please wait'),
359 'thread_level' => $thread_level,
360 'postopts' => $langstr,
362 'network' => $item["item_network"],
363 'network_name' => network_to_name($item['item_network']),
366 $arr = array('item' => $item, 'output' => $tmp_item);
367 call_hooks('display_item', $arr);
369 $result = $arr['output'];
371 $result['children'] = array();
372 $children = $this->get_children();
373 $nb_children = count($children);
374 if($nb_children > 0) {
375 foreach($children as $child) {
376 $result['children'][] = $child->get_template_data($alike, $dlike, $thread_level + 1);
379 if(($nb_children > 2) || ($thread_level > 1)) {
380 $result['children'][0]['comment_firstcollapsed'] = true;
381 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
382 $result['children'][0]['hidden_comments_num'] = $total_children;
383 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
384 $result['children'][0]['hide_text'] = t('show more');
385 if($thread_level > 1) {
386 $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
389 $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
394 if ($this->is_toplevel()) {
395 $result['total_comments_num'] = "$total_children";
396 $result['total_comments_text'] = tt('comment', 'comments', $total_children);
399 $result['private'] = $item['private'];
400 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
402 if($this->is_threaded()) {
403 $result['flatten'] = false;
404 $result['threaded'] = true;
407 $result['flatten'] = true;
408 $result['threaded'] = false;
414 public function get_id() {
415 return $this->get_data_value('id');
418 public function is_threaded() {
419 return $this->threaded;
425 public function add_child($item) {
426 $item_id = $item->get_id();
428 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
431 if($this->get_child($item->get_id())) {
432 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
436 * Only add what will be displayed
438 if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
441 if(activity_match($item->get_data_value('verb'),ACTIVITY_LIKE) || activity_match($item->get_data_value('verb'),ACTIVITY_DISLIKE)) {
445 $item->set_parent($this);
446 $this->children[] = $item;
447 return end($this->children);
451 * Get a child by its ID
453 public function get_child($id) {
454 foreach($this->get_children() as $child) {
455 if($child->get_id() == $id)
462 * Get all ou children
464 public function get_children() {
465 return $this->children;
471 protected function set_parent($item) {
472 $parent = $this->get_parent();
474 $parent->remove_child($this);
476 $this->parent = $item;
477 $this->set_conversation($item->get_conversation());
483 protected function remove_parent() {
484 $this->parent = null;
485 $this->conversation = null;
491 public function remove_child($item) {
492 $id = $item->get_id();
493 foreach($this->get_children() as $key => $child) {
494 if($child->get_id() == $id) {
495 $child->remove_parent();
496 unset($this->children[$key]);
497 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
498 $this->children = array_values($this->children);
502 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
509 protected function get_parent() {
510 return $this->parent;
516 public function set_conversation($conv) {
517 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
519 $this->conversation = $conv;
521 // Set it on our children too
522 foreach($this->get_children() as $child)
523 $child->set_conversation($conv);
529 public function get_conversation() {
530 return $this->conversation;
536 * We shouldn't need this
538 public function get_data() {
549 public function get_data_value($name) {
550 if(!isset($this->data[$name])) {
551 // logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
555 return $this->data[$name];
561 private function set_template($name) {
564 if(!x($this->available_templates, $name)) {
565 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
569 $this->template = $this->available_templates[$name];
575 private function get_template() {
576 return $this->template;
580 * Check if this is a toplevel post
582 private function is_toplevel() {
583 return $this->toplevel;
587 * Check if this is writable
589 private function is_writable() {
590 $conv = $this->get_conversation();
593 // This will allow us to comment on wall-to-wall items owned by our friends
594 // and community forums even if somebody else wrote the post.
596 // bug #517 - this fixes for conversation owner
597 if($conv->get_mode() == 'profile' && $conv->get_profile_owner() == local_user())
600 // this fixes for visitors
601 return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
603 return $this->writable;
607 * Count the total of our descendants
609 private function count_descendants() {
610 $children = $this->get_children();
611 $total = count($children);
613 foreach($children as $child) {
614 $total += $child->count_descendants();
621 * Get the template for the comment box
623 private function get_comment_box_template() {
624 return $this->comment_box_template;
628 * Get the comment box
631 * _ The comment box string (empty if no comment box)
634 private function get_comment_box($indent) {
635 $a = $this->get_app();
636 if(!$this->is_toplevel() && !(get_config('system','thread_allow') && $a->theme_thread_allow)) {
641 $conv = $this->get_conversation();
642 $template = get_markup_template($this->get_comment_box_template());
644 if( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
647 if($conv->is_writable() && $this->is_writable()) {
648 $qc = $qcomment = null;
651 * Hmmm, code depending on the presence of a particular plugin?
652 * This should be better if done by a hook
654 if(in_array('qcomment',$a->plugins)) {
655 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
656 $qcomment = (($qc) ? explode("\n",$qc) : null);
658 $comment_box = replace_macros($template,array(
659 '$return_path' => $a->query_string,
660 '$threaded' => $this->is_threaded(),
661 // '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
663 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
664 '$id' => $this->get_id(),
665 '$parent' => $this->get_id(),
666 '$qcomment' => $qcomment,
667 '$profile_uid' => $conv->get_profile_owner(),
668 '$mylink' => $a->contact['url'],
669 '$mytitle' => t('This is you'),
670 '$myphoto' => $a->contact['thumb'],
671 '$comment' => t('Comment'),
672 '$submit' => t('Submit'),
673 '$edbold' => t('Bold'),
674 '$editalic' => t('Italic'),
675 '$eduline' => t('Underline'),
676 '$edquote' => t('Quote'),
677 '$edcode' => t('Code'),
678 '$edimg' => t('Image'),
679 '$edurl' => t('Link'),
680 '$edvideo' => t('Video'),
681 '$preview' => ((feature_enabled($conv->get_profile_owner(),'preview')) ? t('Preview') : ''),
682 '$indent' => $indent,
683 '$sourceapp' => t($a->sourcename),
684 '$ww' => (($conv->get_mode() === 'network') ? $ww : ''),
685 '$rand_num' => random_digits(12)
692 private function get_redirect_url() {
693 return $this->redirect_url;
697 * Check if we are a wall to wall item and set the relevant properties
699 protected function check_wall_to_wall() {
700 $a = $this->get_app();
701 $conv = $this->get_conversation();
702 $this->wall_to_wall = false;
704 if($this->is_toplevel()) {
705 if($conv->get_mode() !== 'profile') {
706 if($this->get_data_value('wall') AND !$this->get_data_value('self')) {
707 // On the network page, I am the owner. On the display page it will be the profile owner.
708 // This will have been stored in $a->page_contact by our calling page.
709 // Put this person as the wall owner of the wall-to-wall notice.
711 $this->owner_url = zrl($a->page_contact['url']);
712 $this->owner_photo = $a->page_contact['thumb'];
713 $this->owner_name = $a->page_contact['name'];
714 $this->wall_to_wall = true;
716 else if($this->get_data_value('owner-link')) {
718 $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
719 $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
720 $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
722 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
724 // The author url doesn't match the owner (typically the contact)
725 // and also doesn't match the contact alias.
726 // The name match is a hack to catch several weird cases where URLs are
727 // all over the park. It can be tricked, but this prevents you from
728 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
729 // well that it's the same Bob Smith.
731 // But it could be somebody else with the same name. It just isn't highly likely.
734 $this->owner_photo = $this->get_data_value('owner-avatar');
735 $this->owner_name = $this->get_data_value('owner-name');
736 $this->wall_to_wall = true;
737 // If it is our contact, use a friendly redirect link
738 if((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url')))
739 && ($this->get_data_value('network') === NETWORK_DFRN)) {
740 $this->owner_url = $this->get_redirect_url();
743 $this->owner_url = zrl($this->get_data_value('owner-link'));
749 if(!$this->wall_to_wall) {
750 $this->set_template('wall');
751 $this->owner_url = '';
752 $this->owner_photo = '';
753 $this->owner_name = '';
757 private function is_wall_to_wall() {
758 return $this->wall_to_wall;
761 private function get_owner_url() {
762 return $this->owner_url;
765 private function get_owner_photo() {
766 return $this->owner_photo;
769 private function get_owner_name() {
770 return $this->owner_name;
773 private function is_visiting() {
774 return $this->visiting;