{\r
$a = self::getApp();\r
\r
+ if ($a->isAjax()) {\r
+ self::ajaxPost();\r
+ }\r
+\r
if (!local_user()) {\r
notice(L10n::t('Permission denied.'));\r
$a->internalRedirect();\r
}\r
}\r
\r
+ public static function ajaxPost()\r
+ {\r
+ try {\r
+ $a = self::getApp();\r
+\r
+ if (!local_user()) {\r
+ throw new \Exception(L10n::t('Permission denied.'), 403);\r
+ }\r
+\r
+ // POST /group/123/add/123\r
+ // POST /group/123/remove/123\r
+ if ($a->argc == 4) {\r
+ list($group_id, $command, $contact_id) = array_slice($a->argv, 1);\r
+\r
+ if (!Model\Group::exists($group_id, local_user())) {\r
+ throw new \Exception(L10n::t('Unknown group.'), 404);\r
+ }\r
+\r
+ $contact = DBA::selectFirst('contact', ['pending', 'blocked', 'deleted'], ['id' => $contact_id, 'uid' => local_user()]);\r
+ if (!DBA::isResult($contact)) {\r
+ throw new \Exception(L10n::t('Contact not found.'), 404);\r
+ }\r
+\r
+ if ($contact['pending']) {\r
+ throw new \Exception(L10n::t('Contact is unavailable.'), 400);\r
+ }\r
+\r
+ if ($contact['deleted']) {\r
+ throw new \Exception(L10n::t('Contact is deleted.'), 410);\r
+ }\r
+\r
+ switch($command) {\r
+ case 'add':\r
+ if ($contact['blocked']) {\r
+ throw new \Exception(L10n::t('Contact is blocked, unable to add it to a group.'), 400);\r
+ }\r
+\r
+ if (!Model\Group::addMember($group_id, $contact_id)) {\r
+ throw new \Exception(L10n::t('Unable to add the contact to the group.'), 500);\r
+ }\r
+ $message = L10n::t('Contact successfully added to group.');\r
+ break;\r
+ case 'remove':\r
+ if (!Model\Group::removeMember($group_id, $contact_id)) {\r
+ throw new \Exception(L10n::t('Unable to remove the contact from the group.'), 500);\r
+ }\r
+ $message = L10n::t('Contact successfully removed from group.');\r
+ break;\r
+ default:\r
+ throw new \Exception(L10n::t('Unknown group command.'), 400);\r
+ }\r
+ } else {\r
+ throw new \Exception(L10n::t('Bad request.'), 400);\r
+ }\r
+\r
+ notice($message);\r
+ System::jsonExit(['status' => 'OK', 'message' => $message]);\r
+ } catch (\Exception $e) {\r
+ notice($e->getMessage());\r
+ System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);\r
+ }\r
+ }\r
+\r
public static function content()\r
{\r
$change = false;\r