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