Exemple #1
0
 /**
  * Execute the search in the entity.
  * 
  * @param EntityInterface $entity
  * @param SearchQuery     $search
  * 
  * @return array
  */
 private function search(EntityInterface $entity, SearchQuery $search)
 {
     if ($search->getPage() === null) {
         $search->setPage(1);
     }
     return $entity->search($search);
 }
Exemple #2
0
 /**
  * Generates the query to search rows.
  * 
  * @param SearchQuery $search
  * 
  * @return \SimpleCrud\Queries\Query
  */
 protected function getQuery(SearchQuery $search)
 {
     $table = $this->getTable();
     $query = $table->select();
     //Filter by id
     if (count($search->getIds())) {
         $query->byId($search->getIds());
     }
     if ($search->getPage() !== null) {
         $limit = $search->getLimit();
         $query->offset($search->getPage() * $limit - $limit)->limit($limit);
     }
     if ($this->searchFields === null) {
         $this->searchFields = [$this->getFirstField()];
     }
     $orderBy = $search->getSort();
     if (!empty($orderBy) && isset($table->getScheme()['fields'][$orderBy])) {
         $query->orderBy("`{$table->name}`.`{$orderBy}`", $search->getDirection());
     } else {
         $query->orderBy("`{$table->name}`.`id`", 'DESC');
     }
     //Filter by words
     foreach ($search->getWords() as $k => $word) {
         foreach ($this->searchFields as $field) {
             $query->where("`{$table->name}`.`{$field}` LIKE :w{$k}", [":w{$k}" => "%{$word}%"]);
         }
     }
     //Filter by relations
     $db = $table->getDatabase();
     foreach ($search->getConditions() as $name => $value) {
         $related = $db->{$name}->select()->by('id', $value)->run();
         $query->relatedWith($related);
     }
     return $query;
 }
Exemple #3
0
 /**
  * {@inheritdoc}
  */
 public function search(SearchQuery $search)
 {
     $result = [];
     $words = $search->getWords();
     $start = strlen($this->getBasePath()) + 1;
     $length = -strlen($this->extension) - 1;
     $ids = $search->getIds();
     foreach ($this->getIterator() as $file) {
         if (!$file->isFile() || $file->getExtension() !== $this->extension) {
             continue;
         }
         $id = substr($file->getPathname(), $start, $length);
         //Filter by id
         if (!empty($ids) && !in_array($id, $ids, true)) {
             continue;
         }
         //Filter by word
         foreach ($words as $word) {
             if (strpos($id, $word) === false) {
                 continue;
             }
         }
         $result[$id] = $this->parse(file_get_contents($file->getPathname()));
     }
     $sort = $search->getSort();
     if ($sort !== null) {
         uasort($result, function ($a, $b) use($sort) {
             if ($a[$sort] === $b[$sort]) {
                 return 0;
             }
             return $a[$sort] < $b[$sort] ? -1 : 1;
         });
         if ($search->getDirection() === 'DESC') {
             $result = array_reverse($result, true);
         }
     }
     if ($search->getPage() !== null) {
         $limit = $search->getLimit();
         $offset = $search->getPage() * $limit - $limit;
         $result = array_slice($result, $offset, $limit, true);
     }
     return $result;
 }