From dfbd2b2a6d82a60cf9b4508581069c00906f4925 Mon Sep 17 00:00:00 2001 From: Llewellyn van der Merwe Date: Sat, 5 Dec 2020 08:17:54 +0200 Subject: [PATCH] Resolve gh-609 to make sure that custom code set in a field that gets linked to a module will get added to the module. --- README.md | 4 +- admin/README.txt | 4 +- admin/helpers/compiler/a_Get.php | 97 +++- admin/helpers/compiler/b_Structure.php | 585 +++++++------------- admin/helpers/compiler/c_Fields.php | 5 +- admin/helpers/compiler/e_Interpretation.php | 100 ++-- componentbuilder.xml | 2 +- 7 files changed, 319 insertions(+), 478 deletions(-) diff --git a/README.md b/README.md index 29493747c..32364d302 100644 --- a/README.md +++ b/README.md @@ -144,13 +144,13 @@ TODO + *Author*: [Llewellyn van der Merwe](mailto:llewellyn@joomlacomponentbuilder.com) + *Name*: [Component Builder](https://github.com/vdm-io/Joomla-Component-Builder) + *First Build*: 30th April, 2015 -+ *Last Build*: 3rd December, 2020 ++ *Last Build*: 5th December, 2020 + *Version*: 2.12.1 + *Copyright*: Copyright (C) 2015 - 2020 Vast Development Method. All rights reserved. + *License*: GNU General Public License version 2 or later; see LICENSE.txt + *Line count*: **289032** + *Field count*: **1537** -+ *File count*: **1880** ++ *File count*: **1902** + *Folder count*: **304** > This **component** was build with a [Joomla](https://extensions.joomla.org/extension/component-builder/) [Automated Component Builder](http://joomlacomponentbuilder.com). diff --git a/admin/README.txt b/admin/README.txt index 29493747c..32364d302 100644 --- a/admin/README.txt +++ b/admin/README.txt @@ -144,13 +144,13 @@ TODO + *Author*: [Llewellyn van der Merwe](mailto:llewellyn@joomlacomponentbuilder.com) + *Name*: [Component Builder](https://github.com/vdm-io/Joomla-Component-Builder) + *First Build*: 30th April, 2015 -+ *Last Build*: 3rd December, 2020 ++ *Last Build*: 5th December, 2020 + *Version*: 2.12.1 + *Copyright*: Copyright (C) 2015 - 2020 Vast Development Method. All rights reserved. + *License*: GNU General Public License version 2 or later; see LICENSE.txt + *Line count*: **289032** + *Field count*: **1537** -+ *File count*: **1880** ++ *File count*: **1902** + *Folder count*: **304** > This **component** was build with a [Joomla](https://extensions.joomla.org/extension/component-builder/) [Automated Component Builder](http://joomlacomponentbuilder.com). diff --git a/admin/helpers/compiler/a_Get.php b/admin/helpers/compiler/a_Get.php index 1ecf32328..1fc694334 100644 --- a/admin/helpers/compiler/a_Get.php +++ b/admin/helpers/compiler/a_Get.php @@ -3986,8 +3986,6 @@ class Get $this->_fieldData[$id]->css_view_decoded = true; } } - // add this only once to view. - $this->customFieldScript[$name_single][$id] = true; } // check if we should load scripts for list views if (ComponentbuilderHelper::checkString($name_list) @@ -4058,7 +4056,7 @@ class Get $this->setCustomScriptBuilder( $this->_fieldData[$id]->css_views, 'css_views', - $name_list, + $name_single, false, array('prefix' => PHP_EOL), $convert__, @@ -4070,10 +4068,11 @@ class Get $this->_fieldData[$id]->css_views_decoded = true; } } - - // add this only once to view. - $this->customFieldScript[$name_list][$id] = true; } + // add this only once to single view. + $this->customFieldScript[$name_single][$id] = true; + // add this only once to list view. + $this->customFieldScript[$name_list][$id] = true; } if ($id > 0 && isset($this->_fieldData[$id])) { @@ -5235,6 +5234,57 @@ class Get return false; } + /** + * get the a script from the custom script builder + * + * @param string $first The first key + * @param string $second The second key + * @param string $prefix The prefix to add in front of the script if found + * @param string $note The switch/note to add to the script + * @param bool $unset The switch to unset the value if found + * @param string $default The switch/string to use as default return if script not found + * @param string $sufix The sufix to add after the script if found + * + * @return mix The string/script if found or the default value if not found + * + */ + public function getCustomScriptBuilder($first, $second, $prefix = '', + $note = null, $unset = null, $default = null, $sufix = '' + ) { + // default is to return an empty string + $script = ''; + // check if there is any custom script + if (isset($this->customScriptBuilder[$first][$second]) + && ComponentbuilderHelper::checkString( + $this->customScriptBuilder[$first][$second] + )) + { + // add not if set + if ($note) + { + $script .= $note; + } + // load the actual script + $script .= $prefix . str_replace( + array_keys($this->placeholders), + array_values($this->placeholders), + $this->customScriptBuilder[$first][$second] + ) . $sufix; + // clear some memory + if ($unset) + { + unset($this->customScriptBuilder[$first][$second]); + } + } + // if not found return default + if (!ComponentbuilderHelper::checkString($script) && $default) + { + return $default; + } + + return $script; + } + /** * To limit the SQL Demo date build in the views * @@ -8699,7 +8749,7 @@ class Get $unique = $form['fields_name'] . $form['fieldset']; } - // set global fields rule path switchs + // set global fields rule path switches if ($module->fields_rules_paths == 1 && isset($form['fields_rules_paths']) && $form['fields_rules_paths'] == 2) @@ -9095,6 +9145,39 @@ class Get return $xml; } + /** + * get the module admin custom script field + * + * @return string + * + */ + public function getModAdminVvvvvvvdm($fieldScriptBucket) + { + $form_field_class = array(); + $form_field_class[] = $this->hhh . 'BOM' . $this->hhh . PHP_EOL; + $form_field_class[] = "//" . $this->setLine(__LINE__) . " No direct access to this file"; + $form_field_class[] = "defined('_JEXEC') or die('Restricted access');"; + $form_field_class[] = PHP_EOL . "use Joomla\CMS\Form\FormField;"; + $form_field_class[] = "use Joomla\CMS\Factory;"; + $form_field_class[] = PHP_EOL . "class JFormFieldModadminvvvvvvvdm extends FormField"; + $form_field_class[] = "{"; + $form_field_class[] = $this->_t(1) . "protected \$type = 'modadminvvvvvvvdm';"; + $form_field_class[] = PHP_EOL . $this->_t(1) . "protected function getLabel()"; + $form_field_class[] = $this->_t(1) . "{"; + $form_field_class[] = $this->_t(2) . "return;"; + $form_field_class[] = $this->_t(1) . "}"; + $form_field_class[] = PHP_EOL . $this->_t(1) . "protected function getInput()"; + $form_field_class[] = $this->_t(1) . "{"; + $form_field_class[] = $this->_t(2) . "//" . $this->setLine(__LINE__) . " Get the document"; + $form_field_class[] = $this->_t(2) . "\$document = Factory::getDocument();"; + $form_field_class[] = implode(PHP_EOL, $fieldScriptBucket); + $form_field_class[] = $this->_t(2) . "return; // noting for now :)"; + $form_field_class[] = $this->_t(1) . "}"; + $form_field_class[] = "}"; + + return implode(PHP_EOL, $form_field_class); + } + /** * get the Joomla plugins IDs * diff --git a/admin/helpers/compiler/b_Structure.php b/admin/helpers/compiler/b_Structure.php index 281044758..d963a8eb0 100644 --- a/admin/helpers/compiler/b_Structure.php +++ b/admin/helpers/compiler/b_Structure.php @@ -501,16 +501,8 @@ class Structure extends Get $this->dynamicPaths[$module->key] = $module->folder_path; // make sure there is no old build $this->removeFolder($module->folder_path); - // creat the main component folder - if (!JFolder::exists($module->folder_path)) - { - JFolder::create($module->folder_path); - // count the folder created - $this->folderCount++; - $this->indexHTML( - $module->folder_name, $this->compilerPath - ); - } + // creat the main module folder + $this->createFolder($module->folder_path); // set main mod file $fileDetails = array('path' => $module->folder_path . '/' . $module->file_name . '.php', @@ -592,15 +584,7 @@ class Structure extends Get // count the file created $this->fileCount++; // set tmpl folder - if (!JFolder::exists($module->folder_path . '/tmpl')) - { - JFolder::create($module->folder_path . '/tmpl'); - // count the folder created - $this->folderCount++; - $this->indexHTML( - $module->folder_name . '/tmpl', $this->compilerPath - ); - } + $this->createFolder($module->folder_path . '/tmpl'); // set default file $fileDetails = array('path' => $module->folder_path . '/tmpl/default.php', @@ -652,32 +636,116 @@ class Structure extends Get // count the file created $this->fileCount++; } - // set fields & rules folders if needed + // set the folders target path + $target_path = ''; + if ($module->target_client === 'administrator') + { + $target_path = '/administrator'; + } + // check if we have custom fields needed for scripts + $module->add_scripts_field = false; + $field_script_bucket = array(); + // add any css from the fields + if (($css = $this->getCustomScriptBuilder( + 'css_view', $module->key + )) !== null + && ComponentbuilderHelper::checkString($css)) + { + // make sure this script does not have PHP + if (strpos($css, 'add_scripts_field = true; + // create the css folder + $this->createFolder($module->folder_path . '/css'); + // add the CSS file + $fileDetails = array('path' => $module->folder_path + . '/css/mod_admin.css', + 'name' => 'mod_admin.css', + 'zip' => 'mod_admin.css'); + $this->writeFile( + $fileDetails['path'], + $this->hhh . 'BOM' . $this->hhh . PHP_EOL + . PHP_EOL . $css + ); + $this->newFiles[$module->key][] = $fileDetails; + // count the file created + $this->fileCount++; + // add the field script + $field_script_bucket[] = $this->_t(2) . "//" + . $this->setLine(__LINE__) . " Custom CSS"; + $field_script_bucket[] = $this->_t(2) + . "\$document->addStyleSheet('" . $target_path + . "/modules/" . $module->folder_name + . "/css/mod_admin.css', ['version' => 'auto', 'relative' => true]);"; + } + } + // add any JavaScript from the fields + if (($javascript = $this->getCustomScriptBuilder( + 'view_footer', $module->key + )) !== null + && ComponentbuilderHelper::checkString($javascript)) + { + // make sure this script does not have PHP + if (strpos($javascript, 'add_scripts_field = true; + // add the JavaScript file + $this->createFolder($module->folder_path . '/js'); + // add the CSS file + $fileDetails = array('path' => $module->folder_path + . '/js/mod_admin.js', + 'name' => 'mod_admin.js', + 'zip' => 'mod_admin.js'); + $this->writeFile( + $fileDetails['path'], + $this->hhh . 'BOM' . $this->hhh . PHP_EOL + . PHP_EOL . $javascript + ); + $this->newFiles[$module->key][] = $fileDetails; + // count the file created + $this->fileCount++; + // add the field script + $field_script_bucket[] = $this->_t(2) . "//" + . $this->setLine(__LINE__) . " Custom JS"; + $field_script_bucket[] = $this->_t(2) + . "\$document->addScript('" . $target_path + . "/modules/" . $module->folder_name + . "/js/mod_admin.js', ['version' => 'auto', 'relative' => true]);"; + } + } + // set fields folders if needed + if ($module->add_scripts_field + || (isset($module->fields_rules_paths) + && $module->fields_rules_paths == 2)) + { + // create fields folder + $this->createFolder($module->folder_path . '/fields'); + // add the custom script field + if ($module->add_scripts_field) + { + $fileDetails = array('path' => $module->folder_path + . '/fields/modadminvvvvvvvdm.php', + 'name' => 'modadminvvvvvvvdm.php', + 'zip' => 'modadminvvvvvvvdm.php'); + $this->writeFile( + $fileDetails['path'], + $this->getModAdminVvvvvvvdm( + $field_script_bucket + ) + ); + $this->newFiles[$module->key][] = $fileDetails; + // count the file created + $this->fileCount++; + } + } + // set rules folders if needed if (isset($module->fields_rules_paths) && $module->fields_rules_paths == 2) { - // create fields folder - if (!JFolder::exists($module->folder_path . '/fields')) - { - JFolder::create($module->folder_path . '/fields'); - // count the folder created - $this->folderCount++; - $this->indexHTML( - $module->folder_name . '/fields', - $this->compilerPath - ); - } // create rules folder - if (!JFolder::exists($module->folder_path . '/rules')) - { - JFolder::create($module->folder_path . '/rules'); - // count the folder created - $this->folderCount++; - $this->indexHTML( - $module->folder_name . '/rules', - $this->compilerPath - ); - } + $this->createFolder($module->folder_path . '/rules'); } // set forms folder if needed if (isset($module->form_files) @@ -686,16 +754,7 @@ class Structure extends Get )) { // create forms folder - if (!JFolder::exists($module->folder_path . '/forms')) - { - JFolder::create($module->folder_path . '/forms'); - // count the folder created - $this->folderCount++; - $this->indexHTML( - $module->folder_name . '/forms', - $this->compilerPath - ); - } + $this->createFolder($module->folder_path . '/forms'); // set the template files foreach ($module->form_files as $file => $fields) { @@ -705,7 +764,7 @@ class Structure extends Get 'name' => $file . '.xml', 'zip' => 'forms/' . $file . '.xml'); - // biuld basic XML + // build basic XML $xml = ''; $xml .= PHP_EOL . '