From 3a4a0e613b05252ba76f12027b5ad1c36e41b3a0 Mon Sep 17 00:00:00 2001 From: Llewellyn van der Merwe Date: Sat, 7 Jul 2018 02:12:13 +0200 Subject: [PATCH] Added the table and field validation to avoid import breakages due to new table or fields not found in the older local install of JCB. --- README.md | 4 +- admin/README.txt | 4 +- .../en-GB/en-GB.com_componentbuilder.ini | 2 + admin/models/ajax.php | 2 +- admin/models/import_joomla_components.php | 72 ++++++++++++++++++- admin/models/joomla_components.php | 2 +- componentbuilder.xml | 2 +- 7 files changed, 78 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2e9d36eeb..4f4af5ca4 100644 --- a/README.md +++ b/README.md @@ -125,11 +125,11 @@ Watch the [proposed development workflow](https://vdm.bz/proposed-development-wo + *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*: 6th July, 2018 ++ *Last Build*: 7th July, 2018 + *Version*: 2.8.2 + *Copyright*: Copyright (C) 2015 - 2018 Vast Development Method. All rights reserved. + *License*: GNU General Public License version 2 or later; see LICENSE.txt -+ *Line count*: **186219** ++ *Line count*: **186287** + *Field count*: **1058** + *File count*: **1238** + *Folder count*: **197** diff --git a/admin/README.txt b/admin/README.txt index 2e9d36eeb..4f4af5ca4 100644 --- a/admin/README.txt +++ b/admin/README.txt @@ -125,11 +125,11 @@ Watch the [proposed development workflow](https://vdm.bz/proposed-development-wo + *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*: 6th July, 2018 ++ *Last Build*: 7th July, 2018 + *Version*: 2.8.2 + *Copyright*: Copyright (C) 2015 - 2018 Vast Development Method. All rights reserved. + *License*: GNU General Public License version 2 or later; see LICENSE.txt -+ *Line count*: **186219** ++ *Line count*: **186287** + *Field count*: **1058** + *File count*: **1238** + *Folder count*: **197** diff --git a/admin/language/en-GB/en-GB.com_componentbuilder.ini b/admin/language/en-GB/en-GB.com_componentbuilder.ini index 75c1b9d59..7829e8509 100644 --- a/admin/language/en-GB/en-GB.com_componentbuilder.ini +++ b/admin/language/en-GB/en-GB.com_componentbuilder.ini @@ -3896,6 +3896,7 @@ COM_COMPONENTBUILDER_FIELD_ADD_JAVASCRIPT_VIEW_FOOTER_LABEL="Add JavaScript (vie COM_COMPONENTBUILDER_FIELD_BASESIXTY_FOUR="base64" COM_COMPONENTBUILDER_FIELD_BASIC_ENCRYPTION_LOCALDBKEY="Basic Encryption (local-DB-key)" COM_COMPONENTBUILDER_FIELD_BIGINT="BIGINT" +COM_COMPONENTBUILDER_FIELD_BSB_NOT_FOUND_IN_LOCAL_DATABASE_TABLE_S_SO_IMPORTED_OF_ITS_VALUES_FAILED_PLEASE_UPDATE_YOUR_JCB_INSTALL_AND_TRY_AGAIN="Field %s not found in local database table (%s) so imported of its values failed, please update your JCB install and try again." COM_COMPONENTBUILDER_FIELD_CATID_DESCRIPTION="select one of the following categories" COM_COMPONENTBUILDER_FIELD_CATID_LABEL="Category" COM_COMPONENTBUILDER_FIELD_CHAR="CHAR" @@ -6416,6 +6417,7 @@ COM_COMPONENTBUILDER_S_BEING_IMPORTED="%s Being Imported" COM_COMPONENTBUILDER_S_NOT_LINKED="%s Not Linked" COM_COMPONENTBUILDER_S_PLEASE_WAIT_THE_COMPONENT_IS_BEING_COMPILED="%s, please wait! The component is being compiled" COM_COMPONENTBUILDER_TAB="Tab" +COM_COMPONENTBUILDER_TABLE_BSB_NOT_FOUND_IN_THE_LOCAL_DATABASE_SO_ITS_VALUES_COULD_NOT_BE_IMPORTED_PLEASE_UPDATE_YOUR_JCB_INSTALL_AND_TRY_AGAIN="Table %s not found in the local database so its values could not be imported, please update your JCB install and try again." COM_COMPONENTBUILDER_TARGET_BEHAVIOUR="Target Behaviour" COM_COMPONENTBUILDER_TARGET_FIELDS="Target Field/s" COM_COMPONENTBUILDER_TARGET_PATH="Target Path" diff --git a/admin/models/ajax.php b/admin/models/ajax.php index 049225593..c7e03c5c7 100644 --- a/admin/models/ajax.php +++ b/admin/models/ajax.php @@ -2117,7 +2117,7 @@ class ComponentbuilderModelAjax extends JModelList 'php_after_delete','php_after_publish','php_ajaxmethod','php_allowedit','php_batchcopy', 'php_batchmove','php_before_delete','php_before_publish','php_before_save','php_controller', 'php_controller_list','php_document','php_getitem','php_getitems','php_getitems_after_all', - 'php_getlistquery','php_import','php_import_display','php_import_ext','php_import_headers', + 'php_getlistquery','php_import','php_import_display','php_import_ext','php_import_headers','php_getform', 'php_import_save','php_import_setdata','php_model','php_model_list','php_postsavehook','php_save'); $targets['admin_view']['view'] = 'admin_views'; $targets['admin_view']['not_base64'] = array(); diff --git a/admin/models/import_joomla_components.php b/admin/models/import_joomla_components.php index 9ca086665..e062fa750 100644 --- a/admin/models/import_joomla_components.php +++ b/admin/models/import_joomla_components.php @@ -81,6 +81,8 @@ class ComponentbuilderModelImport_joomla_components extends JModelLegacy protected $divergedDataMover = array(); protected $fieldTypes = array(); protected $isMultiple = array(); + protected $tableColumns = array(); + protected $fieldImportErrors = array(); protected $specialValue = false; protected $checksum = null; protected $checksumURLs = array('vdm' => 'https://raw.githubusercontent.com/vdm-io/JCB-Packages/master/', 'jcb' => 'https://raw.githubusercontent.com/vdm-io/JCB-Community-Packages/master/'); @@ -702,13 +704,25 @@ class ComponentbuilderModelImport_joomla_components extends JModelLegacy 'component_custom_admin_views', 'component_updates', 'component_mysql_tweaks', 'component_custom_admin_menus', 'component_config', 'component_dashboard', 'component_files_folders' ); + // get prefix + $prefix = $this->_db->getPrefix(); + // get local tables + $localTables = $this->_db->getTableList(); // smart table loop foreach ($tables as $table) { - // save the table to database - if (!$this->saveSmartItems($table)) + // only continue the import if the table is available locally + if (in_array($prefix . 'componentbuilder_' . $table, $localTables)) { - return false; + // save the table to database + if (!$this->saveSmartItems($table)) + { + return false; + } + } + else + { + $this->app->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_TABLE_BSB_NOT_FOUND_IN_THE_LOCAL_DATABASE_SO_ITS_VALUES_COULD_NOT_BE_IMPORTED_PLEASE_UPDATE_YOUR_JCB_INSTALL_AND_TRY_AGAIN', '#__componentbuilder_' . $table), 'warning'); } } // do a after all run on all items that need it @@ -2205,6 +2219,8 @@ class ComponentbuilderModelImport_joomla_components extends JModelLegacy // update the subform ids $this->updateSubformsIDs($item, $type, $updaterT); } + // remove all fields/columns not part of the current table + $this->removingFields($type, $item); // final action prep switch($action) { @@ -2236,6 +2252,56 @@ class ComponentbuilderModelImport_joomla_components extends JModelLegacy return false; } + /** + * remove all fields/columns not part of the current table + * + * @param string $type The table this item belongs to + * @param object $item The item to clean + * + * @return viod + */ + protected function removingFields($type, &$item) + { + // get the columns + $columns = $this->getTableColumns("#__componentbuilder_" . $type); + if (ComponentbuilderHelper::checkArray($columns)) + { + foreach ($item as $name => $value) + { + if (!isset($columns[$name])) + { + // we must show a warning that this field was not imported (but just once) + if (!isset($this->fieldImportErrors[$type.$name])) + { + $this->app->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_FIELD_BSB_NOT_FOUND_IN_LOCAL_DATABASE_TABLE_S_SO_IMPORTED_OF_ITS_VALUES_FAILED_PLEASE_UPDATE_YOUR_JCB_INSTALL_AND_TRY_AGAIN', $name, '#__componentbuilder_' . $type), 'warning'); + // make sure the message is not loaded again + $this->fieldImportErrors[$type.$name] = true; + } + // remove the field & value + unset($item->{$name}); + } + } + } + } + + /** + * get table columns + * + * @param string $table The table + * + * @return array + */ + protected function getTableColumns($table) + { + // check if the columns are in memory + if (!isset($this->tableColumns[$table])) + { + // get the columns + $this->tableColumns[$table] = $this->_db->getTableColumns($table); + } + return $this->tableColumns[$table]; + } + /** * Set the data that should be moved * diff --git a/admin/models/joomla_components.php b/admin/models/joomla_components.php index 0e02ca7cd..2e38a5667 100644 --- a/admin/models/joomla_components.php +++ b/admin/models/joomla_components.php @@ -1607,7 +1607,7 @@ class ComponentbuilderModelJoomla_components extends JModelList 'php_after_delete','php_after_publish','php_ajaxmethod','php_allowedit','php_batchcopy', 'php_batchmove','php_before_delete','php_before_publish','php_before_save','php_controller', 'php_controller_list','php_document','php_getitem','php_getitems','php_getitems_after_all', - 'php_getlistquery','php_import','php_import_display','php_import_ext','php_import_headers', + 'php_getlistquery','php_import','php_import_display','php_import_ext','php_import_headers','php_getform', 'php_import_save','php_import_setdata','php_model','php_model_list','php_postsavehook','php_save'); $targets['admin_view']['view'] = 'admin_views'; $targets['admin_view']['not_base64'] = array(); diff --git a/componentbuilder.xml b/componentbuilder.xml index 35c527ccc..6bc2639a8 100644 --- a/componentbuilder.xml +++ b/componentbuilder.xml @@ -1,7 +1,7 @@ COM_COMPONENTBUILDER - 6th July, 2018 + 7th July, 2018 Llewellyn van der Merwe llewellyn@joomlacomponentbuilder.com http://www.joomlacomponentbuilder.com