Your IP : 216.73.216.240


Current Path : /home/j/u/v/juvelize/www/plugins/icagenda/pro/
Upload File :
Current File : /home/j/u/v/juvelize/www/plugins/icagenda/pro/pro.php

<?php
/**
 *----------------------------------------------------------------------------
 * iCagenda     Plugin Pro
 *----------------------------------------------------------------------------
 * @version     1.0.3 2022-12-29
 *
 * @package     iCagenda.Plugin
 * @subpackage  icagenda.pro
 * @link        https://www.icagenda.com
 *
 * @author      Cyril Rezé
 * @copyright   (c) 2012-2024 Cyril Rezé / iCagenda. All rights reserved.
 * @license     GNU General Public License version 3 or later; see LICENSE.txt
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @since       iCagenda 3.8
 *----------------------------------------------------------------------------
*/

defined('_JEXEC') or die;

use Joomla\CMS\Component\ComponentHelper as JComponentHelper;
use Joomla\CMS\Factory as JFactory;
use Joomla\CMS\Form\Form as JForm;
use Joomla\CMS\Language\Text as JText;
use Joomla\CMS\Layout\FileLayout as JLayoutFile;
use Joomla\CMS\Toolbar\ToolbarHelper as JToolBarHelper;
use Joomla\CMS\Uri\Uri as JUri;

/**
 * plgIcagendaPro
 */
class plgIcagendaPro extends JPlugin
{
	/**
	 * 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 = JComponentHelper::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(JForm $form, $data)
	{
		$app    = JFactory::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'
				) {
					JForm::addFormPath(__DIR__ . '/forms');
					$form->loadFile('config_pro', true);

					if (version_compare(JVERSION, '4.0', 'lt')) {
						$form->setFieldAttribute('separator_history', 'type', 'hidden');
						$form->setFieldAttribute('save_history', 'type', 'hidden');
						$form->setFieldAttribute('history_limit', 'type', 'hidden');
					}
				}

				break;

			case 'com_icagenda':
				if ($form->getName() == 'com_icagenda.icategory'
					|| $form->getName() == 'com_icagenda.event'
				) {
					if (version_compare(JVERSION, '4.0', 'lt')
						|| ! $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 JRegistry($item->params);

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

		// Set edit button HTML
		$editTooltipLegend = JText::_('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     = JComponentHelper::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     = JComponentHelper::getParams('com_icagenda');
			$save_history = $iCparams->get('save_history', true);

			if ($this->isPro && $save_history && ! empty($item->id)) {
				JToolBarHelper::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  = JFactory::getUser();
		$input = JFactory::getApplication()->input;

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

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

		switch ($element) {
			case 'event':
				$editUrl = '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;
				break;
		}

		$displayData = array(
			'url'     => $editUrl . $redirectUri,
			'view'    => $view,
			'options' => $options,
		);

		$layout = new JLayoutFile('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;
	}
}