]> git.mxchange.org Git - friendica.git/blob - include/conversation.php
Merge https://github.com/friendica/friendica into pull
[friendica.git] / include / conversation.php
1 <?php
2
3 /**
4  * Render actions localized
5  */
6 function localize_item(&$item){
7         $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
8         if ($item['verb']=== ACTIVITY_LIKE || $item['verb']=== ACTIVITY_DISLIKE){
9
10                 $r = q("SELECT * from `item`,`contact` WHERE 
11                                 `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
12                                  dbesc($item['parent-uri']));
13                 if(count($r)==0) return;
14                 $obj=$r[0];
15                 
16                 $author  = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
17                 $objauthor =  '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
18                 
19                 switch($obj['verb']){
20                         case ACTIVITY_POST:
21                                 switch ($obj['object-type']){
22                                         case ACTIVITY_OBJ_EVENT:
23                                                 $post_type = t('event');
24                                                 break;
25                                         default:
26                                                 $post_type = t('status');
27                                 }
28                                 break;
29                         default:
30                                 if($obj['resource-id']){
31                                         $post_type = t('photo');
32                                         $m=array();     preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
33                                         $rr['plink'] = $m[1];
34                                 } else {
35                                         $post_type = t('status');
36                                 }
37                 }
38         
39                 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
40                 
41                 switch($item['verb']){
42                         case ACTIVITY_LIKE :
43                                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
44                                 break;
45                         case ACTIVITY_DISLIKE:
46                                 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
47                                 break;
48                 }
49                 $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
50                         
51         }
52         if ($item['verb']=== ACTIVITY_FRIEND){
53
54                 if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
55
56                 $Aname = $item['author-name'];
57                 $Alink = $item['author-link'];
58                 
59                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
60                 
61                 $obj = parse_xml_string($xmlhead.$item['object']);
62                 $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
63                 
64                 $Bname = $obj->title;
65                 $Blink = ""; $Bphoto = "";
66                 foreach ($links->link as $l){
67                         $atts = $l->attributes();
68                         switch($atts['rel']){
69                                 case "alternate": $Blink = $atts['href'];
70                                 case "photo": $Bphoto = $atts['href'];
71                         }
72                         
73                 }
74                 
75                 $A = '[url=' . $Alink . ']' . $Aname . '[/url]';
76                 $B = '[url=' . $Blink . ']' . $Bname . '[/url]';
77                 if ($Bphoto!="") $Bphoto = '[url=' . $Blink . '][img]' . $Bphoto . '[/img][/url]';
78
79                 $item['body'] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
80
81         }
82     if ($item['verb']===ACTIVITY_TAG){
83                 $r = q("SELECT * from `item`,`contact` WHERE 
84                 `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
85                  dbesc($item['parent-uri']));
86                 if(count($r)==0) return;
87                 $obj=$r[0];
88                 
89                 $author  = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
90                 $objauthor =  '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
91                 
92                 switch($obj['verb']){
93                         case ACTIVITY_POST:
94                                 switch ($obj['object-type']){
95                                         case ACTIVITY_OBJ_EVENT:
96                                                 $post_type = t('event');
97                                                 break;
98                                         default:
99                                                 $post_type = t('status');
100                                 }
101                                 break;
102                         default:
103                                 if($obj['resource-id']){
104                                         $post_type = t('photo');
105                                         $m=array();     preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
106                                         $rr['plink'] = $m[1];
107                                 } else {
108                                         $post_type = t('status');
109                                 }
110                 }
111                 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
112                 
113                 $parsedobj = parse_xml_string($xmlhead.$item['object']);
114                 
115                 $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->link, $parsedobj->content);
116                 $item['body'] = sprintf( t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag );
117                 
118                 
119         }
120
121 }
122
123 /**
124  * "Render" a conversation or list of items for HTML display.
125  * There are two major forms of display:
126  *      - Sequential or unthreaded ("New Item View" or search results)
127  *      - conversation view
128  * The $mode parameter decides between the various renderings and also
129  * figures out how to determine page owner and other contextual items 
130  * that are based on unique features of the calling module.
131  *
132  */
133 function conversation(&$a, $items, $mode, $update) {
134
135         require_once('bbcode.php');
136
137         $profile_owner = 0;
138         $page_writeable      = false;
139
140         if($mode === 'network') {
141                 $profile_owner = local_user();
142                 $page_writeable = true;
143         }
144
145         if($mode === 'profile') {
146                 $profile_owner = $a->profile['profile_uid'];
147                 $page_writeable = can_write_wall($a,$profile_owner);
148         }
149
150         if($mode === 'notes') {
151                 $profile_owner = local_user();
152                 $page_writeable = true;
153         }
154
155         if($mode === 'display') {
156                 $profile_owner = $a->profile['uid'];
157                 $page_writeable = can_write_wall($a,$profile_owner);
158         }
159
160         if($mode === 'community') {
161                 $profile_owner = 0;
162                 $page_writeable = false;
163         }
164
165         if($update)
166                 $return_url = $_SESSION['return_url'];
167         else
168                 $return_url = $_SESSION['return_url'] = $a->cmd;
169
170         load_contact_links(local_user());
171
172
173         $cmnt_tpl    = get_markup_template('comment_item.tpl');
174         $tpl         = get_markup_template('wall_item.tpl');
175         $wallwall    = get_markup_template('wallwall_item.tpl');
176
177         $alike = array();
178         $dlike = array();
179         
180         
181         // array with html for each thread (parent+comments)
182         $threads = array();
183         $threadsid = -1;
184         
185         if(count($items)) {
186
187                 if($mode === 'network-new' || $mode === 'search' || $mode === 'community') {
188
189                         // "New Item View" on network page or search page results 
190                         // - just loop through the items and format them minimally for display
191
192                         $tpl = get_markup_template('search_item.tpl');
193
194                         foreach($items as $item) {
195                                 $threadsid++;
196
197                                 $comment     = '';
198                                 $owner_url   = '';
199                                 $owner_photo = '';
200                                 $owner_name  = '';
201                                 $sparkle     = '';
202
203                                 if($mode === 'search' || $mode === 'community') {
204                                         if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
205                                                 && ($item['id'] != $item['parent']))
206                                                 continue;
207                                         $nickname = $item['nickname'];
208                                 }
209                                 else
210                                         $nickname = $a->user['nickname'];
211                         
212                                 $profile_name   = ((strlen($item['author-name']))   ? $item['author-name']   : $item['name']);
213
214                                 $sp = false;
215                                 $profile_link = best_link_url($item,$sp);
216                                 if($sp)
217                                         $sparkle = ' sparkle';
218                                 if($profile_link === 'mailbox')
219                                         $profile_link = '';
220
221
222                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
223                                 if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
224                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
225                                 else
226                                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
227
228                                 $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
229                                 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
230                                 if($coord) {
231                                         if($location)
232                                                 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
233                                         else
234                                                 $location = '<span class="smalltext">' . $coord . '</span>';
235                                 }
236
237
238                                 localize_item($item);
239                                 if($mode === 'network-new')
240                                         $dropping = true;
241                                 else
242                                         $dropping = false;
243
244
245                                 $drop = array(
246                                         'dropping' => $dropping,
247                                         'select' => t('Select'), 
248                                         'delete' => t('Delete'),
249                                 );
250
251                                 $star = false;
252                                 $isstarred = "unstarred";
253                                 
254                                 $lock = false;
255                                 $likebuttons = false;
256                                 $shareable = false;
257
258                                 $body = prepare_body($item,true);
259                                 
260                                 $tmp_item = replace_macros($tpl,array(
261                                         '$id' => $item['item_id'],
262                                         '$linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
263                                         '$profile_url' => $profile_link,
264                                         '$item_photo_menu' => item_photo_menu($item),
265                                         '$name' => template_escape($profile_name),
266                                         '$sparkle' => $sparkle,
267                                         '$lock' => $lock,
268                                         '$thumb' => $profile_avatar,
269                                         '$title' => template_escape($item['title']),
270                                         '$body' => template_escape($body),
271                                         '$ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
272                                         '$lock' => $lock,
273                                         '$location' => template_escape($location),
274                                         '$indent' => '',
275                                         '$owner_name' => template_escape($owner_name),
276                                         '$owner_url' => $owner_url,
277                                         '$owner_photo' => $owner_photo,
278                                         '$plink' => get_plink($item),
279                                         '$edpost' => false,
280                                         '$isstarred' => $isstarred,
281                                         '$star' => $star,
282                                         '$drop' => $drop,
283                                         '$vote' => $likebuttons,
284                                         '$like' => '',
285                                         '$dislike' => '',
286                                         '$comment' => '',
287                                         '$conv' => array('href'=> $a->get_baseurl() . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context')),
288                                         '$wait' => t('Please wait'),
289                                 ));
290
291                                 $arr = array('item' => $item, 'output' => $tmp_item);
292                                 call_hooks('display_item', $arr);
293
294                                 $threads[$threadsid] .= $arr['output'];
295
296                         }
297
298                 }
299                 else
300                 {
301                         // Normal View
302
303
304                         // Figure out how many comments each parent has
305                         // (Comments all have gravity of 6)
306                         // Store the result in the $comments array
307
308                         $comments = array();
309                         foreach($items as $item) {
310                                 if((intval($item['gravity']) == 6) && ($item['id'] != $item['parent'])) {
311                                         if(! x($comments,$item['parent']))
312                                                 $comments[$item['parent']] = 1;
313                                         else
314                                                 $comments[$item['parent']] += 1;
315                                 }
316                         }
317
318                         // map all the like/dislike activities for each parent item 
319                         // Store these in the $alike and $dlike arrays
320
321                         foreach($items as $item) {
322                                 like_puller($a,$item,$alike,'like');
323                                 like_puller($a,$item,$dlike,'dislike');
324                         }
325
326                         $comments_collapsed = false;
327                         $blowhard = 0;
328                         $blowhard_count = 0;
329
330
331                         foreach($items as $item) {
332
333                                 $comment = '';
334                                 $template = $tpl;
335                                 $commentww = '';
336                                 $sparkle = '';
337                                 $owner_url = $owner_photo = $owner_name = '';
338
339                                 // We've already parsed out like/dislike for special treatment. We can ignore them now
340
341                                 if(((activity_match($item['verb'],ACTIVITY_LIKE)) 
342                                         || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
343                                         && ($item['id'] != $item['parent']))
344                                         continue;
345
346                                 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
347                                 $toplevelprivate = false;
348
349                                 // Take care of author collapsing and comment collapsing
350                                 // If a single author has more than 3 consecutive top-level posts, squash the remaining ones.
351                                 // If there are more than two comments, squash all but the last 2.
352                         
353                                 if($toplevelpost) {
354                                         $toplevelprivate = (($toplevelpost && $item['private']) ? true : false);
355                                         $item_writeable = (($item['writable'] || $item['self']) ? true : false);
356
357                                         /*if($blowhard == $item['cid'] && (! $item['self']) && ($mode != 'profile') && ($mode != 'notes')) {
358                                                 $blowhard_count ++;
359                                                 if($blowhard_count == 3) {
360                                                         $o .= '<div class="icollapse-wrapper fakelink" id="icollapse-wrapper-' . $item['parent'] 
361                                                                 . '" onclick="openClose(' . '\'icollapse-' . $item['parent'] . '\'); $(\'#icollapse-wrapper-' . $item['parent'] . '\').hide();" >' 
362                                                                 . t('See more posts like this') . '</div>' . '<div class="icollapse" id="icollapse-' 
363                                                                 . $item['parent'] . '" style="display: none;" >';
364                                                 }
365                                         }
366                                         else {
367                                                 $blowhard = $item['cid'];                                       
368                                                 if($blowhard_count >= 3)
369                                                         $o .= '</div>';
370                                                 $blowhard_count = 0;
371                                         }*/
372
373                                         $comments_seen = 0;
374                                         $comments_collapsed = false;
375                                         
376                                         $threadsid++;
377                                         $threads[$threadsid] = "";
378                                 }
379                                 else {
380                                         // prevent private email from leaking into public conversation
381                                         if((! $toplevelpost) && (! toplevelprivate) && ($item['private']) && ($profile_owner != local_user()))
382                                                 continue;
383                                         $comments_seen ++;
384                                 }       
385
386                                 $override_comment_box = ((($page_writeable) && ($item_writeable)) ? true : false);
387                                 $show_comment_box = ((($page_writeable) && ($item_writeable) && ($comments_seen == $comments[$item['parent']])) ? true : false);
388
389                                 if(($comments[$item['parent']] > 2) && ($comments_seen <= ($comments[$item['parent']] - 2)) && ($item['gravity'] == 6)) {
390                                         if(! $comments_collapsed) {
391                                                 $threads[$threadsid] .= '<div class="ccollapse-wrapper fakelink" id="ccollapse-wrapper-' . $item['parent'] 
392                                                         . '" onclick="openClose(' . '\'ccollapse-' . $item['parent'] . '\'); $(\'#ccollapse-wrapper-' . $item['parent'] . '\').hide();" >' 
393                                                         . sprintf( t('See all %d comments'), $comments[$item['parent']]) . '</div>'
394                                                         . '<div class="ccollapse" id="ccollapse-' . $item['parent'] . '" style="display: none;" >';
395                                                 $comments_collapsed = true;
396                                         }
397                                 }
398                                 if(($comments[$item['parent']] > 2) && ($comments_seen == ($comments[$item['parent']] - 1))) {
399                                         $threads[$threadsid] .= '</div>';
400                                 }
401
402                                 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
403
404                                 $lock = ((($item['private']) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
405                                         || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
406                                         ? t('Private Message')
407                                         : false);
408
409
410                                 // Top-level wall post not written by the wall owner (wall-to-wall)
411                                 // First figure out who owns it. 
412
413                                 $osparkle = '';
414
415                                 if(($toplevelpost) && (! $item['self']) && ($mode !== 'profile')) {
416
417                                         if($item['wall']) {
418
419                                                 // On the network page, I am the owner. On the display page it will be the profile owner.
420                                                 // This will have been stored in $a->page_contact by our calling page.
421                                                 // Put this person on the left of the wall-to-wall notice.
422
423                                                 $owner_url = $a->page_contact['url'];
424                                                 $owner_photo = $a->page_contact['thumb'];
425                                                 $owner_name = $a->page_contact['name'];
426                                                 $template = $wallwall;
427                                                 $commentww = 'ww';      
428                                         }
429                                         if((! $item['wall']) && (strlen($item['owner-link'])) && ($item['owner-link'] != $item['author-link'])) {
430
431                                                 // Could be anybody. 
432
433                                                 $owner_url = $item['owner-link'];
434                                                 $owner_photo = $item['owner-avatar'];
435                                                 $owner_name = $item['owner-name'];
436                                                 $template = $wallwall;
437                                                 $commentww = 'ww';
438                                                 // If it is our contact, use a friendly redirect link
439                                                 if((link_compare($item['owner-link'],$item['url'])) 
440                                                         && ($item['network'] === 'dfrn')) {
441                                                         $owner_url = $redirect_url;
442                                                         $osparkle = ' sparkle';
443                                                 }
444                                         }
445                                 }
446
447                                 $likebuttons = '';
448                                 $shareable = ((($profile_owner == local_user()) && ($mode != 'display') && (! $item['private'])) ? true : false);
449
450                                 if($page_writeable) {
451                                         if($toplevelpost) {
452                                                 $likebuttons = array(
453                                                         'like' => array( t("I like this \x28toggle\x29"), t("like")),
454                                                         'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
455                                                 );
456                                                 if ($shareable) $likebuttons['share'] = array( t('Share this'), t('share'));
457                                         }
458
459                                         if(($show_comment_box) || (($show_comment_box == false) && ($override_comment_box == false) && ($item['last-child']))) {
460                                                 $comment = replace_macros($cmnt_tpl,array(
461                                                         '$return_path' => '', 
462                                                         '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
463                                                         '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
464                                                         '$id' => $item['item_id'],
465                                                         '$parent' => $item['parent'],
466                                                         '$profile_uid' =>  $profile_owner,
467                                                         '$mylink' => $a->contact['url'],
468                                                         '$mytitle' => t('This is you'),
469                                                         '$myphoto' => $a->contact['thumb'],
470                                                         '$comment' => t('Comment'),
471                                                         '$submit' => t('Submit'),
472                                                         '$ww' => (($mode === 'network') ? $commentww : '')
473                                                 ));
474                                         }
475                                 }
476
477                                 $edpost = (((($profile_owner == local_user()) && ($toplevelpost) && (intval($item['wall']) == 1)) || ($mode === 'notes'))
478                                                 ? array($a->get_baseurl()."/editpost/".$item['id'], t("Edit"))
479                                                 : False);
480
481
482                                 $drop = '';
483                                 $dropping = false;
484
485                                 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
486                                         $dropping = true;
487
488                                 $drop = array(
489                                         'dropping' => $dropping,
490                                         'select' => t('Select'), 
491                                         'delete' => t('Delete'),
492                                 );
493
494                                 $star = false;
495                                 $isstarred = "unstarred";
496                                 if ($profile_owner == local_user() && $toplevelpost) {
497                                         $isstarred = (($item['starred']) ? "starred" : "unstarred");
498
499                                         $star = array(
500                                                 'do' => t("add star"),
501                                                 'undo' => t("remove star"),
502                                                 'toggle' => t("toggle star status"),
503                                                 'classdo' => (($item['starred']) ? "hidden" : ""),
504                                                 'classundo' => (($item['starred']) ? "" : "hidden"),
505                                                 'starred' =>  t('starred'),
506                                                 'tagger' => t("add tag"),
507                                                 'classtagger' => "",
508                                         );
509                                 }
510
511
512
513                                 $photo = $item['photo'];
514                                 $thumb = $item['thumb'];
515
516                                 // Post was remotely authored.
517
518                                 $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
519
520                                 $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
521
522                                 $sp = false;
523                                 $profile_link = best_link_url($item,$sp);
524                                 if($sp)
525                                         $sparkle = ' sparkle';
526
527                                 if($profile_link === 'mailbox')
528                                         $profile_link = '';
529
530                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
531                                 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
532                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
533                                 else
534                                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $thumb);
535
536
537
538
539
540                                 $like    = ((x($alike,$item['id'])) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
541                                 $dislike = ((x($dlike,$item['id'])) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
542
543                                 $location = (($item['location']) ? '<a target="map" title="' . $item['location'] 
544                                         . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
545                                 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] 
546                                         . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
547                                 if($coord) {
548                                         if($location)
549                                                 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
550                                         else
551                                                 $location = '<span class="smalltext">' . $coord . '</span>';
552                                 }
553
554                                 $indent = (($toplevelpost) ? '' : ' comment');
555
556                                 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
557                                         $indent .= ' shiny'; 
558
559                                 // 
560                                 localize_item($item);
561
562
563                                 $tags=array();
564                                 foreach(explode(',',$item['tag']) as $tag){
565                                         $tag = trim($tag);
566                                         if ($tag!="") $tags[] = bbcode($tag);
567                                 }
568
569
570                                 // Build the HTML
571
572                                 $body = prepare_body($item,true);
573                                 
574
575                                 $tmp_item = replace_macros($template,array(
576                                         '$type' => implode("",array_slice(split("/",$item['verb']),-1)),
577                                         '$tags' => $tags,
578                                         '$body' => template_escape($body),
579                                         '$id' => $item['item_id'],
580                                         '$linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
581                                         '$olinktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
582                                         '$to' => t('to'),
583                                         '$wall' => t('Wall-to-Wall'),
584                                         '$vwall' => t('via Wall-To-Wall:'),
585                                         '$profile_url' => $profile_link,
586                                         '$item_photo_menu' => item_photo_menu($item),
587                                         '$name' => template_escape($profile_name),
588                                         '$thumb' => $profile_avatar,
589                                         '$osparkle' => $osparkle,
590                                         '$sparkle' => $sparkle,
591                                         '$title' => template_escape($item['title']),
592                                         '$ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
593                                         '$lock' => $lock,
594                                         '$location' => template_escape($location),
595                                         '$indent' => $indent,
596                                         '$owner_url' => $owner_url,
597                                         '$owner_photo' => $owner_photo,
598                                         '$owner_name' => template_escape($owner_name),
599                                         '$plink' => get_plink($item),
600                                         '$edpost' => $edpost,
601                                         '$isstarred' => $isstarred,
602                                         '$star' => $star,
603                                         '$drop' => $drop,
604                                         '$vote' => $likebuttons,
605                                         '$like' => $like,
606                                         '$dislike' => $dislike,
607                                         '$comment' => $comment,
608                                         '$wait' => t('Please wait'),
609
610                                 ));
611
612
613                                 $arr = array('item' => $item, 'output' => $tmp_item);
614                                 call_hooks('display_item', $arr);
615
616                                 $threads[$threadsid] .= $arr['output'];
617                         }
618                 }
619         }
620
621
622         // if author collapsing is in force but didn't get closed, close it off now.
623
624         /*if($blowhard_count >= 3)
625                 $threads[$threadsid] .= '</div>';*/
626
627         $page_template = get_markup_template("conversation.tpl");
628         $o .= replace_macros($page_template, array(
629                 '$threads' => $threads,
630                 '$dropping' => ($dropping?t('Delete Selected Items'):False),
631         ));
632
633         return $o;
634
635
636 function best_link_url($item,&$sparkle) {
637
638         $a = get_app();
639
640         $best_url = '';
641         $sparkle  = false;
642
643         $clean_url = normalise_link($item['author-link']);
644
645         if((local_user()) && (local_user() == $item['uid'])) {
646                 if(isset($a->contacts) && x($a->contacts,$clean_url)) {
647                         if($a->contacts[$clean_url]['network'] === NETWORK_DFRN) {
648                                 $best_url = $a->get_baseurl() . '/redir/' . $a->contacts[$clean_url]['id'];
649                                 $sparkle = true;
650                         }
651                         else
652                                 $best_url = $a->contacts[$clean_url]['url'];
653                 }
654         }
655         if(! $best_url) {
656                 if(strlen($item['author-link']))
657                         $best_url = $item['author-link'];
658                 else
659                         $best_url = $item['url'];
660         }
661
662         return $best_url;
663 }
664
665
666 if(! function_exists('item_photo_menu')){
667 function item_photo_menu($item){
668         $a = get_app();
669         
670         if (local_user() && (! count($a->contacts)))
671                 load_contact_links(local_user());
672
673         $contact_url="";
674         $pm_url="";
675         $status_link="";
676         $photos_link="";
677         $posts_link="";
678
679         $sparkle = false;
680     $profile_link = best_link_url($item,$sparkle);
681         if($profile_link === 'mailbox')
682                 $profile_link = '';
683
684         if($sparkle) {
685                 $cid = intval(basename($profile_link));
686                 $status_link = $profile_link . "?url=status";
687                 $photos_link = $profile_link . "?url=photos";
688                 $profile_link = $profile_link . "?url=profile";
689                 $pm_url = $a->get_baseurl() . '/message/new/' . $cid;
690         }
691         else {
692                 if(local_user() && local_user() == $item['uid'] && link_compare($item['url'],$item['author-link'])) {
693                         $cid = $item['contact-id'];
694                 }               
695                 else {
696                         $cid = 0;
697                 }
698         }
699         if(($cid) && (! $item['self'])) {
700                 $contact_url = $a->get_baseurl() . '/contacts/' . $cid;
701                 $posts_link = $a->get_baseurl() . '/network/?cid=' . $cid;
702         }
703
704         $menu = Array(
705                 t("View status") => $status_link,
706                 t("View profile") => $profile_link,
707                 t("View photos") => $photos_link,               
708                 t("View recent") => $posts_link, 
709                 t("Edit contact") => $contact_url,
710                 t("Send PM") => $pm_url,
711         );
712         
713         
714         $args = array($item, &$menu);
715         
716         call_hooks('item_photo_menu', $args);
717         
718         $o = "";
719         foreach($menu as $k=>$v){
720                 if ($v!="") $o .= "<li><a href='$v'>$k</a></li>\n";
721         }
722         return $o;
723 }}
724
725 if(! function_exists('like_puller')) {
726 function like_puller($a,$item,&$arr,$mode) {
727
728         $url = '';
729         $sparkle = '';
730         $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
731
732         if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
733                 $url = $item['author-link'];
734                 if((local_user()) && (local_user() == $item['uid']) && ($item['network'] === 'dfrn') && (! $item['self']) && (link_compare($item['author-link'],$item['url']))) {
735                         $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
736                         $sparkle = ' class="sparkle" ';
737                 }
738                 if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
739                         $arr[$item['parent'] . '-l'] = array();
740                 if(! isset($arr[$item['parent']]))
741                         $arr[$item['parent']] = 1;
742                 else    
743                         $arr[$item['parent']] ++;
744                 $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
745         }
746         return;
747 }}
748
749 // Format the like/dislike text for a profile item
750 // $cnt = number of people who like/dislike the item
751 // $arr = array of pre-linked names of likers/dislikers
752 // $type = one of 'like, 'dislike'
753 // $id  = item id
754 // returns formatted text
755
756 if(! function_exists('format_like')) {
757 function format_like($cnt,$arr,$type,$id) {
758         $o = '';
759         if($cnt == 1)
760                 $o .= (($type === 'like') ? sprintf( t('%s likes this.'), $arr[0]) : sprintf( t('%s doesn\'t like this.'), $arr[0])) . EOL ;
761         else {
762                 $spanatts = 'class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');"';
763                 $o .= (($type === 'like') ? 
764                                         sprintf( t('<span  %1$s>%2$d people</span> like this.'), $spanatts, $cnt)
765                                          : 
766                                         sprintf( t('<span  %1$s>%2$d people</span> don\'t like this.'), $spanatts, $cnt) ); 
767                 $o .= EOL ;
768                 $total = count($arr);
769                 if($total >= MAX_LIKERS)
770                         $arr = array_slice($arr, 0, MAX_LIKERS - 1);
771                 if($total < MAX_LIKERS)
772                         $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
773                 $str = implode(', ', $arr);
774                 if($total >= MAX_LIKERS)
775                         $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
776                 $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str));
777                 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
778         }
779         return $o;
780 }}
781
782
783 function status_editor($a,$x, $notes_cid = 0) {
784
785         $o = '';
786                 
787         $geotag = (($x['allow_location']) ? get_markup_template('jot_geotag.tpl') : '');
788
789                 $tpl = get_markup_template('jot-header.tpl');
790         
791                 $a->page['htmlhead'] .= replace_macros($tpl, array(
792                         '$newpost' => 'true',
793                         '$baseurl' => $a->get_baseurl(),
794                         '$geotag' => $geotag,
795                         '$nickname' => $x['nickname'],
796                         '$ispublic' => t('Visible to <strong>everybody</strong>'),
797                         '$linkurl' => t('Please enter a link URL:'),
798                         '$vidurl' => t("Please enter a video link/URL:"),
799                         '$audurl' => t("Please enter an audio link/URL:"),
800                         '$term' => t('Tag term:'),
801                         '$whereareu' => t('Where are you right now?'),
802                         '$title' => t('Enter a title for this item') 
803                 ));
804
805
806                 $tpl = get_markup_template("jot.tpl");
807                 
808                 $jotplugins = '';
809                 $jotnets = '';
810
811                 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
812
813                 $mail_enabled = false;
814                 $pubmail_enabled = false;
815
816                 if(($x['is_owner']) && (! $mail_disabled)) {
817                         $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
818                                 intval(local_user())
819                         );
820                         if(count($r)) {
821                                 $mail_enabled = true;
822                                 if(intval($r[0]['pubmail']))
823                                         $pubmail_enabled = true;
824                         }
825                 }
826
827                 if($mail_enabled) {
828                $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
829                         $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> '
830                 . t("Post to Email") . '</div>';
831                 }
832
833                 call_hooks('jot_tool', $jotplugins);
834                 call_hooks('jot_networks', $jotnets);
835
836                 if($notes_cid)
837                         $jotnets .= '<input type="hidden" name="contact_allow[]" value="' . $notes_cid .'" />';
838
839                 $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));        
840
841                 $o .= replace_macros($tpl,array(
842                         '$return_path' => $a->cmd,
843                         '$action' => 'item',
844                         '$share' => (($x['button']) ? $x['button'] : t('Share')),
845                         '$upload' => t('Upload photo'),
846                         '$attach' => t('Attach file'),
847                         '$weblink' => t('Insert web link'),
848                         '$video' => t('Insert video link'),
849                         '$audio' => t('Insert audio link'),
850                         '$setloc' => t('Set your location'),
851                         '$noloc' => t('Clear browser location'),
852                         '$title' => t('Set title'),
853                         '$wait' => t('Please wait'),
854                         '$permset' => t('Permission settings'),
855                         '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
856                         '$content' => '',
857                         '$post_id' => '',
858                         '$baseurl' => $a->get_baseurl(),
859                         '$defloc' => $x['default_location'],
860                         '$visitor' => $x['visitor'],
861                         '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
862                         '$emailcc' => t('CC: email addresses'),
863                         '$public' => t('Public post'),
864                         '$jotnets' => $jotnets,
865                         '$emtitle' => t('Example: bob@example.com, mary@example.com'),
866                         '$lockstate' => $x['lockstate'],
867                         '$acl' => $x['acl'],
868                         '$bang' => $x['bang'],
869                         '$profile_uid' => $x['profile_uid'],
870                 ));
871
872         return $o;
873 }