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