* @git Joomla Component Builder * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace VDM\Joomla\Componentbuilder\Compiler\Creator; use VDM\Joomla\Componentbuilder\Compiler\Config; use VDM\Joomla\Componentbuilder\Compiler\Language; use VDM\Joomla\Componentbuilder\Compiler\Field; use VDM\Joomla\Componentbuilder\Compiler\Field\Groups; use VDM\Joomla\Componentbuilder\Compiler\Field\Name; use VDM\Joomla\Componentbuilder\Compiler\Field\TypeName; use VDM\Joomla\Componentbuilder\Compiler\Field\Attributes; use VDM\Joomla\Componentbuilder\Compiler\Creator\CustomFieldTypeFile; use VDM\Joomla\Componentbuilder\Compiler\Utilities\Counter; use VastDevelopmentMethod\Joomla\Utilities\ArrayHelper; use VastDevelopmentMethod\Joomla\Utilities\ObjectHelper; use VastDevelopmentMethod\Joomla\Utilities\StringHelper; use VastDevelopmentMethod\Joomla\Utilities\String\FieldHelper; use VDM\Joomla\Componentbuilder\Compiler\Utilities\Indent; use VDM\Joomla\Componentbuilder\Compiler\Utilities\Line; use VDM\Joomla\Componentbuilder\Compiler\Interfaces\Creator\Fieldtypeinterface; /** * Field String Creator Class * * @since 3.2.0 */ final class FieldString implements Fieldtypeinterface { /** * The Config Class. * * @var Config * @since 3.2.0 */ protected Config $config; /** * The Language Class. * * @var Language * @since 3.2.0 */ protected Language $language; /** * The Field Class. * * @var Field * @since 3.2.0 */ protected Field $field; /** * The Groups Class. * * @var Groups * @since 3.2.0 */ protected Groups $groups; /** * The Name Class. * * @var Name * @since 3.2.0 */ protected Name $name; /** * The TypeName Class. * * @var TypeName * @since 3.2.0 */ protected TypeName $typename; /** * The Attributes Class. * * @var Attributes * @since 3.2.0 */ protected Attributes $attributes; /** * The CustomFieldTypeFile Class. * * @var CustomFieldTypeFile * @since 3.2.0 */ protected CustomFieldTypeFile $customfieldtypefile; /** * The Counter Class. * * @var Counter * @since 3.2.0 */ protected Counter $counter; /** * Constructor. * * @param Config $config The Config Class. * @param Language $language The Language Class. * @param Field $field The Field Class. * @param Groups $groups The Groups Class. * @param Name $name The Name Class. * @param TypeName $typename The TypeName Class. * @param Attributes $attributes The Attributes Class. * @param CustomFieldTypeFile $customfieldtypefile The CustomFieldTypeFile Class. * @param Counter $counter The Counter Class. * * @since 3.2.0 */ public function __construct(Config $config, Language $language, Field $field, Groups $groups, Name $name, TypeName $typename, Attributes $attributes, CustomFieldTypeFile $customfieldtypefile, Counter $counter) { $this->config = $config; $this->language = $language; $this->field = $field; $this->groups = $groups; $this->name = $name; $this->typename = $typename; $this->attributes = $attributes; $this->customfieldtypefile = $customfieldtypefile; $this->counter = $counter; } /** * Create a field using string manipulation * * @param string $setType The set of fields type * @param array $fieldAttributes The field values * @param string $name The field name * @param string $typeName The field type * @param string $langView The language string of the view * @param string $nameSingleCode The single view name * @param string $nameListCode The list view name * @param array $placeholders The place holder and replace values * @param array|null $optionArray The option bucket array used to set the field options if needed. * @param array|null $custom Used when field is from config * @param string $taber The tabs to add in layout * * @return string The field in a string * @since 3.2.0 */ public function get(string $setType, array &$fieldAttributes, string &$name, string &$typeName, string &$langView, string &$nameSingleCode, string &$nameListCode, array $placeholders, ?array &$optionArray, ?array $custom = null, string $taber = ''): string { // count the dynamic fields created $this->counter->field++; // build field set using string manipulation $field = ''; if ($setType === 'option') { // now add to the field set $field .= PHP_EOL . Indent::_(1) . $taber . Indent::_(1) . ""; $field .= PHP_EOL . Indent::_(1) . $taber . Indent::_(1) . " $value) { if ($property != 'option') { $field .= PHP_EOL . Indent::_(2) . $taber . Indent::_(1) . $property . '="' . $value . '"'; } elseif ($property === 'option') { $optionSet = ''; if (strtolower($typeName) === 'groupedlist' && strpos( (string) $value, ',' ) !== false && strpos((string) $value, '@@') !== false) { // reset the group temp arrays $groups_ = array(); $grouped_ = array('group' => array(), 'option' => array()); $order_ = array(); // mulitpal options $options = explode(',', (string) $value); foreach ($options as $option) { if (strpos($option, '@@') !== false) { // set the group label $valueKeyArray = explode('@@', $option); if (count((array) $valueKeyArray) == 2) { $langValue = $langView . '_' . FieldHelper::safe( $valueKeyArray[0], true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $valueKeyArray[0] ); // now add group label $groups_[$valueKeyArray[1]] = PHP_EOL . Indent::_(1) . $taber . Indent::_(2) . ''; // set order $order_['group' . $valueKeyArray[1]] = $valueKeyArray[1]; } } elseif (strpos($option, '|') !== false) { // has other value then text $valueKeyArray = explode('|', $option); if (count((array) $valueKeyArray) == 3) { $langValue = $langView . '_' . FieldHelper::safe( $valueKeyArray[1], true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $valueKeyArray[1] ); // now add to option set $grouped_['group'][$valueKeyArray[2]][] = PHP_EOL . Indent::_(1) . $taber . Indent::_(3) . ''; $optionArray[$valueKeyArray[0]] = $langValue; // set order $order_['group' . $valueKeyArray[2]] = $valueKeyArray[2]; } else { $langValue = $langView . '_' . FieldHelper::safe( $valueKeyArray[1], true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $valueKeyArray[1] ); // now add to option set $grouped_['option'][$valueKeyArray[0]] = PHP_EOL . Indent::_(1) . $taber . Indent::_(2) . ''; $optionArray[$valueKeyArray[0]] = $langValue; // set order $order_['option' . $valueKeyArray[0]] = $valueKeyArray[0]; } } else { // text is also the value $langValue = $langView . '_' . FieldHelper::safe( $option, true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $option ); // now add to option set $grouped_['option'][$option] = PHP_EOL . Indent::_(1) . $taber . Indent::_(2) . ''; $optionArray[$option] = $langValue; // set order $order_['option' . $option] = $option; } } // now build the groups foreach ($order_ as $pointer_ => $_id) { // load the default key $key_ = 'group'; if (strpos($pointer_, 'option') !== false) { // load the option field $key_ = 'option'; } // check if this is a group loader if ('group' === $key_ && isset($groups_[$_id]) && isset($grouped_[$key_][$_id]) && ArrayHelper::check( $grouped_[$key_][$_id] )) { // set group label $optionSet .= $groups_[$_id]; foreach ($grouped_[$key_][$_id] as $option_) { $optionSet .= $option_; } unset($groups_[$_id]); unset($grouped_[$key_][$_id]); // close the group $optionSet .= PHP_EOL . Indent::_(1) . $taber . Indent::_(2) . ''; } elseif (isset($grouped_[$key_][$_id]) && StringHelper::check( $grouped_[$key_][$_id] )) { $optionSet .= $grouped_[$key_][$_id]; } } } elseif (strpos((string) $value, ',') !== false) { // mulitpal options $options = explode(',', (string) $value); foreach ($options as $option) { if (strpos($option, '|') !== false) { // has other value then text list($v, $t) = explode('|', $option); $langValue = $langView . '_' . FieldHelper::safe( $t, true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $t ); // now add to option set $optionSet .= PHP_EOL . Indent::_(1) . $taber . Indent::_(2) . ''; $optionArray[$v] = $langValue; } else { // text is also the value $langValue = $langView . '_' . FieldHelper::safe( $option, true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $option ); // now add to option set $optionSet .= PHP_EOL . Indent::_(2) . $taber . Indent::_(1) . ''; $optionArray[$option] = $langValue; } } } else { // one option if (strpos((string) $value, '|') !== false) { // has other value then text list($v, $t) = explode('|', (string) $value); $langValue = $langView . '_' . FieldHelper::safe( $t, true ); // add to lang array $this->language->set($this->config->lang_target, $langValue, $t); // now add to option set $optionSet .= PHP_EOL . Indent::_(2) . $taber . Indent::_(1) . ''; $optionArray[$v] = $langValue; } else { // text is also the value $langValue = $langView . '_' . FieldHelper::safe( $value, true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $value ); // now add to option set $optionSet .= PHP_EOL . Indent::_(2) . $taber . Indent::_(1) . ''; $optionArray[$value] = $langValue; } } } } // if options were found if (StringHelper::check($optionSet)) { $field .= '>'; $field .= PHP_EOL . Indent::_(3) . $taber . ""; $field .= $optionSet; $field .= PHP_EOL . Indent::_(2) . $taber . ""; } // if no options found and must have a list of options elseif ($this->groups->check($typeName, 'list')) { $optionArray = null; $field .= PHP_EOL . Indent::_(2) . $taber . "/>"; $field .= PHP_EOL . Indent::_(2) . $taber . "" . PHP_EOL; } else { $optionArray = null; $field .= PHP_EOL . Indent::_(2) . $taber . "/>"; } } elseif ($setType === 'plain') { // now add to the field set $field .= PHP_EOL . Indent::_(2) . $taber . ""; $field .= PHP_EOL . Indent::_(2) . $taber . " $value) { if ($property != 'option') { $field .= PHP_EOL . Indent::_(2) . $taber . Indent::_(1) . $property . '="' . $value . '"'; } } $field .= PHP_EOL . Indent::_(2) . $taber . "/>"; } elseif ($setType === 'spacer') { // now add to the field set $field .= PHP_EOL . Indent::_(2) . ""; $field .= PHP_EOL . Indent::_(2) . " $value) { if ($property != 'option') { $field .= " " . $property . '="' . $value . '"'; } } $field .= " />"; } elseif ($setType === 'special') { // set the repeatable field if ($typeName === 'repeatable') { // now add to the field set $field .= PHP_EOL . Indent::_(2) . ""; $field .= PHP_EOL . Indent::_(2) . " $value) { if ($property != 'fields') { $field .= PHP_EOL . Indent::_(3) . $property . '="' . $value . '"'; } } $field .= ">"; $field .= PHP_EOL . Indent::_(3) . ''; $field .= PHP_EOL . Indent::_(4) . '"; $field .= PHP_EOL . Indent::_(3) . ""; $field .= PHP_EOL . Indent::_(2) . ""; } // set the subform fields (it is a repeatable without the modal) elseif ($typeName === 'subform') { // now add to the field set $field .= PHP_EOL . Indent::_(2) . $taber . ""; $field .= PHP_EOL . Indent::_(2) . $taber . " $value) { if ($property != 'fields') { $field .= PHP_EOL . Indent::_(3) . $taber . $property . '="' . $value . '"'; } } $field .= ">"; $field .= PHP_EOL . Indent::_(3) . $taber . '"; $field .= PHP_EOL . Indent::_(2) . $taber . ""; } } elseif ($setType === 'custom') { // now add to the field set $field .= PHP_EOL . Indent::_(2) . $taber . ""; $field .= PHP_EOL . Indent::_(2) . $taber . " $value) { if ($property != 'option') { $field .= PHP_EOL . Indent::_(2) . $taber . Indent::_(1) . $property . '="' . $value . '"'; } elseif ($property === 'option') { $optionSet = ''; if (strtolower($typeName) === 'groupedlist' && strpos( (string) $value, ',' ) !== false && strpos((string) $value, '@@') !== false) { // reset the group temp arrays $groups_ = array(); $grouped_ = array('group' => array(), 'option' => array()); $order_ = array(); // mulitpal options $options = explode(',', (string) $value); foreach ($options as $option) { if (strpos($option, '@@') !== false) { // set the group label $valueKeyArray = explode('@@', $option); if (count((array) $valueKeyArray) == 2) { $langValue = $langView . '_' . FieldHelper::safe( $valueKeyArray[0], true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $valueKeyArray[0] ); // now add group label $groups_[$valueKeyArray[1]] = PHP_EOL . Indent::_(1) . $taber . Indent::_(2) . ''; // set order $order_['group' . $valueKeyArray[1]] = $valueKeyArray[1]; } } elseif (strpos($option, '|') !== false) { // has other value then text $valueKeyArray = explode('|', $option); if (count((array) $valueKeyArray) == 3) { $langValue = $langView . '_' . FieldHelper::safe( $valueKeyArray[1], true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $valueKeyArray[1] ); // now add to option set $grouped_['group'][$valueKeyArray[2]][] = PHP_EOL . Indent::_(1) . $taber . Indent::_(3) . ''; $optionArray[$valueKeyArray[0]] = $langValue; // set order $order_['group' . $valueKeyArray[2]] = $valueKeyArray[2]; } else { $langValue = $langView . '_' . FieldHelper::safe( $valueKeyArray[1], true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $valueKeyArray[1] ); // now add to option set $grouped_['option'][$valueKeyArray[0]] = PHP_EOL . Indent::_(1) . $taber . Indent::_(2) . ''; $optionArray[$valueKeyArray[0]] = $langValue; // set order $order_['option' . $valueKeyArray[0]] = $valueKeyArray[0]; } } else { // text is also the value $langValue = $langView . '_' . FieldHelper::safe( $option, true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $option ); // now add to option set $grouped_['option'][$option] = PHP_EOL . Indent::_(1) . $taber . Indent::_(2) . ''; $optionArray[$option] = $langValue; // set order $order_['option' . $option] = $option; } } // now build the groups foreach ($order_ as $pointer_ => $_id) { // load the default key $key_ = 'group'; if (strpos($pointer_, 'option') !== false) { // load the option field $key_ = 'option'; } // check if this is a group loader if ('group' === $key_ && isset($groups_[$_id]) && isset($grouped_[$key_][$_id]) && ArrayHelper::check( $grouped_[$key_][$_id] )) { // set group label $optionSet .= $groups_[$_id]; foreach ($grouped_[$key_][$_id] as $option_) { $optionSet .= $option_; } unset($groups_[$_id]); unset($grouped_[$key_][$_id]); // close the group $optionSet .= PHP_EOL . Indent::_(1) . $taber . Indent::_(2) . ''; } elseif (isset($grouped_[$key_][$_id]) && StringHelper::check( $grouped_[$key_][$_id] )) { $optionSet .= $grouped_[$key_][$_id]; } } } elseif (strpos((string) $value, ',') !== false) { // mulitpal options $options = explode(',', (string) $value); foreach ($options as $option) { if (strpos($option, '|') !== false) { // has other value then text list($v, $t) = explode('|', $option); $langValue = $langView . '_' . FieldHelper::safe( $t, true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $t ); // now add to option set $optionSet .= PHP_EOL . Indent::_(1) . $taber . Indent::_(2) . ''; $optionArray[$v] = $langValue; } else { // text is also the value $langValue = $langView . '_' . FieldHelper::safe( $option, true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $option ); // now add to option set $optionSet .= PHP_EOL . Indent::_(2) . $taber . Indent::_(1) . ''; $optionArray[$option] = $langValue; } } } else { // one option if (strpos((string) $value, '|') !== false) { // has other value then text list($v, $t) = explode('|', (string) $value); $langValue = $langView . '_' . FieldHelper::safe( $t, true ); // add to lang array $this->language->set($this->config->lang_target, $langValue, $t); // now add to option set $optionSet .= PHP_EOL . Indent::_(2) . $taber . Indent::_(1) . ''; $optionArray[$v] = $langValue; } else { // text is also the value $langValue = $langView . '_' . FieldHelper::safe( $value, true ); // add to lang array $this->language->set( $this->config->lang_target, $langValue, $value ); // now add to option set $optionSet .= PHP_EOL . Indent::_(2) . $taber . Indent::_(1) . ''; $optionArray[$value] = $langValue; } } } } // if options were found if (StringHelper::check($optionSet)) { $field .= '>'; $field .= PHP_EOL . Indent::_(3) . $taber . ""; $field .= $optionSet; $field .= PHP_EOL . Indent::_(2) . $taber . ""; } // if no options found and must have a list of options elseif ($this->groups->check($typeName, 'list')) { $optionArray = null; $field .= PHP_EOL . Indent::_(2) . $taber . "/>"; $field .= PHP_EOL . Indent::_(2) . $taber . "" . PHP_EOL; } else { $optionArray = null; $field .= PHP_EOL . Indent::_(2) . $taber . "/>"; } // incase the field is in the config and has not been set if ('config' === $nameSingleCode && 'configs' === $nameListCode || (strpos($nameSingleCode, 'pLuG!n') !== false || strpos( $nameSingleCode, 'M0dUl3' ) !== false)) { // set lang (just incase) $listLangName = $langView . '_' . StringHelper::safe($name, 'U'); // set the custom array $data = array('type' => $typeName, 'code' => $name, 'lang' => $listLangName, 'custom' => $custom); // set the custom field file $this->customfieldtypefile->set( $data, $nameListCode, $nameSingleCode ); } } // return field return $field; } }