* @copyright 2016-2017 GFI Informatique, 2016-2017 TNT * @license https://opensource.org/licenses/MIT MIT License */ require_once _PS_MODULE_DIR_.'tntofficiel/tntofficiel.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Logger.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Debug.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Carrier.php'; class TNTOfficiel_Install { /** @var array */ public static $arrHookList = array( // Header 'displayBackOfficeHeader', 'actionAdminControllerSetMedia', 'displayHeader', // Front-Office display carrier. 'displayBeforeCarrier', 'displayCarrierList', // Front-Office display Order created. //'displayOrderConfirmation', // Front-Office order detail. 'displayOrderDetail', // Back-Office order detail. 'displayAdminOrder', // Carrier updated. 'actionCarrierUpdate', // Order status before changed. 'actionOrderStatusUpdate', 'actionOrderStatusPostUpdate', // Order created. 'actionValidateOrder', // //'actionDeliveryPriceByWeight', //'actionDeliveryPriceByPrice', // //'displayPayment', //'displayPaymentReturn', //'actionPaymentConfirmation', //'actionCartSave', //'actionCarrierProcess', // //'actionObjectAddBefore', //'actionObjectAddAfter', //'actionObjectUpdateBefore', //'actionObjectUpdateAfter', //'actionObjectDeleteBefore', //'actionObjectDeleteAfter', //'actionObjectOrderAddBefore', //'actionObjectOrderAddAfter', //'actionObjectOrderUpdateBefore', //'actionObjectOrderUpdateAfter', //'actionObjectOrderDeleteBefore', //'actionObjectOrderDeleteAfter', ); /** @var array */ public static $arrConfigUpdateDeleteList = array( //'TNT_CARRIER_ID' Carrier ID is created on installCarrier, then preserved. //'TNT_GOOGLE_MAP_API_KEY' Google Map API Key is created on config form submit, then preserved. // Authentication information. 'TNT_CARRIER_USERNAME' => '', 'TNT_CARRIER_ACCOUNT' => '', 'TNT_CARRIER_PASSWORD' => '', // Is Authentication information validated. 'TNT_CARRIER_ACTIVATED' => false, // Show pickup number in AdminOrdersController. 'TNT_CARRIER_PICKUP_NUMBER_SHOW' => '', // Max weight (kg) per parcel. 'TNT_CARRIER_MAX_PACKAGE_B2B' => '30.0', 'TNT_CARRIER_MAX_PACKAGE_B2C' => '20.0', // Comma separated list of item cart attributes. 'TNT_CARRIER_ASSOCIATIONS' => '', // MiddleWare JSON-RPC URL. 'TNT_CARRIER_MIDDLEWARE_URL' => 'https://solutions-ecommerce.tnt.fr/api/handler', // MiddleWare IFrame URL. 'TNT_CARRIER_MIDDLEWARE_SHORT_URL' => 'https://solutions-ecommerce.tnt.fr/login', 'TNT_CARRIER_SOAP_WSDL' => 'https://www.tnt.fr/service/?wsdl', // . for ./libraries/TNTOfficiel_Address.php // DB fields used as default values for delivery address extra data. 'TNT_CARRIER_ADDRESS_EMAIL' => 'customer.email', 'TNT_CARRIER_ADDRESS_PHONE' => 'address.phone_mobile', 'TNT_CARRIER_ADDRESS_BUILDING' => '', 'TNT_CARRIER_ADDRESS_INTERCOM' => '', 'TNT_CARRIER_ADDRESS_FLOOR' => '' ); /** @var array */ public static $arrTemplateOverrideList = array( array( 'fileName' => 'view.tpl', 'directorySrc' => 'views/templates/admin/override/controllers/admin/templates/orders/helpers/view/', 'directoryDst' => 'controllers/admin/templates/orders/helpers/view/', ), ); /** * Prevent Construct. */ final private function __construct() { trigger_error(sprintf('%s() %s is static.', __FUNCTION__, get_class($this)), E_USER_ERROR); } /** * Create a new directory with default index.php file. * * @param $arrArgDirectoryList an array of directories. */ public static function makeModuleDir($arrArgDirectoryList) { $strIndexFileContent = <</cache/'). Media::clearCache(); // Clear class index cache for PrestaShopAutoload ('/cache/class_index.php'). Tools::generateIndex(); /* // Check cache '/cache/class_index.php' $objPSAutoload = PrestaShopAutoload::getInstance(); if ( !$objPSAutoload->_include_override_path || //!Configuration::get('PS_DISABLE_OVERRIDES') || $objPSAutoload->getClassPath('AdminOrdersController') !== 'override/controllers/admin/AdminOrdersController.php' || $objPSAutoload->getClassPath('Order') !== 'override/classes/order/Order.php' || $objPSAutoload->getClassPath('OrderHistory') !== 'override/classes/order/OrderHistory.php' ) { // Warning !! } */ } /** * Update settings fields. * * @return bool */ public static function updateSettings() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $boolUpdated = true; foreach (TNTOfficiel_Install::$arrConfigUpdateDeleteList as $strCfgName => $mxdValue) { $boolUpdated = $boolUpdated && Configuration::updateValue($strCfgName, $mxdValue); } return $boolUpdated; } /** * Delete settings fields. * * @return bool */ public static function deleteSettings() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $boolDeleted = true; foreach (TNTOfficiel_Install::$arrConfigUpdateDeleteList as $strCfgName => $mxdValue) { $boolDeleted = $boolDeleted && Configuration::deleteByName($strCfgName); } return $boolDeleted; } /** * Creates the admin Tab. * * @param $arrArgTabNameLang Module name displayed. * * @return bool */ public static function createTab($arrArgTabNameLang) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); // Creates the parent tab $parentTab = new Tab(); $parentTab->class_name = 'AdminTNTOfficiel'; $parentTab->name = $arrArgTabNameLang; $parentTab->module = TNTOfficiel::MODULE_NAME; $parentTab->id_parent = 0; // TODO : AdminParentShipping as parent ? //$parentTab->id_parent = Tab::getIdFromClassName('AdminParentShipping'); $boolResult = (bool)($parentTab->add()); /* if (version_compare(_PS_VERSION_, '1.6', '<')) { $childrenTab = new Tab(); $childrenTab->class_name = 'AdminTNTOfficiel'; $childrenTab->name = $arrTabNameLang; $childrenTab->module = $strModuleName; $childrenTab->id_parent = Tab::getIdFromClassName('AdminTNTOfficiel'); $boolResult15 = (bool)($childrenTab->add()); return $boolResult && $boolResult15; } */ return $boolResult; } /** * Delete the admin Tab. * * @return bool */ public static function deleteTab() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $objTabsPSCollection = Tab::getCollectionFromModule(TNTOfficiel::MODULE_NAME)->getAll(); foreach ($objTabsPSCollection as $tab) { if (!$tab->delete()) { return false; } } return true; } /** * Update table. * * @return bool */ public static function upgradeTables_1_2_20() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $strTablePrefix = _DB_PREFIX_; // Test if table tnt_extra_address_data exist. $strSQLTableExtraExist = <<execute($strSQLTableLogDropTable)) { return false; } // Update table tnt_extra_address_data if exist. $arrDBResult = $objDB->executeS($strSQLTableExtraExist); if(count($arrDBResult) === 1) { if (!$objDB->execute($strSQLTableExtraAddColumns) || !$objDB->execute($strSQLTableExtraChangeColumns) || !$objDB->execute($strSQLTableExtraRenameTable) ) { return false; } } // Update tnt_order if exist. $arrDBResult = $objDB->executeS($strSQLTableOrderExist); if(count($arrDBResult) === 1) { if (!$objDB->execute($strSQLTableOrderChangeColumns) || !$objDB->execute($strSQLTableOrderRenameTable) ) { return false; } } // Update tnt_parcel if exist. $arrDBResult = $objDB->executeS($strSQLTableParcelExist); if(count($arrDBResult) === 1) { if (!$objDB->execute($strSQLTableParcelChangeColumns) || !$objDB->execute($strSQLTableParcelRenameTable) ) { return false; } } return true; } /** * Creates the tables needed by the module. * * @return bool */ public static function createTables() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); // Update if required. TNTOfficiel_Install::upgradeTables_1_2_20(); $strTablePrefix = _DB_PREFIX_; $strTableEngine = _MYSQL_ENGINE_; // Create tntofficiel_cart table. $strSQLCreateCart = <<execute($strSQLCreateCart) || !$objDB->execute($strSQLCreateOrder) || !$objDB->execute($strSQLCreateParcels) ) { return false; } return true; } /** * Create or Restore an existing TNT carrier. * * @return bool */ public static function installCarrier() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); // Try to undelete previously deleted carrier. $boolResult = TNTOfficiel_Carrier::undeleteGlobalCarrier(); // If not succeed. if (!$boolResult) { // Create a new one. $boolResult = TNTOfficiel_Carrier::createGlobalCarrier(); } return $boolResult; } /** * Add a template override in the override directory. * * @param $strArgModuleDir Module absolute Path. * @return array */ public static function overrideTemplates($strArgModuleDir) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $arrErrors = array(); foreach (TNTOfficiel_Install::$arrTemplateOverrideList as $arrTemplateOverride) { $strPathTemplateSrc = $strArgModuleDir.$arrTemplateOverride['directorySrc']; $strFileTemplateSrc = $strPathTemplateSrc.$arrTemplateOverride['fileName']; $strPathTemplateDst = _PS_OVERRIDE_DIR_.$arrTemplateOverride['directoryDst']; $strFileTemplateDst = $strPathTemplateDst.$arrTemplateOverride['fileName']; try { // Create directory if unexist. if (!is_dir($strPathTemplateDst)) { mkdir($strPathTemplateDst, 0777, true); } // Copy new template file. if (!copy($strFileTemplateSrc, $strFileTemplateDst)) { $arrErrors[] = sprintf(Tools::displayError('Impossible d\'installer la surcharge "%s"'), $arrTemplateOverride['fileName']); } } catch (Exception $objException) { $objFileLogger = new FileLogger(); $objFileLogger->setFilename(_PS_ROOT_DIR_.'/log/'.date('Ymd').'_tnt_exception.log'); $objFileLogger->logError($objException->getMessage()); $arrErrors[] = sprintf(Tools::displayError('Impossible d\'installer la surcharge "%s"'), $arrTemplateOverride['fileName']); } } return $arrErrors; } /** * Delete a template override in the override directory. * * @return array */ public static function unOverrideTemplates() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $arrErrors = array(); // Unoverride templates. foreach (TNTOfficiel_Install::$arrTemplateOverrideList as $arrTemplateOverride) { $strPathTemplateDst = _PS_OVERRIDE_DIR_.$arrTemplateOverride['directoryDst']; $strFileTemplateDst = $strPathTemplateDst.$arrTemplateOverride['fileName']; try { // Create directory if not found. if (!is_dir($strPathTemplateDst)) { mkdir($strPathTemplateDst, 0777, true); } // Delete previous template file if exist. if (file_exists($strFileTemplateDst)) { if(!unlink($strFileTemplateDst)) { $arrErrors[] = sprintf(Tools::displayError('Impossible de supprimer la surcharge "%s"'), $arrTemplateOverride['fileName']); } } } catch (Exception $objException) { $objFileLogger = new FileLogger(); $objFileLogger->setFilename(_PS_ROOT_DIR_.'/log/'.date('Ymd').'_tnt_exception.log'); $objFileLogger->logError($objException->getMessage()); $arrErrors[] = sprintf(Tools::displayError('Impossible de supprimer la surcharge "%s"'), $arrTemplateOverride['fileName']); } } return $arrErrors; } }