/** * Appends stored position coordinates (x,y) from user profile to nodes. * Return original array if no positions stored or user is not logged in. * * @param \NetAssist\Graph\Nodes[] Nodes to append * @return \NetAssist\Graph\Nodes[] Nodes with appended positions */ private function appendUserPositions($nodes) { try { //get user identity information $identity = $this->auth->getIdentity(); if (!$identity || $identity == null) { //if user is not logged in return $nodes; } //get user identifier $uid = $this->auth->getUserId(); //get users saved nodes positions $u_nodes = UserNodes::findFirst(array(array("uid" => $uid))); if ($u_nodes == null) { //nothing found, return nodes return $nodes; } //read nodes positions foreach ($nodes as $node) { if (array_key_exists($node->id, $u_nodes->positions)) { $pos = (object) $u_nodes->positions[$node->id]; $node->x = $pos->x; $node->y = $pos->y; } } } catch (Exception $e) { //log an exception error error_log(sprintf("Couldn't fetch user graph position, error: %s", addslashes($e->getMessage())), 0); } return $nodes; }
/** * 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); }