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