Ejemplo n.º 1
0
 /**
  * Split into meta-parameters and contenttype parameters
  * (tightly coupled to $this->getContent())
  *
  * @see $this->decodeContentQuery()
  */
 private function organizeQueryParameters($in_parameters = null)
 {
     $ctype_parameters = array();
     $meta_parameters = array('order' => false);
     // order in meta_parameters check again in line: 1530!
     if (is_array($in_parameters)) {
         foreach ($in_parameters as $key => $value) {
             if (in_array($key, array('page', 'limit', 'offset', 'returnsingle', 'printquery', 'paging', 'order'))) {
                 $meta_parameters[$key] = $value;
             } else {
                 $ctype_parameters[$key] = $value;
             }
         }
     }
     if (!isset($meta_parameters['page'])) {
         $meta_parameters['page'] = 1;
     }
     // oof!
     if (!empty($meta_parameters['paging']) && $this->app->raw('request') instanceof Request) {
         $meta_parameters['page'] = $this->app['request']->get('page', $meta_parameters['page']);
     }
     // oof, part deux!
     if (isset($meta_parameters['order']) && $meta_parameters['order'] == false && $this->app->raw('request') instanceof Request) {
         $meta_parameters['order'] = $this->app['request']->get('order', false);
     }
     return array($meta_parameters, $ctype_parameters);
 }
Ejemplo n.º 2
0
 public function searchContentType($contenttypename, array $parameters = array(), &$pager = array())
 {
     $where = array();
     $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 array();
     }
     // 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, array('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 = array();
         foreach ($contenttype['fields'] as $key => $value) {
             if (in_array($value['type'], array('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 = array();
     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 = array('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;
 }