public function addFromPost(\Sb\Db\Model\User $user, \Sb\Config\Model\Config $config) { $bookForm = new \Sb\Form\Book($_POST); // Testing if book can be found in db by id $book = null; if ($bookForm->getId()) { $book = BookDao::getInstance()->get($bookForm->getId()); } // Testing if book can be found in db by isbn10, isbn13, asin if (!$book) { $book = BookDao::getInstance()->getOneByCodes($bookForm->getISBN10(), $bookForm->getISBN13(), $bookForm->getASIN()); } // Testing if we need to add the book first if (!$book) { // Getting book from POST $book = new \Sb\Db\Model\Book(); \Sb\Db\Mapping\BookMapper::map($book, $_POST, "book_"); // Completing Book data by calling google in needed if (!$book->IsComplete()) { \Sb\Helpers\BookHelper::completeInfos($book); } $book->setCreationDate(new \DateTime()); $book->setLastModificationDate(new \DateTime()); BookDao::getInstance()->add($book); // Updating the book in cache to make it available for adding a userbook on form (borrowfromfriends, etc...) \Sb\Cache\ZendFileCache::getInstance()->save($book, \Sb\Entity\Constants::BOOK_TO_ADD_PREFIX . session_id()); } if ($book) { return $this->addUserBook($book, $user, $config); } else { Trace::addItem("Erreur lors de l'ajout d'un livre"); return __("Une erreur s'est produite lors de l'ajout du libre", "s1b"); } }
/** * Store book to add in cache and redirect to correct page */ public function prepareAddAction() { try { $globalContext = new \Sb\Context\Model\Context(); // checking if book is already in DB $isBookInDb = false; $bookInUserLib = false; if ($globalContext->getIsShowingFriendLibrary()) { Flash::addItem(__("Vous ne pouvez pas ajouter un livre à la bibliothèque d'un ami.", "s1b")); } $destination = HTTPHelper::Link(Urls::USER_BOOK_ADD_CHOICE, null, false, false); if (ArrayHelper::getSafeFromArray($_POST, Constants::BORROW_FROM_FRIENDS, null)) { $destination = HTTPHelper::Link(Urls::USER_BOOK_BORROW_FROM_FRIENDS, null, false, false); } // Remove book to add in cache ZendFileCache::getInstance()->remove(Constants::BOOK_TO_ADD_PREFIX . session_id()); // Get Book from POST $book = new Book(); BookMapper::map($book, $_POST, "book_"); if ($book->getId()) { $isBookInDb = true; } else { $bookInDb = BookDao::getInstance()->getOneByCodes($book->getISBN10(), $book->getISBN13(), $book->getASIN()); if ($bookInDb) { $isBookInDb = true; $book = $bookInDb; } } // Si le livre existe déjà en base // Vérification de l'existence du livre pour l'utilisateur // et si oui redirection vers la page d'édition if ($isBookInDb) { $userBook = UserBookDao::getInstance()->getByBookIdAndUserId($globalContext->getConnectedUser()->getId(), $book->getId()); if ($userBook && !$userBook->getIs_deleted()) { $bookInUserLib = true; // If the user is trying to borrow the book we display a flash message if (ArrayHelper::getSafeFromArray($_POST, Constants::BORROW_FROM_FRIENDS, null)) { Flash::addItem(__("Vous avez déjà ce livre dans votre bibliothèque.", "s1b")); } } } // On complète les infos qui manquent éventuellement if (!$book->IsComplete()) { Trace::addItem('Requêtage de Google.'); BookHelper::completeInfos($book); } if (!$book->IsValid()) { Flash::addItem('Il manque certaines données pour ajouter ce livre à notre base de données.'); HTTPHelper::redirectToReferer(); } else { ZendFileCache::getInstance()->save($book, Constants::BOOK_TO_ADD_PREFIX . session_id()); } if ($isBookInDb) { if ($bookInUserLib) { HTTPHelper::redirectToUrl(HTTPHelper::Link($book->getLink())); } else { HTTPHelper::redirectToUrl($destination); } } else { HTTPHelper::redirectToUrl($destination); } } catch (\Exception $e) { Trace::addItem(sprintf("Une erreur s'est produite dans \"%s->%s\", TRACE : %s\"", get_class(), __FUNCTION__, $e->getTraceAsString())); $this->forward("error", "error", "default"); } }