private function setQueryConditions(&$query) { $db = $this->getDBO(); if ($this->getState('site')) { // Get current datetime $date = JFactory::getDate()->toSql(); // Get authorised view levels $viewlevels = array_unique(JFactory::getUser()->getAuthorisedViewLevels()); // Published items only $this->setState('state', 1); $this->setState('publish_up', $date); $this->setState('publish_down', $date); // Set state for access $this->setState('access', $viewlevels); // Language filter $application = JFactory::getApplication(); if ($application->isSite() && $application->getLanguageFilter()) { $language = JFactory::getLanguage(); $query->where($db->quoteName('item.language') . ' IN (' . $db->quote($language->getTag()) . ', ' . $db->quote('*') . ')'); } } // Shortcut method for setting the categoy filter if ($this->getState('category.filter')) { $filter = (object) $this->getState('category.filter'); if (isset($filter->enabled) && $filter->enabled) { $this->setState('category', $filter->categories); $this->setState('recursive', $filter->recursive); } } if ($this->getState('category')) { $categories = (array) $this->getState('category'); $filter = K2ModelCategories::getCategoryFilter($categories, $this->getState('recursive'), $this->getState('site')); if (!count($filter)) { $filter[] = 1; } $this->setState('categories.applied', $filter); $query->where($db->quoteName('item.catid') . ' IN (' . implode(',', $filter) . ')'); } else { if ($this->getState('site')) { $authorised = K2ModelCategories::getAuthorised(); if (!count($authorised)) { $authorised[] = 1; } $this->setState('categories.applied', $authorised); $query->where($db->quoteName('item.catid') . ' IN (' . implode(',', $authorised) . ')'); } } if ($this->getState('language')) { $query->where($db->quoteName('item.language') . ' = ' . $db->quote($this->getState('language'))); } if (is_numeric($this->getState('state'))) { $operator = $this->getState('state.operator') ? $this->getState('state.operator') : '='; $query->where($db->quoteName('item.state') . ' ' . $operator . ' ' . (int) $this->getState('state')); } if (is_numeric($this->getState('featured'))) { $query->where($db->quoteName('item.featured') . ' = ' . (int) $this->getState('featured')); } if ($this->getState('access')) { $access = $this->getState('access'); if (is_array($access)) { $access = array_unique($access); JArrayHelper::toInteger($access); $query->where($db->quoteName('item.access') . ' IN (' . implode(',', $access) . ')'); } else { $query->where($db->quoteName('item.access') . ' = ' . (int) $access); } } if ($this->getState('id')) { $id = $this->getState('id'); if (is_array($id)) { JArrayHelper::toInteger($id); $query->where($db->quoteName('item.id') . ' IN (' . implode(',', $id) . ')'); } else { $query->where($db->quoteName('item.id') . ' = ' . (int) $id); } } if ($this->getState('alias')) { $query->where($db->quoteName('item.alias') . ' = ' . $db->quote($this->getState('alias'))); } if ($this->getState('author')) { $query->where($db->quoteName('item.created_by') . ' = ' . (int) $this->getState('author')); if ($this->getState('site')) { $query->where($db->quoteName('item.created_by_alias') . ' = ' . $db->quote('')); } } if ($tag = $this->getState('tag')) { if ($excludeItemId = $this->getState('tag.exclude.item')) { $query->where($db->quoteName('item.id') . ' != ' . (int) $excludeItemId); } // Cast to integer and generate the query string $tag = (array) $tag; JArrayHelper::toInteger($tag); sort($tag, SORT_NUMERIC); $condition = implode(',', $tag); // Optimize query depending on data amount. Use cache to avoid duplicate queries if (!isset(self::$cache[$condition]['count'])) { $subquery = $db->getQuery(true); $subquery->select('COUNT(' . $db->quoteName('itemId') . ')')->from($db->quoteName('#__k2_tags_xref')); $subquery->where($db->quoteName('tagId') . ' IN (' . $condition . ')'); $db->setQuery($subquery); self::$cache[$condition]['count'] = (int) $db->loadResult(); } $numOfTaggedItems = self::$cache[$condition]['count']; if ($numOfTaggedItems == 0 || $numOfTaggedItems == 1 && $this->getState('tag.exclude.item')) { // No results should be returned $query->where($db->quoteName('item.id') . ' IN(0)'); } else { if ($numOfTaggedItems <= 50) { if (!isset(self::$cache[$condition]['itemIds'])) { $subquery = $db->getQuery(true); $subquery->select($db->quoteName('itemId'))->from($db->quoteName('#__k2_tags_xref')); $subquery->where($db->quoteName('tagId') . ' IN (' . $condition . ')'); $db->setQuery($subquery); self::$cache[$condition]['itemIds'] = $db->loadColumn(); } $query->where($db->quoteName('item.id') . ' IN (' . implode(',', self::$cache[$condition]['itemIds']) . ')'); } else { $query->innerJoin($db->quoteName('#__k2_tags_xref') . ' AS ' . $db->quoteName('xref') . ' ON ' . $db->quoteName('item.id') . ' = ' . $db->quoteName('xref.itemId')); $query->where($db->quoteName('xref.tagId') . ' IN (' . $condition . ')'); } } } if ($this->getState('publish_up')) { $query->where('(' . $db->quoteName('item.publish_up') . ' = ' . $db->Quote($db->getNullDate()) . ' OR ' . $db->quoteName('item.publish_up') . ' <= ' . $db->Quote($this->getState('publish_up')) . ')'); } if ($this->getState('publish_down')) { $query->where('(' . $db->quoteName('item.publish_down') . ' = ' . $db->Quote($db->getNullDate()) . ' OR ' . $db->quoteName('item.publish_down') . ' >= ' . $db->Quote($this->getState('publish_down')) . ')'); } if ($this->getState('search')) { $search = trim($this->getState('search')); if ($search) { // Site search if ($this->getState('site')) { $mode = $this->getState('search.mode'); switch ($mode) { case 'exact': $text = $db->quote('%' . $db->escape($search, true) . '%', false); $where = $db->quoteName('item.title') . ' LIKE ' . $text . ' OR ' . $db->quoteName('item.introtext') . ' LIKE ' . $text . ' OR ' . $db->quoteName('item.fulltext') . ' LIKE ' . $text . ' OR ' . $db->quoteName('item.extra_fields') . ' LIKE ' . $text . ' OR ' . $db->quoteName('item.tags') . ' LIKE ' . $text; break; case 'all': case 'any': default: $words = explode(' ', $search); $searchConditions = array(); foreach ($words as $word) { $word = $db->quote('%' . $db->escape($word, true) . '%', false); $wordConditions = array(); $wordConditions[] = $db->quoteName('item.title') . ' LIKE ' . $word; $wordConditions[] = $db->quoteName('item.introtext') . ' LIKE ' . $word; $wordConditions[] = $db->quoteName('item.fulltext') . ' LIKE ' . $word; $wordConditions[] = $db->quoteName('item.extra_fields') . ' LIKE ' . $word; $wordConditions[] = $db->quoteName('item.tags') . ' LIKE ' . $word; $searchConditions[] = implode(' OR ', $wordConditions); } $where = '(' . implode($mode == 'all' ? ') AND (' : ') OR (', $searchConditions) . ')'; break; } $query->where('(' . $where . ')'); } else { $search = $db->escape($search, true); $query->where('(' . $db->quoteName('item.title') . ' LIKE ' . $db->Quote('%' . $search . '%', false) . ' OR ' . $db->quoteName('item.id') . ' = ' . (int) $search . ' OR ' . $db->quoteName('item.introtext') . ' LIKE ' . $db->Quote('%' . $search . '%', false) . ' OR ' . $db->quoteName('item.fulltext') . ' LIKE ' . $db->Quote('%' . $search . '%', false) . ')'); } } } if ($this->getState('year') && $this->getState('month') && $this->getState('day')) { $startDate = JFactory::getDate($this->getState('year') . '-' . $this->getState('month') . '-' . $this->getState('day'))->toSql(); $endDate = JFactory::getDate($this->getState('year') . '-' . $this->getState('month') . '-' . $this->getState('day') . ' 23:59:59')->toSql(); } else { if ($this->getState('year') && $this->getState('month')) { $startDate = JFactory::getDate($this->getState('year') . '-' . $this->getState('month') . '-01')->toSql(); $endDate = JFactory::getDate($this->getState('year') . '-' . $this->getState('month') . '-' . date('t', strtotime('last day of ' . $this->getState('year') . '-' . $this->getState('month') . '-01')) . ' 23:59:59')->toSql(); } else { if ($this->getState('year')) { $startDate = JFactory::getDate($this->getState('year') . '-01-01')->toSql(); $endDate = JFactory::getDate($this->getState('year') . '-12-31 23:59:59')->toSql(); } } } if (isset($startDate)) { $query->where($db->quoteName('item.created') . ' >= ' . $db->quote($startDate)); } if (isset($endDate)) { $query->where($db->quoteName('item.created') . ' <= ' . $db->quote($endDate)); } if ($this->getState('media')) { $query->where($db->quoteName('item.media') . ' != ' . $db->quote('[]')); $query->where($db->quoteName('item.media') . ' != ' . $db->quote('')); } if ($this->getState('created.value')) { $query->where($db->quoteName('item.created') . ' ' . $this->getState('created.operator') . ' ' . $db->quote($this->getState('created.value'))); } if (is_numeric($this->getState('ordering.value'))) { $query->where($db->quoteName('item.ordering') . ' ' . $this->getState('ordering.operator') . ' ' . (int) $this->getState('ordering.value')); } if ($excludeItemId = $this->getState('exclude')) { $query->where($db->quoteName('item.id') . ' != ' . (int) $excludeItemId); } }
/** * getCategoryFilter method. * * @return array */ public static function getCategoryFilter($categories = null, $recursive = false, $access = false) { $filter = K2ModelCategories::getAuthorised(); if ($categories) { if (!is_array($categories)) { $categories = (array) $categories; } $categories = array_filter($categories); if (count($categories)) { if ($recursive) { $children = array(); $model = K2Model::getInstance('Categories'); foreach ($categories as $categoryId) { $key = (string) $access . '|' . (string) $categoryId; if (!isset(self::$cache['trees'][$key])) { $model->setState('site', $access); $model->setState('root', $categoryId); $rows = $model->getRows(); self::$cache['trees'][$key] = $rows; } else { $rows = self::$cache['trees'][$key]; } foreach ($rows as $row) { $children[] = $row->id; } } $categories = array_merge($categories, $children); $categories = array_unique($categories); } if ($access) { $filter = array_intersect($categories, K2ModelCategories::getAuthorised()); } else { $filter = $categories; } } } return array_unique($filter); }