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