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