From 629a91c27bf1f10c3881b7e5cbeb52375a24120f Mon Sep 17 00:00:00 2001 From: wangchunsheng Date: Fri, 22 Jun 2012 08:44:54 +0000 Subject: [PATCH] * Fix bug#306. --- module/tree/control.php | 14 ++++++++++++ module/tree/lang/en.php | 4 +++- module/tree/lang/zh-cn.php | 2 ++ module/tree/model.php | 47 ++++++++++++++++++++++++++------------ 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/module/tree/control.php b/module/tree/control.php index f0d2c77524..0e244bface 100644 --- a/module/tree/control.php +++ b/module/tree/control.php @@ -142,6 +142,20 @@ class tree extends control die($this->display()); } + /** + * Fix path, grades. + * + * @param string $root + * @param string $type + * @access public + * @return void + */ + public function fix($root, $type) + { + $this->tree->fixModulePath($root, $type); + die(js::alert($this->lang->tree->successFixed) . js::reload('parent')); + } + /** * Update modules' orders. * diff --git a/module/tree/lang/en.php b/module/tree/lang/en.php index 825e8449c5..351d05df02 100644 --- a/module/tree/lang/en.php +++ b/module/tree/lang/en.php @@ -16,6 +16,7 @@ $lang->tree->addChild = 'Add child'; $lang->tree->delete = 'Delete'; $lang->tree->browse = 'Module manage'; $lang->tree->manage = 'Modules'; +$lang->tree->fix = 'Fix'; $lang->tree->manageProduct = 'Mange product module'; $lang->tree->manageProject = 'Manage project module'; $lang->tree->manageBug = 'Manage bug module'; @@ -28,7 +29,8 @@ $lang->tree->ajaxGetOptionMenu = 'API: Get select menu'; $lang->tree->ajaxGetSonModules = 'API: Get son modules'; $lang->tree->confirmDelete = 'Are you sure to delete this module?'; -$lang->tree->successSave = 'Success saved'; +$lang->tree->successSave = 'Successfully saved'; +$lang->tree->successFixed = 'Successfully fixed.'; $lang->tree->name = 'Name'; $lang->tree->parent = 'Parent'; diff --git a/module/tree/lang/zh-cn.php b/module/tree/lang/zh-cn.php index a3f07d7ec0..761fee6cc9 100644 --- a/module/tree/lang/zh-cn.php +++ b/module/tree/lang/zh-cn.php @@ -16,6 +16,7 @@ $lang->tree->addChild = '添加子模块'; $lang->tree->delete = '删除模块'; $lang->tree->browse = '模块维护'; $lang->tree->manage = '维护模块'; +$lang->tree->fix = '修正数据'; $lang->tree->manageProduct = '维护产品视图模块'; $lang->tree->manageProject = '维护项目视图模块'; $lang->tree->manageBug = '维护测试视图模块'; @@ -29,6 +30,7 @@ $lang->tree->ajaxGetSonModules = '接口:获得子菜单列表'; $lang->tree->confirmDelete = '您确定删除该模块吗?'; $lang->tree->successSave = '成功保存'; +$lang->tree->successFixed = '成功修正数据!'; $lang->tree->name = '模块名称'; $lang->tree->parent = '上级模块'; diff --git a/module/tree/model.php b/module/tree/model.php index c011a0b618..803c212ed4 100644 --- a/module/tree/model.php +++ b/module/tree/model.php @@ -503,7 +503,7 @@ class treeModel extends model } /** - * Fix fieilds of all module, grade, parent, pathes and so on. + * Fix the path, grade fields according to the id and parent fields. * * @param string $root * @param string $type @@ -512,25 +512,44 @@ class treeModel extends model */ public function fixModulePath($root, $type) { - /* Get all modules order by parent. Thus a parent module can be before a child. */ - $modules = $this->dao->select('id, parent')->from(TABLE_MODULE)->where('root')->eq($root)->andWhere('type')->eq($type)->orderBy('parent')->fetchAll('id'); + /* Get all modules grouped by parent. */ + $groupModules = $this->dao->select('id, parent')->from(TABLE_MODULE)->where('root')->eq($root)->andWhere('type')->eq($type)->fetchGroup('parent', 'id'); + $modules = array(); - foreach($modules as $moduleID => $module) + /* Cycle the groupModules until it has no item any more. */ + while(count($groupModules) > 0) { - if($module->parent == 0) + $oldCounts = count($groupModules); // Record the counts before processing. + foreach($groupModules as $parentModuleID => $childModules) { - $module->grade = 1; - $module->path = ",$moduleID,"; - } - else - { - $parentModule = $modules[$module->parent]; - $module->path = $parentModule->path . "$moduleID,"; - $module->grade = $parentModule->grade + 1; + /* If the parentModule doesn't exsit in the modules, skip it. If exists, compute it's child modules. */ + if(!isset($modules[$parentModuleID]) and $parentModuleID != 0) continue; + if($parentModuleID == 0) + { + $parentModule->grade = 0; + $parentModule->path = ','; + } + else + { + $parentModule = $modules[$parentModuleID]; + } + + /* Compute it's child modules. */ + foreach($childModules as $childModuleID => $childModule) + { + $childModule->grade = $parentModule->grade + 1; + $childModule->path = $parentModule->path . $childModule->id . ','; + $modules[$childModuleID] = $childModule; // Save child module to modules, thus the child of child can compute it's grade and path. + } + unset($groupModules[$parentModuleID]); // Remove it from the groupModules. } + if(count($groupModules) == $oldCounts) break; // If after processing, no module processed, break the cycle. } /* Save modules to database. */ - foreach($modules as $module) $this->dao->update(TABLE_MODULE)->data($module)->where('id')->eq($module->id)->limit(1)->exec(); + foreach($modules as $module) + { + $this->dao->update(TABLE_MODULE)->data($module)->where('id')->eq($module->id)->limit(1)->exec(); + } } }