]> git.mxchange.org Git - friendica.git/blob - mod/group.php
Merge pull request #5959 from vinzv/patch-2
[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 use Friendica\BaseModule;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
13 use Friendica\Core\System;
14 use Friendica\Database\DBA;
15 use Friendica\Model;
16 use Friendica\Module;
17 use Friendica\Util\Security;
18
19 function group_init(App $a) {
20         if (local_user()) {
21                 $a->page['aside'] = Model\Group::sidebarWidget('contacts', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
22         }
23 }
24
25 function group_post(App $a) {
26
27         if (!local_user()) {
28                 notice(L10n::t('Permission denied.') . EOL);
29                 return;
30         }
31
32         if (($a->argc == 2) && ($a->argv[1] === 'new')) {
33                 BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
34
35                 $name = notags(trim($_POST['groupname']));
36                 $r = Model\Group::create(local_user(), $name);
37                 if ($r) {
38                         info(L10n::t('Group created.') . EOL);
39                         $r = Model\Group::getIdByName(local_user(), $name);
40                         if ($r) {
41                                 goaway(System::baseUrl() . '/group/' . $r);
42                         }
43                 } else {
44                         notice(L10n::t('Could not create group.') . EOL);
45                 }
46                 goaway(System::baseUrl() . '/group');
47                 return; // NOTREACHED
48         }
49
50         if (($a->argc == 2) && intval($a->argv[1])) {
51                 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
52
53                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
54                         intval($a->argv[1]),
55                         intval(local_user())
56                 );
57                 if (!DBA::isResult($r)) {
58                         notice(L10n::t('Group not found.') . EOL);
59                         goaway(System::baseUrl() . '/contact');
60                         return; // NOTREACHED
61                 }
62                 $group = $r[0];
63                 $groupname = notags(trim($_POST['groupname']));
64                 if (strlen($groupname) && ($groupname != $group['name'])) {
65                         $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
66                                 DBA::escape($groupname),
67                                 intval(local_user()),
68                                 intval($group['id'])
69                         );
70
71                         if ($r) {
72                                 info(L10n::t('Group name changed.') . EOL);
73                         }
74                 }
75
76                 $a->page['aside'] = Model\Group::sidebarWidget();
77         }
78         return;
79 }
80
81 function group_content(App $a) {
82         $change = false;
83
84         if (!local_user()) {
85                 notice(L10n::t('Permission denied') . EOL);
86                 return;
87         }
88
89         // With no group number provided we jump to the unassigned contacts as a starting point
90         if ($a->argc == 1) {
91                 goaway('group/none');
92         }
93
94         // Switch to text mode interface if we have more than 'n' contacts or group members
95         $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
96         if (is_null($switchtotext)) {
97                 $switchtotext = Config::get('system', 'groupedit_image_limit', 400);
98         }
99
100         $tpl = get_markup_template('group_edit.tpl');
101
102         $context = [
103                 '$submit' => L10n::t('Save Group'),
104                 '$submit_filter' => L10n::t('Filter'),
105         ];
106
107         if (($a->argc == 2) && ($a->argv[1] === 'new')) {
108                 return replace_macros($tpl, $context + [
109                         '$title' => L10n::t('Create a group of contacts/friends.'),
110                         '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
111                         '$gid' => 'new',
112                         '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
113                 ]);
114
115
116         }
117
118         $nogroup = false;
119
120         if (($a->argc == 2) && ($a->argv[1] === 'none')) {
121                 $id = -1;
122                 $nogroup = true;
123                 $group = [
124                         'id' => $id,
125                         'name' => L10n::t('Contacts not in any group'),
126                 ];
127
128                 $members = [];
129                 $preselected = [];
130                 $entry = [];
131
132                 $context = $context + [
133                         '$title' => $group['name'],
134                         '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
135                         '$gid' => $id,
136                         '$editable' => 0,
137                 ];
138         }
139
140
141         if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
142                 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
143
144                 if (intval($a->argv[2])) {
145                         $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
146                                 intval($a->argv[2]),
147                                 intval(local_user())
148                         );
149
150                         $result = null;
151
152                         if (DBA::isResult($r)) {
153                                 $result = Model\Group::removeByName(local_user(), $r[0]['name']);
154                         }
155
156                         if ($result) {
157                                 info(L10n::t('Group removed.') . EOL);
158                         } else {
159                                 notice(L10n::t('Unable to remove group.') . EOL);
160                         }
161                 }
162                 goaway(System::baseUrl() . '/group');
163                 // NOTREACHED
164         }
165
166         if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
167                 BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
168
169                 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
170                         intval($a->argv[2]),
171                         intval(local_user())
172                 );
173                 if (DBA::isResult($r)) {
174                         $change = intval($a->argv[2]);
175                 }
176         }
177
178         if (($a->argc > 1) && intval($a->argv[1])) {
179                 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
180                         intval($a->argv[1]),
181                         intval(local_user())
182                 );
183
184                 if (!DBA::isResult($r)) {
185                         notice(L10n::t('Group not found.') . EOL);
186                         goaway(System::baseUrl() . '/contact');
187                 }
188
189                 $group = $r[0];
190                 $members = Model\Contact::getByGroupId($group['id']);
191                 $preselected = [];
192                 $entry = [];
193                 $id = 0;
194
195                 if (count($members)) {
196                         foreach ($members as $member) {
197                                 $preselected[] = $member['id'];
198                         }
199                 }
200
201                 if ($change) {
202                         if (in_array($change, $preselected)) {
203                                 Model\Group::removeMember($group['id'], $change);
204                         } else {
205                                 Model\Group::addMember($group['id'], $change);
206                         }
207
208                         $members = Model\Contact::getByGroupId($group['id']);
209                         $preselected = [];
210                         if (count($members)) {
211                                 foreach ($members as $member) {
212                                         $preselected[] = $member['id'];
213                                 }
214                         }
215                 }
216
217                 $drop_tpl = get_markup_template('group_drop.tpl');
218                 $drop_txt = replace_macros($drop_tpl, [
219                         '$id' => $group['id'],
220                         '$delete' => L10n::t('Delete Group'),
221                         '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
222                 ]);
223
224
225                 $context = $context + [
226                         '$title' => $group['name'],
227                         '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
228                         '$gid' => $group['id'],
229                         '$drop' => $drop_txt,
230                         '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
231                         '$edit_name' => L10n::t('Edit Group Name'),
232                         '$editable' => 1,
233                 ];
234
235         }
236
237         if (!isset($group)) {
238                 return;
239         }
240
241         $groupeditor = [
242                 'label_members' => L10n::t('Members'),
243                 'members' => [],
244                 'label_contacts' => L10n::t('All Contacts'),
245                 'group_is_empty' => L10n::t('Group is empty'),
246                 'contacts' => [],
247         ];
248
249         $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
250
251         // Format the data of the group members
252         foreach ($members as $member) {
253                 if ($member['url']) {
254                         $entry = Module\Contact::getContactTemplateVars($member);
255                         $entry['label'] = 'members';
256                         $entry['photo_menu'] = '';
257                         $entry['change_member'] = [
258                                 'title'     => L10n::t("Remove contact from group"),
259                                 'gid'       => $group['id'],
260                                 'cid'       => $member['id'],
261                                 'sec_token' => $sec_token
262                         ];
263
264                         $groupeditor['members'][] = $entry;
265                 } else {
266                         Model\Group::removeMember($group['id'], $member['id']);
267                 }
268         }
269
270         if ($nogroup) {
271                 $r = Model\Contact::getUngroupedList(local_user());
272         } else {
273                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
274                         intval(local_user())
275                 );
276                 $context['$desc'] = L10n::t('Click on a contact to add or remove.');
277         }
278
279         if (DBA::isResult($r)) {
280                 // Format the data of the contacts who aren't in the contact group
281                 foreach ($r as $member) {
282                         if (!in_array($member['id'], $preselected)) {
283                                 $entry = Module\Contact::getContactTemplateVars($member);
284                                 $entry['label'] = 'contacts';
285                                 if (!$nogroup)
286                                         $entry['photo_menu'] = [];
287
288                                 if (!$nogroup) {
289                                         $entry['change_member'] = [
290                                                 'title'     => L10n::t("Add contact to group"),
291                                                 'gid'       => $group['id'],
292                                                 'cid'       => $member['id'],
293                                                 'sec_token' => $sec_token
294                                         ];
295                                 }
296
297                                 $groupeditor['contacts'][] = $entry;
298                         }
299                 }
300         }
301
302         $context['$groupeditor'] = $groupeditor;
303
304         // If there are to many contacts we could provide an alternative view mode
305         $total = count($groupeditor['members']) + count($groupeditor['contacts']);
306         $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
307
308         if ($change) {
309                 $tpl = get_markup_template('groupeditor.tpl');
310                 echo replace_macros($tpl, $context);
311                 killme();
312         }
313
314         return replace_macros($tpl, $context);
315
316 }