4 * "Render" a conversation or list of items for HTML display.
5 * There are two major forms of display:
6 * - Sequential or unthreaded ("New Item View" or search results)
8 * The $mode parameter decides between the various renderings and also
9 * figures out how to determine page owner and other contextual items
10 * that are based on unique features of the calling module.
14 function conversation(&$a, $items, $mode, $update) {
16 require_once('bbcode.php');
19 $page_writeable = false;
21 if($mode === 'network') {
22 $profile_owner = local_user();
23 $page_writeable = true;
26 if($mode === 'profile') {
27 $profile_owner = $a->profile['profile_uid'];
28 $page_writeable = can_write_wall($a,$profile_owner);
31 if($mode === 'display') {
32 $profile_owner = $a->profile['uid'];
33 $page_writeable = can_write_wall($a,$profile_owner);
37 $return_url = $_SESSION['return_url'];
39 $return_url = $_SESSION['return_url'] = $a->cmd;
42 // find all the authors involved in remote conversations
43 // We will use a local profile photo if they are one of our contacts
44 // otherwise we have to get the photo from the item owner's site
46 $author_contacts = extract_item_authors($items,local_user());
49 $cmnt_tpl = load_view_file('view/comment_item.tpl');
50 $like_tpl = load_view_file('view/like.tpl');
51 $noshare_tpl = load_view_file('view/like_noshare.tpl');
52 $tpl = load_view_file('view/wall_item.tpl');
53 $wallwall = load_view_file('view/wallwall_item.tpl');
60 if($mode === 'network-new' || $mode === 'search') {
62 // "New Item View" on network page or search page results
63 // - just loop through the items and format them minimally for display
65 $tpl = load_view_file('view/search_item.tpl');
66 $droptpl = load_view_file('view/wall_fake_drop.tpl');
68 foreach($items as $item) {
76 if($mode === 'search') {
77 if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE)))
78 && ($item['id'] != $item['parent']))
80 $nickname = $item['nickname'];
83 $nickname = $a->user['nickname'];
85 $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
86 $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
87 $profile_link = ((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
88 if($profile_link === 'mailbox')
91 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
93 if(strlen($item['author-link'])) {
94 if(link_compare($item['author-link'],$item['url']) && ($item['network'] === 'dfrn') && (! $item['self'])) {
95 $profile_link = $redirect_url;
96 $sparkle = ' sparkle';
98 elseif(isset($author_contacts[$item['author-link']])) {
99 $profile_link = $a->get_baseurl() . '/redir/' . $author_contacts[$item['author-link']];
100 $sparkle = ' sparkle';
104 $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
105 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
108 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
110 $location = '<span class="smalltext">' . $coord . '</span>';
116 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
119 $drop = replace_macros((($dropping)? $droptpl : $fakedrop), array('$id' => $item['id'], '$delete' => t('Delete')));
123 $drop = replace_macros($droptpl,array('$id' => $item['id']));
124 $lock = '<div class="wall-item-lock"></div>';
126 $o .= replace_macros($tpl,array(
127 '$id' => $item['item_id'],
128 '$linktitle' => sprintf( t('View %s\'s profile'), $profile_name),
129 '$profile_url' => $profile_link,
130 '$item_photo_menu' => item_photo_menu($item),
131 '$name' => $profile_name,
132 '$sparkle' => $sparkle,
134 '$thumb' => $profile_avatar,
135 '$title' => $item['title'],
136 '$body' => smilies(bbcode($item['body'])),
137 '$ago' => relative_date($item['created']),
138 '$location' => $location,
140 '$owner_url' => $owner_url,
141 '$owner_photo' => $owner_photo,
142 '$owner_name' => $owner_name,
144 '$conv' => '<a href="' . $a->get_baseurl() . '/display/' . $nickname . '/' . $item['id'] . '">' . t('View in context') . '</a>'
158 // Figure out how many comments each parent has
159 // (Comments all have gravity of 6)
160 // Store the result in the $comments array
163 foreach($items as $item) {
164 if(intval($item['gravity']) == 6) {
165 if(! x($comments,$item['parent']))
166 $comments[$item['parent']] = 1;
168 $comments[$item['parent']] += 1;
172 // map all the like/dislike activities for each parent item
173 // Store these in the $alike and $dlike arrays
175 foreach($items as $item) {
176 like_puller($a,$item,$alike,'like');
177 like_puller($a,$item,$dlike,'dislike');
180 $comments_collapsed = false;
184 foreach($items as $item) {
190 $owner_url = $owner_photo = $owner_name = '';
192 // We've already parsed out like/dislike for special treatment. We can ignore them now
194 if(((activity_match($item['verb'],ACTIVITY_LIKE))
195 || (activity_match($item['verb'],ACTIVITY_DISLIKE)))
196 && ($item['id'] != $item['parent']))
199 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
202 // Take care of author collapsing and comment collapsing
203 // If a single author has more than 3 consecutive top-level posts, squash the remaining ones.
204 // If there are more than two comments, squash all but the last 2.
208 $item_writeable = (($item['writable'] || $item['self']) ? true : false);
210 if($blowhard == $item['cid'] && (! $item['self']) && ($mode != 'profile')) {
212 if($blowhard_count == 3) {
213 $o .= '<div class="icollapse-wrapper fakelink" id="icollapse-wrapper-' . $item['parent']
214 . '" onclick="openClose(' . '\'icollapse-' . $item['parent'] . '\');" >'
215 . t('See more posts like this') . '</div>' . '<div class="icollapse" id="icollapse-'
216 . $item['parent'] . '" style="display: none;" >';
220 $blowhard = $item['cid'];
221 if($blowhard_count >= 3)
227 $comments_collapsed = false;
233 $show_comment_box = ((($page_writeable) && ($item_writeable) && ($comments_seen == $comments[$item['parent']])) ? true : false);
235 if(($comments[$item['parent']] > 2) && ($comments_seen <= ($comments[$item['parent']] - 2)) && ($item['gravity'] == 6)) {
236 if(! $comments_collapsed) {
237 $o .= '<div class="ccollapse-wrapper fakelink" id="ccollapse-wrapper-' . $item['parent']
238 . '" onclick="openClose(' . '\'ccollapse-' . $item['parent'] . '\');" >'
239 . sprintf( t('See all %d comments'), $comments[$item['parent']]) . '</div>'
240 . '<div class="ccollapse" id="ccollapse-' . $item['parent'] . '" style="display: none;" >';
241 $comments_collapsed = true;
244 if(($comments[$item['parent']] > 2) && ($comments_seen == ($comments[$item['parent']] - 1))) {
248 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
250 $lock = ((($item['private']) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
251 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
252 ? '<div class="wall-item-lock"><img src="images/lock_icon.gif" class="lockview" alt="' . t('Private Message') . '" onclick="lockview(event,' . $item['id'] . ');" /></div>'
253 : '<div class="wall-item-lock"></div>');
256 // Top-level wall post not written by the wall owner (wall-to-wall)
257 // First figure out who owns it.
261 if(($toplevelpost) && (! $item['self']) && ($mode !== 'profile')) {
263 if($item['type'] === 'wall') {
265 // On the network page, I am the owner. On the display page it will be the profile owner.
266 // This will have been stored in $a->page_contact by our calling page.
267 // Put this person on the left of the wall-to-wall notice.
269 $owner_url = $a->page_contact['url'];
270 $owner_photo = $a->page_contact['thumb'];
271 $owner_name = $a->page_contact['name'];
272 $template = $wallwall;
275 if(($item['type'] === 'remote') && (strlen($item['owner-link'])) && ($item['owner-link'] != $item['author-link'])) {
279 $owner_url = $item['owner-link'];
280 $owner_photo = $item['owner-avatar'];
281 $owner_name = $item['owner-name'];
282 $template = $wallwall;
284 // If it is our contact, use a friendly redirect link
285 if((link_compare($item['owner-link'],$item['url']))
286 && ($item['network'] === 'dfrn')) {
287 $owner_url = $redirect_url;
288 $osparkle = ' sparkle';
296 if($page_writeable) {
298 $likebuttons = replace_macros((($item['private']) ? $noshare_tpl : $like_tpl),array(
299 '$id' => $item['id'],
300 '$likethis' => t("I like this \x28toggle\x29"),
301 '$nolike' => t("I don't like this \x28toggle\x29"),
302 '$share' => t('Share'),
303 '$wait' => t('Please wait')
307 if(($show_comment_box) || (($show_comment_box == false) && ($item['last-child']))) {
308 $comment = replace_macros($cmnt_tpl,array(
309 '$return_path' => '',
310 '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
311 '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
312 '$id' => $item['item_id'],
313 '$parent' => $item['parent'],
314 '$profile_uid' => $profile_owner,
315 '$mylink' => $a->contact['url'],
316 '$mytitle' => t('This is you'),
317 '$myphoto' => $a->contact['thumb'],
318 '$comment' => t('Comment'),
319 '$submit' => t('Submit'),
320 '$ww' => (($mode === 'network') ? $commentww : '')
325 $edpost = ((($profile_owner == local_user()) && ($toplevelpost) && (intval($item['wall']) == 1))
326 ? '<a class="editpost" href="' . $a->get_baseurl() . '/editpost/' . $item['id']
327 . '" title="' . t('Edit') . '"><img src="images/pencil.gif" /></a>'
329 $drop = replace_macros(load_view_file('view/wall_item_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
331 $photo = $item['photo'];
332 $thumb = $item['thumb'];
334 // Post was remotely authored.
336 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
338 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
339 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $thumb);
341 if($mode === 'profile') {
342 if(local_user() && ($item['contact-uid'] == local_user()) && ($item['network'] === 'dfrn') && (! $item['self'] )) {
343 $profile_link = $redirect_url;
344 $sparkle = ' sparkle';
347 $profile_link = $item['url'];
351 elseif(strlen($item['author-link'])) {
352 $profile_link = $item['author-link'];
353 if(link_compare($item['author-link'],$item['url']) && ($item['network'] === 'dfrn') && (! $item['self'])) {
354 $profile_link = $redirect_url;
355 $sparkle = ' sparkle';
357 elseif(isset($author_contacts[$item['author-link']])) {
358 $profile_link = $a->get_baseurl() . '/redir/' . $author_contacts[$item['author-link']];
359 $sparkle = ' sparkle';
363 $profile_link = $item['url'];
365 if($profile_link === 'mailbox')
368 $like = ((x($alike,$item['id'])) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
369 $dislike = ((x($dlike,$item['id'])) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
371 $location = (($item['location']) ? '<a target="map" title="' . $item['location']
372 . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
373 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord']
374 . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
377 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
379 $location = '<span class="smalltext">' . $coord . '</span>';
382 $indent = (($toplevelpost) ? '' : ' comment');
384 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
389 $tmp_item = replace_macros($template,array(
390 '$id' => $item['item_id'],
391 '$linktitle' => sprintf( t('View %s\'s profile'), $profile_name),
392 '$olinktitle' => sprintf( t('View %s\'s profile'), $owner_name),
394 '$wall' => t('Wall-to-Wall'),
395 '$vwall' => t('via Wall-To-Wall:'),
396 '$profile_url' => $profile_link,
397 '$item_photo_menu' => item_photo_menu($item),
398 '$name' => $profile_name,
399 '$thumb' => $profile_avatar,
400 '$osparkle' => $osparkle,
401 '$sparkle' => $sparkle,
402 '$title' => $item['title'],
403 '$body' => smilies(bbcode($item['body'])),
404 '$ago' => relative_date($item['created']),
406 '$location' => $location,
407 '$indent' => $indent,
408 '$owner_url' => $owner_url,
409 '$owner_photo' => $owner_photo,
410 '$owner_name' => $owner_name,
411 '$plink' => get_plink($item),
412 '$edpost' => $edpost,
414 '$vote' => $likebuttons,
416 '$dislike' => $dislike,
417 '$comment' => $comment
420 $arr = array('item' => $item, 'output' => $tmp_item);
421 call_hooks('display_item', $arr);
423 $o .= $arr['output'];
429 // if author collapsing is in force but didn't get closed, close it off now.
431 if($blowhard_count >= 3)
440 if(! function_exists('extract_item_authors')) {
441 function extract_item_authors($arr,$uid) {
443 if((! $uid) || (! is_array($arr)) || (! count($arr)))
446 foreach($arr as $rr) {
447 if(! in_array("'" . dbesc($rr['author-link']) . "'",$urls))
448 $urls[] = "'" . dbesc($rr['author-link']) . "'";
451 // pre-quoted, don't put quotes on %s
453 $r = q("SELECT `id`,`network`,`url` FROM `contact` WHERE `uid` = %d AND `url` IN ( %s ) AND `self` = 0 AND `blocked` = 0 ",
461 if ($rr['network']=='dfrn')
462 $ret[$rr['url']] = $rr['id'];
463 $authors[$r['url']]= $rr;
465 $a->authors = $authors;
472 if(! function_exists('item_photo_menu')){
473 function item_photo_menu($item){
476 if (!isset($a->authors)){
477 $rr = q("SELECT `id`, `network`, `url` FROM `contact` WHERE `uid`=%d AND `self`=0 AND `blocked`=0 ", intval(local_user()));
479 foreach($rr as $r) $authors[$r['url']]= $r;
480 $a->authors = $authors;
488 $profile_link = ((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
489 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
491 if($profile_link === 'mailbox')
494 // $item['contact-uid'] is only set on profile page and indicates the uid of the user who owns the profile.
496 $profile_owner = ((x($item,'contact-uid')) && intval($item['contact-uid']) ? intval($item['contact-uid']) : 0);
498 // So we are checking that this is a logged in user on some page that *isn't* a profile page
499 // OR a profile page where the viewer owns the profile.
500 // Then check if we can use a sparkle (redirect) link to the profile by virtue of it being our contact
501 // or a friend's contact that we both have a connection to.
503 if((local_user() && ($profile_owner == 0))
504 || ($profile_owner && $profile_owner == local_user())) {
506 if(strlen($item['author-link']) && link_compare($item['author-link'],$item['url'])) {
507 $redir = $redirect_url;
510 elseif(isset($a->authors[$item['author-link']])) {
511 $redir = $a->get_baseurl() . '/redir/' . $a->authors[$item['author-link']]['id'];
512 $cid = $a->authors[$item['author-link']]['id'];
514 if($item['author-link'] === 'mailbox')
517 if((isset($cid)) && (! $item['self'])) {
518 $contact_url = $a->get_baseurl() . '/contacts/' . $cid;
519 if($item['network'] === 'dfrn') {
520 $status_link = $redir . "?url=status";
521 $profile_link = $redir . "?url=profile";
522 $photos_link = $redir . "?url=photos";
523 $pm_url = $a->get_baseurl() . '/message/new/' . $cid;
530 t("View status") => $status_link,
531 t("View profile") => $profile_link,
532 t("View photos") => $photos_link,
533 t("Edit contact") => $contact_url,
534 t("Send PM") => $pm_url,
538 $args = array($item, &$menu);
540 call_hooks('item_photo_menu', $args);
543 foreach($menu as $k=>$v){
544 if ($v!="") $o .= "<li><a href='$v'>$k</a></li>\n";
549 if(! function_exists('like_puller')) {
550 function like_puller($a,$item,&$arr,$mode) {
554 $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
556 if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
557 $url = $item['author-link'];
558 if((local_user()) && (local_user() == $item['uid']) && ($item['network'] === 'dfrn') && (! $item['self']) && (link_compare($item['author-link'],$item['url']))) {
559 $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
560 $sparkle = ' class="sparkle" ';
562 if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
563 $arr[$item['parent'] . '-l'] = array();
564 if(! isset($arr[$item['parent']]))
565 $arr[$item['parent']] = 1;
567 $arr[$item['parent']] ++;
568 $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
573 // Format the like/dislike text for a profile item
574 // $cnt = number of people who like/dislike the item
575 // $arr = array of pre-linked names of likers/dislikers
576 // $type = one of 'like, 'dislike'
578 // returns formatted text
580 if(! function_exists('format_like')) {
581 function format_like($cnt,$arr,$type,$id) {
584 $o .= (($type === 'like') ? sprintf( t('%s likes this.'), $arr[0]) : sprintf( t('%s doesn\'t like this.'), $arr[0])) . EOL ;
586 $spanatts = 'class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');"';
587 $o .= (($type === 'like') ?
588 sprintf( t('<span %1$s>%2$d people</span> like this.'), $spanatts, $cnt)
590 sprintf( t('<span %1$s>%2$d people</span> don\'t like this.'), $spanatts, $cnt) );
592 $total = count($arr);
593 if($total >= MAX_LIKERS)
594 $arr = array_slice($arr, 0, MAX_LIKERS - 1);
595 if($total < MAX_LIKERS)
596 $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
597 $str = implode(', ', $arr);
598 if($total >= MAX_LIKERS)
599 $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
600 $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str));
601 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';