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