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