2 if(class_exists('Item'))
5 require_once('object/BaseObject.php');
6 require_once('include/text.php');
7 require_once('boot.php');
12 class Item extends BaseObject {
13 private $data = array();
14 private $template = null;
15 private $available_templates = array(
16 'wall' => 'wall_thread.tpl',
17 'wall2wall' => 'wallwall_thread.tpl'
19 private $comment_box_template = 'comment_item.tpl';
20 private $toplevel = false;
21 private $writable = false;
22 private $children = array();
23 private $parent = null;
24 private $conversation = null;
25 private $redirect_url = null;
26 private $owner_url = '';
27 private $owner_photo = '';
28 private $owner_name = '';
29 private $wall_to_wall = false;
30 private $threaded = false;
31 private $visiting = false;
33 public function __construct($data) {
34 $a = $this->get_app();
37 $this->set_template('wall');
38 $this->toplevel = ($this->get_id() == $this->get_data_value('parent'));
40 if(is_array($_SESSION['remote'])) {
41 foreach($_SESSION['remote'] as $visitor) {
42 if($visitor['cid'] == $this->get_data_value('contact-id')) {
43 $this->visiting = true;
49 $this->writable = ($this->get_data_value('writable') || $this->get_data_value('self'));
51 $ssl_state = ((local_user()) ? true : false);
52 $this->redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $this->get_data_value('cid') ;
54 if(get_config('system','thread_allow') && $a->theme_thread_allow && !$this->is_toplevel())
55 $this->threaded = true;
57 // Prepare the children
58 if(count($data['children'])) {
59 foreach($data['children'] as $item) {
61 * Only add will be displayed
63 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
66 if(! visible_activity($item)) {
69 $child = new Item($item);
70 $this->add_child($child);
76 * Get data in a form usable by a conversation template
79 * _ The data requested on success
82 public function get_template_data($alike, $dlike, $thread_level=1) {
85 $a = $this->get_app();
87 $item = $this->get_data();
94 $isstarred = "unstarred";
98 $total_children = $this->count_descendants();
100 $conv = $this->get_conversation();
103 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
104 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
105 ? t('Private Message')
107 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
108 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
109 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
112 if(($this->get_data_value('uid') == local_user()) || $this->is_visiting())
116 'dropping' => $dropping,
117 'pagedrop' => ((feature_enabled($conv->get_profile_owner(),'multi_delete')) ? $item['pagedrop'] : ''),
118 'select' => t('Select'),
119 'delete' => t('Delete'),
122 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
124 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
125 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
126 if($item['author-link'] && (! $item['author-name']))
127 $profile_name = $item['author-link'];
130 $profile_link = best_link_url($item,$sp);
131 if($profile_link === 'mailbox')
134 $sparkle = ' sparkle';
136 $profile_link = zrl($profile_link);
138 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
139 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
140 $profile_avatar = $a->contacts[$normalised]['thumb'];
142 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_data_value('thumb')));
144 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
145 call_hooks('render_location',$locate);
146 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
151 foreach(explode(',',$item['tag']) as $tag){
164 $like = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
165 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
168 * We should avoid doing this all the time, but it depends on the conversation mode
169 * And the conv mode may change when we change the conv, or it changes its mode
170 * Maybe we should establish a way to be notified about conversation changes
172 $this->check_wall_to_wall();
174 if($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url()))
175 $osparkle = ' sparkle';
177 if($this->is_toplevel()) {
178 if($conv->get_profile_owner() == local_user()) {
179 $isstarred = (($item['starred']) ? "starred" : "unstarred");
182 'do' => t("add star"),
183 'undo' => t("remove star"),
184 'toggle' => t("toggle star status"),
185 'classdo' => (($item['starred']) ? "hidden" : ""),
186 'classundo' => (($item['starred']) ? "" : "hidden"),
187 'starred' => t('starred'),
190 if(feature_enabled($conv->get_profile_owner(),'commtag')) {
192 'add' => t("add tag"),
201 if($conv->is_writable()) {
203 'like' => array( t("I like this \x28toggle\x29"), t("like")),
204 'dislike' => ((feature_enabled($conv->get_profile_owner(),'dislike')) ? array( t("I don't like this \x28toggle\x29"), t("dislike")) : ''),
206 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
209 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0){
213 localize_item($item);
215 $body = prepare_body($item,true);
217 list($categories, $folders) = get_cats_and_terms($item);
220 'template' => $this->get_template(),
222 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
224 'hashtags' => $hashtags,
225 'mentions' => $mentions,
226 'txt_cats' => t('Categories:'),
227 'txt_folders' => t('Filed under:'),
228 'has_cats' => ((count($categories)) ? 'true' : ''),
229 'has_folders' => ((count($folders)) ? 'true' : ''),
230 'categories' => $categories,
231 'folders' => $folders,
232 'body' => template_escape($body),
233 'text' => strip_tags(template_escape($body)),
234 'id' => $this->get_id(),
235 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
236 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $this->get_owner_name(), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
239 'wall' => t('Wall-to-Wall'),
240 'vwall' => t('via Wall-To-Wall:'),
241 'profile_url' => $profile_link,
242 'item_photo_menu' => item_photo_menu($item),
243 'name' => template_escape($profile_name),
244 'thumb' => $profile_avatar,
245 'osparkle' => $osparkle,
246 'sparkle' => $sparkle,
247 'title' => template_escape($item['title']),
248 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
249 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
251 'location' => template_escape($location),
254 'owner_url' => $this->get_owner_url(),
255 'owner_photo' => $this->get_owner_photo(),
256 'owner_name' => template_escape($this->get_owner_name()),
257 'plink' => get_plink($item),
258 'edpost' => ((feature_enabled($conv->get_profile_owner(),'edit_posts')) ? $edpost : ''),
259 'isstarred' => $isstarred,
260 'star' => ((feature_enabled($conv->get_profile_owner(),'star_posts')) ? $star : ''),
262 'filer' => ((feature_enabled($conv->get_profile_owner(),'filing')) ? $filer : ''),
266 'dislike' => $dislike,
267 'switchcomment' => t('Comment'),
268 'comment' => $this->get_comment_box($indent),
269 'previewing' => ($conv->is_preview() ? ' preview ' : ''),
270 'wait' => t('Please wait'),
271 'thread_level' => $thread_level
274 $arr = array('item' => $item, 'output' => $tmp_item);
275 call_hooks('display_item', $arr);
277 $result = $arr['output'];
279 $result['children'] = array();
280 $children = $this->get_children();
281 $nb_children = count($children);
282 if($nb_children > 0) {
283 foreach($children as $child) {
284 $result['children'][] = $child->get_template_data($alike, $dlike, $thread_level + 1);
287 if(($nb_children > 2) || ($thread_level > 1)) {
288 $result['children'][0]['comment_firstcollapsed'] = true;
289 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
290 $result['children'][0]['hidden_comments_num'] = $total_children;
291 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
292 $result['children'][0]['hide_text'] = t('show more');
293 if($thread_level > 1) {
294 $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
297 $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
302 if ($this->is_toplevel()) {
303 $result['total_comments_num'] = "$total_children";
304 $result['total_comments_text'] = tt('comment', 'comments', $total_children);
307 $result['private'] = $item['private'];
308 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
310 if($this->is_threaded()) {
311 $result['flatten'] = false;
312 $result['threaded'] = true;
315 $result['flatten'] = true;
316 $result['threaded'] = false;
322 public function get_id() {
323 return $this->get_data_value('id');
326 public function is_threaded() {
327 return $this->threaded;
333 public function add_child($item) {
334 $item_id = $item->get_id();
336 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
339 if($this->get_child($item->get_id())) {
340 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
344 * Only add what will be displayed
346 if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
349 if(activity_match($item->get_data_value('verb'),ACTIVITY_LIKE) || activity_match($item->get_data_value('verb'),ACTIVITY_DISLIKE)) {
353 $item->set_parent($this);
354 $this->children[] = $item;
355 return end($this->children);
359 * Get a child by its ID
361 public function get_child($id) {
362 foreach($this->get_children() as $child) {
363 if($child->get_id() == $id)
370 * Get all ou children
372 public function get_children() {
373 return $this->children;
379 protected function set_parent($item) {
380 $parent = $this->get_parent();
382 $parent->remove_child($this);
384 $this->parent = $item;
385 $this->set_conversation($item->get_conversation());
391 protected function remove_parent() {
392 $this->parent = null;
393 $this->conversation = null;
399 public function remove_child($item) {
400 $id = $item->get_id();
401 foreach($this->get_children() as $key => $child) {
402 if($child->get_id() == $id) {
403 $child->remove_parent();
404 unset($this->children[$key]);
405 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
406 $this->children = array_values($this->children);
410 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
417 protected function get_parent() {
418 return $this->parent;
424 public function set_conversation($conv) {
425 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
427 $this->conversation = $conv;
429 // Set it on our children too
430 foreach($this->get_children() as $child)
431 $child->set_conversation($conv);
437 public function get_conversation() {
438 return $this->conversation;
444 * We shouldn't need this
446 public function get_data() {
457 public function get_data_value($name) {
458 if(!isset($this->data[$name])) {
459 logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
463 return $this->data[$name];
469 private function set_template($name) {
470 if(!x($this->available_templates, $name)) {
471 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
474 $this->template = $this->available_templates[$name];
480 private function get_template() {
481 return $this->template;
485 * Check if this is a toplevel post
487 private function is_toplevel() {
488 return $this->toplevel;
492 * Check if this is writable
494 private function is_writable() {
495 $conv = $this->get_conversation();
498 // This will allow us to comment on wall-to-wall items owned by our friends
499 // and community forums even if somebody else wrote the post.
501 // bug #517 - this fixes for conversation owner
502 if($conv->get_mode() == 'profile' && $conv->get_profile_owner() == local_user())
505 // this fixes for visitors
506 return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
508 return $this->writable;
512 * Count the total of our descendants
514 private function count_descendants() {
515 $children = $this->get_children();
516 $total = count($children);
518 foreach($children as $child) {
519 $total += $child->count_descendants();
526 * Get the template for the comment box
528 private function get_comment_box_template() {
529 return $this->comment_box_template;
533 * Get the comment box
536 * _ The comment box string (empty if no comment box)
539 private function get_comment_box($indent) {
540 $a = $this->get_app();
541 if(!$this->is_toplevel() && !(get_config('system','thread_allow') && $a->theme_thread_allow)) {
546 $conv = $this->get_conversation();
547 $template = get_markup_template($this->get_comment_box_template());
549 if( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
552 if($conv->is_writable() && $this->is_writable()) {
553 $qc = $qcomment = null;
556 * Hmmm, code depending on the presence of a particular plugin?
557 * This should be better if done by a hook
559 if(in_array('qcomment',$a->plugins)) {
560 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
561 $qcomment = (($qc) ? explode("\n",$qc) : null);
563 $comment_box = replace_macros($template,array(
564 '$return_path' => '',
565 '$threaded' => $this->is_threaded(),
566 // '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
568 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
569 '$id' => $this->get_id(),
570 '$parent' => $this->get_id(),
571 '$qcomment' => $qcomment,
572 '$profile_uid' => $conv->get_profile_owner(),
573 '$mylink' => $a->contact['url'],
574 '$mytitle' => t('This is you'),
575 '$myphoto' => $a->contact['thumb'],
576 '$comment' => t('Comment'),
577 '$submit' => t('Submit'),
578 '$edbold' => t('Bold'),
579 '$editalic' => t('Italic'),
580 '$eduline' => t('Underline'),
581 '$edquote' => t('Quote'),
582 '$edcode' => t('Code'),
583 '$edimg' => t('Image'),
584 '$edurl' => t('Link'),
585 '$edvideo' => t('Video'),
586 '$preview' => ((feature_enabled($conv->get_profile_owner(),'preview')) ? t('Preview') : ''),
587 '$indent' => $indent,
588 '$sourceapp' => t($a->sourcename),
589 '$ww' => (($conv->get_mode() === 'network') ? $ww : ''),
590 '$rand_num' => random_digits(12)
597 private function get_redirect_url() {
598 return $this->redirect_url;
602 * Check if we are a wall to wall item and set the relevant properties
604 protected function check_wall_to_wall() {
605 $a = $this->get_app();
606 $conv = $this->get_conversation();
607 $this->wall_to_wall = false;
609 if($this->is_toplevel()) {
610 if( (! $this->get_data_value('self')) && ($conv->get_mode() !== 'profile')) {
611 if($this->get_data_value('wall')) {
613 // On the network page, I am the owner. On the display page it will be the profile owner.
614 // This will have been stored in $a->page_contact by our calling page.
615 // Put this person as the wall owner of the wall-to-wall notice.
617 $this->owner_url = zrl($a->page_contact['url']);
618 $this->owner_photo = $a->page_contact['thumb'];
619 $this->owner_name = $a->page_contact['name'];
620 $this->wall_to_wall = true;
622 else if($this->get_data_value('owner-link')) {
624 $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
625 $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
626 $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
627 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
629 // The author url doesn't match the owner (typically the contact)
630 // and also doesn't match the contact alias.
631 // The name match is a hack to catch several weird cases where URLs are
632 // all over the park. It can be tricked, but this prevents you from
633 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
634 // well that it's the same Bob Smith.
636 // But it could be somebody else with the same name. It just isn't highly likely.
639 $this->owner_photo = $this->get_data_value('owner-avatar');
640 $this->owner_name = $this->get_data_value('owner-name');
641 $this->wall_to_wall = true;
642 // If it is our contact, use a friendly redirect link
643 if((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url')))
644 && ($this->get_data_value('network') === NETWORK_DFRN)) {
645 $this->owner_url = $this->get_redirect_url();
648 $this->owner_url = zrl($this->get_data_value('owner-link'));
654 if(!$this->wall_to_wall) {
655 $this->set_template('wall');
656 $this->owner_url = '';
657 $this->owner_photo = '';
658 $this->owner_name = '';
662 private function is_wall_to_wall() {
663 return $this->wall_to_wall;
666 private function get_owner_url() {
667 return $this->owner_url;
670 private function get_owner_photo() {
671 return $this->owner_photo;
674 private function get_owner_name() {
675 return $this->owner_name;
678 private function is_visiting() {
679 return $this->visiting;