]> git.mxchange.org Git - friendica.git/blob - src/Module/Group.php
Merge pull request #7828 from nupplaphil/task/move_enotify
[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(array $parameters = [])
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(array $parameters = [])
136         {
137                 $change = false;
138
139                 if (!local_user()) {
140                         throw new \Friendica\Network\HTTPException\ForbiddenException();
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                 // @TODO: Replace with parameter from router
180                 if (($a->argc == 2) && ($a->argv[1] === 'none') ||
181                         ($a->argc == 1) && ($a->argv[0] === 'nogroup')) {
182                         $id = -1;
183                         $nogroup = true;
184                         $group = [
185                                 'id' => $id,
186                                 'name' => L10n::t('Contacts not in any group'),
187                         ];
188
189                         $members = [];
190                         $preselected = [];
191
192                         $context = $context + [
193                                 '$title' => $group['name'],
194                                 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
195                                 '$gid' => $id,
196                                 '$editable' => 0,
197                         ];
198                 }
199
200                 // @TODO: Replace with parameter from router
201                 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
202                         BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
203
204                         // @TODO: Replace with parameter from router
205                         if (intval($a->argv[2])) {
206                                 if (!Model\Group::exists($a->argv[2], local_user())) {
207                                         notice(L10n::t('Group not found.'));
208                                         $a->internalRedirect('contact');
209                                 }
210
211                                 if (Model\Group::remove($a->argv[2])) {
212                                         info(L10n::t('Group removed.'));
213                                 } else {
214                                         notice(L10n::t('Unable to remove group.'));
215                                 }
216                         }
217                         $a->internalRedirect('group');
218                 }
219
220                 // @TODO: Replace with parameter from router
221                 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
222                         BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
223
224                         if (DBA::exists('contact', ['id' => $a->argv[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) {
225                                 $change = intval($a->argv[2]);
226                         }
227                 }
228
229                 // @TODO: Replace with parameter from router
230                 if (($a->argc > 1) && intval($a->argv[1])) {
231                         $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user(), 'deleted' => false]);
232                         if (!DBA::isResult($group)) {
233                                 notice(L10n::t('Group not found.'));
234                                 $a->internalRedirect('contact');
235                         }
236
237                         $members = Model\Contact::getByGroupId($group['id']);
238                         $preselected = [];
239
240                         if (count($members)) {
241                                 foreach ($members as $member) {
242                                         $preselected[] = $member['id'];
243                                 }
244                         }
245
246                         if ($change) {
247                                 if (in_array($change, $preselected)) {
248                                         Model\Group::removeMember($group['id'], $change);
249                                 } else {
250                                         Model\Group::addMember($group['id'], $change);
251                                 }
252
253                                 $members = Model\Contact::getByGroupId($group['id']);
254                                 $preselected = [];
255                                 if (count($members)) {
256                                         foreach ($members as $member) {
257                                                 $preselected[] = $member['id'];
258                                         }
259                                 }
260                         }
261
262                         $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
263                         $drop_txt = Renderer::replaceMacros($drop_tpl, [
264                                 '$id' => $group['id'],
265                                 '$delete' => L10n::t('Delete Group'),
266                                 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
267                         ]);
268
269                         $context = $context + [
270                                 '$title' => $group['name'],
271                                 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
272                                 '$gid' => $group['id'],
273                                 '$drop' => $drop_txt,
274                                 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
275                                 '$edit_name' => L10n::t('Edit Group Name'),
276                                 '$editable' => 1,
277                         ];
278                 }
279
280                 if (!isset($group)) {
281                         throw new \Friendica\Network\HTTPException\BadRequestException();
282                 }
283
284                 $groupeditor = [
285                         'label_members' => L10n::t('Members'),
286                         'members' => [],
287                         'label_contacts' => L10n::t('All Contacts'),
288                         'group_is_empty' => L10n::t('Group is empty'),
289                         'contacts' => [],
290                 ];
291
292                 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
293
294                 // Format the data of the group members
295                 foreach ($members as $member) {
296                         if ($member['url']) {
297                                 $entry = Contact::getContactTemplateVars($member);
298                                 $entry['label'] = 'members';
299                                 $entry['photo_menu'] = '';
300                                 $entry['change_member'] = [
301                                         'title'     => L10n::t("Remove contact from group"),
302                                         'gid'       => $group['id'],
303                                         'cid'       => $member['id'],
304                                         'sec_token' => $sec_token
305                                 ];
306
307                                 $groupeditor['members'][] = $entry;
308                         } else {
309                                 Model\Group::removeMember($group['id'], $member['id']);
310                         }
311                 }
312
313                 if ($nogroup) {
314                         $contacts = Model\Contact::getUngroupedList(local_user());
315                 } else {
316                         $contacts_stmt = DBA::select('contact', [],
317                                 ['uid' => local_user(), 'pending' => false, 'blocked' => false, 'self' => false],
318                                 ['order' => ['name']]
319                         );
320                         $contacts = DBA::toArray($contacts_stmt);
321                         $context['$desc'] = L10n::t('Click on a contact to add or remove.');
322                 }
323
324                 if (DBA::isResult($contacts)) {
325                         // Format the data of the contacts who aren't in the contact group
326                         foreach ($contacts as $member) {
327                                 if (!in_array($member['id'], $preselected)) {
328                                         $entry = Contact::getContactTemplateVars($member);
329                                         $entry['label'] = 'contacts';
330                                         if (!$nogroup)
331                                                 $entry['photo_menu'] = [];
332
333                                         if (!$nogroup) {
334                                                 $entry['change_member'] = [
335                                                         'title'     => L10n::t("Add contact to group"),
336                                                         'gid'       => $group['id'],
337                                                         'cid'       => $member['id'],
338                                                         'sec_token' => $sec_token
339                                                 ];
340                                         }
341
342                                         $groupeditor['contacts'][] = $entry;
343                                 }
344                         }
345                 }
346
347                 $context['$groupeditor'] = $groupeditor;
348
349                 // If there are to many contacts we could provide an alternative view mode
350                 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
351                 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
352
353                 if ($change) {
354                         $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
355                         echo Renderer::replaceMacros($tpl, $context);
356                         exit();
357                 }
358
359                 return Renderer::replaceMacros($tpl, $context);
360         }
361 }