4 * Render actions localized
6 function localize_item(&$item){
8 if ($item['verb']=="http://activitystrea.ms/schema/1.0/like" ||
9 $item['verb']=="http://activitystrea.ms/schema/1.0/dislike"){
11 $r = q("SELECT * from `item`,`contact` WHERE
12 `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
13 dbesc($item['parent-uri']));
14 if(count($r)==0) return;
17 $author = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
18 $objauthor = '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
20 $post_type = (($obj['resource-id']) ? t('photo') : t('status'));
21 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
23 switch($item['verb']){
24 case "http://activitystrea.ms/schema/1.0/like":
25 $bodyverb = t('%1$s likes %2$s\'s %3$s');
27 case "http://activitystrea.ms/schema/1.0/dislike":
28 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
31 $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
34 if ($item['verb']=='http://activitystrea.ms/schema/1.0/make-friend'){
36 $b = str_replace("[/url]","[/url]\n", $item['body']);
37 preg_match_all("|(\[url.*\[/url\])|", $b, $match);
39 $item['body'] = $match[0][0]." "
40 .t('is now friends with')
41 ." ".$match[0][1]."\n\n\n"
49 * "Render" a conversation or list of items for HTML display.
50 * There are two major forms of display:
51 * - Sequential or unthreaded ("New Item View" or search results)
53 * The $mode parameter decides between the various renderings and also
54 * figures out how to determine page owner and other contextual items
55 * that are based on unique features of the calling module.
58 function conversation(&$a, $items, $mode, $update) {
60 require_once('bbcode.php');
63 $page_writeable = false;
65 if($mode === 'network') {
66 $profile_owner = local_user();
67 $page_writeable = true;
70 if($mode === 'profile') {
71 $profile_owner = $a->profile['profile_uid'];
72 $page_writeable = can_write_wall($a,$profile_owner);
75 if($mode === 'display') {
76 $profile_owner = $a->profile['uid'];
77 $page_writeable = can_write_wall($a,$profile_owner);
81 $return_url = $_SESSION['return_url'];
83 $return_url = $_SESSION['return_url'] = $a->cmd;
86 // find all the authors involved in remote conversations
87 // We will use a local profile photo if they are one of our contacts
88 // otherwise we have to get the photo from the item owner's site
90 $author_contacts = extract_item_authors($items,local_user());
93 $cmnt_tpl = load_view_file('view/comment_item.tpl');
94 $like_tpl = load_view_file('view/like.tpl');
95 $noshare_tpl = load_view_file('view/like_noshare.tpl');
96 $tpl = load_view_file('view/wall_item.tpl');
97 $wallwall = load_view_file('view/wallwall_item.tpl');
104 if($mode === 'network-new' || $mode === 'search') {
106 // "New Item View" on network page or search page results
107 // - just loop through the items and format them minimally for display
109 $tpl = load_view_file('view/search_item.tpl');
110 $droptpl = load_view_file('view/wall_fake_drop.tpl');
112 foreach($items as $item) {
120 if($mode === 'search') {
121 if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE)))
122 && ($item['id'] != $item['parent']))
124 $nickname = $item['nickname'];
127 $nickname = $a->user['nickname'];
129 $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
130 $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
131 $profile_link = ((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
132 if($profile_link === 'mailbox')
135 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
137 if(strlen($item['author-link'])) {
138 if(link_compare($item['author-link'],$item['url']) && ($item['network'] === 'dfrn') && (! $item['self'])) {
139 $profile_link = $redirect_url;
140 $sparkle = ' sparkle';
142 elseif(isset($author_contacts[$item['author-link']])) {
143 $profile_link = $a->get_baseurl() . '/redir/' . $author_contacts[$item['author-link']];
144 $sparkle = ' sparkle';
148 $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
149 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
152 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
154 $location = '<span class="smalltext">' . $coord . '</span>';
160 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
163 $drop = replace_macros((($dropping)? $droptpl : $fakedrop), array('$id' => $item['id'], '$delete' => t('Delete')));
166 localize_item($item);
168 $drop = replace_macros($droptpl,array('$id' => $item['id']));
169 $lock = '<div class="wall-item-lock"></div>';
171 $o .= replace_macros($tpl,array(
172 '$id' => $item['item_id'],
173 '$linktitle' => sprintf( t('View %s\'s profile'), $profile_name),
174 '$profile_url' => $profile_link,
175 '$item_photo_menu' => item_photo_menu($item),
176 '$name' => $profile_name,
177 '$sparkle' => $sparkle,
179 '$thumb' => $profile_avatar,
180 '$title' => $item['title'],
181 '$body' => smilies(bbcode($item['body'])),
182 '$ago' => relative_date($item['created']),
183 '$location' => $location,
185 '$owner_url' => $owner_url,
186 '$owner_photo' => $owner_photo,
187 '$owner_name' => $owner_name,
189 '$conv' => '<a href="' . $a->get_baseurl() . '/display/' . $nickname . '/' . $item['id'] . '">' . t('View in context') . '</a>'
203 // Figure out how many comments each parent has
204 // (Comments all have gravity of 6)
205 // Store the result in the $comments array
208 foreach($items as $item) {
209 if(intval($item['gravity']) == 6) {
210 if(! x($comments,$item['parent']))
211 $comments[$item['parent']] = 1;
213 $comments[$item['parent']] += 1;
217 // map all the like/dislike activities for each parent item
218 // Store these in the $alike and $dlike arrays
220 foreach($items as $item) {
221 like_puller($a,$item,$alike,'like');
222 like_puller($a,$item,$dlike,'dislike');
225 $comments_collapsed = false;
229 foreach($items as $item) {
235 $owner_url = $owner_photo = $owner_name = '';
237 // We've already parsed out like/dislike for special treatment. We can ignore them now
239 if(((activity_match($item['verb'],ACTIVITY_LIKE))
240 || (activity_match($item['verb'],ACTIVITY_DISLIKE)))
241 && ($item['id'] != $item['parent']))
244 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
247 // Take care of author collapsing and comment collapsing
248 // If a single author has more than 3 consecutive top-level posts, squash the remaining ones.
249 // If there are more than two comments, squash all but the last 2.
253 $item_writeable = (($item['writable'] || $item['self']) ? true : false);
255 if($blowhard == $item['cid'] && (! $item['self']) && ($mode != 'profile')) {
257 if($blowhard_count == 3) {
258 $o .= '<div class="icollapse-wrapper fakelink" id="icollapse-wrapper-' . $item['parent']
259 . '" onclick="openClose(' . '\'icollapse-' . $item['parent'] . '\');" >'
260 . t('See more posts like this') . '</div>' . '<div class="icollapse" id="icollapse-'
261 . $item['parent'] . '" style="display: none;" >';
265 $blowhard = $item['cid'];
266 if($blowhard_count >= 3)
272 $comments_collapsed = false;
278 $show_comment_box = ((($page_writeable) && ($item_writeable) && ($comments_seen == $comments[$item['parent']])) ? true : false);
280 if(($comments[$item['parent']] > 2) && ($comments_seen <= ($comments[$item['parent']] - 2)) && ($item['gravity'] == 6)) {
281 if(! $comments_collapsed) {
282 $o .= '<div class="ccollapse-wrapper fakelink" id="ccollapse-wrapper-' . $item['parent']
283 . '" onclick="openClose(' . '\'ccollapse-' . $item['parent'] . '\');" >'
284 . sprintf( t('See all %d comments'), $comments[$item['parent']]) . '</div>'
285 . '<div class="ccollapse" id="ccollapse-' . $item['parent'] . '" style="display: none;" >';
286 $comments_collapsed = true;
289 if(($comments[$item['parent']] > 2) && ($comments_seen == ($comments[$item['parent']] - 1))) {
293 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
295 $lock = ((($item['private']) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
296 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
297 ? '<div class="wall-item-lock"><img src="images/lock_icon.gif" class="lockview" alt="' . t('Private Message') . '" onclick="lockview(event,' . $item['id'] . ');" /></div>'
298 : '<div class="wall-item-lock"></div>');
301 // Top-level wall post not written by the wall owner (wall-to-wall)
302 // First figure out who owns it.
306 if(($toplevelpost) && (! $item['self']) && ($mode !== 'profile')) {
308 if($item['type'] === 'wall') {
310 // On the network page, I am the owner. On the display page it will be the profile owner.
311 // This will have been stored in $a->page_contact by our calling page.
312 // Put this person on the left of the wall-to-wall notice.
314 $owner_url = $a->page_contact['url'];
315 $owner_photo = $a->page_contact['thumb'];
316 $owner_name = $a->page_contact['name'];
317 $template = $wallwall;
320 if(($item['type'] === 'remote') && (strlen($item['owner-link'])) && ($item['owner-link'] != $item['author-link'])) {
324 $owner_url = $item['owner-link'];
325 $owner_photo = $item['owner-avatar'];
326 $owner_name = $item['owner-name'];
327 $template = $wallwall;
329 // If it is our contact, use a friendly redirect link
330 if((link_compare($item['owner-link'],$item['url']))
331 && ($item['network'] === 'dfrn')) {
332 $owner_url = $redirect_url;
333 $osparkle = ' sparkle';
341 if($page_writeable) {
343 $likebuttons = replace_macros((($item['private']) ? $noshare_tpl : $like_tpl),array(
344 '$id' => $item['id'],
345 '$likethis' => t("I like this \x28toggle\x29"),
346 '$nolike' => t("I don't like this \x28toggle\x29"),
347 '$share' => t('Share'),
348 '$wait' => t('Please wait')
352 if(($show_comment_box) || (($show_comment_box == false) && ($item['last-child']))) {
353 $comment = replace_macros($cmnt_tpl,array(
354 '$return_path' => '',
355 '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
356 '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
357 '$id' => $item['item_id'],
358 '$parent' => $item['parent'],
359 '$profile_uid' => $profile_owner,
360 '$mylink' => $a->contact['url'],
361 '$mytitle' => t('This is you'),
362 '$myphoto' => $a->contact['thumb'],
363 '$comment' => t('Comment'),
364 '$submit' => t('Submit'),
365 '$ww' => (($mode === 'network') ? $commentww : '')
370 $edpost = ((($profile_owner == local_user()) && ($toplevelpost) && (intval($item['wall']) == 1))
371 ? '<a class="editpost" href="' . $a->get_baseurl() . '/editpost/' . $item['id']
372 . '" title="' . t('Edit') . '"><img src="images/pencil.gif" /></a>'
374 $drop = replace_macros(load_view_file('view/wall_item_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
376 $photo = $item['photo'];
377 $thumb = $item['thumb'];
379 // Post was remotely authored.
381 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
383 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
384 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $thumb);
386 if($mode === 'profile') {
387 if(local_user() && ($item['contact-uid'] == local_user()) && ($item['network'] === 'dfrn') && (! $item['self'] )) {
388 $profile_link = $redirect_url;
389 $sparkle = ' sparkle';
392 $profile_link = $item['url'];
396 elseif(strlen($item['author-link'])) {
397 $profile_link = $item['author-link'];
398 if(link_compare($item['author-link'],$item['url']) && ($item['network'] === 'dfrn') && (! $item['self'])) {
399 $profile_link = $redirect_url;
400 $sparkle = ' sparkle';
402 elseif(isset($author_contacts[$item['author-link']])) {
403 $profile_link = $a->get_baseurl() . '/redir/' . $author_contacts[$item['author-link']];
404 $sparkle = ' sparkle';
408 $profile_link = $item['url'];
410 if($profile_link === 'mailbox')
413 $like = ((x($alike,$item['id'])) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
414 $dislike = ((x($dlike,$item['id'])) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
416 $location = (($item['location']) ? '<a target="map" title="' . $item['location']
417 . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
418 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord']
419 . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
422 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
424 $location = '<span class="smalltext">' . $coord . '</span>';
427 $indent = (($toplevelpost) ? '' : ' comment');
429 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
433 localize_item($item);
437 $tmp_item = replace_macros($template,array(
438 '$id' => $item['item_id'],
439 '$linktitle' => sprintf( t('View %s\'s profile'), $profile_name),
440 '$olinktitle' => sprintf( t('View %s\'s profile'), $owner_name),
442 '$wall' => t('Wall-to-Wall'),
443 '$vwall' => t('via Wall-To-Wall:'),
444 '$profile_url' => $profile_link,
445 '$item_photo_menu' => item_photo_menu($item),
446 '$name' => $profile_name,
447 '$thumb' => $profile_avatar,
448 '$osparkle' => $osparkle,
449 '$sparkle' => $sparkle,
450 '$title' => $item['title'],
451 '$body' => smilies(bbcode($item['body'])),
452 '$ago' => relative_date($item['created']),
454 '$location' => $location,
455 '$indent' => $indent,
456 '$owner_url' => $owner_url,
457 '$owner_photo' => $owner_photo,
458 '$owner_name' => $owner_name,
459 '$plink' => get_plink($item),
460 '$edpost' => $edpost,
462 '$vote' => $likebuttons,
464 '$dislike' => $dislike,
465 '$comment' => $comment
468 $arr = array('item' => $item, 'output' => $tmp_item);
469 call_hooks('display_item', $arr);
471 $o .= $arr['output'];
477 // if author collapsing is in force but didn't get closed, close it off now.
479 if($blowhard_count >= 3)
488 if(! function_exists('extract_item_authors')) {
489 function extract_item_authors($arr,$uid) {
491 if((! $uid) || (! is_array($arr)) || (! count($arr)))
494 foreach($arr as $rr) {
495 if(! in_array("'" . dbesc($rr['author-link']) . "'",$urls))
496 $urls[] = "'" . dbesc($rr['author-link']) . "'";
499 // pre-quoted, don't put quotes on %s
501 $r = q("SELECT `id`,`network`,`url` FROM `contact` WHERE `uid` = %d AND `url` IN ( %s ) AND `self` = 0 AND `blocked` = 0 ",
509 if ($rr['network']=='dfrn')
510 $ret[$rr['url']] = $rr['id'];
511 $authors[$r['url']]= $rr;
513 $a->authors = $authors;
520 if(! function_exists('item_photo_menu')){
521 function item_photo_menu($item){
524 if (!isset($a->authors)){
525 $rr = q("SELECT `id`, `network`, `url` FROM `contact` WHERE `uid`=%d AND `self`=0 AND `blocked`=0 ", intval(local_user()));
527 foreach($rr as $r) $authors[$r['url']]= $r;
528 $a->authors = $authors;
537 $profile_link = ((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
538 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
540 if($profile_link === 'mailbox')
543 // $item['contact-uid'] is only set on profile page and indicates the uid of the user who owns the profile.
545 $profile_owner = ((x($item,'contact-uid')) && intval($item['contact-uid']) ? intval($item['contact-uid']) : 0);
547 // So we are checking that this is a logged in user on some page that *isn't* a profile page
548 // OR a profile page where the viewer owns the profile.
549 // Then check if we can use a sparkle (redirect) link to the profile by virtue of it being our contact
550 // or a friend's contact that we both have a connection to.
552 if((local_user() && ($profile_owner == 0))
553 || ($profile_owner && $profile_owner == local_user())) {
555 if(strlen($item['author-link']) && link_compare($item['author-link'],$item['url'])) {
556 $redir = $redirect_url;
559 elseif(isset($a->authors[$item['author-link']])) {
560 $redir = $a->get_baseurl() . '/redir/' . $a->authors[$item['author-link']]['id'];
561 $cid = $a->authors[$item['author-link']]['id'];
563 if($item['author-link'] === 'mailbox')
566 if((isset($cid)) && (! $item['self'])) {
567 $contact_url = $a->get_baseurl() . '/contacts/' . $cid;
568 $posts_link = $a->get_baseurl() . '/network/?cid=' . $cid;
569 if($item['network'] === 'dfrn') {
570 $status_link = $redir . "?url=status";
571 $profile_link = $redir . "?url=profile";
572 $photos_link = $redir . "?url=photos";
573 $pm_url = $a->get_baseurl() . '/message/new/' . $cid;
580 t("View status") => $status_link,
581 t("View profile") => $profile_link,
582 t("View photos") => $photos_link,
583 t("View recent") => $posts_link,
584 t("Edit contact") => $contact_url,
585 t("Send PM") => $pm_url,
589 $args = array($item, &$menu);
591 call_hooks('item_photo_menu', $args);
594 foreach($menu as $k=>$v){
595 if ($v!="") $o .= "<li><a href='$v'>$k</a></li>\n";
600 if(! function_exists('like_puller')) {
601 function like_puller($a,$item,&$arr,$mode) {
605 $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
607 if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
608 $url = $item['author-link'];
609 if((local_user()) && (local_user() == $item['uid']) && ($item['network'] === 'dfrn') && (! $item['self']) && (link_compare($item['author-link'],$item['url']))) {
610 $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
611 $sparkle = ' class="sparkle" ';
613 if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
614 $arr[$item['parent'] . '-l'] = array();
615 if(! isset($arr[$item['parent']]))
616 $arr[$item['parent']] = 1;
618 $arr[$item['parent']] ++;
619 $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
624 // Format the like/dislike text for a profile item
625 // $cnt = number of people who like/dislike the item
626 // $arr = array of pre-linked names of likers/dislikers
627 // $type = one of 'like, 'dislike'
629 // returns formatted text
631 if(! function_exists('format_like')) {
632 function format_like($cnt,$arr,$type,$id) {
635 $o .= (($type === 'like') ? sprintf( t('%s likes this.'), $arr[0]) : sprintf( t('%s doesn\'t like this.'), $arr[0])) . EOL ;
637 $spanatts = 'class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');"';
638 $o .= (($type === 'like') ?
639 sprintf( t('<span %1$s>%2$d people</span> like this.'), $spanatts, $cnt)
641 sprintf( t('<span %1$s>%2$d people</span> don\'t like this.'), $spanatts, $cnt) );
643 $total = count($arr);
644 if($total >= MAX_LIKERS)
645 $arr = array_slice($arr, 0, MAX_LIKERS - 1);
646 if($total < MAX_LIKERS)
647 $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
648 $str = implode(', ', $arr);
649 if($total >= MAX_LIKERS)
650 $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
651 $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str));
652 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';