* @package api * @version $Id$ * @link http://www.zentao.net */ class apiModel extends model { const STATUS_DOING = 'doing'; const STATUS_DONE = 'done'; const STATUS_HIDDEN = 'hidden'; const SCOPE_QUERY = 'query'; const SCOPE_FORM_DATA = 'formData'; const SCOPE_PATH = 'path'; const SCOPE_BODY = 'body'; const SCOPE_HEADER = 'header'; const SCOPE_COOKIE = 'cookie'; const PARAMS_TYPE_CUSTOM = 'custom'; /** * Create an api doc. * * @param stdClass $params * @return int * @author thanatos thanatos915@163.com */ public function create($params) { $this->dao->insert(TABLE_API)->data($params) ->autoCheck() ->batchCheck($this->config->api->create->requiredFields, 'notempty') ->exec(); if(dao::isError()) return false; return $this->dao->lastInsertID(); } /** * Create a global struct * * @param $data * @access public * @return int */ public function createStruct($data) { $this->dao->insert(TABLE_APISTRUCT)->data($data) ->autoCheck() ->batchCheck($this->config->api->struct->requiredFields, 'notempty') ->exec(); return dao::isError() ? 0 : $this->dao->lastInsertID(); } /** * Update a struct. * * @param $id * @param $data * @return array */ public function updateStruct($id, $data) { $old = $this->dao->findByID($id)->from(TABLE_APISTRUCT)->fetch(); unset($data->addedBy); unset($data->addedDate); $this->dao->update(TABLE_APISTRUCT) ->data($data)->autoCheck() ->batchCheck($this->config->api->struct->requiredFields, 'notempty') ->where('id')->eq($id) ->exec(); if(dao::isError()) return false; return common::createChanges($old, $data); } /** * Update an api doc. * * @param $id * @param $data * @author thanatos thanatos915@163.com */ public function update($id, $data) { $oldApi = $this->dao->findByID($id)->from(TABLE_API)->fetch(); $data->id = $oldApi->id; $data->version = $oldApi->version + 1; $apiSpec = $this->getApiSpecByData($data); $this->dao->replace(TABLE_API_SPEC)->data($apiSpec)->exec(); unset($data->id); $this->dao->update(TABLE_API) ->data($data) ->autoCheck() ->batchCheck($this->config->api->edit->requiredFields, 'notempty') ->where('id')->eq($id) ->exec(); return; } /** * Get struct list by api doc id. * * @param $id * @access public * @return array */ public function getStructListByLibID($id) { $res = $this->dao->select('*') ->from(TABLE_APISTRUCT) ->where('lib')->eq($id) ->fetchAll(); array_map(function ($item) { $item->attribute = json_decode(htmlspecialchars_decode($item->attribute), true); return $item; }, $res); return $res; } /** * Get a struct info. * * @param $id * @access public * @return stdClass */ public function getStructByID($id) { $model = $this->dao->select('*') ->from(TABLE_APISTRUCT) ->where('id')->eq($id) ->fetch(); if($model) $model->attribute = json_decode(htmlspecialchars_decode($model->attribute), true); return $model; } /** * Get api doc by id. * * @param int $id * @access public * @return object */ public function getLibById($id, $version = 0) { if($version) { $fields = 'spec.*,api.id,api.product,api.lib,api.version,doc.name as libName,module.name as moduleName'; } else { $fields = 'api.*,doc.name as libName,module.name as moduleName'; } $model = $this->dao ->select($fields) ->from(TABLE_API)->alias('api') ->beginIF($version)->leftJoin(TABLE_API_SPEC)->alias('spec')->on('api.id = spec.doc')->fi() ->leftJoin(TABLE_DOCLIB)->alias('doc')->on('api.lib = doc.id') ->leftJoin(TABLE_MODULE)->alias('module')->on('api.module = module.id') ->where('api.id')->eq($id) ->beginIF($version)->andWhere('spec.version')->eq($version)->fi() ->fetch(); if($model) { $model->params = json_decode(htmlspecialchars_decode($model->params), true); $model->response = json_decode(htmlspecialchars_decode($model->response), true); } return $model; } /** * Get api doc list by module id * * @param int $libID * @param int $moduleID * @return array $list * @author thanatos thanatos915@163.com */ public function getListByModuleId($libID = 0, $moduleID = 0) { if($moduleID > 0) { $sub = $this->dao->select('id')->from(TABLE_MODULE)->where('FIND_IN_SET(' . $moduleID . ', path)')->processSQL(); $where = 'module in (' . $sub . ')'; } else { $where = 'lib = ' . $libID; } $list = $this->dao->select('*') ->from(TABLE_API) ->where($where) ->andWhere('deleted')->eq(0) ->fetchAll(); array_map(function ($item) { $item->params = json_decode(htmlspecialchars_decode($item->params), true); return $item; }, $list); return $list; } /** * Get status text by status. * * @param $status * @return string * @author thanatos thanatos915@163.com */ public static function getApiStatusText($status) { global $lang; switch($status) { case static::STATUS_DOING: return $lang->api->doing; case static::STATUS_DONE: return $lang->api->done; } } /** * Get struct tree by lib id * * @param int $libID * @param int $structID * @return string * @access public */ public function getStructTreeByLib($libID = 0, $structID = 0) { $list = $this->getStructListByLibID($libID); $html = "