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