Your IP : 216.73.216.240


Current Path : /home/j/u/v/juvelize/www/libraries/gjfields/
Upload File :
Current File : /home/j/u/v/juvelize/www/libraries/gjfields/categoryext.php

<?php
/**
 * @package     Joomla.Legacy
 * @subpackage  Form
 *
 * @copyright   Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE
 */

defined('JPATH_PLATFORM') or die;

JFormHelper::loadFieldClass('list');

use Joomla\CMS\Factory;

/**
 * Form Field class for the Joomla Platform.
 * Supports an HTML select list of categories
 *
 * @since  11.1
 */
class GJFieldsFormFieldCategoryext extends JFormFieldCategory
{
	/**
	 * The form field type.
	 *
	 * @var    string
	 * @since  11.1
	 */
	public $type = 'Categoryext';

	/**
	 * Method to get the field options for category
	 * Use the extension attribute in a form to specify the.specific extension for
	 * which categories should be displayed.
	 * Use the show_root attribute to specify whether to show the global category root in the list.
	 *
	 * @return  array    The field option objects.
	 *
	 * @since   11.1
	 */
	public function getOptions($flag = false)
	{
		// Due to inheritance the function can be called twice. To avoid we use the $flag parameter and static $options
		static $options = false;

		if (!$flag)
		{
			return $options;
		}

		$options = array();

		$context_or_contenttype = $this->element['context_or_contenttype']
			? (string) $this->element['context_or_contenttype'] : (string) $this->element['scope'];

		$extension = $this->element[$context_or_contenttype] ? (string) $this->element[$context_or_contenttype] : (string) $this->element['scope'];

		$extension = explode(PHP_EOL, $extension);
		$extension = trim($extension[0]);

		switch ($context_or_contenttype)
		{
			case 'context':
				break;
			case 'content_type':
			default :
				$category = JTable::getInstance('contenttype');
				$category->load($extension);

				$extension = $category->type_alias;
				break;
		}

		$extension = explode('.', $extension);
		$extension = $extension[0];

		$published = (string) $this->element['published'];

		// Load the category options for a given extension.
		if (!empty($extension))
		{
			switch ($extension)
			{
				case 'com_k2':
					try
					{
						$db = Factory::getDBO();
						$query = 'SELECT m.* FROM #__k2_categories m WHERE trash = 0 ORDER BY parent, ordering';
						$db->setQuery($query);
						$mitems = $db->loadObjectList();
						$children = array();

						if ($mitems)
						{
							foreach ($mitems as $v)
							{
								if (K2_JVERSION != '15')
								{
									$v->title = $v->name;
									$v->parent_id = $v->parent;
								}

								$pt = $v->parent;
								$list = @$children[$pt] ? $children[$pt] : array();
								array_push($list, $v);
								$children[$pt] = $list;
							}
						}

						$list = JHTML::_('menu.treerecurse', 0, '', array(), $children, 9999, 0, 0);
						$mitems = array();

						foreach ($list as $item)
						{
							$item->treename = JString::str_ireplace('&#160;', ' -', $item->treename);
							$mitems[] = JHTML::_('select.option', $item->id, $item->treename);
						}

						$options = $mitems;
					}
					catch (Exception $e)
					{
						Factory::getApplication()->enqueueMessage(
							JText::sprintf('LIB_GJFIELDS_NOT_INSTALLED', $extension) . '<br><pre>' . $e->getMessage() . '</pre>',
							'error'
						);
					}
					break;
				case 'com_phocadownload':
				case 'com_phocagallery':

					switch ($extension) {
						case 'com_phocadownload':
							$tblName = '#__phocadownload_categories';
							if (! class_exists('\PhocaDownloadCategory'))
							{

								require JPATH_ADMINISTRATOR . '/components/com_phocadownload/libraries/phocadownload/category/category.php';
							}
							break;
						case 'com_phocagallery':
							$tblName = '#__phocagallery_categories';
							if (! class_exists('\PhocaGalleryCategory'))
							{
								require JPATH_ADMINISTRATOR . '/components/com_phocagallery/libraries/phocagallery/html/category.php';

							}
							break;
					}

					$db = \Factory::getDBO();

					// Build the list of categories
					$query = 'SELECT a.title AS text, a.id AS value, a.parent_id as parentid'
					. ' FROM ' . $tblName . '  AS a'
					. ' WHERE a.published = 1'
					. ' ORDER BY a.ordering';
					$db->setQuery($query);
					$data = $db->loadObjectList();

					$catId	= -1;

					$tree = array();
					$text = '';

					switch ($extension) {
						case 'com_phocadownload':
							$tree = \PhocaDownloadCategory::CategoryTreeOption($data, $tree, 0, $text, $catId);
							break;
						case 'com_phocagallery':
							$tree = \PhocaGalleryCategory::CategoryTreeOption($data, $tree, 0, $text, $catId);
							break;
					}

					$options = $tree;

					break;

				case 'com_jdownloads':

					$file = JPATH_ADMINISTRATOR . '/components/' . $extension . '/models/fields/jdcategoryselect.php';

					if (!file_exists($file))
					{
						Factory::getApplication()->enqueueMessage(
							JText::sprintf('LIB_GJFIELDS_NOT_INSTALLED', $extension) . '<br>' . JText::_('LIB_GJFIELDS_FILE_NOT_EXISTS') . '<pre>' . $file . '</pre>',
							'error'
						);
						break;
					}

					JLoader::register('JFormFieldjdCategorySelect', $file);
					$formfield = JFormHelper::loadFieldType('jdcategoryselect');
					$formfield->setup($this->element, '');
					$options = $formfield->getOptions();

					if (empty($options) && Factory::getApplication()->isSite()) {
						$options = $this->getJDownloadsCategories();
					}
					break;
				default :
					if (strpos($extension, 'com_') !== 0)
					{
						$extension = 'com_' . $extension;
					}

					// Filter over published state or not depending upon if it is present.
					if ($published)
					{
						$options = JHtml::_('category.options', $extension, array('filter.published' => explode(',', $published)));
					}
					else
					{
						$options = JHtml::_('category.options', $extension);
					}

					// Verify permissions.  If the action attribute is set, then we scan the options.
					if ((string) $this->element['action'])
					{
						// Get the current user object.
						$user = Factory::getUser();

						foreach ($options as $i => $option)
						{
							/*
							 * To take save or create in a category you need to have create rights for that category
							 * unless the item is already in that category.
							 * Unset the option if the user isn't authorised for it. In this field assets are always categories.
							 */
							if ($user->authorise('core.create', $extension . '.category.' . $option->value) != true)
							{
								unset($options[$i]);
							}
						}
					}
					break;
			}

			if (isset($this->element['show_root']))
			{
				array_unshift($options, JHtml::_('select.option', '0', JText::_('JGLOBAL_ROOT')));
			}
		}
		else
		{
			JLog::add(
				'GJFields: ' . $this->getAttribute('name') . ' ' . JText::_('JLIB_FORM_ERROR_FIELDS_CATEGORY_ERROR_EXTENSION_EMPTY'),
				JLog::WARNING,
				'jerror'
			);
		}

		// Merge any additional options in the XML definition.

		/*##mygruz20160213194844 {
		$options = array_merge(parent::getOptions(), $options);
		It was:
		It became:*/
		/*##mygruz20160213194844 } */

		return $options;
	}

