]> git.mxchange.org Git - friendica.git/blob - mod/group.php
Merge pull request #3253 from friendica/revert-3112-rewrites/coding-convention
[friendica.git] / mod / group.php
1 <?php
2
3 function validate_members(&$item) {
4         $item = intval($item);
5 }
6
7 function group_init(App $a) {
8         if(local_user()) {
9                 require_once('include/group.php');
10                 $a->page['aside'] = group_side('contacts','group','extended',(($a->argc > 1) ? intval($a->argv[1]) : 0));
11         }
12 }
13
14
15
16 function group_post(App $a) {
17
18         if (! local_user()) {
19                 notice( t('Permission denied.') . EOL);
20                 return;
21         }
22
23         if(($a->argc == 2) && ($a->argv[1] === 'new')) {
24                 check_form_security_token_redirectOnErr('/group/new', 'group_edit');
25
26                 $name = notags(trim($_POST['groupname']));
27                 $r = group_add(local_user(),$name);
28                 if ($r) {
29                         info( t('Group created.') . EOL );
30                         $r = group_byname(local_user(),$name);
31                         if ($r) {
32                                 goaway(App::get_baseurl() . '/group/' . $r);
33                         }
34                 } else {
35                         notice( t('Could not create group.') . EOL );
36                 }
37                 goaway(App::get_baseurl() . '/group');
38                 return; // NOTREACHED
39         }
40
41         if (($a->argc == 2) && (intval($a->argv[1]))) {
42                 check_form_security_token_redirectOnErr('/group', 'group_edit');
43
44                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
45                         intval($a->argv[1]),
46                         intval(local_user())
47                 );
48                 if (! dbm::is_result($r)) {
49                         notice( t('Group not found.') . EOL );
50                         goaway(App::get_baseurl() . '/contacts');
51                         return; // NOTREACHED
52                 }
53                 $group = $r[0];
54                 $groupname = notags(trim($_POST['groupname']));
55                 if ((strlen($groupname))  && ($groupname != $group['name'])) {
56                         $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
57                                 dbesc($groupname),
58                                 intval(local_user()),
59                                 intval($group['id'])
60                         );
61
62                         if ($r) {
63                                 info( t('Group name changed.') . EOL );
64                         }
65                 }
66
67                 $a->page['aside'] = group_side();
68         }
69         return;
70 }
71
72 function group_content(App $a) {
73         $change = false;
74
75         if (! local_user()) {
76                 notice( t('Permission denied') . EOL);
77                 return;
78         }
79
80         // Switch to text mode interface if we have more than 'n' contacts or group members
81
82         $switchtotext = get_pconfig(local_user(),'system','groupedit_image_limit');
83         if($switchtotext === false)
84                 $switchtotext = get_config('system','groupedit_image_limit');
85         if($switchtotext === false)
86                 $switchtotext = 400;
87
88         $tpl = get_markup_template('group_edit.tpl');
89
90         $context = array(
91                         '$submit' => t('Save Group'),
92         );
93
94         if (($a->argc == 2) && ($a->argv[1] === 'new')) {
95
96                 return replace_macros($tpl, $context + array(
97                         '$title' => t('Create a group of contacts/friends.'),
98                         '$gname' => array('groupname',t('Group Name: '), '', ''),
99                         '$gid' => 'new',
100                         '$form_security_token' => get_form_security_token("group_edit"),
101                 ));
102
103
104         }
105
106         if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
107                 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
108
109                 if (intval($a->argv[2])) {
110                         $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
111                                 intval($a->argv[2]),
112                                 intval(local_user())
113                         );
114
115                         $result = null;
116
117                         if (dbm::is_result($r)) {
118                                 $result = group_rmv(local_user(),$r[0]['name']);
119                         }
120
121                         if ($result) {
122                                 info( t('Group removed.') . EOL);
123                         } else {
124                                 notice( t('Unable to remove group.') . EOL);
125                         }
126                 }
127                 goaway(App::get_baseurl() . '/group');
128                 // NOTREACHED
129         }
130
131         if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
132                 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
133
134                 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
135                         intval($a->argv[2]),
136                         intval(local_user())
137                 );
138                 if (dbm::is_result($r))
139                         $change = intval($a->argv[2]);
140         }
141
142         if (($a->argc > 1) && (intval($a->argv[1]))) {
143
144                 require_once('include/acl_selectors.php');
145                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
146                         intval($a->argv[1]),
147                         intval(local_user())
148                 );
149                 if (! dbm::is_result($r)) {
150                         notice( t('Group not found.') . EOL );
151                         goaway(App::get_baseurl() . '/contacts');
152                 }
153                 $group = $r[0];
154                 $members = group_get_members($group['id']);
155                 $preselected = array();
156                 if(count($members))     {
157                         foreach($members as $member)
158                                 $preselected[] = $member['id'];
159                 }
160
161                 if($change) {
162                         if(in_array($change,$preselected)) {
163                                 group_rmv_member(local_user(),$group['name'],$change);
164                         }
165                         else {
166                                 group_add_member(local_user(),$group['name'],$change);
167                         }
168
169                         $members = group_get_members($group['id']);
170                         $preselected = array();
171                         if(count($members))     {
172                                 foreach($members as $member)
173                                         $preselected[] = $member['id'];
174                         }
175                 }
176
177
178                 $drop_tpl = get_markup_template('group_drop.tpl');
179                 $drop_txt = replace_macros($drop_tpl, array(
180                         '$id' => $group['id'],
181                         '$delete' => t('Delete'),
182                         '$form_security_token' => get_form_security_token("group_drop"),
183                 ));
184
185
186                 $context = $context + array(
187                         '$title' => t('Group Editor'),
188                         '$gname' => array('groupname',t('Group Name: '),$group['name'], ''),
189                         '$gid' => $group['id'],
190                         '$drop' => $drop_txt,
191                         '$form_security_token' => get_form_security_token('group_edit'),
192                 );
193
194         }
195
196         if(! isset($group))
197                 return;
198
199         $groupeditor = array(
200                 'label_members' => t('Members'),
201                 'members' => array(),
202                 'label_contacts' => t('All Contacts'),
203                 'group_is_empty' => t('Group is empty'),
204                 'contacts' => array(),
205         );
206
207         $sec_token = addslashes(get_form_security_token('group_member_change'));
208         $textmode = (($switchtotext && (count($members) > $switchtotext)) ? true : false);
209         foreach($members as $member) {
210                 if($member['url']) {
211                         $member['click'] = 'groupChangeMember(' . $group['id'] . ',' . $member['id'] . ',\'' . $sec_token . '\'); return true;';
212                         $groupeditor['members'][] = micropro($member,true,'mpgroup', $textmode);
213                 }
214                 else
215                         group_rmv_member(local_user(),$group['name'],$member['id']);
216         }
217
218         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
219                 intval(local_user())
220         );
221
222         if (dbm::is_result($r)) {
223                 $textmode = (($switchtotext && (count($r) > $switchtotext)) ? true : false);
224                 foreach($r as $member) {
225                         if(! in_array($member['id'],$preselected)) {
226                                 $member['click'] = 'groupChangeMember(' . $group['id'] . ',' . $member['id'] . ',\'' . $sec_token . '\'); return true;';
227                                 $groupeditor['contacts'][] = micropro($member,true,'mpall', $textmode);
228                         }
229                 }
230         }
231
232         $context['$groupeditor'] = $groupeditor;
233         $context['$desc'] = t('Click on a contact to add or remove.');
234
235         if($change) {
236                 $tpl = get_markup_template('groupeditor.tpl');
237                 echo replace_macros($tpl, $context);
238                 killme();
239         }
240
241         return replace_macros($tpl, $context);
242
243 }