Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #184273 feat: Conditional fields implementation #363

Open
wants to merge 13 commits into
base: release-2.0.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions admin_language/en-GB/en-GB.com_tjfields.ini
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,25 @@ COM_TJFIELDS_FORM_LBL_ITEM_CATEGORY_FIELD_STATE="State"
COM_TJFIELDS_FORM_DESC_ITEM_CATEGORY_FIELD_STATE="(1/0/2/-2) is whether the drop down will show only published (1), unpublished (0), archived (2) or trashed (-2) categories. It is possible to combine different publishing status by entering the list of the corresponding numbers separated by comma (e.g. "0,2,-2" will display only unpublished, archived and trashed categories in the drop-down)"
COM_TJFIELDS_CAPTURE_IMAGE="Capture Image"
COM_TJFIELDS_LBL_ACCORDION="Accordion Tag"

COM_TJFIELDS_CONDITION_SELCET_FIELD="Select Field"
COM_TJFIELDS_CONDITION_SELCET_FIELD_OPTIONS="Select Option"
COM_TJFIELDS_TITLE_CONDITIONS="Conditions"
COM_TJFIELDS_ADD_CONDITION="Add"
COM_TJFIELDS_EDIT_CONDITION="Edit Condition"
COM_TJFIELDS_N_CONDITIONS_UNPUBLISHED="%d conditions successfully unpublished"
COM_TJFIELDS_N_CONDITIONS_PUBLISHED="%d conditions successfully published"
COM_TJFIELDS_CONDITIONAL_FIELDS="Conditional Fields"
COM_TJFIELDS_FORM_LBL_CONDITION_SHOW="Show/Hide"
COM_TJFIELDS_FORM_DESC_CONDITION_SHOW="Show/Hide"
COM_TJFIELDS_FORM_LBL_CONDITION_FIELD_TO_SHOW="Select the field to show/hide"
COM_TJFIELDS_FORM_DESC_CONDITION_FIELD_TO_SHOW="Select the field to show/hide"
COM_TJFIELDS_FORM_LBL_CONDITION_CONDITION_MATCH="Select All/Any of the defined condition matches"
COM_TJFIELDS_FORM_DESC_CONDITION_CONDITION_MATCH="Select All/Any of the defined condition matches"
COM_TJFIELDS_FORM_LBL_CONDITION_FIELD_ON_SHOW="Select the field on which you want to add condition"
COM_TJFIELDS_FORM_DESC_CONDITION_FIELD_ON_SHOW="Select the field on which you want to add condition"
COM_TJFIELDS_FORM_LBL_CONDITION_OPERATOR="Is/Is Not"
COM_TJFIELDS_FORM_DESC_CONDITION_OPERATOR="Is/Is Not"
COM_TJFIELDS_FORM_LBL_CONDITION_OPTION="Select the option"
COM_TJFIELDS_FORM_DESC_CONDITION_OPTION="Select the option"
COM_TJFIELDS_FORM_LBL_CONDITION="Condition"
129 changes: 129 additions & 0 deletions administrator/controllers/condition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/**
* @package Tjfields
* @author Techjoomla <extensions@techjoomla.com>
* @copyright Copyright (c) 2009-2023 TechJoomla. All rights reserved.
* @license GNU General Public License version 2 or later.
*/

// No direct access
defined('_JEXEC') or die();
use Joomla\CMS\Factory;
use Joomla\CMS\Response\JsonResponse;
use Joomla\CMS\MVC\Controller\FormController;
use Joomla\CMS\Table\Table;

