/** * Finds matching elements * * @param DTO $dto A DTO that can accept the following params: * <PrimaryKey> * Slug * PluginID * Anchored * DefaultOrder * AnchoredSiteID * StartDate string ModifiedDate is after this * EndDate string ModifiedDate is before this * Search string Matches any part of name, slug, or description * IncludesAllAspects string A list of aspects that results must be related to * IncludesAspects string A list of aspects that results must relate to at least one of * * @return DTO The filled DTO object */ public function findAll(DTO $dto) { if ($dto->hasParameter($this->getModel()->getPrimaryKey()) || $dto->hasParameter('Slug') || $dto->hasParameter('PluginID') || $dto->getLimit() != null || $dto->getOffset() != null || $dto->getOrderBys() != null) { $sd = __CLASS__ . (string) serialize($dto); $slugs = $this->SystemCache->get($sd); if ($slugs === false) { // find slugs $this->loadSource(); $slugs = array(); $sort_array = array(); $dir = 'ASC'; $orderbys = $dto->getOrderBys(); if (!empty($orderbys)) { foreach ($orderbys as $col => $dir) { } } else { $col = 'Slug'; } foreach ($this->objectsBySlug as $slug => $obj) { if (($val = $dto->getParameter('Slug')) != null) { if ($obj->Slug != $val) { continue; } } if (($val = $dto->getParameter('PluginID')) != null) { if ($obj->PluginID != $val) { continue; } } $slugs[] = $slug; $sort_array[] = $obj[$col]; } array_multisort($sort_array, strtolower($dir) == 'asc' ? SORT_ASC : SORT_DESC, SORT_REGULAR, $slugs); $this->SystemCache->put($sd, $slugs, 0); } } else { $this->populateRels(); if (!empty($this->aspectrel)) { foreach ($this->aspectrel as $elementSlug => $element) { if ($dto->hasParameter('IncludesAspect')) { $aspect = strtolower($dto->getParameter('IncludesAspect')); if (!in_array($aspect, (array) $element['Aspects'])) { continue; } } $slugs[] = $elementSlug; } } } $results = array(); if (!empty($slugs)) { // retrieve objects $rows = $this->multiGetBySlug($slugs); foreach ($slugs as $slug) { $results[] = $rows[$slug]; } } $dto->setResults($results); return $dto; }
/** * Extends the {@link $query} with Limits, Offsets, and Orderbys specified in the DTO * * @param Database $db The database this query will be used on (used to escape values) * @param Query $query The query object that we'll extend with the limits, offsets and orderbys * @param DTO $dto The DTO object that contains our limits, offsets, and orderbys * @param array $defaultSorts An array of the form [name => direction] that defines default sorts (used if none are in the DTO) * * @return void */ public function buildLimitOffsetOrderbys($db, $query, $dto, array $defaultSorts) { if ($dto->getLimit() != null) { $query->LIMIT($dto->getLimit()); } if ($dto->getOffset() != null) { $query->OFFSET($dto->getOffset()); } $query->ORDERBY(); $sorts = array(); $arr = func_num_args() == 5 ? func_get_arg(4) : null; $arr = is_array($arr) ? $arr : array_slice(func_get_args(), 4); $dtoSorts = $dto->getOrderBys(); if ($dtoSorts == null) { $dtoSorts = $defaultSorts; } $diff = array_diff(array_keys($dtoSorts), array_keys($arr)); $merged = array_merge($diff, $arr); $sorts = array_unique($merged); foreach ($sorts as $name => $column) { if (is_int($name)) { $name = $column; } if (isset($dtoSorts[$name])) { $direction = $dtoSorts[$name]; if (strcasecmp($name, 'FIELD') === 0) { if (count($dto->getParameter($direction)) == 0 || array_sum($dto->getParameter($direction)) == 0) { continue; } $query->ORDERBY('FIELD(' . $direction . ',' . $db->joinQuote($dto->getParameter($direction)) . ')'); } else { $query->ORDERBY("{$column} {$direction}"); } } } }