2 if(class_exists('Item'))
5 require_once('object/BaseObject.php');
6 require_once('include/text.php');
7 require_once('include/diaspora.php');
8 require_once('boot.php');
13 class Item extends BaseObject {
14 private $data = array();
15 private $template = null;
16 private $available_templates = array(
17 'wall' => 'wall_thread.tpl',
18 'wall2wall' => 'wallwall_thread.tpl'
20 private $comment_box_template = 'comment_item.tpl';
21 private $toplevel = false;
22 private $writable = false;
23 private $children = array();
24 private $parent = null;
25 private $conversation = null;
26 private $redirect_url = null;
27 private $owner_url = '';
28 private $owner_photo = '';
29 private $owner_name = '';
30 private $wall_to_wall = false;
31 private $threaded = false;
32 private $visiting = false;
34 public function __construct($data) {
35 $a = $this->get_app();
38 $this->set_template('wall');
39 $this->toplevel = ($this->get_id() == $this->get_data_value('parent'));
41 if(is_array($_SESSION['remote'])) {
42 foreach($_SESSION['remote'] as $visitor) {
43 if($visitor['cid'] == $this->get_data_value('contact-id')) {
44 $this->visiting = true;
50 $this->writable = ($this->get_data_value('writable') || $this->get_data_value('self'));
52 $ssl_state = ((local_user()) ? true : false);
53 $this->redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $this->get_data_value('cid') ;
55 if(get_config('system','thread_allow') && $a->theme_thread_allow && !$this->is_toplevel())
56 $this->threaded = true;
58 // Prepare the children
59 if(count($data['children'])) {
60 foreach($data['children'] as $item) {
62 * Only add will be displayed
64 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
67 if(! visible_activity($item)) {
70 $item['pagedrop'] = $data['pagedrop'];
71 $child = new Item($item);
72 $this->add_child($child);
78 * Get data in a form usable by a conversation template
81 * _ The data requested on success
84 public function get_template_data($conv_responses, $thread_level=1) {
85 require_once("mod/proxy.php");
89 $a = $this->get_app();
91 $item = $this->get_data();
93 if (strcmp($item['created'], $item['edited'])<>0) {
95 'label' => t('This entry was edited'),
96 'date' => datetime_convert('UTC', date_default_timezone_get(), $item['edited'], 'r'),
97 'relative' => relative_date($item['edited'])
106 $isstarred = "unstarred";
110 $total_children = $this->count_descendants();
112 $conv = $this->get_conversation();
115 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
116 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
117 ? t('Private Message')
119 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
120 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
121 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
124 if(($this->get_data_value('uid') == local_user()) || $this->is_visiting())
128 'dropping' => $dropping,
129 'pagedrop' => ((feature_enabled($conv->get_profile_owner(),'multi_delete')) ? $item['pagedrop'] : ''),
130 'select' => t('Select'),
131 'delete' => t('Delete'),
134 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
136 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
137 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
138 if($item['author-link'] && (! $item['author-name']))
139 $profile_name = $item['author-link'];
142 $profile_link = best_link_url($item,$sp);
143 if($profile_link === 'mailbox')
146 $sparkle = ' sparkle';
148 $profile_link = zrl($profile_link);
150 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
151 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
152 $profile_avatar = $a->contacts[$normalised]['thumb'];
154 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_data_value('thumb')));
156 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
157 call_hooks('render_location',$locate);
158 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
160 $searchpath = $a->get_baseurl()."/search?tag=";
166 /*foreach(explode(',',$item['tag']) as $tag){
178 // process action responses - e.g. like/dislike/attend/agree/whatever
179 $response_verbs = array('like');
180 if(feature_enabled($conv->get_profile_owner(),'dislike'))
181 $response_verbs[] = 'dislike';
182 if($item['object-type'] === ACTIVITY_OBJ_EVENT) {
183 $response_verbs[] = 'attendyes';
184 $response_verbs[] = 'attendno';
185 $response_verbs[] = 'attendmaybe';
186 if($conv->is_writable()) {
188 $attend = array( t('I will attend'), t('I will not attend'), t('I might attend'));
192 // red part for consensus
193 // we need a solution in friendica for missing item_flags
194 // to test $consensus with friendica set $consensus = true
195 // $consensus = (($item['item_flags'] & ITEM_CONSENSUS)? true : false);
199 $response_verbs[] = 'agree';
200 $response_verbs[] = 'disagree';
201 $response_verbs[] = 'abstain';
202 if($conv->is_writable()) {
203 $conlabels = array( t('I agree'), t('I disagree'), t('I abstain'));
207 $responses = get_responses($conv_responses,$response_verbs,$this,$item);
209 foreach ($response_verbs as $value=>$verbs) {
210 $responses[$verbs][output] = ((x($conv_responses[$verbs],$item['uri'])) ? format_like($conv_responses[$verbs][$item['uri']],$conv_responses[$verbs][$item['uri'] . '-l'],$verbs,$item['uri']) : '');
215 * We should avoid doing this all the time, but it depends on the conversation mode
216 * And the conv mode may change when we change the conv, or it changes its mode
217 * Maybe we should establish a way to be notified about conversation changes
219 $this->check_wall_to_wall();
221 if($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url()))
222 $osparkle = ' sparkle';
224 if($this->is_toplevel()) {
225 if($conv->get_profile_owner() == local_user()) {
226 $isstarred = (($item['starred']) ? "starred" : "unstarred");
229 'do' => t("add star"),
230 'undo' => t("remove star"),
231 'toggle' => t("toggle star status"),
232 'classdo' => (($item['starred']) ? "hidden" : ""),
233 'classundo' => (($item['starred']) ? "" : "hidden"),
234 'starred' => t('starred'),
236 $r = q("SELECT `ignored` FROM `thread` WHERE `uid` = %d AND `iid` = %d LIMIT 1",
237 intval($item['uid']),
242 'do' => t("ignore thread"),
243 'undo' => t("unignore thread"),
244 'toggle' => t("toggle ignore status"),
245 'classdo' => (($r[0]['ignored']) ? "hidden" : ""),
246 'classundo' => (($r[0]['ignored']) ? "" : "hidden"),
247 'ignored' => t('ignored'),
252 if(feature_enabled($conv->get_profile_owner(),'commtag')) {
254 'add' => t("add tag"),
263 if($conv->is_writable()) {
265 'like' => array( t("I like this \x28toggle\x29"), t("like")),
266 'dislike' => ((feature_enabled($conv->get_profile_owner(),'dislike')) ? array( t("I don't like this \x28toggle\x29"), t("dislike")) : ''),
268 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
271 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0){
275 localize_item($item);
277 if ($item["postopts"] and !get_config("system", "suppress_language")) {
278 //$langdata = explode(";", $item["postopts"]);
279 //$langstr = substr($langdata[0], 5)." (".round($langdata[1]*100, 1)."%)";
281 if (substr($item["postopts"], 0, 5) == "lang=") {
282 $postopts = substr($item["postopts"], 5);
284 $languages = explode(":", $postopts);
286 if (sizeof($languages) == 1) {
287 $languages = array();
288 $languages[] = $postopts;
291 foreach ($languages as $language) {
292 $langdata = explode(";", $language);
296 //$langstr .= $langdata[0]." (".round($langdata[1]*100, 1)."%)";
297 $langstr .= round($langdata[1]*100, 1)."% ".$langdata[0];
302 $body = prepare_body($item,true);
304 list($categories, $folders) = get_cats_and_terms($item);
306 if($a->theme['template_engine'] === 'internal') {
307 $body_e = template_escape($body);
308 $text_e = strip_tags(template_escape($body));
309 $name_e = template_escape($profile_name);
310 $title_e = template_escape($item['title']);
311 $location_e = template_escape($location);
312 $owner_name_e = template_escape($this->get_owner_name());
316 $text_e = strip_tags($body);
317 $name_e = $profile_name;
318 $title_e = $item['title'];
319 $location_e = $location;
320 $owner_name_e = $this->get_owner_name();
323 // Disable features that aren't available in several networks
324 if (($item["item_network"] != NETWORK_DFRN) AND isset($buttons["dislike"])) {
325 unset($buttons["dislike"],$isevent,$consensus);
329 if (($item["item_network"] == NETWORK_FEED) AND isset($buttons["like"]))
330 unset($buttons["like"]);
332 if (($item["item_network"] == NETWORK_MAIL) AND isset($buttons["like"]))
333 unset($buttons["like"]);
335 // Diaspora isn't able to do likes on comments - but red does
336 if (($item["item_network"] == NETWORK_DIASPORA) AND ($indent == 'comment') AND
337 !diaspora_is_redmatrix($item["owner-link"]) AND isset($buttons["like"]))
338 unset($buttons["like"]);
340 // Facebook can like comments - but it isn't programmed in the connector yet.
341 if (($item["item_network"] == NETWORK_FACEBOOK) AND ($indent == 'comment') AND isset($buttons["like"]))
342 unset($buttons["like"]);
345 'template' => $this->get_template(),
347 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
348 'tags' => $item['tags'],
349 'hashtags' => $item['hashtags'],
350 'mentions' => $item['mentions'],
351 'txt_cats' => t('Categories:'),
352 'txt_folders' => t('Filed under:'),
353 'has_cats' => ((count($categories)) ? 'true' : ''),
354 'has_folders' => ((count($folders)) ? 'true' : ''),
355 'categories' => $categories,
356 'folders' => $folders,
359 'id' => $this->get_id(),
360 'guid' => $item['guid'],
361 'isevent' => $isevent,
363 'consensus' => $consensus,
364 'conlabels' => $conlabels,
365 'canvote' => $canvote,
366 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
367 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $this->get_owner_name(), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
370 'wall' => t('Wall-to-Wall'),
371 'vwall' => t('via Wall-To-Wall:'),
372 'profile_url' => $profile_link,
373 'item_photo_menu' => item_photo_menu($item),
375 'thumb' => proxy_url($profile_avatar),
376 'osparkle' => $osparkle,
377 'sparkle' => $sparkle,
379 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
380 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
381 'app' => $item['app'],
382 'created' => relative_date($item['created']),
384 'location' => $location_e,
387 'owner_url' => $this->get_owner_url(),
388 'owner_photo' => proxy_url($this->get_owner_photo()),
389 'owner_name' => $owner_name_e,
390 'plink' => get_plink($item),
391 'edpost' => ((feature_enabled($conv->get_profile_owner(),'edit_posts')) ? $edpost : ''),
392 'isstarred' => $isstarred,
393 'star' => ((feature_enabled($conv->get_profile_owner(),'star_posts')) ? $star : ''),
394 'ignore' => ((feature_enabled($conv->get_profile_owner(),'ignore_posts')) ? $ignore : ''),
396 'filer' => ((feature_enabled($conv->get_profile_owner(),'filing')) ? $filer : ''),
399 'like' => $responses['like']['output'],
400 'dislike' => $responses['dislike']['output'],
401 'responses' => $responses,
402 'switchcomment' => t('Comment'),
403 'comment' => $this->get_comment_box($indent),
404 'previewing' => ($conv->is_preview() ? ' preview ' : ''),
405 'wait' => t('Please wait'),
406 'thread_level' => $thread_level,
407 'postopts' => $langstr,
409 'network' => $item["item_network"],
410 'network_name' => network_to_name($item['item_network'], $profile_link),
413 $arr = array('item' => $item, 'output' => $tmp_item);
414 call_hooks('display_item', $arr);
416 $result = $arr['output'];
418 $result['children'] = array();
419 $children = $this->get_children();
420 $nb_children = count($children);
421 if($nb_children > 0) {
422 foreach($children as $child) {
423 $result['children'][] = $child->get_template_data($conv_responses, $thread_level + 1);
426 if(($nb_children > 2) || ($thread_level > 1)) {
427 $result['children'][0]['comment_firstcollapsed'] = true;
428 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
429 $result['children'][0]['hidden_comments_num'] = $total_children;
430 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
431 $result['children'][0]['hide_text'] = t('show more');
432 if($thread_level > 1) {
433 $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
436 $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
441 if ($this->is_toplevel()) {
442 $result['total_comments_num'] = "$total_children";
443 $result['total_comments_text'] = tt('comment', 'comments', $total_children);
446 $result['private'] = $item['private'];
447 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
449 if($this->is_threaded()) {
450 $result['flatten'] = false;
451 $result['threaded'] = true;
454 $result['flatten'] = true;
455 $result['threaded'] = false;
461 public function get_id() {
462 return $this->get_data_value('id');
465 public function is_threaded() {
466 return $this->threaded;
472 public function add_child($item) {
473 $item_id = $item->get_id();
475 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
478 if($this->get_child($item->get_id())) {
479 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
483 * Only add what will be displayed
485 if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
488 if(activity_match($item->get_data_value('verb'),ACTIVITY_LIKE) || activity_match($item->get_data_value('verb'),ACTIVITY_DISLIKE)) {
492 $item->set_parent($this);
493 $this->children[] = $item;
494 return end($this->children);
498 * Get a child by its ID
500 public function get_child($id) {
501 foreach($this->get_children() as $child) {
502 if($child->get_id() == $id)
509 * Get all ou children
511 public function get_children() {
512 return $this->children;
518 protected function set_parent($item) {
519 $parent = $this->get_parent();
521 $parent->remove_child($this);
523 $this->parent = $item;
524 $this->set_conversation($item->get_conversation());
530 protected function remove_parent() {
531 $this->parent = null;
532 $this->conversation = null;
538 public function remove_child($item) {
539 $id = $item->get_id();
540 foreach($this->get_children() as $key => $child) {
541 if($child->get_id() == $id) {
542 $child->remove_parent();
543 unset($this->children[$key]);
544 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
545 $this->children = array_values($this->children);
549 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
556 protected function get_parent() {
557 return $this->parent;
563 public function set_conversation($conv) {
564 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
566 $this->conversation = $conv;
568 // Set it on our children too
569 foreach($this->get_children() as $child)
570 $child->set_conversation($conv);
576 public function get_conversation() {
577 return $this->conversation;
583 * We shouldn't need this
585 public function get_data() {
596 public function get_data_value($name) {
597 if(!isset($this->data[$name])) {
598 // logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
602 return $this->data[$name];
608 private function set_template($name) {
611 if(!x($this->available_templates, $name)) {
612 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
616 $this->template = $this->available_templates[$name];
622 private function get_template() {
623 return $this->template;
627 * Check if this is a toplevel post
629 private function is_toplevel() {
630 return $this->toplevel;
634 * Check if this is writable
636 private function is_writable() {
637 $conv = $this->get_conversation();
640 // This will allow us to comment on wall-to-wall items owned by our friends
641 // and community forums even if somebody else wrote the post.
643 // bug #517 - this fixes for conversation owner
644 if($conv->get_mode() == 'profile' && $conv->get_profile_owner() == local_user())
647 // this fixes for visitors
648 return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
650 return $this->writable;
654 * Count the total of our descendants
656 private function count_descendants() {
657 $children = $this->get_children();
658 $total = count($children);
660 foreach($children as $child) {
661 $total += $child->count_descendants();
668 * Get the template for the comment box
670 private function get_comment_box_template() {
671 return $this->comment_box_template;
675 * Get the comment box
678 * _ The comment box string (empty if no comment box)
681 private function get_comment_box($indent) {
682 $a = $this->get_app();
683 if(!$this->is_toplevel() && !(get_config('system','thread_allow') && $a->theme_thread_allow)) {
688 $conv = $this->get_conversation();
689 $template = get_markup_template($this->get_comment_box_template());
691 if( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
694 if($conv->is_writable() && $this->is_writable()) {
695 $qc = $qcomment = null;
698 * Hmmm, code depending on the presence of a particular plugin?
699 * This should be better if done by a hook
701 if(in_array('qcomment',$a->plugins)) {
702 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
703 $qcomment = (($qc) ? explode("\n",$qc) : null);
705 $comment_box = replace_macros($template,array(
706 '$return_path' => $a->query_string,
707 '$threaded' => $this->is_threaded(),
708 // '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
710 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
711 '$id' => $this->get_id(),
712 '$parent' => $this->get_id(),
713 '$qcomment' => $qcomment,
714 '$profile_uid' => $conv->get_profile_owner(),
715 '$mylink' => $a->contact['url'],
716 '$mytitle' => t('This is you'),
717 '$myphoto' => $a->contact['thumb'],
718 '$comment' => t('Comment'),
719 '$submit' => t('Submit'),
720 '$edbold' => t('Bold'),
721 '$editalic' => t('Italic'),
722 '$eduline' => t('Underline'),
723 '$edquote' => t('Quote'),
724 '$edcode' => t('Code'),
725 '$edimg' => t('Image'),
726 '$edurl' => t('Link'),
727 '$edvideo' => t('Video'),
728 '$preview' => ((feature_enabled($conv->get_profile_owner(),'preview')) ? t('Preview') : ''),
729 '$indent' => $indent,
730 '$sourceapp' => t($a->sourcename),
731 '$ww' => (($conv->get_mode() === 'network') ? $ww : ''),
732 '$rand_num' => random_digits(12)
739 private function get_redirect_url() {
740 return $this->redirect_url;
744 * Check if we are a wall to wall item and set the relevant properties
746 protected function check_wall_to_wall() {
747 $a = $this->get_app();
748 $conv = $this->get_conversation();
749 $this->wall_to_wall = false;
751 if($this->is_toplevel()) {
752 if($conv->get_mode() !== 'profile') {
753 if($this->get_data_value('wall') AND !$this->get_data_value('self')) {
754 // On the network page, I am the owner. On the display page it will be the profile owner.
755 // This will have been stored in $a->page_contact by our calling page.
756 // Put this person as the wall owner of the wall-to-wall notice.
758 $this->owner_url = zrl($a->page_contact['url']);
759 $this->owner_photo = $a->page_contact['thumb'];
760 $this->owner_name = $a->page_contact['name'];
761 $this->wall_to_wall = true;
763 else if($this->get_data_value('owner-link')) {
765 $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
766 $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
767 $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
769 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
771 // The author url doesn't match the owner (typically the contact)
772 // and also doesn't match the contact alias.
773 // The name match is a hack to catch several weird cases where URLs are
774 // all over the park. It can be tricked, but this prevents you from
775 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
776 // well that it's the same Bob Smith.
778 // But it could be somebody else with the same name. It just isn't highly likely.
781 $this->owner_photo = $this->get_data_value('owner-avatar');
782 $this->owner_name = $this->get_data_value('owner-name');
783 $this->wall_to_wall = true;
784 // If it is our contact, use a friendly redirect link
785 if((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url')))
786 && ($this->get_data_value('network') === NETWORK_DFRN)) {
787 $this->owner_url = $this->get_redirect_url();
790 $this->owner_url = zrl($this->get_data_value('owner-link'));
796 if(!$this->wall_to_wall) {
797 $this->set_template('wall');
798 $this->owner_url = '';
799 $this->owner_photo = '';
800 $this->owner_name = '';
804 private function is_wall_to_wall() {
805 return $this->wall_to_wall;
808 private function get_owner_url() {
809 return $this->owner_url;
812 private function get_owner_photo() {
813 return $this->owner_photo;
816 private function get_owner_name() {
817 return $this->owner_name;
820 private function is_visiting() {
821 return $this->visiting;