]> git.mxchange.org Git - friendica.git/blob - include/conversation.php
Merge remote-tracking branch 'friendica/master'
[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->id, $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  * "Render" a conversation or list of items for HTML display.
124  * There are two major forms of display:
125  *      - Sequential or unthreaded ("New Item View" or search results)
126  *      - conversation view
127  * The $mode parameter decides between the various renderings and also
128  * figures out how to determine page owner and other contextual items 
129  * that are based on unique features of the calling module.
130  *
131  */
132 function conversation(&$a, $items, $mode, $update) {
133
134         require_once('bbcode.php');
135
136         $profile_owner = 0;
137         $page_writeable      = false;
138
139         if($mode === 'network') {
140                 $profile_owner = local_user();
141                 $page_writeable = true;
142         }
143
144         if($mode === 'profile') {
145                 $profile_owner = $a->profile['profile_uid'];
146                 $page_writeable = can_write_wall($a,$profile_owner);
147         }
148
149         if($mode === 'notes') {
150                 $profile_owner = local_user();
151                 $page_writeable = true;
152         }
153
154         if($mode === 'display') {
155                 $profile_owner = $a->profile['uid'];
156                 $page_writeable = can_write_wall($a,$profile_owner);
157         }
158
159         if($mode === 'community') {
160                 $profile_owner = 0;
161                 $page_writeable = false;
162         }
163
164         if($update)
165                 $return_url = $_SESSION['return_url'];
166         else
167                 $return_url = $_SESSION['return_url'] = $a->cmd;
168
169         load_contact_links(local_user());
170
171
172         $cmnt_tpl    = get_markup_template('comment_item.tpl');
173         $tpl         = get_markup_template('wall_item.tpl');
174         $wallwall    = get_markup_template('wallwall_item.tpl');
175         $hide_comments_tpl = get_markup_template('hide_comments.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                         
213                                 $profile_name   = ((strlen($item['author-name']))   ? $item['author-name']   : $item['name']);
214                                 if($item['author-link'] && (! $item['author-name']))
215                                         $profile_name = $item['author-link'];
216
217                                 $sp = false;
218                                 $profile_link = best_link_url($item,$sp);
219                                 if($sp)
220                                         $sparkle = ' sparkle';
221                                 if($profile_link === 'mailbox')
222                                         $profile_link = '';
223
224
225                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
226                                 if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
227                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
228                                 else
229                                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
230
231                                 $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
232                                 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
233                                 if($coord) {
234                                         if($location)
235                                                 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
236                                         else
237                                                 $location = '<span class="smalltext">' . $coord . '</span>';
238                                 }
239
240
241                                 localize_item($item);
242                                 if($mode === 'network-new')
243                                         $dropping = true;
244                                 else
245                                         $dropping = false;
246
247
248                                 $drop = array(
249                                         'dropping' => $dropping,
250                                         'select' => t('Select'), 
251                                         'delete' => t('Delete'),
252                                 );
253
254                                 $star = false;
255                                 $isstarred = "unstarred";
256                                 
257                                 $lock = false;
258                                 $likebuttons = false;
259                                 $shareable = false;
260
261                                 $body = prepare_body($item,true);
262                                 
263                                 $tmp_item = replace_macros($tpl,array(
264                                         '$id' => $item['item_id'],
265                                         '$linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
266                                         '$profile_url' => $profile_link,
267                                         '$item_photo_menu' => item_photo_menu($item),
268                                         '$name' => template_escape($profile_name),
269                                         '$sparkle' => $sparkle,
270                                         '$lock' => $lock,
271                                         '$thumb' => $profile_avatar,
272                                         '$title' => template_escape($item['title']),
273                                         '$body' => template_escape($body),
274                                         '$ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
275                                         '$lock' => $lock,
276                                         '$location' => template_escape($location),
277                                         '$indent' => '',
278                                         '$owner_name' => template_escape($owner_name),
279                                         '$owner_url' => $owner_url,
280                                         '$owner_photo' => $owner_photo,
281                                         '$plink' => get_plink($item),
282                                         '$edpost' => false,
283                                         '$isstarred' => $isstarred,
284                                         '$star' => $star,
285                                         '$drop' => $drop,
286                                         '$vote' => $likebuttons,
287                                         '$like' => '',
288                                         '$dislike' => '',
289                                         '$comment' => '',
290                                         '$conv' => array('href'=> $a->get_baseurl() . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context')),
291                                         '$wait' => t('Please wait'),
292                                 ));
293
294                                 $arr = array('item' => $item, 'output' => $tmp_item);
295                                 call_hooks('display_item', $arr);
296
297                                 $threads[$threadsid]['id'] = $item['item_id'];
298                                 $threads[$threadsid]['html'] .= $arr['output'];
299
300                         }
301
302                 }
303                 else
304                 {
305                         // Normal View
306
307
308                         // Figure out how many comments each parent has
309                         // (Comments all have gravity of 6)
310                         // Store the result in the $comments array
311
312                         $comments = array();
313                         foreach($items as $item) {
314                                 if((intval($item['gravity']) == 6) && ($item['id'] != $item['parent'])) {
315                                         if(! x($comments,$item['parent']))
316                                                 $comments[$item['parent']] = 1;
317                                         else
318                                                 $comments[$item['parent']] += 1;
319                                 }
320                         }
321
322                         // map all the like/dislike activities for each parent item 
323                         // Store these in the $alike and $dlike arrays
324
325                         foreach($items as $item) {
326                                 like_puller($a,$item,$alike,'like');
327                                 like_puller($a,$item,$dlike,'dislike');
328                         }
329
330                         $comments_collapsed = false;
331                         $blowhard = 0;
332                         $blowhard_count = 0;
333
334
335                         foreach($items as $item) {
336
337                                 $comment = '';
338                                 $template = $tpl;
339                                 $commentww = '';
340                                 $sparkle = '';
341                                 $owner_url = $owner_photo = $owner_name = '';
342
343                                 // We've already parsed out like/dislike for special treatment. We can ignore them now
344
345                                 if(((activity_match($item['verb'],ACTIVITY_LIKE)) 
346                                         || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
347                                         && ($item['id'] != $item['parent']))
348                                         continue;
349
350                                 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
351                                 $toplevelprivate = false;
352
353                                 // Take care of author collapsing and comment collapsing
354                                 // If a single author has more than 3 consecutive top-level posts, squash the remaining ones.
355                                 // If there are more than two comments, squash all but the last 2.
356                         
357                                 if($toplevelpost) {
358                                         $toplevelprivate = (($toplevelpost && $item['private']) ? true : false);
359                                         $item_writeable = (($item['writable'] || $item['self']) ? true : false);
360
361                                         /*if($blowhard == $item['cid'] && (! $item['self']) && ($mode != 'profile') && ($mode != 'notes')) {
362                                                 $blowhard_count ++;
363                                                 if($blowhard_count == 3) {
364                                                         $o .= '<div class="icollapse-wrapper fakelink" id="icollapse-wrapper-' . $item['parent'] 
365                                                                 . '" onclick="openClose(' . '\'icollapse-' . $item['parent'] . '\'); $(\'#icollapse-wrapper-' . $item['parent'] . '\').hide();" >' 
366                                                                 . t('See more posts like this') . '</div>' . '<div class="icollapse" id="icollapse-' 
367                                                                 . $item['parent'] . '" style="display: none;" >';
368                                                 }
369                                         }
370                                         else {
371                                                 $blowhard = $item['cid'];                                       
372                                                 if($blowhard_count >= 3)
373                                                         $o .= '</div>';
374                                                 $blowhard_count = 0;
375                                         }*/
376
377                                         $comments_seen = 0;
378                                         $comments_collapsed = false;
379                                         
380                                         $threadsid++;
381                                         $threads[$threadsid]['id'] = $item['item_id'];
382                                         $threads[$threadsid]['html'] = "";
383
384                                 }
385                                 else {
386                                         // prevent private email from leaking into public conversation
387                                         if((! $toplevelpost) && (! toplevelprivate) && ($item['private']) && ($profile_owner != local_user()))
388                                                 continue;
389                                         $comments_seen ++;
390                                 }       
391
392                                 $override_comment_box = ((($page_writeable) && ($item_writeable)) ? true : false);
393                                 $show_comment_box = ((($page_writeable) && ($item_writeable) && ($comments_seen == $comments[$item['parent']])) ? true : false);
394
395
396                                 if(($comments[$item['parent']] > 2) && ($comments_seen <= ($comments[$item['parent']] - 2)) && ($item['gravity'] == 6)) {
397                                         if(! $comments_collapsed) {
398
399                                                 // IMPORTANT: the closing </div> in the hide_comments template
400                                                 // is supplied below in code. 
401
402                                                 $threads[$threadsid]['html'] .= replace_macros($hide_comments_tpl,array(
403                                                         '$id' => $item['parent'],
404                                                         '$num_comments' => sprintf( tt('%d comment','%d comments',$comments[$item['parent']]),
405                                                                 $comments[$item['parent']]),
406                                                         '$display' => 'none',
407                                                         '$hide_text' => t('show more')
408                                                 ));
409                                                 $comments_collapsed = true;
410                                         }
411                                 }
412                                 if(($comments[$item['parent']] > 2) && ($comments_seen == ($comments[$item['parent']] - 1))) {
413                                         $threads[$threadsid]['html'] .= '</div>';
414                                 }
415
416                                 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
417
418                                 $lock = ((($item['private']) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
419                                         || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
420                                         ? t('Private Message')
421                                         : false);
422
423
424                                 // Top-level wall post not written by the wall owner (wall-to-wall)
425                                 // First figure out who owns it. 
426
427                                 $osparkle = '';
428
429                                 if(($toplevelpost) && (! $item['self']) && ($mode !== 'profile')) {
430
431                                         if($item['wall']) {
432
433                                                 // On the network page, I am the owner. On the display page it will be the profile owner.
434                                                 // This will have been stored in $a->page_contact by our calling page.
435                                                 // Put this person on the left of the wall-to-wall notice.
436
437                                                 $owner_url = $a->page_contact['url'];
438                                                 $owner_photo = $a->page_contact['thumb'];
439                                                 $owner_name = $a->page_contact['name'];
440                                                 $template = $wallwall;
441                                                 $commentww = 'ww';      
442                                         }
443                                         if((! $item['wall']) && (strlen($item['owner-link'])) && ($item['owner-link'] != $item['author-link'])) {
444
445                                                 // Could be anybody. 
446
447                                                 $owner_url = $item['owner-link'];
448                                                 $owner_photo = $item['owner-avatar'];
449                                                 $owner_name = $item['owner-name'];
450                                                 $template = $wallwall;
451                                                 $commentww = 'ww';
452                                                 // If it is our contact, use a friendly redirect link
453                                                 if((link_compare($item['owner-link'],$item['url'])) 
454                                                         && ($item['network'] === 'dfrn')) {
455                                                         $owner_url = $redirect_url;
456                                                         $osparkle = ' sparkle';
457                                                 }
458                                         }
459                                 }
460
461                                 $likebuttons = '';
462                                 $shareable = ((($profile_owner == local_user()) && ($mode != 'display') && (! $item['private'])) ? true : false);
463
464                                 if($page_writeable) {
465                                         if($toplevelpost) {
466                                                 $likebuttons = array(
467                                                         'like' => array( t("I like this \x28toggle\x29"), t("like")),
468                                                         'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
469                                                 );
470                                                 if ($shareable) $likebuttons['share'] = array( t('Share this'), t('share'));
471                                         }
472
473                                         if(($show_comment_box) || (($show_comment_box == false) && ($override_comment_box == false) && ($item['last-child']))) {
474                                                 $comment = replace_macros($cmnt_tpl,array(
475                                                         '$return_path' => '', 
476                                                         '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
477                                                         '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
478                                                         '$id' => $item['item_id'],
479                                                         '$parent' => $item['parent'],
480                                                         '$profile_uid' =>  $profile_owner,
481                                                         '$mylink' => $a->contact['url'],
482                                                         '$mytitle' => t('This is you'),
483                                                         '$myphoto' => $a->contact['thumb'],
484                                                         '$comment' => t('Comment'),
485                                                         '$submit' => t('Submit'),
486                                                         '$ww' => (($mode === 'network') ? $commentww : '')
487                                                 ));
488                                         }
489                                 }
490
491                                 $edpost = (((($profile_owner == local_user()) && ($toplevelpost) && (intval($item['wall']) == 1)) || ($mode === 'notes'))
492                                                 ? array($a->get_baseurl()."/editpost/".$item['id'], t("Edit"))
493                                                 : False);
494
495
496                                 $drop = '';
497                                 $dropping = false;
498
499                                 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
500                                         $dropping = true;
501
502                                 $drop = array(
503                                         'dropping' => $dropping,
504                                         'select' => t('Select'), 
505                                         'delete' => t('Delete'),
506                                 );
507
508                                 $star = false;
509                                 $isstarred = "unstarred";
510                                 if ($profile_owner == local_user() && $toplevelpost) {
511                                         $isstarred = (($item['starred']) ? "starred" : "unstarred");
512
513                                         $star = array(
514                                                 'do' => t("add star"),
515                                                 'undo' => t("remove star"),
516                                                 'toggle' => t("toggle star status"),
517                                                 'classdo' => (($item['starred']) ? "hidden" : ""),
518                                                 'classundo' => (($item['starred']) ? "" : "hidden"),
519                                                 'starred' =>  t('starred'),
520                                                 'tagger' => t("add tag"),
521                                                 'classtagger' => "",
522                                         );
523                                 }
524
525
526
527                                 $photo = $item['photo'];
528                                 $thumb = $item['thumb'];
529
530                                 // Post was remotely authored.
531
532                                 $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
533
534                                 $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
535
536                                 if($item['author-link'] && (! $item['author-name']))
537                                         $profile_name = $item['author-link'];
538
539
540                                 $sp = false;
541                                 $profile_link = best_link_url($item,$sp);
542                                 if($sp)
543                                         $sparkle = ' sparkle';
544
545                                 if($profile_link === 'mailbox')
546                                         $profile_link = '';
547
548                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
549                                 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
550                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
551                                 else
552                                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $thumb);
553
554
555
556
557
558                                 $like    = ((x($alike,$item['id'])) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
559                                 $dislike = ((x($dlike,$item['id'])) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
560
561                                 $location = (($item['location']) ? '<a target="map" title="' . $item['location'] 
562                                         . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
563                                 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] 
564                                         . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
565                                 if($coord) {
566                                         if($location)
567                                                 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
568                                         else
569                                                 $location = '<span class="smalltext">' . $coord . '</span>';
570                                 }
571
572                                 $indent = (($toplevelpost) ? '' : ' comment');
573
574                                 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
575                                         $indent .= ' shiny'; 
576
577                                 // 
578                                 localize_item($item);
579
580
581                                 $tags=array();
582                                 foreach(explode(',',$item['tag']) as $tag){
583                                         $tag = trim($tag);
584                                         if ($tag!="") $tags[] = bbcode($tag);
585                                 }
586
587
588                                 // Build the HTML
589
590                                 $body = prepare_body($item,true);
591                                 
592
593                                 $tmp_item = replace_macros($template,array(
594                                         '$type' => implode("",array_slice(split("/",$item['verb']),-1)),
595                                         '$tags' => $tags,
596                                         '$body' => template_escape($body),
597                                         '$id' => $item['item_id'],
598                                         '$linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
599                                         '$olinktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
600                                         '$to' => t('to'),
601                                         '$wall' => t('Wall-to-Wall'),
602                                         '$vwall' => t('via Wall-To-Wall:'),
603                                         '$profile_url' => $profile_link,
604                                         '$item_photo_menu' => item_photo_menu($item),
605                                         '$name' => template_escape($profile_name),
606                                         '$thumb' => $profile_avatar,
607                                         '$osparkle' => $osparkle,
608                                         '$sparkle' => $sparkle,
609                                         '$title' => template_escape($item['title']),
610                                         '$ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
611                                         '$lock' => $lock,
612                                         '$location' => template_escape($location),
613                                         '$indent' => $indent,
614                                         '$owner_url' => $owner_url,
615                                         '$owner_photo' => $owner_photo,
616                                         '$owner_name' => template_escape($owner_name),
617                                         '$plink' => get_plink($item),
618                                         '$edpost' => $edpost,
619                                         '$isstarred' => $isstarred,
620                                         '$star' => $star,
621                                         '$drop' => $drop,
622                                         '$vote' => $likebuttons,
623                                         '$like' => $like,
624                                         '$dislike' => $dislike,
625                                         '$comment' => $comment,
626                                         '$wait' => t('Please wait'),
627
628                                 ));
629
630
631                                 $arr = array('item' => $item, 'output' => $tmp_item);
632                                 call_hooks('display_item', $arr);
633
634                                 $threads[$threadsid]['html'] .= $arr['output'];
635                         }
636                 }
637         }
638
639
640         $page_template = get_markup_template("conversation.tpl");
641         $o .= replace_macros($page_template, array(
642                 '$threads' => $threads,
643                 '$dropping' => ($dropping?t('Delete Selected Items'):False),
644         ));
645
646         return $o;
647
648
649 function best_link_url($item,&$sparkle) {
650
651         $a = get_app();
652
653         $best_url = '';
654         $sparkle  = false;
655
656         $clean_url = normalise_link($item['author-link']);
657
658         if((local_user()) && (local_user() == $item['uid'])) {
659                 if(isset($a->contacts) && x($a->contacts,$clean_url)) {
660                         if($a->contacts[$clean_url]['network'] === NETWORK_DFRN) {
661                                 $best_url = $a->get_baseurl() . '/redir/' . $a->contacts[$clean_url]['id'];
662                                 $sparkle = true;
663                         }
664                         else
665                                 $best_url = $a->contacts[$clean_url]['url'];
666                 }
667         }
668         if(! $best_url) {
669                 if(strlen($item['author-link']))
670                         $best_url = $item['author-link'];
671                 else
672                         $best_url = $item['url'];
673         }
674
675         return $best_url;
676 }
677
678
679 if(! function_exists('item_photo_menu')){
680 function item_photo_menu($item){
681         $a = get_app();
682         
683         if (local_user() && (! count($a->contacts)))
684                 load_contact_links(local_user());
685
686         $contact_url="";
687         $pm_url="";
688         $status_link="";
689         $photos_link="";
690         $posts_link="";
691
692         $sparkle = false;
693     $profile_link = best_link_url($item,$sparkle);
694         if($profile_link === 'mailbox')
695                 $profile_link = '';
696
697         if($sparkle) {
698                 $cid = intval(basename($profile_link));
699                 $status_link = $profile_link . "?url=status";
700                 $photos_link = $profile_link . "?url=photos";
701                 $profile_link = $profile_link . "?url=profile";
702                 $pm_url = $a->get_baseurl() . '/message/new/' . $cid;
703         }
704         else {
705                 if(local_user() && local_user() == $item['uid'] && link_compare($item['url'],$item['author-link'])) {
706                         $cid = $item['contact-id'];
707                 }               
708                 else {
709                         $cid = 0;
710                 }
711         }
712         if(($cid) && (! $item['self'])) {
713                 $contact_url = $a->get_baseurl() . '/contacts/' . $cid;
714                 $posts_link = $a->get_baseurl() . '/network/?cid=' . $cid;
715         }
716
717         $menu = Array(
718                 t("View status") => $status_link,
719                 t("View profile") => $profile_link,
720                 t("View photos") => $photos_link,               
721                 t("View recent") => $posts_link, 
722                 t("Edit contact") => $contact_url,
723                 t("Send PM") => $pm_url,
724         );
725         
726         
727         $args = array($item, &$menu);
728         
729         call_hooks('item_photo_menu', $args);
730         
731         $o = "";
732         foreach($menu as $k=>$v){
733                 if ($v!="") $o .= "<li><a href='$v'>$k</a></li>\n";
734         }
735         return $o;
736 }}
737
738 if(! function_exists('like_puller')) {
739 function like_puller($a,$item,&$arr,$mode) {
740
741         $url = '';
742         $sparkle = '';
743         $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
744
745         if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
746                 $url = $item['author-link'];
747                 if((local_user()) && (local_user() == $item['uid']) && ($item['network'] === 'dfrn') && (! $item['self']) && (link_compare($item['author-link'],$item['url']))) {
748                         $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
749                         $sparkle = ' class="sparkle" ';
750                 }
751                 if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
752                         $arr[$item['parent'] . '-l'] = array();
753                 if(! isset($arr[$item['parent']]))
754                         $arr[$item['parent']] = 1;
755                 else    
756                         $arr[$item['parent']] ++;
757                 $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
758         }
759         return;
760 }}
761
762 // Format the like/dislike text for a profile item
763 // $cnt = number of people who like/dislike the item
764 // $arr = array of pre-linked names of likers/dislikers
765 // $type = one of 'like, 'dislike'
766 // $id  = item id
767 // returns formatted text
768
769 if(! function_exists('format_like')) {
770 function format_like($cnt,$arr,$type,$id) {
771         $o = '';
772         if($cnt == 1)
773                 $o .= (($type === 'like') ? sprintf( t('%s likes this.'), $arr[0]) : sprintf( t('%s doesn\'t like this.'), $arr[0])) . EOL ;
774         else {
775                 $spanatts = 'class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');"';
776                 $o .= (($type === 'like') ? 
777                                         sprintf( t('<span  %1$s>%2$d people</span> like this.'), $spanatts, $cnt)
778                                          : 
779                                         sprintf( t('<span  %1$s>%2$d people</span> don\'t like this.'), $spanatts, $cnt) ); 
780                 $o .= EOL ;
781                 $total = count($arr);
782                 if($total >= MAX_LIKERS)
783                         $arr = array_slice($arr, 0, MAX_LIKERS - 1);
784                 if($total < MAX_LIKERS)
785                         $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
786                 $str = implode(', ', $arr);
787                 if($total >= MAX_LIKERS)
788                         $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
789                 $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str));
790                 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
791         }
792         return $o;
793 }}
794
795
796 function status_editor($a,$x, $notes_cid = 0) {
797
798         $o = '';
799                 
800         $geotag = (($x['allow_location']) ? get_markup_template('jot_geotag.tpl') : '');
801
802                 $tpl = get_markup_template('jot-header.tpl');
803         
804                 $a->page['htmlhead'] .= replace_macros($tpl, array(
805                         '$newpost' => 'true',
806                         '$baseurl' => $a->get_baseurl(),
807                         '$geotag' => $geotag,
808                         '$nickname' => $x['nickname'],
809                         '$ispublic' => t('Visible to <strong>everybody</strong>'),
810                         '$linkurl' => t('Please enter a link URL:'),
811                         '$vidurl' => t("Please enter a video link/URL:"),
812                         '$audurl' => t("Please enter an audio link/URL:"),
813                         '$term' => t('Tag term:'),
814                         '$whereareu' => t('Where are you right now?'),
815                         '$title' => t('Enter a title for this item') 
816                 ));
817
818
819                 $tpl = get_markup_template("jot.tpl");
820                 
821                 $jotplugins = '';
822                 $jotnets = '';
823
824                 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
825
826                 $mail_enabled = false;
827                 $pubmail_enabled = false;
828
829                 if(($x['is_owner']) && (! $mail_disabled)) {
830                         $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
831                                 intval(local_user())
832                         );
833                         if(count($r)) {
834                                 $mail_enabled = true;
835                                 if(intval($r[0]['pubmail']))
836                                         $pubmail_enabled = true;
837                         }
838                 }
839
840                 if($mail_enabled) {
841                $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
842                         $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> '
843                 . t("Post to Email") . '</div>';
844                 }
845
846                 call_hooks('jot_tool', $jotplugins);
847                 call_hooks('jot_networks', $jotnets);
848
849                 if($notes_cid)
850                         $jotnets .= '<input type="hidden" name="contact_allow[]" value="' . $notes_cid .'" />';
851
852                 $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));        
853
854                 $o .= replace_macros($tpl,array(
855                         '$return_path' => $a->cmd,
856                         '$action' => 'item',
857                         '$share' => (($x['button']) ? $x['button'] : t('Share')),
858                         '$upload' => t('Upload photo'),
859                         '$shortupload' => t('upload photo'),
860                         '$attach' => t('Attach file'),
861                         '$shortattach' => t('attach file'),
862                         '$weblink' => t('Insert web link'),
863                         '$shortweblink' => t('web link'),
864                         '$video' => t('Insert video link'),
865                         '$shortvideo' => t('video link'),
866                         '$audio' => t('Insert audio link'),
867                         '$shortaudio' => t('audio link'),
868                         '$setloc' => t('Set your location'),
869                         '$shortsetloc' => t('set location'),
870                         '$noloc' => t('Clear browser location'),
871                         '$shortnoloc' => t('clear location'),
872                         '$title' => "",
873                         '$placeholdertitle' => t('Set title'),
874                         '$wait' => t('Please wait'),
875                         '$permset' => t('Permission settings'),
876                         '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
877                         '$content' => '',
878                         '$post_id' => '',
879                         '$baseurl' => $a->get_baseurl(),
880                         '$defloc' => $x['default_location'],
881                         '$visitor' => $x['visitor'],
882                         '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
883                         '$emailcc' => t('CC: email addresses'),
884                         '$public' => t('Public post'),
885                         '$jotnets' => $jotnets,
886                         '$emtitle' => t('Example: bob@example.com, mary@example.com'),
887                         '$lockstate' => $x['lockstate'],
888                         '$acl' => $x['acl'],
889                         '$bang' => $x['bang'],
890                         '$profile_uid' => $x['profile_uid'],
891                 ));
892
893         return $o;
894 }
895
896
897 function conv_sort($arr,$order) {
898
899         if((!(is_array($arr) && count($arr))))
900                 return array();
901
902         $parents = array();
903
904         foreach($arr as $x)
905                 if($x['id'] == $x['parent'])
906                                 $parents[] = $x;
907
908         if(stristr($order,'created'))
909                 usort($parents,'sort_thr_created');
910         elseif(stristr($order,'commented'))
911                 usort($parents,'sort_thr_commented');
912
913         foreach($parents as $x) 
914                 $x['children'] = array();
915
916         foreach($arr as $x) {
917                 if($x['id'] != $x['parent']) {
918                         $p = find_thread_parent_index($parents,$x);
919                         $parents[$p]['children'][] = $x;
920                 }
921         }
922         foreach($parents as $x)
923                 if(count($x['children']))
924                         usort($x['children'],'sort_thr_created_rev');
925
926         $ret = array();
927         foreach($parents as $x) {
928                 $ret[] = $x;
929                 if(count($x['children']))
930                         foreach($x['children'] as $y)
931                                 $ret[] = $y;
932         }
933
934         return $ret;
935 }
936
937
938 function sort_thr_created($a,$b) {
939         return strcmp($b['created'],$a['created']);
940 }
941
942 function sort_thr_created_rev($a,$b) {
943         return strcmp($a['created'],$b['created']);
944 }
945
946 function sort_thr_commented($a,$b) {
947         return strcmp($b['commented'],$a['commented']);
948 }
949
950 function find_thread_parent_index($arr,$x) {
951         foreach($arr as $k => $v)
952                 if($v['id'] == $x['parent'])
953                         return $k;
954 }