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