/**
* Country form controller class.
*
* @package Tjfields
* @subpackage com_tjfields
* @since 2.2
*/
class TjfieldsControllerCondition extends FormController
{
/**
* The extension for which the countries apply.
*
* @var string
* @since 1.6
*/
protected $client;

/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
*
* @since 1.6
* @see JController
*/
public function __construct($config = array())
{
parent::__construct($config);
$this->view_list = 'conditions';

$this->input = Factory::getApplication()->input;

if (empty($this->client))
{
$this->client = $this->input->get('client', '');
}
}

/**
* Gets the URL arguments to append to an item redirect.
*
* @param integer $recordId The primary key id for the item.
* @param string $urlVar The name of the URL variable for the id.
*
* @return string The arguments to append to the redirect URL.
*
* @since 1.6
*/
protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id')
{
$append = parent::getRedirectToItemAppend($recordId);
$append .= '&client=' . $this->client;

return $append;
}

/**
* Gets the URL arguments to append to a list redirect.
*
* @return string The arguments to append to the redirect URL.
*
* @since 1.6
*/
protected function getRedirectToListAppend()
{
$append = parent::getRedirectToListAppend();
$append .= '&client=' . $this->client;

return $append;
}

/**
* Gets the URL arguments to append to a list redirect.
*
* @return string The arguments to append to the redirect URL.
*
* @since 1.6
*/
public function getFieldsOptions()
{
$app = Factory::getApplication();
$fieldId = $app->input->get('fieldId', 0, 'INT');

Table::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tjfields/tables');
$fieldTable = Table::getInstance('field', 'TjfieldsTable');
$fieldTable->load((int) $fieldId);
$fieldParams = json_decode($fieldTable->params);

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

// Select the required fields from the table.
$query->select('t.id AS value, t.options AS text');
$query->from('`#__tjfields_options` AS t');
$query->where('t.field_id = ' . (int) $fieldId);
$query->order($db->escape('t.ordering ASC'));
$db->setQuery($query);

// Get all countries.
$fieldOptions = $db->loadObjectList();

if ($fieldParams->other)
{
$object = new stdClass();
$object->value = 'tjlistothervalue';
$object->text = 'Other';

array_push($fieldOptions, $object);
}

echo new JsonResponse($fieldOptions);
$app->close();
}
}
155 changes: 155 additions & 0 deletions administrator/controllers/conditions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
/**
* @package Tjfields
* @author Techjoomla <extensions@techjoomla.com>
* @copyright Copyright (c) 2009-2023 TechJoomla. All rights reserved.
* @license GNU General Public License version 2 or later.
*/

// No direct access.
defined('_JEXEC') or die();
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Controller\AdminController;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Session\Session;
use Joomla\Utilities\ArrayHelper;

/**
* Countries list controller class.
*
* @package Tjfields
* @subpackage com_tjfields
* @since 2.2
*/
class TjfieldsControllerConditions extends AdminController
{
/**
* Proxy for getModel.
*
* @param string $name The name of the model.
* @param string $prefix The prefix for the PHP class name.
* @param array $config A named array of configuration variables.
*
* @return JModel
*
* @since 1.6
*/
public function getModel($name = 'Condition', $prefix = 'TjfieldsModel', $config = array('ignore_request' => true))
{
$model = parent::getModel($name, $prefix, $config);

return $model;
}

/**
* Method to publish records.
*
* @return void
*
* @since 3.0
*/
public function publish()
{
$client = Factory::getApplication()->input->get('client', '', 'STRING');
$cid = Factory::getApplication()->input->get('cid', array(), 'array');
$data = array(
'publish' => 1,
'unpublish' => 0
);

$task = $this->getTask();
$value = ArrayHelper::getValue($data, $task, 0, 'int');

// Get some variables from the request
if (empty($cid))
{
Log::add(Text::_('COM_TJFIELDS_NO_CONDITIONS_SELECTED'), Log::WARNING, 'jerror');
}
else
{
// Get the model.
$model = $this->getModel();

// Make sure the item ids are integers
ArrayHelper::toInteger($cid);

// Publish the items.
try
{
$model->publish($cid, $value);

if ($value === 1)
{
$ntext = 'COM_TJFIELDS_N_CONDITIONS_PUBLISHED';
}
elseif ($value === 0)
{
$ntext = 'COM_TJFIELDS_N_CONDITIONS_UNPUBLISHED';
}

// Generate xml here
$TjfieldsHelper = new TjfieldsHelper;
$client_form = explode('.', $client);
$client_type = $client_form[1];

$data = array();
$data['client'] = $client;
$data['client_type'] = $client_type;
$TjfieldsHelper->generateXml($data);

$this->setMessage(Text::plural($ntext, count($cid)));
}
catch (Exception $e)
{
$this->setMessage($e->getMessage(), 'error');
}
}

$this->setRedirect('index.php?option=com_tjfields&view=conditions&client=' . $client);
}

public function delete()
{
//GET CLIENT AND CLIENT TYPE
$app = Factory::getApplication();
$input = $app->input;
$client = $input->get('client','','STRING');
$client_form = explode('.',$client);
$client_type = $client_form[1];

// Get items to remove from the request.
$cid = $app->input->get('cid', array(), 'array');

if (!is_array($cid) || count($cid) < 1)
{
Log::add(Text::_($this->text_prefix . '_NO_ITEM_SELECTED'), Log::WARNING, 'jerror');
}
else
{
// Get the model.
$model = $this->getModel();

// Make sure the item ids are integers
ArrayHelper::toInteger($cid);

// Remove the items.
if ($model->delete($cid))
{
$TjfieldsHelper = new TjfieldsHelper();
$data = array();
$data['client'] = $client;
$data['client_type'] = $client_type;
$TjfieldsHelper->generateXml($data);
$ntext = $this->text_prefix . '_N_ITEMS_DELETED';
}
else
{
$this->setMessage($model->getError());
}
}

$this->setMessage(Text::plural($ntext, count($cid)));
$this->setRedirect('index.php?option=com_tjfields&view=conditions&client='.$client, false);
}
}
67 changes: 67 additions & 0 deletions administrator/helpers/tjfields.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Table\Table;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;

