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'
20 private $page_writeable = false;
21 private $profile_owner = 0;
22 private $toplevel = false;
23 private $writeable = false;
24 private $children = array();
25 private $parent = null;
27 public function __construct($data) {
29 $this->set_template('wall');
30 $this->toplevel = ($this->get_id() == $this->get_data_value('parent'));
31 $this->writeable = ($this->get_data_value('writeable') || $this->get_data_value('self'));
33 // Prepare the children
34 foreach($data['children'] as $item) {
35 $child = new Item($item);
36 $this->add_child($child);
41 * Get data in a form usable by a conversation template
44 * _ The data requested on success
47 public function get_template_data($cmnt_tpl, $mode, $alike, $dlike) {
50 $a = $this->get_app();
52 $this->set_mode($mode);
54 $item = $this->get_data();
59 $owner_url = $owner_photo = $owner_name = '';
63 $isstarred = "unstarred";
66 $lastcollapsed = false;
67 $firstcollapsed = false;
68 $total_children = $this->count_descendants();
70 $show_comment_box = ((($this->is_page_writeable()) && ($this->is_writeable())) ? true : false);
71 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
72 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
73 ? t('Private Message')
75 $redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $item['cid'] ;
76 $shareable = ((($this->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
77 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
78 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
81 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
85 'dropping' => $dropping,
86 'select' => t('Select'),
87 'delete' => t('Delete'),
90 $filer = (($this->get_profile_owner() == local_user()) ? t("save to folder") : false);
92 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
93 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
94 if($item['author-link'] && (! $item['author-name']))
95 $profile_name = $item['author-link'];
98 $profile_link = best_link_url($item,$sp);
99 if($profile_link === 'mailbox')
102 $sparkle = ' sparkle';
104 $profile_link = zrl($profile_link);
106 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
107 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
108 $profile_avatar = $a->contacts[$normalised]['thumb'];
110 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_data_value('thumb')));
112 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
113 call_hooks('render_location',$locate);
114 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
117 foreach(explode(',',$item['tag']) as $tag){
119 if ($tag!="") $tags[] = bbcode($tag);
122 like_puller($a,$item,$alike,'like');
123 like_puller($a,$item,$dlike,'dislike');
125 $like = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
126 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
128 if($this->is_toplevel()) {
129 if((! $item['self']) && ($this->get_mode() !== 'profile')) {
132 // On the network page, I am the owner. On the display page it will be the profile owner.
133 // This will have been stored in $a->page_contact by our calling page.
134 // Put this person as the wall owner of the wall-to-wall notice.
136 $owner_url = zrl($a->page_contact['url']);
137 $owner_photo = $a->page_contact['thumb'];
138 $owner_name = $a->page_contact['name'];
139 $this->set_template('wall2wall');
143 else if($item['owner-link']) {
145 $owner_linkmatch = (($item['owner-link']) && link_compare($item['owner-link'],$item['author-link']));
146 $alias_linkmatch = (($item['alias']) && link_compare($item['alias'],$item['author-link']));
147 $owner_namematch = (($item['owner-name']) && $item['owner-name'] == $item['author-name']);
148 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
150 // The author url doesn't match the owner (typically the contact)
151 // and also doesn't match the contact alias.
152 // The name match is a hack to catch several weird cases where URLs are
153 // all over the park. It can be tricked, but this prevents you from
154 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
155 // well that it's the same Bob Smith.
157 // But it could be somebody else with the same name. It just isn't highly likely.
160 $owner_url = $item['owner-link'];
161 $owner_photo = $item['owner-avatar'];
162 $owner_name = $item['owner-name'];
163 $this->set_template('wall2wall');
165 // If it is our contact, use a friendly redirect link
166 if((link_compare($item['owner-link'],$item['url']))
167 && ($item['network'] === NETWORK_DFRN)) {
168 $owner_url = $redirect_url;
169 $osparkle = ' sparkle';
172 $owner_url = zrl($owner_url);
175 if($this->get_profile_owner() == local_user()) {
176 $isstarred = (($item['starred']) ? "starred" : "unstarred");
179 'do' => t("add star"),
180 'undo' => t("remove star"),
181 'toggle' => t("toggle star status"),
182 'classdo' => (($item['starred']) ? "hidden" : ""),
183 'classundo' => (($item['starred']) ? "" : "hidden"),
184 'starred' => t('starred'),
185 'tagger' => t("add tag"),
192 if(($nb_items > 2) || ($thread_level > 2)) {
193 if($items_seen == 1) {
194 $firstcollapsed = true;
196 if($thread_level > 2) {
197 if($items_seen == $nb_items)
198 $lastcollapsed = true;
200 else if($items_seen == ($nb_items - 2)) {
201 $lastcollapsed = true;
206 if($this->is_page_writeable()) {
208 'like' => array( t("I like this \x28toggle\x29"), t("like")),
209 'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
211 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
214 if($show_comment_box) {
215 $qc = $qcomment = null;
217 if(in_array('qcomment',$a->plugins)) {
218 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
219 $qcomment = (($qc) ? explode("\n",$qc) : null);
221 $comment = replace_macros($cmnt_tpl,array(
222 '$return_path' => '',
223 '$jsreload' => (($this->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
224 '$type' => (($this->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
225 '$id' => $item['item_id'],
226 '$parent' => $item['item_id'],
227 '$qcomment' => $qcomment,
228 '$profile_uid' => $this->get_profile_owner(),
229 '$mylink' => $a->contact['url'],
230 '$mytitle' => t('This is you'),
231 '$myphoto' => $a->contact['thumb'],
232 '$comment' => t('Comment'),
233 '$submit' => t('Submit'),
234 '$edbold' => t('Bold'),
235 '$editalic' => t('Italic'),
236 '$eduline' => t('Underline'),
237 '$edquote' => t('Quote'),
238 '$edcode' => t('Code'),
239 '$edimg' => t('Image'),
240 '$edurl' => t('Link'),
241 '$edvideo' => t('Video'),
242 '$preview' => t('Preview'),
243 '$sourceapp' => t($a->sourcename),
244 '$ww' => (($this->get_mode() === 'network') ? $commentww : '')
249 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
252 localize_item($item);
254 $body = prepare_body($item,true);
257 // collapse comments in template. I don't like this much...
258 'comment_firstcollapsed' => $firstcollapsed,
259 'comment_lastcollapsed' => $lastcollapsed,
260 // template to use to render item (wall, walltowall, search)
261 'template' => $this->get_template(),
263 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
265 'body' => template_escape($body),
266 'text' => strip_tags(template_escape($body)),
267 'id' => $item['item_id'],
268 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
269 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $owner_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
271 'wall' => t('Wall-to-Wall'),
272 'vwall' => t('via Wall-To-Wall:'),
273 'profile_url' => $profile_link,
274 'item_photo_menu' => item_photo_menu($item),
275 'name' => template_escape($profile_name),
276 'thumb' => $profile_avatar,
277 'osparkle' => $osparkle,
278 'sparkle' => $sparkle,
279 'title' => template_escape($item['title']),
280 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
282 'location' => template_escape($location),
284 'owner_url' => $owner_url,
285 'owner_photo' => $owner_photo,
286 'owner_name' => template_escape($owner_name),
287 'plink' => get_plink($item),
289 'isstarred' => $isstarred,
295 'dislike' => $dislike,
296 'comment' => $comment,
297 'previewing' => $previewing,
298 'wait' => t('Please wait'),
301 $arr = array('item' => $item, 'output' => $tmp_item);
302 call_hooks('display_item', $arr);
304 $item_result = $arr['output'];
305 if($firstcollapsed) {
306 $item_result['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
307 $item_result['hide_text'] = t('show more');
310 $item_result['children'] = array();
311 if(count($item['children'])) {
312 $item_result['children'] = prepare_threads_body($a, $item['children'], $cmnt_tpl, $this->is_page_writeable(), $this->get_mode(), $this->get_profile_owner(), $alike, $dlike, ($thread_level + 1));
314 $item_result['private'] = $item['private'];
315 $item_result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
317 if(get_config('system','thread_allow')) {
318 $item_result['flatten'] = false;
319 $item_result['threaded'] = true;
322 $item_result['flatten'] = true;
323 $item_result['threaded'] = false;
324 if(!$htis->is_toplevel()) {
325 $item_result['comment'] = false;
329 $result = $item_result;
334 public function get_id() {
335 return $this->get_data_value('id');
341 public function add_child($item) {
342 $item_id = $item->get_id();
344 logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
347 if($this->get_child($item->get_id())) {
348 logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
351 $item->set_parent($this);
352 $this->children[] = $item;
353 return end($this->children);
357 * Get a child by its ID
359 public function get_child($id) {
360 foreach($this->get_children() as $child) {
361 if($child->get_id() == $id)
368 * Get all ou children
370 public function get_children() {
371 return $this->children;
377 protected function set_parent($item) {
378 $parent = $this->get_parent();
380 $parent->remove_child($this);
382 $this->parent = $item;
388 public function remove_child($item) {
389 $id = $item->get_id();
390 foreach($this->get_children() as $key => $child) {
391 if($child->get_id() == $id) {
392 unset($this->children[$key]);
393 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
394 $this->children = array_values($this->children);
398 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
405 protected function get_parent() {
406 return $this->parent;
412 * We shouldn't need this
414 public function get_data() {
425 public function get_data_value($name) {
426 if(!x($this->data, $name)) {
427 logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
431 return $this->data[$name];
435 * Set the mode we'll be displayed on
437 private function set_mode($mode) {
438 if($this->get_mode() == $mode)
444 $this->profile_owner = local_user();
445 $this->page_writeable = true;
448 $this->profile_owner = $a->profile['profile_uid'];
449 $this->page_writeable = can_write_wall($a,$this->profile_owner);
452 $this->profile_owner = $a->profile['uid'];
453 $this->page_writeable = can_write_wall($a,$this->profile_owner);
456 logger('[ERROR] Item::set_mode : Unhandled mode ('. $mode .').', LOGGER_DEBUG);
465 private function get_mode() {
472 private function get_profile_owner() {
473 return $this->profile_owner;
479 private function is_page_writeable() {
480 return $this->page_writeable;
486 private function set_template($name) {
487 if(!x($this->available_templates, $name)) {
488 logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
491 $this->template = $this->available_templates[$name];
497 private function get_template() {
498 return $this->template;
502 * Check if this is a toplevel post
504 private function is_toplevel() {
505 return $this->toplevel;
509 * Check if this is writeable
511 private function is_writeable() {
512 return $this->writeable;
516 * Count the total of our descendants
518 private function count_descendants() {
519 $children = $this->get_children();
520 $total = count($children);
522 foreach($children as $child) {
523 $total += $child->count_descendants();