public function routes(&$router)
 {
     // Get all contact groups
     $router->get('/contacts/groups', function () use($router) {
         $groups = ContactsGroups::load($GLOBALS['user']->id);
         $users = array();
         foreach ($groups as $index => $group) {
             $members = ContactsGroups::loadMembers($GLOBALS['user']->id, $group['group_id']);
             if (!$router->compact()) {
                 foreach ($members as $user_id) {
                     if (!isset($users[$user_id])) {
                         $user = $router->dispatch('get', '/user(/:user_id)', $user_id);
                         $users[$user_id] = $user['user'];
                     }
                 }
             }
             $groups[$index]['members'] = $members;
         }
         $router->render($router->compact() ? compact('groups') : compact('groups', 'users'));
     });
     // Create new contact group
     $router->post('/contacts/groups', function () use($router) {
         $name = trim(Request::get('name'));
         if (!$name) {
             $router->halt(406, 'No name provided');
         }
         AddNewStatusgruppe($name, $GLOBALS['user']->id, 0);
         $router->render($router->dispatch('get', '/contacts/groups'));
     });
     // Get members of contact group
     $router->get('/contacts/groups/:group_id', function ($group_id) use($router) {
         if (!ContactsGroups::exists($group_id)) {
             $router->halt(404, 'Contact group "%s" not found', $group_id);
         }
         $members = ContactsGroups::loadMembers($GLOBALS['user']->id, $group_id);
         if ($router->compact()) {
             $router->render(compact('members'));
         }
         $users = array();
         foreach ($members as $user_id) {
             $user = $router->dispatch('get', '/user(/:user_id)', $user_id);
             $users[] = $user['user'];
         }
         $router->render(compact('members', 'users'));
     });
     // Remove contact group
     $router->delete('/contacts/groups/:group_id', function ($group_id) use($router) {
         if (!ContactsGroups::exists($group_id)) {
             $router->halt(404, 'Contact group "%s" not found', $group_id);
         }
         DeleteStatusgruppe($group_id);
         $router->halt(200, 'Contact group "%s" has been deleted', $group_id);
     });
     // Put a user into contact group
     $router->put('/contacts/groups/:group_id/:user_id', function ($group_id, $user_id) use($router) {
         if (!ContactsGroups::exists($group_id)) {
             $router->halt(404, 'Contact group "%s" not found', $group_id);
         }
         $user = \User::find($user_id);
         if (!$user) {
             $router->halt(404, 'User "%s" not found', $user_id);
         }
         if (!InsertPersonStatusgruppe($user_id, $group_id)) {
             $router->halt(500);
         }
         $router->render($router->dispatch('get', '/contacts/groups/:group_id', $group_id));
     });
     // Remove user from contact group
     $router->delete('/contacts/groups/:group_id/:user_id', function ($group_id, $user_id) use($router) {
         if (!ContactsGroups::exists($group_id)) {
             $router->halt(404, 'Contact group "%s" not found', $group_id);
         }
         $user = \User::find($user_id);
         if (!$user) {
             $router->halt(404, 'User "%s" not found', $user_id);
         }
         $username = $user->username;
         RemovePersonStatusgruppe($username, $group_id);
         $router->halt(200);
     });
 }
Ejemplo n.º 2
0
 /**
  * Create a new contact group for a user.
  *
  * @post /user/:user_id/contact_groups
  */
 public function createContactGroup($user_id)
 {
     if ($GLOBALS['user']->id !== $user_id) {
         $this->error(401);
     }
     if (!isset($this->data['name']) || !strlen($name = trim($this->data['name']))) {
         $this->error(400, 'Contact group name required.');
     }
     $id = AddNewStatusgruppe($name, $GLOBALS['user']->id, $size = 0);
     $this->redirect('contact_group/' . $id, 201, 'ok');
 }