From 0eb9a89280fde11d0299b2a2d436a9b5ed7d3401 Mon Sep 17 00:00:00 2001 From: wangchunsheng Date: Wed, 3 Mar 2010 15:16:12 +0000 Subject: [PATCH] + add the feature of delete a module. --- trunk/module/tree/model.php | 126 ++++++++++++++++++------- trunk/module/tree/view/browse.html.php | 13 ++- 2 files changed, 104 insertions(+), 35 deletions(-) diff --git a/trunk/module/tree/model.php b/trunk/module/tree/model.php index 2541cae6b2..27fd67ec65 100644 --- a/trunk/module/tree/model.php +++ b/trunk/module/tree/model.php @@ -28,20 +28,26 @@ class treeModel extends model /* 通过模块id获取模块信息。*/ public function getByID($moduleID) { - return $this->dbh->query("SELECT * FROM " . TABLE_MODULE . " WHERE id = '$moduleID'")->fetch(); + return $this->dao->findById((int)$moduleID)->from(TABLE_MODULE)->fetch(); } /* 生成查询的sql语句。*/ private function buildMenuQuery($productID, $viewType, $rootModuleID) { - $sql = "SELECT * FROM " . TABLE_MODULE . " WHERE product = '$productID' AND `view` = '$viewType'"; + /* 查找rootModule。*/ + $rootModulePath = ''; if($rootModuleID > 0) { - $rootModule = $this->getByID($rootModuleID); - if($rootModule) $sql .= " AND `path` LIKE '$rootModule->path%'"; + $rootModule = $this->getById($rootModuleID); + if($rootModule) $rootModulePath = $rootModule->path . '%'; } - $sql .= " ORDER BY grade DESC, `order`"; - return $sql; + + return $this->dao->select('*')->from(TABLE_MODULE) + ->where('product')->eq((int)$productID) + ->andWhere('view')->eq($viewType) + ->onCaseOf($rootModulePath)->andWhere('path')->like($rootModulePath)->endCase() + ->orderBy('grade desc, `order`') + ->get(); } /* 获取模块的下类列表,用于生成select控件。*/ @@ -169,33 +175,30 @@ class treeModel extends model /* 获得某一个模块的直接下级模块。*/ public function getSons($productID, $moduleID, $viewType = 'product') { - $sql = "SELECT * FROM " . TABLE_MODULE . " WHERE product = '$productID' AND parent = '$moduleID' AND view = '$viewType' ORDER BY `order`"; - return $this->dbh->query($sql)->fetchAll(); + return $this->dao->select('*')->from(TABLE_MODULE) + ->where('product')->eq((int)$productID) + ->andWhere('parent')->eq((int)$moduleID) + ->andWhere('view')->eq($viewType) + ->orderBy('`order`') + ->fetchAll(); } /* 获得一个模块的id列表。*/ public function getAllChildId($moduleID) { if($moduleID == 0) return array(); - $module = $this->getById($moduleID); - $sql = "SELECT id FROM " . TABLE_MODULE . " WHERE path LIKE '{$module->path}%'"; - $stmt = $this->dbh->query($sql); - $moduleIds = array(); - while($id = $stmt->fetchColumn()) $moduleIds[] = $id; - return $moduleIds; + $module = $this->getById((int)$moduleID); + return $this->dao->select('id')->from(TABLE_MODULE)->where('path')->like($module->path . '%')->fetchPairs(); } /* 获得一个模块的所有上级模块。*/ public function getParents($moduleID) { if($moduleID == 0) return array(); - $sql = "SELECT path FROM " . TABLE_MODULE . " WHERE id = '$moduleID'"; - $path = $this->dbh->query($sql)->fetchColumn(); - $path = substr($path, 1, -1); + $path = $this->dao->select('path')->from(TABLE_MODULE)->where('id')->eq((int)$moduleID)->fetch('path'); + $path = trim($path, ','); if(!$path) return array(); - $sql = "SELECT * FROM " . TABLE_MODULE . " WHERE id IN($path) ORDER BY grade"; - $parents = $this->dbh->query($sql)->fetchAll(); - return $parents; + return $this->dao->select('*')->from(TABLE_MODULE)->where('id')->in($path)->orderBy('grade')->fetchAll(); } /* 更新排序信息。*/ @@ -203,8 +206,7 @@ class treeModel extends model { foreach($orders as $moduleID => $order) { - $sql = "UPDATE " . TABLE_MODULE . " SET `order` = '$order' WHERE id = '$moduleID' LIMIT 1"; - $this->dbh->exec($sql); + $this->dao->update(TABLE_MODULE)->set('`order`')->eq($order)->where('id')->eq((int)$moduleID)->limit(1)->exec(); } } @@ -222,32 +224,90 @@ class treeModel extends model $grade = 1; $parentPath = ','; } + $i = 1; foreach($childs as $moduleID => $moduleName) { if(empty($moduleName)) continue; + + /* 新增模块。*/ if(is_numeric($moduleID)) { - $sql = "INSERT INTO " . TABLE_MODULE . "(`product`, `name`, `parent`, `grade`, `view`) - VALUES('$productID', '$moduleName', '$parentModuleID', '$grade', '$viewType')"; - $this->dbh->exec($sql); - $moduleID = $this->dbh->lastInsertID(); + $module->product = $productID; + $module->name = $moduleName; + $module->parent = $parentModuleID; + $module->grade = $grade; + $module->view = $viewType; + $module->order = $this->post->maxOrder + $i * 10; + $this->dao->insert(TABLE_MODULE)->data($module)->exec(); + $moduleID = $this->dao->lastInsertID(); $childPath = $parentPath . "$moduleID,"; - $sql = "UPDATE " . TABLE_MODULE . " SET `path` = '$childPath' WHERE id = '$moduleID' LIMIT 1"; - $this->dbh->exec($sql); + $this->dao->update(TABLE_MODULE)->set('path')->eq($childPath)->where('id')->eq($moduleID)->limit(1)->exec(); + $i ++; } else { $moduleID = str_replace('id', '', $moduleID); - $sql = "UPDATE " . TABLE_MODULE . " SET `name` = '$moduleName' WHERE id = '$moduleID' LIMIT 1"; - $this->dbh->exec($sql); + $this->dao->update(TABLE_MODULE)->set('name')->eq($moduleName)->where('id')->eq($moduleID)->limit(1)->exec(); } } } - /* 删除一个模块。Todo: 需要修改下级目录的权限,还有对应的需求列表。*/ + /* 删除一个模块。*/ public function delete($moduleID) { - $module = $this->getById((int)$moduleID); - $this->dao->delete()->from(TABLE_MODULE)->where('id')->eq($moduleID)->exec(); + $module = $this->getById($moduleID); + $childs = $this->getAllChildId($moduleID); + + $this->dao->update(TABLE_MODULE)->set('grade = grade - 1')->where('id')->in($childs)->exec(); // 更新所有的下级模块的grade。 + $this->dao->update(TABLE_MODULE)->set('parent')->eq($module->parent)->where('parent')->eq($moduleID)->exec(); // 更新直接下级的parent。 + $this->dao->delete()->from(TABLE_MODULE)->where('id')->eq($moduleID)->exec(); // 删除自己。 + $this->fixModulePath(); + + if($module->view == 'product') $this->dao->update(TABLE_STORY)->set('module')->eq($module->parent)->where('module')->eq($moduleID)->exec(); + if($module->view == 'bug') $this->dao->update(TABLE_BUG)->set('module')->eq($module->parent)->where('module')->eq($moduleID)->exec(); + if($module->view == 'case') $this->dao->update(TABLE_CASE)->set('module')->eq($module->parent)->where('module')->eq($moduleID)->exec(); + } + + /* 修正modulePath字段。*/ + public function fixModulePath() + { + /* 获得最大的级别。*/ + $maxGrade = $this->dao->select('MAX(grade) AS grade')->from(TABLE_MODULE)->fetch('grade'); + $modules = array(); + + /* 依次处理每个级别的模块。*/ + for($grade = 1; $grade <= $maxGrade; $grade ++) + { + /* 当前级别的模块。*/ + $gradeModules = $this->dao->select('id, parent')->from(TABLE_MODULE)->where('grade')->eq($grade)->fetchAll('id'); + foreach($gradeModules as $moduleID => $module) + { + if($grade == 1) + { + $module->path = ",$moduleID,"; + } + else + { + /* 取parent模块的path。*/ + if(isset($modules[$module->parent])) + { + $module->path = $modules[$module->parent]->path . "$moduleID,"; + } + else + { + $module->parent = 0; + $module->grade = 1; + $module->path = ",$moduleID,"; + } + } + } + $modules += $gradeModules; + } + + /* 最后更新每一个模块。*/ + foreach($modules as $moduleID => $module) + { + $this->dao->update(TABLE_MODULE)->data($module)->where('id')->eq($module->id)->limit(1)->exec(); + } } } diff --git a/trunk/module/tree/view/browse.html.php b/trunk/module/tree/view/browse.html.php index 37ded63b14..79054736c8 100644 --- a/trunk/module/tree/view/browse.html.php +++ b/trunk/module/tree/view/browse.html.php @@ -61,14 +61,23 @@ id]", $sonModule->name, 'style="margin-bottom:5px"') . '
'; + $maxOrder = 0; + foreach($sons as $sonModule) + { + if($sonModule->order > $maxOrder) $maxOrder = $sonModule->order; + echo html::input("modules[id$sonModule->id]", $sonModule->name, 'style="margin-bottom:5px"') . '
'; + } for($i = 0; $i < TREE::NEW_CHILD_COUNT ; $i ++) echo html::input("modules[]", '', 'style="margin-bottom:5px"') . '
'; ?> - +