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