/**
  * @api {post} /admin/users POST /users
  * @apiExample Example usage:
  * curl -i -X POST "http://apibeta.compargo.com/v1/admin/users/?countryCode=ph&language=en"
  *      -H "X-COMPARE-REST-API-KEY: 1234567890"
  *      -d "groupId=56c4b6c2-1d54-11e4-b32d-eff91066cccf&emailAddress=steve@moneymax.ph&firstName=Steve&lastName=Jobs&password=secret&status=1"
  * @apiDescription  Create a new User
  * @apiName         PostUser
  * @apiGroup        Users
  *
  * @apiHeader       {String} X-COMPARE-REST-API-KEY   Users unique access-key.
  * 
  * @apiParam        {String} language                 Mandatory Language.
  * @apiParam        {String} countryCode              Mandatory Country code.
  * 
  * @apiParam        {Number} groupId                  Mandatory ID of the Group. 
  * @apiParam        {String} emailAddress             Mandatory Email Address of the User.
  * @apiParam        {String} firstName                Mandatory Firstname of the User.
  * @apiParam        {String} lastName                 Mandatory Lastname of the User.
  * @apiParam        {String} password                 Mandatory Password of the User.
  * @apiParam        {String} status                   Mandatory Status of the User.
  * 
  * @apiSuccess      {String} id                       The new Users-ID.
  *
  * @apiSuccessExample Success-Response:
  *     HTTP/1.1 200 OK
  *     {
  *       "id": "1535ebcc-22b8-11e4-bd33-17609cecca2f"       
  *     }
  *
  * @apiError BadInputParameter The request cannot be fulfilled due to bad syntax.
  *
  * @apiErrorExample Error-Response:
  *     HTTP/1.1 400
  *     {
  *       "error": "BadInputParameter"
  *     }
  *     
  * @apiError UserNotFound The id of the User was not found.
  *
  * @apiErrorExample Error-Response:
  *     HTTP/1.1 404 Not Found
  *     {
  *       "error": "UserNotFound"
  *     }
  *     
  * @apiError InvalidAccessToken The access token is invalid.
  *
  * @apiErrorExample Error-Response:
  *     HTTP/1.1 401 Unauthorized
  *     {
  *       "error": "InvalidAccessToken"
  *     }
  *     	 
  * @apiError MissingAuthenticationCredentials The authentication credentials are missing.
  * 
  * @apiErrorExample Error-Response:
  *     HTTP/1.1 401 Unauthorized
  *     {
  *       "error": "MissingAuthenticationCredentials"
  *     }
  *        
  * @apiError RouteNotFound That route was not found on the server.
  *
  * @apiErrorExample Error-Response:
  *     HTTP/1.1 404 
  *     {
  *       "error": "RouteNotFound"
  *     } 
  */
 public function post()
 {
     $results = array();
     $request = $this->di->get('request');
     $data = $request->getPost();
     if (!empty($data)) {
         $user = new Users();
         $data['groupId'] = isset($data['groupId']) ? $data['groupId'] : $user->groupId;
         if (isset($data['password'])) {
             $salt = $this->security->getTokenKey();
             $password = $salt . $data['password'];
             $passwordHash = create_hash($password);
             $user->password = sha1($salt . $data['password']);
             $user->salt = $salt;
             $user->hash = $passwordHash;
             $data['password'] = $user->password;
         }
         $data['id'] = $user->id;
         $data['emailAddress'] = isset($data['emailAddress']) ? $data['emailAddress'] : $user->emailAddress;
         $data['firstName'] = isset($data['firstName']) ? $data['firstName'] : $user->firstName;
         $data['lastName'] = isset($data['lastName']) ? $data['lastName'] : $user->lastName;
         $data['status'] = isset($data['status']) ? $data['status'] : 0;
         $data['createdBy'] = isset($data['createdBy']) ? $data['createdBy'] : '';
         $data['modifiedBy'] = isset($data['modifiedBy']) ? $data['modifiedBy'] : '';
         if (isset($data['status'])) {
             $data['active'] = $data['status'] != Users::ACTIVE ? 0 : 1;
         }
         if ($user->create($data)) {
             $results['id'] = $user->id;
         } else {
             throw new HTTPException("Request unable to be followed due to semantic errors", 422, array('dev' => $user->getMessages(), 'internalCode' => 'P1000', 'more' => ''));
         }
     } else {
         throw new HTTPException("The request cannot be fulfilled due to bad syntax.", 400, array('dev' => 'A required field is missing.', 'internalCode' => 'P1000', 'more' => ''));
     }
     return $results;
 }