	/**
	 * HTML output
	 *
	 * @return   string
	 */
	protected function getInput()
	{
		$options = (array) $this->getOptions(true);

		if (empty($options))
		{
			$formfield = JFormHelper::loadFieldType('text');
			$formfield->setup($this->element, '');

			if (is_array($this->value))
			{
				$formfield->value = implode(',', $this->value);
			}
			else
			{
				$formfield->value = $this->value;
			}

			$formfield->hint = JText::_((string) $this->hint);

			return $formfield->getInput() . PHP_EOL;
		}

		return parent::getInput();
	}

	/**
	 * This is a copy of file administrator/components/com_jdownloads/models/fields/jdcategoryselect.php:getOptions()
	 *
	 * Original code doesn't allow to load categroies on Frontend. So must copy it here.
	 *
	 */
	private function getJDownloadsCategories() {
		// Include helpers
		require_once JPATH_SITE.'/administrator/components/com_jdownloads/helpers/jdownloadshelper.php';

		$jd_config = JDownloadsHelper::buildjlistConfig();

		// Initialise variables.
		$options = array();

		$db		= JFactory::getDbo();
		$query	= $db->getQuery(true);

		$query->select('a.id AS value, a.title AS text, a.level');
		$query->from('#__jdownloads_categories AS a');
		$query->join('LEFT', '`#__jdownloads_categories` AS b ON a.lft > b.lft AND a.rgt < b.rgt');

		// Prevent parenting to children of this item.
		/* if ($id = $this->form->getValue('id')) {
			$query->join('LEFT', '`#__jdownloads_categories` AS p ON p.id = '.(int) $id);
			$query->where('NOT(a.lft >= p.lft AND a.rgt <= p.rgt)');

			$rowQuery	= $db->getQuery(true);
			$rowQuery->select('a.id AS value, a.title AS text, a.level, a.parent_id');
			$rowQuery->from('#__jdownloads_categories AS a');
			$rowQuery->where('a.id = ' . (int) $id);
			$db->setQuery($rowQuery);
			$row = $db->loadObject();
		}  */

		$query->where('a.published IN (0,1)');
		$query->group('a.id');
		$query->order('a.lft ASC');

		// Get the options.
		$db->setQuery($query);

		$options = $db->loadObjectList();

		// Check for a database error.
		if ($db->getErrorNum()) {
			JError::raiseWarning(500, $db->getErrorMsg());
		}

		// Pad the option text with spaces using depth level as a multiplier.
		for ($i = 0, $n = count($options); $i < $n; $i++)
		{
			// Translate ROOT
			if ($options[$i]->level == 0) {
				if ($jd_config['show.header.catlist.uncategorised'] == 1){
					$options[$i]->text = JText::_('COM_JDOWNLOADS_SELECT_UNCATEGORISED');
				} else {
					$root = array_shift($options);
					$i++;
					$n--;
				}
			}
			if ($options[$i]->level > 1){
				$options[$i]->text = str_repeat('- ',($options[$i]->level -1)).$options[$i]->text;
			}
		}

		// Initialise variables.
		/** ##mygruz20200718174443 {
		It was:
		$user = JFactory::getUser();

		if (empty($id)) {
			// New item, only have to check core.create.
			foreach ($options as $i => $option)
			{
				// Unset the option if the user isn't authorised for it.
				if (!$user->authorise('core.create', 'com_jdownloads.category.'.$option->value)) {
					unset($options[$i]);
				}
			}
		}
		else {
			// Existing item is a bit more complex. Need to account for core.edit and core.edit.own.
			foreach ($options as $i => $option)
			{
				// Unset the option if the user isn't authorised for it.
				if (!$user->authorise('core.edit', 'com_jdownloads.category.'.$option->value)) {
					// As a backup, check core.edit.own
					if (!$user->authorise('core.edit.own', 'com_jdownloads.category.'.$option->value)) {
						// No core.edit nor core.edit.own - bounce this one
						unset($options[$i]);
					}
					else {
						// TODO I've got a funny feeling we need to check core.create here.
						// Maybe you can only get the list of categories you are allowed to create in?
						// Need to think about that. If so, this is the place to do the check.
					}
				}
			}
		}
		It became: */
		/** ##mygruz20200718174443 } */

		// Merge any additional options in the XML definition.
		$options = array_merge(parent::getOptions(), $options);

		return $options;
	}
}

// Preserve compatibility
if (!class_exists('JFormFieldCategoryext'))
{
	/**
	 * Old-fashioned field name
	 *
	 * @since  1.2.0
	 */
				class JFormFieldCategoryext extends GJFieldsFormFieldCategoryext
				{
				}
}