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 $writeable = false;
22 private $children = array();
23 private $parent = null;
24 private $conversation = null;
25 private $redirect_url = null;
27 public function __construct($data) {
28 $a = $this->get_app();
31 $this->set_template('wall');
32 $this->toplevel = ($this->get_id() == $this->get_data_value('parent'));
33 $this->writeable = ($this->get_data_value('writeable') || $this->get_data_value('self'));
35 $ssl_state = ((local_user()) ? true : false);
36 $this->redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $this->get_data_value('cid') ;
38 // Prepare the children
39 if(count($data['children'])) {
40 foreach($data['children'] as $item) {
41 $child = new Item($item);
42 $this->add_child($child);
48 * Get data in a form usable by a conversation template
51 * _ The data requested on success
54 public function get_template_data($alike, $dlike, $thread_level=1) {
57 $a = $this->get_app();
59 $item = $this->get_data();
63 $owner_url = $owner_photo = $owner_name = '';
67 $isstarred = "unstarred";
70 $total_children = $this->count_descendants();
72 $conv = $this->get_conversation();
74 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
75 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
76 ? t('Private Message')
78 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
79 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
80 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
83 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
87 'dropping' => $dropping,
88 'select' => t('Select'),
89 'delete' => t('Delete'),
92 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
94 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
95 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
96 if($item['author-link'] && (! $item['author-name']))
97 $profile_name = $item['author-link'];
100 $profile_link = best_link_url($item,$sp);
101 if($profile_link === 'mailbox')
104 $sparkle = ' sparkle';
106 $profile_link = zrl($profile_link);
108 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
109 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
110 $profile_avatar = $a->contacts[$normalised]['thumb'];
112 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_data_value('thumb')));
114 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
115 call_hooks('render_location',$locate);
116 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
119 foreach(explode(',',$item['tag']) as $tag){
121 if ($tag!="") $tags[] = bbcode($tag);
124 $like = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
125 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
127 if($this->is_toplevel()) {
128 if((! $item['self']) && ($conv->get_mode() !== 'profile')) {
131 // On the network page, I am the owner. On the display page it will be the profile owner.
132 // This will have been stored in $a->page_contact by our calling page.
133 // Put this person as the wall owner of the wall-to-wall notice.
135 $owner_url = zrl($a->page_contact['url']);
136 $owner_photo = $a->page_contact['thumb'];
137 $owner_name = $a->page_contact['name'];
138 $this->set_template('wall2wall');
142 else if($item['owner-link']) {
144 $owner_linkmatch = (($item['owner-link']) && link_compare($item['owner-link'],$item['author-link']));
145 $alias_linkmatch = (($item['alias']) && link_compare($item['alias'],$item['author-link']));
146 $owner_namematch = (($item['owner-name']) && $item['owner-name'] == $item['author-name']);
147 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
149 // The author url doesn't match the owner (typically the contact)
150 // and also doesn't match the contact alias.
151 // The name match is a hack to catch several weird cases where URLs are
152 // all over the park. It can be tricked, but this prevents you from
153 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
154 // well that it's the same Bob Smith.
156 // But it could be somebody else with the same name. It just isn't highly likely.
159 $owner_url = $item['owner-link'];
160 $owner_photo = $item['owner-avatar'];
161 $owner_name = $item['owner-name'];
162 $this->set_template('wall2wall');
164 // If it is our contact, use a friendly redirect link
165 if((link_compare($item['owner-link'],$item['url']))
166 && ($item['network'] === NETWORK_DFRN)) {
167 $owner_url = $this->get_redirect_url();
168 $osparkle = ' sparkle';
171 $owner_url = zrl($owner_url);
174 if($conv->get_profile_owner() == local_user()) {
175 $isstarred = (($item['starred']) ? "starred" : "unstarred");
178 'do' => t("add star"),
179 'undo' => t("remove star"),
180 'toggle' => t("toggle star status"),
181 'classdo' => (($item['starred']) ? "hidden" : ""),
182 'classundo' => (($item['starred']) ? "" : "hidden"),
183 'starred' => t('starred'),
184 'tagger' => t("add tag"),
192 if($conv->is_writeable()) {
194 'like' => array( t("I like this \x28toggle\x29"), t("like")),
195 'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
197 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
200 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
203 localize_item($item);
205 $body = prepare_body($item,true);
208 // template to use to render item (wall, walltowall, search)
209 'template' => $this->get_template(),
211 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
213 'body' => template_escape($body),
214 'text' => strip_tags(template_escape($body)),
215 'id' => $this->get_id(),
216 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
217 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $owner_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
219 'wall' => t('Wall-to-Wall'),
220 'vwall' => t('via Wall-To-Wall:'),
221 'profile_url' => $profile_link,
222 'item_photo_menu' => item_photo_menu($item),
223 'name' => template_escape($profile_name),
224 'thumb' => $profile_avatar,
225 'osparkle' => $osparkle,
226 'sparkle' => $sparkle,
227 'title' => template_escape($item['title']),
228 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
230 'location' => template_escape($location),
232 'owner_url' => $owner_url,
233 'owner_photo' => $owner_photo,
234 'owner_name' => template_escape($owner_name),
235 'plink' => get_plink($item),
237 'isstarred' => $isstarred,
243 'dislike' => $dislike,
244 'comment' => $this->get_comment_box($commentww),
245 'previewing' => $previewing,
246 'wait' => t('Please wait'),
249 $arr = array('item' => $item, 'output' => $tmp_item);
250 call_hooks('display_item', $arr);
252 $item_result = $arr['output'];
254 $item_result['children'] = array();
255 $children = $this->get_children();
256 $nb_children = count($children);
257 if($nb_children > 0) {
258 foreach($this->get_children() as $child) {
259 $item_result['children'][] = $child->get_template_data($alike, $dlike, $thread_level + 1);
262 if(($nb_children > 2) || ($thread_level > 1)) {
263 $item_result['children'][0]['comment_firstcollapsed'] = true;
264 $item_result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
265 $item_result['children'][0]['hide_text'] = t('show more');
266 if($thread_level > 1) {
267 $item_result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
270 $item_result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
275 $item_result['private'] = $item['private'];
276 $item_result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
278 if(get_config('system','thread_allow')) {
279 $item_result['flatten'] = false;
280 $item_result['threaded'] = true;
283 $item_result['flatten'] = true;
284 $item_result['threaded'] = false;
287 $result = $item_result;
292 public function get_id() {
293 return $this->get_data_value('id');
299 public function add_child($item) {
300 $item_id = $item->get_id();
302 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
305 if($this->get_child($item->get_id())) {
306 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
309 $item->set_parent($this);
310 $this->children[] = $item;
311 return end($this->children);
315 * Get a child by its ID
317 public function get_child($id) {
318 foreach($this->get_children() as $child) {
319 if($child->get_id() == $id)
326 * Get all ou children
328 public function get_children() {
329 return $this->children;
335 protected function set_parent($item) {
336 $parent = $this->get_parent();
338 $parent->remove_child($this);
340 $this->parent = $item;
341 $this->set_conversation($item->get_conversation());
347 protected function remove_parent() {
348 $this->parent = null;
349 $this->conversation = null;
355 public function remove_child($item) {
356 $id = $item->get_id();
357 foreach($this->get_children() as $key => $child) {
358 if($child->get_id() == $id) {
359 $child->remove_parent();
360 unset($this->children[$key]);
361 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
362 $this->children = array_values($this->children);
366 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
373 protected function get_parent() {
374 return $this->parent;
380 public function set_conversation($conv) {
381 $this->conversation = $conv;
383 // Set it on our children too
384 foreach($this->get_children() as $child)
385 $child->set_conversation($conv);
391 public function get_conversation() {
392 return $this->conversation;
398 * We shouldn't need this
400 public function get_data() {
411 public function get_data_value($name) {
412 if(!x($this->data, $name)) {
413 logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
417 return $this->data[$name];
423 private function set_template($name) {
424 if(!x($this->available_templates, $name)) {
425 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
428 $this->template = $this->available_templates[$name];
434 private function get_template() {
435 return $this->template;
439 * Check if this is a toplevel post
441 private function is_toplevel() {
442 return $this->toplevel;
446 * Check if this is writeable
448 private function is_writeable() {
449 return $this->writeable;
453 * Count the total of our descendants
455 private function count_descendants() {
456 $children = $this->get_children();
457 $total = count($children);
459 foreach($children as $child) {
460 $total += $child->count_descendants();
467 * Get the template for the comment box
469 private function get_comment_box_template() {
470 return $this->comment_box_template;
474 * Get the comment box
477 * _ The comment box string (empty if no comment box)
480 private function get_comment_box($ww) {
481 if(!$this->is_toplevel() && !get_config('system','thread_allow')) {
486 $conv = $this->get_conversation();
487 $template = get_markup_template($this->get_comment_box_template());
489 if($conv->is_writeable() && $this->is_writeable()) {
490 $a = $this->get_app();
491 $qc = $qcomment = null;
494 * Hmmm, code depending on the presence of a particular plugin?
495 * This should be better if done by a hook
497 if(in_array('qcomment',$a->plugins)) {
498 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
499 $qcomment = (($qc) ? explode("\n",$qc) : null);
501 $comment_box = replace_macros($template,array(
502 '$return_path' => '',
503 '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
504 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
505 '$id' => $this->get_id(),
506 '$parent' => $this->get_id(),
507 '$qcomment' => $qcomment,
508 '$profile_uid' => $conv->get_profile_owner(),
509 '$mylink' => $a->contact['url'],
510 '$mytitle' => t('This is you'),
511 '$myphoto' => $a->contact['thumb'],
512 '$comment' => t('Comment'),
513 '$submit' => t('Submit'),
514 '$edbold' => t('Bold'),
515 '$editalic' => t('Italic'),
516 '$eduline' => t('Underline'),
517 '$edquote' => t('Quote'),
518 '$edcode' => t('Code'),
519 '$edimg' => t('Image'),
520 '$edurl' => t('Link'),
521 '$edvideo' => t('Video'),
522 '$preview' => t('Preview'),
523 '$sourceapp' => t($a->sourcename),
524 '$ww' => (($conv->get_mode() === 'network') ? $ww : '')
531 private function get_redirect_url() {
532 return $this->redirect_url;