/** * BookAdd Action * * Save new EPUBS sent as POST and redirect to BookEdit * @throws \Exception * @return Ambigous <\Zend\Http\Response, \Zend\Stdlib\ResponseInterface> */ public function bookaddAction() { //Redirecting if not logged if (!$this->logged) { return $this->redirect()->toRoute('application/default', array('controller' => 'index', 'action' => 'index')); } $book = new Book(); //Verify validity of the file $file_ok = !empty($_FILES['fileInput']['name']); $file_ok = $_FILES["fileInput"]["error"] > 0 ? false : $file_ok; $file_ok = end(explode(".", $_FILES["fileInput"]["name"])) != "epub" ? false : $file_ok; if ($file_ok) { //Get EPUB information $epub = new EPUB($_FILES["fileInput"]["tmp_name"], $this->getServiceLocator()->get('config')['uploadFolder']); $book = new Book(); $epub->getBook($book); //Complement book information $sm = $this->getServiceLocator(); $books = new Books($sm); $book->user_id = $this->user->user_id; $book->date = date("Y-m-d"); $book->status = 'tmp'; $book = $books->addBook($book); //Save book cover $imagine = $this->getServiceLocator()->get('my_image_service'); $epub->saveCover($book->book_id, $imagine); //Move EPUB to proper location if (file_exists($this->getServiceLocator()->get('config')['uploadFolder'] . $book->book_id . ".epub")) { unlink($this->getServiceLocator()->get('config')['uploadFolder'] . $book->book_id . ".epub"); } move_uploaded_file($_FILES["fileInput"]["tmp_name"], $this->getServiceLocator()->get('config')['uploadFolder'] . $book->book_id . ".epub"); //Redirect to BookEdit action return $this->redirect()->toRoute('application/default', array('controller' => 'index', 'action' => 'bookedit', 'id' => $book->book_id)); } else { //Throw excpetion if file have a problem throw new \Exception("The uploaded file must be a valid EPUB"); } }