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