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