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