Created
March 30, 2015 14:32
-
-
Save ryandemmer/a6701fa7b4755a60cc83 to your computer and use it in GitHub Desktop.
Joomla Link Plugin Example for JCE 3.0
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\Link; | |
use Wf\Application\Application; | |
use Wf\Plugin\Observer; | |
use Wf\Plugin\Document\Document; | |
use JFolder; | |
use JFactory; | |
use JVersion; | |
use JText; | |
use JCategories; | |
defined('WF_EDITOR') or die; | |
class Joomla extends Observer { | |
protected $option = array(); | |
protected $adapters = array(); | |
protected $url_pattern = '#index.php\?option=%s&?(.*)#'; | |
protected $path_pattern = '#%s:(.*)\/?(.*)#'; | |
/** | |
* Constructor activating the default information of the class | |
* | |
* @access protected | |
*/ | |
public function __construct(&$observable, $config = array()) { | |
// Get all files | |
$files = JFolder::files(__DIR__ . '/adapter', '\.(php)$', false, true); | |
if (!empty($files)) { | |
foreach ($files as $file) { | |
$classname = '\\Wf\\Plugins\\Link\\Joomla\\Adapter\\' . ucfirst(basename($file, '.php')); | |
if (class_exists($classname)) { | |
$this->adapters[] = new $classname; | |
} | |
} | |
} | |
parent::__construct($observable, $config); | |
} | |
public function onLinkPluginInit() { | |
} | |
public function onLinkPluginDisplay() { | |
$document = Document::getInstance(); | |
$document->addStyleSheet('adapter/search/joomla/css/joomla.css'); | |
} | |
public function isEnabled() { | |
$app = Application::getInstance(); | |
return (bool) $app->getParam('links.joomla.enable', 0); | |
} | |
public function onLinkPluginGetItems($data, $options = array()) { | |
foreach ($this->adapters as $adapter) { | |
$option = $adapter->getOption(); | |
// parse path style URL, eg: com_content:Articles/2:Uncategorized | |
if (preg_match(sprintf($this->path_pattern, $option), $data, $matches)) { | |
$data = $matches[0]; | |
// remove trailing and leading slash | |
$data = trim($data, '/'); | |
// create array from data string | |
$data = explode('/', $data); | |
// get values from path | |
array_walk($data, function(&$value) { | |
if (strpos($value, ':') !== false) { | |
$value = substr($value, 0, strpos($value, ':')); | |
} | |
}); | |
$args = array('id' => array_pop($data)); | |
return $adapter->getItems($args, $options); | |
} | |
// parse Joomla URL, eg: index.php?option=com_content&view=category&id=1 | |
$data = str_replace('&', '&', $data); | |
if (preg_match(sprintf($this->url_pattern, $option), $data, $matches)) { | |
parse_str($matches[1], $data); | |
return $adapter->getItems($data, $options); | |
} | |
} | |
return null; | |
} | |
public function onLinkPluginGetIcon() { | |
$icons = array(); | |
foreach ($this->adapters as $adapter) { | |
$icons['folders'][] = $adapter->getIcon(); | |
} | |
return $icons; | |
} | |
public static function createPath($options) { | |
$db = JFactory::getDBO(); | |
$query = $db->getQuery(true); | |
$path = array(); | |
extract($options); | |
if (empty($option)) { | |
return $path; | |
} | |
// add option name | |
$path[] = $option . ':' . JText::_($option); | |
// get categories from id | |
if (!empty($id)) { | |
$categories = JCategories::getInstance(str_replace('com_', '', $option)); | |
$node = $categories->get($id); | |
$title = $node->title; | |
if (is_numeric($id)) { | |
$path[] = $id . ':' . $title; | |
} | |
while($node && $node->hasParent()) { | |
$parent = $node->getParent(); | |
if (is_numeric($parent->id)) { | |
$path[] = $parent->id . ':' . $parent->title; | |
} | |
$node = $categories->get($parent->id); | |
} | |
} | |
array_reverse($path); | |
return implode('/', $path); | |
} | |
/** | |
* Category function used by many extensions | |
* | |
* @access public | |
* @return Category list object. | |
* @since 1.5 | |
*/ | |
public static function getCategory($extension, $parent = 1) { | |
$db = JFactory::getDBO(); | |
$user = JFactory::getUser(); | |
$app = Application::getInstance(); | |
$query = $db->getQuery(true); | |
$where = array(); | |
$version = new JVersion(); | |
$language = $version->isCompatible('3.0') ? ', language' : ''; | |
$where[] = 'parent_id = ' . (int) $parent; | |
$where[] = 'extension = ' . $db->Quote($extension); | |
$where[] = 'access IN (' . implode(',', $user->getAuthorisedViewLevels()) . ')'; | |
if (!$app->getParam('link.joomla.static', 1)) { | |
$where[] = 'path != ' . $db->Quote('uncategorised'); | |
} | |
if ($app->getParam('link.joomla.category_alias', 1) == 1) { | |
//sqlsrv changes | |
$case = ', CASE WHEN '; | |
$case .= $query->charLength('alias', '!=', '0'); | |
$case .= ' THEN '; | |
$a_id = $query->castAsChar('id'); | |
$case .= $query->concatenate(array($a_id, 'alias'), ':'); | |
$case .= ' ELSE '; | |
$case .= $a_id . ' END as slug'; | |
} | |
$where[] = 'published = 1'; | |
$query->select('id AS slug, id AS id, title, alias, UNIX_TIMESTAMP(modified_time) AS date, access' . $language . $case)->from('#__categories')->where($where)->order('title'); | |
$db->setQuery($query); | |
return $db->loadObjectList(); | |
} | |
/** | |
* (Attempt to) Get an Itemid | |
* | |
* @access public | |
* @param string $component | |
* @param array $needles | |
* @return Category list object. | |
*/ | |
public function getItemId($component, $needles = array()) { | |
$match = null; | |
require_once(JPATH_SITE . '/includes/application.php'); | |
$tag = 'component_id'; | |
$component = JComponentHelper::getComponent($component); | |
$menu = JSite::getMenu(); | |
$items = $menu->getItems($tag, $component->id); | |
if ($items) { | |
foreach ($needles as $needle => $id) { | |
foreach ($items as $item) { | |
if ((@$item->query['view'] == $needle) && (@$item->query['id'] == $id)) { | |
$match = $item->id; | |
break; | |
} | |
} | |
if (isset($match)) { | |
break; | |
} | |
} | |
} | |
return $match ? '&Itemid=' . $match : ''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment