<?php
/**
* @copyright Copyright (c) 2015 Skyline Technology Ltd (http://extstore.com). All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// no direct access
defined('_JEXEC') or die;
jimport('joomla.plugin.plugin');
/**
* Content - Advanced Poll Plugin
*
* @package Joomla.Plugin
* @subpakage ExtStore.AdvPoll
*/
class plgContentAdvPoll extends JPlugin {
/**
* Constructor.
*
* @param $subject
* @param array $config
*/
function __construct(&$subject, $config = array()) {
// call parent constructor
parent::__construct($subject, $config);
$this->loadLanguage();
}
/**
* onContentPrepare hook.
*/
function onContentPrepare($context, &$article, &$params, $page = 0) {
$regex = '#{advpoll(.*?)}#s';
if ($context == 'com_content.article' || $context == 'com_k2.item' || strpos($context, 'com_zoo.element.') !== false) {
require_once JPATH_ADMINISTRATOR . '/components/com_advpoll/helpers/factory.php';
JModelLegacy::addIncludePath(JPATH_ROOT . '/components/com_advpoll/models');
$model = AdvPollFactory::getModel('Poll');
$model->setState('filter.published', 1);
$model->setState('filter.access', true);
preg_match_all($regex, $article->text, $matches);
for ($i = 0, $n = count(@$matches[0]); $i < $n; $i++) {
if (@$matches[1][$i]) {
$inlineParams = $this->_parseParams($matches[1][$i]);
$id = $this->_getParam('id', $inlineParams, 0);
$width = $this->_getParam('width', $inlineParams, '');
$position = $this->_getParam('position', $inlineParams, 'center');
$view_result = $this->_getParam('view_result', $inlineParams, '0');
$item = $model->getItem($id);
$html = array();
if ($item) {
$data = array(
'item' => $item,
'options' => array(
'view_result' => $view_result
)
);
if ($width) {
if (is_numeric($width)) {
$widthStyle = 'width: ' . $width . 'px;';
} else {
$widthStyle = 'width: ' . $width;
}
} else {
$widthStyle = '';
}
switch ($position) {
case 'left':
$positionStyle = 'float: left;';
break;
case 'right':
$positionStyle = 'float: right;';
break;
case 'center':
default:
$positionStyle = 'margin: 0 auto;';
break;
}
$html[] = '<div class="advpoll-content-wrapper clearfix">';
$html[] = '<div style="' . $positionStyle . $widthStyle . '">';
$html[] = JLayoutHelper::render('poll', $data, JPATH_ROOT . '/components/com_advpoll/layouts');
$html[] = '</div>';
$html[] = '</div>';
}
$article->text = str_replace(@$matches[0][$i], implode("\n", $html), $article->text);
}
}
} else {
$article->text = preg_replace($regex, '', $article->text);
}
}
/**
* Parse params to array
*
* @param string $params
* @return array
*/
protected function _parseParams($params) {
$result = array();
$pattern = '#\s*([^\s^=]+)?=\'(.*?)\'#s';
if (preg_match_all($pattern, $params, $matches)) {
for ($i = 0, $n = count($matches[0]); $i < $n; $i++) {
$key = $matches[1][$i];
if ($key) {
$result[$key] = trim($matches[2][$i]);
}
}
}
return $result;
}
/**
* Get param value
* @param string $key
* @param array $params
* @return bool|string
*/
protected function _getParam($key, $params, $default = false) {
if (isset($params[$key])) {
return $params[$key];
} else {
return $default;
}
}
}