public function searchContentType($contenttypename, array $parameters = [], &$pager = []) { $where = []; $tablename = $this->getContenttypeTablename($contenttypename); $contenttype = $this->app['config']->get('contenttypes/' . $contenttypename); // If this contenttype has 'searchable: false', we skip it. if (isset($contenttype['searchable']) && $contenttype['searchable'] === false) { return []; } // for all the non-reserved parameters that are fields, we assume people want to do a 'where' foreach ($parameters as $key => $value) { if (in_array($key, ['order', 'where', 'limit', 'offset'])) { continue; // Skip this one. } if (!$this->isValidColumn($key, $contenttype)) { continue; // Also skip if 'key' isn't a field in the contenttype. } $where[] = $this->parseWhereParameter($key, $value); } // @todo update with nice search string // If we need to filter, add the WHERE for that. // Meh, InnoDB doesn't support full text search. if (!empty($parameters['filter'])) { $filter = $this->app['db']->quote($parameters['filter']); $filterWhere = []; foreach ($contenttype['fields'] as $key => $value) { if (in_array($value['type'], ['text', 'textarea', 'html', 'markdown'])) { $filterWhere[] = sprintf("%s LIKE '%%%s%%'", $key, $filter); } } if (!empty($filterWhere)) { $where[] = '(' . implode(' OR ', $filterWhere) . ')'; } } $limit = !empty($parameters['limit']) ? $parameters['limit'] : 9999; $page = !empty($parameters['page']) ? $parameters['page'] : 1; // If we're allowed to use pagination, use the 'page' parameter. if (!empty($parameters['paging']) && $this->app->raw('request') instanceof Request) { $page = $this->app['request']->get('page', $page); } $queryparams = ""; // implode 'where' if (!empty($where)) { $queryparams .= sprintf(' WHERE (%s)', implode(" AND ", $where)); } // Order, with a special case for 'RANDOM'. if (!empty($parameters['order'])) { if ($parameters['order'] == "RANDOM") { $dboptions = $this->app['db']->getParams(); $queryparams .= sprintf(' ORDER BY %s', $dboptions['randomfunction']); } else { $order = $this->getEscapedSortorder($parameters['order'], false); if (!empty($order)) { $queryparams .= sprintf(' ORDER BY %s', $order); } } } // Make the query for the pager. $pagerquery = sprintf('SELECT COUNT(*) AS count FROM %s %s', $tablename, $queryparams); // Add the limit $queryparams = $this->app['db']->getDatabasePlatform()->modifyLimitQuery($queryparams, $limit, ($page - 1) * $limit); // Make the query to get the results. $query = "SELECT * FROM {$tablename}" . $queryparams; $rows = $this->app['db']->fetchAll($query); // Make sure content is set, and all content has information about its contenttype $content = []; foreach ($rows as $row) { $content[$row['id']] = $this->getContentObject($contenttype, $row); } // TODO: Check if we need to hydrate here! // Make sure all content has their taxonomies and relations $this->getTaxonomy($content); $this->getRelation($content); // Set up the $pager array with relevant values. $rowcount = $this->app['db']->executeQuery($pagerquery)->fetch(); $pager = ['for' => 'search', 'count' => $rowcount['count'], 'totalpages' => ceil($rowcount['count'] / $limit), 'current' => $page, 'showing_from' => ($page - 1) * $limit + 1, 'showing_to' => ($page - 1) * $limit + count($content)]; return $content; }