Created
March 30, 2015 14:31
-
-
Save ryandemmer/9d630e767a7abe635b1d to your computer and use it in GitHub Desktop.
JCE 3.0 Search Plugin Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package JCE | |
* @copyright Copyright (c) 2009-2014 Ryan Demmer. All rights reserved. | |
* @license GNU/GPL 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html | |
* JCE is free software. This version may have been modified pursuant | |
* to the GNU General Public License, and as distributed it includes or | |
* is derivative of works licensed under the GNU General Public License or | |
* other free or open source software licenses. | |
*/ | |
namespace Wf\Plugins\Search; | |
use Wf\Application\Application; | |
use Wf\Plugin\Observer; | |
use Wf\Plugin\Document\Template; | |
use JPluginHelper; | |
use JFactory; | |
use JFilterInput; | |
use JURI; | |
use stdClass; | |
defined('WF_EDITOR') or die; | |
class Joomla extends Observer { | |
protected $plugins = array(); | |
/** | |
* Constructor activating the default information of the class | |
* | |
* @access protected | |
*/ | |
public function __construct(&$observable, $config = array()) { | |
$app = Application::getInstance(); | |
$this->plugins = $app->getParam('search.joomla.plugins'); | |
// use tested defaults | |
if (empty($this->plugins)) { | |
$this->plugins = array('categories', 'contacts', 'content', 'newsfeeds', 'weblinks'); | |
} | |
foreach ($this->plugins as $plugin) { | |
if (JPluginHelper::isEnabled('search', $plugin)) { | |
JPluginHelper::importPlugin('search', $plugin); | |
} | |
} | |
parent::__construct($observable, $config); | |
} | |
public function onSearchPluginDisplay() {} | |
public function isEnabled() { | |
$app = Application::getInstance(); | |
return (bool) $app->getParam('search.joomla.enable', 0); | |
} | |
/** | |
* Method to get the search areas | |
*/ | |
protected function getAreas() { | |
$app = JFactory::getApplication('site'); | |
$areas = array(); | |
$results = array(); | |
$searchareas = $app->triggerEvent('onContentSearchAreas'); | |
foreach ($searchareas as $area) { | |
if (is_array($area)) { | |
$areas = array_merge($areas, $area); | |
} | |
} | |
foreach ($areas as $k => $v) { | |
$results[$k] = JText::_($v); | |
} | |
return $results; | |
} | |
/* | |
* Render Search fields | |
* This method uses portions of SearchViewSearch::display from components/com_search/views/search/view.html.php | |
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. | |
*/ | |
public function onSearchPluginGetOptions() { | |
// built select lists | |
$orders = array(); | |
$orders[] = JHtml::_('select.option', 'newest', JText::_('WF_SEARCH_NEWEST_FIRST')); | |
$orders[] = JHtml::_('select.option', 'oldest', JText::_('WF_SEARCH_OLDEST_FIRST')); | |
$orders[] = JHtml::_('select.option', 'popular', JText::_('WF_SEARCH_MOST_POPULAR')); | |
$orders[] = JHtml::_('select.option', 'alpha', JText::_('WF_SEARCH_ALPHABETICAL')); | |
$orders[] = JHtml::_('select.option', 'category', JText::_('WF_CATEGORY')); | |
$lists = array(); | |
$lists['ordering'] = JHtml::_('select.genericlist', $orders, 'ordering', 'class="span9"', 'value', 'text'); | |
$searchphrases = array(); | |
$searchphrases[] = JHtml::_('select.option', 'all', JText::_('WF_SEARCH_ALL_WORDS')); | |
$searchphrases[] = JHtml::_('select.option', 'any', JText::_('WF_SEARCH_ANY_WORDS')); | |
$searchphrases[] = JHtml::_('select.option', 'exact', JText::_('WF_SEARCH_EXACT_PHRASE')); | |
$lists['searchphrase'] = JHtml::_('select.radiolist', $searchphrases, 'searchphrase', 'class="radio inline"', 'value', 'text', 'all'); | |
$lists['searchareas'] = $this->getAreas(); | |
$template = new Template(); | |
$template->setPaths(array(__DIR__ . '/tmpl')); | |
$template->set('lists', $lists); | |
return (string) $template; | |
} | |
/** | |
* Process search | |
* @param type $query Search query | |
* @return array Rerach Results | |
* | |
* This method uses portions of SearchController::search from components/com_search/controller.php | |
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. | |
*/ | |
public function onSearchPluginSearch($query) { | |
if (empty($query)) { | |
return null; | |
} | |
$filter = JFilterInput::getInstance(); | |
$app = JFactory::getApplication('site'); | |
// get SearchHelper | |
require_once(JPATH_ADMINISTRATOR . '/components/com_search/helpers/search.php'); | |
// set router mode to RAW | |
$router = $app->getRouter(); | |
$router->setMode(0); | |
// slashes cause errors, <> get stripped anyway later on. # causes problems. | |
$searchword = trim(str_replace(array('#', '>', '<', '\\'), '', $filter->clean($query))); | |
$ordering = $app->input->getWord('ordering', null, 'post'); | |
$searchphrase = $app->input->getWord('searchphrase', 'all', 'post'); | |
$areas = $app->input->get('areas', null, 'post', 'array'); | |
// if searchword enclosed in double quotes, strip quotes and do exact match | |
if (substr($searchword, 0, 1) == '"' && substr($searchword, -1) == '"') { | |
$searchword = substr($searchword, 1, -1); | |
$searchphrase = 'exact'; | |
} | |
// clean areas | |
if (!empty($areas)) { | |
foreach ($areas as $area) { | |
$areas[] = $filter->clean($area, 'cmd'); | |
} | |
} | |
if (!class_exists('JSite')) { | |
// Load JSite class | |
\JLoader::register('JSite', JPATH_SITE . '/includes/application.php'); | |
} | |
// trigger search on loaded plugins | |
$searches = $app->triggerEvent('onContentSearch', array( | |
$searchword, | |
$searchphrase, | |
$ordering, | |
$areas | |
)); | |
$results = array(); | |
$rows = array(); | |
foreach ($searches as $search) { | |
$rows = array_merge((array) $rows, (array) $search); | |
} | |
for ($i = 0, $count = count($rows); $i < $count; $i++) { | |
$row = &$rows[$i]; | |
$result = new stdClass; | |
if ($searchphrase == 'exact') { | |
$searchwords = array($searchword); | |
$needle = $searchword; | |
} else { | |
$searchworda = preg_replace('#\xE3\x80\x80#s', ' ', $searchword); | |
$searchwords = preg_split("/\s+/u", $searchworda); | |
$needle = $searchwords[0]; | |
} | |
// get anchors | |
$anchors = self::getAnchors($row->text); | |
if (!empty($anchors)) { | |
$row->anchors = $anchors; | |
} | |
$row->text = \SearchHelper::prepareSearchContent($row->text, $needle); | |
// remove base url | |
if (strpos($row->href, JURI::base(true)) !== false) { | |
$row->href = substr_replace($row->href, '', 0, strlen(JURI::base(true)) + 1); | |
} | |
$results[] = array( | |
'title' => $row->title, | |
'text' => $row->text, | |
'url' => $row->href, | |
'data' => array() | |
); | |
} | |
return array('label' => 'Joomla Content', 'type' => 'joomla', 'data' => $results); | |
} | |
private static function getAnchors($content) { | |
preg_match_all('#<a([^>]+)(name|id)="([a-z]+[\w\-\:\.]*)"([^>]*)>#i', $content, $matches, PREG_SET_ORDER); | |
$anchors = array(); | |
if (!empty($matches)) { | |
foreach ($matches as $match) { | |
if (strpos($match[0], 'href') === false) { | |
$anchors[] = $match[3]; | |
} | |
} | |
} | |
return $anchors; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment