]> git.mxchange.org Git - friendica.git/blob - src/Module/Group.php
Merge pull request #7044 from MrPetovan/task/router
[friendica.git] / src / Module / Group.php
1 <?php
2 /**
3  * @file src/Module/Group.php
4  */
5
6 namespace Friendica\Module;
7
8 use Friendica\BaseModule;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\Renderer;
13 use Friendica\Core\System;
14 use Friendica\Database\DBA;
15 use Friendica\Model;
16 use Friendica\Util\Strings;
17
18 require_once 'boot.php';
19
20 class Group extends BaseModule
21 {
22         public static function post()
23         {
24                 $a = self::getApp();
25
26                 if ($a->isAjax()) {
27                         self::ajaxPost();
28                 }
29
30                 if (!local_user()) {
31                         notice(L10n::t('Permission denied.'));
32                         $a->internalRedirect();
33                 }
34
35                 // @TODO: Replace with parameter from router
36                 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
37                         BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
38
39                         $name = Strings::escapeTags(trim($_POST['groupname']));
40                         $r = Model\Group::create(local_user(), $name);
41                         if ($r) {
42                                 info(L10n::t('Group created.'));
43                                 $r = Model\Group::getIdByName(local_user(), $name);
44                                 if ($r) {
45                                         $a->internalRedirect('group/' . $r);
46                                 }
47                         } else {
48                                 notice(L10n::t('Could not create group.'));
49                         }
50                         $a->internalRedirect('group');
51                 }
52
53                 // @TODO: Replace with parameter from router
54                 if (($a->argc == 2) && intval($a->argv[1])) {
55                         BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
56
57                         $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user()]);
58                         if (!DBA::isResult($group)) {
59                                 notice(L10n::t('Group not found.'));
60                                 $a->internalRedirect('contact');
61                         }
62                         $groupname = Strings::escapeTags(trim($_POST['groupname']));
63                         if (strlen($groupname) && ($groupname != $group['name'])) {
64                                 if (Model\Group::update($group['id'], $groupname)) {
65                                         info(L10n::t('Group name changed.'));
66                                 }
67                         }
68                 }
69         }
70
71         public static function ajaxPost()
72         {
73                 try {
74                         $a = self::getApp();
75
76                         if (!local_user()) {
77                                 throw new \Exception(L10n::t('Permission denied.'), 403);
78                         }
79
80                         // POST /group/123/add/123
81                         // POST /group/123/remove/123
82                         // @TODO: Replace with parameter from router
83                         if ($a->argc == 4) {
84                                 list($group_id, $command, $contact_id) = array_slice($a->argv, 1);
85
86                                 if (!Model\Group::exists($group_id, local_user())) {
87                                         throw new \Exception(L10n::t('Unknown group.'), 404);
88                                 }
89
90                                 $contact = DBA::selectFirst('contact', ['pending', 'blocked', 'deleted'], ['id' => $contact_id, 'uid' => local_user()]);
91                                 if (!DBA::isResult($contact)) {
92                                         throw new \Exception(L10n::t('Contact not found.'), 404);
93                                 }
94
95                                 if ($contact['pending']) {
96                                         throw new \Exception(L10n::t('Contact is unavailable.'), 400);
97                                 }
98
99                                 if ($contact['deleted']) {
100                                         throw new \Exception(L10n::t('Contact is deleted.'), 410);
101                                 }
102
103                                 switch($command) {
104                                         case 'add':
105                                                 if ($contact['blocked']) {
106                                                         throw new \Exception(L10n::t('Contact is blocked, unable to add it to a group.'), 400);
107                                                 }
108
109                                                 if (!Model\Group::addMember($group_id, $contact_id)) {
110                                                         throw new \Exception(L10n::t('Unable to add the contact to the group.'), 500);
111                                                 }
112                                                 $message = L10n::t('Contact successfully added to group.');
113                                                 break;
114                                         case 'remove':
115                                                 if (!Model\Group::removeMember($group_id, $contact_id)) {
116                                                         throw new \Exception(L10n::t('Unable to remove the contact from the group.'), 500);
117                                                 }
118                                                 $message = L10n::t('Contact successfully removed from group.');
119                                                 break;
120                                         default:
121                                                 throw new \Exception(L10n::t('Unknown group command.'), 400);
122                                 }
123                         } else {
124                                 throw new \Exception(L10n::t('Bad request.'), 400);
125                         }
126
127                         notice($message);
128                         System::jsonExit(['status' => 'OK', 'message' => $message]);
129                 } catch (\Exception $e) {
130                         notice($e->getMessage());
131                         System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
132                 }
133         }
134
135         public static function content()
136         {
137                 $change = false;
138
139                 if (!local_user()) {
140                         System::httpExit(403);
141                 }
142
143                 $a = self::getApp();
144
145                 $a->page['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
146
147                 // With no group number provided we jump to the unassigned contacts as a starting point
148                 // @TODO: Replace with parameter from router
149                 if ($a->argc == 1) {
150                         $a->internalRedirect('group/none');
151                 }
152
153                 // Switch to text mode interface if we have more than 'n' contacts or group members
154                 $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
155                 if (is_null($switchtotext)) {
156                         $switchtotext = Config::get('system', 'groupedit_image_limit', 200);
157                 }
158
159                 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
160
161
162                 $context = [
163                         '$submit' => L10n::t('Save Group'),
164                         '$submit_filter' => L10n::t('Filter'),
165                 ];
166
167                 // @TODO: Replace with parameter from router
168                 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
169                         return Renderer::replaceMacros($tpl, $context + [
170                                 '$title' => L10n::t('Create a group of contacts/friends.'),
171                                 '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
172                                 '$gid' => 'new',
173                                 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
174                         ]);
175                 }
176
177                 $nogroup = false;
178
179                 if (($a->argc == 2) && ($a->argv[1] === 'none')) {
180                         $id = -1;
181                         $nogroup = true;
182                         $group = [
183                                 'id' => $id,
184                                 'name' => L10n::t('Contacts not in any group'),
185                         ];
186
187                         $members = [];
188                         $preselected = [];
189
190                         $context = $context + [
191                                 '$title' => $group['name'],
192                                 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
193                                 '$gid' => $id,
194                                 '$editable' => 0,
195                         ];
196                 }
197
198                 // @TODO: Replace with parameter from router
199                 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
200                         BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
201
202                         // @TODO: Replace with parameter from router
203                         if (intval($a->argv[2])) {
204                                 if (!Model\Group::exists($a->argv[2], local_user())) {
205                                         notice(L10n::t('Group not found.'));
206                                         $a->internalRedirect('contact');
207                                 }
208
209                                 if (Model\Group::remove($a->argv[2])) {
210                                         info(L10n::t('Group removed.'));
211                                 } else {
212                                         notice(L10n::t('Unable to remove group.'));
213                                 }
214                         }
215                         $a->internalRedirect('group');
216                 }
217
218                 // @TODO: Replace with parameter from router
219                 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
220                         BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
221
222                         if (DBA::exists('contact', ['id' => $a->argv[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) {
223                                 $change = intval($a->argv[2]);
224                         }
225                 }
226
227                 // @TODO: Replace with parameter from router
228                 if (($a->argc > 1) && intval($a->argv[1])) {
229                         $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user(), 'deleted' => false]);
230                         if (!DBA::isResult($group)) {
231                                 notice(L10n::t('Group not found.'));
232                                 $a->internalRedirect('contact');
233                         }
234
235                         $members = Model\Contact::getByGroupId($group['id']);
236                         $preselected = [];
237
238                         if (count($members)) {
239                                 foreach ($members as $member) {
240                                         $preselected[] = $member['id'];
241                                 }
242                         }
243
244                         if ($change) {
245                                 if (in_array($change, $preselected)) {
246                                         Model\Group::removeMember($group['id'], $change);
247                                 } else {
248                                         Model\Group::addMember($group['id'], $change);
249                                 }
250
251                                 $members = Model\Contact::getByGroupId($group['id']);
252                                 $preselected = [];
253                                 if (count($members)) {
254                                         foreach ($members as $member) {
255                                                 $preselected[] = $member['id'];
256                                         }
257                                 }
258                         }
259
260                         $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
261                         $drop_txt = Renderer::replaceMacros($drop_tpl, [
262                                 '$id' => $group['id'],
263                                 '$delete' => L10n::t('Delete Group'),
264                                 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
265                         ]);
266
267                         $context = $context + [
268                                 '$title' => $group['name'],
269                                 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
270                                 '$gid' => $group['id'],
271                                 '$drop' => $drop_txt,
272                                 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
273                                 '$edit_name' => L10n::t('Edit Group Name'),
274                                 '$editable' => 1,
275                         ];
276                 }
277
278                 if (!isset($group)) {
279                         System::httpExit(400);
280                 }
281
282                 $groupeditor = [
283                         'label_members' => L10n::t('Members'),
284                         'members' => [],
285                         'label_contacts' => L10n::t('All Contacts'),
286                         'group_is_empty' => L10n::t('Group is empty'),
287                         'contacts' => [],
288                 ];
289
290                 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
291
292                 // Format the data of the group members
293                 foreach ($members as $member) {
294                         if ($member['url']) {
295                                 $entry = Contact::getContactTemplateVars($member);
296                                 $entry['label'] = 'members';
297                                 $entry['photo_menu'] = '';
298                                 $entry['change_member'] = [
299                                         'title'     => L10n::t("Remove contact from group"),
300                                         'gid'       => $group['id'],
301                                         'cid'       => $member['id'],
302                                         'sec_token' => $sec_token
303                                 ];
304
305                                 $groupeditor['members'][] = $entry;
306                         } else {
307                                 Model\Group::removeMember($group['id'], $member['id']);
308                         }
309                 }
310
311                 if ($nogroup) {
312                         $contacts = Model\Contact::getUngroupedList(local_user());
313                 } else {
314                         $contacts_stmt = DBA::select('contact', [],
315                                 ['uid' => local_user(), 'pending' => false, 'blocked' => false, 'self' => false],
316                                 ['order' => ['name']]
317                         );
318                         $contacts = DBA::toArray($contacts_stmt);
319                         $context['$desc'] = L10n::t('Click on a contact to add or remove.');
320                 }
321
322                 if (DBA::isResult($contacts)) {
323                         // Format the data of the contacts who aren't in the contact group
324                         foreach ($contacts as $member) {
325                                 if (!in_array($member['id'], $preselected)) {
326                                         $entry = Contact::getContactTemplateVars($member);
327                                         $entry['label'] = 'contacts';
328                                         if (!$nogroup)
329                                                 $entry['photo_menu'] = [];
330
331                                         if (!$nogroup) {
332                                                 $entry['change_member'] = [
333                                                         'title'     => L10n::t("Add contact to group"),
334                                                         'gid'       => $group['id'],
335                                                         'cid'       => $member['id'],
336                                                         'sec_token' => $sec_token
337                                                 ];
338                                         }
339
340                                         $groupeditor['contacts'][] = $entry;
341                                 }
342                         }
343                 }
344
345                 $context['$groupeditor'] = $groupeditor;
346
347                 // If there are to many contacts we could provide an alternative view mode
348                 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
349                 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
350
351                 if ($change) {
352                         $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
353                         echo Renderer::replaceMacros($tpl, $context);
354                         exit();
355                 }
356
357                 return Renderer::replaceMacros($tpl, $context);
358         }
359 }