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