<?php
/********************************************************************
Product : Simple Sliders Plugin
Date : 30 November 2023
Copyright : Les Arbres Design 2020-2023
Contact : https://www.lesarbresdesign.info
Licence : GNU General Public License
*********************************************************************/
defined('_JEXEC') or die('Restricted Access');
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Factory;
use Joomla\CMS\Utility\Utility;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Uri\Uri;
class plgContentsimplesliders extends CMSPlugin
{
//-------------------------------------------------------------------------------
// Joomla calls this for every article so we must determine as quickly as possible
// if there are any calls for us in this article
//
function onContentPrepare($context, &$article, &$params, $limitstart=null)
{
$app = Factory::getApplication();
if (!$app->isClient('site'))
return;
if (empty($article->text))
return;
if (strpos($article->text, '{slider ') === false)
return;
$document = $app->getDocument();
if ($document instanceof \Joomla\CMS\Document\HtmlDocument)
$doc_type = 'html';
else
$doc_type = '';
// load our CSS and JS
$app = Factory::getApplication();
$css = '/media/plg_simplesliders/simplesliders.css';
$override_css = '/media/plg_simplesliders/_simplesliders.css';
if (file_exists(JPATH_SITE.$override_css))
$css = $override_css;
if (file_exists(JPATH_SITE.$css))
{
$ftime = @filemtime(JPATH_SITE.$css);
if (!$ftime)
$ftime = '0';
if ($doc_type == 'html')
$document->addStyleSheet(URI::root(true).$css.'?'.$ftime);
}
if ($doc_type == 'html')
{
HTMLHelper::_('jquery.framework'); // make sure jQuery is loaded
$document->addScript(URI::root(true).'/media/plg_simplesliders/simplesliders.js?104');
}
// is there a "slider" URL variable?
$jinput = Factory::getApplication()->input;
$url_slider = $jinput->get('slider', '', 'string');
// create an array ($matches) of {slider...} tags in the article
$regex = "#{slider\s(.*?)}|{/sliders}#s";
if (preg_match_all($regex, $article->text, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE) == 0)
return;
$new_text = '';
// $new_text = '<pre>'.print_r($matches,true).'</pre>'; // useful for debugging
// get the text before the first {slider call
$first_offset = $matches[0][0][1];
$new_text .= substr($article->text,0,$first_offset);
// for each call, generate the slider structure
static $slider_num; // handle multiple articles on the same page
if (!isset($slider_num))
$slider_num = 1;
foreach ($matches as $i => $match)
{
$call = $match[0][0]; // e.g. {slider title="Blah blah blah" open="false"}
if (empty($match[1][0]))
$match_params = '';
else
$match_params = $match[1][0]; // e.g. title="Blah blah blah" open="false"
$parsed_params = self::parse_params($match_params);
$call_offset = $match[0][1];
$call_offset_end = $call_offset + strlen($call);
if (isset($matches[$i+1]))
{
$next_match_offset = $matches[$i+1][0][1];
$content_length = $next_match_offset - $call_offset_end;
$content = substr($article->text,$call_offset_end,$content_length);
}
else
$content = substr($article->text,$call_offset_end); // take the rest of the buffer
if (strstr($call, '{/sliders}'))
{
$new_text .= $content;
continue;
}
$slider_id = 'slider'.$slider_num;
if (empty($parsed_params['title']))
$parsed_params['title'] = $slider_num; // if no title, default it (if slider title is a number, check the plugin call parameters!)
else
$slider_id = strtolower(str_replace(' ','-',$parsed_params['title'])); // use title as slider id
if (!empty($parsed_params['alias'])) // if we have an alias, use that as the slider id
$slider_id = $parsed_params['alias'];
$scroll = '';
if ($url_slider == $slider_id)
{
$parsed_params['open'] = 'true'; // open the requested slider
$scroll = ' data-siv="true"';
}
elseif ($url_slider != '')
$parsed_params['open'] = ''; // .. and close all others
$new_text .= '<div id="'.$slider_id.'" class="ssl-outer '.$parsed_params['class'].'">';
if ($parsed_params['open'] == 'true')
{
$new_text .= '<div class="ssl-title ssl-title-open" data-num="'.$slider_num.'"'.$scroll.'>'.$parsed_params['title'].'</div>';
$new_text .= '<div id="ssl-'.$slider_num.'" class="ssl-content ssl-content-open">'.$content.'</div></div>';
}
else
{
$new_text .= '<div class="ssl-title" data-num="'.$slider_num.'">'.$parsed_params['title'].'</div>';
$new_text .= '<div id="ssl-'.$slider_num.'" class="ssl-content">'.$content.'</div></div>';
}
$slider_num ++;
}
$article->text = $new_text;
}
//-------------------------------------------------------------------------------
// Parse and validate the plugin parameters, which may be in two formats:
// {slider This Is The Title}
// {slider title="This Is The Title" alias="slider-1" open="true" class="blue"}
//
function parse_params($match_params)
{
if (!strstr($match_params,'='))
$params['title'] = $match_params;
else
$params = Utility::parseAttributes($match_params);
if (!isset($params['title'])) $params['title'] = '';
if (!isset($params['alias'])) $params['alias'] = '';
if (!isset($params['open'])) $params['open'] = '';
if (!isset($params['class'])) $params['class'] = '';
return $params;
}
}