]> git.mxchange.org Git - friendica.git/blob - mod/group.php
Merge branch 'develop' into 1703-worker-splitting
[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         }
86         if ($switchtotext === false) {
87                 $switchtotext = 400;
88         }
89
90         $tpl = get_markup_template('group_edit.tpl');
91
92         $context = array(
93                         '$submit' => t('Save Group'),
94         );
95
96         if (($a->argc == 2) && ($a->argv[1] === 'new')) {
97
98                 return replace_macros($tpl, $context + array(
99                         '$title' => t('Create a group of contacts/friends.'),
100                         '$gname' => array('groupname',t('Group Name: '), '', ''),
101                         '$gid' => 'new',
102                         '$form_security_token' => get_form_security_token("group_edit"),
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
143         if (($a->argc > 1) && (intval($a->argv[1]))) {
144
145                 require_once('include/acl_selectors.php');
146                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
147                         intval($a->argv[1]),
148                         intval(local_user())
149                 );
150                 if (! dbm::is_result($r)) {
151                         notice( t('Group not found.') . EOL );
152                         goaway(App::get_baseurl() . '/contacts');
153                 }
154                 $group = $r[0];
155                 $members = group_get_members($group['id']);
156                 $preselected = array();
157                 if (count($members)) {
158                         foreach ($members as $member) {
159                                 $preselected[] = $member['id'];
160                         }
161                 }
162
163                 if ($change) {
164                         if (in_array($change,$preselected)) {
165                                 group_rmv_member(local_user(),$group['name'],$change);
166                         } else {
167                                 group_add_member(local_user(),$group['name'],$change);
168                         }
169
170                         $members = group_get_members($group['id']);
171                         $preselected = array();
172                         if (count($members))    {
173                                 foreach ($members as $member)
174                                         $preselected[] = $member['id'];
175                         }
176                 }
177
178
179                 $drop_tpl = get_markup_template('group_drop.tpl');
180                 $drop_txt = replace_macros($drop_tpl, array(
181                         '$id' => $group['id'],
182                         '$delete' => t('Delete'),
183                         '$form_security_token' => get_form_security_token("group_drop"),
184                 ));
185
186
187                 $context = $context + array(
188                         '$title' => t('Group Editor'),
189                         '$gname' => array('groupname',t('Group Name: '),$group['name'], ''),
190                         '$gid' => $group['id'],
191                         '$drop' => $drop_txt,
192                         '$form_security_token' => get_form_security_token('group_edit'),
193                 );
194
195         }
196
197         if (! isset($group)) {
198                 return;
199         }
200
201         $groupeditor = array(
202                 'label_members' => t('Members'),
203                 'members' => array(),
204                 'label_contacts' => t('All Contacts'),
205                 'group_is_empty' => t('Group is empty'),
206                 'contacts' => array(),
207         );
208
209         $sec_token = addslashes(get_form_security_token('group_member_change'));
210         $textmode = (($switchtotext && (count($members) > $switchtotext)) ? true : false);
211         foreach ($members as $member) {
212                 if ($member['url']) {
213                         $member['click'] = 'groupChangeMember(' . $group['id'] . ',' . $member['id'] . ',\'' . $sec_token . '\'); return true;';
214                         $groupeditor['members'][] = micropro($member,true,'mpgroup', $textmode);
215                 } else {
216                         group_rmv_member(local_user(),$group['name'],$member['id']);
217                 }
218         }
219
220         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
221                 intval(local_user())
222         );
223
224         if (dbm::is_result($r)) {
225                 $textmode = (($switchtotext && (count($r) > $switchtotext)) ? true : false);
226                 foreach ($r as $member) {
227                         if (! in_array($member['id'],$preselected)) {
228                                 $member['click'] = 'groupChangeMember(' . $group['id'] . ',' . $member['id'] . ',\'' . $sec_token . '\'); return true;';
229                                 $groupeditor['contacts'][] = micropro($member,true,'mpall', $textmode);
230                         }
231                 }
232         }
233
234         $context['$groupeditor'] = $groupeditor;
235         $context['$desc'] = t('Click on a contact to add or remove.');
236
237         if ($change) {
238                 $tpl = get_markup_template('groupeditor.tpl');
239                 echo replace_macros($tpl, $context);
240                 killme();
241         }
242
243         return replace_macros($tpl, $context);
244
245 }