Exemplo n.º 1
0
 /**
  * Get the entity related to user in the active identity
  *
  * @return \NetAssist\Models\Users
  */
 public function getUser()
 {
     $identity = $this->session->get('auth-identity');
     if (isset($identity['id'])) {
         $user = Users::findById(new MongoId(substr($identity['id'], 0, 24)));
         if ($user == false) {
             throw new Exception('The user does not exist');
         }
         return $user;
     }
     return false;
 }
 /**
  * POST  /Graph/Positions
  * Save user node positions
  */
 public function savePositionsAction()
 {
     //disable view rendering, we rendering JSON instead of HTML
     $this->view->setRenderLevel(View::LEVEL_NO_RENDER);
     //Get user identity
     $identity = $this->auth->getIdentity();
     if (!$identity) {
         //If user is not logged in, send HTTP 417
         return $this->response->setStatusCode(417, "User is not logged in");
     }
     try {
         //get user identitiy
         $uid = $this->auth->getUserId();
         //get MongoDate object for current time
         $m_now = new \MongoDate(time());
         //fetch user record
         $user = Users::findById($uid);
         if ($user == false) {
             //user record not found, send HTTP 403
             return $this->response->setStatusCode(403, "User not found");
         }
         //fetch user saved nodes positions
         $u_nodes = UserNodes::findFirst(array(array("uid" => $uid)));
         if ($u_nodes == false) {
             //nothing exists yet, add new
             $u_nodes = new UserNodes();
             $u_nodes->uid = $uid;
         }
         //update modification time
         $u_nodes->lastModified = $m_now;
         //read JSON from request data
         $rawBody = $this->request->getJsonRawBody(true);
         //iterate over expected array
         foreach ($rawBody as $pos) {
             //save node position
             $node_id = (int) $pos['id'];
             $position = new NodePosition();
             //save coordinates
             $position->x = (double) $pos['x'];
             $position->y = (double) $pos['y'];
             $position->node_id = $node_id;
             $u_nodes->positions[$node_id] = $position;
         }
         //save data to the database
         if ($u_nodes->save() == false) {
             //we have an error, send http 500
             return $this->sendStateResponse(false, 500, "MongoDB save failure");
         }
     } catch (Exception $e) {
         //we have an exception error, send http 500, log error
         error_log(sprintf("Error during saving nodes positions: %s", addslashes($e->getMessage())), 0);
         return $this->sendStateResponse(false, 500, "MongoDB save failure");
     }
     //success
     return $this->sendStateResponse(true);
 }