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;
25 public function __construct($data) {
27 $this->set_template('wall');
28 $this->toplevel = ($this->get_id() == $this->get_parent());
29 $this->writeable = ($this->get_data_value('writeable') || $this->get_data_value('self'));
33 * Get data in a form usable by a conversation template
36 * _ The data requested on success
39 public function get_template_data($cmnt_tpl, $mode, $alike, $dlike) {
42 $a = $this->get_app();
44 $this->set_mode($mode);
46 $item = $this->get_data();
51 $owner_url = $owner_photo = $owner_name = '';
55 $isstarred = "unstarred";
58 $lastcollapsed = false;
59 $firstcollapsed = false;
60 $total_children += count_descendants($item);
62 $show_comment_box = ((($this->is_page_writeable()) && ($this->is_writeable())) ? true : false);
63 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
64 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
65 ? t('Private Message')
67 $redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $item['cid'] ;
68 $shareable = ((($this->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
69 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
70 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
73 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
77 'dropping' => $dropping,
78 'select' => t('Select'),
79 'delete' => t('Delete'),
82 $filer = (($this->get_profile_owner() == local_user()) ? t("save to folder") : false);
84 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
85 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
86 if($item['author-link'] && (! $item['author-name']))
87 $profile_name = $item['author-link'];
90 $profile_link = best_link_url($item,$sp);
91 if($profile_link === 'mailbox')
94 $sparkle = ' sparkle';
96 $profile_link = zrl($profile_link);
98 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
99 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
100 $profile_avatar = $a->contacts[$normalised]['thumb'];
102 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_thumb()));
104 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
105 call_hooks('render_location',$locate);
106 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
109 foreach(explode(',',$item['tag']) as $tag){
111 if ($tag!="") $tags[] = bbcode($tag);
114 like_puller($a,$item,$alike,'like');
115 like_puller($a,$item,$dlike,'dislike');
117 $like = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
118 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
120 if($this->is_toplevel()) {
121 if((! $item['self']) && ($this->get_mode() !== 'profile')) {
124 // On the network page, I am the owner. On the display page it will be the profile owner.
125 // This will have been stored in $a->page_contact by our calling page.
126 // Put this person as the wall owner of the wall-to-wall notice.
128 $owner_url = zrl($a->page_contact['url']);
129 $owner_photo = $a->page_contact['thumb'];
130 $owner_name = $a->page_contact['name'];
131 $this->set_template('wall2wall');
135 else if($item['owner-link']) {
137 $owner_linkmatch = (($item['owner-link']) && link_compare($item['owner-link'],$item['author-link']));
138 $alias_linkmatch = (($item['alias']) && link_compare($item['alias'],$item['author-link']));
139 $owner_namematch = (($item['owner-name']) && $item['owner-name'] == $item['author-name']);
140 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
142 // The author url doesn't match the owner (typically the contact)
143 // and also doesn't match the contact alias.
144 // The name match is a hack to catch several weird cases where URLs are
145 // all over the park. It can be tricked, but this prevents you from
146 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
147 // well that it's the same Bob Smith.
149 // But it could be somebody else with the same name. It just isn't highly likely.
152 $owner_url = $item['owner-link'];
153 $owner_photo = $item['owner-avatar'];
154 $owner_name = $item['owner-name'];
155 $this->set_template('wall2wall');
157 // If it is our contact, use a friendly redirect link
158 if((link_compare($item['owner-link'],$item['url']))
159 && ($item['network'] === NETWORK_DFRN)) {
160 $owner_url = $redirect_url;
161 $osparkle = ' sparkle';
164 $owner_url = zrl($owner_url);
167 if($this->get_profile_owner() == local_user()) {
168 $isstarred = (($item['starred']) ? "starred" : "unstarred");
171 'do' => t("add star"),
172 'undo' => t("remove star"),
173 'toggle' => t("toggle star status"),
174 'classdo' => (($item['starred']) ? "hidden" : ""),
175 'classundo' => (($item['starred']) ? "" : "hidden"),
176 'starred' => t('starred'),
177 'tagger' => t("add tag"),
184 if(($nb_items > 2) || ($thread_level > 2)) {
185 if($items_seen == 1) {
186 $firstcollapsed = true;
188 if($thread_level > 2) {
189 if($items_seen == $nb_items)
190 $lastcollapsed = true;
192 else if($items_seen == ($nb_items - 2)) {
193 $lastcollapsed = true;
198 if($this->is_page_writeable()) {
200 'like' => array( t("I like this \x28toggle\x29"), t("like")),
201 'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
203 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
206 if($show_comment_box) {
207 $qc = $qcomment = null;
209 if(in_array('qcomment',$a->plugins)) {
210 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
211 $qcomment = (($qc) ? explode("\n",$qc) : null);
213 $comment = replace_macros($cmnt_tpl,array(
214 '$return_path' => '',
215 '$jsreload' => (($this->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
216 '$type' => (($this->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
217 '$id' => $item['item_id'],
218 '$parent' => $item['item_id'],
219 '$qcomment' => $qcomment,
220 '$profile_uid' => $this->get_profile_owner(),
221 '$mylink' => $a->contact['url'],
222 '$mytitle' => t('This is you'),
223 '$myphoto' => $a->contact['thumb'],
224 '$comment' => t('Comment'),
225 '$submit' => t('Submit'),
226 '$edbold' => t('Bold'),
227 '$editalic' => t('Italic'),
228 '$eduline' => t('Underline'),
229 '$edquote' => t('Quote'),
230 '$edcode' => t('Code'),
231 '$edimg' => t('Image'),
232 '$edurl' => t('Link'),
233 '$edvideo' => t('Video'),
234 '$preview' => t('Preview'),
235 '$sourceapp' => t($a->sourcename),
236 '$ww' => (($this->get_mode() === 'network') ? $commentww : '')
241 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
244 localize_item($item);
246 $body = prepare_body($item,true);
249 // collapse comments in template. I don't like this much...
250 'comment_firstcollapsed' => $firstcollapsed,
251 'comment_lastcollapsed' => $lastcollapsed,
252 // template to use to render item (wall, walltowall, search)
253 'template' => $this->get_template(),
255 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
257 'body' => template_escape($body),
258 'text' => strip_tags(template_escape($body)),
259 'id' => $item['item_id'],
260 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
261 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $owner_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
263 'wall' => t('Wall-to-Wall'),
264 'vwall' => t('via Wall-To-Wall:'),
265 'profile_url' => $profile_link,
266 'item_photo_menu' => item_photo_menu($item),
267 'name' => template_escape($profile_name),
268 'thumb' => $profile_avatar,
269 'osparkle' => $osparkle,
270 'sparkle' => $sparkle,
271 'title' => template_escape($item['title']),
272 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
274 'location' => template_escape($location),
276 'owner_url' => $owner_url,
277 'owner_photo' => $owner_photo,
278 'owner_name' => template_escape($owner_name),
279 'plink' => get_plink($item),
281 'isstarred' => $isstarred,
287 'dislike' => $dislike,
288 'comment' => $comment,
289 'previewing' => $previewing,
290 'wait' => t('Please wait'),
293 $arr = array('item' => $item, 'output' => $tmp_item);
294 call_hooks('display_item', $arr);
296 $item_result = $arr['output'];
297 if($firstcollapsed) {
298 $item_result['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
299 $item_result['hide_text'] = t('show more');
302 $item_result['children'] = array();
303 if(count($item['children'])) {
304 $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));
306 $item_result['private'] = $item['private'];
307 $item_result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
309 if(get_config('system','thread_allow')) {
310 $item_result['flatten'] = false;
311 $item_result['threaded'] = true;
314 $item_result['flatten'] = true;
315 $item_result['threaded'] = false;
316 if(!$htis->is_toplevel()) {
317 $item_result['comment'] = false;
321 $result = $item_result;
326 public function get_id() {
327 return $this->get_data_value('id');
330 public function get_network() {
331 return $this->get_data_value('network');
334 public function get_uid() {
335 return $this->get_data_value('uid');
338 public function get_thumb() {
339 return $this->get_data_value('thumb');
342 public function get_parent() {
343 return $this->get_data_value('parent');
349 * We shouldn't need this
351 public function get_data() {
362 private function get_data_value($name) {
363 if(!x($this->data, $name)) {
364 logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
368 return $this->data[$name];
372 * Set the mode we'll be displayed on
374 private function set_mode($mode) {
375 if($this->get_mode() == $mode)
381 $this->profile_owner = local_user();
382 $this->page_writeable = true;
385 $this->profile_owner = $a->profile['profile_uid'];
386 $this->page_writeable = can_write_wall($a,$this->profile_owner);
389 $this->profile_owner = $a->profile['uid'];
390 $this->page_writeable = can_write_wall($a,$this->profile_owner);
393 logger('[ERROR] Item::set_mode : Unhandled mode ('. $mode .').', LOGGER_DEBUG);
402 private function get_mode() {
409 private function get_profile_owner() {
410 return $this->profile_owner;
416 private function is_page_writeable() {
417 return $this->page_writeable;
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;