2 if(class_exists('Conversation'))
5 require_once('object/Item.php');
6 require_once('include/text.php');
11 * We should think about making this a SPL Iterator
14 private $threads = array();
17 * Add a thread to the conversation
20 * _ The inserted item on success
23 public function add_thread($item) {
24 $item_id = $item->get_id();
26 logger('[ERROR] Conversation::add_thread : Item has no ID!!', LOGGER_DEBUG);
29 if($this->get_thread($item->get_id())) {
30 logger('[WARN] Conversation::add_thread : Thread already exists ('. $item->get_id() .').', LOGGER_DEBUG);
33 $this->threads[] = $item;
34 return end($this->threads);
38 * Get data in a form usable by a conversation template
40 * We should find a way to avoid using those arguments (at least most of them)
43 * _ The data requested on success
46 public function get_template_data($a, $cmnt_tpl, $page_writeable, $mode, $profile_owner) {
49 /*foreach($this->threads as $item) {
50 $item_data = $item->get_template_data();
52 logger('[ERROR] Conversation::get_template_data : Failed to get item template data ('. $item->get_id() .').', LOGGER_DEBUG);
55 $result[] = $item_data;
59 * This should be moved on the item object;
62 foreach($this->threads as $item) {
63 $threads[] = $item->get_data();
65 return $this->prepare_threads_body($a, $threads, $cmnt_tpl, $page_writeable, $mode, $profile_owner);
71 * Get a thread based on its item id
74 * _ The found item on success
77 private function get_thread($id) {
78 foreach($this->threads as $item) {
79 if($item->get_id() == $id)
87 * Recursively prepare a thread for HTML
90 private function prepare_threads_body($a, $items, $cmnt_tpl, $page_writeable, $mode, $profile_owner, $thread_level=1) {
93 $wall_template = 'wall_thread.tpl';
94 $wallwall_template = 'wallwall_thread.tpl';
96 $nb_items = count($items);
98 $total_children = $nb_items;
100 foreach($items as $item) {
101 // prevent private email reply to public conversation from leaking.
102 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
103 // Don't count it as a visible item
113 $template = $wall_template;
116 $owner_url = $owner_photo = $owner_name = '';
120 $isstarred = "unstarred";
121 $photo = $item['photo'];
122 $thumb = $item['thumb'];
125 $lastcollapsed = false;
126 $firstcollapsed = false;
127 $total_children += count_descendants($item);
129 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
130 $item_writeable = (($item['writable'] || $item['self']) ? true : false);
131 $show_comment_box = ((($page_writeable) && ($item_writeable)) ? true : false);
132 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
133 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
134 ? t('Private Message')
136 $redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $item['cid'] ;
137 $shareable = ((($profile_owner == local_user()) && ($item['private'] != 1)) ? true : false);
138 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
139 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
142 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
146 'dropping' => $dropping,
147 'select' => t('Select'),
148 'delete' => t('Delete'),
151 $filer = (($profile_owner == local_user()) ? t("save to folder") : false);
153 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
154 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
155 if($item['author-link'] && (! $item['author-name']))
156 $profile_name = $item['author-link'];
159 $profile_link = best_link_url($item,$sp);
160 if($profile_link === 'mailbox')
163 $sparkle = ' sparkle';
165 $profile_link = zrl($profile_link);
167 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
168 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
169 $profile_avatar = $a->contacts[$normalised]['thumb'];
171 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($thumb));
173 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
174 call_hooks('render_location',$locate);
175 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
178 foreach(explode(',',$item['tag']) as $tag){
180 if ($tag!="") $tags[] = bbcode($tag);
183 like_puller($a,$item,$alike,'like');
184 like_puller($a,$item,$dlike,'dislike');
186 $like = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
187 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
190 if((! $item['self']) && ($mode !== 'profile')) {
193 // On the network page, I am the owner. On the display page it will be the profile owner.
194 // This will have been stored in $a->page_contact by our calling page.
195 // Put this person as the wall owner of the wall-to-wall notice.
197 $owner_url = zrl($a->page_contact['url']);
198 $owner_photo = $a->page_contact['thumb'];
199 $owner_name = $a->page_contact['name'];
200 $template = $wallwall_template;
204 else if($item['owner-link']) {
206 $owner_linkmatch = (($item['owner-link']) && link_compare($item['owner-link'],$item['author-link']));
207 $alias_linkmatch = (($item['alias']) && link_compare($item['alias'],$item['author-link']));
208 $owner_namematch = (($item['owner-name']) && $item['owner-name'] == $item['author-name']);
209 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
211 // The author url doesn't match the owner (typically the contact)
212 // and also doesn't match the contact alias.
213 // The name match is a hack to catch several weird cases where URLs are
214 // all over the park. It can be tricked, but this prevents you from
215 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
216 // well that it's the same Bob Smith.
218 // But it could be somebody else with the same name. It just isn't highly likely.
221 $owner_url = $item['owner-link'];
222 $owner_photo = $item['owner-avatar'];
223 $owner_name = $item['owner-name'];
224 $template = $wallwall_template;
226 // If it is our contact, use a friendly redirect link
227 if((link_compare($item['owner-link'],$item['url']))
228 && ($item['network'] === NETWORK_DFRN)) {
229 $owner_url = $redirect_url;
230 $osparkle = ' sparkle';
233 $owner_url = zrl($owner_url);
236 if($profile_owner == local_user()) {
237 $isstarred = (($item['starred']) ? "starred" : "unstarred");
240 'do' => t("add star"),
241 'undo' => t("remove star"),
242 'toggle' => t("toggle star status"),
243 'classdo' => (($item['starred']) ? "hidden" : ""),
244 'classundo' => (($item['starred']) ? "" : "hidden"),
245 'starred' => t('starred'),
246 'tagger' => t("add tag"),
253 if(($nb_items > 2) || ($thread_level > 2)) {
254 if($items_seen == 1) {
255 $firstcollapsed = true;
257 if($thread_level > 2) {
258 if($items_seen == $nb_items)
259 $lastcollapsed = true;
261 else if($items_seen == ($nb_items - 2)) {
262 $lastcollapsed = true;
267 if($page_writeable) {
269 'like' => array( t("I like this \x28toggle\x29"), t("like")),
270 'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
272 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
275 if($show_comment_box) {
276 $qc = $qcomment = null;
278 if(in_array('qcomment',$a->plugins)) {
279 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
280 $qcomment = (($qc) ? explode("\n",$qc) : null);
282 $comment = replace_macros($cmnt_tpl,array(
283 '$return_path' => '',
284 '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
285 '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
286 '$id' => $item['item_id'],
287 '$parent' => $item['item_id'],
288 '$qcomment' => $qcomment,
289 '$profile_uid' => $profile_owner,
290 '$mylink' => $a->contact['url'],
291 '$mytitle' => t('This is you'),
292 '$myphoto' => $a->contact['thumb'],
293 '$comment' => t('Comment'),
294 '$submit' => t('Submit'),
295 '$edbold' => t('Bold'),
296 '$editalic' => t('Italic'),
297 '$eduline' => t('Underline'),
298 '$edquote' => t('Quote'),
299 '$edcode' => t('Code'),
300 '$edimg' => t('Image'),
301 '$edurl' => t('Link'),
302 '$edvideo' => t('Video'),
303 '$preview' => t('Preview'),
304 '$sourceapp' => t($a->sourcename),
305 '$ww' => (($mode === 'network') ? $commentww : '')
310 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
313 localize_item($item);
315 $body = prepare_body($item,true);
318 // collapse comments in template. I don't like this much...
319 'comment_firstcollapsed' => $firstcollapsed,
320 'comment_lastcollapsed' => $lastcollapsed,
321 // template to use to render item (wall, walltowall, search)
322 'template' => $template,
324 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
326 'body' => template_escape($body),
327 'text' => strip_tags(template_escape($body)),
328 'id' => $item['item_id'],
329 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
330 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $owner_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
332 'wall' => t('Wall-to-Wall'),
333 'vwall' => t('via Wall-To-Wall:'),
334 'profile_url' => $profile_link,
335 'item_photo_menu' => item_photo_menu($item),
336 'name' => template_escape($profile_name),
337 'thumb' => $profile_avatar,
338 'osparkle' => $osparkle,
339 'sparkle' => $sparkle,
340 'title' => template_escape($item['title']),
341 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
343 'location' => template_escape($location),
345 'owner_url' => $owner_url,
346 'owner_photo' => $owner_photo,
347 'owner_name' => template_escape($owner_name),
348 'plink' => get_plink($item),
350 'isstarred' => $isstarred,
356 'dislike' => $dislike,
357 'comment' => $comment,
358 'previewing' => $previewing,
359 'wait' => t('Please wait'),
362 $arr = array('item' => $item, 'output' => $tmp_item);
363 call_hooks('display_item', $arr);
365 $item_result = $arr['output'];
366 if($firstcollapsed) {
367 $item_result['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
368 $item_result['hide_text'] = t('show more');
371 $item_result['children'] = array();
372 if(count($item['children'])) {
373 $item_result['children'] = prepare_threads_body($a, $item['children'], $cmnt_tpl, $page_writeable, $mode, $profile_owner, ($thread_level + 1));
375 $item_result['private'] = $item['private'];
376 $item_result['toplevel'] = ($toplevelpost ? 'toplevel_item' : '');
379 * I don't like this very much...
381 if(get_config('system','thread_allow')) {
382 $item_result['flatten'] = false;
383 $item_result['threaded'] = true;
386 $item_result['flatten'] = true;
387 $item_result['threaded'] = false;
389 $item_result['comment'] = false;
393 $result[] = $item_result;