| Current Path : /var/www/consult-e-syn/public_html/plugins/ats/customfields/ |
| Current File : /var/www/consult-e-syn/public_html/plugins/ats/customfields/customfields.php |
<?php
/**
* @package ats
* @copyright Copyright (c)2011-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
defined('_JEXEC') or die();
use Akeeba\TicketSystem\Admin\Model\CustomFields;
use FOF40\Container\Container;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Language\Text as JText;
class plgAtsCustomfields extends CMSPlugin
{
private $fieldTypes = [];
public function __construct(&$subject, $config = [])
{
parent::__construct($subject, $config);
if (!defined('FOF40_INCLUDED') && !@include_once(JPATH_LIBRARIES . '/fof40/include.php'))
{
throw new RuntimeException('This extension requires FOF 4.', 500);
}
// Invoke the container to register our autoload, if required
Container::getInstance('com_ats');
// Let's get all the fields
$customFields = \Akeeba\TicketSystem\Admin\Helper\Select::getFieldTypes();
$this->fieldTypes = array_keys($customFields);
}
/**
* Renders per-category custom fields
*
* @param array $cache
* @param array $userparams
*
* @return array
*/
public function onTicketFormRenderPerCatFields($cache, $userparams = null)
{
$container = Container::getInstance('com_ats');
$lang = $container->platform->getLanguage();
$lang->load('plg_ats_customfields', JPATH_ADMINISTRATOR, 'en-GB', true);
$lang->load('plg_ats_customfields', JPATH_ADMINISTRATOR, null, true);
// Init the fields array which will be returned
$fields = [];
if (!isset($cache['params']))
{
$cache['params'] = [];
}
// No catid? Stop here
if (!isset($cache['catid']) || !$cache['catid'])
{
return $fields;
}
$items = $this->getCustomFieldsFor($cache['catid']);
if ($items->isEmpty())
{
return $fields;
}
// Loop through the items
/** @var CustomFields $item */
foreach ($items as $item)
{
// Get the names of the methods to use
$type = $item->type;
$class = 'Akeeba\\TicketSystem\\Admin\\CustomField\\' . ucfirst($type);
if (!class_exists($class))
{
continue;
}
/** @var \Akeeba\TicketSystem\Admin\CustomField\Base $object */
$object = new $class;
// Add the field to the list
$result = $object->getField($item, $cache);
$result['public'] = (bool) $item->public;
if (is_null($result) || empty($result))
{
continue;
}
$fields[] = $result;
}
return $fields;
}
/**
* Validate the custom fields' values
*
* @param array $data The values for all custom fields
* @param int $category The numeric category ID the ticket belongs to
*
* @return array
*
* @since version
*/
public function onAtsValidate(&$data, $category)
{
$response = [
'valid' => true,
'isValid' => true,
'custom_validation' => [],
];
$items = $this->getCustomFieldsFor($category);
// If there are no custom fields return true (all valid)
if ($items->isEmpty())
{
return $response;
}
// Loop through each custom field
/** @var CustomFields $item */
foreach ($items as $item)
{
// Make sure there is a validation method for this type of field
$type = $item->type;
$class = 'Akeeba\\TicketSystem\\Admin\\CustomField\\' . ucfirst($type);
if (!class_exists($class))
{
continue;
}
/** @var \Akeeba\TicketSystem\Admin\CustomField\Base $object */
$object = new $class;
// Get the validation result and save it in the $response array
$response['custom_validation'][$item->slug] = $object->validate($item, $data);
if (is_null($response['custom_validation'][$item->slug]))
{
unset($response['custom_validation'][$item->slug]);
}
elseif (!$item->allow_empty)
{
$response['isValid'] = $response['isValid'] && $response['custom_validation'][$item->slug];
}
}
/**
* Update the master "valid" reponse. If one of the fields is invalid, the entire plugin's result is invalid
* (the form should not be submitted)
*/
$response['valid'] = $response['isValid'];
return $response;
}
/**
* Render a single custom field
*
* @param CustomFields $field The field we are rendering
* @param mixed $value The field's value
*
* @return array|null ['label' => $label, 'value' => $value] or null when we can't render this field
*
* @since 3.0.0
*/
public function onAtsDisplayCustomField(CustomFields $field, $value)
{
$type = $field->type;
$class = 'Akeeba\\TicketSystem\\Admin\\CustomField\\' . ucfirst($type);
if (!class_exists($class))
{
return null;
}
/** @var \Akeeba\TicketSystem\Admin\CustomField\Base $o */
$o = new $class;
$value = $o->getDisplay($field, $value);
return [
'label' => JText::_($field->title),
'value' => $value,
];
}
/**
* Get the custom field definitions for a category.
*
* Returns the custom field definitions which apply to this specific category plus those that apply to *all*
* categories sorted by their ordering field.
*
* @param int $catId The category to get custom field definitions for
*
* @return \FOF40\Utils\Collection
*
* @since 3.2.0
*/
protected function getCustomFieldsFor($catId)
{
$container = Container::getInstance('com_ats');
/** @var CustomFields $model */
$model = $container->factory->model('CustomFields')->tmpInstance();
// First, get custom fields applicable to all categories.
$forAll = $model
->enabled(1)
->show('all')
->filter_order('ordering')
->filter_order_Dir('ASC')
->limit(0)
->limitstart(0)
->get();
// Now, get custom fields applicable to this category only
$forThisCat = $model
->enabled(1)
->show('category')
->whereHas('cats', function (JDatabaseQuery $q) use ($catId) {
$q->where($q->qn('pivotTable.catid') . ' = ' . $q->q($catId));
})
->filter_order('ordering')
->filter_order_Dir('ASC')
->limit(0)
->limitstart(0)
->get();
return $forAll->merge($forThisCat)
->sort(function (CustomFields $a, CustomFields $b) {
if ($a->ordering == $b->ordering)
{
// This should never, ever happen!
if ($a->getId() == $b->getId())
{
return 0;
}
return ($a->getId() < $b->getId()) ? -1 : 1;
}
return ($a->ordering < $b->ordering) ? -1 : 1;
});
}
}