2014-01-24 16:41:37 +01:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-01-24 16:41:37 +01:00
|
|
|
|
2015-08-27 14:36:26 +02:00
|
|
|
namespace Icinga\Module\Doc\Controllers;
|
|
|
|
|
2014-01-24 16:41:37 +01:00
|
|
|
use Icinga\Application\Icinga;
|
2014-05-27 14:53:25 +02:00
|
|
|
use Icinga\Module\Doc\DocController;
|
2014-06-30 15:48:43 +02:00
|
|
|
use Icinga\Module\Doc\Exception\DocException;
|
2014-01-24 16:41:37 +01:00
|
|
|
|
2015-08-27 14:36:26 +02:00
|
|
|
class ModuleController extends DocController
|
2014-01-24 16:41:37 +01:00
|
|
|
{
|
2014-12-09 14:30:10 +01:00
|
|
|
/**
|
|
|
|
* Get the path to a module documentation
|
|
|
|
*
|
2015-07-28 13:59:59 +02:00
|
|
|
* @param string $module The name of the module
|
|
|
|
* @param string $default The default path
|
|
|
|
* @param bool $suppressErrors Whether to not throw an exception if the module documentation is not available
|
2014-12-09 14:30:10 +01:00
|
|
|
*
|
2015-07-28 13:59:59 +02:00
|
|
|
* @return string|null Path to the documentation or null if the module documentation is not available
|
|
|
|
* and errors are suppressed
|
2014-12-09 14:30:10 +01:00
|
|
|
*
|
2015-07-28 13:59:59 +02:00
|
|
|
* @throws \Icinga\Exception\Http\HttpNotFoundException If the module documentation is not available and errors
|
|
|
|
* are not suppressed
|
2014-12-09 14:30:10 +01:00
|
|
|
*/
|
|
|
|
protected function getPath($module, $default, $suppressErrors = false)
|
|
|
|
{
|
2014-12-09 14:55:24 +01:00
|
|
|
if (is_dir($default)) {
|
|
|
|
return $default;
|
|
|
|
}
|
2014-12-09 14:30:10 +01:00
|
|
|
if (($path = $this->Config()->get('documentation', 'modules')) !== null) {
|
|
|
|
$path = str_replace('{module}', $module, $path);
|
|
|
|
if (is_dir($path)) {
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($suppressErrors) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-07-28 13:47:06 +02:00
|
|
|
$this->httpNotFound($this->translate('Documentation for module \'%s\' is not available'), $module);
|
2014-12-09 14:30:10 +01:00
|
|
|
}
|
|
|
|
|
2014-01-24 16:41:37 +01:00
|
|
|
/**
|
2014-06-30 11:43:25 +02:00
|
|
|
* List modules which are enabled and having the 'doc' directory
|
2014-01-24 16:41:37 +01:00
|
|
|
*/
|
|
|
|
public function indexAction()
|
|
|
|
{
|
2014-06-30 11:43:25 +02:00
|
|
|
$moduleManager = Icinga::app()->getModuleManager();
|
|
|
|
$modules = array();
|
2015-07-27 15:19:32 +02:00
|
|
|
foreach ($moduleManager->listInstalledModules() as $module) {
|
2014-12-09 14:30:10 +01:00
|
|
|
$path = $this->getPath($module, $moduleManager->getModuleDir($module, '/doc'), true);
|
|
|
|
if ($path !== null) {
|
2015-07-27 15:19:32 +02:00
|
|
|
$modules[] = $moduleManager->getModule($module, false);
|
2014-06-30 11:43:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->view->modules = $modules;
|
2014-01-24 16:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-27 15:19:32 +02:00
|
|
|
* Assert that the given module is installed
|
2014-01-24 16:41:37 +01:00
|
|
|
*
|
2015-07-27 15:19:32 +02:00
|
|
|
* @param string $moduleName
|
2014-01-24 16:41:37 +01:00
|
|
|
*
|
2015-07-28 13:59:59 +02:00
|
|
|
* @throws \Icinga\Exception\Http\HttpNotFoundException If the given module is not installed
|
2014-01-24 16:41:37 +01:00
|
|
|
*/
|
2015-07-27 15:19:32 +02:00
|
|
|
protected function assertModuleInstalled($moduleName)
|
2014-01-24 16:41:37 +01:00
|
|
|
{
|
2014-05-23 14:06:28 +02:00
|
|
|
$moduleManager = Icinga::app()->getModuleManager();
|
2014-05-27 14:53:25 +02:00
|
|
|
if (! $moduleManager->hasInstalled($moduleName)) {
|
2015-07-28 13:47:06 +02:00
|
|
|
$this->httpNotFound($this->translate('Module \'%s\' is not installed'), $moduleName);
|
2014-05-27 14:53:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-28 19:17:03 +02:00
|
|
|
* View the toc of a module's documentation
|
|
|
|
*
|
2015-07-28 13:59:59 +02:00
|
|
|
* @throws \Icinga\Exception\MissingParameterException If the required parameter 'moduleName' is empty
|
|
|
|
* @throws \Icinga\Exception\Http\HttpNotFoundException If the given module is not installed
|
|
|
|
* @see assertModuleInstalled()
|
2014-05-27 14:53:25 +02:00
|
|
|
*/
|
|
|
|
public function tocAction()
|
|
|
|
{
|
2015-07-28 13:59:59 +02:00
|
|
|
$module = $this->params->getRequired('moduleName');
|
2015-07-27 15:19:32 +02:00
|
|
|
$this->assertModuleInstalled($module);
|
2014-12-09 14:30:10 +01:00
|
|
|
$this->view->moduleName = $module;
|
2014-06-30 15:48:43 +02:00
|
|
|
try {
|
2014-12-09 14:30:57 +01:00
|
|
|
$this->renderToc(
|
2014-12-09 14:30:10 +01:00
|
|
|
$this->getPath($module, Icinga::app()->getModuleManager()->getModuleDir($module, '/doc')),
|
|
|
|
$module,
|
2014-07-28 19:17:03 +02:00
|
|
|
'doc/module/chapter',
|
2014-12-09 14:30:10 +01:00
|
|
|
array('moduleName' => $module)
|
2014-07-28 19:17:03 +02:00
|
|
|
);
|
2014-06-30 15:48:43 +02:00
|
|
|
} catch (DocException $e) {
|
2015-07-28 13:47:06 +02:00
|
|
|
$this->httpNotFound($e->getMessage());
|
2014-06-30 15:48:43 +02:00
|
|
|
}
|
2014-01-24 16:41:37 +01:00
|
|
|
}
|
2014-05-27 15:09:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* View a chapter of a module's documentation
|
|
|
|
*
|
2015-07-28 13:59:59 +02:00
|
|
|
* @throws \Icinga\Exception\MissingParameterException If one of the required parameters 'moduleName' and
|
|
|
|
* 'chapter' is empty
|
|
|
|
* @throws \Icinga\Exception\Http\HttpNotFoundException If the given module is not installed
|
2015-07-27 15:19:32 +02:00
|
|
|
* @see assertModuleInstalled()
|
2014-05-27 15:09:01 +02:00
|
|
|
*/
|
|
|
|
public function chapterAction()
|
|
|
|
{
|
2015-07-28 13:59:59 +02:00
|
|
|
$module = $this->params->getRequired('moduleName');
|
2015-07-27 15:19:32 +02:00
|
|
|
$this->assertModuleInstalled($module);
|
2015-07-28 13:59:59 +02:00
|
|
|
$chapter = $this->params->getRequired('chapter');
|
2014-12-09 14:30:10 +01:00
|
|
|
$this->view->moduleName = $module;
|
2014-06-30 15:48:43 +02:00
|
|
|
try {
|
2014-12-09 14:30:57 +01:00
|
|
|
$this->renderChapter(
|
2014-12-09 14:30:10 +01:00
|
|
|
$this->getPath($module, Icinga::app()->getModuleManager()->getModuleDir($module, '/doc')),
|
2015-02-10 17:09:56 +01:00
|
|
|
$chapter,
|
2014-07-28 19:17:03 +02:00
|
|
|
'doc/module/chapter',
|
2014-12-09 14:30:10 +01:00
|
|
|
array('moduleName' => $module)
|
2014-07-28 19:17:03 +02:00
|
|
|
);
|
2014-06-30 15:48:43 +02:00
|
|
|
} catch (DocException $e) {
|
2015-07-28 13:47:06 +02:00
|
|
|
$this->httpNotFound($e->getMessage());
|
2014-06-30 15:48:43 +02:00
|
|
|
}
|
2014-05-27 15:09:01 +02:00
|
|
|
}
|
2014-07-28 19:17:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* View a module's documentation as PDF
|
|
|
|
*
|
2015-07-28 13:59:59 +02:00
|
|
|
* @throws \Icinga\Exception\MissingParameterException If the required parameter 'moduleName' is empty
|
|
|
|
* @throws \Icinga\Exception\Http\HttpNotFoundException If the given module is not installed
|
|
|
|
* @see assertModuleInstalled()
|
2014-07-28 19:17:03 +02:00
|
|
|
*/
|
|
|
|
public function pdfAction()
|
|
|
|
{
|
2015-07-28 13:59:59 +02:00
|
|
|
$module = $this->params->getRequired('moduleName');
|
2015-07-27 15:19:32 +02:00
|
|
|
$this->assertModuleInstalled($module);
|
2014-12-09 14:30:57 +01:00
|
|
|
$this->renderPdf(
|
2014-12-09 14:30:10 +01:00
|
|
|
$this->getPath($module, Icinga::app()->getModuleManager()->getModuleDir($module, '/doc')),
|
|
|
|
$module,
|
2014-07-28 19:17:03 +02:00
|
|
|
'doc/module/chapter',
|
2014-12-09 14:30:10 +01:00
|
|
|
array('moduleName' => $module)
|
2014-07-28 19:17:03 +02:00
|
|
|
);
|
|
|
|
}
|
2014-01-24 16:41:37 +01:00
|
|
|
}
|