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