Ejemplo n.º 1
0
 public function saveData($aData, $aModuleHash = array(), $clearCache = true, $hideEvents = false)
 {
     if (!$aData) {
         return false;
     }
     if (!$aModuleHash) {
         $aModuleHash = $this->_getModuleHash();
     }
     $sModuleEtcDir = $this->_getEtcDir();
     // dispatching pre-disable event
     $aStatusHash = $this->getModulesStatusHash();
     if ($this->_isCorrection) {
         // For correction $aData is an array of modules to be corrected, not a list of all modules with new statuses.
         // To prevent status changes of all other modules we are re-creating $aData from $aStatusHash.
         // Then in $sStatusHash we change current statuses of modules that require correction to an opposite one to simulate
         // normal process of enabling/disabling of modules.
         $newData = array();
         foreach ($aStatusHash as $module => $oldStatus) {
             $newData[$module] = $oldStatus;
             $aStatusHash[$module] = isset($aData[$module]) ? !$oldStatus : $oldStatus;
         }
         $aData = $newData;
     }
     if ($aModuleHash) {
         foreach ($aModuleHash as $sFile => $sFullPath) {
             if (!file_exists($sFullPath)) {
                 continue;
             }
             $oModuleBaseConfigCheck = simplexml_load_file($sFullPath);
             $this->checkModulesCompatibility($aData, $oModuleBaseConfigCheck, $aModuleHash);
         }
     }
     $eventArg = array('aitsys' => $this, 'data' => $aData, 'module_hash' => $aModuleHash, 'status_hash' => $aStatusHash);
     if (!$hideEvents) {
         $this->tool()->event('aitoc_module_save_data_before', $eventArg);
     }
     foreach ($aStatusHash as $sModule => $bIsActive) {
         if (!isset($aData[$sModule]) or !$aData[$sModule]) {
             $aEventParams = array('object' => $this, 'aitocmodulename' => $sModule);
             if (!$hideEvents) {
                 Mage::dispatchEvent('aitoc_module_disable_before', $aEventParams);
             }
         }
     }
     // checking if we got any error from events
     $aErrors = $this->_getErrorList();
     if (!empty($aErrors)) {
         return $aErrors;
     }
     if ($aModuleHash) {
         foreach ($aModuleHash as $sFile => $sFullPath) {
             if (!file_exists($sFullPath)) {
                 continue;
             }
             $sModuleName = substr($sFile, 0, strpos($sFile, '.'));
             if (!$hideEvents) {
                 $this->tool()->event('aitoc_module_modify_before', array('aitsys' => $this, 'key' => $sModuleName));
             }
             $sModuleDir = $this->_getModuleDir($sModuleName);
             // get main module config
             $oModuleMainConfig = $this->_getModuleConfig($sModuleDir);
             // set module status
             if (isset($aData[$sModuleName]) and $aData[$sModuleName]) {
                 $sModuleActive = 'true';
                 //$this->_addModuleClassRewrite($oModuleMainConfig, $sModuleName); //deprecated since 2.19.0
             } else {
                 $sModuleActive = 'false';
             }
             // get base module config
             $oModuleCustomConfig = $this->_getModuleConfig($sModuleDir, true);
             $this->_setFileMergeData($oModuleCustomConfig, $sModuleDir);
             $oModuleBaseConfig = simplexml_load_file($sFullPath);
             $oModuleBaseConfig->modules->{$sModuleName}->active = $sModuleActive;
             if ($iPriority = $oModuleBaseConfig->modules->{$sModuleName}->priority) {
                 $this->_aModulePriority[$sModuleName] = (int) $iPriority;
             } else {
                 $this->_aModulePriority[$sModuleName] = 0;
             }
             $sFileContent = $oModuleBaseConfig->asXML();
             if ($this->_isCorrection || $this->_checkFileSaveWithContent($sFullPath, $sFileContent)) {
                 // check if module's status was changed
                 // check if module has license helper
                 // launch module's specific install/uninstall methods from license helper
                 if (array_key_exists($sModuleName, $aData) && array_key_exists($sModuleName, $aStatusHash) && $aData[$sModuleName] != $aStatusHash[$sModuleName]) {
                     if ($sModuleActive == 'false') {
                         $this->tool()->getLicenseHelper($sModuleName)->uninstallBefore();
                     } else {
                         $this->tool()->getLicenseHelper($sModuleName)->installBefore();
                     }
                 }
                 // check if module has resource setup files
                 // also if it has version in `core_resource`
                 // run 'activate' or 'uninstall' scripts
                 if ($oModuleMainConfig and isset($oModuleMainConfig->global->resources)) {
                     $resourceName = '';
                     foreach ($oModuleMainConfig->global->resources->children() as $key => $object) {
                         if ($object->setup) {
                             $resourceName = $key;
                             break;
                         }
                     }
                     if ($resourceName) {
                         // check if module has version in `core_resource`, so it was previously enabled
                         $dbVersion = Mage::getResourceModel('core/resource')->getDBVersion($resourceName);
                         if ($dbVersion && array_key_exists($sModuleName, $aData) && array_key_exists($sModuleName, $aStatusHash) && $aData[$sModuleName] != $aStatusHash[$sModuleName]) {
                             if ($sModuleActive == 'false') {
                                 $aitsysSetup = new Aitoc_Aitsys_Model_Mysql4_Setup('core_setup');
                                 $aitsysSetup->applyAitocModuleUninstall($sModuleName);
                             } else {
                                 $aitsysSetup = new Aitoc_Aitsys_Model_Mysql4_Setup('core_setup');
                                 $aitsysSetup->applyAitocModuleActivate($sModuleName);
                             }
                         }
                     }
                 }
                 // save module status in the database for future correction checks
                 Aitoc_Aitsys_Model_Module_Status::updateStatus($sModuleName, $sModuleActive != 'false');
             }
         }
         $this->_clearFileMergeData($aData, $aModuleHash, $aStatusHash);
         // set new php class inheritance
         /* deprecated since 2.19.0
            if ($this->_aModuleClassRewrite)
            {
                $aModuleOrder = $this->_getModuleOrder();
               
                foreach ($this->_aModuleClassRewrite as $sKey => $aData)
                {
                    $aReplaceHash = $this->_setRewriteClass($sKey, $aData, $aModuleOrder);
                    
                    if ($aReplaceHash)
                    {
                        foreach ($aReplaceHash as $sModuleName => $aReplace)
                        {
                            if ($aReplace['extends']) // must change inheritance
                            {
                                // modify class inheritance
                                
                                $sExtends = ' extends ';
                                
                                $aReplaceData = array
                                (
                                    'search'    => array($sExtends . $aReplace['original']),
                                    'replace'   => array($sExtends . $aReplace['extends']),
                                );   
                            }
                            else 
                            {
                                $aReplaceData = array();
                            }
                            
                            $this->_checkFileSaveWithBackup($aReplace['path'], 'php', $aReplaceData);
                            
                            $this->_aConfigReplace[$sModuleName][$aReplace['module']] = $aReplace['config'];         
                        }
                    }
                }
            }
            */
         // checking write permissions to var
         $this->_checkFileWritePermissions(Mage::getBaseDir('var'));
         // save updated config modules files
         /* deprecated since 2.19.0
                    if ($this->_aConfigReplace)
                    {
                        foreach ($this->_aConfigReplace as $sModuleName => $aData)
                        {
                            $aReplaceData = array();
                            
                            foreach ($aData as $sKey => $sVal)
                            {
                                $aReplaceData['search'][]  = $sKey;
                                $aReplaceData['replace'][] = $sVal;
                            }
                            
                            $sModuleDir = $this->_getModuleDir($sModuleName);
            
                            $sConfigFilePath = $sModuleDir . '/etc/config';
                            
                            $this->_checkFileSaveWithBackup($sConfigFilePath, 'xml', $aReplaceData);
                        }
                    }
                    */
         if (!$this->_aErrorList) {
             if ($this->_aFileMerge) {
                 $oPatch = new Aitoc_Aitsys_Model_Aitpatch();
                 $oPatch->setPatchFiles($this->_aFileMerge);
                 $aErrorList = $oPatch->applyPatchDryrun();
                 if ($aErrorList) {
                     foreach ($aErrorList as $aError) {
                         $this->_addError($aError['file'], $aError['type']);
                     }
                 } else {
                     $oPatch->applyPatch();
                 }
             }
             if (!$this->_aErrorList) {
                 $this->_saveAllFileContent();
                 $this->_deleteFiles();
             }
         }
     }
     if ($clearCache) {
         $this->tool()->clearCache();
     }
     if (!$hideEvents) {
         $this->tool()->event('aitoc_module_save_data_after', $eventArg);
     }
     return $this->_getErrorList();
 }
