NodeService.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace Admin\Service;
  3. /**
  4. * NodeService
  5. */
  6. class NodeService extends CommonService {
  7. /**
  8. * 节点类型
  9. * @var array
  10. */
  11. private $NODE_TYPE = array(
  12. 1 => '应用(GROUP)',
  13. 2 => '模块(MODULE)',
  14. 3 => '操作(ACTION)'
  15. );
  16. /**
  17. * 得到带又层级的node数据
  18. * @return array
  19. */
  20. public function getNodes() {
  21. $category = new \Org\Util\Category($this->getModelName(),
  22. array('id', 'pid','title'));
  23. return $category->getList();
  24. }
  25. /**
  26. * 添加节点
  27. * @param array $role 节点信息
  28. * @return array
  29. */
  30. public function addNode($node) {
  31. $Node = $this->getD();
  32. if (false === ($node = $Node->create($node))) {
  33. return $this->errorResultReturn($Node->getError());
  34. }
  35. if (false === $Node->add($node)) {
  36. return $this->errorResultReturn('系统错误!');
  37. }
  38. return $this->resultReturn(true);
  39. }
  40. /**
  41. * 得到应用节点
  42. * @param array $where 查询条件
  43. * @return array
  44. */
  45. public function getGroupNodes(array $where) {
  46. if (!isset($where) || !is_array($where)) {
  47. $where = array();
  48. }
  49. $map = array('level' => 1);
  50. return $this->getM()->where(array_merge($map, $where))->select();
  51. }
  52. /**
  53. * 得到模块节点
  54. * @param array $where 查询条件
  55. * @return array
  56. */
  57. public function getModuleNodes(array $where) {
  58. if (!isset($where) || !is_array($where)) {
  59. $where = array();
  60. }
  61. $map = array('level' => 2);
  62. return $this->getM()->where(array_merge($map, $where))->select();
  63. }
  64. /**
  65. * 得到操作节点
  66. * @param array $where 查询条件
  67. * @return array
  68. */
  69. public function getActionNodes(array $where) {
  70. if (!isset($where) || !is_array($where)) {
  71. $where = array();
  72. }
  73. $map = array('level' => 3);
  74. return $this->getM()->where(array_merge($map, $where))->select();
  75. }
  76. /**
  77. * 得到带有级别所有的节点
  78. * @return array
  79. */
  80. public function getLevelNodes() {
  81. $groups = $this->getGroupNodes();
  82. foreach ($groups as $i => $group) {
  83. $where['pid'] = $group['id'];
  84. $modules = $this->getModuleNodes($where);
  85. foreach ($modules as $j => $module) {
  86. $where['pid'] = $module['id'];
  87. $actions = $this->getActionNodes($where);
  88. $modules[$j]['actions'] = $actions;
  89. }
  90. $groups[$i]['modules'] = $modules;
  91. }
  92. return $groups;
  93. }
  94. /**
  95. * 得到节点的类型
  96. * @param int $type 节点的类型
  97. * @return string
  98. */
  99. public function getNodeType($type) {
  100. return $this->NODE_TYPE[$type];
  101. }
  102. /**
  103. * 添加模块管理节点
  104. * @param string $name 节点名称
  105. * @param string $ctrlName 控制器名称
  106. * @return boolean
  107. */
  108. public function addModuleNodes($name, $ctrlName) {
  109. $Node = $this->getM();
  110. // 得到顶级节点的id
  111. $pNode = $Node->field('id')->getByPid(0);
  112. if (is_null($pNode)) {
  113. return false;
  114. }
  115. $Node->startTrans();
  116. $node = array(
  117. 'status' => 1,
  118. 'created_at' => time(),
  119. 'updated_at' => time()
  120. );
  121. // 模块节点
  122. $mNode = array(
  123. 'pid' => $pNode['id'],
  124. 'title' => "{$name}管理",
  125. 'name' => $ctrlName,
  126. 'level' => 2
  127. );
  128. $ms = $Node->add(array_merge($mNode, $node));
  129. // 模块id
  130. $pid = $this->getM()->getLastInsId();
  131. // 操作节点
  132. $node['pid'] = $pid;
  133. $node['level'] = 3;
  134. // index
  135. $index = array('title' => "{$name}管理", 'name' => "index");
  136. // add
  137. $add = array('title' => "添加{$name}", 'name' => "add");
  138. // create
  139. $create = array('title' => "创建{$name}", 'name' => "create");
  140. // edit
  141. $edit = array('title' => "编辑{$name}", 'name' => "edit");
  142. // update
  143. $update = array('title' => "更新{$name}", 'name' => "update");
  144. // delete
  145. $delete = array('title' => "删除{$name}", 'name' =>"delete");
  146. // medit
  147. $medit = array('title' => "批量操作{$name}", 'name' =>"medit");
  148. $nodes = array(
  149. array_merge($node, $index),
  150. array_merge($node, $add),
  151. array_merge($node, $create),
  152. array_merge($node, $edit),
  153. array_merge($node, $update),
  154. array_merge($node, $delete),
  155. array_merge($node, $medit),
  156. );
  157. $ns = $Node->addAll($nodes);
  158. if (false === $ms || false === $ns) {
  159. $Node->rollback();
  160. return false;
  161. }
  162. $Node->commit();
  163. return true;
  164. }
  165. /**
  166. * 删除模块管理节点
  167. * @param string $ctrlName 控制器名称
  168. * @return boolean
  169. */
  170. public function deleteModuleNodes($ctrlName) {
  171. $Node = $this->getM();
  172. $mNode = $Node->field('id')->getByName($ctrlName);
  173. if (is_null($mNode)) {
  174. return false;
  175. }
  176. $Node->delete($mNode['id']);
  177. $Node->where("pid={$mNode['id']}")->delete();
  178. return true;
  179. }
  180. /**
  181. * 设置节点状态
  182. * @param int $id 节点id
  183. * @param int $status 节点状态
  184. * @return mixed
  185. */
  186. public function setStatus($id, $status) {
  187. return $this->getM()
  188. ->where("id={$id}")
  189. ->save(array('status' => $status));
  190. }
  191. /**
  192. * 节点是否存在
  193. * @param int $id 节点id
  194. * @return boolean
  195. */
  196. public function existNode($id) {
  197. $node = $this->getM()->getById($id);
  198. return !empty($node);
  199. }
  200. protected function getModelName() {
  201. return 'Node';
  202. }
  203. }