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 if ($item["event-id"] != 0)
122 $edpost = array($a->get_baseurl($ssl_state)."/events/event/".$item['event-id'], t("Edit"));
124 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
127 if(($this->get_data_value('uid') == local_user()) || $this->is_visiting())
131 'dropping' => $dropping,
132 'pagedrop' => ((feature_enabled($conv->get_profile_owner(),'multi_delete')) ? $item['pagedrop'] : ''),
133 'select' => t('Select'),
134 'delete' => t('Delete'),
137 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
139 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
140 $profile_name = htmlentities(((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
141 if($item['author-link'] && (! $item['author-name']))
142 $profile_name = $item['author-link'];
145 $profile_link = best_link_url($item,$sp);
146 if($profile_link === 'mailbox')
149 $sparkle = ' sparkle';
151 $profile_link = zrl($profile_link);
153 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
154 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
155 $profile_avatar = $a->contacts[$normalised]['thumb'];
157 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_data_value('thumb')));
159 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
160 call_hooks('render_location',$locate);
161 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
163 $searchpath = $a->get_baseurl()."/search?tag=";
169 /*foreach(explode(',',$item['tag']) as $tag){
181 // process action responses - e.g. like/dislike/attend/agree/whatever
182 $response_verbs = array('like');
183 if(feature_enabled($conv->get_profile_owner(),'dislike'))
184 $response_verbs[] = 'dislike';
185 if($item['object-type'] === ACTIVITY_OBJ_EVENT) {
186 $response_verbs[] = 'attendyes';
187 $response_verbs[] = 'attendno';
188 $response_verbs[] = 'attendmaybe';
189 if($conv->is_writable()) {
191 $attend = array( t('I will attend'), t('I will not attend'), t('I might attend'));
195 $responses = get_responses($conv_responses,$response_verbs,$this,$item);
197 foreach ($response_verbs as $value=>$verbs) {
198 $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']) : '');
203 * We should avoid doing this all the time, but it depends on the conversation mode
204 * And the conv mode may change when we change the conv, or it changes its mode
205 * Maybe we should establish a way to be notified about conversation changes
207 $this->check_wall_to_wall();
209 if($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url()))
210 $osparkle = ' sparkle';
212 if($this->is_toplevel()) {
213 if($conv->get_profile_owner() == local_user()) {
214 $isstarred = (($item['starred']) ? "starred" : "unstarred");
217 'do' => t("add star"),
218 'undo' => t("remove star"),
219 'toggle' => t("toggle star status"),
220 'classdo' => (($item['starred']) ? "hidden" : ""),
221 'classundo' => (($item['starred']) ? "" : "hidden"),
222 'starred' => t('starred'),
224 $r = q("SELECT `ignored` FROM `thread` WHERE `uid` = %d AND `iid` = %d LIMIT 1",
225 intval($item['uid']),
230 'do' => t("ignore thread"),
231 'undo' => t("unignore thread"),
232 'toggle' => t("toggle ignore status"),
233 'classdo' => (($r[0]['ignored']) ? "hidden" : ""),
234 'classundo' => (($r[0]['ignored']) ? "" : "hidden"),
235 'ignored' => t('ignored'),
240 if(feature_enabled($conv->get_profile_owner(),'commtag')) {
242 'add' => t("add tag"),
251 if($conv->is_writable()) {
253 'like' => array( t("I like this \x28toggle\x29"), t("like")),
254 'dislike' => ((feature_enabled($conv->get_profile_owner(),'dislike')) ? array( t("I don't like this \x28toggle\x29"), t("dislike")) : ''),
256 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
259 $comment = $this->get_comment_box($indent);
261 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0){
265 localize_item($item);
267 if ($item["postopts"] and !get_config("system", "suppress_language")) {
268 //$langdata = explode(";", $item["postopts"]);
269 //$langstr = substr($langdata[0], 5)." (".round($langdata[1]*100, 1)."%)";
271 if (substr($item["postopts"], 0, 5) == "lang=") {
272 $postopts = substr($item["postopts"], 5);
274 $languages = explode(":", $postopts);
276 if (sizeof($languages) == 1) {
277 $languages = array();
278 $languages[] = $postopts;
281 foreach ($languages as $language) {
282 $langdata = explode(";", $language);
286 //$langstr .= $langdata[0]." (".round($langdata[1]*100, 1)."%)";
287 $langstr .= round($langdata[1]*100, 1)."% ".$langdata[0];
292 $body = prepare_body($item,true);
294 list($categories, $folders) = get_cats_and_terms($item);
296 if($a->theme['template_engine'] === 'internal') {
297 $body_e = template_escape($body);
298 $text_e = strip_tags(template_escape($body));
299 $name_e = template_escape($profile_name);
300 $title_e = template_escape($item['title']);
301 $location_e = template_escape($location);
302 $owner_name_e = template_escape($this->get_owner_name());
306 $text_e = strip_tags($body);
307 $name_e = $profile_name;
308 $title_e = $item['title'];
309 $location_e = $location;
310 $owner_name_e = $this->get_owner_name();
313 // Disable features that aren't available in several networks
314 if (($item["item_network"] != NETWORK_DFRN) AND isset($buttons["dislike"])) {
315 unset($buttons["dislike"],$isevent);
319 if (($item["item_network"] == NETWORK_FEED) AND isset($buttons["like"]))
320 unset($buttons["like"]);
322 if (($item["item_network"] == NETWORK_MAIL) AND isset($buttons["like"]))
323 unset($buttons["like"]);
325 // Diaspora isn't able to do likes on comments - but red does
326 if (($item["item_network"] == NETWORK_DIASPORA) AND ($indent == 'comment') AND
327 !diaspora_is_redmatrix($item["owner-link"]) AND isset($buttons["like"]))
328 unset($buttons["like"]);
330 // Diaspora doesn't has multithreaded comments
331 if (($item["item_network"] == NETWORK_DIASPORA) AND ($indent == 'comment'))
334 // Facebook can like comments - but it isn't programmed in the connector yet.
335 if (($item["item_network"] == NETWORK_FACEBOOK) AND ($indent == 'comment') AND isset($buttons["like"]))
336 unset($buttons["like"]);
339 'template' => $this->get_template(),
341 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
342 'tags' => $item['tags'],
343 'hashtags' => $item['hashtags'],
344 'mentions' => $item['mentions'],
345 'txt_cats' => t('Categories:'),
346 'txt_folders' => t('Filed under:'),
347 'has_cats' => ((count($categories)) ? 'true' : ''),
348 'has_folders' => ((count($folders)) ? 'true' : ''),
349 'categories' => $categories,
350 'folders' => $folders,
353 'id' => $this->get_id(),
354 'guid' => urlencode($item['guid']),
355 'isevent' => $isevent,
357 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
358 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), htmlentities($this->get_owner_name()), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
361 'wall' => t('Wall-to-Wall'),
362 'vwall' => t('via Wall-To-Wall:'),
363 'profile_url' => $profile_link,
364 'item_photo_menu' => item_photo_menu($item),
366 'thumb' => proxy_url($profile_avatar, false, PROXY_SIZE_THUMB),
367 'osparkle' => $osparkle,
368 'sparkle' => $sparkle,
370 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
371 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
372 'app' => $item['app'],
373 'created' => relative_date($item['created']),
375 'location' => $location_e,
378 'owner_url' => $this->get_owner_url(),
379 'owner_photo' => proxy_url($this->get_owner_photo(), false, PROXY_SIZE_THUMB),
380 'owner_name' => htmlentities($owner_name_e),
381 'plink' => get_plink($item),
382 'edpost' => ((feature_enabled($conv->get_profile_owner(),'edit_posts')) ? $edpost : ''),
383 'isstarred' => $isstarred,
384 'star' => ((feature_enabled($conv->get_profile_owner(),'star_posts')) ? $star : ''),
385 'ignore' => ((feature_enabled($conv->get_profile_owner(),'ignore_posts')) ? $ignore : ''),
387 'filer' => ((feature_enabled($conv->get_profile_owner(),'filing')) ? $filer : ''),
390 'like' => $responses['like']['output'],
391 'dislike' => $responses['dislike']['output'],
392 'responses' => $responses,
393 'switchcomment' => t('Comment'),
394 'comment' => $comment,
395 'previewing' => ($conv->is_preview() ? ' preview ' : ''),
396 'wait' => t('Please wait'),
397 'thread_level' => $thread_level,
398 'postopts' => $langstr,
400 'network' => $item["item_network"],
401 'network_name' => network_to_name($item['item_network'], $profile_link),
404 $arr = array('item' => $item, 'output' => $tmp_item);
405 call_hooks('display_item', $arr);
407 $result = $arr['output'];
409 $result['children'] = array();
410 $children = $this->get_children();
411 $nb_children = count($children);
412 if($nb_children > 0) {
413 foreach($children as $child) {
414 $result['children'][] = $child->get_template_data($conv_responses, $thread_level + 1);
417 if(($nb_children > 2) || ($thread_level > 1)) {
418 $result['children'][0]['comment_firstcollapsed'] = true;
419 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
420 $result['children'][0]['hidden_comments_num'] = $total_children;
421 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
422 $result['children'][0]['hide_text'] = t('show more');
423 if($thread_level > 1) {
424 $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
427 $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
432 if ($this->is_toplevel()) {
433 $result['total_comments_num'] = "$total_children";
434 $result['total_comments_text'] = tt('comment', 'comments', $total_children);
437 $result['private'] = $item['private'];
438 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
440 if($this->is_threaded()) {
441 $result['flatten'] = false;
442 $result['threaded'] = true;
445 $result['flatten'] = true;
446 $result['threaded'] = false;
452 public function get_id() {
453 return $this->get_data_value('id');
456 public function is_threaded() {
457 return $this->threaded;
463 public function add_child($item) {
464 $item_id = $item->get_id();
466 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
469 if($this->get_child($item->get_id())) {
470 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
474 * Only add what will be displayed
476 if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
479 if(activity_match($item->get_data_value('verb'),ACTIVITY_LIKE) || activity_match($item->get_data_value('verb'),ACTIVITY_DISLIKE)) {
483 $item->set_parent($this);
484 $this->children[] = $item;
485 return end($this->children);
489 * Get a child by its ID
491 public function get_child($id) {
492 foreach($this->get_children() as $child) {
493 if($child->get_id() == $id)
500 * Get all ou children
502 public function get_children() {
503 return $this->children;
509 protected function set_parent($item) {
510 $parent = $this->get_parent();
512 $parent->remove_child($this);
514 $this->parent = $item;
515 $this->set_conversation($item->get_conversation());
521 protected function remove_parent() {
522 $this->parent = null;
523 $this->conversation = null;
529 public function remove_child($item) {
530 $id = $item->get_id();
531 foreach($this->get_children() as $key => $child) {
532 if($child->get_id() == $id) {
533 $child->remove_parent();
534 unset($this->children[$key]);
535 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
536 $this->children = array_values($this->children);
540 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
547 protected function get_parent() {
548 return $this->parent;
554 public function set_conversation($conv) {
555 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
557 $this->conversation = $conv;
559 // Set it on our children too
560 foreach($this->get_children() as $child)
561 $child->set_conversation($conv);
567 public function get_conversation() {
568 return $this->conversation;
574 * We shouldn't need this
576 public function get_data() {
587 public function get_data_value($name) {
588 if(!isset($this->data[$name])) {
589 // logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
593 return $this->data[$name];
599 private function set_template($name) {
602 if(!x($this->available_templates, $name)) {
603 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
607 $this->template = $this->available_templates[$name];
613 private function get_template() {
614 return $this->template;
618 * Check if this is a toplevel post
620 private function is_toplevel() {
621 return $this->toplevel;
625 * Check if this is writable
627 private function is_writable() {
628 $conv = $this->get_conversation();
631 // This will allow us to comment on wall-to-wall items owned by our friends
632 // and community forums even if somebody else wrote the post.
634 // bug #517 - this fixes for conversation owner
635 if($conv->get_mode() == 'profile' && $conv->get_profile_owner() == local_user())
638 // this fixes for visitors
639 return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
641 return $this->writable;
645 * Count the total of our descendants
647 private function count_descendants() {
648 $children = $this->get_children();
649 $total = count($children);
651 foreach($children as $child) {
652 $total += $child->count_descendants();
659 * Get the template for the comment box
661 private function get_comment_box_template() {
662 return $this->comment_box_template;
666 * Get the comment box
669 * _ The comment box string (empty if no comment box)
672 private function get_comment_box($indent) {
673 $a = $this->get_app();
674 if(!$this->is_toplevel() && !(get_config('system','thread_allow') && $a->theme_thread_allow)) {
679 $conv = $this->get_conversation();
680 $template = get_markup_template($this->get_comment_box_template());
682 if( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
685 if($conv->is_writable() && $this->is_writable()) {
686 $qc = $qcomment = null;
689 * Hmmm, code depending on the presence of a particular plugin?
690 * This should be better if done by a hook
692 if(in_array('qcomment',$a->plugins)) {
693 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
694 $qcomment = (($qc) ? explode("\n",$qc) : null);
696 $comment_box = replace_macros($template,array(
697 '$return_path' => $a->query_string,
698 '$threaded' => $this->is_threaded(),
699 // '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
701 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
702 '$id' => $this->get_id(),
703 '$parent' => $this->get_id(),
704 '$qcomment' => $qcomment,
705 '$profile_uid' => $conv->get_profile_owner(),
706 '$mylink' => $a->contact['url'],
707 '$mytitle' => t('This is you'),
708 '$myphoto' => $a->contact['thumb'],
709 '$comment' => t('Comment'),
710 '$submit' => t('Submit'),
711 '$edbold' => t('Bold'),
712 '$editalic' => t('Italic'),
713 '$eduline' => t('Underline'),
714 '$edquote' => t('Quote'),
715 '$edcode' => t('Code'),
716 '$edimg' => t('Image'),
717 '$edurl' => t('Link'),
718 '$edvideo' => t('Video'),
719 '$preview' => ((feature_enabled($conv->get_profile_owner(),'preview')) ? t('Preview') : ''),
720 '$indent' => $indent,
721 '$sourceapp' => t($a->sourcename),
722 '$ww' => (($conv->get_mode() === 'network') ? $ww : ''),
723 '$rand_num' => random_digits(12)
730 private function get_redirect_url() {
731 return $this->redirect_url;
735 * Check if we are a wall to wall item and set the relevant properties
737 protected function check_wall_to_wall() {
738 $a = $this->get_app();
739 $conv = $this->get_conversation();
740 $this->wall_to_wall = false;
742 if($this->is_toplevel()) {
743 if($conv->get_mode() !== 'profile') {
744 if($this->get_data_value('wall') AND !$this->get_data_value('self')) {
745 // On the network page, I am the owner. On the display page it will be the profile owner.
746 // This will have been stored in $a->page_contact by our calling page.
747 // Put this person as the wall owner of the wall-to-wall notice.
749 $this->owner_url = zrl($a->page_contact['url']);
750 $this->owner_photo = $a->page_contact['thumb'];
751 $this->owner_name = $a->page_contact['name'];
752 $this->wall_to_wall = true;
754 else if($this->get_data_value('owner-link')) {
756 $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
757 $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
758 $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
760 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
762 // The author url doesn't match the owner (typically the contact)
763 // and also doesn't match the contact alias.
764 // The name match is a hack to catch several weird cases where URLs are
765 // all over the park. It can be tricked, but this prevents you from
766 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
767 // well that it's the same Bob Smith.
769 // But it could be somebody else with the same name. It just isn't highly likely.
772 $this->owner_photo = $this->get_data_value('owner-avatar');
773 $this->owner_name = $this->get_data_value('owner-name');
774 $this->wall_to_wall = true;
775 // If it is our contact, use a friendly redirect link
776 if((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url')))
777 && ($this->get_data_value('network') === NETWORK_DFRN)) {
778 $this->owner_url = $this->get_redirect_url();
781 $this->owner_url = zrl($this->get_data_value('owner-link'));
787 if(!$this->wall_to_wall) {
788 $this->set_template('wall');
789 $this->owner_url = '';
790 $this->owner_photo = '';
791 $this->owner_name = '';
795 private function is_wall_to_wall() {
796 return $this->wall_to_wall;
799 private function get_owner_url() {
800 return $this->owner_url;
803 private function get_owner_photo() {
804 return $this->owner_photo;
807 private function get_owner_name() {
808 return $this->owner_name;
811 private function is_visiting() {
812 return $this->visiting;