diff --git a/modules/themeinstallator/themeinstallator.php b/modules/themeinstallator/themeinstallator.php index f068606da..cb8ca92d9 100644 --- a/modules/themeinstallator/themeinstallator.php +++ b/modules/themeinstallator/themeinstallator.php @@ -38,6 +38,11 @@ class ThemeInstallator extends Module private $to_disable = array(); private $to_export = array(); + private $selected_shops = array(); + private $selected_variations = array(); + + public $action_form; + /* ** index */ @@ -64,6 +69,8 @@ class ThemeInstallator extends Module parent::__construct(); $this->displayName = $this->l('Import/export a theme'); $this->description = $this->l('Export or Install a theme and its modules on your shop.'); + + $this->action_form = AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::htmlentitiesUTF8(Tools::getValue('token')); } private function getTheNativeModules() @@ -108,7 +115,7 @@ class ThemeInstallator extends Module self::deleteDirectory($dirname.'/'.$file); elseif (file_exists($dirname.'/'.$file)) unlink($dirname.'/'.$file); - } + } rmdir($dirname); } @@ -118,7 +125,7 @@ class ThemeInstallator extends Module return; if (!file_exists($dst)) mkdir($dst); - while(($file = readdir($dir)) !== false) + while (($file = readdir($dir)) !== false) if (strncmp($file, '.', 1) != 0) { if (is_dir($src.'/'.$file)) @@ -167,7 +174,7 @@ class ThemeInstallator extends Module unlink(_EXPORT_FOLDER_.'archive.zip'); } - private function init_defines() + private function initDefines() { define('_EXPORT_FOLDER_', dirname(__FILE__).'/export/'); define('_IMPORT_FOLDER_', dirname(__FILE__).'/import/'); @@ -179,12 +186,26 @@ class ThemeInstallator extends Module if (!Tools::isSubmit('cancelExport') && (Tools::isSubmit('exportTheme') || Tools::isSubmit('submitExport'))) $this->page = 'exportPage'; - $this->_html = '
'; + + $action_form = AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::htmlentitiesUTF8(Tools::getValue('token')); + $this->_html = ''; if (Tools::isSubmit('modulesToExport') || Tools::isSubmit('submitModules')) $this->to_export = Tools::getValue('modulesToExport'); if (Tools::isSubmit('submitThemes')) - $this->selectedVariations = Tools::getValue('variation'); + $this->selected_variations = Tools::getValue('variation'); + + if (Tools::isSubmit('submitModules') && Shop::isFeatureActive()) + { + // Get all selected shops (Key and values are inversed) + $shops = Tools::getValue('checkBoxShopAsso_', array($this->context->shop->id => 1)); + foreach ($shops as $shop) + foreach ($shop as $id_shop => $row) + $this->selected_shops[] = (int)$id_shop; + } + else + $this->selected_shops = array($this->context->shop->id); + $_POST = @array_map('trim', $_POST); define('DEFAULT_COMPATIBILITY_FROM', _PS_VERSION_); define('DEFAULT_COMPATIBILITY_TO', _PS_VERSION_); @@ -267,7 +288,7 @@ class ThemeInstallator extends Module } } if (file_exists(ARCHIVE_NAME)) - unlink(ARCHIVE_NAME); + @unlink(ARCHIVE_NAME); if ($this->page != 1) { if (!self::checkXmlFields()) @@ -276,7 +297,7 @@ class ThemeInstallator extends Module $this->page = 1; } else - return ; + return; } self::deleteTmpFiles(); } @@ -322,11 +343,11 @@ class ThemeInstallator extends Module self::generateArchive(); } } - self::AuthorInformationForm(); - self::ModulesInformationForm(); - self::ThemeInformationForm(); - self::DocInformationForm(); - self::VariationInformationForm(); + self::authorInformationForm(); + self::modulesInformationForm(); + self::themeInformationForm(); + self::docInformationForm(); + self::variationInformationForm(); return $this->_html; } @@ -341,23 +362,23 @@ class ThemeInstallator extends Module return '
'.$this->l('This functionality has been disabled.').'
'; } - self::init_defines(); + self::initDefines(); if (!Tools::isSubmit('cancelExport') && $this->page == 'exportPage') return self::getContentExport(); self::handleInformations(); switch ($this->page) { case 1: - self::_displayForm1(); + self::displayForm1(); break; case 2: - self::_displayForm2(); + self::displayForm2(); break; case 3: - self::_displayForm3(); + self::displayForm3(); break; case 4: - self::_displayForm4(); + self::displayForm4(); break; } return implode($this->errors, '').$this->_msg.$this->_html; @@ -366,7 +387,7 @@ class ThemeInstallator extends Module /* ** Checker si le dossier doc existe : Si oui appeler la fonction ! */ - private function _loadDocForm() + private function loadDocForm() { $docname = array(); $docpath = array(); @@ -444,121 +465,143 @@ class ThemeInstallator extends Module return $return; } - private function _displayForm4() + private function displayForm4() { $xml = simplexml_load_file(_IMPORT_FOLDER_.XMLFILENAME); $this->xml = $xml; self::getModules(); $hook = array(); - $hookedModule = array(); + $hooked_module = array(); $position = array(); $msg = ''; - $shopID = $this->context->shop->id; foreach ($this->xml->modules->hooks->hook as $row) { - $hookedModule[] = strval($row['module']); + $hooked_module[] = strval($row['module']); $hook[] = strval($row['hook']); $position[] = strval($row['position']); $exceptions[] = (isset($row['exceptions']) ? explode(',', strval($row['exceptions'])) : array()); } if (file_exists(_IMPORT_FOLDER_.'doc') && count($xml->docs->doc) != 0) - self::_loadDocForm(); + self::loadDocForm(); // install selected modules $flag = 0; - if (isset($this->to_export) && $this->to_export) - foreach ($this->to_export as $row) - { - if (in_array($row, $this->native_modules)) - continue; - if ($flag++ == 0) - $msg .= ''.$this->l('The following modules have been installed').' :
'; - - // We copy module only if it does not already exists - if (!file_exists(_PS_ROOT_DIR_.'/modules/'.$row)) - self::recurseCopy(_IMPORT_FOLDER_.'modules/'.$row, _PS_ROOT_DIR_.'/modules/'.$row); - - $obj = Module::getInstanceByName($row); - if (Validate::isLoadedObject($obj)) - Db::getInstance()->execute(' - UPDATE `'._DB_PREFIX_.'module` - SET `active`= 1 - WHERE `name` = \''.pSQL($row).'\''); - elseif (!$obj || !$obj->install()) - continue; - Db::getInstance()->execute('INSERT IGNORE INTO '._DB_PREFIX_.'module_shop (id_module, id_shop) VALUES('.(int)$obj->id.', '.$shopID.')'); - $msg .= '- '.pSQL($row).'
'; - Db::getInstance()->execute(' - DELETE FROM `'._DB_PREFIX_.'hook_module` - WHERE `id_module` = '.pSQL($obj->id).' AND id_shop = '.$shopID); - $count = -1; - while (isset($hookedModule[++$count])) - if ($hookedModule[$count] == $row) - { - Db::getInstance()->execute(' - INSERT INTO `'._DB_PREFIX_.'hook_module` (`id_module`, `id_shop`, `id_hook`, `position`) - VALUES ('.(int)$obj->id.', '.$shopID.', '.(int)Hook::getIdByName($hook[$count]).', '.(int)$position[$count].')'); - if ($exceptions[$count]) - foreach ($exceptions[$count] as $file_name) - Db::getInstance()->execute(' - INSERT INTO `'._DB_PREFIX_.'hook_module_exceptions` (`id_module`, `id_hook`, `file_name`) - VALUES ('.(int)$obj->id.', '.(int)Hook::getIdByName($hook[$count]).', "'.pSQL($file_name).'")'); - } - } - if (($val = (int)(Tools::getValue('nativeModules'))) != 1) + foreach ($this->selected_shops as $id_shop) { - $flag = 0; - // Disable native modules - if ($val == 2 && $this->to_disable && count($this->to_disable)) - foreach ($this->to_disable as $row) + if (isset($this->to_export) && $this->to_export) + foreach ($this->to_export as $row) { + if (in_array($row, $this->native_modules)) + continue; + if ($flag++ == 0) + $msg .= ''.$this->l('The following modules have been installed:').'
'; + + // We copy module only if it does not already exists + if (!file_exists(_PS_ROOT_DIR_.'/modules/'.$row)) + self::recurseCopy(_IMPORT_FOLDER_.'modules/'.$row, _PS_ROOT_DIR_.'/modules/'.$row); + $obj = Module::getInstanceByName($row); if (Validate::isLoadedObject($obj)) - { - if ($flag++ == 0) - $msg .= ''.$this->l('The following modules have been disabled').' :
'; - $msg .= '- '.pSQL($row).'
'; - Db::getInstance()->execute(' - DELETE FROM `'._DB_PREFIX_.'module_shop` - WHERE `id_module` = '.pSQL($obj->id).' AND id_shop = '.$shopID); - } - } - $flag = 0; - if ($this->to_enable && count($this->to_enable)) - foreach ($this->to_enable as $row) - { - $obj = Module::getInstanceByName($row); - if (Validate::isLoadedObject($obj)) - { Db::getInstance()->execute(' UPDATE `'._DB_PREFIX_.'module` SET `active`= 1 - WHERE `name` = \''.pSQL($row).'\''); - Db::getInstance()->execute('INSERT IGNORE INTO '._DB_PREFIX_.'module_shop (id_module, id_shop) VALUES('.(int)$obj->id.', '.$shopID.')'); - } - elseif (!is_object($obj) || !$obj->install()) - continue ; - if ($flag++ == 0) - $msg .= ''.$this->l('The following modules have been enabled').' :
'; - $msg .= '- '.pSQL($row).'
'; - Db::getInstance()->execute(' - DELETE FROM `'._DB_PREFIX_.'hook_module` - WHERE `id_module` = '.pSQL($obj->id).' AND id_shop = '.$shopID); + WHERE `name` = \''.pSQL($row).'\' + '); + else if (!$obj || !$obj->install()) + continue; + Db::getInstance()->execute('INSERT IGNORE INTO '._DB_PREFIX_.'module_shop (id_module, id_shop) VALUES('.(int)$obj->id.', '.(int)$id_shop.')'); + + $sql = 'DELETE FROM `'._DB_PREFIX_.'hook_module` WHERE `id_module` = '.pSQL($obj->id).' AND id_shop = '.(int)$id_shop; + if (Db::getInstance()->execute($sql)) + $msg .= '- '.pSQL($row).'
'; + $count = -1; - while (isset($hookedModule[++$count])) - if ($hookedModule[$count] == $row) + while (isset($hooked_module[++$count])) + if ($hooked_module[$count] == $row) { Db::getInstance()->execute(' INSERT INTO `'._DB_PREFIX_.'hook_module` (`id_module`, `id_shop`, `id_hook`, `position`) - VALUES ('.(int)$obj->id.', '.$shopID.', '.(int)Hook::getIdByName($hook[$count]).', '.(int)$position[$count].')'); - foreach ($exceptions[$count] as $filename) - if (!empty($filename)) + VALUES ('.(int)$obj->id.', '.(int)$id_shop.', '.(int)Hook::getIdByName($hook[$count]).', '.(int)$position[$count].') + '); + if ($exceptions[$count]) + foreach ($exceptions[$count] as $file_name) Db::getInstance()->execute(' - INSERT INTO `'._DB_PREFIX_.'hook_module_exceptions` (`id_module`, id_shop, `id_hook`, `file_name`) - VALUES ('.(int)$obj->id.', '.$shopID.', '.(int)Hook::getIdByName($hook[$count]).', "'.pSQL($filename).'")'); + INSERT INTO `'._DB_PREFIX_.'hook_module_exceptions` (`id_module`, `id_hook`, `file_name`) + VALUES ('.(int)$obj->id.', '.(int)Hook::getIdByName($hook[$count]).', "'.pSQL($file_name).'") + '); } } + if (($val = (int)Tools::getValue('nativeModules')) != 1) + { + $flag = 0; + // Disable native modules + if ($val == 2 && $this->to_disable && count($this->to_disable)) + foreach ($this->to_disable as $row) + { + $obj = Module::getInstanceByName($row); + if (Validate::isLoadedObject($obj)) + { + if ($flag++ == 0) + $msg .= ''.$this->l('The following modules have been unhook:').' :
'; + + // Delete all native module which are in the front office feature category and in selected shops + $sql = 'DELETE FROM `'._DB_PREFIX_.'module_shop` WHERE `id_module` = '.pSQL($obj->id).' AND `id_shop` = '.(int)$id_shop; + if (Db::getInstance()->execute($sql)) + $msg .= '- '.pSQL($row).'
'; + } + } + + $flag = 0; + if ($this->to_enable && count($this->to_enable)) + foreach ($this->to_enable as $row) + { + $obj = Module::getInstanceByName($row); + if (Validate::isLoadedObject($obj)) + { + Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'module` + SET `active`= 1 + WHERE `name` = \''.pSQL($row).'\'' + ); + Db::getInstance()->execute(' + INSERT IGNORE INTO '._DB_PREFIX_.'module_shop (id_module, id_shop) + VALUES('.(int)$obj->id.', '.(int)$id_shop.') + '); + } + else if (!is_object($obj) || !$obj->install()) + continue; + + if ($flag++ == 0) + $msg .= ''.$this->l('The following modules have been enabled:').'
'; + + $sql = 'DELETE FROM `'._DB_PREFIX_.'hook_module` WHERE `id_module` = '.pSQL($obj->id).' AND id_shop = '.(int)$id_shop; + if (Db::getInstance()->execute($sql)) + $msg .= '- '.pSQL($row).'
'; + + $count = -1; + while (isset($hooked_module[++$count])) + if ($hooked_module[$count] == $row) + { + Db::getInstance()->execute(' + INSERT INTO `'._DB_PREFIX_.'hook_module` (`id_module`, `id_shop`, `id_hook`, `position`) + VALUES ('.(int)$obj->id.', '.(int)$id_shop.', '.(int)Hook::getIdByName($hook[$count]).', '.(int)$position[$count].') + '); + + foreach ($exceptions[$count] as $filename) + if (!empty($filename)) + Db::getInstance()->execute(' + INSERT INTO `'._DB_PREFIX_.'hook_module_exceptions` (`id_module`, id_shop, `id_hook`, `file_name`) + VALUES ('.(int)$obj->id.', '.(int)$id_shop.', '.(int)Hook::getIdByName($hook[$count]).', "'.pSQL($filename).'") + '); + } + } + } + + $theme = Db::getInstance()->getRow('SELECT * FROM `'._DB_PREFIX_.'theme` WHERE `name` LIKE \''.(string)$this->xml['name'].'\''); + $shop = new Shop((int)$id_shop); + $shop->id_theme = (int)$theme['id_theme']; + $shop->update(); } // note : theme was previously created at this point. @@ -568,11 +611,15 @@ class ThemeInstallator extends Module $msg .= $this->l('Images have been correctly updated in database'); else { - $errors = ''.$this->l('Warning: Copy/Paste your errors if you want to manually set the image type (in the tab Preferences > Images):').'
'; + $errors = ''. + $this->l('Warning: Copy/Paste your errors if you want to manually set the image type (in the tab Preferences > Images):'). + '
'; $errors .= $this->l('Some kind of image could not be added because they exists. Here\'s the list:'); $errors .= ''; } @@ -589,17 +636,17 @@ class ThemeInstallator extends Module } - private function _displayForm3() + private function displayForm3() { $res = true; $theme_directory = Tools::getValue('theme_directory'); $xml = simplexml_load_file(_IMPORT_FOLDER_.XMLFILENAME); $this->xml = $xml; - if ($this->selectedVariations && count($this->selectedVariations) > 0) + if ($this->selected_variations && count($this->selected_variations) > 0) { $ok = array(); - foreach ($this->selectedVariations as $variation) + foreach ($this->selected_variations as $variation) { if ($variation == 'prestashop') continue; @@ -615,9 +662,17 @@ class ThemeInstallator extends Module $res &= self::recurseCopy(_IMPORT_FOLDER_.'themes/'.$variation, $target_dir); $new_theme = new Theme(); $new_theme->name = (string)$this->xml['name']; - $new_theme->directory = $theme_directory; - $res &= $new_theme->add(); + $name_exist = true; + + // Check name theme + $themes = $new_theme->getThemes(); + foreach ($themes as $row) + if ($row->name == $new_theme->name) + $name_exist &= false; + + if ($name_exist) + $res &= $new_theme->add(); if ($res) $ok[] = $variation; @@ -634,7 +689,7 @@ class ThemeInstallator extends Module } self::getModules(); if (file_exists(_IMPORT_FOLDER_.'doc') && count($xml->docs->doc) != 0) - self::_loadDocForm(); + self::loadDocForm(); $this->_html .= '
'; if ($this->to_install && count($this->to_install) > 0) { @@ -643,7 +698,8 @@ class ThemeInstallator extends Module if (file_exists(_IMPORT_FOLDER_.'modules/'.$row)) { $module_already_exists = file_exists(_PS_MODULE_DIR_.$row); - $var .= '