/** * Get the search result * The sql must return the following fields that are used in a common display * routine: href, title, section, created, text, browsernav * @param string Target search string * @param string mathcing option, exact|any|all * @param string ordering option, newest|oldest|popular|alpha|category * @param mixed An array if the search it to be restricted to areas, null if search all */ function getSearchResults($text, $phrase = '', $ordering = '', $areas = null) { global $jlistConfig; $db = JFactory::getDbo(); $app = JFactory::getApplication(); $user = JFactory::getUser(); $groups = implode(',', $user->getAuthorisedViewLevels()); $tag = JFactory::getLanguage()->getTag(); require_once JPATH_SITE . '/components/com_jdownloads/helpers/route.php'; require_once JPATH_SITE . '/components/com_jdownloads/helpers/search.php'; require_once JPATH_SITE . '/components/com_jdownloads/helpers/jdownloadshelper.php'; $user_rules = JDHelper::getUserRules(); $searchText = $text; $limit = $this->state->get('search_limit', 500); $nullDate = $db->getNullDate(); $date = JFactory::getDate(); $now = $date->toSql(); $text = trim($text); if ($text == '') { return array(); } $wheres = array(); switch ($phrase) { case 'exact': $text = $db->Quote('%' . $db->escape($text, true) . '%', false); $wheres2 = array(); if (!$areas || in_array('title', $areas)) { $wheres2[] = 'a.file_title LIKE ' . $text; } if (!$areas || in_array('description', $areas)) { $wheres2[] = 'a.description LIKE ' . $text; $wheres2[] = 'a.description_long LIKE ' . $text; } if (!$areas || in_array('changelog', $areas)) { $wheres2[] = 'a.changelog LIKE ' . $text; } if ($jlistConfig['custom.field.6.title'] != '') { if (!$areas || in_array('customtext1', $areas)) { $wheres2[] = 'a.custom_field_6 LIKE ' . $text; } } if ($jlistConfig['custom.field.7.title'] != '') { if (!$areas || in_array('customtext1', $areas)) { $wheres2[] = 'a.custom_field_7 LIKE ' . $text; } } if ($jlistConfig['custom.field.8.title'] != '') { if (!$areas || in_array('customtext1', $areas)) { $wheres2[] = 'a.custom_field_8 LIKE ' . $text; } } if ($jlistConfig['custom.field.9.title'] != '') { if (!$areas || in_array('customtext1', $areas)) { $wheres2[] = 'a.custom_field_9 LIKE ' . $text; } } if ($jlistConfig['custom.field.10.title'] != '') { if (!$areas || in_array('customtext1', $areas)) { $wheres2[] = 'a.custom_field_10 LIKE ' . $text; } } if ($jlistConfig['custom.field.13.title'] != '') { if (!$areas || in_array('customtext1', $areas)) { $wheres2[] = 'a.custom_field_13 LIKE ' . $text; } } if ($jlistConfig['custom.field.14.title'] != '') { if (!$areas || in_array('customtext1', $areas)) { $wheres2[] = 'a.custom_field_14 LIKE ' . $text; } } if (!$areas || in_array('author', $areas)) { $wheres2[] = 'a.author LIKE ' . $text; } if (!$areas || in_array('metatags', $areas)) { $wheres2[] = 'a.metakey LIKE ' . $text; $wheres2[] = 'a.metadesc LIKE ' . $text; } $where = '(' . implode(') OR (', $wheres2) . ')'; break; case 'all': case 'any': default: $words = explode(' ', $text); $wheres = array(); foreach ($words as $word) { $word = $db->Quote('%' . $db->escape($word, true) . '%', false); $wheres2 = array(); if (!$areas || in_array('title', $areas)) { $wheres2[] = 'a.file_title LIKE ' . $word; } if (!$areas || in_array('description', $areas)) { $wheres2[] = 'a.description LIKE ' . $word; $wheres2[] = 'a.description_long LIKE ' . $word; } if (!$areas || in_array('changelog', $areas)) { $wheres2[] = 'a.changelog LIKE ' . $word; } if ($jlistConfig['custom.field.6.title'] != '') { if (!$areas || in_array('customtext1', $areas)) { $wheres2[] = 'a.custom_field_6 LIKE ' . $word; } } if ($jlistConfig['custom.field.7.title'] != '') { if (!$areas || in_array('customtext1', $areas)) { $wheres2[] = 'a.custom_field_7 LIKE ' . $word; } } if ($jlistConfig['custom.field.8.title'] != '') { if (!$areas || in_array('customtext1', $areas)) { $wheres2[] = 'a.custom_field_8 LIKE ' . $word; } } if ($jlistConfig['custom.field.9.title'] != '') { if (!$areas || in_array('customtext1', $areas)) { $wheres2[] = 'a.custom_field_9 LIKE ' . $word; } } if ($jlistConfig['custom.field.10.title'] != '') { if (!$areas || in_array('customtext1', $areas)) { $wheres2[] = 'a.custom_field_10 LIKE ' . $word; } } if ($jlistConfig['custom.field.13.title'] != '') { if (!$areas || in_array('customtext1', $areas)) { $wheres2[] = 'a.custom_field_13 LIKE ' . $word; } } if ($jlistConfig['custom.field.14.title'] != '') { if (!$areas || in_array('customtext2', $areas)) { $wheres2[] = 'a.custom_field_14 LIKE ' . $word; } } if (!$areas || in_array('author', $areas)) { $wheres2[] = 'a.author LIKE ' . $word; } if (!$areas || in_array('metatags', $areas)) { $wheres2[] = 'a.metakey LIKE ' . $word; $wheres2[] = 'a.metadesc LIKE ' . $word; } $wheres[] = implode(' OR ', $wheres2); } $where = '(' . implode($phrase == 'all' ? ') AND (' : ') OR (', $wheres) . ')'; break; } $morder = ''; switch ($ordering) { case 'oldest': $order = 'a.date_added ASC'; break; case 'popular': $order = 'a.downloads DESC'; break; case 'alpha': $order = 'a.file_title ASC'; break; case 'category': $order = 'c.title ASC, a.file_title ASC'; $morder = 'a.file_title ASC'; break; case 'newest': default: $order = 'a.date_added DESC'; break; } $uncategorised = JText::_('COM_JDOWNLOADS_SELECT_UNCATEGORISED'); $rows = array(); $query = $db->getQuery(true); // search downloads if ($limit > 0) { $query->clear(); //sqlsrv changes $case_when = ' CASE WHEN '; $case_when .= $query->charLength('a.file_alias'); $case_when .= ' THEN '; $a_id = $query->castAsChar('a.file_id'); $case_when .= $query->concatenate(array($a_id, 'a.file_alias'), ':'); $case_when .= ' ELSE '; $case_when .= $a_id . ' END as slug'; $case_when1 = ' CASE WHEN '; $case_when1 .= $query->charLength('c.alias'); $case_when1 .= ' THEN '; $c_id = $query->castAsChar('c.id'); $case_when1 .= $query->concatenate(array($c_id, 'c.alias'), ':'); $case_when1 .= ' ELSE '; $case_when1 .= $c_id . ' END as catslug'; $query->select('a.file_title AS title, a.metadesc, a.metakey, a.url_download, a.extern_file, a.other_file_id, a.license_agree, a.password, a.author, a.date_added AS created, a.language, a.custom_field_6, a.custom_field_7, a.custom_field_8, a.custom_field_9, a.custom_field_10'); $query->select($query->concatenate(array('a.description', 'a.description_long', 'a.changelog', 'a.custom_field_13', 'a.custom_field_14')) . ' AS text'); $query->select('CASE c.title WHEN \'root\' THEN ' . $db->Quote($uncategorised) . ' ELSE c.title END AS section, ' . $case_when . ',' . $case_when1 . ', ' . '\'2\' AS browsernav'); $query->from('#__jdownloads_files AS a'); $query->innerJoin('#__jdownloads_categories AS c ON c.id = a.cat_id'); $query->where('(' . $where . ')' . 'AND a.published = 1 AND c.published = 1 AND a.access IN (' . $groups . ') ' . 'AND c.access IN (' . $groups . ') ' . 'AND (a.publish_from = ' . $db->Quote($nullDate) . ' OR a.publish_from <= ' . $db->Quote($now) . ') ' . 'AND (a.publish_to = ' . $db->Quote($nullDate) . ' OR a.publish_to >= ' . $db->Quote($now) . ')'); $query->group('a.file_id, a.file_title, a.metadesc, a.metakey, a.author, a.date_added, a.description, a.description_long, a.changelog, a.custom_field_6, a.custom_field_7, a.custom_field_8, a.custom_field_9, a.custom_field_10, a.custom_field_13, a.custom_field_14, c.title, a.file_alias, c.alias, c.id'); $query->order($order); // Filter by language if ($app->isSite() && $app->getLanguageFilter()) { $query->where('a.language in (' . $db->Quote($tag) . ',' . $db->Quote('*') . ')'); $query->where('c.language in (' . $db->Quote($tag) . ',' . $db->Quote('*') . ')'); } $db->setQuery($query, 0, $limit); $list = $db->loadObjectList(); $limit -= count($list); if (isset($list)) { foreach ($list as $key => $item) { $direct_download = $jlistConfig['direct.download']; if (!$item->url_download && !$item->extern_file && !$item->other_file_id || $item->password || $item->license_agree || $user_rules->view_captcha) { // this download is a simple document without a file so we can not use 'direct' download option // or we need the summary page for password, captcha or license agree $direct_download = 0; } if ($jlistConfig['view.detailsite']) { // we must link to the details page $list[$key]->href = JDownloadsHelperRoute::getDownloadRoute($item->slug, $item->catslug, $item->language); } else { if ($direct_download) { // we must start the download process directly $list[$key]->href = JRoute::_('index.php?option=com_jdownloads&task=download.send&id=' . (int) $item->slug . '&catid=' . (int) $item->catslug . '&m=0'); } else { if (!$item->url_download && !$item->extern_file && !$item->other_file_id) { // Download is only a simple document without a file so we must link to the details page $list[$key]->href = JDownloadsHelperRoute::getDownloadRoute($item->slug, $item->catslug, $item->language); } else { // we must link to the summary page $list[$key]->href = JRoute::_('index.php?option=com_jdownloads&view=summary&id=' . $item->slug . '&catid=' . (int) $item->catslug); } } } } } $rows[] = $list; } $results = array(); if (count($rows)) { foreach ($rows as $row) { $new_row = array(); foreach ($row as $key => $download) { if (JDSearchHelper::checkNoHTML($download, $searchText, array('text', 'title', 'author', 'metadesc', 'metakey', 'custom_field_6', 'custom_field_7', 'custom_field_8', 'custom_field_9', 'custom_field_10'))) { $new_row[] = $download; } } $results = array_merge($results, (array) $new_row); } } return $results; }
function display($tpl = null) { global $jlistConfig; require_once JPATH_COMPONENT . '/helpers/search.php'; // Initialise some variables $app = JFactory::getApplication(); $user = JFactory::getUser(); $pathway = $app->getPathway(); $uri = JFactory::getURI(); $jd_user_settings = JDHelper::getUserRules(); $error = null; $rows = null; $results = null; $total = 0; // Get some data from the model $areas = $this->get('areas'); $state = $this->get('state'); $searchword = $state->get('keyword'); $params = $app->getParams(); $menus = $app->getMenu(); $menu = $menus->getActive(); // add all needed cripts and css files $document = JFactory::getDocument(); $document->addScript(JURI::base() . 'components/com_jdownloads/assets/js/jdownloads.js'); $document->addStyleSheet(JURI::base() . "components/com_jdownloads/assets/css/jdownloads_fe.css", "text/css", null, array()); $document->addStyleSheet(JURI::base() . "components/com_jdownloads/assets/css/jdownloads_buttons.css", "text/css", null, array()); $custom_css_path = JPATH_ROOT . '/components/com_jdownloads/assets/css/jdownloads_custom.css'; if (JFile::exists($custom_css_path)) { $document->addStyleSheet(JURI::base() . "components/com_jdownloads/assets/css/jdownloads_custom.css", 'text/css', null, array()); } $document->addScriptDeclaration('var live_site = "' . JURI::base() . '";'); $document->addScriptDeclaration('function openWindow (url) { fenster = window.open(url, "_blank", "width=550, height=480, STATUS=YES, DIRECTORIES=NO, MENUBAR=NO, SCROLLBARS=YES, RESIZABLE=NO"); fenster.focus(); }'); // because the application sets a default page title, we need to get it // right from the menu item itself if (is_object($menu)) { $menu_params = new JRegistry(); $menu_params->loadString($menu->params); if (!$menu_params->get('page_title')) { $params->set('page_title', JText::_('COM_JDOWNLOADS_SEARCH')); } } else { $params->set('page_title', JText::_('COM_JDOWNLOADS_SEARCH')); } $title = $params->get('page_title'); if ($app->getCfg('sitename_pagetitles', 0) == 1) { $title = JText::sprintf('JPAGETITLE', $app->getCfg('sitename'), $title); } elseif ($app->getCfg('sitename_pagetitles', 0) == 2) { $title = JText::sprintf('JPAGETITLE', $title, $app->getCfg('sitename')); } $this->document->setTitle($title); if ($params->get('menu-meta_description')) { $this->document->setDescription($params->get('menu-meta_description')); } if ($params->get('menu-meta_keywords')) { $this->document->setMetadata('keywords', $params->get('menu-meta_keywords')); } if ($params->get('robots')) { $this->document->setMetadata('robots', $params->get('robots')); } // upload icon handling $this->view_upload_button = false; if ($jd_user_settings->uploads_view_upload_icon) { // we must here check whether the user has the permissions to create new downloads // this can be defined in the components permissions but also in any category // but the upload icon is only viewed when in the user groups settings is also activated the: 'display add/upload icon' option // 1. check the component permissions if (!$user->authorise('core.create', 'com_jdownloads')) { // 2. not global permissions so we must check now every category (for a lot of categories can this be very slow) $this->authorised_cats = JDHelper::getAuthorisedJDCategories('core.create', $user); if (count($this->authorised_cats) > 0) { $this->view_upload_button = true; } } else { $this->view_upload_button = true; } } // built select lists $orders = array(); $orders[] = JHtml::_('select.option', 'newest', JText::_('COM_JDOWNLOADS_SEARCH_NEWEST_FIRST')); $orders[] = JHtml::_('select.option', 'oldest', JText::_('COM_JDOWNLOADS_SEARCH_OLDEST_FIRST')); $orders[] = JHtml::_('select.option', 'popular', JText::_('COM_JDOWNLOADS_SEARCH_MOST_POPULAR')); $orders[] = JHtml::_('select.option', 'alpha', JText::_('COM_JDOWNLOADS_SEARCH_ALPHABETICAL')); $orders[] = JHtml::_('select.option', 'category', JText::_('COM_JDOWNLOADS_SEARCH_CATEGORY')); $lists = array(); $lists['ordering'] = JHtml::_('select.genericlist', $orders, 'ordering', 'class="inputbox"', 'value', 'text', $state->get('ordering')); $searchphrases = array(); $searchphrases[] = JHtml::_('select.option', 'all', JText::_('COM_JDOWNLOADS_SEARCH_ALL_WORDS')); $searchphrases[] = JHtml::_('select.option', 'any', JText::_('COM_JDOWNLOADS_SEARCH_ANY_WORDS')); $searchphrases[] = JHtml::_('select.option', 'exact', JText::_('COM_JDOWNLOADS_SEARCH_EXACT_PHRASE')); $lists['searchphrase'] = JHtml::_('select.radiolist', $searchphrases, 'searchphrase', '', 'value', 'text', $state->get('match')); // log the search // not used currently // SearchHelper::logSearch($searchword); //limit searchword $lang = JFactory::getLanguage(); $upper_limit = $lang->getUpperLimitSearchWord(); $lower_limit = $lang->getLowerLimitSearchWord(); if (JDSearchHelper::limitSearchWord($searchword)) { $error = JText::sprintf('COM_JDOWNLOADS_ERROR_SEARCH_MESSAGE', $lower_limit, $upper_limit); } //sanatise searchword if (JDSearchHelper::santiseSearchWord($searchword, $state->get('match'))) { $error = JText::_('COM_JDOWNLOADS_ERROR_IGNOREKEYWORD'); } if (!$searchword && count(JRequest::get('post'))) { //$error = JText::_('COM_JDOWNLOADS_ERROR_ENTERKEYWORD'); } // put the filtered results back into the model // for next release, the checks should be done in the model perhaps... $state->set('keyword', $searchword); if ($error == null) { $results = $this->get('data'); $total = $this->get('total'); $pagination = $this->get('pagination'); // require_once JPATH_SITE . '/components/com_jdownloads/helpers/route.php'; for ($i = 0, $count = count($results); $i < $count; $i++) { $row =& $results[$i]->text; if ($state->get('match') == 'exact') { $searchwords = array($searchword); $needle = $searchword; } else { $searchworda = preg_replace('#\\xE3\\x80\\x80#s', ' ', $searchword); $searchwords = preg_split("/\\s+/u", $searchworda); $needle = $searchwords[0]; } $row = JDSearchHelper::prepareSearchContent($row, $needle); $searchwords = array_unique($searchwords); $searchRegex = '#('; $x = 0; foreach ($searchwords as $k => $hlword) { $searchRegex .= $x == 0 ? '' : '|'; $searchRegex .= preg_quote($hlword, '#'); $x++; } $searchRegex .= ')#iu'; $row = preg_replace($searchRegex, '<span class="highlight">\\0</span>', $row); $result =& $results[$i]; if ($result->created) { $created = JHtml::_('date', $result->created, JText::_('DATE_FORMAT_LC3')); } else { $created = ''; } $result->text = JHtml::_('content.prepare', $result->text, '', 'com_jdownloads.search'); $result->created = $created; $result->count = $i + 1; } } // Check for layout override $active = JFactory::getApplication()->getMenu()->getActive(); if (isset($active->query['layout'])) { $this->setLayout($active->query['layout']); } //Escape strings for HTML output $this->pageclass_sfx = htmlspecialchars($params->get('pageclass_sfx')); $this->assignRef('pagination', $pagination); $this->assignRef('results', $results); $this->assignRef('lists', $lists); $this->assignRef('params', $params); $this->ordering = $state->get('ordering'); $this->searchword = $searchword; $this->origkeyword = $state->get('origkeyword'); $this->searchphrase = $state->get('match'); $this->searchareas = $areas; $this->total = $total; $this->error = $error; $this->action = $uri; parent::display($tpl); }