* @package api * @version $Id: control.php 5143 2013-07-15 06:11:59Z thanatos thanatos915@163.com $ * @link http://www.zentao.net */ class api extends control { public function __construct($moduleName = '', $methodName = '', $appName = '') { parent::__construct($moduleName, $methodName, $appName); $this->group = $this->loadModel('group'); $this->user = $this->loadModel('user'); $this->doc = $this->loadModel('doc'); $this->action = $this->loadModel('action'); $this->tree = $this->loadModel('tree'); $this->api = $this->loadModel('api'); } /** * Api doc index page. * * @param int $libID * @param int $moduleID * @param int $apiID * @access public * @return void */ public function index($libID = 0, $moduleID = 0, $apiID = 0, $version = 0) { /* Get all api doc libraries. */ $libs = $this->doc->getApiLibs(); /* Generate bread crumbs dropMenu. */ if($libs) { if($libID == 0) $libID = key($libs); $this->lang->modulePageNav = $this->generateLibsDropMenu($libs, $libID); } $this->setMenu($libID); /* Get an api doc. */ if($apiID > 0) { $api = $this->api->getLibById($apiID, $version); if($api) { $moduleID = $api->module; $libID = $api->lib; $this->view->api = $api; $this->view->apiID = $apiID; $this->view->version = $version; $this->view->actions = $apiID ? $this->action->getList('api', $apiID) : array(); } } else { /* Get module api list. */ $apiList = $this->api->getListByModuleId($libID, $moduleID); $this->view->apiList = $apiList; } $this->view->title = $this->lang->api->title; $this->view->libID = $libID; $this->view->apiID = $apiID; $this->view->libs = $libs; $this->view->moduleTree = $libID ? $this->doc->getApiModuleTree($libID, $apiID) : ''; $this->view->users = $this->user->getPairs('noclosed,noletter'); $this->display(); } /** * Create a api doc library. * * @access public * @return void */ public function createLib() { if(!empty($_POST)) { $lib = fixer::input('post') ->join('groups', ',') ->join('users', ',') ->get(); if($lib->acl == 'private') $lib->users = $this->app->user->account; /* save api doc library */ $libID = $this->doc->createApiLib($lib); if(dao::isError()) { $this->sendError(dao::getError()); exit; } $this->action->create('docLib', $libID, 'Created'); /* save doc library success */ $this->sendSuccess([ 'locate' => $this->createLink('api', 'index', "libID=$libID"), ]); exit; } $this->view->groups = $this->group->getPairs(); $this->view->users = $this->user->getPairs('nocode'); $this->display(); } /** * Edit an api doc library * * @param $id * @access public * @return void */ public function editLib($id) { $doc = $this->doc->getLibById($id); if(!empty($_POST)) { $lib = fixer::input('post') ->join('groups', ',') ->join('users', ',') ->get(); if($lib->acl == 'private') $lib->users = $this->app->user->account; $this->doc->updateApiLib($id, $doc, $lib); if(dao::isError()) { $this->sendError(dao::getError()); exit; } $res = array( 'message' => $this->lang->saveSuccess, 'closeModal' => true, 'callback' => "redirectParentWindow($id)", ); return $this->sendSuccess($res); } $this->view->doc = $doc; $this->view->groups = $this->group->getPairs(); $this->view->users = $this->user->getPairs('nocode'); $this->display(); } /** * Edit library. * * @param int $apiID * @access public * @return void */ public function edit($apiID) { if(helper::isAjaxRequest() && !empty($_POST)) { $this->loadModel('api'); $now = helper::now(); $userId = $this->app->user->account; $params = fixer::input('post') ->add('addedBy', $userId) ->add('addedDate', $now) ->add('editedBy', $userId) ->add('editedDate', $now) ->setDefault('product,module', 0) ->json('params,response') ->get(); $this->api->update($apiID, $params); if(dao::isError()) { $this->sendError(dao::getError()); exit; } $this->action->create('api', $apiID, 'Edited'); $this->sendSuccess([ 'locate' => helper::createLink('api', 'index', "libID=0&moduleID=0&apiID=$apiID"), ]); exit; } $api = $this->api->getLibById($apiID); if($api) { $this->view->api = $api; $this->view->edit = true; } $example = array('example' => 'type,description'); $example = json_encode($example, JSON_PRETTY_PRINT); $allUsers = $this->loadModel('user')->getPairs('devfirst|noclosed'); $this->view->user = $this->app->user->account; $this->view->allUsers = $allUsers; $this->view->moduleOptionMenu = $this->tree->getOptionMenu($api->lib, 'api', $startModuleID = 0); $this->view->moduleID = $api->module ? (int)$api->module : (int)$this->cookie->lastDocModule; $this->view->example = $example; $this->view->title = $api->title . $this->lang->api->edit; $this->display(); } /** * Create an api doc. * * @param int $libID * @param int $moduleID * @access public * @return void */ public function create($libID, $moduleID = 0) { if(!empty($_POST)) { $now = helper::now(); $userId = $this->app->user->account; $params = fixer::input('post') ->add('addedBy', $userId) ->add('addedDate', $now) ->add('editedBy', $userId) ->add('editedDate', $now) ->add('version', 1) ->setDefault('product,module', 0) ->unHtml('params,response') ->json('params,response') ->get(); $apiID = $this->api->create($params); if(empty($apiID)) return $this->sendError(dao::getError()); $this->action->create('api', $apiID, 'Created'); return $this->sendSuccess(array('locate' => helper::createLink('api', 'index', "libID=0&moduleID=0&apiID=$apiID"))); } $libs = $this->doc->getLibs('api', '', $libID); if(!$libID and !empty($libs)) $libID = key($libs); $lib = $this->doc->getLibByID($libID); $libName = isset($lib->name) ? $lib->name . $this->lang->colon : ''; $example = array('example' => 'type,description'); $example = json_encode($example, JSON_PRETTY_PRINT); $allUsers = $this->loadModel('user')->getPairs('devfirst|noclosed'); $this->view->user = $this->app->user->account; $this->view->allUsers = $allUsers; $this->view->libID = $libID; $this->view->libName = $lib->name; $this->view->moduleOptionMenu = $this->tree->getOptionMenu($libID, 'api', $startModuleID = 0); $this->view->moduleID = $moduleID ? (int)$moduleID : (int)$this->cookie->lastDocModule; $this->view->libs = $libs; $this->view->example = $example; $this->view->title = $libName . $this->lang->api->create; $this->view->users = $this->user->getPairs('nocode'); $this->display(); } /** * @param $apiID * @param string $confirm * @author thanatos thanatos915@163.com */ public function delete($apiID, $confirm = 'no') { if($confirm == 'no') { $tips = $this->lang->api->confirmDelete; die(js::confirm($tips, inlink('delete', "apiID=$apiID&confirm=yes"))); } else { $api = $this->api->getLibById($apiID); $this->api->delete(TABLE_API, $apiID); if(dao::isError()) { $this->sendError(dao::getError()); } else { $this->sendSuccess([ 'locate' => $this->createLink('api', 'index', "libID=$api->lib&module=$api->module"), ]); } } } /** * Get params type options by scope * * @param string $scope the params position * @author thanatos thanatos915@163.com */ public function ajaxGetParamsTypeOptions($scope) { if(empty($scope)) die(); $options = array(); if($scope == apiModel::SCOPE_BODY) { $options = $this->lang->api->allParamsTypeOptions; } else { $options = $this->lang->api->paramsTypeOptions; } echo html::select('paramsTypeOptions', $options, '', "class='form-control' onchange='changeType(this);'"); exit; } /** * Set doc menu by method name. * * @author thanatos thanatos915@163.com */ private function setMenu($libID = 0) { $menu = ''; // page of index menu if(intval($libID) > 0) { $menu = "