Minahito
minah****@users*****
2006年 11月 14日 (火) 20:53:23 JST
Index: xoops2jp/html/modules/legacy/admin/class/ModuleInstallUtils.class.php diff -u xoops2jp/html/modules/legacy/admin/class/ModuleInstallUtils.class.php:1.1.2.10 xoops2jp/html/modules/legacy/admin/class/ModuleInstallUtils.class.php:1.1.2.11 --- xoops2jp/html/modules/legacy/admin/class/ModuleInstallUtils.class.php:1.1.2.10 Mon Nov 13 20:11:35 2006 +++ xoops2jp/html/modules/legacy/admin/class/ModuleInstallUtils.class.php Tue Nov 14 20:53:23 2006 @@ -685,6 +685,38 @@ } } + function installPreferenceByInfo(&$info, &$module, &$log) + { + $handler =& xoops_gethandler('config'); + $config =& $handler->createConfig(); + $config->set('conf_modid', $module->get('mid')); + $config->set('conf_catid', 0); + $config->set('conf_name', $info->mName); + $config->set('conf_title', $info->mTitle); + $config->set('conf_desc', $info->mDescription); + $config->set('conf_formtype', $info->mFormType); + $config->set('conf_valuetype', $info->mValueType); + $config->setConfValueForInput($info->mDefault); + $config->set('conf_order', $info->mOrder); + + if (count($info->mOption->mOptions) > 0) { + foreach (array_keys($info->mOption->mOptions) as $idx) { + $option =& $handler->createOption(); + $option->set('confop_name', $info->mOption->mOptions[$idx]->mName); + $option->set('confop_value', $info->mOption->mOptions[$idx]->mValue); + $config->setConfOptions($option); + unset($option); + } + } + + if ($handler->insertConfig($config)) { + $log->addReport(XCube_Utils::formatMessage(_AD_LEGACY_MESSAGE_INSERT_CONFIG, $config->get('conf_name'))); + } + else { + $log->addError(XCube_Utils::formatMessage(_AD_LEGACY_ERROR_COULD_NOT_INSERT_CONFIG, $config->get('conf_name'))); + } + } + /** * Get & build config items from Manifesto by specific module object. */ @@ -796,7 +828,7 @@ } } - function smartUpdateAllOfBlock(&$module, &$log) + function smartUpdateAllOfBlocks(&$module, &$log) { $dirname = $module->get('dirname'); @@ -829,6 +861,85 @@ } } + function smartUpdateAllOfPreferences(&$module, &$log) + { + $dirname = $module->get('dirname'); + + $fileReader =& new Legacy_ModinfoX2FileReader($dirname); + $latestPreferences =& $fileReader->loadPreferenceInformations(); + + $dbReader =& new Legacy_ModinfoX2DBReader($dirname); + $currentPreferences =& $dbReader->loadPreferenceInformations(); + + $currentPreferences->update($latestPreferences); + + // + // Preferences + // + foreach (array_keys($currentPreferences->mPreferences) as $idx) { + switch ($currentPreferences->mPreferences[$idx]->mStatus) { + case LEGACY_BLOCKINFOMATION_STATUS_LOADED: + break; + + case LEGACY_BLOCKINFOMATION_STATUS_UPDATED: + Legacy_ModuleInstallUtils::updatePreferenceByInfo($currentPreferences->mPreferences[$idx], $module, $log); + break; + + case LEGACY_BLOCKINFOMATION_STATUS_NEW: + Legacy_ModuleInstallUtils::installPreferenceByInfo($currentPreferences->mPreferences[$idx], $module, $log); + break; + + case LEGACY_BLOCKINFOMATION_STATUS_DELETED: + Legacy_ModuleInstallUtils::uninstallPreferenceByOrder($currentPreferences->mPreferences[$idx]->mOrder, $module, $log); + break; + } + } + + // + // Comments + // + foreach (array_keys($currentPreferences->mComments) as $idx) { + switch ($currentPreferences->mComments[$idx]->mStatus) { + case LEGACY_BLOCKINFOMATION_STATUS_LOADED: + break; + + case LEGACY_BLOCKINFOMATION_STATUS_UPDATED: + Legacy_ModuleInstallUtils::updateCommentByInfo($currentPreferences->mComments[$idx], $module, $log); + break; + + case LEGACY_BLOCKINFOMATION_STATUS_NEW: + Legacy_ModuleInstallUtils::installPreferenceByInfo($currentPreferences->mComments[$idx], $module, $log); + break; + + case LEGACY_BLOCKINFOMATION_STATUS_DELETED: + Legacy_ModuleInstallUtils::uninstallPreferenceByOrder($currentPreferences->mComments[$idx]->mOrder, $module, $log); + break; + } + } + + // + // Notifications + // + foreach (array_keys($currentPreferences->mNotifications) as $idx) { + switch ($currentPreferences->mNotifications[$idx]->mStatus) { + case LEGACY_BLOCKINFOMATION_STATUS_LOADED: + break; + + case LEGACY_BLOCKINFOMATION_STATUS_UPDATED: + Legacy_ModuleInstallUtils::updateCommentByInfo($currentPreferences->mNotifications[$idx], $module, $log); + break; + + case LEGACY_BLOCKINFOMATION_STATUS_NEW: + Legacy_ModuleInstallUtils::installPreferenceByInfo($currentPreferences->mNotifications[$idx], $module, $log); + break; + + case LEGACY_BLOCKINFOMATION_STATUS_DELETED: + Legacy_ModuleInstallUtils::uninstallPreferenceByOrder($currentPreferences->mNotifications[$idx]->mOrder, $module, $log); + break; + } + } + } + function updateBlockTemplateByInfo(&$info, &$module, &$log) { $handler =& xoops_getmodulehandler('newblocks', 'legacy'); @@ -873,6 +984,83 @@ } } + function updatePreferenceByInfo(&$info, &$module, &$log) + { + $handler =& xoops_gethandler('config'); + + $criteria =& new CriteriaCompo(); + $criteria->add(new Criteria('conf_modid', $module->get('mid'))); + $criteria->add(new Criteria('conf_catid', 0)); + $criteria->add(new Criteria('conf_order', $info->mOrder)); + + $configArr =& $handler->getConfigs($criteria); + + if (count($configArr) == 0) { + $log->addError('Execption Error: Could not find config.'); + } + + $config =& $configArr[0]; + + $config->set('conf_name', $info->mName); + $config->set('conf_title', $info->mTitle); + $config->set('conf_desc', $info->mDescription); + $config->set('conf_formtype', $info->mFormType); + $config->set('conf_valuetype', $info->mValueType); + $config->setConfValueForInput($info->mDefault); + + $optionArr =& $handler->getConfigOptions(new Criteria('conf_id', $config->get('conf_id'))); + if (is_array($optionArr)) { + foreach (array_keys($optionArr) as $idx) { + $handler->_oHandler->delete($optionArr[$idx]); + } + } + + if (count($info->mOption->mOptions) > 0) { + foreach (array_keys($info->mOption->mOptions) as $idx) { + $option =& $handler->createConfigOption(); + $option->set('confop_name', $info->mOption->mOptions[$idx]->mName); + $option->set('confop_value', $info->mOption->mOptions[$idx]->mValue); + $option->set('conf_id', $option->get('conf_id')); + $config->setConfOptions($option); + unset($option); + } + } + + if ($handler->insertConfig($config)) { + $log->addReport(XCube_Utils::formatMessage("Preference '{0}' is updateded.", $config->get('conf_name'))); + } + else { + $log->addError(XCube_Utils::formatMessage("Could not update preference '{0}'.", $config->get('conf_name'))); + } + } + + function updateCommentByInfo(&$info, &$module, &$log) + { + $handler =& xoops_gethandler('config'); + + $criteria =& new CriteriaCompo(); + $criteria->add(new Criteria('conf_modid', $module->get('mid'))); + $criteria->add(new Criteria('conf_catid', 0)); + $criteria->add(new Criteria('conf_order', $info->mOrder)); + + $configArr =& $handler->getConfigs($criteria); + + if (count($configArr) == 0) { + $log->addError('Execption Error: Could not find config.'); + } + + $config =& $configArr[0]; + + $config->set('conf_order', $info->mOrder); + + if ($handler->insertConfig($config)) { + $log->addReport(XCube_Utils::formatMessage("Preference '{0}' is updateded.", $config->get('conf_name'))); + } + else { + $log->addError(XCube_Utils::formatMessage("Could not update preference '{0}'.", $config->get('conf_name'))); + } + } + function installBlockByInfo(&$info, &$module, &$log) { $handler =& xoops_gethandler('block'); @@ -938,6 +1126,27 @@ $handler->deleteAll($criteria); } + function uninstallPreferenceByOrder($order, &$module, &$log) + { + $handler =& xoops_gethandler('config'); + + $criteria =& new CriteriaCompo(); + $criteria->add(new Criteria('conf_modid', $module->get('mid'))); + $criteria->add(new Criteria('conf_catid', 0)); + $criteria->add(new Criteria('conf_order', $order)); + + $configArr =& $handler->getConfigs($criteria); + + foreach (array_keys($configArr) as $idx) { + if ($handler->deleteConfig($configArr[$idx])) { + $log->addReport(XCube_Utils::formatMessage("Delete preference '{0}'.", $configArr[$idx]->get('conf_name'))); + } + else { + $log->addError(XCube_Utils::formatMessage("Could not delete preference '{0}'.", $configArr[$idx]->get('conf_name'))); + } + } + } + /** * Executes SQL query as cube style. */ @@ -991,7 +1200,7 @@ define('LEGACY_BLOCKINFOMATION_STATUS_DELETED', "deleted"); /** - * The structure which is able to keep block's informations without DB. THis + * The structure which is able to keep block's informations without DB. This * is installer only. */ class Legacy_BlockInformation @@ -1122,6 +1331,294 @@ } } +/** + * The structure which is able to keep preference's informations without DB. + * This is installer only. + */ +class Legacy_PreferenceInformation +{ + var $mStatus = LEGACY_BLOCKINFOMATION_STATUS_LOADED; + + var $mOrder = 0; + + var $mName = ""; + + var $mTitle = ""; + + var $mDescription = ""; + + var $mFormType = ""; + + var $mValueType = ""; + + var $mDefault = null; + + var $mOption = null; + + function Legacy_PreferenceInformation($order, $name, $title, $description, $formType, $valueType, $default) + { + $this->mOrder = intval($order); + $this->mName = $name; + $this->mTitle = $title; + $this->mDescription = $description; + $this->mFormType = $formType; + $this->mValueType = $valueType; + $this->mDefault = $default; + + $this->mOption =& new Legacy_PreferenceOptionInfoCollection(); + } + + /** + * @return bool + */ + function isEqual(&$preference) + { + if ($this->mOrder != $preference->mOrder) { + return false; + } + + if ($this->mName != $preference->mName) { + return false; + } + + if ($this->mTitle != $preference->mTitle) { + return false; + } + + if ($this->mDescription != $preference->mDescription) { + return false; + } + + if ($this->mFormType != $preference->mFormType) { + return false; + } + + if ($this->mValueType != $preference->mValueType) { + return false; + } + + if (!$this->mOption->isEqual($preference->mOption)) { + return false; + } + + return true; + } + + function update(&$preference) + { + $this->mStatus = LEGACY_BLOCKINFOMATION_STATUS_UPDATED; + + $this->mName = $preference->mName; + $this->mTitle = $preference->mTitle; + $this->mDescription = $preference->mDescription; + $this->mFormType = $preference->mFormType; + $this->mValueType = $preference->mValueType; + $this->mDefault = $preference->mDefault; + + unset($this->mOption); + $this->mOption =& $preference->mOption; + } +} + +class Legacy_PreferenceInfoCollection +{ + var $mPreferences = array(); + + var $mComments = array(); + + var $mNotifications = array(); + + function Legacy_PreferenceInfoCollection() + { + } + + function add(&$preference) + { + if ($preference->mName == 'com_rule' || $preference->mName == 'com_anonpost') { + $this->mComments[$preference->mOrder] =& $preference; + $this->_sort(); + return true; + } + + if ($preference->mName == 'notification_enabled' || $preference->mName == 'notification_events') { + $this->mNotifications[$preference->mOrder] =& $preference; + $this->_sort(); + return true; + } + + if (isset($this->mPreferences[$preference->mOrder])) { + return false; + } + + $this->mPreferences[$preference->mOrder] =& $preference; + $this->_sort(); + + return true; + } + + function _sort() + { + $maxOrder = 0; + foreach (array_keys($this->mPreferences) as $idx) { + if ($this->mPreferences[$idx]->mOrder > $maxOrder) { + $maxOrder = $this->mPreferences[$idx]->mOrder; + } + } + + $maxOrder++; + + $t_comments = array(); + foreach (array_keys($this->mComments) as $idx) { + if ($this->mComments[$idx]->mOrder != $maxOrder) { + $this->mComments[$idx]->mStatus = LEGACY_BLOCKINFOMATION_STATUS_UPDATED; + $this->mComments[$idx]->mOrder = $maxOrder; + } + $t_comments[$maxOrder++] =& $this->mComments[$idx]; + } + + unset($this->mComments); + $this->mComments =& $t_comments; + + // + // Sort notifications + // + $t_notifications = array(); + foreach (array_keys($this->mNotifications) as $idx) { + if ($this->mNotifications[$idx]->mOrder != $maxOrder) { + $this->mNotifications[$idx]->mStatus = LEGACY_BLOCKINFOMATION_STATUS_UPDATED; + $this->mNotifications[$idx]->mOrder = $maxOrder; + } + $t_notifications[$maxOrder++] =& $this->mNotifications[$idx]; + } + + unset($this->mNotifications); + $this->mNotifications =& $t_notifications; + } + + function &get($order) + { + $ret = null; + + if (isset($this->mPreferences[$order])) { + return $this->mPreferences[$order]; + } + + return $ret; + } + + /** + * Updates the list of blocks by comparing with $collection. + * @todo need delete comments' data + * @todo need delete notifications' data + */ + function update(&$collection) + { + // + // Preferences + // + foreach (array_keys($this->mPreferences) as $idx) { + $t_preference =& $collection->get($this->mPreferences[$idx]->mOrder); + if ($t_preference == null) { + $this->mPreferences[$idx]->mStatus = LEGACY_BLOCKINFOMATION_STATUS_DELETED; + } + elseif (!$this->mPreferences[$idx]->isEqual($t_preference)) { + $this->mPreferences[$idx]->update($t_preference); + } + } + + foreach (array_keys($collection->mPreferences) as $idx) { + $order = $collection->mPreferences[$idx]->mOrder; + if (!isset($this->mPreferences[$order])) { + $this->add($collection->mPreferences[$idx]); + $this->mPreferences[$order]->mStatus = LEGACY_BLOCKINFOMATION_STATUS_NEW; + } + } + + // + // Comments + // + if (count($this->mComments) > 0 && count($collection->mComments) == 0) { + foreach (array_keys($this->mComments) as $idx) { + $this->mComments[$idx]->mStatus = LEGACY_BLOCKINFOMATION_STATUS_DELETED; + } + } + elseif (count($this->mComments) == 0 && count($collection->mComments) > 0) { + $this->mComments =& $collection->mComments; + } + + // + // Notifications + // + if (count($this->mNotifications) > 0 && count($collection->mNotifications) == 0) { + foreach (array_keys($this->mNotifications) as $idx) { + $this->mNotifications[$idx]->mStatus = LEGACY_BLOCKINFOMATION_STATUS_DELETED; + } + } + elseif (count($this->mNotifications) == 0 && count($collection->mNotifications) > 0) { + $this->mNotifications =& $collection->mNotifications; + } + } + + function reset() + { + unset($this->mPreferences); + $this->mPreferences = array(); + } +} + +class Legacy_PreferenceOptionInformation +{ + var $mName = ""; + var $mValue = ""; + + function Legacy_PreferenceOptionInformation($name, $value) + { + $this->mName = $name; + $this->mValue = $value; + } + + function isEqual($option) + { + return (($this->mName == $option->mName) && ($this->mValue == $option->mValue)); + } +} + +class Legacy_PreferenceOptionInfoCollection +{ + var $mOptions = array(); + + function Legacy_PreferenceOptionInfoCollection() + { + } + + function add(&$option) + { + $this->mOptions[] =& $option; + return true; + } + + function isEqual(&$collection) + { + if (count($this->mOptions) != count($collection->mOptions)) { + return false; + } + + foreach (array_keys($this->mOptions) as $idx) { + if (!$this->mOptions[$idx]->isEqual($collection->mOptions[$idx])) { + return false; + } + } + + return true; + } + + function reset() + { + unset($this->mOptions); + $this->mOptions = array(); + } +} + class Legacy_AbstractModinfoReader { function Legacy_AbstractModinfoReader() @@ -1134,6 +1631,13 @@ function &loadBlockInformations() { } + + /** + * @return Legacy_PreferenceInfoCollection + */ + function &loadPreferenceInformations() + { + } } /** @@ -1249,6 +1753,76 @@ return $collection; } + + function &_createPreferenceInformation($order, $arr) + { + $arr['description'] = isset($arr['description']) ? $arr['description'] : null; + $info =& new Legacy_PreferenceInformation($order, $arr['name'], $arr['title'], $arr['description'], $arr['formtype'], $arr['valuetype'], $arr['default']); + if (isset($arr['options'])) { + foreach ($arr['options'] as $name => $value) { + $option =& new Legacy_PreferenceOptionInformation($name, $value); + $info->mOption->add($option); + } + } + + return $info; + } + + /** + * @todo Need guarantee of global variables. + */ + function &loadPreferenceInformations() + { + $collection =& new Legacy_PreferenceInfoCollection(); + + $t_filePath = XOOPS_ROOT_PATH . '/modules/' . $this->_mDirname . '/xoops_version.php'; + if (!file_exists($t_filePath)) { + return $collection; + } + + include $t_filePath; + + if (!isset($modversion['config'])) { + return $collection; + } + + $preferenceArr = $modversion['config']; + + // + // Try (1) --- index pattern + // + + $successFlag = true; + foreach ($preferenceArr as $idx => $preference) { + if (is_int($idx)) { + $info =& $this->_createPreferenceInformation($idx, $preference); + $successFlag &= $collection->add($info); + unset($info); + } + else { + $successFlag = false; + break; + } + } + + if ($successFlag) { + return $collection; + } + + // + // Try (2) --- automatic + // + $collection->reset(); + + $idx = 1; + foreach ($preferenceArr as $preference) { + $info =& $this->_createPreferenceInformation($idx++, $preference); + $successFlag &= $collection->add($info); + unset($info); + } + + return $collection; + } } class Legacy_ModinfoX2DBReader extends Legacy_AbstractModinfoReader @@ -1286,6 +1860,41 @@ return $collection; } + + function &_createPreferenceInformation(&$config) + { + $info =& new Legacy_PreferenceInformation($config->get('conf_order'), $config->get('conf_name'), $config->get('conf_title'), $config->get('conf_desc'), $config->get('conf_formtype'), $config->get('conf_valuetype'), $config->get('conf_value')); + + $configOptionArr =& $config->getOptionItems(); + + foreach (array_keys($configOptionArr) as $idx) { + $option =& new Legacy_PreferenceOptionInformation($configOptionArr[$idx]->get('confop_name'), $configOptionArr[$idx]->get('confop_value')); + $info->mOption->add($option); + unset($option); + } + + return $info; + } + + function &loadPreferenceInformations() + { + $collection =& new Legacy_PreferenceInfoCollection(); + + $handler =& xoops_gethandler('module'); + $module =& $handler->getByDirname($this->_mDirname); + + $handler =& xoops_gethandler('config'); + $configArr =& $handler->getConfigs(new Criteria('conf_modid', $module->get('mid'))); + + foreach (array_keys($configArr) as $idx) { + $info =& $this->_createPreferenceInformation($configArr[$idx]); + while (!$collection->add($info)) { + $info->mOrder++; + } + } + + return $collection; + } }