]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
Merge pull request #1209 from FX7/master
[friendica.git] / mod / contacts.php
1 <?php
2
3 require_once('include/Contact.php');
4 require_once('include/socgraph.php');
5 require_once('include/contact_selectors.php');
6
7 function contacts_init(&$a) {
8         if(! local_user())
9                 return;
10
11         $contact_id = 0;
12
13         if(($a->argc == 2) && intval($a->argv[1])) {
14                 $contact_id = intval($a->argv[1]);
15                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
16                         intval(local_user()),
17                         intval($contact_id)
18                 );
19                 if(! count($r)) {
20                         $contact_id = 0;
21                 }
22         }
23
24         require_once('include/group.php');
25         require_once('include/contact_widgets.php');
26
27         if(! x($a->page,'aside'))
28                 $a->page['aside'] = '';
29
30         if($contact_id) {
31                         $a->data['contact'] = $r[0];
32                         $vcard_widget = replace_macros(get_markup_template("vcard-widget.tpl"),array(
33                                 '$name' => $a->data['contact']['name'],
34                                 '$photo' => $a->data['contact']['photo']
35                         ));
36                         $follow_widget = '';
37         }
38         else {
39                 $vcard_widget = '';
40                 if (isset($_GET['add']))
41                         $follow_widget = follow_widget($_GET['add']);
42                 else
43                         $follow_widget = follow_widget();
44         }
45
46         $groups_widget .= group_side('contacts','group',false,0,$contact_id);
47         $findpeople_widget .= findpeople_widget();
48         $networks_widget .= networks_widget('contacts',$_GET['nets']);
49         $a->page['aside'] .= replace_macros(get_markup_template("contacts-widget-sidebar.tpl"),array(
50                 '$vcard_widget' => $vcard_widget,
51                 '$follow_widget' => $follow_widget,
52                 '$groups_widget' => $groups_widget,
53                 '$findpeople_widget' => $findpeople_widget,
54                 '$networks_widget' => $networks_widget
55         ));
56
57         $base = $a->get_baseurl();
58         $tpl = get_markup_template("contacts-head.tpl");
59         $a->page['htmlhead'] .= replace_macros($tpl,array(
60                 '$baseurl' => $a->get_baseurl(true),
61                 '$base' => $base
62         ));
63
64         $tpl = get_markup_template("contacts-end.tpl");
65         $a->page['end'] .= replace_macros($tpl,array(
66                 '$baseurl' => $a->get_baseurl(true),
67                 '$base' => $base
68         ));
69
70
71 }
72
73 function contacts_batch_actions(&$a){
74         $contacts_id = $_POST['contact_batch'];
75         if (!is_array($contacts_id)) return;
76         
77         $orig_records = q("SELECT * FROM `contact` WHERE `id` IN (%s) AND `uid` = %d AND `self` = 0",
78                 implode(",", $contacts_id),
79                 intval(local_user())
80         );
81         
82         $count_actions=0;
83         foreach($orig_records as $orig_record) {
84                 $contact_id = $orig_record['id'];
85                 if (x($_POST, 'contacts_batch_update')) {
86                         _contact_update($contact_id);
87                         $count_actions++;
88                 }
89                 if (x($_POST, 'contacts_batch_block')) {
90                         $r  = _contact_block($contact_id, $orig_record);
91                         if ($r) $count_actions++;
92                 }
93                 if (x($_POST, 'contacts_batch_ignore')) {
94                         $r = _contact_ignore($contact_id, $orig_record);
95                         if ($r) $count_actions++;
96                 }
97                 if (x($_POST, 'contacts_batch_archive')) {
98                         $r = _contact_archive($contact_id, $orig_record);
99                         if ($r) $count_actions++;
100                 }
101                 if (x($_POST, 'contacts_batch_drop')) {
102                         _contact_drop($contact_id, $orig_record);
103                         $count_actions++;
104                 }
105         }
106         if ($count_actions>0) {
107                 info ( sprintf( tt("%d contact edited.", "%d contacts edited", $count_actions), $count_actions) );
108         }
109         
110         if(x($_SESSION,'return_url'))
111                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
112         else
113                 goaway($a->get_baseurl(true) . '/contacts');
114
115 }
116
117
118 function contacts_post(&$a) {
119
120         if(! local_user())
121                 return;
122
123         if ($a->argv[1]==="batch") {
124                 contacts_batch_actions($a);
125                 return;
126         }
127
128         $contact_id = intval($a->argv[1]);
129         if(! $contact_id)
130                 return;
131
132         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
133                 intval($contact_id),
134                 intval(local_user())
135         );
136
137         if(! count($orig_record)) {
138                 notice( t('Could not access contact record.') . EOL);
139                 goaway($a->get_baseurl(true) . '/contacts');
140                 return; // NOTREACHED
141         }
142
143         call_hooks('contact_edit_post', $_POST);
144
145         $profile_id = intval($_POST['profile-assign']);
146         if($profile_id) {
147                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
148                         intval($profile_id),
149                         intval(local_user())
150                 );
151                 if(! count($r)) {
152                         notice( t('Could not locate selected profile.') . EOL);
153                         return;
154                 }
155         }
156
157         $hidden = intval($_POST['hidden']);
158
159         $notify = intval($_POST['notify']);
160
161         $fetch_further_information = intval($_POST['fetch_further_information']);
162
163         $priority = intval($_POST['poll']);
164         if($priority > 5 || $priority < 0)
165                 $priority = 0;
166
167         $info = fix_mce_lf(escape_tags(trim($_POST['info'])));
168
169         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
170                 `hidden` = %d, `notify_new_posts` = %d, `fetch_further_information` = %d WHERE `id` = %d AND `uid` = %d",
171                 intval($profile_id),
172                 intval($priority),
173                 dbesc($info),
174                 intval($hidden),
175                 intval($notify),
176                 intval($fetch_further_information),
177                 intval($contact_id),
178                 intval(local_user())
179         );
180         if($r)
181                 info( t('Contact updated.') . EOL);
182         else
183                 notice( t('Failed to update contact record.') . EOL);
184
185         $r = q("select * from contact where id = %d and uid = %d limit 1",
186                 intval($contact_id),
187                 intval(local_user())
188         );
189         if($r && count($r))
190                 $a->data['contact'] = $r[0];
191
192         return;
193
194 }
195
196 /*contact actions*/
197 function _contact_update($contact_id) {
198         // pull feed and consume it, which should subscribe to the hub.
199         proc_run('php',"include/poller.php","$contact_id"); 
200 }
201 function _contact_block($contact_id, $orig_record) {
202         $blocked = (($orig_record['blocked']) ? 0 : 1);
203         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d",
204                 intval($blocked),
205                 intval($contact_id),
206                 intval(local_user())
207         );
208         return $r;
209
210 }
211 function _contact_ignore($contact_id, $orig_record) {
212         $readonly = (($orig_record['readonly']) ? 0 : 1);
213         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d",
214                 intval($readonly),
215                 intval($contact_id),
216                 intval(local_user())
217         );
218         return $r;
219 }
220 function _contact_archive($contact_id, $orig_record) {
221         $archived = (($orig_record['archive']) ? 0 : 1);
222         $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d",
223                 intval($archived),
224                 intval($contact_id),
225                 intval(local_user())
226         );
227         if ($archived) {
228                 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact_id), intval(local_user()));
229         }
230         return $r;
231 }
232 function _contact_drop($contact_id, $orig_record) {
233         require_once('include/Contact.php');
234         $a = get_app();
235
236         terminate_friendship($a->user,$a->contact,$orig_record);
237         contact_remove($orig_record['id']);
238 }
239
240
241 function contacts_content(&$a) {
242
243         $sort_type = 0;
244         $o = '';
245         nav_set_selected('contacts');
246
247
248         if(! local_user()) {
249                 notice( t('Permission denied.') . EOL);
250                 return;
251         }
252
253         if($a->argc == 3) {
254
255                 $contact_id = intval($a->argv[1]);
256                 if(! $contact_id)
257                         return;
258
259                 $cmd = $a->argv[2];
260
261                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
262                         intval($contact_id),
263                         intval(local_user())
264                 );
265
266                 if(! count($orig_record)) {
267                         notice( t('Could not access contact record.') . EOL);
268                         goaway($a->get_baseurl(true) . '/contacts');
269                         return; // NOTREACHED
270                 }
271
272                 if($cmd === 'update') {
273                         _contact_update($contact_id);
274                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
275                         // NOTREACHED
276                 }
277
278                 if($cmd === 'block') {
279                         $r = _contact_block($contact_id, $orig_record[0]);
280                         if($r) {
281                                 $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
282                                 info((($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')).EOL);
283                         }
284
285                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
286                         return; // NOTREACHED
287                 }
288
289                 if($cmd === 'ignore') {
290                         $r = _contact_ignore($contact_id, $orig_record[0]);
291                         if($r) {
292                                 $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
293                                 info((($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')).EOL);
294                         }
295
296                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
297                         return; // NOTREACHED
298                 }
299
300
301                 if($cmd === 'archive') {
302                         $r = _contact_archive($contact_id, $orig_record[0]);
303                         if($r) {
304                                 $archived = (($orig_record[0]['archive']) ? 0 : 1);
305                                 info((($archived) ? t('Contact has been archived') : t('Contact has been unarchived')).EOL);
306                         }
307
308                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
309                         return; // NOTREACHED
310                 }
311
312                 if($cmd === 'drop') {
313
314                         // Check if we should do HTML-based delete confirmation
315                         if($_REQUEST['confirm']) {
316                                 // <form> can't take arguments in its "action" parameter
317                                 // so add any arguments as hidden inputs
318                                 $query = explode_querystring($a->query_string);
319                                 $inputs = array();
320                                 foreach($query['args'] as $arg) {
321                                         if(strpos($arg, 'confirm=') === false) {
322                                                 $arg_parts = explode('=', $arg);
323                                                 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
324                                         }
325                                 }
326
327                                 $a->page['aside'] = '';
328                                 return replace_macros(get_markup_template('confirm.tpl'), array(
329                                         '$method' => 'get',
330                                         '$message' => t('Do you really want to delete this contact?'),
331                                         '$extra_inputs' => $inputs,
332                                         '$confirm' => t('Yes'),
333                                         '$confirm_url' => $query['base'],
334                                         '$confirm_name' => 'confirmed',
335                                         '$cancel' => t('Cancel'),
336                                 ));
337                         }
338                         // Now check how the user responded to the confirmation query
339                         if($_REQUEST['canceled']) {
340                                 if(x($_SESSION,'return_url'))
341                                         goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
342                                 else
343                                         goaway($a->get_baseurl(true) . '/contacts');
344                         }
345
346                         _contact_drop($contact_id, $orig_record[0]);
347                         info( t('Contact has been removed.') . EOL );
348                         if(x($_SESSION,'return_url'))
349                                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
350                         else
351                                 goaway($a->get_baseurl(true) . '/contacts');
352                         return; // NOTREACHED
353                 }
354         }
355
356
357
358         $_SESSION['return_url'] = $a->query_string;
359
360         if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
361
362                 $contact_id = $a->data['contact']['id'];
363                 $contact = $a->data['contact'];
364
365                 $editselect = 'none';
366                 if( feature_enabled(local_user(),'richtext') )
367                         $editselect = 'exact';
368
369                 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
370                         '$baseurl' => $a->get_baseurl(true),
371                         '$editselect' => $editselect,
372                 ));
373                 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
374                         '$baseurl' => $a->get_baseurl(true),
375                         '$editselect' => $editselect,
376                 ));
377
378                 require_once('include/contact_selectors.php');
379
380                 $tpl = get_markup_template("contact_edit.tpl");
381
382                 switch($contact['rel']) {
383                         case CONTACT_IS_FRIEND:
384                                 $dir_icon = 'images/lrarrow.gif';
385                                 $relation_text = t('You are mutual friends with %s');
386                                 break;
387                         case CONTACT_IS_FOLLOWER;
388                                 $dir_icon = 'images/larrow.gif';
389                                 $relation_text = t('You are sharing with %s');
390                                 break;
391         
392                         case CONTACT_IS_SHARING;
393                                 $dir_icon = 'images/rarrow.gif';
394                                 $relation_text = t('%s is sharing with you');
395                                 break;
396                         default:
397                                 break;
398                 }
399
400                 $relation_text = sprintf($relation_text,$contact['name']);
401
402                 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
403                         $url = "redir/{$contact['id']}";
404                         $sparkle = ' class="sparkle" ';
405                 }
406                 else { 
407                         $url = $contact['url'];
408                         $sparkle = '';
409                 }
410
411                 $insecure = t('Private communications are not available for this contact.');
412
413                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00') 
414                                 ? t('Never') 
415                                 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
416
417                 if($contact['last-update'] !== '0000-00-00 00:00:00')
418                         $last_update .= ' ' . (($contact['last-update'] == $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
419
420                 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
421
422                 $poll_enabled = (($contact['network'] !== NETWORK_DIASPORA) ? true : false);
423
424                 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
425
426                 $common = count_common_friends(local_user(),$contact['id']);
427                 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
428
429                 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : ''); 
430
431                 $x = count_all_friends(local_user(), $contact['id']);
432                 $all_friends = (($x) ? t('View all contacts') : '');
433
434                 // tabs
435                 $tabs = array(
436                         array(
437                                 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
438                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
439                                 'sel'   => '',
440                                 'title' => t('Toggle Blocked status'),
441                         ),
442                         array(
443                                 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
444                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
445                                 'sel'   => '',
446                                 'title' => t('Toggle Ignored status'),
447                         ),
448
449                         array(
450                                 'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
451                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
452                                 'sel'   => '',
453                                 'title' => t('Toggle Archive status'),
454                         ),
455                         array(
456                                 'label' => t('Repair'),
457                                 'url'   => $a->get_baseurl(true) . '/crepair/' . $contact_id,
458                                 'sel'   => '',
459                                 'title' => t('Advanced Contact Settings'),
460                         )
461                 );
462                 $tab_tpl = get_markup_template('common_tabs.tpl');
463                 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
464
465                 $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!') : '');
466
467                 $o .= replace_macros($tpl, array(
468                         '$header' => t('Contact Editor'),
469                         '$tab_str' => $tab_str,
470                         '$submit' => t('Submit'),
471                         '$lbl_vis1' => t('Profile Visibility'),
472                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
473                         '$lbl_info1' => t('Contact Information / Notes'),
474                         '$infedit' => t('Edit contact notes'),
475                         '$common_text' => $common_text,
476                         '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
477                         '$all_friends' => $all_friends,
478                         '$relation_text' => $relation_text,
479                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
480                         '$blockunblock' => t('Block/Unblock contact'),
481                         '$ignorecont' => t('Ignore contact'),
482                         '$lblcrepair' => t("Repair URL settings"),
483                         '$lblrecent' => t('View conversations'),
484                         '$lblsuggest' => $lblsuggest,
485                         '$delete' => t('Delete contact'),
486                         '$nettype' => $nettype,
487                         '$poll_interval' => contact_poll_interval($contact['priority'],(! $poll_enabled)),
488                         '$poll_enabled' => $poll_enabled,
489                         '$lastupdtext' => t('Last update:'),
490                         '$lost_contact' => $lost_contact,
491                         '$updpub' => t('Update public posts'),
492                         '$last_update' => $last_update,
493                         '$udnow' => t('Update now'),
494                         '$profile_select' => contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false)),
495                         '$contact_id' => $contact['id'],
496                         '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
497                         '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
498                         '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
499                         '$info' => $contact['info'],
500                         '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
501                         '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
502                         '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
503                         '$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')),
504                         '$notify' => array('notify', t('Notification for new posts'), ($contact['notify_new_posts'] == 1), t('Send a notification of every new post of this contact')),
505                         '$fetch_further_information' => array('fetch_further_information', t('Fetch further information for feeds'), $contact['fetch_further_information'], t('Fetch further information for feeds'),
506                                                                 array('0'=>t('Disabled'), '1'=>t('Fetch information'), '2'=>t('Fetch information and keywords'))),
507                         '$photo' => $contact['photo'],
508                         '$name' => $contact['name'],
509                         '$dir_icon' => $dir_icon,
510                         '$alt_text' => $alt_text,
511                         '$sparkle' => $sparkle,
512                         '$url' => $url,
513
514                 ));
515
516                 $arr = array('contact' => $contact,'output' => $o);
517
518                 call_hooks('contact_edit', $arr);
519
520                 return $arr['output'];
521
522         }
523
524         $blocked = false;
525         $hidden = false;
526         $ignored = false;
527         $all = false;
528
529         if(($a->argc == 2) && ($a->argv[1] === 'all')) {
530                 $sql_extra = '';
531                 $all = true;
532         }
533         elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
534                 $sql_extra = " AND `blocked` = 1 ";
535                 $blocked = true;
536         }
537         elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
538                 $sql_extra = " AND `hidden` = 1 ";
539                 $hidden = true;
540         }
541         elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
542                 $sql_extra = " AND `readonly` = 1 ";
543                 $ignored = true;
544         }
545         elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
546                 $sql_extra = " AND `archive` = 1 ";
547                 $archived = true;
548         }
549         else
550                 $sql_extra = " AND `blocked` = 0 ";
551
552         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
553         $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
554
555         $tabs = array(
556                 array(
557                         'label' => t('Suggestions'),
558                         'url'   => $a->get_baseurl(true) . '/suggest', 
559                         'sel'   => '',
560                         'title' => t('Suggest potential friends'),
561                 ),
562                 array(
563                         'label' => t('All Contacts'),
564                         'url'   => $a->get_baseurl(true) . '/contacts/all', 
565                         'sel'   => ($all) ? 'active' : '',
566                         'title' => t('Show all contacts'),
567                 ),
568                 array(
569                         'label' => t('Unblocked'),
570                         'url'   => $a->get_baseurl(true) . '/contacts',
571                         'sel'   => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
572                         'title' => t('Only show unblocked contacts'),
573                 ),
574
575                 array(
576                         'label' => t('Blocked'),
577                         'url'   => $a->get_baseurl(true) . '/contacts/blocked',
578                         'sel'   => ($blocked) ? 'active' : '',
579                         'title' => t('Only show blocked contacts'),
580                 ),
581
582                 array(
583                         'label' => t('Ignored'),
584                         'url'   => $a->get_baseurl(true) . '/contacts/ignored',
585                         'sel'   => ($ignored) ? 'active' : '',
586                         'title' => t('Only show ignored contacts'),
587                 ),
588
589                 array(
590                         'label' => t('Archived'),
591                         'url'   => $a->get_baseurl(true) . '/contacts/archived',
592                         'sel'   => ($archived) ? 'active' : '',
593                         'title' => t('Only show archived contacts'),
594                 ),
595
596                 array(
597                         'label' => t('Hidden'),
598                         'url'   => $a->get_baseurl(true) . '/contacts/hidden',
599                         'sel'   => ($hidden) ? 'active' : '',
600                         'title' => t('Only show hidden contacts'),
601                 ),
602
603         );
604
605         $tab_tpl = get_markup_template('common_tabs.tpl');
606         $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
607
608
609
610         $searching = false;
611         if($search) {
612                 $search_hdr = $search;
613                 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
614                 $searching = true;
615         }
616         $sql_extra .= (($searching) ? " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt'  OR nick REGEXP '$search_txt') " : "");
617
618         if($nets)
619                 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
620  
621         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : ''); 
622
623         
624         $r = q("SELECT COUNT(*) AS `total` FROM `contact` 
625                 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
626                 intval($_SESSION['uid']));
627         if(count($r)) {
628                 $a->set_pager_total($r[0]['total']);
629                 $total = $r[0]['total'];
630         }
631
632
633         $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 ",
634                 intval($_SESSION['uid']),
635                 intval($a->pager['start']),
636                 intval($a->pager['itemspage'])
637         );
638
639         $contacts = array();
640
641         if(count($r)) {
642
643                 foreach($r as $rr) {
644
645                         switch($rr['rel']) {
646                                 case CONTACT_IS_FRIEND:
647                                         $dir_icon = 'images/lrarrow.gif';
648                                         $alt_text = t('Mutual Friendship');
649                                         break;
650                                 case  CONTACT_IS_FOLLOWER;
651                                         $dir_icon = 'images/larrow.gif';
652                                         $alt_text = t('is a fan of yours');
653                                         break;
654                                 case CONTACT_IS_SHARING;
655                                         $dir_icon = 'images/rarrow.gif';
656                                         $alt_text = t('you are a fan of');
657                                         break;
658                                 default:
659                                         break;
660                         }
661                         if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
662                                 $url = "redir/{$rr['id']}";
663                                 $sparkle = ' class="sparkle" ';
664                         }
665                         else { 
666                                 $url = $rr['url'];
667                                 $sparkle = '';
668                         }
669
670
671                         $contacts[] = array(
672                                 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
673                                 'edit_hover' => t('Edit contact'),
674                                 'photo_menu' => contact_photo_menu($rr),
675                                 'id' => $rr['id'],
676                                 'alt_text' => $alt_text,
677                                 'dir_icon' => $dir_icon,
678                                 'thumb' => $rr['thumb'], 
679                                 'name' => $rr['name'],
680                                 'username' => $rr['name'],
681                                 'sparkle' => $sparkle,
682                                 'itemurl' => $rr['url'],
683                                 'url' => $url,
684                                 'network' => network_to_name($rr['network']),
685                         );
686                 }
687
688
689
690         }
691
692         $tpl = get_markup_template("contacts-template.tpl");
693         $o .= replace_macros($tpl, array(
694                 '$baseurl' => $a->get_baseurl(),
695                 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
696                 '$tabs' => $t,
697                 '$total' => $total,
698                 '$search' => $search_hdr,
699                 '$desc' => t('Search your contacts'),
700                 '$finding' => (($searching) ? t('Finding: ') . "'" . $search . "'" : ""),
701                 '$submit' => t('Find'),
702                 '$cmd' => $a->cmd,
703                 '$contacts' => $contacts,
704                 '$contact_drop_confirm' => t('Do you really want to delete this contact?'),
705                 '$batch_actions' => array(
706                         'contacts_batch_update' => t('Update'),
707                         'contacts_batch_block' => t('Block')."/".t("Unblock"),
708                         "contacts_batch_ignore" => t('Ignore')."/".t("Unignore"),
709                         "contacts_batch_archive" => t('Archive')."/".t("Unarchive"),
710                         "contacts_batch_drop" => t('Delete'),
711                 ),
712                 '$paginate' => paginate($a),
713
714         )); 
715         
716         return $o;
717 }