* @package doc * @version $Id: control.php 933 2010-07-06 06:53:40Z wwccss $ * @link http://www.zentao.net */ class doc extends control { /** * Construct function, load user, tree, action auto. * * @access public * @return void */ public function __construct($moduleName = '', $methodName = '') { parent::__construct($moduleName, $methodName); $this->loadModel('user'); $this->loadModel('tree'); $this->loadModel('action'); $this->loadModel('product'); $this->loadModel('project'); $this->from = $this->cookie->from ? $this->cookie->from : 'doc'; $this->productID = $this->cookie->product ? $this->cookie->product : '0'; } /** * Go to browse page. * * @access public * @return void */ public function index() { $this->from = 'doc'; setcookie('from', 'doc', $this->config->cookieLife, $this->config->webRoot, '', false, true); $this->doc->setMenu(); $this->session->set('docList', $this->app->getURI(true)); $this->app->loadClass('pager', $static = true); $pager = new pager(0, 5, 1); $this->lang->TRActions = $this->doc->setFastMenu($this->lang->doc->fast); $this->lang->TRActions .= common::hasPriv('doc', 'createLib') ? html::a(helper::createLink('doc', 'createLib'), " " . $this->lang->doc->createLib, '', "class='btn btn-secondary iframe' data-width='70%'") : ''; $this->lang->TRActions .= common::hasPriv('doc', 'create') ? $this->doc->buildCreateButton4Doc() : ''; $actionURL = $this->createLink('doc', 'browse', "lib=0&browseType=bySearch&queryID=myQueryID"); $this->doc->buildSearchForm(0, array(), 0, $actionURL, 'index'); $this->view->title = $this->lang->doc->common . $this->lang->colon . $this->lang->doc->index; $this->view->position[] = $this->lang->doc->index; $this->view->latestEditedDocs = $this->loadModel('doc')->getDocsByBrowseType(0, 'byediteddate', 0, 0, 'editedDate_desc, id_desc', $pager); $this->view->myDocs = $this->loadModel('doc')->getDocsByBrowseType(0, 'openedbyme', 0, 0, 'addedDate_desc', $pager); $this->view->statisticInfo = $this->doc->getStatisticInfo(); $this->view->users = $this->loadModel('user')->getPairs('noletter'); $this->view->doingProjects = $this->loadModel('project')->getList('undone', 5); $this->display(); } /** * Browse docs. * * @param string|int $libID product|project or the int id of custom library * @param int $moduleID * @param int $productID * @param int $projectID * @param string $orderBy * @param int $recTotal * @param int $recPerPage * @param int $pageID * @access public * @return void */ public function browse($libID = 0, $browseType = 'all', $param = 0, $orderBy = 'id_desc', $from = 'doc', $recTotal = 0, $recPerPage = 20, $pageID = 1) { $this->from = $from; setcookie('from', $from, $this->config->cookieLife, $this->config->webRoot, '', false, true); $this->loadModel('search'); /* Set browseType.*/ $browseType = strtolower($browseType); $queryID = ($browseType == 'bysearch') ? (int)$param : 0; $moduleID = ($browseType == 'bymodule') ? (int)$param : 0; $type = ''; $productID = 0; $projectID = 0; if($libID) { $lib = $this->doc->getLibByID($libID); $type = $lib->type; $productID = $lib->product; $projectID = $lib->project; if($type != 'product' and $type != 'project') $from = 'doc'; } $this->libs = $this->doc->getLibs($type, '', $libID); /* According the from, set menus. */ if($from == 'product') { $this->lang->doc->menu = $this->lang->product->menu; $this->lang->doc->menuOrder = $this->lang->product->menuOrder; $this->product->setMenu($this->product->getPairs(), $lib->product); $this->lang->set('menugroup.doc', 'product'); } elseif($from == 'project') { $this->lang->doc->menu = $this->lang->project->menu; $this->lang->doc->menuOrder = $this->lang->project->menuOrder; $this->project->setMenu($this->project->getPairs('nocode'), $lib->project); $this->lang->set('menugroup.doc', 'project'); } else { $menuType = (!$type && (in_array($browseType, array_keys($this->lang->doc->fastMenuList)) || $browseType == 'bysearch')) ? $browseType : $type; $this->doc->setMenu($menuType, $libID, $moduleID, $productID, $projectID); } $this->session->set('docList', $this->app->getURI(true)); /* Set header and position. */ $this->view->title = $this->lang->doc->common . ($libID ? $this->lang->colon . $this->libs[$libID] : ''); $this->view->position[] = $libID ? $this->libs[$libID] : ''; /* Load pager. */ $this->app->loadClass('pager', $static = true); $pager = new pager($recTotal, $recPerPage, $pageID); /* Append id for secend sort. */ $sort = $this->loadModel('common')->appendOrder($orderBy); /* Build the search form. */ $actionURL = $this->createLink('doc', 'browse', "lib=$libID&browseType=bySearch&queryID=myQueryID&orderBy=$orderBy&from=$from"); $this->doc->buildSearchForm($libID, $this->libs, $queryID, $actionURL, $type); $title = ''; $module = $moduleID ? $this->loadModel('tree')->getByID($moduleID) : ''; if($module) $title = $module->name; if($libID) $title = html::a(helper::createLink('doc', 'browse', "libID=$libID"), $this->libs[$libID], ''); if(in_array($browseType, array_keys($this->lang->doc->fastMenuList))) $title = $this->lang->doc->fastMenuList[$browseType]; if($browseType == 'bysearch') $title = $this->lang->doc->search; if($param != 0) $title = $this->doc->buildCrumbTitle($libID, $param, $title); if($browseType == 'fastsearch') { if($this->post->searchDoc) $this->session->set('searchDoc', $this->post->searchDoc); $title = '"' . $this->session->searchDoc . '" ' . $this->lang->doc->searchResult; } else { $this->session->set('searchDoc', ''); } $libs = array(); if($browseType == 'collectedbyme') { $libs = $this->doc->getAllLibsByType('collector'); $this->view->itemCounts = $this->doc->statLibCounts(array_keys($libs)); } $attachLibs = array(); if(!empty($lib) and (!empty($lib->product) or !empty($lib->project)) and $browseType != 'bymodule') { $count = $this->dao->select('count(*) as count')->from(TABLE_DOCLIB)->where('project')->eq($lib->project)->andWhere('product')->eq($lib->product)->fetch('count'); if($count == 1 and $type and isset($lib->$type)) { $objectLibs = $this->doc->getLibsByObject($type, $lib->$type); if(isset($objectLibs['project'])) $attachLibs['project'] = $objectLibs['project']; if(isset($objectLibs['files'])) $attachLibs['files'] = $objectLibs['files']; } } $this->view->breadTitle = $title; $this->view->libID = $libID; $this->view->moduleID = $moduleID; $this->view->modules = $this->doc->getDocMenu($libID, $moduleID, '`order`', $browseType); $this->view->docs = $this->doc->getDocsByBrowseType($libID, $browseType, $queryID, $moduleID, $sort, $pager); $this->view->attachLibs = $attachLibs; $this->view->users = $this->loadModel('user')->getPairs('noletter'); $this->view->orderBy = $orderBy; $this->view->browseType = $browseType; $this->view->param = $param; $this->view->type = $type; $this->view->from = $from; $this->view->pager = $pager; $this->view->libs = $libs; $this->view->currentLib = $libID ? $lib : ''; $this->display(); } /** * Create a library. * * @param string $type * @param int $objectID * @access public * @return void */ public function createLib($type = '', $objectID = 0) { if(!empty($_POST)) { $libID = $this->doc->createLib(); if(!dao::isError()) { $this->loadModel('action')->create('docLib', $libID, 'Created'); die(js::locate($this->createLink($this->moduleName, 'browse', "libID=$libID"), 'parent.parent')); } else { echo js::error(dao::getError()); } } $libTypeList = $this->lang->doc->libTypeList; $products = $this->product->getPairs('nocode'); $projects = $this->project->getPairs('nocode'); if(empty($products)) unset($libTypeList['product']); if(empty($projects)) unset($libTypeList['project']); $this->view->groups = $this->loadModel('group')->getPairs(); $this->view->users = $this->user->getPairs('nocode'); $this->view->products = $products; $this->view->projects = $projects; $this->view->type = $type; $this->view->libTypeList = $libTypeList; $this->view->objectID = $objectID; die($this->display()); } /** * Edit a library. * * @param int $libID * @access public * @return void */ public function editLib($libID) { if(!empty($_POST)) { $changes = $this->doc->updateLib($libID); if(dao::isError()) die(js::error(dao::getError())); if($changes) { $actionID = $this->loadModel('action')->create('docLib', $libID, 'edited'); $this->action->logHistory($actionID, $changes); } die(js::locate($this->createLink($this->moduleName, 'browse', "libID=$libID"), 'parent.parent')); } $lib = $this->doc->getLibByID($libID); if(!empty($lib->product)) $this->view->product = $this->dao->select('id,name')->from(TABLE_PRODUCT)->where('id')->eq($lib->product)->fetch(); if(!empty($lib->project)) $this->view->project = $this->dao->select('id,name')->from(TABLE_PROJECT)->where('id')->eq($lib->project)->fetch(); $this->view->lib = $lib; $this->view->groups = $this->loadModel('group')->getPairs(); $this->view->users = $this->user->getPairs('noletter', $lib->users); $this->view->libID = $libID; die($this->display()); } /** * Delete a library. * * @param int $libID * @param string $confirm yes|no * @access public * @return void */ public function deleteLib($libID, $confirm = 'no') { if($libID == 'product' or $libID == 'project') die(); if($confirm == 'no') { die(js::confirm($this->lang->doc->confirmDeleteLib, $this->createLink('doc', 'deleteLib', "libID=$libID&confirm=yes"))); } else { $lib = $this->doc->getLibByID($libID); if(!empty($lib->main)) die(js::alert($this->lang->doc->errorMainSysLib)); $this->doc->delete(TABLE_DOCLIB, $libID); die(js::locate($this->createLink('doc', 'browse'), 'parent')); } } /** * Create a doc. * * @param int|string $libID * @param int $moduleID * @param int $productID * @param int $projectID * @param string $from * @access public * @return void */ public function create($libID, $moduleID = 0, $docType = '') { if(!empty($_POST)) { setcookie('lastDocModule', (int)$this->post->module, $this->config->cookieLife, $this->config->webRoot, '', false, false); $docResult = $this->doc->create(); if(!$docResult or dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError())); $docID = $docResult['id']; $files = $docResult['files']; $lib = $this->doc->getLibByID($this->post->lib); if($docResult['status'] == 'exists') { $this->send(array('result' => 'fail', 'message' => sprintf($this->lang->duplicate, $this->lang->doc->common), 'locate' => $this->createLink('doc', 'view', "docID=$docID"))); } $fileAction = ''; if(!empty($files)) $fileAction = $this->lang->addFiles . join(',', $files) . "\n" ; $this->action->create('doc', $docID, 'Created', $fileAction); $vars = "libID=" . (int)$this->post->lib . "&browseType=byModule&moduleID=" . (int)$this->post->module . "&orderBy=id_desc&from=$this->from"; $link = $this->createLink('doc', 'browse', $vars); if($this->app->getViewType() == 'xhtml') $link = $this->createLink('doc', 'view', "docID=$docID"); $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $link)); } $lib = $this->doc->getLibByID($libID); $type = $lib->type; /* According the from, set menus. */ if($this->from == 'product') { $this->lang->doc->menu = $this->lang->product->menu; $this->lang->doc->menuOrder = $this->lang->product->menuOrder; $this->product->setMenu($this->product->getPairs(), $lib->product); $this->lang->set('menugroup.doc', 'product'); $this->lang->TRActions = common::hasPriv('doc', 'createLib') ? html::a(helper::createLink('doc', 'createLib'), " " . $this->lang->doc->createLib, '', "class='btn btn-secondary iframe' data-width='70%'") : ''; } elseif($this->from == 'project') { $this->lang->doc->menu = $this->lang->project->menu; $this->lang->doc->menuOrder = $this->lang->project->menuOrder; $this->project->setMenu($this->project->getPairs('nocode'), $lib->project); $this->lang->set('menugroup.doc', 'project'); $this->lang->TRActions = common::hasPriv('doc', 'createLib') ? html::a(helper::createLink('doc', 'createLib'), " " . $this->lang->doc->createLib, '', "class='btn btn-secondary iframe' data-width='70%'") : ''; } else { $this->doc->setMenu($type, $libID, $moduleID, $lib->product, $lib->project); } $this->view->title = $lib->name . $this->lang->colon . $this->lang->doc->create; $this->view->position[] = html::a($this->createLink('doc', 'browse', "libID=$libID"), $lib->name); $this->view->position[] = $this->lang->doc->create; $unclosed = strpos($this->config->doc->custom->showLibs, 'unclosed') !== false ? 'unclosedProject' : ''; $this->view->libID = $libID; $this->view->libs = $this->doc->getLibs($type = 'all', $extra = "withObject,$unclosed", $libID); $this->view->libName = $this->dao->findByID($libID)->from(TABLE_DOCLIB)->fetch('name'); $this->view->moduleOptionMenu = $this->tree->getOptionMenu($libID, 'doc', $startModuleID = 0); $this->view->moduleID = $moduleID ? (int)$moduleID : (int)$this->cookie->lastDocModule; $this->view->type = $type; $this->view->docType = $docType; $this->view->groups = $this->loadModel('group')->getPairs(); $this->view->users = $this->user->getPairs('nocode|noclosed|nodeleted'); $this->display(); } /** * Edit a doc. * * @param int $docID * @access public * @return void */ public function edit($docID, $comment = false) { if(!empty($_POST)) { if($comment == false) { $result = $this->doc->update($docID); if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError())); $changes = $result['changes']; $files = $result['files']; } if($this->post->comment != '' or !empty($changes) or !empty($files)) { $action = !empty($changes) ? 'Edited' : 'Commented'; $fileAction = ''; if(!empty($files)) $fileAction = $this->lang->addFiles . join(',', $files) . "\n" ; $actionID = $this->action->create('doc', $docID, $action, $fileAction . $this->post->comment); if(!empty($changes)) $this->action->logHistory($actionID, $changes); } $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->createLink('doc', 'view', "docID=$docID"))); } /* Get doc and set menu. */ $doc = $this->doc->getById($docID); $libID = $doc->lib; if($doc->contentType == 'markdown') $this->config->doc->markdown->edit = array('id' => 'content', 'tools' => 'toolbar'); $lib = $this->doc->getLibByID($libID); $type = $lib->type; $this->doc->setMenu($type, $libID, $doc->module, $lib->product, $lib->project); $this->view->title = $lib->name . $this->lang->colon . $this->lang->doc->edit; $this->view->position[] = html::a($this->createLink('doc', 'browse', "libID=$libID"), $lib->name); $this->view->position[] = $this->lang->doc->edit; $this->view->doc = $doc; $this->view->moduleOptionMenu = $this->tree->getOptionMenu($libID, 'doc', $startModuleID = 0); $this->view->type = $type; $this->view->libs = $this->doc->getLibs($type = 'all', $extra = 'withObject'); $this->view->groups = $this->loadModel('group')->getPairs(); $this->view->users = $this->user->getPairs('noletter|noclosed|nodeleted', $doc->users); $this->display(); } /** * View a doc. * * @param int $docID * @param int $version * @access public * @return void */ public function view($docID, $version = 0) { /* Get doc. */ $doc = $this->doc->getById($docID, $version, true); if(!$doc) die(js::error($this->lang->notFound) . js::locate('back')); if($doc->contentType == 'markdown') { $hyperdown = $this->app->loadClass('hyperdown'); $doc->content = $hyperdown->makeHtml($doc->content); /* Add a newline attribute to the table. */ $doc->content = str_replace("