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 // You can always comment on Diaspora items
72 if (($item['network'] == NETWORK_DIASPORA) && (local_user() == $item['uid'])) {
73 $item['writable'] = true;
76 $item['pagedrop'] = $data['pagedrop'];
77 $child = new Item($item);
78 $this->add_child($child);
84 * Get data in a form usable by a conversation template
87 * _ The data requested on success
90 public function get_template_data($conv_responses, $thread_level=1) {
91 require_once("mod/proxy.php");
95 $a = $this->get_app();
97 $item = $this->get_data();
99 if (strcmp($item['created'], $item['edited'])<>0) {
101 'label' => t('This entry was edited'),
102 'date' => datetime_convert('UTC', date_default_timezone_get(), $item['edited'], 'r'),
103 'relative' => relative_date($item['edited'])
112 $isstarred = "unstarred";
116 $total_children = $this->count_descendants();
118 $conv = $this->get_conversation();
120 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
121 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
122 ? t('Private Message')
124 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
125 if (local_user() && link_compare($a->contact['url'],$item['author-link'])) {
126 if ($item["event-id"] != 0) {
127 $edpost = array("events/event/".$item['event-id'], t("Edit"));
129 $edpost = array("editpost/".$item['id'], t("Edit"));
135 if (($this->get_data_value('uid') == local_user()) || $this->is_visiting()) {
140 'dropping' => $dropping,
141 'pagedrop' => ((feature_enabled($conv->get_profile_owner(),'multi_delete')) ? $item['pagedrop'] : ''),
142 'select' => t('Select'),
143 'delete' => t('Delete'),
146 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
148 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
149 $profile_name = htmlentities(((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
150 if ($item['author-link'] && (! $item['author-name'])) {
151 $profile_name = $item['author-link'];
155 $profile_link = best_link_url($item,$sp);
156 if ($profile_link === 'mailbox') {
161 $sparkle = ' sparkle';
163 $profile_link = zrl($profile_link);
166 if (!isset($item['author-thumb']) || ($item['author-thumb'] == "")) {
167 $author_contact = get_contact_details_by_url($item['author-link'], $conv->get_profile_owner());
168 if ($author_contact["thumb"]) {
169 $item['author-thumb'] = $author_contact["thumb"];
171 $item['author-thumb'] = $item['author-avatar'];
175 if (!isset($item['owner-thumb']) || ($item['owner-thumb'] == "")) {
176 $owner_contact = get_contact_details_by_url($item['owner-link'], $conv->get_profile_owner());
177 if ($owner_contact["thumb"]) {
178 $item['owner-thumb'] = $owner_contact["thumb"];
180 $item['owner-thumb'] = $item['owner-avatar'];
184 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
185 call_hooks('render_location',$locate);
186 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
193 /*foreach(explode(',',$item['tag']) as $tag){
205 // process action responses - e.g. like/dislike/attend/agree/whatever
206 $response_verbs = array('like');
207 if (feature_enabled($conv->get_profile_owner(),'dislike')) {
208 $response_verbs[] = 'dislike';
210 if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
211 $response_verbs[] = 'attendyes';
212 $response_verbs[] = 'attendno';
213 $response_verbs[] = 'attendmaybe';
214 if ($conv->is_writable()) {
216 $attend = array( t('I will attend'), t('I will not attend'), t('I might attend'));
220 $responses = get_responses($conv_responses,$response_verbs,$this,$item);
222 foreach ($response_verbs as $value=>$verbs) {
223 $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']) : '');
227 * We should avoid doing this all the time, but it depends on the conversation mode
228 * And the conv mode may change when we change the conv, or it changes its mode
229 * Maybe we should establish a way to be notified about conversation changes
231 $this->check_wall_to_wall();
233 if ($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url())) {
234 $osparkle = ' sparkle';
237 if ($this->is_toplevel()) {
238 if ($conv->get_profile_owner() == local_user()) {
239 $isstarred = (($item['starred']) ? "starred" : "unstarred");
242 'do' => t("add star"),
243 'undo' => t("remove star"),
244 'toggle' => t("toggle star status"),
245 'classdo' => (($item['starred']) ? "hidden" : ""),
246 'classundo' => (($item['starred']) ? "" : "hidden"),
247 'starred' => t('starred'),
249 $r = dba::select('thread', array('ignored'), array('uid' => $item['uid'], 'iid' => $item['id']), array('limit' => 1));
250 if (dbm::is_result($r)) {
252 'do' => t("ignore thread"),
253 'undo' => t("unignore thread"),
254 'toggle' => t("toggle ignore status"),
255 'classdo' => (($r['ignored']) ? "hidden" : ""),
256 'classundo' => (($r['ignored']) ? "" : "hidden"),
257 'ignored' => t('ignored'),
262 if(feature_enabled($conv->get_profile_owner(),'commtag')) {
264 'add' => t("add tag"),
273 if ($conv->is_writable()) {
275 'like' => array( t("I like this \x28toggle\x29"), t("like")),
276 'dislike' => ((feature_enabled($conv->get_profile_owner(),'dislike')) ? array( t("I don't like this \x28toggle\x29"), t("dislike")) : ''),
279 $buttons['share'] = array( t('Share this'), t('share'));
283 $comment = $this->get_comment_box($indent);
285 if (strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0){
289 localize_item($item);
291 $body = prepare_body($item,true);
293 list($categories, $folders) = get_cats_and_terms($item);
295 if ($a->theme['template_engine'] === 'internal') {
296 $body_e = template_escape($body);
297 $text_e = strip_tags(template_escape($body));
298 $name_e = template_escape($profile_name);
299 $title_e = template_escape($item['title']);
300 $location_e = template_escape($location);
301 $owner_name_e = template_escape($this->get_owner_name());
304 $text_e = strip_tags($body);
305 $name_e = $profile_name;
306 $title_e = $item['title'];
307 $location_e = $location;
308 $owner_name_e = $this->get_owner_name();
311 // Disable features that aren't available in several networks
313 /// @todo Add NETWORK_DIASPORA when it will pass this information
314 if (!in_array($item["item_network"], array(NETWORK_DFRN)) && isset($buttons["dislike"])) {
315 unset($buttons["dislike"],$isevent);
319 if (($item["item_network"] == NETWORK_FEED) && isset($buttons["like"])) {
320 unset($buttons["like"]);
323 if (($item["item_network"] == NETWORK_MAIL) && isset($buttons["like"])) {
324 unset($buttons["like"]);
328 'template' => $this->get_template(),
329 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
330 'tags' => $item['tags'],
331 'hashtags' => $item['hashtags'],
332 'mentions' => $item['mentions'],
333 'txt_cats' => t('Categories:'),
334 'txt_folders' => t('Filed under:'),
335 'has_cats' => ((count($categories)) ? 'true' : ''),
336 'has_folders' => ((count($folders)) ? 'true' : ''),
337 'categories' => $categories,
338 'folders' => $folders,
341 'id' => $this->get_id(),
342 'guid' => urlencode($item['guid']),
343 'isevent' => $isevent,
345 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
346 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), htmlentities($this->get_owner_name()), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
349 'wall' => t('Wall-to-Wall'),
350 'vwall' => t('via Wall-To-Wall:'),
351 'profile_url' => $profile_link,
352 'item_photo_menu' => item_photo_menu($item),
354 'thumb' => $a->remove_baseurl(proxy_url($item['author-thumb'], false, PROXY_SIZE_THUMB)),
355 'osparkle' => $osparkle,
356 'sparkle' => $sparkle,
358 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
359 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
360 'app' => $item['app'],
361 'created' => relative_date($item['created']),
363 'location' => $location_e,
366 'owner_url' => $this->get_owner_url(),
367 'owner_photo' => $a->remove_baseurl(proxy_url($item['owner-thumb'], false, PROXY_SIZE_THUMB)),
368 'owner_name' => htmlentities($owner_name_e),
369 'plink' => get_plink($item),
370 'edpost' => ((feature_enabled($conv->get_profile_owner(),'edit_posts')) ? $edpost : ''),
371 'isstarred' => $isstarred,
372 'star' => ((feature_enabled($conv->get_profile_owner(),'star_posts')) ? $star : ''),
373 'ignore' => ((feature_enabled($conv->get_profile_owner(),'ignore_posts')) ? $ignore : ''),
375 'filer' => ((feature_enabled($conv->get_profile_owner(),'filing')) ? $filer : ''),
378 'like' => $responses['like']['output'],
379 'dislike' => $responses['dislike']['output'],
380 'responses' => $responses,
381 'switchcomment' => t('Comment'),
382 'comment' => $comment,
383 'previewing' => ($conv->is_preview() ? ' preview ' : ''),
384 'wait' => t('Please wait'),
385 'thread_level' => $thread_level,
387 'network' => $item["item_network"],
388 'network_name' => network_to_name($item['item_network'], $profile_link),
389 'received' => $item['received'],
390 'commented' => $item['commented'],
391 'created_date' => $item['created'],
394 $arr = array('item' => $item, 'output' => $tmp_item);
395 call_hooks('display_item', $arr);
397 $result = $arr['output'];
399 $result['children'] = array();
400 $children = $this->get_children();
401 $nb_children = count($children);
402 if ($nb_children > 0) {
403 foreach ($children as $child) {
404 $result['children'][] = $child->get_template_data($conv_responses, $thread_level + 1);
407 if (($nb_children > 2) || ($thread_level > 1)) {
408 $result['children'][0]['comment_firstcollapsed'] = true;
409 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
410 $result['children'][0]['hidden_comments_num'] = $total_children;
411 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
412 $result['children'][0]['hide_text'] = t('show more');
413 if ($thread_level > 1) {
414 $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
416 $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
421 if ($this->is_toplevel()) {
422 $result['total_comments_num'] = "$total_children";
423 $result['total_comments_text'] = tt('comment', 'comments', $total_children);
426 $result['private'] = $item['private'];
427 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
429 if ($this->is_threaded()) {
430 $result['flatten'] = false;
431 $result['threaded'] = true;
433 $result['flatten'] = true;
434 $result['threaded'] = false;
440 public function get_id() {
441 return $this->get_data_value('id');
444 public function is_threaded() {
445 return $this->threaded;
451 public function add_child(Item $item) {
452 $item_id = $item->get_id();
454 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
456 } elseif ($this->get_child($item->get_id())) {
457 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
461 * Only add what will be displayed
463 if ($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
465 } elseif (activity_match($item->get_data_value('verb'),ACTIVITY_LIKE) || activity_match($item->get_data_value('verb'),ACTIVITY_DISLIKE)) {
469 $item->set_parent($this);
470 $this->children[] = $item;
471 return end($this->children);
475 * Get a child by its ID
477 public function get_child($id) {
478 foreach ($this->get_children() as $child) {
479 if ($child->get_id() == $id) {
487 * Get all ou children
489 public function get_children() {
490 return $this->children;
496 protected function set_parent($item) {
497 $parent = $this->get_parent();
499 $parent->remove_child($this);
501 $this->parent = $item;
502 $this->set_conversation($item->get_conversation());
508 protected function remove_parent() {
509 $this->parent = null;
510 $this->conversation = null;
516 public function remove_child($item) {
517 $id = $item->get_id();
518 foreach ($this->get_children() as $key => $child) {
519 if ($child->get_id() == $id) {
520 $child->remove_parent();
521 unset($this->children[$key]);
522 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
523 $this->children = array_values($this->children);
527 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
534 protected function get_parent() {
535 return $this->parent;
541 public function set_conversation($conv) {
542 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
544 $this->conversation = $conv;
546 // Set it on our children too
547 foreach ($this->get_children() as $child) {
548 $child->set_conversation($conv);
555 public function get_conversation() {
556 return $this->conversation;
562 * We shouldn't need this
564 public function get_data() {
575 public function get_data_value($name) {
576 if (!isset($this->data[$name])) {
577 // logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
581 return $this->data[$name];
587 private function set_template($name) {
588 if (!x($this->available_templates, $name)) {
589 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
593 $this->template = $this->available_templates[$name];
599 private function get_template() {
600 return $this->template;
604 * Check if this is a toplevel post
606 private function is_toplevel() {
607 return $this->toplevel;
611 * Check if this is writable
613 private function is_writable() {
614 $conv = $this->get_conversation();
617 // This will allow us to comment on wall-to-wall items owned by our friends
618 // and community forums even if somebody else wrote the post.
620 // bug #517 - this fixes for conversation owner
621 if ($conv->get_mode() == 'profile' && $conv->get_profile_owner() == local_user()) {
625 // this fixes for visitors
626 return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
628 return $this->writable;
632 * Count the total of our descendants
634 private function count_descendants() {
635 $children = $this->get_children();
636 $total = count($children);
638 foreach ($children as $child) {
639 $total += $child->count_descendants();
646 * Get the template for the comment box
648 private function get_comment_box_template() {
649 return $this->comment_box_template;
653 * Get the comment box
656 * _ The comment box string (empty if no comment box)
659 private function get_comment_box($indent) {
660 $a = $this->get_app();
661 if (!$this->is_toplevel() && !(get_config('system','thread_allow') && $a->theme_thread_allow)) {
666 $conv = $this->get_conversation();
667 $template = get_markup_template($this->get_comment_box_template());
669 if ( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
672 if ($conv->is_writable() && $this->is_writable()) {
673 $qc = $qcomment = null;
676 * Hmmm, code depending on the presence of a particular plugin?
677 * This should be better if done by a hook
679 if (in_array('qcomment',$a->plugins)) {
680 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
681 $qcomment = (($qc) ? explode("\n",$qc) : null);
683 $comment_box = replace_macros($template,array(
684 '$return_path' => $a->query_string,
685 '$threaded' => $this->is_threaded(),
686 // '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
688 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
689 '$id' => $this->get_id(),
690 '$parent' => $this->get_id(),
691 '$qcomment' => $qcomment,
692 '$profile_uid' => $conv->get_profile_owner(),
693 '$mylink' => $a->remove_baseurl($a->contact['url']),
694 '$mytitle' => t('This is you'),
695 '$myphoto' => $a->remove_baseurl($a->contact['thumb']),
696 '$comment' => t('Comment'),
697 '$submit' => t('Submit'),
698 '$edbold' => t('Bold'),
699 '$editalic' => t('Italic'),
700 '$eduline' => t('Underline'),
701 '$edquote' => t('Quote'),
702 '$edcode' => t('Code'),
703 '$edimg' => t('Image'),
704 '$edurl' => t('Link'),
705 '$edvideo' => t('Video'),
706 '$preview' => ((feature_enabled($conv->get_profile_owner(),'preview')) ? t('Preview') : ''),
707 '$indent' => $indent,
708 '$sourceapp' => t($a->sourcename),
709 '$ww' => (($conv->get_mode() === 'network') ? $ww : ''),
710 '$rand_num' => random_digits(12)
717 private function get_redirect_url() {
718 return $this->redirect_url;
722 * Check if we are a wall to wall item and set the relevant properties
724 protected function check_wall_to_wall() {
725 $a = $this->get_app();
726 $conv = $this->get_conversation();
727 $this->wall_to_wall = false;
729 if($this->is_toplevel()) {
730 if($conv->get_mode() !== 'profile') {
731 if($this->get_data_value('wall') && !$this->get_data_value('self')) {
732 // On the network page, I am the owner. On the display page it will be the profile owner.
733 // This will have been stored in $a->page_contact by our calling page.
734 // Put this person as the wall owner of the wall-to-wall notice.
736 $this->owner_url = zrl($a->page_contact['url']);
737 $this->owner_photo = $a->page_contact['thumb'];
738 $this->owner_name = $a->page_contact['name'];
739 $this->wall_to_wall = true;
740 } elseif($this->get_data_value('owner-link')) {
742 $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
743 $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
744 $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
746 if ((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
748 // The author url doesn't match the owner (typically the contact)
749 // and also doesn't match the contact alias.
750 // The name match is a hack to catch several weird cases where URLs are
751 // all over the park. It can be tricked, but this prevents you from
752 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
753 // well that it's the same Bob Smith.
755 // But it could be somebody else with the same name. It just isn't highly likely.
758 $this->owner_photo = $this->get_data_value('owner-avatar');
759 $this->owner_name = $this->get_data_value('owner-name');
760 $this->wall_to_wall = true;
761 // If it is our contact, use a friendly redirect link
762 if ((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url')))
763 && ($this->get_data_value('network') === NETWORK_DFRN)) {
764 $this->owner_url = $this->get_redirect_url();
766 $this->owner_url = zrl($this->get_data_value('owner-link'));
773 if (!$this->wall_to_wall) {
774 $this->set_template('wall');
775 $this->owner_url = '';
776 $this->owner_photo = '';
777 $this->owner_name = '';
781 private function is_wall_to_wall() {
782 return $this->wall_to_wall;
785 private function get_owner_url() {
786 return $this->owner_url;
789 private function get_owner_photo() {
790 return $this->owner_photo;
793 private function get_owner_name() {
794 return $this->owner_name;
797 private function is_visiting() {
798 return $this->visiting;