Exemple #1
0
 public static function completeInfos(Book &$book)
 {
     try {
         $config = self::getConfig();
         $googleBook = new \Sb\Google\Model\GoogleBook($book->getISBN10(), $book->getISBN13(), $book->getASIN(), $config->getGoogleApiKey());
         if ($googleBook->getVolumeInfo()) {
             $bookFromGoogle = new Book();
             \Sb\Db\Mapping\BookMapper::mapFromGoogleBookVolumeInfo($bookFromGoogle, $googleBook->getVolumeInfo());
             if (!$book->getDescription() && $bookFromGoogle->getDescription()) {
                 $book->setDescription($bookFromGoogle->getDescription());
             }
             if (!$book->getImageUrl() && $bookFromGoogle->getImageUrl()) {
                 $book->setImageUrl($bookFromGoogle->getImageUrl());
             }
             if (!$book->getSmallImageUrl() && $bookFromGoogle->getSmallImageUrl()) {
                 $book->setSmallImageUrl($bookFromGoogle->getSmallImageUrl());
             }
             if (!$book->getPublishingDate() && $bookFromGoogle->getPublishingDate()) {
                 $book->setPublishingDate($bookFromGoogle->getPublishingDate());
             }
         } else {
             \Sb\Trace\Trace::addItem('Le livre n\'a pas été trouvé sur Google.');
         }
     } catch (\Exception $exc) {
         $message = sprintf("Une erreur s'est produite lors de l'appel à l'api google books : %s", $exc->getMessage());
         MailSvc::getInstance()->send(Constants::WEBMASTER_EMAIL, "Erreur interne", $message);
         \Sb\Trace\Trace::addItem($message);
     }
 }
Exemple #2
0
 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");
     }
 }
Exemple #3
0
 /**
  *
  * @return array of Books (of type Book) from Amazon
  */
 private function getAmazonResults($searchTerm, $amazonApiKey, $amazonSecretKey, $amazonAssociateTag, $amazonNumberOfPageRequested, $amazonSite)
 {
     $amazonService = new \Zend_Service_Amazon($amazonApiKey, $amazonSite, $amazonSecretKey);
     $amazonResults = null;
     // tableau d'amazon item recu directement
     $booksFromAmazon = null;
     // tableau d'objet Book reçu depuis amazon
     $responsesGroups = 'Small,ItemAttributes,Images,EditorialReview,Reviews,BrowseNodes,OfferSummary';
     //$version = '2005-10-05';
     $nbPageRequested = $amazonNumberOfPageRequested;
     $nbMaxPageRequested = 5;
     if ($nbPageRequested > $nbMaxPageRequested) {
         $nbPageRequested = $nbMaxPageRequested;
     }
     for ($itemPageNum = 1; $itemPageNum <= $nbPageRequested; $itemPageNum++) {
         $tmpAmazonResults = null;
         // recherche faite sur le code isbn10
         if (strlen($searchTerm) == 10) {
             $tmpAmazonResults = $amazonService->itemSearch(array('SearchIndex' => 'Books', 'AssociateTag' => $amazonAssociateTag, 'ResponseGroup' => $responsesGroups, 'Power' => 'isbn:' . $searchTerm, 'ItemPage' => $itemPageNum));
             if (!$tmpAmazonResults || $tmpAmazonResults->totalResults() == 0) {
                 $tmpAmazonResults = $amazonService->itemSearch(array('SearchIndex' => 'Books', 'AssociateTag' => $amazonAssociateTag, 'ResponseGroup' => $responsesGroups, 'Power' => 'asin:' . $searchTerm, 'ItemPage' => $itemPageNum));
             }
         }
         // recherche faite sur le mot clé
         if (!$tmpAmazonResults || $tmpAmazonResults->totalResults() == 0) {
             $tmpAmazonResults = $amazonService->itemSearch(array('SearchIndex' => 'Books', 'AssociateTag' => $amazonAssociateTag, 'ResponseGroup' => $responsesGroups, 'Keywords' => $searchTerm, 'ItemPage' => $itemPageNum));
         }
         $amazonResults = $tmpAmazonResults;
         // mapping des résultats amazon en objet Book
         if ($amazonResults) {
             foreach ($amazonResults as $amazonResult) {
                 $result = new \Sb\Db\Model\Book();
                 \Sb\Db\Mapping\BookMapper::mapFromAmazonResult($result, $amazonResult);
                 // Book is added to the collection only if
                 // author is set
                 // and ISBN10, ISBN13 or ASIN is set
                 if (count($result->getContributors()) > 0) {
                     if ($result->getISBN10() || $result->getISBN13() || $result->getASIN()) {
                         $booksFromAmazon[$result->getISBN10()] = $result;
                     }
                 }
             }
         }
     }
     return $booksFromAmazon;
 }
 private function getBooksFromAmazon($isbns, Config $config)
 {
     \Sb\Trace\Trace::addItem(count($isbns) . " Isbns vont être requetés à Amazon : " . implode(",", $isbns));
     $booksFromAmazon = array();
     for ($index = 0; $index < count($isbns); $index = $index + 10) {
         $isbnsSlice = array_slice($isbns, $index, 10);
         \Sb\Trace\Trace::addItem(count($isbnsSlice) . " Isbns requetés par requête Amazon : " . implode(",", $isbnsSlice));
         $amazonService = new Zend_Service_Amazon($config->getAmazonApiKey(), 'FR', $config->getAmazonSecretKey());
         $responsesGroups = 'Small,ItemAttributes,Images,EditorialReview,Reviews';
         // Recherche d'une liste de livre
         $amazonResults = $amazonService->itemLookup(implode(",", $isbnsSlice), array('SearchIndex' => 'Books', 'AssociateTag' => $config->getAmazonAssociateTag(), 'ResponseGroup' => $responsesGroups, 'IdType' => 'ISBN'));
         if ($amazonResults) {
             $i = 0;
             foreach ($amazonResults as $amazonResult) {
                 $result = new \Sb\Db\Model\Book();
                 \Sb\Db\Mapping\BookMapper::mapFromAmazonResult($result, $amazonResult);
                 if ($result->IsValid()) {
                     $i++;
                     $booksFromAmazon[] = $result;
                 }
                 //var_dump($booksFromAmazon);
             }
             \Sb\Trace\Trace::addItem($i . " trouvés lors de cette requête amazon.");
         }
     }
     return $booksFromAmazon;
 }