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 = 'redir/' . $this->get_data_value('cid') ;
55 if (get_config('system','thread_allow') && $a->theme_thread_allow && !$this->is_toplevel()) {
56 $this->threaded = true;
59 // Prepare the children
60 if (count($data['children'])) {
61 foreach ($data['children'] as $item) {
63 * Only add will be displayed
65 if ($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
67 } elseif (! visible_activity($item)) {
71 $item['pagedrop'] = $data['pagedrop'];
72 $child = new Item($item);
73 $this->add_child($child);
79 * Get data in a form usable by a conversation template
82 * _ The data requested on success
85 public function get_template_data($conv_responses, $thread_level=1) {
86 require_once("mod/proxy.php");
90 $a = $this->get_app();
92 $item = $this->get_data();
94 if (strcmp($item['created'], $item['edited'])<>0) {
96 'label' => t('This entry was edited'),
97 'date' => datetime_convert('UTC', date_default_timezone_get(), $item['edited'], 'r'),
98 'relative' => relative_date($item['edited'])
107 $isstarred = "unstarred";
111 $total_children = $this->count_descendants();
113 $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("events/event/".$item['event-id'], t("Edit"));
124 $edpost = array("editpost/".$item['id'], t("Edit"));
130 if (($this->get_data_value('uid') == local_user()) || $this->is_visiting()) {
135 'dropping' => $dropping,
136 'pagedrop' => ((feature_enabled($conv->get_profile_owner(),'multi_delete')) ? $item['pagedrop'] : ''),
137 'select' => t('Select'),
138 'delete' => t('Delete'),
141 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
143 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
144 $profile_name = htmlentities(((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
145 if ($item['author-link'] && (! $item['author-name'])) {
146 $profile_name = $item['author-link'];
150 $profile_link = best_link_url($item,$sp);
151 if ($profile_link === 'mailbox') {
156 $sparkle = ' sparkle';
158 $profile_link = zrl($profile_link);
161 if (!isset($item['author-thumb']) || ($item['author-thumb'] == "")) {
162 $author_contact = get_contact_details_by_url($item['author-link'], $conv->get_profile_owner());
163 if ($author_contact["thumb"]) {
164 $item['author-thumb'] = $author_contact["thumb"];
166 $item['author-thumb'] = $item['author-avatar'];
170 if (!isset($item['owner-thumb']) || ($item['owner-thumb'] == "")) {
171 $owner_contact = get_contact_details_by_url($item['owner-link'], $conv->get_profile_owner());
172 if ($owner_contact["thumb"]) {
173 $item['owner-thumb'] = $owner_contact["thumb"];
175 $item['owner-thumb'] = $item['owner-avatar'];
179 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
180 call_hooks('render_location',$locate);
181 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
188 /*foreach(explode(',',$item['tag']) as $tag){
200 // process action responses - e.g. like/dislike/attend/agree/whatever
201 $response_verbs = array('like');
202 if (feature_enabled($conv->get_profile_owner(),'dislike')) {
203 $response_verbs[] = 'dislike';
205 if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
206 $response_verbs[] = 'attendyes';
207 $response_verbs[] = 'attendno';
208 $response_verbs[] = 'attendmaybe';
209 if ($conv->is_writable()) {
211 $attend = array( t('I will attend'), t('I will not attend'), t('I might attend'));
215 $responses = get_responses($conv_responses,$response_verbs,$this,$item);
217 foreach ($response_verbs as $value=>$verbs) {
218 $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']) : '');
222 * We should avoid doing this all the time, but it depends on the conversation mode
223 * And the conv mode may change when we change the conv, or it changes its mode
224 * Maybe we should establish a way to be notified about conversation changes
226 $this->check_wall_to_wall();
228 if ($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url())) {
229 $osparkle = ' sparkle';
232 if ($this->is_toplevel()) {
233 if ($conv->get_profile_owner() == local_user()) {
234 $isstarred = (($item['starred']) ? "starred" : "unstarred");
237 'do' => t("add star"),
238 'undo' => t("remove star"),
239 'toggle' => t("toggle star status"),
240 'classdo' => (($item['starred']) ? "hidden" : ""),
241 'classundo' => (($item['starred']) ? "" : "hidden"),
242 'starred' => t('starred'),
244 $r = dba::select('thread', array('ignored'), array('uid' => $item['uid'], 'iid' => $item['id']), array('limit' => 1));
245 if (dbm::is_result($r)) {
247 'do' => t("ignore thread"),
248 'undo' => t("unignore thread"),
249 'toggle' => t("toggle ignore status"),
250 'classdo' => (($r['ignored']) ? "hidden" : ""),
251 'classundo' => (($r['ignored']) ? "" : "hidden"),
252 'ignored' => t('ignored'),
257 if(feature_enabled($conv->get_profile_owner(),'commtag')) {
259 'add' => t("add tag"),
268 if ($conv->is_writable()) {
270 'like' => array( t("I like this \x28toggle\x29"), t("like")),
271 'dislike' => ((feature_enabled($conv->get_profile_owner(),'dislike')) ? array( t("I don't like this \x28toggle\x29"), t("dislike")) : ''),
274 $buttons['share'] = array( t('Share this'), t('share'));
278 $comment = $this->get_comment_box($indent);
280 if (strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0){
284 localize_item($item);
286 $body = prepare_body($item,true);
288 list($categories, $folders) = get_cats_and_terms($item);
290 if ($a->theme['template_engine'] === 'internal') {
291 $body_e = template_escape($body);
292 $text_e = strip_tags(template_escape($body));
293 $name_e = template_escape($profile_name);
294 $title_e = template_escape($item['title']);
295 $location_e = template_escape($location);
296 $owner_name_e = template_escape($this->get_owner_name());
299 $text_e = strip_tags($body);
300 $name_e = $profile_name;
301 $title_e = $item['title'];
302 $location_e = $location;
303 $owner_name_e = $this->get_owner_name();
306 // Disable features that aren't available in several networks
308 /// @todo Add NETWORK_DIASPORA when it will pass this information
309 if (!in_array($item["item_network"], array(NETWORK_DFRN)) && isset($buttons["dislike"])) {
310 unset($buttons["dislike"],$isevent);
314 if (($item["item_network"] == NETWORK_FEED) && isset($buttons["like"])) {
315 unset($buttons["like"]);
318 if (($item["item_network"] == NETWORK_MAIL) && isset($buttons["like"])) {
319 unset($buttons["like"]);
322 // Diaspora isn't able to do likes on comments - but Hubzilla does
323 /// @todo When Diaspora will pass this information we will remove these lines
324 if (($item["item_network"] == NETWORK_DIASPORA) && ($indent == 'comment') &&
325 !Diaspora::is_redmatrix($item["owner-link"]) && isset($buttons["like"])) {
326 unset($buttons["like"]);
329 // Facebook can like comments - but it isn't programmed in the connector yet.
330 if (($item["item_network"] == NETWORK_FACEBOOK) && ($indent == 'comment') && isset($buttons["like"])) {
331 unset($buttons["like"]);
335 'template' => $this->get_template(),
336 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
337 'tags' => $item['tags'],
338 'hashtags' => $item['hashtags'],
339 'mentions' => $item['mentions'],
340 'txt_cats' => t('Categories:'),
341 'txt_folders' => t('Filed under:'),
342 'has_cats' => ((count($categories)) ? 'true' : ''),
343 'has_folders' => ((count($folders)) ? 'true' : ''),
344 'categories' => $categories,
345 'folders' => $folders,
348 'id' => $this->get_id(),
349 'guid' => urlencode($item['guid']),
350 'isevent' => $isevent,
352 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
353 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), htmlentities($this->get_owner_name()), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
356 'wall' => t('Wall-to-Wall'),
357 'vwall' => t('via Wall-To-Wall:'),
358 'profile_url' => $profile_link,
359 'item_photo_menu' => item_photo_menu($item),
361 'thumb' => $a->remove_baseurl(proxy_url($item['author-thumb'], false, PROXY_SIZE_THUMB)),
362 'osparkle' => $osparkle,
363 'sparkle' => $sparkle,
365 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
366 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
367 'app' => $item['app'],
368 'created' => relative_date($item['created']),
370 'location' => $location_e,
373 'owner_url' => $this->get_owner_url(),
374 'owner_photo' => $a->remove_baseurl(proxy_url($item['owner-thumb'], false, PROXY_SIZE_THUMB)),
375 'owner_name' => htmlentities($owner_name_e),
376 'plink' => get_plink($item),
377 'edpost' => ((feature_enabled($conv->get_profile_owner(),'edit_posts')) ? $edpost : ''),
378 'isstarred' => $isstarred,
379 'star' => ((feature_enabled($conv->get_profile_owner(),'star_posts')) ? $star : ''),
380 'ignore' => ((feature_enabled($conv->get_profile_owner(),'ignore_posts')) ? $ignore : ''),
382 'filer' => ((feature_enabled($conv->get_profile_owner(),'filing')) ? $filer : ''),
385 'like' => $responses['like']['output'],
386 'dislike' => $responses['dislike']['output'],
387 'responses' => $responses,
388 'switchcomment' => t('Comment'),
389 'comment' => $comment,
390 'previewing' => ($conv->is_preview() ? ' preview ' : ''),
391 'wait' => t('Please wait'),
392 'thread_level' => $thread_level,
394 'network' => $item["item_network"],
395 'network_name' => network_to_name($item['item_network'], $profile_link),
396 'received' => $item['received'],
397 'commented' => $item['commented'],
398 'created_date' => $item['created'],
401 $arr = array('item' => $item, 'output' => $tmp_item);
402 call_hooks('display_item', $arr);
404 $result = $arr['output'];
406 $result['children'] = array();
407 $children = $this->get_children();
408 $nb_children = count($children);
409 if ($nb_children > 0) {
410 foreach ($children as $child) {
411 $result['children'][] = $child->get_template_data($conv_responses, $thread_level + 1);
414 if (($nb_children > 2) || ($thread_level > 1)) {
415 $result['children'][0]['comment_firstcollapsed'] = true;
416 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
417 $result['children'][0]['hidden_comments_num'] = $total_children;
418 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
419 $result['children'][0]['hide_text'] = t('show more');
420 if ($thread_level > 1) {
421 $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
423 $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
428 if ($this->is_toplevel()) {
429 $result['total_comments_num'] = "$total_children";
430 $result['total_comments_text'] = tt('comment', 'comments', $total_children);
433 $result['private'] = $item['private'];
434 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
436 if ($this->is_threaded()) {
437 $result['flatten'] = false;
438 $result['threaded'] = true;
440 $result['flatten'] = true;
441 $result['threaded'] = false;
447 public function get_id() {
448 return $this->get_data_value('id');
451 public function is_threaded() {
452 return $this->threaded;
458 public function add_child(Item $item) {
459 $item_id = $item->get_id();
461 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
463 } elseif ($this->get_child($item->get_id())) {
464 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
468 * Only add what will be displayed
470 if ($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
472 } elseif (activity_match($item->get_data_value('verb'),ACTIVITY_LIKE) || activity_match($item->get_data_value('verb'),ACTIVITY_DISLIKE)) {
476 $item->set_parent($this);
477 $this->children[] = $item;
478 return end($this->children);
482 * Get a child by its ID
484 public function get_child($id) {
485 foreach ($this->get_children() as $child) {
486 if ($child->get_id() == $id) {
494 * Get all ou children
496 public function get_children() {
497 return $this->children;
503 protected function set_parent($item) {
504 $parent = $this->get_parent();
506 $parent->remove_child($this);
508 $this->parent = $item;
509 $this->set_conversation($item->get_conversation());
515 protected function remove_parent() {
516 $this->parent = null;
517 $this->conversation = null;
523 public function remove_child($item) {
524 $id = $item->get_id();
525 foreach ($this->get_children() as $key => $child) {
526 if ($child->get_id() == $id) {
527 $child->remove_parent();
528 unset($this->children[$key]);
529 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
530 $this->children = array_values($this->children);
534 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
541 protected function get_parent() {
542 return $this->parent;
548 public function set_conversation($conv) {
549 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
551 $this->conversation = $conv;
553 // Set it on our children too
554 foreach ($this->get_children() as $child) {
555 $child->set_conversation($conv);
562 public function get_conversation() {
563 return $this->conversation;
569 * We shouldn't need this
571 public function get_data() {
582 public function get_data_value($name) {
583 if (!isset($this->data[$name])) {
584 // logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
588 return $this->data[$name];
594 private function set_template($name) {
595 if (!x($this->available_templates, $name)) {
596 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
600 $this->template = $this->available_templates[$name];
606 private function get_template() {
607 return $this->template;
611 * Check if this is a toplevel post
613 private function is_toplevel() {
614 return $this->toplevel;
618 * Check if this is writable
620 private function is_writable() {
621 $conv = $this->get_conversation();
624 // This will allow us to comment on wall-to-wall items owned by our friends
625 // and community forums even if somebody else wrote the post.
627 // bug #517 - this fixes for conversation owner
628 if ($conv->get_mode() == 'profile' && $conv->get_profile_owner() == local_user()) {
632 // this fixes for visitors
633 return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
635 return $this->writable;
639 * Count the total of our descendants
641 private function count_descendants() {
642 $children = $this->get_children();
643 $total = count($children);
645 foreach ($children as $child) {
646 $total += $child->count_descendants();
653 * Get the template for the comment box
655 private function get_comment_box_template() {
656 return $this->comment_box_template;
660 * Get the comment box
663 * _ The comment box string (empty if no comment box)
666 private function get_comment_box($indent) {
667 $a = $this->get_app();
668 if (!$this->is_toplevel() && !(get_config('system','thread_allow') && $a->theme_thread_allow)) {
673 $conv = $this->get_conversation();
674 $template = get_markup_template($this->get_comment_box_template());
676 if ( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
679 if ($conv->is_writable() && $this->is_writable()) {
680 $qc = $qcomment = null;
683 * Hmmm, code depending on the presence of a particular plugin?
684 * This should be better if done by a hook
686 if (in_array('qcomment',$a->plugins)) {
687 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
688 $qcomment = (($qc) ? explode("\n",$qc) : null);
690 $comment_box = replace_macros($template,array(
691 '$return_path' => $a->query_string,
692 '$threaded' => $this->is_threaded(),
693 // '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
695 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
696 '$id' => $this->get_id(),
697 '$parent' => $this->get_id(),
698 '$qcomment' => $qcomment,
699 '$profile_uid' => $conv->get_profile_owner(),
700 '$mylink' => $a->remove_baseurl($a->contact['url']),
701 '$mytitle' => t('This is you'),
702 '$myphoto' => $a->remove_baseurl($a->contact['thumb']),
703 '$comment' => t('Comment'),
704 '$submit' => t('Submit'),
705 '$edbold' => t('Bold'),
706 '$editalic' => t('Italic'),
707 '$eduline' => t('Underline'),
708 '$edquote' => t('Quote'),
709 '$edcode' => t('Code'),
710 '$edimg' => t('Image'),
711 '$edurl' => t('Link'),
712 '$edvideo' => t('Video'),
713 '$preview' => ((feature_enabled($conv->get_profile_owner(),'preview')) ? t('Preview') : ''),
714 '$indent' => $indent,
715 '$sourceapp' => t($a->sourcename),
716 '$ww' => (($conv->get_mode() === 'network') ? $ww : ''),
717 '$rand_num' => random_digits(12)
724 private function get_redirect_url() {
725 return $this->redirect_url;
729 * Check if we are a wall to wall item and set the relevant properties
731 protected function check_wall_to_wall() {
732 $a = $this->get_app();
733 $conv = $this->get_conversation();
734 $this->wall_to_wall = false;
736 if($this->is_toplevel()) {
737 if($conv->get_mode() !== 'profile') {
738 if($this->get_data_value('wall') && !$this->get_data_value('self')) {
739 // On the network page, I am the owner. On the display page it will be the profile owner.
740 // This will have been stored in $a->page_contact by our calling page.
741 // Put this person as the wall owner of the wall-to-wall notice.
743 $this->owner_url = zrl($a->page_contact['url']);
744 $this->owner_photo = $a->page_contact['thumb'];
745 $this->owner_name = $a->page_contact['name'];
746 $this->wall_to_wall = true;
747 } elseif($this->get_data_value('owner-link')) {
749 $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
750 $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
751 $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
753 if ((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
755 // The author url doesn't match the owner (typically the contact)
756 // and also doesn't match the contact alias.
757 // The name match is a hack to catch several weird cases where URLs are
758 // all over the park. It can be tricked, but this prevents you from
759 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
760 // well that it's the same Bob Smith.
762 // But it could be somebody else with the same name. It just isn't highly likely.
765 $this->owner_photo = $this->get_data_value('owner-avatar');
766 $this->owner_name = $this->get_data_value('owner-name');
767 $this->wall_to_wall = true;
768 // If it is our contact, use a friendly redirect link
769 if ((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url')))
770 && ($this->get_data_value('network') === NETWORK_DFRN)) {
771 $this->owner_url = $this->get_redirect_url();
773 $this->owner_url = zrl($this->get_data_value('owner-link'));
780 if (!$this->wall_to_wall) {
781 $this->set_template('wall');
782 $this->owner_url = '';
783 $this->owner_photo = '';
784 $this->owner_name = '';
788 private function is_wall_to_wall() {
789 return $this->wall_to_wall;
792 private function get_owner_url() {
793 return $this->owner_url;
796 private function get_owner_photo() {
797 return $this->owner_photo;
800 private function get_owner_name() {
801 return $this->owner_name;
804 private function is_visiting() {
805 return $this->visiting;