2 if(class_exists('Item'))
5 use Friendica\Core\Config;
6 use Friendica\Core\PConfig;
7 use Friendica\Database\DBM;
8 use Friendica\Protocol\Diaspora;
10 require_once('object/BaseObject.php');
11 require_once('include/text.php');
12 require_once('boot.php');
17 class Item extends BaseObject {
18 private $data = array();
19 private $template = null;
20 private $available_templates = array(
21 'wall' => 'wall_thread.tpl',
22 'wall2wall' => 'wallwall_thread.tpl'
24 private $comment_box_template = 'comment_item.tpl';
25 private $toplevel = false;
26 private $writable = false;
27 private $children = array();
28 private $parent = null;
29 private $conversation = null;
30 private $redirect_url = null;
31 private $owner_url = '';
32 private $owner_photo = '';
33 private $owner_name = '';
34 private $wall_to_wall = false;
35 private $threaded = false;
36 private $visiting = false;
38 public function __construct($data) {
39 $a = $this->get_app();
42 $this->set_template('wall');
43 $this->toplevel = ($this->get_id() == $this->get_data_value('parent'));
45 if (is_array($_SESSION['remote'])) {
46 foreach ($_SESSION['remote'] as $visitor) {
47 if ($visitor['cid'] == $this->get_data_value('contact-id')) {
48 $this->visiting = true;
54 $this->writable = ($this->get_data_value('writable') || $this->get_data_value('self'));
56 $ssl_state = ((local_user()) ? true : false);
57 $this->redirect_url = 'redir/' . $this->get_data_value('cid') ;
59 if (Config::get('system','thread_allow') && $a->theme_thread_allow && !$this->is_toplevel()) {
60 $this->threaded = true;
63 // Prepare the children
64 if (count($data['children'])) {
65 foreach ($data['children'] as $item) {
67 * Only add will be displayed
69 if ($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
71 } elseif (! visible_activity($item)) {
75 // You can always comment on Diaspora items
76 if (($item['network'] == NETWORK_DIASPORA) && (local_user() == $item['uid'])) {
77 $item['writable'] = true;
80 $item['pagedrop'] = $data['pagedrop'];
81 $child = new Item($item);
82 $this->add_child($child);
88 * Get data in a form usable by a conversation template
91 * _ The data requested on success
94 public function get_template_data($conv_responses, $thread_level=1) {
95 require_once("mod/proxy.php");
99 $a = $this->get_app();
101 $item = $this->get_data();
103 // If the time between "created" and "edited" differs we add
104 // a notice that the post was edited.
105 // Note: In some networks reshared items seem to have (sometimes) a difference
106 // between creation time and edit time of a second. Thats why we add the notice
107 // only if the difference is more than 1 second.
108 if (strtotime($item['edited']) - strtotime($item['created']) > 1) {
110 'label' => t('This entry was edited'),
111 'date' => datetime_convert('UTC', date_default_timezone_get(), $item['edited'], 'r'),
112 'relative' => relative_date($item['edited'])
121 $isstarred = "unstarred";
125 $total_children = $this->count_descendants();
127 $conv = $this->get_conversation();
129 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
130 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
131 ? t('Private Message')
133 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
134 if (local_user() && link_compare($a->contact['url'],$item['author-link'])) {
135 if ($item["event-id"] != 0) {
136 $edpost = array("events/event/".$item['event-id'], t("Edit"));
138 $edpost = array("editpost/".$item['id'], t("Edit"));
144 if (($this->get_data_value('uid') == local_user()) || $this->is_visiting()) {
149 'dropping' => $dropping,
150 'pagedrop' => ((feature_enabled($conv->get_profile_owner(),'multi_delete')) ? $item['pagedrop'] : ''),
151 'select' => t('Select'),
152 'delete' => t('Delete'),
155 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
157 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
158 $profile_name = htmlentities(((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
159 if ($item['author-link'] && (! $item['author-name'])) {
160 $profile_name = $item['author-link'];
164 $profile_link = best_link_url($item,$sp);
165 if ($profile_link === 'mailbox') {
170 $sparkle = ' sparkle';
172 $profile_link = zrl($profile_link);
175 if (!isset($item['author-thumb']) || ($item['author-thumb'] == "")) {
176 $author_contact = get_contact_details_by_url($item['author-link'], $conv->get_profile_owner());
177 if ($author_contact["thumb"]) {
178 $item['author-thumb'] = $author_contact["thumb"];
180 $item['author-thumb'] = $item['author-avatar'];
184 if (!isset($item['owner-thumb']) || ($item['owner-thumb'] == "")) {
185 $owner_contact = get_contact_details_by_url($item['owner-link'], $conv->get_profile_owner());
186 if ($owner_contact["thumb"]) {
187 $item['owner-thumb'] = $owner_contact["thumb"];
189 $item['owner-thumb'] = $item['owner-avatar'];
193 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
194 call_hooks('render_location',$locate);
195 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
202 /*foreach(explode(',',$item['tag']) as $tag){
214 // process action responses - e.g. like/dislike/attend/agree/whatever
215 $response_verbs = array('like');
216 if (feature_enabled($conv->get_profile_owner(),'dislike')) {
217 $response_verbs[] = 'dislike';
219 if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
220 $response_verbs[] = 'attendyes';
221 $response_verbs[] = 'attendno';
222 $response_verbs[] = 'attendmaybe';
223 if ($conv->is_writable()) {
225 $attend = array( t('I will attend'), t('I will not attend'), t('I might attend'));
229 $responses = get_responses($conv_responses,$response_verbs,$this,$item);
231 foreach ($response_verbs as $value=>$verbs) {
232 $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']) : '');
236 * We should avoid doing this all the time, but it depends on the conversation mode
237 * And the conv mode may change when we change the conv, or it changes its mode
238 * Maybe we should establish a way to be notified about conversation changes
240 $this->check_wall_to_wall();
242 if ($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url())) {
243 $osparkle = ' sparkle';
246 if ($this->is_toplevel()) {
247 if ($conv->get_profile_owner() == local_user()) {
248 $isstarred = (($item['starred']) ? "starred" : "unstarred");
251 'do' => t("add star"),
252 'undo' => t("remove star"),
253 'toggle' => t("toggle star status"),
254 'classdo' => (($item['starred']) ? "hidden" : ""),
255 'classundo' => (($item['starred']) ? "" : "hidden"),
256 'starred' => t('starred'),
258 $r = dba::select('thread', array('ignored'), array('uid' => $item['uid'], 'iid' => $item['id']), array('limit' => 1));
259 if (DBM::is_result($r)) {
261 'do' => t("ignore thread"),
262 'undo' => t("unignore thread"),
263 'toggle' => t("toggle ignore status"),
264 'classdo' => (($r['ignored']) ? "hidden" : ""),
265 'classundo' => (($r['ignored']) ? "" : "hidden"),
266 'ignored' => t('ignored'),
271 if(feature_enabled($conv->get_profile_owner(),'commtag')) {
273 'add' => t("add tag"),
282 if ($conv->is_writable()) {
284 'like' => array( t("I like this \x28toggle\x29"), t("like")),
285 'dislike' => ((feature_enabled($conv->get_profile_owner(),'dislike')) ? array( t("I don't like this \x28toggle\x29"), t("dislike")) : ''),
288 $buttons['share'] = array( t('Share this'), t('share'));
292 $comment = $this->get_comment_box($indent);
294 if (strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0){
298 localize_item($item);
300 $body = prepare_body($item,true);
302 list($categories, $folders) = get_cats_and_terms($item);
304 if ($a->theme['template_engine'] === 'internal') {
305 $body_e = template_escape($body);
306 $text_e = strip_tags(template_escape($body));
307 $name_e = template_escape($profile_name);
308 $title_e = template_escape($item['title']);
309 $location_e = template_escape($location);
310 $owner_name_e = template_escape($this->get_owner_name());
313 $text_e = strip_tags($body);
314 $name_e = $profile_name;
315 $title_e = $item['title'];
316 $location_e = $location;
317 $owner_name_e = $this->get_owner_name();
320 // Disable features that aren't available in several networks
322 /// @todo Add NETWORK_DIASPORA when it will pass this information
323 if (!in_array($item["item_network"], array(NETWORK_DFRN)) && isset($buttons["dislike"])) {
324 unset($buttons["dislike"],$isevent);
328 if (($item["item_network"] == NETWORK_FEED) && isset($buttons["like"])) {
329 unset($buttons["like"]);
332 if (($item["item_network"] == NETWORK_MAIL) && isset($buttons["like"])) {
333 unset($buttons["like"]);
337 'template' => $this->get_template(),
338 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
339 'tags' => $item['tags'],
340 'hashtags' => $item['hashtags'],
341 'mentions' => $item['mentions'],
342 'txt_cats' => t('Categories:'),
343 'txt_folders' => t('Filed under:'),
344 'has_cats' => ((count($categories)) ? 'true' : ''),
345 'has_folders' => ((count($folders)) ? 'true' : ''),
346 'categories' => $categories,
347 'folders' => $folders,
350 'id' => $this->get_id(),
351 'guid' => urlencode($item['guid']),
352 'isevent' => $isevent,
354 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
355 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), htmlentities($this->get_owner_name()), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
358 'wall' => t('Wall-to-Wall'),
359 'vwall' => t('via Wall-To-Wall:'),
360 'profile_url' => $profile_link,
361 'item_photo_menu' => item_photo_menu($item),
363 'thumb' => $a->remove_baseurl(proxy_url($item['author-thumb'], false, PROXY_SIZE_THUMB)),
364 'osparkle' => $osparkle,
365 'sparkle' => $sparkle,
367 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
368 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
369 'app' => $item['app'],
370 'created' => relative_date($item['created']),
372 'location' => $location_e,
375 'owner_url' => $this->get_owner_url(),
376 'owner_photo' => $a->remove_baseurl(proxy_url($item['owner-thumb'], false, PROXY_SIZE_THUMB)),
377 'owner_name' => htmlentities($owner_name_e),
378 'plink' => get_plink($item),
379 'edpost' => ((feature_enabled($conv->get_profile_owner(),'edit_posts')) ? $edpost : ''),
380 'isstarred' => $isstarred,
381 'star' => ((feature_enabled($conv->get_profile_owner(),'star_posts')) ? $star : ''),
382 'ignore' => ((feature_enabled($conv->get_profile_owner(),'ignore_posts')) ? $ignore : ''),
384 'filer' => ((feature_enabled($conv->get_profile_owner(),'filing')) ? $filer : ''),
387 'like' => $responses['like']['output'],
388 'dislike' => $responses['dislike']['output'],
389 'responses' => $responses,
390 'switchcomment' => t('Comment'),
391 'comment' => $comment,
392 'previewing' => ($conv->is_preview() ? ' preview ' : ''),
393 'wait' => t('Please wait'),
394 'thread_level' => $thread_level,
396 'network' => $item["item_network"],
397 'network_name' => network_to_name($item['item_network'], $profile_link),
398 'received' => $item['received'],
399 'commented' => $item['commented'],
400 'created_date' => $item['created'],
403 $arr = array('item' => $item, 'output' => $tmp_item);
404 call_hooks('display_item', $arr);
406 $result = $arr['output'];
408 $result['children'] = array();
409 $children = $this->get_children();
410 $nb_children = count($children);
411 if ($nb_children > 0) {
412 foreach ($children as $child) {
413 $result['children'][] = $child->get_template_data($conv_responses, $thread_level + 1);
416 if (($nb_children > 2) || ($thread_level > 1)) {
417 $result['children'][0]['comment_firstcollapsed'] = true;
418 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
419 $result['children'][0]['hidden_comments_num'] = $total_children;
420 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
421 $result['children'][0]['hide_text'] = t('show more');
422 if ($thread_level > 1) {
423 $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
425 $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
430 if ($this->is_toplevel()) {
431 $result['total_comments_num'] = "$total_children";
432 $result['total_comments_text'] = tt('comment', 'comments', $total_children);
435 $result['private'] = $item['private'];
436 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
438 if ($this->is_threaded()) {
439 $result['flatten'] = false;
440 $result['threaded'] = true;
442 $result['flatten'] = true;
443 $result['threaded'] = false;
449 public function get_id() {
450 return $this->get_data_value('id');
453 public function is_threaded() {
454 return $this->threaded;
460 public function add_child(Item $item) {
461 $item_id = $item->get_id();
463 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
465 } elseif ($this->get_child($item->get_id())) {
466 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
470 * Only add what will be displayed
472 if ($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
474 } elseif (activity_match($item->get_data_value('verb'),ACTIVITY_LIKE) || activity_match($item->get_data_value('verb'),ACTIVITY_DISLIKE)) {
478 $item->set_parent($this);
479 $this->children[] = $item;
480 return end($this->children);
484 * Get a child by its ID
486 public function get_child($id) {
487 foreach ($this->get_children() as $child) {
488 if ($child->get_id() == $id) {
496 * Get all ou children
498 public function get_children() {
499 return $this->children;
505 protected function set_parent($item) {
506 $parent = $this->get_parent();
508 $parent->remove_child($this);
510 $this->parent = $item;
511 $this->set_conversation($item->get_conversation());
517 protected function remove_parent() {
518 $this->parent = null;
519 $this->conversation = null;
525 public function remove_child($item) {
526 $id = $item->get_id();
527 foreach ($this->get_children() as $key => $child) {
528 if ($child->get_id() == $id) {
529 $child->remove_parent();
530 unset($this->children[$key]);
531 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
532 $this->children = array_values($this->children);
536 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
543 protected function get_parent() {
544 return $this->parent;
550 public function set_conversation($conv) {
551 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
553 $this->conversation = $conv;
555 // Set it on our children too
556 foreach ($this->get_children() as $child) {
557 $child->set_conversation($conv);
564 public function get_conversation() {
565 return $this->conversation;
571 * We shouldn't need this
573 public function get_data() {
584 public function get_data_value($name) {
585 if (!isset($this->data[$name])) {
586 // logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
590 return $this->data[$name];
596 private function set_template($name) {
597 if (!x($this->available_templates, $name)) {
598 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
602 $this->template = $this->available_templates[$name];
608 private function get_template() {
609 return $this->template;
613 * Check if this is a toplevel post
615 private function is_toplevel() {
616 return $this->toplevel;
620 * Check if this is writable
622 private function is_writable() {
623 $conv = $this->get_conversation();
626 // This will allow us to comment on wall-to-wall items owned by our friends
627 // and community forums even if somebody else wrote the post.
629 // bug #517 - this fixes for conversation owner
630 if ($conv->get_mode() == 'profile' && $conv->get_profile_owner() == local_user()) {
634 // this fixes for visitors
635 return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
637 return $this->writable;
641 * Count the total of our descendants
643 private function count_descendants() {
644 $children = $this->get_children();
645 $total = count($children);
647 foreach ($children as $child) {
648 $total += $child->count_descendants();
655 * Get the template for the comment box
657 private function get_comment_box_template() {
658 return $this->comment_box_template;
662 * Get the comment box
665 * _ The comment box string (empty if no comment box)
668 private function get_comment_box($indent) {
669 $a = $this->get_app();
670 if (!$this->is_toplevel() && !(Config::get('system','thread_allow') && $a->theme_thread_allow)) {
675 $conv = $this->get_conversation();
676 $template = get_markup_template($this->get_comment_box_template());
678 if ( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
681 if ($conv->is_writable() && $this->is_writable()) {
682 $qc = $qcomment = null;
685 * Hmmm, code depending on the presence of a particular plugin?
686 * This should be better if done by a hook
688 if (in_array('qcomment',$a->plugins)) {
689 $qc = ((local_user()) ? PConfig::get(local_user(),'qcomment','words') : null);
690 $qcomment = (($qc) ? explode("\n",$qc) : null);
692 $comment_box = replace_macros($template,array(
693 '$return_path' => $a->query_string,
694 '$threaded' => $this->is_threaded(),
695 // '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
697 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
698 '$id' => $this->get_id(),
699 '$parent' => $this->get_id(),
700 '$qcomment' => $qcomment,
701 '$profile_uid' => $conv->get_profile_owner(),
702 '$mylink' => $a->remove_baseurl($a->contact['url']),
703 '$mytitle' => t('This is you'),
704 '$myphoto' => $a->remove_baseurl($a->contact['thumb']),
705 '$comment' => t('Comment'),
706 '$submit' => t('Submit'),
707 '$edbold' => t('Bold'),
708 '$editalic' => t('Italic'),
709 '$eduline' => t('Underline'),
710 '$edquote' => t('Quote'),
711 '$edcode' => t('Code'),
712 '$edimg' => t('Image'),
713 '$edurl' => t('Link'),
714 '$edvideo' => t('Video'),
715 '$preview' => ((feature_enabled($conv->get_profile_owner(),'preview')) ? t('Preview') : ''),
716 '$indent' => $indent,
717 '$sourceapp' => t($a->sourcename),
718 '$ww' => (($conv->get_mode() === 'network') ? $ww : ''),
719 '$rand_num' => random_digits(12)
726 private function get_redirect_url() {
727 return $this->redirect_url;
731 * Check if we are a wall to wall item and set the relevant properties
733 protected function check_wall_to_wall() {
734 $a = $this->get_app();
735 $conv = $this->get_conversation();
736 $this->wall_to_wall = false;
738 if($this->is_toplevel()) {
739 if($conv->get_mode() !== 'profile') {
740 if($this->get_data_value('wall') && !$this->get_data_value('self')) {
741 // On the network page, I am the owner. On the display page it will be the profile owner.
742 // This will have been stored in $a->page_contact by our calling page.
743 // Put this person as the wall owner of the wall-to-wall notice.
745 $this->owner_url = zrl($a->page_contact['url']);
746 $this->owner_photo = $a->page_contact['thumb'];
747 $this->owner_name = $a->page_contact['name'];
748 $this->wall_to_wall = true;
749 } elseif($this->get_data_value('owner-link')) {
751 $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
752 $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
753 $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
755 if ((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
757 // The author url doesn't match the owner (typically the contact)
758 // and also doesn't match the contact alias.
759 // The name match is a hack to catch several weird cases where URLs are
760 // all over the park. It can be tricked, but this prevents you from
761 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
762 // well that it's the same Bob Smith.
764 // But it could be somebody else with the same name. It just isn't highly likely.
767 $this->owner_photo = $this->get_data_value('owner-avatar');
768 $this->owner_name = $this->get_data_value('owner-name');
769 $this->wall_to_wall = true;
770 // If it is our contact, use a friendly redirect link
771 if ((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url')))
772 && ($this->get_data_value('network') === NETWORK_DFRN)) {
773 $this->owner_url = $this->get_redirect_url();
775 $this->owner_url = zrl($this->get_data_value('owner-link'));
782 if (!$this->wall_to_wall) {
783 $this->set_template('wall');
784 $this->owner_url = '';
785 $this->owner_photo = '';
786 $this->owner_name = '';
790 private function is_wall_to_wall() {
791 return $this->wall_to_wall;
794 private function get_owner_url() {
795 return $this->owner_url;
798 private function get_owner_photo() {
799 return $this->owner_photo;
802 private function get_owner_name() {
803 return $this->owner_name;
806 private function is_visiting() {
807 return $this->visiting;