Ejemplo n.º 2
0
 /**
  * Apply module sql file of appropriate type
  *
  * @param string $processType self::TYPE_AITOC_DB_STATE_*
  * @param string $moduleName like [Aitoc|AdjustWare]_[AitModuleName]
  * 
  * @return bool 
  */
 protected function _processStateSql($processType, $moduleName)
 {
     $localDir = Aitoc_Aitsys_Abstract_Service::get()->filesystem()->getLocalDir();
     $moduleDir = $localDir . str_replace('_', '/', $moduleName);
     // Attempt to locate and load module's main config file or its .data version
     $configFile = $moduleDir . DS . 'etc' . DS . 'config.data.xml';
     if (file_exists($configFile)) {
         $config = simplexml_load_file($configFile);
     } else {
         $configFile = $moduleDir . DS . 'etc' . DS . 'config.xml';
         if (file_exists($configFile)) {
             $config = simplexml_load_file($configFile);
         }
     }
     // Module config file not found
     if (!isset($config)) {
         return false;
     }
     if (isset($config->global) && isset($config->global->resources)) {
         foreach ($config->global->resources->children() as $key => $object) {
             if ($object->setup) {
                 $resourceName = $key;
                 break;
             }
         }
     }
     // There's no resource setup entities in the config file
     if (!isset($resourceName)) {
         return false;
     }
     $sqlFilesDir = $moduleDir . DS . 'sql' . DS . $resourceName;
     // Resource setup directory is empty
     if (!is_dir($sqlFilesDir) || !is_readable($sqlFilesDir)) {
         return false;
     }
     // Read resource files
     $arrAvailableFiles = array();
     $sqlDir = dir($sqlFilesDir);
     while (false !== ($sqlFile = $sqlDir->read())) {
         $matches = array();
         if (preg_match('#^mysql4-' . $processType . '-(.*)\\.(sql|php)$#i', $sqlFile, $matches)) {
             $arrAvailableFiles[$matches[1]] = $sqlFile;
         }
     }
     $sqlDir->close();
     // There are no appropriate state sql files in the resource directory
     if (empty($arrAvailableFiles)) {
         return false;
     }
     $dbVersion = Mage::getResourceModel('core/resource')->getDBVersion($resourceName);
     foreach ($arrAvailableFiles as $version => $resourceFile) {
         // Stop processing if state script is for the higher version of the module
         if (version_compare($version, $dbVersion) > 0) {
             break;
         }
         $sqlFile = $sqlFilesDir . DS . $resourceFile;
         $fileType = pathinfo($resourceFile, PATHINFO_EXTENSION);
         // Execute SQL
         if ($this->_conn) {
             if (method_exists($this->_conn, 'disallowDdlCache')) {
                 $this->_conn->disallowDdlCache();
             }
             try {
                 switch ($fileType) {
                     case 'sql':
                         $sql = file_get_contents($sqlFile);
                         if ($sql != '') {
                             $result = $this->run($sql);
                         } else {
                             $result = true;
                         }
                         break;
                     case 'php':
                         $conn = $this->_conn;
                         $result = (include $sqlFile);
                         break;
                     default:
                         $result = false;
                 }
             } catch (Exception $e) {
                 echo "<pre>" . print_r($e, 1) . "</pre>";
                 throw Mage::exception('Mage_Core', Mage::helper('core')->__('Error in file: "%s" - %s', $sqlFile, $e->getMessage()));
             }
             if (method_exists($this->_conn, 'allowDdlCache')) {
                 $this->_conn->allowDdlCache();
             }
         }
     }
     self::$_hadUpdates = true;
     return true;
 }
