Your IP : 216.73.216.240


Current Path : /home/juvelize/www/plugins/icagenda/pro/src/Extension/
Upload File :
Current File : /home/juvelize/www/plugins/icagenda/pro/src/Extension/Pro.php

<?php
/**
 *----------------------------------------------------------------------------
 * iCagenda     Plugin iCagenda - Pro
 *----------------------------------------------------------------------------
 * @version     2.0.1 2024-07-01
 *
 * @package     iCagenda
 * @subpackage  Plugin.Icagenda.Pro
 * @link        https://www.icagenda.com
 *
 * @author      Cyril Rezé
 * @copyright   (c) 2013-2024 Cyril Rezé / iCagenda. All rights reserved.
 * @license     GNU General Public License version 3 or later; see LICENSE.txt
 *
 * @since       iCagenda 3.8
 *----------------------------------------------------------------------------
*/

namespace W3biC\Plugin\Icagenda\Pro\Extension;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Layout\FileLayout;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Registry\Registry;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
 * iCagenda Pro Plugin.
 */
final class Pro extends CMSPlugin
{
	use DatabaseAwareTrait;

	/**
	 * Load the language file on instantiation.
	 *
	 * @var boolean
	 */
	protected $autoloadLanguage = true;

	/**
	 * Package type version.
	 *
	 * @var    string
	 */
	protected $type = 'pro';

	/**
	 * Check iCagenda version.
	 *
	 * @var    string
	 */
	protected $isPro;

	/**
	 * Constructor.
	 *
	 * @param   object  &$subject  The object to observe.
	 * @param   array   $config    An optional associative array of configuration settings.
	 */
	public function __construct(&$subject, $config)
	{
		parent::__construct($subject, $config);

		$iCparams = ComponentHelper::getParams('com_icagenda');

		$this->isPro = ($iCparams->get('icsys') == $this->type);
	}

	/**
	 * Adds additional fields to iCagenda Options
	 *
	 * @param   Form   $form  The form to be altered.
	 * @param   mixed  $data  The associated data for the form.
	 *
	 * @return  boolean
	 */
	public function onContentPrepareForm(Form $form, $data)
	{
		$app    = Factory::getApplication();
		$input  = $app->input;
		$option = $input->get('option');

		switch ($option) {
			case 'com_config':
				$component = $input->get('component');

				if ($this->isPro
					&& $app->isClient('administrator')
					&& $component == 'com_icagenda'
				) {
					FormHelper::addFormPath(JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name . '/forms');
					$form->loadFile('config_pro', true);
				}

				break;

			case 'com_icagenda':
				if (($form->getName() == 'com_icagenda.icategory'
					|| $form->getName() == 'com_icagenda.event')
					&& ! $this->isPro
				) {
					$form->setFieldAttribute('version_note', 'type', 'hidden');
					$form->setFieldAttribute('version', 'type', 'hidden');
				}

				break;
		}

		return true;
	}

	/**
	 * On Rendering Site Manager toolbar method
	 * This method adds element to the manager toolbar
	 * Method is called right after main toolbar buttons are rendered
	 *
	 * @param   object   $item     A JTable object
	 * @param   array    $options  Options for the toolbar elements
	 *
	 * @return  mixed
	 */
	public function onICagendaManagerTools($item, $options = array())
	{
		$managerToolsExtended = '';

		$params = new Registry($item->params);

		$layoutPath = JPATH_SITE . '/components/com_icagenda/themes/packs/' . $params->get('template', 'default') . '/layouts';

		// Set edit button HTML
		$editTooltipLegend = Text::_('COM_ICAGENDA_EVENT_EDIT_BUTTON_DESC_LEGEND');
		$editTooltipText   = htmlspecialchars($item->title);

		if ( ! isset($options['edit']['layoutPath'])) $options['edit']['layoutPath'] = $layoutPath;
		if ( ! isset($options['edit']['tipLegend']))  $options['edit']['tipLegend']  = $editTooltipLegend;
		if ( ! isset($options['edit']['tipText']))    $options['edit']['tipText']    = $editTooltipText;
		

		$editButton = $this->editButton('event', $item, $options['edit']);

		if ($editButton) {
			$managerToolsExtended.= $editButton;
		}

		return $managerToolsExtended;
	}

