327 lines
12 KiB
PHP
327 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* @version 6.2.6 tabulizer $
|
|
* @package tabulizer
|
|
* @copyright Copyright © 2011 - All rights reserved.
|
|
* @license GNU/GPL
|
|
* @author Dimitrios Mourloukos
|
|
* @author mail info@alterora.gr
|
|
* @website www.tabulizer.com
|
|
*
|
|
*/
|
|
|
|
|
|
// no direct access
|
|
defined('_JEXEC') or die('Restricted access');
|
|
|
|
|
|
/**
|
|
* Tabulizerds Search plugin
|
|
*
|
|
* @package Joomla.Plugin
|
|
* @subpackage Search.content
|
|
* @since 1.6
|
|
*/
|
|
class plgSearchTabulizerds extends JPlugin
|
|
{
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @access protected
|
|
* @param object $subject The object to observe
|
|
* @param array $config An array that holds the plugin configuration
|
|
* @since 1.5
|
|
*/
|
|
public function __construct(& $subject, $config)
|
|
{
|
|
parent::__construct($subject, $config);
|
|
$this->loadLanguage();
|
|
}
|
|
|
|
/**
|
|
* @return array An array of search areas
|
|
*/
|
|
function onContentSearchAreas()
|
|
{
|
|
static $areas = array(
|
|
'tabulizerds' => 'PLG_SEARCH_TABULIZERDS_TABULIZERDS'
|
|
);
|
|
return $areas;
|
|
}
|
|
|
|
function onContentSearch( $text, $phrase='', $ordering='', $areas=null )
|
|
{
|
|
$db = JFactory::getDBO();
|
|
$app = JFactory::getApplication();
|
|
$user = JFactory::getUser();
|
|
$groups = implode(',', $user->getAuthorisedViewLevels());
|
|
$tag = JFactory::getLanguage()->getTag();
|
|
|
|
require_once JPATH_ADMINISTRATOR . '/components/com_tabulizer/assets/classes/common/helper.php';
|
|
|
|
// If the array is not correct, return it:
|
|
if (is_array( $areas )) {
|
|
if (!array_intersect( $areas, array_keys( $this->onContentSearchAreas() ) )) {
|
|
return array();
|
|
}
|
|
}
|
|
|
|
// Use the PHP function trim to delete spaces in front of or at the back of the searching terms
|
|
$text = trim( $text );
|
|
|
|
// Return Array when nothing was filled in.
|
|
if ($text == '') {
|
|
return array();
|
|
}
|
|
|
|
$query = 'SELECT * FROM #__content WHERE (`introtext` LIKE '.$db->quote('%{tabulizer:data_source%').') OR (`fulltext` LIKE '.$db->quote('%{tabulizer:data_source%').')';
|
|
$db->setQuery($query);
|
|
$articles = $db->loadObjectList();
|
|
if (empty($articles)) {
|
|
return array();
|
|
}
|
|
|
|
$article_ids = array();
|
|
foreach ($articles as $article) {
|
|
$full_content = $article->introtext . $article->fulltext;
|
|
$ds_content = '';
|
|
# find occurances of tabulizer data source directive that contain specified user parameters (editor)
|
|
$pattern = TABULIZER_DATA_SOURCE_PARAMS_REGEX;
|
|
while (preg_match($pattern, $full_content, $regs, PREG_OFFSET_CAPTURE)) {
|
|
TabulizerPath::requireLib('data_source','common');
|
|
$data_source_tag = $regs[1][0];
|
|
$data_source_user_params = base64_decode($regs[2][0]);
|
|
|
|
if (preg_match('/^[a-z0-9\._\-]{2,128}$/i',$data_source_tag)) {
|
|
$ds_content .= TabulizerDataSource::getTableHTML($data_source_tag,$data_source_user_params);
|
|
}
|
|
// Replace the found tabulizer directive
|
|
$full_content = substr_replace($full_content, '', $regs[0][1], strlen($regs[0][0]));
|
|
}
|
|
|
|
# find occurances of tabulizer data source directive with no user parameters (editor)
|
|
$pattern = TABULIZER_DATA_SOURCE_REGEX;
|
|
while (preg_match($pattern, $full_content, $regs, PREG_OFFSET_CAPTURE)) {
|
|
TabulizerPath::requireLib('data_source','common');
|
|
$data_source_tag = $regs[1][0];
|
|
|
|
if (preg_match('/^[a-z0-9\._\-]{2,128}$/i',$data_source_tag)) {
|
|
$ds_content .= TabulizerDataSource::getTableHTML($data_source_tag);
|
|
}
|
|
// Replace the found tabulizer directive
|
|
$full_content = substr_replace($full_content, '', $regs[0][1], strlen($regs[0][0]));
|
|
}
|
|
|
|
if (!empty($ds_content)) {
|
|
// search for keywords
|
|
$found = false;
|
|
switch ($phrase) {
|
|
case 'exact':
|
|
if (preg_match('/'.preg_quote($text, '/').'/imu',$ds_content)) {
|
|
$found = true;
|
|
}
|
|
break;
|
|
case 'all':
|
|
$words = explode(' ', $text);
|
|
if (!empty($words)) {
|
|
$found = true;
|
|
foreach ($words as $word) {
|
|
if (!preg_match('/' . preg_quote($word, '/') . '/imu', $ds_content)) {
|
|
$found = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case 'any':
|
|
default:
|
|
$words = explode(' ', $text);
|
|
foreach ($words as $word) {
|
|
if (preg_match('/'.preg_quote($word, '/').'/imu',$ds_content)) {
|
|
$found = true;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
if ($found) $article_ids[] = $article->id;
|
|
}
|
|
}
|
|
|
|
if (empty($article_ids)) {
|
|
return array();
|
|
}
|
|
|
|
$where = 'a.id IN ('.implode(',',$article_ids).') ';
|
|
|
|
switch ($ordering) {
|
|
case 'oldest':
|
|
$order = 'a.created ASC';
|
|
break;
|
|
|
|
case 'popular':
|
|
$order = 'a.hits DESC';
|
|
break;
|
|
|
|
case 'alpha':
|
|
$order = 'a.title ASC';
|
|
break;
|
|
|
|
case 'category':
|
|
$order = 'c.title ASC, a.title ASC';
|
|
break;
|
|
|
|
case 'newest':
|
|
default:
|
|
$order = 'a.created DESC';
|
|
break;
|
|
}
|
|
|
|
$sContent = $this->params->get('search_content', 1);
|
|
$sArchived = $this->params->get('search_archived', 1);
|
|
$limit = $this->params->def('search_limit', 50);
|
|
|
|
$nullDate = $db->getNullDate();
|
|
$date = new JDate();
|
|
$now = $date->toSql();
|
|
|
|
$rows = array();
|
|
$query = $db->getQuery(true);
|
|
|
|
// search articles
|
|
if ($sContent && $limit > 0)
|
|
{
|
|
$query->clear();
|
|
//sqlsrv changes
|
|
$case_when = ' CASE WHEN ';
|
|
$case_when .= $query->charLength('a.alias');
|
|
$case_when .= ' THEN ';
|
|
$a_id = $query->castAsChar('a.id');
|
|
$case_when .= $query->concatenate(array($a_id, 'a.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.title AS title, a.metadesc, a.metakey, a.created AS created');
|
|
$query->select($query->concatenate(array('a.introtext', 'a.fulltext')).' AS text');
|
|
$query->select('c.title AS section, '.$case_when.','.$case_when1.', '.'\'2\' AS browsernav');
|
|
|
|
$query->from('#__content AS a');
|
|
$query->innerJoin('#__categories AS c ON c.id=a.catid');
|
|
$query->where('('. $where .')' . 'AND a.state=1 AND c.published = 1 AND a.access IN ('.$groups.') '
|
|
.'AND c.access IN ('.$groups.') '
|
|
.'AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).') '
|
|
.'AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).')' );
|
|
$query->group('a.id, a.title, a.metadesc, a.metakey, a.created, a.introtext, a.fulltext, c.title, a.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)
|
|
{
|
|
$list[$key]->href = ContentHelperRoute::getArticleRoute($item->slug, $item->catslug);
|
|
}
|
|
}
|
|
$rows[] = $list;
|
|
}
|
|
|
|
// search archived content
|
|
if ($sArchived && $limit > 0)
|
|
{
|
|
$searchArchived = JText::_('JARCHIVED');
|
|
|
|
$query->clear();
|
|
//sqlsrv changes
|
|
$case_when = ' CASE WHEN ';
|
|
$case_when .= $query->charLength('a.alias');
|
|
$case_when .= ' THEN ';
|
|
$a_id = $query->castAsChar('a.id');
|
|
$case_when .= $query->concatenate(array($a_id, 'a.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.title AS title, a.metadesc, a.metakey, a.created AS created, '
|
|
.$query->concatenate(array("a.introtext", "a.fulltext")).' AS text,'
|
|
.$case_when.','.$case_when1.', '
|
|
.'c.title AS section, \'2\' AS browsernav');
|
|
$query->from('#__content AS a');
|
|
$query->innerJoin('#__categories AS c ON c.id=a.catid AND c.access IN ('. $groups .')');
|
|
$query->where('('. $where .') AND a.state = 2 AND c.published = 1 AND a.access IN ('. $groups
|
|
.') AND c.access IN ('. $groups .') '
|
|
.'AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).') '
|
|
.'AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).')' );
|
|
$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);
|
|
$list3 = $db->loadObjectList();
|
|
|
|
// find an itemid for archived to use if there isn't another one
|
|
$item = $app->getMenu()->getItems('link', 'index.php?option=com_content&view=archive', true);
|
|
$itemid = isset($item->id) ? '&Itemid='.$item->id : '';
|
|
|
|
if (isset($list3))
|
|
{
|
|
foreach($list3 as $key => $item)
|
|
{
|
|
$date = new JDate($item->created);
|
|
|
|
$created_month = $date->format("n");
|
|
$created_year = $date->format("Y");
|
|
|
|
$list3[$key]->href = JRoute::_('index.php?option=com_content&view=archive&year='.$created_year.'&month='.$created_month.$itemid);
|
|
}
|
|
}
|
|
|
|
$rows[] = $list3;
|
|
}
|
|
|
|
$results = array();
|
|
if (count($rows))
|
|
{
|
|
foreach($rows as $row)
|
|
{
|
|
$new_row = array();
|
|
foreach($row as $key => $article) {
|
|
$new_row[] = $article;
|
|
}
|
|
$results = array_merge($results, (array) $new_row);
|
|
}
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
}
|
|
|
|
?>
|