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;
31 public function __construct($data) {
32 $a = $this->get_app();
35 $this->set_template('wall');
36 $this->toplevel = ($this->get_id() == $this->get_data_value('parent'));
37 $this->writable = ($this->get_data_value('writable') || $this->get_data_value('self'));
39 $ssl_state = ((local_user()) ? true : false);
40 $this->redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $this->get_data_value('cid') ;
42 // Prepare the children
43 if(count($data['children'])) {
44 foreach($data['children'] as $item) {
46 * Only add will be displayed
48 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
51 if($item['verb'] === ACTIVITY_LIKE || $item['verb'] === ACTIVITY_DISLIKE) {
54 $child = new Item($item);
55 $this->add_child($child);
61 * Get data in a form usable by a conversation template
64 * _ The data requested on success
67 public function get_template_data($alike, $dlike, $thread_level=1) {
70 $a = $this->get_app();
72 $item = $this->get_data();
79 $isstarred = "unstarred";
82 $total_children = $this->count_descendants();
84 $conv = $this->get_conversation();
86 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
87 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
88 ? t('Private Message')
90 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
91 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
92 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
95 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
99 'dropping' => $dropping,
100 'select' => t('Select'),
101 'delete' => t('Delete'),
104 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
106 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
107 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
108 if($item['author-link'] && (! $item['author-name']))
109 $profile_name = $item['author-link'];
112 $profile_link = best_link_url($item,$sp);
113 if($profile_link === 'mailbox')
116 $sparkle = ' sparkle';
118 $profile_link = zrl($profile_link);
120 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
121 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
122 $profile_avatar = $a->contacts[$normalised]['thumb'];
124 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_data_value('thumb')));
126 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
127 call_hooks('render_location',$locate);
128 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
131 foreach(explode(',',$item['tag']) as $tag){
133 if ($tag!="") $tags[] = bbcode($tag);
136 $like = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
137 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
140 * We should avoid doing this all the time, but it depends on the conversation mode
141 * And the conv mode may change when we change the conv, or it changes its mode
142 * Maybe we should establish a way to be notified about conversation changes
144 $this->check_wall_to_wall();
146 if($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url()))
147 $osparkle = ' sparkle';
149 if($this->is_toplevel()) {
150 if($conv->get_profile_owner() == local_user()) {
151 $isstarred = (($item['starred']) ? "starred" : "unstarred");
154 'do' => t("add star"),
155 'undo' => t("remove star"),
156 'toggle' => t("toggle star status"),
157 'classdo' => (($item['starred']) ? "hidden" : ""),
158 'classundo' => (($item['starred']) ? "" : "hidden"),
159 'starred' => t('starred'),
160 'tagger' => t("add tag"),
168 if($conv->is_writable()) {
170 'like' => array( t("I like this \x28toggle\x29"), t("like")),
171 'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
173 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
176 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
179 localize_item($item);
181 $body = prepare_body($item,true);
184 'template' => $this->get_template(),
186 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
188 'body' => template_escape($body),
189 'text' => strip_tags(template_escape($body)),
190 'id' => $this->get_id(),
191 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
192 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $this->get_owner_name(), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
194 'wall' => t('Wall-to-Wall'),
195 'vwall' => t('via Wall-To-Wall:'),
196 'profile_url' => $profile_link,
197 'item_photo_menu' => item_photo_menu($item),
198 'name' => template_escape($profile_name),
199 'thumb' => $profile_avatar,
200 'osparkle' => $osparkle,
201 'sparkle' => $sparkle,
202 'title' => template_escape($item['title']),
203 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
205 'location' => template_escape($location),
207 'owner_url' => $this->get_owner_url(),
208 'owner_photo' => $this->get_owner_photo(),
209 'owner_name' => template_escape($this->get_owner_name()),
210 'plink' => get_plink($item),
212 'isstarred' => $isstarred,
218 'dislike' => $dislike,
219 'comment' => $this->get_comment_box($indent),
220 'previewing' => $previewing,
221 'wait' => t('Please wait'),
222 'thread_level' => $thread_level
225 $arr = array('item' => $item, 'output' => $tmp_item);
226 call_hooks('display_item', $arr);
228 $result = $arr['output'];
230 $result['children'] = array();
231 $children = $this->get_children();
232 $nb_children = count($children);
233 if($nb_children > 0) {
234 foreach($children as $child) {
235 $result['children'][] = $child->get_template_data($alike, $dlike, $thread_level + 1);
238 if(($nb_children > 2) || ($thread_level > 1)) {
239 $result['children'][0]['comment_firstcollapsed'] = true;
240 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
241 $result['children'][0]['hide_text'] = t('show more');
242 if($thread_level > 1) {
243 $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
246 $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
251 $result['private'] = $item['private'];
252 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
254 if(get_config('system','thread_allow') && $a->theme_thread_allow) {
255 $result['flatten'] = false;
256 $result['threaded'] = true;
259 $result['flatten'] = true;
260 $result['threaded'] = false;
266 public function get_id() {
267 return $this->get_data_value('id');
273 public function add_child($item) {
274 $item_id = $item->get_id();
276 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
279 if($this->get_child($item->get_id())) {
280 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
284 * Only add what will be displayed
286 if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
287 logger('[WARN] Item::add_child : Item is a mail ('. $item->get_id() .').', LOGGER_DEBUG);
290 if($item->get_data_value('verb') === ACTIVITY_LIKE || $item->get_data_value('verb') === ACTIVITY_DISLIKE) {
291 logger('[WARN] Item::add_child : Item is a (dis)like ('. $item->get_id() .').', LOGGER_DEBUG);
295 $item->set_parent($this);
296 $this->children[] = $item;
297 return end($this->children);
301 * Get a child by its ID
303 public function get_child($id) {
304 foreach($this->get_children() as $child) {
305 if($child->get_id() == $id)
312 * Get all ou children
314 public function get_children() {
315 return $this->children;
321 protected function set_parent($item) {
322 $parent = $this->get_parent();
324 $parent->remove_child($this);
326 $this->parent = $item;
327 $this->set_conversation($item->get_conversation());
333 protected function remove_parent() {
334 $this->parent = null;
335 $this->conversation = null;
341 public function remove_child($item) {
342 $id = $item->get_id();
343 foreach($this->get_children() as $key => $child) {
344 if($child->get_id() == $id) {
345 $child->remove_parent();
346 unset($this->children[$key]);
347 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
348 $this->children = array_values($this->children);
352 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
359 protected function get_parent() {
360 return $this->parent;
366 public function set_conversation($conv) {
367 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
369 $this->conversation = $conv;
371 // Set it on our children too
372 foreach($this->get_children() as $child)
373 $child->set_conversation($conv);
379 public function get_conversation() {
380 return $this->conversation;
386 * We shouldn't need this
388 public function get_data() {
399 public function get_data_value($name) {
400 if(!isset($this->data[$name])) {
401 logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
405 return $this->data[$name];
411 private function set_template($name) {
412 if(!x($this->available_templates, $name)) {
413 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
416 $this->template = $this->available_templates[$name];
422 private function get_template() {
423 return $this->template;
427 * Check if this is a toplevel post
429 private function is_toplevel() {
430 return $this->toplevel;
434 * Check if this is writable
436 private function is_writable() {
437 return $this->writable;
441 * Count the total of our descendants
443 private function count_descendants() {
444 $children = $this->get_children();
445 $total = count($children);
447 foreach($children as $child) {
448 $total += $child->count_descendants();
455 * Get the template for the comment box
457 private function get_comment_box_template() {
458 return $this->comment_box_template;
462 * Get the comment box
465 * _ The comment box string (empty if no comment box)
468 private function get_comment_box($indent) {
469 if(!$this->is_toplevel() && !get_config('system','thread_allow')) {
474 $conv = $this->get_conversation();
475 $template = get_markup_template($this->get_comment_box_template());
477 if( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
480 if($conv->is_writable() && $this->is_writable()) {
481 $a = $this->get_app();
482 $qc = $qcomment = null;
485 * Hmmm, code depending on the presence of a particular plugin?
486 * This should be better if done by a hook
488 if(in_array('qcomment',$a->plugins)) {
489 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
490 $qcomment = (($qc) ? explode("\n",$qc) : null);
492 $comment_box = replace_macros($template,array(
493 '$return_path' => '',
494 '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
495 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
496 '$id' => $this->get_id(),
497 '$parent' => $this->get_id(),
498 '$qcomment' => $qcomment,
499 '$profile_uid' => $conv->get_profile_owner(),
500 '$mylink' => $a->contact['url'],
501 '$mytitle' => t('This is you'),
502 '$myphoto' => $a->contact['thumb'],
503 '$comment' => t('Comment'),
504 '$submit' => t('Submit'),
505 '$edbold' => t('Bold'),
506 '$editalic' => t('Italic'),
507 '$eduline' => t('Underline'),
508 '$edquote' => t('Quote'),
509 '$edcode' => t('Code'),
510 '$edimg' => t('Image'),
511 '$edurl' => t('Link'),
512 '$edvideo' => t('Video'),
513 '$preview' => t('Preview'),
514 '$indent' => $indent,
515 '$sourceapp' => t($a->sourcename),
516 '$ww' => (($conv->get_mode() === 'network') ? $ww : '')
523 private function get_redirect_url() {
524 return $this->redirect_url;
528 * Check if we are a wall to wall item and set the relevant properties
530 protected function check_wall_to_wall() {
531 $a = $this->get_app();
532 $conv = $this->get_conversation();
533 $this->wall_to_wall = false;
535 if($this->is_toplevel()) {
536 if( (! $this->get_data_value('self')) && ($conv->get_mode() !== 'profile')) {
537 if($this->get_data_value('wall')) {
539 // On the network page, I am the owner. On the display page it will be the profile owner.
540 // This will have been stored in $a->page_contact by our calling page.
541 // Put this person as the wall owner of the wall-to-wall notice.
543 $this->owner_url = zrl($a->page_contact['url']);
544 $this->owner_photo = $a->page_contact['thumb'];
545 $this->owner_name = $a->page_contact['name'];
546 $this->set_template('wall2wall');
547 $this->wall_to_wall = true;
549 else if($this->get_data_value('owner-link')) {
551 $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
552 $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
553 $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
554 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
556 // The author url doesn't match the owner (typically the contact)
557 // and also doesn't match the contact alias.
558 // The name match is a hack to catch several weird cases where URLs are
559 // all over the park. It can be tricked, but this prevents you from
560 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
561 // well that it's the same Bob Smith.
563 // But it could be somebody else with the same name. It just isn't highly likely.
566 $this->owner_photo = $this->get_data_value('owner-avatar');
567 $this->owner_name = $this->get_data_value('owner-name');
568 $this->set_template('wall2wall');
569 $this->wall_to_wall = true;
570 // If it is our contact, use a friendly redirect link
571 if((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url')))
572 && ($this->get_data_value('network') === NETWORK_DFRN)) {
573 $this->owner_url = $this->get_redirect_url();
576 $this->owner_url = zrl($this->get_data_value('owner-link'));
582 if(!$this->wall_to_wall) {
583 $this->set_template('wall');
584 $this->owner_url = '';
585 $this->owner_photo = '';
586 $this->owner_name = '';
590 private function is_wall_to_wall() {
591 return $this->wall_to_wall;
594 private function get_owner_url() {
595 return $this->owner_url;
598 private function get_owner_photo() {
599 return $this->owner_photo;
602 private function get_owner_name() {
603 return $this->owner_name;