	/**
	 * On Saving Categories and Events method (J4)
	 * Method is called when a category or event is being saved
	 *
	 * @param   string   $context  The context of the content passed to the plugin
	 * @param   JTable   $table    DataBase Table object
	 *
	 * @return  void
	 */
	public function onICagendaPrepareTable($context, &$table)
	{
		if ($context == 'com_icagenda.icategory'
			|| $context == 'com_icagenda.event'
		) {
			$iCparams     = ComponentHelper::getParams('com_icagenda');
			$save_history = $iCparams->get('save_history', true);

			if ($this->isPro && $save_history) {
				// Increment the version number.
				$table->version++;
			}
		}
	}

	/**
	 * On Rendering toolbar method (J4 Category and Event)
	 * This method adds element to the toolbar
	 * Method is called right after main toolbar buttons are rendered
	 *
	 * @param   string   $context  The context of the content passed to the plugin
	 * @param   object   $item     A JTable object
	 *
	 * @return  void
	 */
	public function onICagendaToolBar($context, &$item)
	{
		// Versions Button in edit form
		if ($context == 'com_icagenda.icategory'
			|| $context == 'com_icagenda.event'
		) {
			$iCparams     = ComponentHelper::getParams('com_icagenda');
			$save_history = $iCparams->get('save_history', true);

			if ($this->isPro && $save_history && ! empty($item->id)) {
				ToolbarHelper::versions($context, $item->id);
			}
		}
	}

	/**
	 * Function to render Edit button
	 *
	 * @param   string   $element  The element to be edited
	 * @param   object   $item     A JTable object
	 * @param   array    $options  Options for the button
	 *
	 * @return  HTML
	 */
	protected function editButton($element, $item, $options = array())
	{
		$user  = Factory::getUser();
		$input = Factory::getApplication()->input;

		$userId = $user->get('id');
		$view   = $input->get('view');
		$Itemid = $input->getInt('Itemid');

		$redirectUri = '&return=' . urlencode(base64_encode(Uri::getInstance()->toString()));

		switch ($element) {
			case 'event':
//				$editUrl = 'index.php?option=com_icagenda&task=event.edit&Itemid=' . $Itemid . '&event_id=' . (int) $item->id;
				$editUrl = Uri::base() . 'index.php?option=com_icagenda&task=event.edit&Itemid=' . $Itemid . '&event_id=' . (int) $item->id;
				break;
			default:
//				$editUrl = 'index.php?option=com_icagenda&task=event.edit&Itemid=' . $Itemid . '&event_id=' . (int) $item->id;
				$editUrl = Uri::base() . 'index.php?option=com_icagenda&task=event.edit&Itemid=' . $Itemid . '&event_id=' . (int) $item->id;
				break;
		}

		$displayData = [
			'url'     => $editUrl . $redirectUri,
			'view'    => $view,
			'options' => $options,
		];

		$layout = new FileLayout('icagenda.manager.button.edit');

		if (isset($options['layoutPath'])) {
			$layout->addIncludePaths($options['layoutPath']);
		}

		// Check general edit permission first.
		if ($this->isPro
			&& $user->authorise('core.edit', 'com_icagenda')
		) {
			return $layout->render($displayData);
		}

		// Now check if edit.own is available.
		elseif ($this->isPro
			&& $user->authorise('core.edit.own', 'com_icagenda')
			&& ! empty($userId)
		) {
			// Check for a valid user and that they are the owner.
			if ($userId == $item->created_by) {
				return $layout->render($displayData);
			}
		}

		return;
	}
}