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