/**
* Helper class for tjfields
Expand Down Expand Up @@ -298,6 +299,10 @@ public function createXml($data, $fields, $category = null)

// To store added field groups to the JForm
$addedFieldGroups = array();

JLoader::import('components.com_tjfields.models.conditions', JPATH_ADMINISTRATOR);
$conditionsModel = BaseDatabaseModel::getInstance('Conditions', 'TjfieldsModel');
$conditionalFields = $conditionsModel->getConditionalFields();

foreach ($fields as $f)
{
Expand All @@ -319,6 +324,68 @@ public function createXml($data, $fields, $category = null)
$field->addAttribute('label', $f->label);
$field->addAttribute('description', $f->description);

if (in_array($f->id, $conditionalFields))
{
$conditions = $conditionsModel->getConditions($f->id);

$showonCondition = "";
$flag = 1;

foreach ($conditions as $condition)
{

$conditionMatch = $condition->condition_match;
$matchCase = ($conditionMatch == 1) ? "[AND]" : "[OR]" ;
$index = 1;

foreach (json_decode($condition->condition) as $condition1)
{
$jsonDecoded = json_decode($condition1);

Table::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tjfields/tables');
$fieldTable = Table::getInstance('field', 'TjfieldsTable');
$fieldTable->load((int) $jsonDecoded->field_on_show);
$fieldName = $fieldTable->name;

$optionTable = Table::getInstance('Option', 'TjfieldsTable');
$optionTable->load(array('field_id' => $jsonDecoded->field_on_show, 'id' => $jsonDecoded->option));
$optionValue = $optionTable->value;

if (json_decode($fieldTable->params)->other && empty($optionValue))
{
$optionValue = $jsonDecoded->option;
}

if ($condition->show == 1)
{
$operator = ($jsonDecoded->operator == 1) ? ":" : "!:";
}
else
{
$operator = ($jsonDecoded->operator == 1) ? "!:" : ":";
}

$showonCondition .= $fieldName . $operator . $optionValue;

if (count((array) json_decode($condition->condition)) > $index)
{
$showonCondition .= $matchCase;
}

$index ++;
}

if (count((array) $conditions) > $flag)
{
$showonCondition .= $matchCase;
}

$flag ++;
}

$field->addAttribute('showon', $showonCondition);
}

if ($f->required == 1)
{
$field->addAttribute('required', 'true');
Expand Down
Loading