private function categories() { // Get application $application = JFactory::getApplication(); // Get filter $filter = $this->params->get('categoryIDs'); // Get categories model $model = K2Model::getInstance('Categories'); $model->setState('site', true); $model->setState('limit', 0); $model->setState('limitstart', 0); // Apply filtering if enabled if ($filter['enabled']) { $categoryIds = K2ModelCategories::getCategoryFilter($filter['categories'], $filter['recursive'], true); if (count($categoryIds)) { $model->setState('id', $categoryIds); } else { $this->categories = array(); return; } } $this->categories = $model->getRows(); foreach ($this->categories as $category) { // Get items model $model = K2Model::getInstance('Items'); $model->setState('site', true); $model->setState('recursive', false); $model->setState('limit', $this->params->get('latestItemsLimit', 10)); $model->setState('limitstart', 0); $model->setState('category', $category->id); $category->items = $model->getRows(); } $this->blocks = $this->categories; }
public static function getSearch($params) { $application = JFactory::getApplication(); $search = new stdClass(); $search->action = JRoute::_(K2HelperRoute::getSearchRoute()); $search->text = $params->get('text', JText::_('K2_SEARCH')); $search->width = intval($params->get('width', 20)); $search->maxLength = $search->width > 20 ? $search->width : 20; $search->button = $params->get('button'); $search->buttonText = htmlspecialchars($params->get('button_text', JText::_('K2_SEARCH'))); $search->imageButton = $params->get('imagebutton'); $search->buttonPosition = $params->get('button_pos', 'left'); $search->sef = $application->getCfg('sef'); $search->filter = ''; $filter = $params->get('category_id'); if ($filter && isset($filter->enabled) && $filter->enabled) { $model = K2Model::getInstance('Categories'); $categories = K2ModelCategories::getCategoryFilter($filter->categories, $filter->recursive, true); $search->filter = implode(',', $categories); } if ($params->get('liveSearch')) { $document = JFactory::getDocument(); if ($document->getType() == 'html') { $document->addScript(JURI::root(true) . '/media/k2app/vendor/underscore/underscore-min.js?v=3.0.0'); $document->addScriptDeclaration('var K2Site = "' . JURI::root(true) . '";'); } } return $search; }
private function setQueryConditions(&$query) { $db = $this->getDBO(); if ($itemId = $this->getState('itemId')) { if (is_array($itemId)) { $query->where($db->quoteName('comment.itemId') . ' IN (' . implode(',', $itemId) . ')'); } else { $query->where($db->quoteName('comment.itemId') . ' = ' . (int) $itemId); } } if (is_numeric($this->getState('state'))) { $query->where($db->quoteName('comment.state') . ' = ' . (int) $this->getState('state')); } if ($this->getState('id')) { $id = $this->getState('id'); if ($this->getState('id.operator')) { $operator = $this->getState('id.operator'); } else { $operator = '='; } $query->where($db->quoteName('comment.id') . ' ' . $operator . ' ' . (int) $id); } if ($this->getState('search')) { $search = trim($this->getState('search')); $search = strtolower($search); if ($search) { $search = $db->escape($search, true); $query->where('(' . $db->quoteName('comment.text') . ' LIKE ' . $db->Quote('%' . $search . '%', false) . ' OR ' . $db->quoteName('comment.name') . ' LIKE ' . $db->Quote('%' . $search . '%', false) . ' OR ' . $db->quoteName('comment.email') . ' LIKE ' . $db->Quote('%' . $search . '%', false) . ' OR ' . $db->quoteName('comment.ip') . ' LIKE ' . $db->Quote('%' . $search . '%', false) . ' OR ' . $db->quoteName('comment.hostname') . ' LIKE ' . $db->Quote('%' . $search . '%', false) . ')'); } } if ($this->getState('userId')) { $query->where($db->quoteName('comment.userId') . ' = ' . (int) $this->getState('userId')); } if ($category = (int) $this->getState('category')) { $query->where($db->quoteName('item.catid') . ' = ' . $category); } if ($this->getState('filter.items')) { // Items should be published $query->where($db->quoteName('item.state') . ' = 1'); // Check categories access level $filter = K2ModelCategories::getCategoryFilter($this->getState('category'), false, true); $query->where($db->quoteName('item.catid') . ' IN (' . implode(',', $filter) . ')'); // Check item access level $viewlevels = array_unique(JFactory::getUser()->getAuthorisedViewLevels()); $query->where($db->quoteName('item.access') . ' IN (' . implode(',', $viewlevels) . ')'); // Check publish up/down $date = JFactory::getDate()->toSql(); $query->where('(' . $db->quoteName('item.publish_up') . ' = ' . $db->Quote($db->getNullDate()) . ' OR ' . $db->quoteName('item.publish_up') . ' <= ' . $db->Quote($date) . ')'); $query->where('(' . $db->quoteName('item.publish_down') . ' = ' . $db->Quote($db->getNullDate()) . ' OR ' . $db->quoteName('item.publish_down') . ' >= ' . $db->Quote($date) . ')'); } }
public function getTagCloud() { // Get database $db = $this->getDbo(); // Get query $query = $db->getQuery(true); // Select tag id $query->select($db->quoteName('tag') . '.*'); // counter $query->select('COUNT(' . $db->quoteName('tag.id') . ') AS ' . $db->quoteName('counter')); // From statement $query->from($db->quoteName('#__k2_tags', 'tag')); // Tags should be published $query->where($db->quoteName('tag.state') . ' = 1'); // Join over the reference table $query->leftJoin($db->quoteName('#__k2_tags_xref', 'xref') . ' ON ' . $db->quoteName('xref.tagId') . ' = ' . $db->quoteName('tag.id')); // Join over the items table $query->leftJoin($db->quoteName('#__k2_items', 'item') . ' ON ' . $db->quoteName('item.id') . ' = ' . $db->quoteName('xref.itemId')); // Items should be published $query->where($db->quoteName('item.state') . ' = 1'); // Handle categories $categories = K2ModelCategories::getCategoryFilter($this->getState('categories'), $this->getState('recursive'), true); // user cannot see any category return empty data if (empty($categories)) { return array(); } // Apply the filter to the query $query->where($db->quoteName('item.catid') . ' IN (' . implode(',', $categories) . ')'); // Check access level $viewlevels = array_unique(JFactory::getUser()->getAuthorisedViewLevels()); $query->where($db->quoteName('item.access') . ' IN (' . implode(',', $viewlevels) . ')'); // Check publish up/down $date = JFactory::getDate()->toSql(); $query->where('(' . $db->quoteName('item.publish_up') . ' = ' . $db->Quote($db->getNullDate()) . ' OR ' . $db->quoteName('item.publish_up') . ' <= ' . $db->Quote($date) . ')'); $query->where('(' . $db->quoteName('item.publish_down') . ' = ' . $db->Quote($db->getNullDate()) . ' OR ' . $db->quoteName('item.publish_down') . ' >= ' . $db->Quote($date) . ')'); // Group by tag Id $query->order($db->quoteName('counter') . ' DESC'); // Group by tag Id $query->group($db->quoteName('tag.id')); // Set query $db->setQuery($query, 0, (int) $this->getState('limit')); // Get rows $rows = $db->loadObjectList(); // Return return $rows; }
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); } }