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