public function newAction() { $response = new ApiResponse(); if ($this->request->isPost()) { $answer = new Answers(); $answer->id = uniqid(); $answer->content = $this->request->getPost('content'); $answer->users_id = $this->request->getPost('users_id'); $answer->questions_id = $this->request->getPost('questions_id'); if ($this->request->hasFiles() == true) { $baseLocation = 'files/'; foreach ($this->request->getUploadedFiles() as $file) { $photos = new Photos(); $unique_filename = $answer->id; $photos->size = $file->getSize(); $photos->original_name = $file->getName(); $photos->file_name = $unique_filename; $photos->extension = $file->getExtension(); $location = $baseLocation . $unique_filename . "." . $file->getExtension(); $photos->public_link = $location; try { if (!$photos->save()) { $response->setResponseError($photos->getMessages()); } else { //Move the file into the application $file->moveTo($location); $answer->photo = $photos->public_link; } } catch (PDOException $e) { $response->setResponseError($e->getMessage()); } } } try { if ($answer->save() == false) { $response->setResponseError($answer->getMessages()); } else { $response->setResponse($answer->id); } } catch (PDOException $e) { $response->setResponseError($e->getMessage()); } } else { $response->setResponseError('Wrong HTTP Method'); } return $response; }
public function textanswerAction() { $this->view->disable(); if ($this->request->isPost()) { $data = $this->request->getPost(); if (!isset($data['uuid'])) { return false; } $check = Answers::findFirst(array('uuid = ?1 AND question = ?2 AND type = ?3', 'bind' => array(1 => $data['uuid'], 2 => $data['question'], 3 => 'text'))); if ($check) { // If i find one already, replace hte existing value $check->value = json_encode($data['value']); if (!$check->save()) { echo json_encode($check->getMessages()); } else { echo json_encode(true); } } else { // otherwise create a new one. $answer = new Answers(); $answer->assign(array('uuid' => $data['uuid'], 'question' => $data['question'], 'type' => 'text', 'value' => json_encode($data['value']))); if (!$answer->save()) { echo json_encode($answer->getMessages()); } else { echo json_encode(true); } } } }