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