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