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