Files
EasySoft-ZenTaoPMS/module/host/model.php
T
2023-12-25 14:11:33 +08:00

329 lines
11 KiB
PHP

<?php
/**
* The model file of ops module of ZenTaoCMS.
*
* @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
* @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
* @author Jiangxiu Peng <pengjiangxiu@cnezsoft.com>
* @package ops
* @version $Id$
* @link https://www.zentao.net
*/
class hostModel extends model
{
/**
* Get host list.
*
* @param string $browseType
* @param int $param
* @param string $orderBy
* @param object $pager
* @access public
* @return array
*/
public function getList($browseType = 'all', $param = 0, $orderBy = 'id_desc', $pager = null)
{
$query = '';
$modules = '';
$browseType = strtolower($browseType);
if($browseType == 'bysearch')
{
/* Concatenate the conditions for the query. */
if($param)
{
$query = $this->loadModel('search')->getQuery($param);
if($query)
{
$this->session->set('hostQuery', $query->sql);
$this->session->set('hostForm', $query->form);
}
else
{
$this->session->set('hostQuery', ' 1 = 1');
}
}
else
{
if($this->session->hostQuery == false) $this->session->set('hostQuery', ' 1 = 1');
}
$query = $this->session->hostQuery;
}
elseif($browseType == 'bymodule')
{
$modules = $param ? $this->loadModel('tree')->getAllChildId($param) : '0';
}
$orderBy = str_replace('t1.', '', $orderBy);
$host = $this->dao->select('*,id as hostID')->from(TABLE_HOST)
->where('deleted')->eq('0')
->andWhere('type')->eq('normal')
->beginIF($modules)->andWhere('`group`')->in($modules)->fi()
->beginIF($query)->andWhere($query)->fi()
->orderBy($orderBy)
->page($pager)
->fetchAll();
return $host;
}
/**
* Get pairs by services.
*
* @param string $services
* @access public
* @return array
*/
public function getPairsByService($services)
{
$servers = $this->dao->select('id,hosts')->from(TABLE_SERVICE)->where('id')->in($services)->andWhere('hosts')->ne('')->fetchPairs('id', 'hosts');
$hostIdList = array_unique(explode(',', join(',', $servers)));
return $this->dao->select('id,name')->from(TABLE_HOST)
->where('deleted')->eq('0')
->andWhere('`id`')->in($hostIdList)
->andWhere('type')->eq('normal')
->orderBy('`group`')
->fetchPairs('id', 'name');
}
/**
* Get pairs.
*
* @param string $moduleIdList
* @param string $idFrom
* @access public
* @return array
*/
public function getPairs($moduleIdList = 0)
{
$modules = array();
if($moduleIdList)
{
$this->loadModel('tree');
foreach(explode(',', $moduleIdList) as $moduleID)
{
if(empty($moduleID)) continue;
$modules += $this->tree->getAllChildId($moduleID);
}
}
return $this->dao->select("id,name")->from(TABLE_HOST)
->where('deleted')->eq('0')
->andWhere('type')->eq('normal')
->beginIF($modules)->andWhere('`group`')->in($modules)->fi()
->orderBy('`group`')
->fetchPairs('id', 'name');
}
/**
* 创建主机。
* create a host.
*
* @param object $formData
* @access public
* @return bool
*/
public function create(object $formData): bool
{
$this->dao->insert(TABLE_HOST)->data($formData)->batchCheck($this->config->host->create->requiredFields, 'notempty')->autoCheck()->exec();
if(dao::isError()) return false;
$hostID = $this->dao->lastInsertID();
$this->loadModel('action')->create('host', $hostID, 'created');
return !dao::isError();
}
/**
* 更新主机。
* Update a host.
*
* @param object $formData
* @access public
* @return bool
*/
public function update(object $formData): bool
{
$oldHost = $this->fetchByID($formData->id);
$this->dao->update(TABLE_HOST)->data($formData)->batchCheck($this->config->host->create->requiredFields, 'notempty')->autoCheck()->where('id')->eq($formData->id)->exec();
if(dao::isError()) return false;
$changes = common::createChanges($oldHost, $formData);
if($changes)
{
$actionID = $this->loadModel('action')->create('host', $formData->id, 'Edited');
if(!empty($changes)) $this->action->logHistory($actionID, $changes);
}
return !dao::isError();
}
/**
* 改变主机的状态。
* Update a host status.
*
* @param object $formData
* @access public
* @return bool
*/
public function updateStatus(object $formData): bool
{
$this->dao->update(TABLE_HOST)->data($formData, 'reason')->where('id')->eq($formData->id)->exec();
if(dao::isError()) return false;
$this->loadModel('action')->create('host', $formData->id, $formData->status, $formData->reason);
return !dao::isError();
}
/**
* Get tree map of server room.
*
* @access public
* @return string
*/
public function getServerroomTreemap()
{
/* Get host list. */
$this->app->loadLang('serverroom');
$stmt = $this->dao->select('t1.id,t1.name,t3.id as roomID,t3.city,t3.name as roomName,t1.extranet')->from(TABLE_HOST)->alias('t1')
->leftJoin(TABLE_SERVERROOM)->alias('t3')->on('t1.serverRoom=t3.id')
->where('t1.deleted')->eq(0)
->andWhere('t1.type')->eq('normal')
->andWhere('t3.deleted')->eq(0)
->andWhere('t1.serverRoom')->ne(0)
->orderBy('t3.city,t3.id,t1.id')
->query();
/* Group host by city and server room. */
$hostGroup = array();
while($host = $stmt->fetch())
{
$hostGroup[$host->city][$host->roomID][] = $host;
}
$treeMap = array();
foreach($hostGroup as $city => $rooms)
{
$children = array();
$children['text'] = zget($this->lang->serverroom->cityList, $city);
$children['collapsed'] = false;
$children['children'] = array();
foreach($rooms as $roomID => $cabinets)
{
$host = reset($cabinets);
if(is_array($host)) $host = reset($host);
$subChildren = array();
$subChildren['text'] = htmlspecialchars($host->roomName);
$subChildren['collapsed'] = false;
$subChildren['children'] = array();
foreach($cabinets as $cabinet => $hosts)
{
if(is_array($hosts))
{
$hostNameList = array();
foreach($hosts as $host) $hostNameList[] = array('text' => htmlspecialchars($host->name), 'hostid' => $host->id);
$subChildren['children'][] = array(
'text' => htmlspecialchars($cabinet),
'collapsed' => false,
'children' => $hostNameList
);
}
else
{
$subChildren['children'][] = array('text' => htmlspecialchars($hosts->name), 'hostid' => $hosts->id);
}
}
$children['children'][] = $subChildren;
}
$treeMap[] = $children;
}
return $treeMap;
}
/**
* Get tree map by group.
*
* @access public
* @return string
*/
public function getGroupTreemap()
{
/* Get host list by group. */
$this->app->loadLang('serverroom');
$hostGroups = $this->dao->select('id,name,`group`,extranet')->from(TABLE_HOST)
->where('deleted')->eq(0)
->andWhere('type')->eq('normal')
->fetchGroup('group', 'id');
/* Get module list by host group. */
$modules = $this->dao->select('*')->from(TABLE_MODULE)->where('id')->in(array_keys($hostGroups))->fetchAll();
$paths = array();
foreach($modules as $module)
{
foreach(explode(',', trim($module->path)) as $path) $paths[$path] = $path;
}
$modules = $this->dao->select('*')->from(TABLE_MODULE)
->where('id')->in($paths)
->orderBy('grade_desc,`order`,id')
->fetchAll();
$treemap = array();
foreach($modules as $module)
{
if(!isset($treemap[$module->parent])) $treemap[$module->parent] = array('text' => '', 'collapsed' => false, 'children' => array());
$treemap[$module->parent]['text'] = htmlspecialchars($module->name);
if(isset($treemap[$module->id]))
{
$treemap[$module->parent]['children'][] = $treemap[$module->id];
unset($treemap[$module->id]);
}
if(isset($hostGroups[$module->id]))
{
$treemap[$module->id] = array('text' => $module->name, 'collapsed' => false, 'children' => array());
foreach($hostGroups[$module->id] as $host)
{
$treemap[$module->id]['children'][] = array('text' => htmlspecialchars($host->name), 'hostid' => $host->id);
}
}
}
if(isset($treemap[0]) or isset($hostGroups[0]))
{
if(isset($treemap[0])) $groupTree = $treemap[0];
if(isset($hostGroups[0]))
{
foreach($hostGroups[0] as $host) $groupTree['children'][] = array('text' => htmlspecialchars($host->name), 'hostid' => $host->id);
}
$treemap[0] = $groupTree;
}
$treeData['text'] = '/';
$treeData['collapsed'] = false;
$treeData['children'] = array_values($treemap);
return $treeData;
}
/**
* 判断列表页操作按钮是否可点击。
* Judge an action is clickable or not.
*
* @param object $host
* @param string $action
* @static
* @access public
* @return bool
*/
public static function isClickable($host, $action)
{
if(!$host->id) return false;
if (!common::hasPriv('host', 'changeStatus')) return false;
if($host->status == 'online' && $action == 'online') return false;
if($host->status == 'offline' && $action == 'offline') return false;
return true;
}
}