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