Skip to content

Commit

Permalink
Merge pull request #209 from pravinTek/release1.4.4
Browse files Browse the repository at this point in the history
Task #208 chore: Update tjlist saving and rendering logic, also added bootstrap tag input feature
  • Loading branch information
ankush-maherwal authored Nov 20, 2019
2 parents 4980a5f + 2fe070c commit b38134e
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 15 deletions.
1 change: 1 addition & 0 deletions administrator/assets/css/bootstrap-tagsinput.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions administrator/assets/js/bootstrap-tagsinput.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 26 additions & 2 deletions administrator/assets/js/tjlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@
* @license GNU General Public License version 2, or later
*/
var tjlist = {

addOtherOption: function (element) {

jQuery(tjlist.getTextBox(element)).insertAfter(element.next(".chzn-container"));

setTimeout(function() {
element.siblings('div.tjfieldTjListOtherText').children('.bootstrap-tagsinput').children().focus();

if (element.attr('multiple') == undefined)
{
element.siblings('div.tjfieldTjListOtherText').children().focus();
}

}, 100);
},
removeOtherOption: function (element) {

Expand All @@ -21,7 +30,22 @@ var tjlist = {
isRequired = (element.attr('required') != undefined) ? 'required="required"' : '',
requiredClass = (isRequired != '') ? 'required' : '';

return '<div class="tjfieldTjListOtherText"><br/><input ' + isRequired + ' type="text" name="' + inputName + '" id="' + inputId + '" value="" class=" ' + requiredClass + '" aria-invalid="false"></div>';
if (element.attr('multiple') != undefined)
{
this.loadTagsinputjs();
var tagInput = (element.attr('multiple') != undefined) ? 'data-role="tagsinput"' : '';
}

return '<div class="tjfieldTjListOtherText"><br/><input ' + tagInput + ' placeholder="' + Joomla.JText._('COM_TJFIELDS_OTHER_VALUE') + '" ' + isRequired + ' type="text" name="' + inputName + '" id="' + inputId + '" value="" class=" focus ' + requiredClass + '" aria-invalid="false"></div>';
},
loadTagsinputjs: function()
{
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');

// @Todo - Decide right place to store below JS
script.src= Joomla.getOptions('system.paths').base +'/administrator/components/com_tjfields/assets/js/bootstrap-tagsinput.min.js';
head.appendChild(script);
}
}

Expand Down
2 changes: 1 addition & 1 deletion administrator/assets/js/tjlist.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 29 additions & 5 deletions administrator/models/fields/tjlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Factory;

/**
* Form Field class for the Joomla Platform.
Expand Down Expand Up @@ -105,9 +106,15 @@ protected function getInput()

$html = parent::getInput();

$doc = JFactory::getDocument();
$doc = Factory::getDocument();
$doc->addScript(JUri::root() . 'administrator/components/com_tjfields/assets/js/tjlist.min.js');

if ($this->multiple)
{
$doc->addStyleSheet(JUri::root() . 'administrator/components/com_tjfields/assets/css/bootstrap-tagsinput.css');
$doc->addScript(JUri::root() . 'administrator/components/com_tjfields/assets/js/bootstrap-tagsinput.min.js');
}

$options = $this->getOptions();

if ($this->element['other'])
Expand All @@ -131,7 +138,8 @@ function ($e) {

if (empty($valueFromSelectList))
{
$this->otherSelectedValue = $this->value[0];
// Remove prefix values from other text values
$this->otherSelectedValue = str_replace($this->type . ':-', '', $this->value[0]);
}
}
elseif ($this->multiple)
Expand All @@ -150,7 +158,10 @@ function ($e) {

if (!empty($otherValues))
{
$this->otherSelectedValue = $otherValues[0];
$this->otherSelectedValue = implode(',', $otherValues);

// Remove prefix values from other text values
$this->otherSelectedValue = str_replace($this->type . ':-', '', $this->otherSelectedValue);
}
}
}
Expand Down Expand Up @@ -202,8 +213,21 @@ public function getOptions()
private function getInputBox()
{
$text = '<div class="tjfieldTjListOtherText"><br/>';
$text .= '<input type="text" class=" ' . $this->otherInputClass . '" ' . $this->otherInputRequired . '
name="' . $this->name . '" id="' . $this->id . '" value="' . $this->otherSelectedValue . '" aria-invalid="false"></div>';

// Check multiple select option true or not
if ($this->multiple)
{
// Enable the bootstrap taging input
$text .= '<input data-role="tagsinput" type="text" class=" focus ' . $this->otherInputClass . '" ' .
$this->otherInputRequired . ' name="' . $this->name . '" id="' . $this->id . '" value="' . $this->otherSelectedValue . '" aria-invalid="false">';
}
else
{
$text .= '<input type="text" class=" focus ' . $this->otherInputClass . '" ' . $this->otherInputRequired . '
name="' . $this->name . '" id="' . $this->id . '" value="' . $this->otherSelectedValue . '" aria-invalid="false">';
}

$text .= '</div>';

return $text;
}
Expand Down
78 changes: 71 additions & 7 deletions site/helpers/tjfields.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,68 @@ public function saveFieldsValue($data)
}
}
}
elseif ($field->type == 'tjlist')
{
// Check for Tjlist - start
$tjListParams = json_decode($field->params);

if ($tjListParams->other)
{
// Get all the fields of the specified client
JLoader::import('components.com_tjfields.models.options', JPATH_ADMINISTRATOR);
$tjFieldOptionsModel = JModelLegacy::getInstance('Options', 'TjfieldsModel', array('ignore_request' => true));
$tjFieldOptionsModel->setState('filter.field_id', $field->id);
$optionsValue = $tjFieldOptionsModel->getItems();

// Get array of dropdown values
$otherValues = array_column($optionsValue, 'value');
}

if (is_array($fieldValue))
{
$fieldVal = array();

if ($tjListParams->other)
{
$otherValues[] = $field->type . 'othervalue';

foreach ($fieldValue as $key => $listfieldVale)
{
if (strpos($listfieldVale, ','))
{
$fieldVal = explode(',', $listfieldVale);

// Add prefix for other values for tjlist field
$fieldVal = preg_filter('/^/', $field->type . ':-', $fieldVal);
unset($fieldValue[$key]);
}
elseif (!in_array($listfieldVale, $otherValues))
{
$fieldValue[$key] = $field->type . ':-' . $listfieldVale;
}
}

if (!empty($fieldVal))
{
$fieldValue = array_merge($fieldValue, $fieldVal);
}
}

$this->saveMultiValuedFieldData($fieldValue, $field->client, $data['content_id'], $field->id, $fieldStoredValues);
}
else
{
// Check other option enable for tjlist field

if ($tjListParams->other && !in_array($fieldValue, $otherValues))
{
// Add prefix for other values for tjlist field
$fieldValue = $field->type . ':-' . $fieldValue;
}

$this->saveSingleValuedFieldData($fieldValue, $field->client, $data['content_id'], $field->id, $fieldStoredValues);
}
}
elseif (is_array($fieldValue))
{
if (strpos($fieldValue[0], ','))
Expand Down Expand Up @@ -1485,15 +1547,17 @@ public function getOptions($field_id, $option_value = '')

if (!empty($otherValues))
{
$tjListOtherObj = new stdClass;

$tjListOtherObj->options = $otherValues[0];
$tjListOtherObj->default_option = '';
$tjListOtherObj->value = $otherValues[0];

$extra_options[] = $tjListOtherObj;
foreach ($otherValues as $othervalue)
{
$tjListOtherObj = new stdClass;
$tjListOtherObj->default_option = '';
$tjListOtherObj->options = $othervalue;
$tjListOtherObj->value = $othervalue;
$extra_options[] = $tjListOtherObj;
}
}
}

// Check for Tjlist - end
}
}
Expand Down

0 comments on commit b38134e

Please sign in to comment.