]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
quattro: in post display page, scroll to and flash selected comment
[friendica.git] / mod / contacts.php
1 <?php
2
3 require_once('include/Contact.php');
4 require_once('include/socgraph.php');
5
6 function contacts_init(&$a) {
7         if(! local_user())
8                 return;
9
10         $contact_id = 0;
11
12         if(($a->argc == 2) && intval($a->argv[1])) {
13                 $contact_id = intval($a->argv[1]);
14                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
15                         intval(local_user()),
16                         intval($contact_id)
17                 );
18                 if(! count($r)) {
19                         $contact_id = 0;
20                 }
21         }
22
23         require_once('include/group.php');
24         require_once('include/contact_widgets.php');
25
26         if(! x($a->page,'aside'))
27                 $a->page['aside'] = '';
28
29         if($contact_id) {
30                         $a->data['contact'] = $r[0];
31                         $o .= '<div class="vcard">';
32                         $o .= '<div class="fn">' . $a->data['contact']['name'] . '</div>';
33                         $o .= '<div id="profile-photo-wrapper"><img class="photo" style="width: 175px; height: 175px;" src="' . $a->data['contact']['photo'] . '" alt="' . $a->data['contact']['name'] . '" /></div>';
34                         $o .= '</div>';
35                         $a->page['aside'] .= $o;
36
37         }       
38         else
39                 $a->page['aside'] .= follow_widget();
40
41         $a->page['aside'] .= group_side('contacts','group',false,0,$contact_id);
42
43         $a->page['aside'] .= findpeople_widget();
44
45         $a->page['aside'] .= networks_widget('contacts',$_GET['nets']);
46         $base = $a->get_baseurl();
47
48         $a->page['htmlhead'] .= '<script src="' . $a->get_baseurl(true) . '/library/jquery_ac/friendica.complete.js" ></script>';
49         $a->page['htmlhead'] .= <<< EOT
50
51 <script>$(document).ready(function() { 
52         var a; 
53         a = $("#contacts-search").autocomplete({ 
54                 serviceUrl: '$base/acl',
55                 minChars: 2,
56                 width: 350,
57         });
58         a.setOptions({ params: { type: 'a' }});
59
60 }); 
61
62 </script>
63 EOT;
64
65
66 }
67
68 function contacts_post(&$a) {
69         
70         if(! local_user())
71                 return;
72
73         $contact_id = intval($a->argv[1]);
74         if(! $contact_id)
75                 return;
76
77         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
78                 intval($contact_id),
79                 intval(local_user())
80         );
81
82         if(! count($orig_record)) {
83                 notice( t('Could not access contact record.') . EOL);
84                 goaway($a->get_baseurl(true) . '/contacts');
85                 return; // NOTREACHED
86         }
87
88         call_hooks('contact_edit_post', $_POST);
89
90         $profile_id = intval($_POST['profile-assign']);
91         if($profile_id) {
92                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
93                         intval($profile_id),
94                         intval(local_user())
95                 );
96                 if(! count($r)) {
97                         notice( t('Could not locate selected profile.') . EOL);
98                         return;
99                 }
100         }
101
102         $hidden = intval($_POST['hidden']);
103
104         $priority = intval($_POST['poll']);
105         if($priority > 5 || $priority < 0)
106                 $priority = 0;
107
108         $info = fix_mce_lf(escape_tags(trim($_POST['info'])));
109
110         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
111                 `hidden` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
112                 intval($profile_id),
113                 intval($priority),
114                 dbesc($info),
115                 intval($hidden),
116                 intval($contact_id),
117                 intval(local_user())
118         );
119         if($r)
120                 info( t('Contact updated.') . EOL);
121         else
122                 notice( t('Failed to update contact record.') . EOL);
123
124         $r = q("select * from contact where id = %d and uid = %d limit 1",
125                 intval($contact_id),
126                 intval(local_user())
127         );
128         if($r && count($r))
129                 $a->data['contact'] = $r[0];
130
131         return;
132
133 }
134
135
136
137 function contacts_content(&$a) {
138
139         $sort_type = 0;
140         $o = '';
141         nav_set_selected('contacts');
142
143
144         if(! local_user()) {
145                 notice( t('Permission denied.') . EOL);
146                 return;
147         }
148
149         if($a->argc == 3) {
150
151                 $contact_id = intval($a->argv[1]);
152                 if(! $contact_id)
153                         return;
154
155                 $cmd = $a->argv[2];
156
157                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
158                         intval($contact_id),
159                         intval(local_user())
160                 );
161
162                 if(! count($orig_record)) {
163                         notice( t('Could not access contact record.') . EOL);
164                         goaway($a->get_baseurl(true) . '/contacts');
165                         return; // NOTREACHED
166                 }
167                 
168                 if($cmd === 'update') {
169
170                         // pull feed and consume it, which should subscribe to the hub.
171                         proc_run('php',"include/poller.php","$contact_id");
172                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
173                         // NOTREACHED
174                 }
175
176                 if($cmd === 'block') {
177                         $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
178                         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
179                                 intval($blocked),
180                                 intval($contact_id),
181                                 intval(local_user())
182                         );
183                         if($r) {
184                                 //notice( t('Contact has been ') . (($blocked) ? t('blocked') : t('unblocked')) . EOL );
185                                 info( (($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')) . EOL );
186                         }
187                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
188                         return; // NOTREACHED
189                 }
190
191                 if($cmd === 'ignore') {
192                         $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
193                         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
194                                 intval($readonly),
195                                 intval($contact_id),
196                                 intval(local_user())
197                         );
198                         if($r) {
199                                 info( (($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')) . EOL );
200                         }
201                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
202                         return; // NOTREACHED
203                 }
204
205
206                 if($cmd === 'archive') {
207                         $archived = (($orig_record[0]['archive']) ? 0 : 1);
208                         $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
209                                 intval($archived),
210                                 intval($contact_id),
211                                 intval(local_user())
212                         );
213                         if($r) {
214                                 //notice( t('Contact has been ') . (($archived) ? t('archived') : t('unarchived')) . EOL );
215                                 info( (($archived) ? t('Contact has been archived') : t('Contact has been unarchived')) . EOL );
216                         }
217                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
218                         return; // NOTREACHED
219                 }
220
221                 if($cmd === 'drop') {
222
223                         require_once('include/Contact.php');
224
225                         terminate_friendship($a->user,$a->contact,$orig_record[0]);
226
227                         contact_remove($orig_record[0]['id']);
228                         info( t('Contact has been removed.') . EOL );
229                         if(x($_SESSION,'return_url'))
230                                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
231                         else
232                                 goaway($a->get_baseurl(true) . '/contacts');
233                         return; // NOTREACHED
234                 }
235         }
236
237         if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
238
239                 $contact_id = $a->data['contact']['id'];
240                 $contact = $a->data['contact'];
241
242                 $editselect = 'exact';
243                 if(intval(get_pconfig(local_user(),'system','plaintext')))
244                         $editselect = 'none';
245
246                 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
247                         '$baseurl' => $a->get_baseurl(true),
248                         '$editselect' => $editselect,
249                 ));
250
251                 require_once('include/contact_selectors.php');
252
253                 $tpl = get_markup_template("contact_edit.tpl");
254
255                 switch($contact['rel']) {
256                         case CONTACT_IS_FRIEND:
257                                 $dir_icon = 'images/lrarrow.gif';
258                                 $relation_text = t('You are mutual friends with %s');
259                                 break;
260                         case CONTACT_IS_FOLLOWER;
261                                 $dir_icon = 'images/larrow.gif';
262                                 $relation_text = t('You are sharing with %s');
263                                 break;
264         
265                         case CONTACT_IS_SHARING;
266                                 $dir_icon = 'images/rarrow.gif';
267                                 $relation_text = t('%s is sharing with you');
268                                 break;
269                         default:
270                                 break;
271                 }
272
273                 $relation_text = sprintf($relation_text,$contact['name']);
274
275                 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
276                         $url = "redir/{$contact['id']}";
277                         $sparkle = ' class="sparkle" ';
278                 }
279                 else { 
280                         $url = $contact['url'];
281                         $sparkle = '';
282                 }
283
284                 $insecure = t('Private communications are not available for this contact.');
285
286                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00') 
287                                 ? t('Never') 
288                                 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
289
290                 if($contact['last-update'] !== '0000-00-00 00:00:00')
291                         $last_update .= ' ' . (($contact['last-update'] == $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
292
293                 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
294
295                 $poll_enabled = (($contact['network'] !== NETWORK_DIASPORA) ? true : false);
296
297                 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
298
299                 $common = count_common_friends(local_user(),$contact['id']);
300                 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
301
302                 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : ''); 
303
304                 $x = count_all_friends(local_user(), $contact['id']);
305                 $all_friends = (($x) ? t('View all contacts') : '');
306
307                 // tabs
308                 $tabs = array(
309                         array(
310                                 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
311                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
312                                 'sel'   => '',
313                                 'title' => t('Toggle Blocked status'),
314                         ),
315                         array(
316                                 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
317                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
318                                 'sel'   => '',
319                                 'title' => t('Toggle Ignored status'),
320                         ),
321
322                         array(
323                                 'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
324                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
325                                 'sel'   => '',
326                                 'title' => t('Toggle Archive status'),
327                         ),
328                         array(
329                                 'label' => t('Repair'),
330                                 'url'   => $a->get_baseurl(true) . '/crepair/' . $contact_id,
331                                 'sel'   => '',
332                                 'title' => t('Advanced Contact Settings'),
333                         )
334                 );
335                 $tab_tpl = get_markup_template('common_tabs.tpl');
336                 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
337
338                 $lost_contact = (($contact['archive'] && $contact['term-date'] != '0000-00-00 00:00:00' && $contact['term-date'] < datetime_convert('','','now')) ? t('Communications lost with this contact!') : '');
339
340                 $o .= replace_macros($tpl,array(
341                         '$header' => t('Contact Editor'),
342                         '$tab_str' => $tab_str,
343                         '$submit' => t('Submit'),
344                         '$lbl_vis1' => t('Profile Visibility'),
345                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
346                         '$lbl_info1' => t('Contact Information / Notes'),
347                         '$infedit' => t('Edit contact notes'),
348                         '$common_text' => $common_text,
349                         '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
350                         '$all_friends' => $all_friends,
351                         '$relation_text' => $relation_text,
352                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
353                         '$blockunblock' => t('Block/Unblock contact'),
354                         '$ignorecont' => t('Ignore contact'),
355                         '$lblcrepair' => t("Repair URL settings"),
356                         '$lblrecent' => t('View conversations'),
357                         '$lblsuggest' => $lblsuggest,
358                         '$delete' => t('Delete contact'),
359                         '$nettype' => $nettype,
360                         '$poll_interval' => contact_poll_interval($contact['priority'],(! $poll_enabled)),
361                         '$poll_enabled' => $poll_enabled,
362                         '$lastupdtext' => t('Last update:'),
363                         '$lost_contact' => $lost_contact,
364                         '$updpub' => t('Update public posts'),
365                         '$last_update' => $last_update,
366                         '$udnow' => t('Update now'),
367                         '$profile_select' => contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false)),
368                         '$contact_id' => $contact['id'],
369                         '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
370                         '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
371                         '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
372                         '$info' => $contact['info'],
373                         '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
374                         '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
375                         '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
376                         '$hidden' => array('hidden', t('Hide this contact from others'), ($contact['hidden'] == 1), t('Replies/likes to your public posts <strong>may</strong> still be visible')),
377                         '$photo' => $contact['photo'],
378                         '$name' => $contact['name'],
379                         '$dir_icon' => $dir_icon,
380                         '$alt_text' => $alt_text,
381                         '$sparkle' => $sparkle,
382                         '$url' => $url
383
384                 ));
385
386                 $arr = array('contact' => $contact,'output' => $o);
387
388                 call_hooks('contact_edit', $arr);
389
390                 return $arr['output'];
391
392         }
393
394         $blocked = false;
395         $hidden = false;
396         $ignored = false;
397         $all = false;
398
399         $_SESSION['return_url'] = $a->query_string;
400
401         if(($a->argc == 2) && ($a->argv[1] === 'all')) {
402                 $sql_extra = '';
403                 $all = true;
404         }
405         elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
406                 $sql_extra = " AND `blocked` = 1 ";
407                 $blocked = true;
408         }
409         elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
410                 $sql_extra = " AND `hidden` = 1 ";
411                 $hidden = true;
412         }
413         elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
414                 $sql_extra = " AND `readonly` = 1 ";
415                 $ignored = true;
416         }
417         elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
418                 $sql_extra = " AND `archive` = 1 ";
419                 $archived = true;
420         }
421         else
422                 $sql_extra = " AND `blocked` = 0 ";
423
424         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
425         $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
426
427         $tabs = array(
428                 array(
429                         'label' => t('Suggestions'),
430                         'url'   => $a->get_baseurl(true) . '/suggest', 
431                         'sel'   => '',
432                         'title' => t('Suggest potential friends'),
433                 ),
434                 array(
435                         'label' => t('All Contacts'),
436                         'url'   => $a->get_baseurl(true) . '/contacts/all', 
437                         'sel'   => ($all) ? 'active' : '',
438                         'title' => t('Show all contacts'),
439                 ),
440                 array(
441                         'label' => t('Unblocked'),
442                         'url'   => $a->get_baseurl(true) . '/contacts',
443                         'sel'   => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
444                         'title' => t('Only show unblocked contacts'),
445                 ),
446
447                 array(
448                         'label' => t('Blocked'),
449                         'url'   => $a->get_baseurl(true) . '/contacts/blocked',
450                         'sel'   => ($blocked) ? 'active' : '',
451                         'title' => t('Only show blocked contacts'),
452                 ),
453
454                 array(
455                         'label' => t('Ignored'),
456                         'url'   => $a->get_baseurl(true) . '/contacts/ignored',
457                         'sel'   => ($ignored) ? 'active' : '',
458                         'title' => t('Only show ignored contacts'),
459                 ),
460
461                 array(
462                         'label' => t('Archived'),
463                         'url'   => $a->get_baseurl(true) . '/contacts/archived',
464                         'sel'   => ($archived) ? 'active' : '',
465                         'title' => t('Only show archived contacts'),
466                 ),
467
468                 array(
469                         'label' => t('Hidden'),
470                         'url'   => $a->get_baseurl(true) . '/contacts/hidden',
471                         'sel'   => ($hidden) ? 'active' : '',
472                         'title' => t('Only show hidden contacts'),
473                 ),
474
475         );
476
477         $tab_tpl = get_markup_template('common_tabs.tpl');
478         $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
479
480
481
482
483         if($search) {
484                 $search_hdr = $search;
485                 $search = dbesc($search.'*');
486         }
487         $sql_extra .= ((strlen($search)) ? " AND MATCH `name` AGAINST ('$search' IN BOOLEAN MODE) " : "");
488
489         if($nets)
490                 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
491  
492         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : ''); 
493
494         
495         $r = q("SELECT COUNT(*) AS `total` FROM `contact` 
496                 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
497                 intval($_SESSION['uid']));
498         if(count($r)) {
499                 $a->set_pager_total($r[0]['total']);
500                 $total = $r[0]['total'];
501         }
502
503
504
505         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ORDER BY `name` ASC LIMIT %d , %d ",
506                 intval($_SESSION['uid']),
507                 intval($a->pager['start']),
508                 intval($a->pager['itemspage'])
509         );
510
511         $contacts = array();
512
513         if(count($r)) {
514
515                 foreach($r as $rr) {
516
517                         switch($rr['rel']) {
518                                 case CONTACT_IS_FRIEND:
519                                         $dir_icon = 'images/lrarrow.gif';
520                                         $alt_text = t('Mutual Friendship');
521                                         break;
522                                 case  CONTACT_IS_FOLLOWER;
523                                         $dir_icon = 'images/larrow.gif';
524                                         $alt_text = t('is a fan of yours');
525                                         break;
526                                 case CONTACT_IS_SHARING;
527                                         $dir_icon = 'images/rarrow.gif';
528                                         $alt_text = t('you are a fan of');
529                                         break;
530                                 default:
531                                         break;
532                         }
533                         if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
534                                 $url = "redir/{$rr['id']}";
535                                 $sparkle = ' class="sparkle" ';
536                         }
537                         else { 
538                                 $url = $rr['url'];
539                                 $sparkle = '';
540                         }
541
542
543                         $contacts[] = array(
544                                 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
545                                 'edit_hover' => t('Edit contact'),
546                                 'photo_menu' => contact_photo_menu($rr),
547                                 'id' => $rr['id'],
548                                 'alt_text' => $alt_text,
549                                 'dir_icon' => $dir_icon,
550                                 'thumb' => $rr['thumb'], 
551                                 'name' => $rr['name'],
552                                 'username' => $rr['name'],
553                                 'sparkle' => $sparkle,
554                                 'itemurl' => $rr['url'],
555                                 'url' => $url,
556                                 'network' => network_to_name($rr['network']),
557                         );
558                 }
559
560                 
561
562         }
563         
564         $tpl = get_markup_template("contacts-template.tpl");
565         $o .= replace_macros($tpl,array(
566                 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
567                 '$tabs' => $t,
568                 '$total' => $total,
569                 '$search' => $search_hdr,
570                 '$desc' => t('Search your contacts'),
571                 '$finding' => (strlen($search) ? t('Finding: ') . "'" . $search . "'" : ""),
572                 '$submit' => t('Find'),
573                 '$cmd' => $a->cmd,
574                 '$contacts' => $contacts,
575                 '$paginate' => paginate($a),
576
577         )); 
578         
579         return $o;
580 }