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