]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
29ef8a8cd76a24d2d7e416745430f18166a77ac9
[friendica.git] / mod / contacts.php
1 <?php
2
3 require_once('include/Contact.php');
4
5 function contacts_init(&$a) {
6         require_once('include/group.php');
7         if(! x($a->page,'aside'))
8                 $a->page['aside'] = '';
9         $a->page['aside'] .= group_side();
10
11         if($a->config['register_policy'] != REGISTER_CLOSED)
12                 $a->page['aside'] .= '<div class="side-invite-link-wrapper" id="side-invite-link-wrapper" ><a href="invite" class="side-invite-link" id="side-invite-link">' . t("Invite Friends") . '</a></div>';
13
14         $tpl = load_view_file('view/follow.tpl');
15         $a->page['aside'] .= replace_macros($tpl,array(
16                 '$label' => t('Connect/Follow [profile address]'),
17                 '$hint' => t('Example: bob@example.com, http://example.com/barbara'),
18                 '$follow' => t('Follow')
19         ));
20
21 }
22
23 function contacts_post(&$a) {
24         
25         if(! local_user())
26                 return;
27
28         $contact_id = intval($a->argv[1]);
29         if(! $contact_id)
30                 return;
31
32         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
33                 intval($contact_id),
34                 intval(local_user())
35         );
36
37         if(! count($orig_record)) {
38                 notice( t('Could not access contact record.') . EOL);
39                 goaway($a->get_baseurl() . '/contacts');
40                 return; // NOTREACHED
41         }
42
43         $profile_id = intval($_POST['profile-assign']);
44         if($profile_id) {
45                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
46                         intval($profile_id),
47                         intval(local_user())
48                 );
49                 if(! count($r)) {
50                         notice( t('Could not locate selected profile.') . EOL);
51                         return;
52                 }
53         }
54
55
56         $priority = intval($_POST['priority']);
57         if($priority == (-1))
58                 
59         if($priority > 5 || $priority < 0)
60                 $priority = 0;
61
62         $rating = intval($_POST['reputation']);
63         if($rating > 5 || $rating < 0)
64                 $rating = 0;
65
66         $reason = notags(trim($_POST['reason']));
67
68         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `rating` = %d, `reason` = '%s'
69                 WHERE `id` = %d AND `uid` = %d LIMIT 1",
70                 intval($profile_id),
71                 intval($priority),
72                 intval($rating),
73                 dbesc($reason),
74                 intval($contact_id),
75                 intval(local_user())
76         );
77         if($r)
78                 notice( t('Contact updated.') . EOL);
79         else
80                 notice( t('Failed to update contact record.') . EOL);
81         return;
82
83 }
84
85
86
87 function contacts_content(&$a) {
88
89         $sort_type = 0;
90         $o = '';
91         $o .= '<script> $(document).ready(function() { $(\'#nav-contacts-link\').addClass(\'nav-selected\'); });</script>';
92
93         $_SESSION['return_url'] = $a->get_baseurl() . '/' . $a->cmd;
94
95         if(! local_user()) {
96                 notice( t('Permission denied.') . EOL);
97                 return;
98         }
99
100         if($a->argc == 3) {
101
102                 $contact_id = intval($a->argv[1]);
103                 if(! $contact_id)
104                         return;
105
106                 $cmd = $a->argv[2];
107
108                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
109                         intval($contact_id),
110                         intval(local_user())
111                 );
112
113                 if(! count($orig_record)) {
114                         notice( t('Could not access contact record.') . EOL);
115                         goaway($a->get_baseurl() . '/contacts');
116                         return; // NOTREACHED
117                 }
118
119
120                 if($cmd === 'block') {
121                         $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
122                         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
123                                         intval($blocked),
124                                         intval($contact_id),
125                                         intval(local_user())
126                         );
127                         if($r) {
128                                 notice( t('Contact has been ') . (($blocked) ? t('blocked') : t('unblocked')) . EOL );
129                         }
130                         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
131                         return; // NOTREACHED
132                 }
133
134                 if($cmd === 'ignore') {
135                         $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
136                         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
137                                         intval($readonly),
138                                         intval($contact_id),
139                                         intval(local_user())
140                         );
141                         if($r) {
142                                 notice( t('Contact has been ') . (($readonly) ? t('ignored') : t('unignored')) . EOL );
143                         }
144                         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
145                         return; // NOTREACHED
146                 }
147
148                 if($cmd === 'drop') {
149
150                         // create an unfollow slap
151
152                         if($orig_record[0]['network'] === 'stat') {
153                                 $tpl = load_view_file('view/follow_slap.tpl');
154                                 $slap = replace_macros($tpl, array(
155                                         '$name' => $a->user['username'],
156                                         '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
157                                         '$photo' => $a->contact['photo'],
158                                         '$thumb' => $a->contact['thumb'],
159                                         '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
160                                         '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':unfollow:' . random_string(),
161                                         '$title' => '',
162                                         '$type' => 'text',
163                                         '$content' => t('stopped following'),
164                                         '$nick' => $a->user['nickname'],
165                                         '$verb' => ACTIVITY_UNFOLLOW
166                                 ));
167
168                                 if((x($orig_record[0],'notify')) && (strlen($orig_record[0]['notify']))) {
169                                         require_once('include/salmon.php');
170                                         slapper($a->user,$orig_record[0]['notify'],$slap);
171                                 }
172                         }
173
174                         contact_remove($contact_id);
175                         notice( t('Contact has been removed.') . EOL );
176                         goaway($a->get_baseurl() . '/contacts');
177                         return; // NOTREACHED
178                 }
179         }
180
181         if(($a->argc == 2) && intval($a->argv[1])) {
182
183                 $contact_id = intval($a->argv[1]);
184                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
185                         intval(local_user()),
186                         intval($contact_id)
187                 );
188                 if(! count($r)) {
189                         notice( t('Contact not found.') . EOL);
190                         return;
191                 }
192
193
194                 require_once('include/contact_selectors.php');
195
196                 $tpl = load_view_file("view/contact_edit.tpl");
197
198                 switch($r[0]['rel']) {
199                         case REL_BUD:
200                                 $dir_icon = 'images/lrarrow.gif';
201                                 $alt_text = t('Mutual Friendship');
202                                 break;
203                         case REL_VIP;
204                                 $dir_icon = 'images/larrow.gif';
205                                 $alt_text = t('is a fan of yours');
206                                 break;
207         
208                         case REL_FAN;
209                                 $dir_icon = 'images/rarrow.gif';
210                                 $alt_text = t('you are a fan of');
211                                 break;
212                         default:
213                                 break;
214                 }
215
216                 if(($r[0]['network'] === 'dfrn') && ($r[0]['rel'])) {
217                         $url = "redir/{$r[0]['id']}";
218                         $sparkle = ' class="sparkle" ';
219                 }
220                 else { 
221                         $url = $r[0]['url'];
222                         $sparkle = '';
223                 }
224
225                 $o .= replace_macros($tpl,array(
226                         '$header' => t('Contact Editor'),
227                         '$visit' => t('Visit $name\'s profile'),
228                         '$blockunblock' => t('Block/Unblock contact'),
229                         '$ignorecont' => t('Ignore contact'),
230                         '$delete' => t('Delete contact'),
231                         '$poll_interval' => contact_poll_interval($r[0]['priority']),
232                         '$lastupdtext' => t('Last updated: '),
233                         '$updpub' => t('Update public posts: '),
234                         '$last_update' => (($r[0]['last-update'] == '0000-00-00 00:00:00') 
235                                 ? t('Never') 
236                                 : datetime_convert('UTC',date_default_timezone_get(),$r[0]['last-update'],'D, j M Y, g:i A')),
237                         '$profile_select' => contact_profile_assign($r[0]['profile-id'],(($r[0]['network'] !== 'dfrn') ? true : false)),
238                         '$contact_id' => $r[0]['id'],
239                         '$block_text' => (($r[0]['blocked']) ? t('Unblock this contact') : t('Block this contact') ),
240                         '$ignore_text' => (($r[0]['readonly']) ? t('Unignore this contact') : t('Ignore this contact') ),
241                         '$insecure' => (($r[0]['network'] === 'dfrn') ? '' : load_view_file('view/insecure_net.tpl')),
242                         '$blocked' => (($r[0]['blocked']) ? '<div id="block-message">' . t('Currently blocked') . '</div>' : ''),
243                         '$ignored' => (($r[0]['readonly']) ? '<div id="ignore-message">' . t('Currently ignored') . '</div>' : ''),
244                         '$rating' => contact_reputation($r[0]['rating']),
245                         '$reason' => $r[0]['reason'],
246                         '$groups' => '', // group_selector(),
247                         '$photo' => $r[0]['photo'],
248                         '$name' => $r[0]['name'],
249                         '$dir_icon' => $dir_icon,
250                         '$alt_text' => $alt_text,
251                         '$sparkle' => $sparkle,
252                         '$url' => $url
253
254                 ));
255
256                 return $o;
257
258         }
259
260
261         if(($a->argc == 2) && ($a->argv[1] === 'all'))
262                 $sql_extra = '';
263         else
264                 $sql_extra = " AND `blocked` = 0 ";
265
266         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
267
268         $tpl = load_view_file("view/contacts-top.tpl");
269         $o .= replace_macros($tpl,array(
270                 '$header' => t('Contacts'),
271                 '$hide_url' => ((strlen($sql_extra)) ? 'contacts/all' : 'contacts' ),
272                 '$hide_text' => ((strlen($sql_extra)) ? t('Show Blocked Connections') : t('Hide Blocked Connections')),
273                 '$search' => $search,
274                 '$finding' => (strlen($search) ? '<h4>' . t('Finding: ') . "'" . $search . "'" . '</h4>' : ""),
275                 '$submit' => t('Find'),
276                 '$cmd' => $a->cmd
277
278
279         )); 
280
281         if($search)
282                 $search = dbesc($search.'*');
283         $sql_extra .= ((strlen($search)) ? " AND MATCH `name` AGAINST ('$search' IN BOOLEAN MODE) " : "");
284
285         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= REL_BUD)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : ''); 
286
287         
288         $r = q("SELECT COUNT(*) AS `total` FROM `contact` 
289                 WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ",
290                 intval($_SESSION['uid']));
291         if(count($r))
292                 $a->set_pager_total($r[0]['total']);
293
294         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ORDER BY `name` ASC LIMIT %d , %d ",
295                 intval($_SESSION['uid']),
296                 intval($a->pager['start']),
297                 intval($a->pager['itemspage'])
298         );
299
300         if(count($r)) {
301
302                 $tpl = load_view_file("view/contact_template.tpl");
303
304                 foreach($r as $rr) {
305                         if($rr['self'])
306                                 continue;
307
308                         switch($rr['rel']) {
309                                 case REL_BUD:
310                                         $dir_icon = 'images/lrarrow.gif';
311                                         $alt_text = t('Mutual Friendship');
312                                         break;
313                                 case  REL_VIP;
314                                         $dir_icon = 'images/larrow.gif';
315                                         $alt_text = t('is a fan of yours');
316                                         break;
317                                 case REL_FAN;
318                                         $dir_icon = 'images/rarrow.gif';
319                                         $alt_text = t('you are a fan of');
320                                         break;
321                                 default:
322                                         break;
323                         }
324                         if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
325                                 $url = "redir/{$rr['id']}";
326                                 $sparkle = ' class="sparkle" ';
327                         }
328                         else { 
329                                 $url = $rr['url'];
330                                 $sparkle = '';
331                         }
332
333
334                         $o .= replace_macros($tpl, array(
335                                 '$img_hover' => t('Visit ') . $rr['name'] . t('\'s profile'),
336                                 '$edit_hover' => t('Edit contact'),
337                                 '$id' => $rr['id'],
338                                 '$alt_text' => $alt_text,
339                                 '$dir_icon' => $dir_icon,
340                                 '$thumb' => $rr['thumb'], 
341                                 '$name' => $rr['name'],
342                                 '$sparkle' => $sparkle,
343                                 '$url' => $url
344                         ));
345                 }
346                 $o .= '<div id="contact-edit-end"></div>';
347
348         }
349         $o .= paginate($a);
350         return $o;
351 }