private function searchData($searchTerm, $nbResultsToShow, $amazonApiKey, $amazonSecretKey, $amazonAssociateTag, $amazonNumberOfPageRequested) { if ($searchTerm) { $searchTerm = trim($searchTerm); // faire la recherche dans la base de donnée $this->allResults = \Sb\Db\Dao\BookDao::getInstance()->getListByKeyword($searchTerm); try { $amazonResults = null; // si pas de résultat ou nb de resultats < à ce que l'on souhaite afficher, faire la recherche Amazon if (!$this->allResults || count($this->allResults) < $nbResultsToShow) { // Requesting amazon FR first $amazonResults = $this->getAmazonResults($searchTerm, $amazonApiKey, $amazonSecretKey, $amazonAssociateTag, $amazonNumberOfPageRequested, 'FR'); // Requesting amazon US if (!$amazonResults) { $amazonResults = $this->getAmazonResults($searchTerm, $amazonApiKey, $amazonSecretKey, $amazonAssociateTag, $amazonNumberOfPageRequested, 'US'); } } // si des résultats ont été trouvés avec Amazon // si des résultats avaient été trouvés dans la base // ==> ils doivent être mergés ensemble if ($this->allResults && $amazonResults) { $allResultsKeys = array_map(array(&$this, "extractKey"), $this->allResults); $this->allResults = array_combine($allResultsKeys, $this->allResults); // Adding the books not present in sql found on amazon after the books that are already in sql $this->allResults = $this->allResults + array_diff_key($amazonResults, $this->allResults); } elseif ($amazonResults) { $this->allResults = $amazonResults; } // Sort books by publishing date desc $sorting = new Sorting(); $sorting->setDirection(EntityHelper::DESC); $sorting->setField('publishing_date'); BooksHelper::sort($this->allResults, $sorting); } catch (\Exception $exc) { \Sb\Trace\Trace::addItem(sprintf("Une erreur s'est produite lors de l'appel à l'api amazon : %s", $exc->getMessage())); } } }
private function setListOptionsForSorting($key, $sortCriteria) { $listOptions = $this->getListOptions($key); if ($sortCriteria) { // Un objet Options a été récupéré pour cette liste : on assigne uniquement les infos de sorting if ($listOptions) { if ($listOptions->getSorting()) { if ($listOptions->getSorting()->getField() == $sortCriteria) { $sortDirection = $listOptions->getSorting()->getDirection() == EntityHelper::ASC ? EntityHelper::DESC : EntityHelper::ASC; } else { $sortDirection = EntityHelper::ASC; } $listOptions->getSorting()->setField($sortCriteria); $listOptions->getSorting()->setDirection($sortDirection); } else { $sorting = new Sorting(); $sorting->setField($sortCriteria); $sorting->setDirection(EntityHelper::ASC); $listOptions->setSorting($sorting); } } else { // Aucun objet Options n'a été récupéré pour cette liste : on en créé un avec le numéro de page $sorting = new Sorting(); $sorting->setField($sortCriteria); $sorting->setDirection(EntityHelper::ASC); $listOptions = new Options(); $listOptions->setSorting($sorting); } } $this->setListOptions($key, $listOptions); }
public static function sort(&$books, \Sb\Lists\Sorting $sorting) { $className = get_class(); $sortingField = $sorting->getField(); $sortingDirection = $sorting->getDirection(); switch ($sortingField) { case self::SORTING_FIELD_RATING: switch ($sortingDirection) { case EntityHelper::ASC: \Sb\Trace\Trace::addItem($className . "::compareByRatingAsc"); usort($books, $className . "::compareByRatingAsc"); break; case EntityHelper::DESC: \Sb\Trace\Trace::addItem($className . "::compareByRatingDesc"); usort($books, $className . "::compareByRatingDesc"); break; } break; case self::SORTING_FIELD_TITLE: switch ($sortingDirection) { case EntityHelper::ASC: \Sb\Trace\Trace::addItem($className . "::compareByTitleAsc"); usort($books, $className . "::compareByTitleAsc"); break; case EntityHelper::DESC: \Sb\Trace\Trace::addItem($className . "::compareByTitleDesc"); usort($books, $className . "::compareByTitleDesc"); break; } break; case self::SORTING_FIELD_AUTHOR: switch ($sortingDirection) { case EntityHelper::ASC: \Sb\Trace\Trace::addItem($className . "::compareByAuthorAsc"); usort($books, $className . "::compareByAuthorAsc"); break; case EntityHelper::DESC: \Sb\Trace\Trace::addItem($className . "::compareByAuthorDesc"); usort($books, $className . "::compareByAuthorDesc"); break; } break; case self::SORTING_FIELD_STATE: switch ($sortingDirection) { case EntityHelper::ASC: \Sb\Trace\Trace::addItem($className . "::compareByStateAsc"); usort($books, $className . "::compareByStateAsc"); break; case EntityHelper::DESC: \Sb\Trace\Trace::addItem($className . "::compareByStateDesc"); usort($books, $className . "::compareByStateDesc"); break; } break; case self::SORTING_FIELD_PUBLISHING_DATE: switch ($sortingDirection) { case EntityHelper::ASC: \Sb\Trace\Trace::addItem($className . "::compareByPublishingDateAsc"); usort($books, $className . "::compareByPublishingDateAsc"); break; case EntityHelper::DESC: \Sb\Trace\Trace::addItem($className . "::compareByPublishingDateDesc"); usort($books, $className . "::compareByPublishingDateDesc"); break; } break; } }