+ add the feature of delete a module.

This commit is contained in:
wangchunsheng
2010-03-03 15:16:12 +00:00
parent c771c0ab66
commit 0eb9a89280
2 changed files with 104 additions and 35 deletions

View File

@@ -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();
}
}
}

View File

@@ -61,14 +61,23 @@
</td>
<td>
<?php
foreach($sons as $sonModule) echo html::input("modules[id$sonModule->id]", $sonModule->name, 'style="margin-bottom:5px"') . '<br />';
$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"') . '<br />';
}
for($i = 0; $i < TREE::NEW_CHILD_COUNT ; $i ++) echo html::input("modules[]", '', 'style="margin-bottom:5px"') . '<br />';
?>
</td>
</tr>
<tr>
<td class='a-center' colspan='2'>
<?php echo html::submitButton() . html::resetButton();?>
<?php
echo html::submitButton() . html::resetButton();
echo html::hidden('parentModuleID', $currentModuleID);
echo html::hidden('maxOrder', $maxOrder);
?>
<input type='hidden' value='<?php echo $currentModuleID;?>' name='parentModuleID' />
</td>
</tr>