Ejemplo n.º 3
0
 public function applyAitocModuleActivate($moduleName)
 {
     $localDir = Aitoc_Aitsys_Abstract_Service::get()->filesystem()->getLocalDir();
     $moduleDir = $localDir . str_replace('_', '/', $moduleName);
     $configFile = $moduleDir . DS . 'etc' . DS . 'config.data.xml';
     if (file_exists($configFile)) {
         $config = simplexml_load_file($configFile);
     } else {
         $configFile = $moduleDir . DS . 'etc' . DS . 'config.xml';
         if (file_exists($configFile)) {
             $config = simplexml_load_file($configFile);
         }
     }
     if (!isset($config)) {
         return false;
     }
     //list($curVersion) = (array)$config->modules->$moduleName->version;
     if (isset($config->global) && isset($config->global->resources)) {
         foreach ($config->global->resources->children() as $key => $object) {
             if ($object->setup) {
                 $resourceName = $key;
                 break;
             }
         }
     }
     if (!isset($resourceName)) {
         return false;
     }
     $dbVersion = Mage::getResourceModel('core/resource')->getDBVersion($resourceName);
     $sqlFilesDir = $moduleDir . DS . 'sql' . DS . $resourceName;
     if (!is_dir($sqlFilesDir) || !is_readable($sqlFilesDir)) {
         return false;
     }
     // Read resource files
     $arrAvailableFiles = array();
     $sqlDir = dir($sqlFilesDir);
     while (false !== ($sqlFile = $sqlDir->read())) {
         $matches = array();
         if (preg_match('#^mysql4-activate-(.*)\\.(sql|php)$#i', $sqlFile, $matches)) {
             $arrAvailableFiles[$matches[1]] = $sqlFile;
         }
     }
     if (empty($arrAvailableFiles)) {
         return false;
     }
     foreach ($arrAvailableFiles as $version => $resourceFile) {
         if (version_compare($version, $dbVersion) > 0) {
             break;
         }
         $sqlFile = $sqlFilesDir . DS . $resourceFile;
         $fileType = pathinfo($resourceFile, PATHINFO_EXTENSION);
         // Execute SQL
         if ($this->_conn) {
             if (method_exists($this->_conn, 'disallowDdlCache')) {
                 $this->_conn->disallowDdlCache();
             }
             try {
                 switch ($fileType) {
                     case 'sql':
                         $sql = file_get_contents($sqlFile);
                         if ($sql != '') {
                             $result = $this->run($sql);
                         } else {
                             $result = true;
                         }
                         break;
                     case 'php':
                         $conn = $this->_conn;
                         $result = (include $sqlFile);
                         break;
                     default:
                         $result = false;
                 }
             } catch (Exception $e) {
                 echo "<pre>" . print_r($e, 1) . "</pre>";
                 throw Mage::exception('Mage_Core', Mage::helper('core')->__('Error in file: "%s" - %s', $sqlFile, $e->getMessage()));
             }
             if (method_exists($this->_conn, 'allowDdlCache')) {
                 $this->_conn->allowDdlCache();
             }
         }
     }
     self::$_hadUpdates = true;
     return true;
 }