diff --git a/.gitignore b/.gitignore index dc5d9380..159d3ccc 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ /.idea/inspectionProfiles/Project_Default.xml /.idea/vcs.xml /administrator/com_joomgallery/joomgallery.xml +/administrator/com_joomgallery/joomgallery_old.xml node_modules diff --git a/administrator/com_joomgallery/src/Table/CategoryTable.php b/administrator/com_joomgallery/src/Table/CategoryTable.php index 29356a11..010e5be5 100644 --- a/administrator/com_joomgallery/src/Table/CategoryTable.php +++ b/administrator/com_joomgallery/src/Table/CategoryTable.php @@ -108,6 +108,18 @@ public function getTypeAlias() return $this->typeAlias; } + /** + * Resets the root_id property to the default value: 0 + * + * @return void + * + * @since 4.0.0 + */ + public static function resetRootId() + { + self::$root_id = 0; + } + /** * Define a namespaced asset name for inclusion in the #__assets table * diff --git a/script.php b/script.php index 1df1d6a5..9cef174d 100644 --- a/script.php +++ b/script.php @@ -11,15 +11,16 @@ defined('_JEXEC') or die(); use \Joomla\CMS\Factory; -use \Joomla\Database\DatabaseInterface; +use \Joomla\CMS\Uri\Uri; +use \Joomla\CMS\Table\Table; use \Joomla\CMS\Language\Text; use \Joomla\CMS\Router\Route; -use \Joomla\CMS\Installer\Installer; -use \Joomla\CMS\Installer\InstallerScript; use \Joomla\CMS\Filesystem\File; use \Joomla\CMS\Filesystem\Folder; -use \Joomla\CMS\Uri\Uri; -use \Joomla\CMS\Table\Table; +use \Joomla\CMS\Installer\Installer; +use \Joomla\CMS\Installer\InstallerScript; +use \Joomla\Database\DatabaseInterface; +use \Joomla\CMS\Language\LanguageFactoryInterface; /** * Install method @@ -58,6 +59,13 @@ class com_joomgalleryInstallerScript extends InstallerScript */ protected $new_code = ''; + /** + * Counter variable + * + * @var int + */ + protected $count = 0; + /** * True to skip output during install() method * @@ -65,6 +73,14 @@ class com_joomgalleryInstallerScript extends InstallerScript */ protected $installSkipMsg = false; + /** + * True to show that the current script is exectuted during an upgrade + * from an old JoomGallery version (JG 1-3) + * + * @var bool + */ + protected $fromOldJG = false; + /** * Method called before install/update the component. Note: This method won't be called during uninstall process. @@ -141,13 +157,19 @@ public function preflight($type, $parent) if($jgtables) { $db = Factory::getContainer()->get(DatabaseInterface::class); - foreach($jgtables as $oldTable) { $db->renameTable($oldTable, $oldTable.'_old'); } } + // copy old XML file (JGv1-3) + $xml_path = JPATH_ADMINISTRATOR.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_joomgallery'.DIRECTORY_SEPARATOR; + if(File::exists($xml_path.'joomgallery.xml')) + { + File::copy($xml_path.'joomgallery.xml', $xml_path.'joomgallery_old.xml'); + } + // remove old JoomGallery files and folders foreach($this->detectJGfolders() as $folder) { @@ -208,12 +230,14 @@ public function install($parent) if($this->installSkipMsg) { + // Skip install method here if we upgrade from an old version + // and we don't want to show the install text. return; } // Create news feed module $subdomain = ''; - $language = Factory::getLanguage(); + $language = $app->getLanguage(); if(strpos($language->getTag(), 'de-') === false) { $subdomain = 'en.'; @@ -269,6 +293,7 @@ public function update($parent) { // We update from an old version (JG 1-3) $this->installSkipMsg = true; + $this->fromOldJG = true; $this->install($parent); } else @@ -388,7 +413,7 @@ public function uninstall($parent) */ function postflight($type, $parent) { - if($type == 'install') + if($type == 'install' || ($type == 'update' && $this->fromOldJG)) { $app = Factory::getApplication(); @@ -520,7 +545,8 @@ public function addDefaultCategory() require_once $class_path; if(class_exists($tableClass)) - { + { + $tableClass::resetRootId(); $table = new $tableClass($db); } else @@ -854,35 +880,47 @@ private function installModules($parent) /** * Uninstalls plugins * - * @param mixed $parent Object who called the uninstall method + * @param mixed $parent Object who called the uninstall method or array with plugin names * - * @return void + * @return void */ private function uninstallPlugins($parent) { $app = Factory::getApplication(); - if(method_exists($parent, 'getManifest')) - { - $plugins = $parent->getManifest()->plugins; - } - else - { - $plugins = $parent->get('manifest')->plugins; - } - - if(!$plugins || empty($plugins->children()) || count($plugins->children()) <= 0) + if(is_array($parent)) { - return; + // We got an array of module names + $modules = $parent; } + else + { + // We got the parent object + if(method_exists($parent, 'getManifest')) + { + $plugins = $parent->getManifest()->plugins; + } + else + { + $plugins = $parent->get('manifest')->plugins; + } + + if(!$plugins || empty($plugins->children()) || count($plugins->children()) <= 0) + { + return; + } + + $plugins = $plugins->children(); + } $db = Factory::getContainer()->get(DatabaseInterface::class); $query = $db->getQuery(true); - foreach($plugins->children() as $plugin) + foreach($plugins as $plugin) { $pluginName = (string) $plugin['plugin']; $pluginGroup = (string) $plugin['group']; + $query ->clear() ->select('extension_id') @@ -941,17 +979,19 @@ private function uninstallModules($parent) { $modules = $parent->get('manifest')->modules; } - } - if(!$modules || empty($modules->children()) || count($modules->children()) <= 0) - { - return; - } + if(!$modules || empty($modules->children()) || count($modules->children()) <= 0) + { + return; + } + + $modules = $modules->children(); + } $db = Factory::getContainer()->get(DatabaseInterface::class); $query = $db->getQuery(true); - foreach($modules->children() as $module) + foreach($modules as $module) { if(is_array($parent)) { @@ -1300,7 +1340,7 @@ private function createModule($title, $position, $module, $ordering, $access, $s $row->language = $lang; if(!$row->store()) { - $app->enqueueMessage(Text::_('Unable to create "'.$title.'" module!'), 'error'); + Factory::getApplication()->enqueueMessage(Text::_('Unable to create "'.$title.'" module!'), 'error'); return false; }