]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
585ac1423227b7d5d4878feaa6f5f1c1c73c324f
[friendica.git] / mod / contacts.php
1 <?php
2
3 function contacts_init(&$a) {
4         require_once('include/group.php');
5         $a->page['aside'] .= group_side();
6 }
7
8 function contacts_post(&$a) {
9
10         
11         if(! local_user())
12                 return;
13
14         $contact_id = intval($a->argv[1]);
15         if(! $contact_id)
16                 return;
17
18         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
19                 intval($contact_id),
20                 intval($_SESSION['uid'])
21         );
22
23         if(! count($orig_record)) {
24                 notice("Could not access contact record." . EOL);
25                 goaway($a->get_baseurl() . '/contacts');
26                 return; // NOTREACHED
27         }
28
29         $profile_id = intval($_POST['profile-assign']);
30         if($profile_id) {
31                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
32                         intval($profile_id),
33                         intval($_SESSION['uid'])
34                 );
35                 if(! count($r)) {
36                         notice("Cannot locate selected profile." . EOL);
37                         return;
38                 }
39         }
40         $rating = intval($_POST['reputation']);
41         if($rating > 5 || $rating < 0)
42                 $rating = 0;
43
44         $reason = notags(trim($_POST['reason']));
45
46         $r = q("UPDATE `contact` SET `profile-id` = %d, `rating` = %d, `reason` = '%s'
47                 WHERE `id` = %d AND `uid` = %d LIMIT 1",
48                 intval($profile_id),
49                 intval($rating),
50                 dbesc($reason),
51                 intval($contact_id),
52                 intval($_SESSION['uid'])
53         );
54         if($r)
55                 notice("Contact updated." . EOL);
56         else
57                 notice("Failed to update contact record." . EOL);
58         return;
59
60 }
61
62
63
64 function contacts_content(&$a) {
65
66         if(! local_user()) {
67                 $_SESSION['sysmsg'] .= "Permission denied." . EOL;
68                 return;
69         }
70
71         if($a->argc == 3) {
72
73                 $contact_id = intval($a->argv[1]);
74                 if(! $contact_id)
75                         return;
76
77                 $cmd = $a->argv[2];
78
79                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
80                         intval($contact_id),
81                         intval($_SESSION['uid'])
82                 );
83
84                 if(! count($orig_record)) {
85                         notice("Could not access contact record." . EOL);
86                         goaway($a->get_baseurl() . '/contacts');
87                         return; // NOTREACHED
88                 }
89
90
91                 $photo = str_replace('-4.jpg', '' , $r[0]['photo']);
92                 $photos = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d",
93                                 dbesc($photo),
94                                 intval($_SESSION['uid'])
95                 );
96         
97                 if($cmd == 'block') {
98                         $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
99                         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
100                                         intval($blocked),
101                                         intval($contact_id),
102                                         intval($_SESSION['uid'])
103                         );
104                         if($r) {
105                                 $msg = "Contact has been " . (($blocked) ? '' : 'un') . "blocked." . EOL ;
106                                 notice($msg);
107                         }
108                         goaway($a->get_baseurl() ."/contacts/$contact_id");
109                         return; // NOTREACHED
110                 }
111
112                 if($cmd == 'drop') {
113                         $r = q("DELETE FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
114                                 intval($contact_id),
115                                 intval($_SESSION['uid']));
116                         if(count($photos)) {
117                                 foreach($photos as $p) {
118                                         q("DELETE FROM `photos` WHERE `id` = %d LIMIT 1",
119                                                 $p['id']);
120                                 }
121                         }
122                         if($intval($contact_id))
123                                 q("DELETE FROM `item` WHERE `contact-id` = %d LIMIT 1",
124                                         intval($contact_id)
125                                 );
126         
127                         notice("Contact has been removed." . EOL );
128                         goaway($a->get_baseurl() . '/contacts');
129                         return; // NOTREACHED
130                 }
131         }
132
133         if(($a->argc == 2) && intval($a->argv[1])) {
134
135                 $contact_id = intval($a->argv[1]);
136                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
137                         $_SESSION['uid'],
138                         intval($contact_id)
139                 );
140                 if(! count($r)) {
141                         notice("Contact not found.");
142                         return;
143                 }
144
145                 require_once('view/contact_selectors.php');
146
147                 $tpl = file_get_contents("view/contact_edit.tpl");
148
149                 $direction = '';
150                 if(strlen($r[0]['issued-id'])) {
151                         if(strlen($r[0]['dfrn-id'])) {
152                                 $direction = DIRECTION_BOTH;
153                                 $dir_icon = 'images/lrarrow.gif';
154                                 $alt_text = 'Mutual Friendship';
155                         }
156                         else {
157                                 $direction = DIRECTION_IN;
158                                 $dir_icon = 'images/larrow.gif';
159                                 $alt_text = 'is a fan of yours';
160                         }
161                 }
162                 else {
163                         $direction = DIRECTION_OUT;
164                         $dir_icon = 'images/rarrow.gif';
165                         $alt_text = 'you are a fan of';
166                 }
167
168                 $o .= replace_macros($tpl,array(
169                         '$profile_select' => contact_profile_assign($r[0]['profile-id']),
170                         '$contact_id' => $r[0]['id'],
171                         '$block_text' => (($r[0]['blocked']) ? 'Unblock this contact' : 'Block this contact' ),
172                         '$blocked' => (($r[0]['blocked']) ? '<div id="block-message">Currently blocked</div>' : ''),
173                         '$rating' => contact_reputation($r[0]['rating']),
174                         '$reason' => $r[0]['reason'],
175                         '$groups' => '', // group_selector(),
176                         '$photo' => $r[0]['photo'],
177                         '$name' => $r[0]['name'],
178                         '$dir_icon' => $dir_icon,
179                         '$alt_text' => $alt_text,
180                         '$url' => (($direction != DIRECTION_IN) ? "redir/{$r[0]['id']}" : $r[0]['url'] )
181
182                 ));
183
184                 return $o;
185
186         }
187 dbg(2);
188         if(($a->argc == 2) && ($a->argv[1] == 'all'))
189                 $sql_extra = '';
190         else
191                 $sql_extra = " AND `blocked` = 0 ";
192
193         $tpl = file_get_contents("view/contacts-top.tpl");
194         $o .= replace_macros($tpl,array(
195                 '$hide_url' => ((strlen($sql_extra)) ? 'contacts/all' : 'contacts' ),
196                 '$hide_text' => ((strlen($sql_extra)) ? 'Show Blocked Connections' : 'Hide Blocked Connections')
197         )); 
198
199         switch($sort_type) {
200                 case DIRECTION_BOTH :
201                         $sql_extra2 = " AND `dfrn-id` != '' AND `issued-id` != '' ";
202                         break;
203                 case DIRECTION_IN :
204                         $sql_extra2 = " AND `dfrn-id` = '' AND `issued-id` != '' ";
205                         break;
206                 case DIRECTION_OUT :
207                         $sql_extra2 = " AND `dfrn-id` != '' AND `issued-id` = '' ";
208                         break;
209                 case DIRECTION_ANY :
210                 default:
211                         $sql_extra2 = '';
212                         break;
213         }
214
215         $r = q("SELECT * FROM `contact` WHERE `uid` = %d $sql_extra $sql_extra2 ",
216                 intval($_SESSION['uid']));
217
218         if(count($r)) {
219
220                 $tpl = file_get_contents("view/contact_template.tpl");
221
222                 foreach($r as $rr) {
223                         if($rr['self'])
224                                 continue;
225                         $direction = '';
226                         if(strlen($rr['issued-id'])) {
227                                 if(strlen($rr['dfrn-id'])) {
228                                         $direction = DIRECTION_BOTH;
229                                         $dir_icon = 'images/lrarrow.gif';
230                                         $alt_text = 'Mutual Friendship';
231                                 }
232                                 else {
233                                         $direction = DIRECTION_IN;
234                                         $dir_icon = 'images/larrow.gif';
235                                         $alt_text = 'is a fan of yours';
236                                 }
237                         }
238                         else {
239                                 $direction = DIRECTION_OUT;
240                                 $dir_icon = 'images/rarrow.gif';
241                                 $alt_text = 'you are a fan of';
242                         }
243
244                         $o .= replace_macros($tpl, array(
245                                 '$id' => $rr['id'],
246                                 '$alt_text' => $alt_text,
247                                 '$dir_icon' => $dir_icon,
248                                 '$thumb' => $rr['thumb'], 
249                                 '$name' => $rr['name'],
250                                 '$url' => (($direction != DIRECTION_IN) ? "redir/{$rr['id']}" : $rr['url'] )
251                         ));
252                 }
253         }
254         return $o;
255 }