model.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * The model file of host module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Jiangxiu Peng <pengjiangxiu@cnezsoft.com>
  8. * @package module
  9. * @version $Id$
  10. * @link https://www.zentao.net
  11. */
  12. class hostModel extends model
  13. {
  14. /**
  15. * 获取主机列表。
  16. * Get host list.
  17. *
  18. * @param string $browseType
  19. * @param int $param
  20. * @param string $orderBy
  21. * @param object $pager
  22. * @access public
  23. * @return array
  24. */
  25. public function getList($browseType = 'all', $param = 0, $orderBy = 'id_desc', $pager = null)
  26. {
  27. $browseType = strtolower($browseType);
  28. $query = '';
  29. if($browseType == 'bysearch')
  30. {
  31. /* Concatenate the conditions for the query. */
  32. if($param)
  33. {
  34. $query = $this->loadModel('search')->getQuery($param);
  35. if($query)
  36. {
  37. $this->session->set('hostQuery', $query->sql);
  38. $this->session->set('hostForm', $query->form);
  39. }
  40. else
  41. {
  42. $this->session->set('hostQuery', ' 1 = 1');
  43. }
  44. }
  45. else
  46. {
  47. if($this->session->hostQuery == false) $this->session->set('hostQuery', ' 1 = 1');
  48. }
  49. $query = $this->session->hostQuery;
  50. }
  51. $modules = 0;
  52. if($browseType == 'bymodule' && $param) $modules = $this->loadModel('tree')->getAllChildId($param);
  53. $hostList = $this->dao->select('*')->from(TABLE_HOST)
  54. ->where('deleted')->eq('0')
  55. ->andWhere('type')->eq('normal')
  56. ->beginIF($modules)->andWhere('`group`')->in($modules)->fi()
  57. ->beginIF($query)->andWhere($query)->fi()
  58. ->orderBy($orderBy)
  59. ->page($pager)
  60. ->fetchAll();
  61. foreach($hostList as &$host) if(!$host->serverRoom) $host->serverRoom = '';
  62. return $hostList;
  63. }
  64. /**
  65. * 获取主机键值对列表。
  66. * Get pairs.
  67. *
  68. * @param string $moduleIdList
  69. * @param string $status
  70. * @access public
  71. * @return array
  72. */
  73. public function getPairs($moduleIdList = '', $status = '')
  74. {
  75. $modules = array();
  76. if($moduleIdList)
  77. {
  78. $this->loadModel('tree');
  79. foreach(explode(',', $moduleIdList) as $moduleID)
  80. {
  81. if(empty($moduleID)) continue;
  82. $modules += $this->tree->getAllChildId((int)$moduleID);
  83. }
  84. }
  85. return $this->dao->select('id, name')->from(TABLE_HOST)
  86. ->where('deleted')->eq('0')
  87. ->andWhere('type')->eq('normal')
  88. ->beginIF($modules)->andWhere('`group`')->in($modules)->fi()
  89. ->beginIF($status)->andWhere('status')->eq($status)->fi()
  90. ->orderBy('`group`')
  91. ->fetchPairs();
  92. }
  93. /**
  94. * 创建主机。
  95. * create a host.
  96. *
  97. * @param object $formData
  98. * @access public
  99. * @return bool
  100. */
  101. public function create($formData)
  102. {
  103. $this->dao->insert(TABLE_HOST)->data($formData)
  104. ->check('name', 'unique')
  105. ->batchCheck($this->config->host->create->requiredFields, 'notempty')
  106. ->autoCheck()
  107. ->exec();
  108. if(dao::isError()) return false;
  109. $hostID = $this->dao->lastInsertID();
  110. $this->loadModel('action')->create('host', $hostID, 'created');
  111. return !dao::isError();
  112. }
  113. /**
  114. * 更新主机。
  115. * Update a host.
  116. *
  117. * @param object $formData
  118. * @access public
  119. * @return bool
  120. */
  121. public function update($formData)
  122. {
  123. if(empty($formData->id)) return false;
  124. $oldHost = $this->fetchByID($formData->id);
  125. $this->dao->update(TABLE_HOST)->data($formData)
  126. ->check('name', 'unique', '`id` != ' . $formData->id)
  127. ->batchCheck($this->config->host->edit->requiredFields, 'notempty')
  128. ->autoCheck()
  129. ->where('id')->eq($formData->id)
  130. ->exec();
  131. if(dao::isError()) return false;
  132. $changes = common::createChanges($oldHost, $formData);
  133. if($changes)
  134. {
  135. $actionID = $this->loadModel('action')->create('host', $formData->id, 'Edited');
  136. if(!empty($changes)) $this->action->logHistory($actionID, $changes);
  137. }
  138. return !dao::isError();
  139. }
  140. /**
  141. * 改变主机的状态。
  142. * Update a host status.
  143. *
  144. * @param object $formData
  145. * @access public
  146. * @return bool
  147. */
  148. public function updateStatus($formData)
  149. {
  150. if(empty($formData->id) || empty($formData->status) || empty($formData->reason)) return false;
  151. $this->dao->update(TABLE_HOST)->data($formData, 'reason')->where('id')->eq($formData->id)->exec();
  152. if(dao::isError()) return false;
  153. $this->loadModel('action')->create('host', $formData->id, $formData->status, $formData->reason);
  154. return !dao::isError();
  155. }
  156. /**
  157. * 获取物理拓扑图所需的数据结构。
  158. * Get tree map of server room.
  159. *
  160. * @access public
  161. * @return array
  162. */
  163. public function getServerroomTreemap()
  164. {
  165. $this->app->loadLang('serverroom');
  166. /* Get host list. */
  167. $stmt = $this->dao->select('t1.id,t1.id as hostID,t1.name,t2.id as roomID,t2.city,t2.name as roomName,t1.extranet')->from(TABLE_HOST)->alias('t1')
  168. ->leftJoin(TABLE_SERVERROOM)->alias('t2')->on('t1.serverRoom=t2.id')
  169. ->where('t1.deleted')->eq(0)
  170. ->andWhere('t1.type')->eq('normal')
  171. ->andWhere('t2.deleted')->eq(0)
  172. ->andWhere('t1.serverRoom')->ne(0)
  173. ->orderBy('t2.city,t2.id,t1.id')
  174. ->query();
  175. /* Group host by city and server room. */
  176. $hostGroup = array();
  177. while($host = $stmt->fetch()) $hostGroup[$host->city][$host->roomID][] = $host;
  178. $treeMap = $this->processTreemap($hostGroup);
  179. return $treeMap;
  180. }
  181. /**
  182. * 获取分组拓扑图所需的数据结构。
  183. * Get tree map by group.
  184. *
  185. * @access public
  186. * @return array
  187. */
  188. public function getGroupTreemap()
  189. {
  190. $this->app->loadLang('serverroom');
  191. /* Get host list by group. */
  192. $hosts = $this->dao->select('id,id as hostID,name,`group`,extranet')->from(TABLE_HOST)->where('deleted')->eq(0)->andWhere('type')->eq('normal')->fetchGroup('group', 'id');
  193. $modules = $this->getTreeModules(0, $hosts);
  194. $treemap = array();
  195. $treemap['text'] = '/';
  196. $treemap['collapsed'] = false;
  197. $treemap['children'] = $this->processTreemap($modules);
  198. return $treemap;
  199. }
  200. /**
  201. * 判断操作按钮是否可点击。
  202. * Judge an action is clickable or not.
  203. *
  204. * @param object $host
  205. * @param string $action
  206. * @static
  207. * @access public
  208. * @return bool
  209. */
  210. public static function isClickable($host, $action)
  211. {
  212. if(!$host->id) return false;
  213. if($action == 'online') return $host->status != 'online';
  214. if($action == 'offline') return $host->status != 'offline';
  215. if($action == 'delete') return $host->deleted == '0';
  216. if($action == 'edit') return $host->deleted == '0';
  217. return true;
  218. }
  219. /**
  220. * 处理成树形图需要的数据结构。
  221. * Process to treemap data.
  222. *
  223. * @param array $datas
  224. * @access private
  225. * @return array
  226. */
  227. private function processTreemap($datas)
  228. {
  229. $treeMap = array();
  230. foreach($datas as $key => $data)
  231. {
  232. $text = '';
  233. $host = is_array($data) ? reset($data) : $data;
  234. if(is_array($host)) $text = zget($this->lang->serverroom->cityList, $key);
  235. if(is_object($host)) $text = is_array($data) ? $host->roomName : $host->name;
  236. $children = array();
  237. $children['text'] = htmlspecialchars($text);
  238. if(is_array($data))
  239. {
  240. $children['collapsed'] = false;
  241. $children['children'] = $this->processTreemap($data);
  242. }
  243. elseif(!empty($data->children))
  244. {
  245. $children['collapsed'] = false;
  246. $children['children'] = $this->processTreemap($data->children);
  247. }
  248. elseif(!empty($data->hostID))
  249. {
  250. $children['hostid'] = $data->hostID;
  251. }
  252. $treeMap[] = $children;
  253. }
  254. return $treeMap;
  255. }
  256. /**
  257. * 获取树状结构的模块数据。
  258. * Get tree modules.
  259. *
  260. * @param int $rootID
  261. * @param array $hosts
  262. * @access private
  263. * @return array
  264. */
  265. private function getTreeModules($rootID, $hosts)
  266. {
  267. $treemap = array();
  268. $modules = $this->dao->select('*')->from(TABLE_MODULE)->where('parent')->eq($rootID)->andWhere('type')->eq('host')->orderBy('`order`,id')->fetchAll();
  269. foreach($modules as $module)
  270. {
  271. $module->children = $this->getTreeModules($module->id, $hosts);
  272. $treemap[] = $module;
  273. }
  274. if(!empty($hosts[$rootID])) $treemap = array_merge($treemap, $hosts[$rootID]);
  275. return $treemap;
  276. }
  277. }