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']);
89 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
91 if(strlen($item['author-link'])) {
92 if(link_compare($item['author-link'],$item['url']) && ($item['network'] === 'dfrn') && (! $item['self'])) {
93 $profile_link = $redirect_url;
94 $sparkle = ' sparkle';
96 elseif(isset($author_contacts[$item['author-link']])) {
97 $profile_link = $a->get_baseurl() . '/redir/' . $author_contacts[$item['author-link']];
98 $sparkle = ' sparkle';
102 $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
103 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
106 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
108 $location = '<span class="smalltext">' . $coord . '</span>';
114 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
117 $drop = replace_macros((($dropping)? $droptpl : $fakedrop), array('$id' => $item['id'], '$delete' => t('Delete')));
121 $drop = replace_macros($droptpl,array('$id' => $item['id']));
122 $lock = '<div class="wall-item-lock"></div>';
124 $o .= replace_macros($tpl,array(
125 '$id' => $item['item_id'],
126 '$linktitle' => sprintf( t('View %s\'s profile'), $profile_name),
127 '$profile_url' => $profile_link,
128 '$item_photo_menu' => item_photo_menu($item),
129 '$name' => $profile_name,
130 '$sparkle' => $sparkle,
132 '$thumb' => $profile_avatar,
133 '$title' => $item['title'],
134 '$body' => smilies(bbcode($item['body'])),
135 '$ago' => relative_date($item['created']),
136 '$location' => $location,
138 '$owner_url' => $owner_url,
139 '$owner_photo' => $owner_photo,
140 '$owner_name' => $owner_name,
142 '$conv' => '<a href="' . $a->get_baseurl() . '/display/' . $nickname . '/' . $item['id'] . '">' . t('View in context') . '</a>'
156 // Figure out how many comments each parent has
157 // (Comments all have gravity of 6)
158 // Store the result in the $comments array
161 foreach($items as $item) {
162 if(intval($item['gravity']) == 6) {
163 if(! x($comments,$item['parent']))
164 $comments[$item['parent']] = 1;
166 $comments[$item['parent']] += 1;
170 // map all the like/dislike activities for each parent item
171 // Store these in the $alike and $dlike arrays
173 foreach($items as $item) {
174 like_puller($a,$item,$alike,'like');
175 like_puller($a,$item,$dlike,'dislike');
178 $comments_collapsed = false;
182 foreach($items as $item) {
188 $owner_url = $owner_photo = $owner_name = '';
190 // We've already parsed out like/dislike for special treatment. We can ignore them now
192 if(((activity_match($item['verb'],ACTIVITY_LIKE))
193 || (activity_match($item['verb'],ACTIVITY_DISLIKE)))
194 && ($item['id'] != $item['parent']))
197 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
200 // Take care of author collapsing and comment collapsing
201 // If a single author has more than 3 consecutive top-level posts, squash the remaining ones.
202 // If there are more than two comments, squash all but the last 2.
206 $item_writeable = (($item['writable'] || $item['self']) ? true : false);
208 if($blowhard == $item['cid'] && (! $item['self']) && ($mode != 'profile')) {
210 if($blowhard_count == 3) {
211 $o .= '<div class="icollapse-wrapper fakelink" id="icollapse-wrapper-' . $item['parent']
212 . '" onclick="openClose(' . '\'icollapse-' . $item['parent'] . '\');" >'
213 . t('See more posts like this') . '</div>' . '<div class="icollapse" id="icollapse-'
214 . $item['parent'] . '" style="display: none;" >';
218 $blowhard = $item['cid'];
219 if($blowhard_count >= 3)
225 $comments_collapsed = false;
231 $show_comment_box = ((($page_writeable) && ($item_writeable) && ($comments_seen == $comments[$item['parent']])) ? true : false);
233 if(($comments[$item['parent']] > 2) && ($comments_seen <= ($comments[$item['parent']] - 2)) && ($item['gravity'] == 6)) {
234 if(! $comments_collapsed) {
235 $o .= '<div class="ccollapse-wrapper fakelink" id="ccollapse-wrapper-' . $item['parent']
236 . '" onclick="openClose(' . '\'ccollapse-' . $item['parent'] . '\');" >'
237 . sprintf( t('See all %d comments'), $comments[$item['parent']]) . '</div>'
238 . '<div class="ccollapse" id="ccollapse-' . $item['parent'] . '" style="display: none;" >';
239 $comments_collapsed = true;
242 if(($comments[$item['parent']] > 2) && ($comments_seen == ($comments[$item['parent']] - 1))) {
246 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
248 $lock = ((($item['private']) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
249 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
250 ? '<div class="wall-item-lock"><img src="images/lock_icon.gif" class="lockview" alt="' . t('Private Message') . '" onclick="lockview(event,' . $item['id'] . ');" /></div>'
251 : '<div class="wall-item-lock"></div>');
254 // Top-level wall post not written by the wall owner (wall-to-wall)
255 // First figure out who owns it.
259 if(($toplevelpost) && (! $item['self']) && ($mode !== 'profile')) {
261 if($item['type'] === 'wall') {
263 // On the network page, I am the owner. On the display page it will be the profile owner.
264 // This will have been stored in $a->page_contact by our calling page.
265 // Put this person on the left of the wall-to-wall notice.
267 $owner_url = $a->page_contact['url'];
268 $owner_photo = $a->page_contact['thumb'];
269 $owner_name = $a->page_contact['name'];
270 $template = $wallwall;
273 if(($item['type'] === 'remote') && (strlen($item['owner-link'])) && ($item['owner-link'] != $item['author-link'])) {
277 $owner_url = $item['owner-link'];
278 $owner_photo = $item['owner-avatar'];
279 $owner_name = $item['owner-name'];
280 $template = $wallwall;
282 // If it is our contact, use a friendly redirect link
283 if((link_compare($item['owner-link'],$item['url']))
284 && ($item['network'] === 'dfrn')) {
285 $owner_url = $redirect_url;
286 $osparkle = ' sparkle';
294 if($page_writeable) {
296 $likebuttons = replace_macros((($item['private']) ? $noshare_tpl : $like_tpl),array(
297 '$id' => $item['id'],
298 '$likethis' => t("I like this \x28toggle\x29"),
299 '$nolike' => t("I don't like this \x28toggle\x29"),
300 '$share' => t('Share'),
301 '$wait' => t('Please wait')
305 if(($show_comment_box) || (($show_comment_box == false) && ($item['last-child']))) {
306 $comment = replace_macros($cmnt_tpl,array(
307 '$return_path' => '',
308 '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
309 '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
310 '$id' => $item['item_id'],
311 '$parent' => $item['parent'],
312 '$profile_uid' => $profile_owner,
313 '$mylink' => $a->contact['url'],
314 '$mytitle' => t('This is you'),
315 '$myphoto' => $a->contact['thumb'],
316 '$comment' => t('Comment'),
317 '$submit' => t('Submit'),
318 '$ww' => (($mode === 'network') ? $commentww : '')
323 $edpost = ((($profile_owner == local_user()) && ($toplevelpost) && (intval($item['wall']) == 1))
324 ? '<a class="editpost" href="' . $a->get_baseurl() . '/editpost/' . $item['id']
325 . '" title="' . t('Edit') . '"><img src="images/pencil.gif" /></a>'
327 $drop = replace_macros(load_view_file('view/wall_item_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
329 $photo = $item['photo'];
330 $thumb = $item['thumb'];
332 // Post was remotely authored.
334 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
336 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
337 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $thumb);
339 if($mode === 'profile') {
340 if(local_user() && ($item['contact-uid'] == local_user()) && ($item['network'] === 'dfrn') && (! $item['self'] )) {
341 $profile_link = $redirect_url;
342 $sparkle = ' sparkle';
345 $profile_link = $item['url'];
349 elseif(strlen($item['author-link'])) {
350 $profile_link = $item['author-link'];
351 if(link_compare($item['author-link'],$item['url']) && ($item['network'] === 'dfrn') && (! $item['self'])) {
352 $profile_link = $redirect_url;
353 $sparkle = ' sparkle';
355 elseif(isset($author_contacts[$item['author-link']])) {
356 $profile_link = $a->get_baseurl() . '/redir/' . $author_contacts[$item['author-link']];
357 $sparkle = ' sparkle';
361 $profile_link = $item['url'];
363 $like = ((x($alike,$item['id'])) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
364 $dislike = ((x($dlike,$item['id'])) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
366 $location = (($item['location']) ? '<a target="map" title="' . $item['location']
367 . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
368 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord']
369 . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
372 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
374 $location = '<span class="smalltext">' . $coord . '</span>';
377 $indent = (($toplevelpost) ? '' : ' comment');
379 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
384 $tmp_item = replace_macros($template,array(
385 '$id' => $item['item_id'],
386 '$linktitle' => sprintf( t('View %s\'s profile'), $profile_name),
387 '$olinktitle' => sprintf( t('View %s\'s profile'), $owner_name),
389 '$wall' => t('Wall-to-Wall'),
390 '$vwall' => t('via Wall-To-Wall:'),
391 '$profile_url' => $profile_link,
392 '$item_photo_menu' => item_photo_menu($item),
393 '$name' => $profile_name,
394 '$thumb' => $profile_avatar,
395 '$osparkle' => $osparkle,
396 '$sparkle' => $sparkle,
397 '$title' => $item['title'],
398 '$body' => smilies(bbcode($item['body'])),
399 '$ago' => relative_date($item['created']),
401 '$location' => $location,
402 '$indent' => $indent,
403 '$owner_url' => $owner_url,
404 '$owner_photo' => $owner_photo,
405 '$owner_name' => $owner_name,
406 '$plink' => get_plink($item),
407 '$edpost' => $edpost,
409 '$vote' => $likebuttons,
411 '$dislike' => $dislike,
412 '$comment' => $comment
415 $arr = array('item' => $item, 'output' => $tmp_item);
416 call_hooks('display_item', $arr);
418 $o .= $arr['output'];
424 // if author collapsing is in force but didn't get closed, close it off now.
426 if($blowhard_count >= 3)
435 if(! function_exists('extract_item_authors')) {
436 function extract_item_authors($arr,$uid) {
438 if((! $uid) || (! is_array($arr)) || (! count($arr)))
441 foreach($arr as $rr) {
442 if(! in_array("'" . dbesc($rr['author-link']) . "'",$urls))
443 $urls[] = "'" . dbesc($rr['author-link']) . "'";
446 // pre-quoted, don't put quotes on %s
448 $r = q("SELECT `id`,`network`,`url` FROM `contact` WHERE `uid` = %d AND `url` IN ( %s ) AND `self` = 0 AND `blocked` = 0 ",
456 if ($rr['network']=='dfrn')
457 $ret[$rr['url']] = $rr['id'];
458 $authors[$r['url']]= $rr;
460 $a->authors = $authors;
467 if(! function_exists('item_photo_menu')){
468 function item_photo_menu($item){
471 if (!isset($a->authors)){
472 $rr = q("SELECT `id`, `network`, `url` FROM `contact` WHERE `uid`=%d AND `self`=0 AND `blocked`=0 ", intval(local_user()));
474 foreach($rr as $r) $authors[$r['url']]= $r;
475 $a->authors = $authors;
483 $profile_link = ((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
484 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
486 // $item['contact-uid'] is only set on profile page and indicates the uid of the user who owns the profile.
488 $profile_owner = ((x($item,'contact-uid')) && intval($item['contact-uid']) ? intval($item['contact-uid']) : 0);
490 // So we are checking that this is a logged in user on some page that *isn't* a profile page
491 // OR a profile page where the viewer owns the profile.
492 // Then check if we can use a sparkle (redirect) link to the profile by virtue of it being our contact
493 // or a friend's contact that we both have a connection to.
495 if((local_user() && ($profile_owner == 0))
496 || ($profile_owner && $profile_owner == local_user())) {
498 if(strlen($item['author-link']) && link_compare($item['author-link'],$item['url'])) {
499 $redir = $redirect_url;
502 elseif(isset($a->authors[$item['author-link']])) {
503 $redir = $a->get_baseurl() . '/redir/' . $a->authors[$item['author-link']]['id'];
504 $cid = $a->authors[$item['author-link']]['id'];
507 if((isset($cid)) && (! $item['self'])) {
508 $contact_url = $a->get_baseurl() . '/contacts/' . $cid;
509 if($item['network'] === 'dfrn') {
510 $status_link = $redir . "?url=status";
511 $profile_link = $redir . "?url=profile";
512 $photos_link = $redir . "?url=photos";
513 $pm_url = $a->get_baseurl() . '/message/new/' . $cid;
520 t("View status") => $status_link,
521 t("View profile") => $profile_link,
522 t("View photos") => $photos_link,
523 t("Edit contact") => $contact_url,
524 t("Send PM") => $pm_url,
528 $args = array($item, &$menu);
530 call_hooks('item_photo_menu', $args);
533 foreach($menu as $k=>$v){
534 if ($v!="") $o .= "<li><a href='$v'>$k</a></li>\n";
539 if(! function_exists('like_puller')) {
540 function like_puller($a,$item,&$arr,$mode) {
544 $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
546 if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
547 $url = $item['author-link'];
548 if((local_user()) && (local_user() == $item['uid']) && ($item['network'] === 'dfrn') && (! $item['self']) && (link_compare($item['author-link'],$item['url']))) {
549 $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
550 $sparkle = ' class="sparkle" ';
552 if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
553 $arr[$item['parent'] . '-l'] = array();
554 if(! isset($arr[$item['parent']]))
555 $arr[$item['parent']] = 1;
557 $arr[$item['parent']] ++;
558 $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
563 // Format the like/dislike text for a profile item
564 // $cnt = number of people who like/dislike the item
565 // $arr = array of pre-linked names of likers/dislikers
566 // $type = one of 'like, 'dislike'
568 // returns formatted text
570 if(! function_exists('format_like')) {
571 function format_like($cnt,$arr,$type,$id) {
574 $o .= (($type === 'like') ? sprintf( t('%s likes this.'), $arr[0]) : sprintf( t('%s doesn\'t like this.'), $arr[0])) . EOL ;
576 $spanatts = 'class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');"';
577 $o .= (($type === 'like') ?
578 sprintf( t('<span %1$s>%2$d people</span> like this.'), $spanatts, $cnt)
580 sprintf( t('<span %1$s>%2$d people</span> don\'t like this.'), $spanatts, $cnt) );
582 $total = count($arr);
583 if($total >= MAX_LIKERS)
584 $arr = array_slice($arr, 0, MAX_LIKERS - 1);
585 if($total < MAX_LIKERS)
586 $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
587 $str = implode(', ', $arr);
588 if($total >= MAX_LIKERS)
589 $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
590 $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str));
591 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';