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