]> git.mxchange.org Git - friendica.git/blob - include/conversation.php
fix typo
[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                                         );
470                                 }
471
472
473
474                                 $photo = $item['photo'];
475                                 $thumb = $item['thumb'];
476
477                                 // Post was remotely authored.
478
479                                 $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
480
481                                 $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
482
483                                 $sp = false;
484                                 $profile_link = best_link_url($item,$sp);
485                                 if($sp)
486                                         $sparkle = ' sparkle';
487
488                                 if($profile_link === 'mailbox')
489                                         $profile_link = '';
490
491                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
492                                 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
493                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
494                                 else
495                                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $thumb);
496
497
498
499
500
501                                 $like    = ((x($alike,$item['id'])) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
502                                 $dislike = ((x($dlike,$item['id'])) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
503
504                                 $location = (($item['location']) ? '<a target="map" title="' . $item['location'] 
505                                         . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
506                                 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] 
507                                         . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
508                                 if($coord) {
509                                         if($location)
510                                                 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
511                                         else
512                                                 $location = '<span class="smalltext">' . $coord . '</span>';
513                                 }
514
515                                 $indent = (($toplevelpost) ? '' : ' comment');
516
517                                 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
518                                         $indent .= ' shiny'; 
519
520                                 // 
521                                 localize_item($item);
522
523                                 // Build the HTML
524
525                                 $body = prepare_body($item,true);
526
527
528                                 $tmp_item = replace_macros($template,array(
529                                         '$body' => template_escape($body),
530                                         '$id' => $item['item_id'],
531                                         '$linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
532                                         '$olinktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
533                                         '$to' => t('to'),
534                                         '$wall' => t('Wall-to-Wall'),
535                                         '$vwall' => t('via Wall-To-Wall:'),
536                                         '$profile_url' => $profile_link,
537                                         '$item_photo_menu' => item_photo_menu($item),
538                                         '$name' => template_escape($profile_name),
539                                         '$thumb' => $profile_avatar,
540                                         '$osparkle' => $osparkle,
541                                         '$sparkle' => $sparkle,
542                                         '$title' => template_escape($item['title']),
543                                         '$ago' => ((($item['app']) && ($item['id'] == $item['parent'])) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
544                                         '$lock' => $lock,
545                                         '$location' => template_escape($location),
546                                         '$indent' => $indent,
547                                         '$owner_url' => $owner_url,
548                                         '$owner_photo' => $owner_photo,
549                                         '$owner_name' => template_escape($owner_name),
550                                         '$plink' => get_plink($item),
551                                         '$edpost' => $edpost,
552                                         '$isstarred' => $isstarred,
553                                         '$star' => $star,
554                                         '$drop' => $drop,
555                                         '$vote' => $likebuttons,
556                                         '$like' => $like,
557                                         '$dislike' => $dislike,
558                                         '$comment' => $comment,
559                                         '$wait' => t('Please wait'),
560
561                                 ));
562
563
564                                 $arr = array('item' => $item, 'output' => $tmp_item);
565                                 call_hooks('display_item', $arr);
566
567                                 $threads[$threadsid] .= $arr['output'];
568                         }
569                 }
570         }
571
572
573         // if author collapsing is in force but didn't get closed, close it off now.
574
575         /*if($blowhard_count >= 3)
576                 $threads[$threadsid] .= '</div>';*/
577
578         $page_template = get_markup_template("conversation.tpl");
579         $o .= replace_macros($page_template, array(
580                 '$threads' => $threads,
581                 '$dropping' => ($dropping?t('Delete Selected Items'):False),
582         ));
583
584         return $o;
585
586
587 function best_link_url($item,&$sparkle) {
588
589         $a = get_app();
590
591         $best_url = '';
592         $sparkle  = false;
593
594         $clean_url = normalise_link($item['author-link']);
595
596         if((local_user()) && (local_user() == $item['uid'])) {
597                 if(isset($a->contacts) && x($a->contacts,$clean_url)) {
598                         if($a->contacts[$clean_url]['network'] === NETWORK_DFRN) {
599                                 $best_url = $a->get_baseurl() . '/redir/' . $a->contacts[$clean_url]['id'];
600                                 $sparkle = true;
601                         }
602                         else
603                                 $best_url = $a->contacts[$clean_url]['url'];
604                 }
605         }
606         if(! $best_url) {
607                 if(strlen($item['author-link']))
608                         $best_url = $item['author-link'];
609                 else
610                         $best_url = $item['url'];
611         }
612
613         return $best_url;
614 }
615
616
617 if(! function_exists('item_photo_menu')){
618 function item_photo_menu($item){
619         $a = get_app();
620         
621         if (local_user() && (! count($a->contacts)))
622                 load_contact_links(local_user());
623
624         $contact_url="";
625         $pm_url="";
626         $status_link="";
627         $photos_link="";
628         $posts_link="";
629
630         $sparkle = false;
631     $profile_link = best_link_url($item,$sparkle);
632         if($profile_link === 'mailbox')
633                 $profile_link = '';
634
635         if($sparkle) {
636                 $cid = intval(basename($profile_link));
637                 $status_link = $profile_link . "?url=status";
638                 $photos_link = $profile_link . "?url=photos";
639                 $profile_link = $profile_link . "?url=profile";
640                 $pm_url = $a->get_baseurl() . '/message/new/' . $cid;
641         }
642         else {
643                 if(local_user() && local_user() == $item['uid'] && link_compare($item['url'],$item['author-link'])) {
644                         $cid = $item['contact-id'];
645                 }               
646                 else {
647                         $cid = 0;
648                 }
649         }
650         if(($cid) && (! $item['self'])) {
651                 $contact_url = $a->get_baseurl() . '/contacts/' . $cid;
652                 $posts_link = $a->get_baseurl() . '/network/?cid=' . $cid;
653         }
654
655         $menu = Array(
656                 t("View status") => $status_link,
657                 t("View profile") => $profile_link,
658                 t("View photos") => $photos_link,               
659                 t("View recent") => $posts_link, 
660                 t("Edit contact") => $contact_url,
661                 t("Send PM") => $pm_url,
662         );
663         
664         
665         $args = array($item, &$menu);
666         
667         call_hooks('item_photo_menu', $args);
668         
669         $o = "";
670         foreach($menu as $k=>$v){
671                 if ($v!="") $o .= "<li><a href='$v'>$k</a></li>\n";
672         }
673         return $o;
674 }}
675
676 if(! function_exists('like_puller')) {
677 function like_puller($a,$item,&$arr,$mode) {
678
679         $url = '';
680         $sparkle = '';
681         $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
682
683         if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
684                 $url = $item['author-link'];
685                 if((local_user()) && (local_user() == $item['uid']) && ($item['network'] === 'dfrn') && (! $item['self']) && (link_compare($item['author-link'],$item['url']))) {
686                         $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
687                         $sparkle = ' class="sparkle" ';
688                 }
689                 if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
690                         $arr[$item['parent'] . '-l'] = array();
691                 if(! isset($arr[$item['parent']]))
692                         $arr[$item['parent']] = 1;
693                 else    
694                         $arr[$item['parent']] ++;
695                 $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
696         }
697         return;
698 }}
699
700 // Format the like/dislike text for a profile item
701 // $cnt = number of people who like/dislike the item
702 // $arr = array of pre-linked names of likers/dislikers
703 // $type = one of 'like, 'dislike'
704 // $id  = item id
705 // returns formatted text
706
707 if(! function_exists('format_like')) {
708 function format_like($cnt,$arr,$type,$id) {
709         $o = '';
710         if($cnt == 1)
711                 $o .= (($type === 'like') ? sprintf( t('%s likes this.'), $arr[0]) : sprintf( t('%s doesn\'t like this.'), $arr[0])) . EOL ;
712         else {
713                 $spanatts = 'class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');"';
714                 $o .= (($type === 'like') ? 
715                                         sprintf( t('<span  %1$s>%2$d people</span> like this.'), $spanatts, $cnt)
716                                          : 
717                                         sprintf( t('<span  %1$s>%2$d people</span> don\'t like this.'), $spanatts, $cnt) ); 
718                 $o .= EOL ;
719                 $total = count($arr);
720                 if($total >= MAX_LIKERS)
721                         $arr = array_slice($arr, 0, MAX_LIKERS - 1);
722                 if($total < MAX_LIKERS)
723                         $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
724                 $str = implode(', ', $arr);
725                 if($total >= MAX_LIKERS)
726                         $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
727                 $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str));
728                 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
729         }
730         return $o;
731 }}
732
733
734 function status_editor($a,$x, $notes_cid = 0) {
735
736         $o = '';
737                 
738         $geotag = (($x['allow_location']) ? get_markup_template('jot_geotag.tpl') : '');
739
740                 $tpl = get_markup_template('jot-header.tpl');
741         
742                 $a->page['htmlhead'] .= replace_macros($tpl, array(
743                         '$baseurl' => $a->get_baseurl(),
744                         '$geotag' => $geotag,
745                         '$nickname' => $x['nickname'],
746                         '$ispublic' => t('Visible to <strong>everybody</strong>'),
747                         '$linkurl' => t('Please enter a link URL:'),
748                         '$utubeurl' => t('Please enter a YouTube link:'),
749                         '$vidurl' => t("Please enter a video\x28.ogg\x29 link/URL:"),
750                         '$audurl' => t("Please enter an audio\x28.ogg\x29 link/URL:"),
751                         '$whereareu' => t('Where are you right now?'),
752                         '$title' => t('Enter a title for this item') 
753                 ));
754
755
756                 $tpl = get_markup_template("jot.tpl");
757                 
758                 $jotplugins = '';
759                 $jotnets = '';
760
761                 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
762
763                 $mail_enabled = false;
764                 $pubmail_enabled = false;
765
766                 if(($x['is_owner']) && (! $mail_disabled)) {
767                         $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
768                                 intval(local_user())
769                         );
770                         if(count($r)) {
771                                 $mail_enabled = true;
772                                 if(intval($r[0]['pubmail']))
773                                         $pubmail_enabled = true;
774                         }
775                 }
776
777                 if($mail_enabled) {
778                $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
779                         $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> '
780                 . t("Post to Email") . '</div>';
781                 }
782
783                 call_hooks('jot_tool', $jotplugins);
784                 call_hooks('jot_networks', $jotnets);
785
786                 if($notes_cid)
787                         $jotnets .= '<input type="hidden" name="contact_allow[]" value="' . $notes_cid .'" />';
788
789                 $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));        
790
791                 $o .= replace_macros($tpl,array(
792                         '$return_path' => $a->cmd,
793                         '$action' => 'item',
794                         '$share' => (($x['button']) ? $x['button'] : t('Share')),
795                         '$upload' => t('Upload photo'),
796                         '$attach' => t('Attach file'),
797                         '$weblink' => t('Insert web link'),
798                         '$youtube' => t('Insert YouTube video'),
799                         '$video' => t('Insert Vorbis [.ogg] video'),
800                         '$audio' => t('Insert Vorbis [.ogg] audio'),
801                         '$setloc' => t('Set your location'),
802                         '$noloc' => t('Clear browser location'),
803                         '$title' => t('Set title'),
804                         '$wait' => t('Please wait'),
805                         '$permset' => t('Permission settings'),
806                         '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
807                         '$content' => '',
808                         '$post_id' => '',
809                         '$baseurl' => $a->get_baseurl(),
810                         '$defloc' => $x['default_location'],
811                         '$visitor' => $x['visitor'],
812                         '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
813                         '$emailcc' => t('CC: email addresses'),
814                         '$public' => t('Public post'),
815                         '$jotnets' => $jotnets,
816                         '$emtitle' => t('Example: bob@example.com, mary@example.com'),
817                         '$lockstate' => $x['lockstate'],
818                         '$acl' => $x['acl'],
819                         '$bang' => $x['bang'],
820                         '$profile_uid' => $x['profile_uid'],
821                 ));
822
823         return $o;
824 }