2010-03-04 05:39:02 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2010-04-11 09:25:26 +02:00
|
|
|
* Documentation Viewer.
|
2010-03-04 05:39:02 +01:00
|
|
|
*
|
2010-06-24 16:22:41 +02:00
|
|
|
* Reads the bundled markdown files from documentation folders and displays the output (either
|
|
|
|
* via markdown or plain text)
|
|
|
|
*
|
|
|
|
* For more documentation on how to use this class see the documentation in /sapphiredocs/docs folder
|
|
|
|
*
|
|
|
|
* To view the documentation in the browser use:
|
|
|
|
*
|
|
|
|
* http://yoursite.com/dev/docs/ Which is locked to ADMIN only
|
|
|
|
*
|
|
|
|
* @todo - Add ability to have docs on the front end as the main site.
|
|
|
|
* - Fix Language Selector (enabling it troubles the handleRequest when submitting)
|
|
|
|
* - SS_HTTPRequest when we ask for 10 params it gives us 10. Could be 10 blank ones.
|
|
|
|
* It would mean I could save alot of code if it only gave back an array of size X
|
|
|
|
* up to a maximum of 10...
|
2010-03-04 05:39:02 +01:00
|
|
|
*
|
|
|
|
* @package sapphiredocs
|
|
|
|
*/
|
|
|
|
|
|
|
|
class DocumentationViewer extends Controller {
|
2010-06-24 16:22:41 +02:00
|
|
|
|
|
|
|
static $allowed_actions = array(
|
|
|
|
'LanguageForm',
|
|
|
|
'doLanguageForm',
|
|
|
|
'handleRequest',
|
|
|
|
'fr', // better way of handling this?
|
|
|
|
'en'
|
|
|
|
);
|
2010-03-04 05:39:02 +01:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
static $casting = array(
|
|
|
|
'Version' => 'Text',
|
|
|
|
'Lang' => 'Text',
|
|
|
|
'Module' => 'Text',
|
|
|
|
'LanguageTitle' => 'Text'
|
2010-03-04 05:39:02 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
function init() {
|
|
|
|
parent::init();
|
|
|
|
|
|
|
|
$canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
|
2010-04-11 09:25:26 +02:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
if(!$canAccess) return Security::permissionFailure($this);
|
2010-04-11 09:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-24 16:22:41 +02:00
|
|
|
* Handle the url parsing for the documentation. In order to make this
|
|
|
|
* user friendly this does some tricky things..
|
2010-04-11 09:25:26 +02:00
|
|
|
*
|
2010-06-24 16:22:41 +02:00
|
|
|
* The urls which should work
|
|
|
|
* / - index page
|
|
|
|
* /en/sapphire - the index page of sapphire (shows versions)
|
|
|
|
* /2.4/en/sapphire - the docs for 2.4 sapphire.
|
|
|
|
* /2.4/en/sapphire/installation/
|
|
|
|
*
|
|
|
|
* @return SS_HTTPResponse
|
2010-04-11 09:25:26 +02:00
|
|
|
*/
|
2010-06-24 16:22:41 +02:00
|
|
|
public function handleRequest(SS_HTTPRequest $request) {
|
|
|
|
|
|
|
|
$this->Version = $request->shift();
|
|
|
|
$this->Lang = $request->shift();
|
|
|
|
|
|
|
|
$this->Remaining = $request->shift(10);
|
|
|
|
|
|
|
|
DocumentationService::load_automatic_registration();
|
|
|
|
|
|
|
|
if(isset($this->Version)) {
|
|
|
|
// check to see if its a valid version. If its not a float then its not actually a version
|
|
|
|
// its actually a language and it needs to change. So this means we support 2 structures
|
|
|
|
// /2.4/en/sapphire/page and
|
|
|
|
// /en/sapphire/page which is a link to the latest one
|
2010-03-04 05:39:02 +01:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
if(!is_numeric($this->Version)) {
|
|
|
|
// not numeric so /en/sapphire/folder/page
|
|
|
|
if(isset($this->Lang) && $this->Lang)
|
|
|
|
array_unshift($this->Remaining, $this->Lang);
|
|
|
|
|
|
|
|
$this->Lang = $this->Version;
|
|
|
|
$this->Version = null;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// if(!DocumentationService::is_registered_version($this->Version)) {
|
|
|
|
// $this->httpError(404, 'The requested version could not be found.');
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(isset($this->Lang)) {
|
|
|
|
// check to see if its a valid language
|
|
|
|
// if(!DocumentationService::is_registered_language($this->Lang)) {
|
|
|
|
// $this->httpError(404, 'The requested language could not be found.');
|
|
|
|
// }
|
2010-04-11 09:25:26 +02:00
|
|
|
}
|
|
|
|
else {
|
2010-06-24 16:22:41 +02:00
|
|
|
$this->Lang = 'en';
|
2010-04-11 09:25:26 +02:00
|
|
|
}
|
2010-03-04 05:39:02 +01:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
return parent::handleRequest($request);
|
2010-04-11 09:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-24 16:22:41 +02:00
|
|
|
* Custom templates for each of the sections.
|
2010-04-11 09:25:26 +02:00
|
|
|
*/
|
2010-06-24 16:22:41 +02:00
|
|
|
function getViewer($action) {
|
|
|
|
// count the number of parameters after the language, version are taken
|
|
|
|
// into account. This automatically includes ' ' so all the counts
|
|
|
|
// are 1 more than what you would expect
|
2010-03-04 05:39:02 +01:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
if($this->Remaining) {
|
|
|
|
$params = count(array_unique($this->Remaining));
|
|
|
|
|
|
|
|
switch($params) {
|
|
|
|
case '1':
|
|
|
|
return parent::getViewer('home');
|
|
|
|
case '2':
|
|
|
|
return parent::getViewer('folder');
|
|
|
|
default:
|
|
|
|
if($module = $this->getModule()) {
|
|
|
|
$params = $this->Remaining;
|
|
|
|
array_shift($params);
|
|
|
|
|
|
|
|
$path = implode('/', array_unique($params));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_dir($module->getPath() . $path)) return parent::getViewer('folder');
|
2010-03-04 05:39:02 +01:00
|
|
|
}
|
|
|
|
}
|
2010-06-24 16:22:41 +02:00
|
|
|
|
|
|
|
return parent::getViewer($action);
|
2010-03-04 05:39:02 +01:00
|
|
|
}
|
2010-03-04 05:59:00 +01:00
|
|
|
|
2010-03-04 05:39:02 +01:00
|
|
|
/**
|
2010-06-24 16:22:41 +02:00
|
|
|
* Return all the available languages. Optionally the languages which are
|
|
|
|
* available for a given module
|
2010-03-04 05:39:02 +01:00
|
|
|
*
|
2010-06-24 16:22:41 +02:00
|
|
|
* @param String - The name of the module
|
2010-04-11 09:25:26 +02:00
|
|
|
* @return DataObjectSet
|
2010-03-04 05:39:02 +01:00
|
|
|
*/
|
2010-06-24 16:22:41 +02:00
|
|
|
function getLanguages($module = false) {
|
|
|
|
$output = new DataObjectSet();
|
2010-03-04 05:39:02 +01:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
if($module) {
|
|
|
|
// lookup the module for the available languages
|
|
|
|
|
|
|
|
// @todo
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$languages = DocumentationService::get_registered_languages();
|
|
|
|
|
|
|
|
if($languages) {
|
|
|
|
foreach($languages as $key => $lang) {
|
|
|
|
|
|
|
|
if(stripos($_SERVER['REQUEST_URI'], '/'. $this->Lang .'/') === false) {
|
|
|
|
// no language is in the URL currently. It needs to insert the language
|
|
|
|
// into the url like /sapphire/install to /en/sapphire/install
|
|
|
|
//
|
|
|
|
// @todo
|
|
|
|
}
|
|
|
|
|
|
|
|
$link = str_ireplace('/'.$this->Lang .'/', '/'. $lang .'/', $_SERVER['REQUEST_URI']);
|
|
|
|
|
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'Title' => $lang,
|
|
|
|
'Link' => $link
|
|
|
|
)));
|
|
|
|
}
|
2010-04-11 09:25:26 +02:00
|
|
|
}
|
2010-03-04 05:39:02 +01:00
|
|
|
}
|
2010-06-24 16:22:41 +02:00
|
|
|
|
|
|
|
return $output;
|
2010-03-04 05:39:02 +01:00
|
|
|
}
|
2010-06-24 16:22:41 +02:00
|
|
|
|
2010-03-04 11:18:02 +01:00
|
|
|
/**
|
2010-06-24 16:22:41 +02:00
|
|
|
* Get all the versions loaded into the module. If the project is only displaying from
|
|
|
|
* the filesystem then they are loaded under the 'Current' namespace.
|
|
|
|
*
|
|
|
|
* @todo Only show 'core' versions (2.3, 2.4) versions of the modules are going
|
|
|
|
* to spam this
|
2010-04-11 09:25:26 +02:00
|
|
|
*
|
2010-06-24 16:22:41 +02:00
|
|
|
* @param String $module name of module to limit it to eg sapphire
|
2010-04-11 09:25:26 +02:00
|
|
|
* @return DataObjectSet
|
2010-03-04 11:18:02 +01:00
|
|
|
*/
|
2010-06-24 16:22:41 +02:00
|
|
|
function getVersions($module = false) {
|
|
|
|
$versions = DocumentationService::get_registered_versions($module);
|
|
|
|
$output = new DataObjectSet();
|
2010-03-04 11:18:02 +01:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
foreach($versions as $key => $version) {
|
|
|
|
// work out the link to this version of the documentation.
|
|
|
|
//
|
|
|
|
// @todo Keep the user on their given page rather than redirecting to module.
|
|
|
|
// @todo Get links working
|
|
|
|
$linkingMode = ($this->Version == $version) ? 'current' : 'link';
|
|
|
|
|
|
|
|
if(!$version) $version = 'Current';
|
|
|
|
$major = (in_array($version, DocumentationService::get_major_versions())) ? true : false;
|
|
|
|
|
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'Title' => $version,
|
|
|
|
'Link' => $_SERVER['REQUEST_URI'],
|
|
|
|
'LinkingMode' => $linkingMode,
|
|
|
|
'MajorRelease' => $major
|
|
|
|
)));
|
2010-03-04 11:18:02 +01:00
|
|
|
}
|
2010-04-11 09:25:26 +02:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
return $output;
|
2010-03-04 11:18:02 +01:00
|
|
|
}
|
|
|
|
|
2010-04-11 09:25:26 +02:00
|
|
|
/**
|
2010-06-24 16:22:41 +02:00
|
|
|
* Generate the module which are to be documented. It filters
|
|
|
|
* the list based on the current head version. It displays the contents
|
|
|
|
* from the index.md file on the page to use.
|
2010-04-11 09:25:26 +02:00
|
|
|
*
|
2010-06-24 16:22:41 +02:00
|
|
|
* @return DataObject
|
|
|
|
*/
|
|
|
|
function getModules($version = false, $lang = false) {
|
|
|
|
if(!$version) $version = $this->Version;
|
|
|
|
if(!$lang) $lang = $this->Lang;
|
|
|
|
|
|
|
|
$modules = DocumentationService::get_registered_modules($version, $lang);
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
|
|
|
|
if($modules) {
|
|
|
|
foreach($modules as $module) {
|
|
|
|
// build the dataset. Load the $Content from an index.md
|
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'Title' => $module->getTitle(),
|
|
|
|
'Code' => $module,
|
|
|
|
'Content' => DocumentationParser::parse($module->getPath(), array('index'))
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
2010-03-04 11:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-24 16:22:41 +02:00
|
|
|
* Get the currently accessed entity from the site.
|
2010-03-04 11:18:02 +01:00
|
|
|
*
|
2010-06-24 16:22:41 +02:00
|
|
|
* @return false|DocumentationEntity
|
2010-03-04 11:18:02 +01:00
|
|
|
*/
|
2010-06-24 16:22:41 +02:00
|
|
|
function getModule() {
|
|
|
|
if($this->Remaining && is_array($this->Remaining)) {
|
|
|
|
return DocumentationService::is_registered_module($this->Remaining[0], $this->Version, $this->Lang);
|
2010-03-04 11:18:02 +01:00
|
|
|
}
|
2010-06-24 16:22:41 +02:00
|
|
|
|
2010-03-04 11:18:02 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-04 05:39:02 +01:00
|
|
|
/**
|
2010-06-24 16:22:41 +02:00
|
|
|
* Get the related pages to this module and the children to those pages
|
2010-03-04 05:39:02 +01:00
|
|
|
*
|
2010-06-24 16:22:41 +02:00
|
|
|
* @todo this only handles 2 levels. Could make it recursive
|
2010-03-04 05:39:02 +01:00
|
|
|
*
|
2010-06-24 16:22:41 +02:00
|
|
|
* @return false|DataObjectSet
|
2010-03-04 05:39:02 +01:00
|
|
|
*/
|
2010-06-24 16:22:41 +02:00
|
|
|
function getModulePages() {
|
|
|
|
if($module = $this->getModule()) {
|
|
|
|
$pages = DocumentationParser::get_pages_from_folder($module->getPath());
|
|
|
|
|
|
|
|
if($pages) {
|
|
|
|
foreach($pages as $page) {
|
|
|
|
$linkParts = array($module->getModuleFolder());
|
|
|
|
|
|
|
|
// don't include the 'index in the url
|
|
|
|
if($page->Title != "Index") $linkParts[] = $page->Filename;
|
2010-03-26 03:50:30 +01:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
$page->Link = $this->Link($linkParts);
|
|
|
|
|
|
|
|
$page->LinkingMode = 'link';
|
|
|
|
$page->Children = false;
|
|
|
|
|
|
|
|
if(isset($this->Remaining[1])) {
|
|
|
|
if(strtolower($this->Remaining[1]) == $page->Filename) {
|
|
|
|
$page->LinkingMode = 'current';
|
|
|
|
|
|
|
|
if(is_dir($page->Path)) {
|
|
|
|
$children = DocumentationParser::get_pages_from_folder($page->Path);
|
|
|
|
$segments = array($module->getModuleFolder(), $this->Remaining[1]);
|
|
|
|
|
|
|
|
foreach($children as $child) {
|
|
|
|
$child->Link = $this->Link(array_merge($segments, array($child->Filename)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$page->Children = $children;
|
|
|
|
}
|
|
|
|
}
|
2010-03-04 05:39:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-24 16:22:41 +02:00
|
|
|
|
|
|
|
return $pages;
|
2010-03-04 05:39:02 +01:00
|
|
|
}
|
2010-06-24 16:22:41 +02:00
|
|
|
|
2010-03-04 05:39:02 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
2010-06-24 16:22:41 +02:00
|
|
|
* Return the content for the page. If its an actual documentation page then
|
|
|
|
* display the content from the page, otherwise display the contents from
|
|
|
|
* the index.md file if its a folder
|
2010-03-04 05:39:02 +01:00
|
|
|
*
|
2010-06-24 16:22:41 +02:00
|
|
|
* @return HTMLText
|
2010-03-04 05:39:02 +01:00
|
|
|
*/
|
2010-06-24 16:22:41 +02:00
|
|
|
function getContent() {
|
|
|
|
if($module = $this->getModule()) {
|
|
|
|
// name of the module. Throw it away since we already have the module path.
|
|
|
|
$filepath = $this->Remaining;
|
|
|
|
array_shift($filepath);
|
|
|
|
|
|
|
|
return DocumentationParser::parse($module->getPath(), $filepath);
|
|
|
|
}
|
2010-04-11 09:25:26 +02:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
return false;
|
2010-03-04 05:39:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-24 16:22:41 +02:00
|
|
|
* Generate a list of breadcrumbs for the user. Based off the remaining params
|
|
|
|
* in the url
|
2010-03-04 05:39:02 +01:00
|
|
|
*
|
2010-06-24 16:22:41 +02:00
|
|
|
* @return DataObjectSet
|
2010-03-04 05:39:02 +01:00
|
|
|
*/
|
2010-06-24 16:22:41 +02:00
|
|
|
function getBreadcrumbs() {
|
|
|
|
$pages = $this->Remaining;
|
2010-03-04 05:39:02 +01:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
$output = new DataObjectSet();
|
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'Title' => ($this->Version) ? $this->Version : _t('DocumentationViewer.DOCUMENTATION', 'Documentation'),
|
|
|
|
'Link' => $this->Link()
|
|
|
|
)));
|
|
|
|
|
|
|
|
if($pages) {
|
|
|
|
$path = array();
|
|
|
|
|
|
|
|
foreach($pages as $page => $title) {
|
|
|
|
if($title) {
|
|
|
|
$path[] = $title;
|
|
|
|
|
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'Title' => DocumentationParser::clean_page_name($title),
|
|
|
|
'Link' => $this->Link($path)
|
|
|
|
)));
|
2010-03-04 05:39:02 +01:00
|
|
|
}
|
2010-06-24 16:22:41 +02:00
|
|
|
}
|
2010-03-04 05:39:02 +01:00
|
|
|
}
|
2010-06-24 16:22:41 +02:00
|
|
|
|
2010-04-11 09:25:26 +02:00
|
|
|
return $output;
|
2010-03-04 05:39:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-24 16:22:41 +02:00
|
|
|
* Return the base link to this documentation location
|
2010-03-04 05:39:02 +01:00
|
|
|
*
|
2010-06-24 16:22:41 +02:00
|
|
|
* @todo Make this work on non /dev/
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public function Link($path = false) {
|
|
|
|
$base = Director::absoluteBaseURL();
|
|
|
|
|
|
|
|
// @todo
|
|
|
|
$loc = 'dev/docs/';
|
|
|
|
|
|
|
|
$version = ($this->Version) ? $this->Version . '/' : false;
|
|
|
|
$lang = ($this->Lang) ? $this->Lang .'/' : false;
|
|
|
|
|
|
|
|
$action = '';
|
|
|
|
if(is_string($path)) $action = $path . '/';
|
|
|
|
|
|
|
|
if(is_array($path)) {
|
|
|
|
foreach($path as $key => $value) {
|
|
|
|
if($value) {
|
|
|
|
$action .= $value .'/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $base . $loc . $version . $lang . $action;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the language dropdown.
|
2010-03-04 05:39:02 +01:00
|
|
|
*
|
2010-06-24 16:22:41 +02:00
|
|
|
* @todo do this on a page by page rather than global
|
|
|
|
*
|
|
|
|
* @return Form
|
2010-03-04 05:39:02 +01:00
|
|
|
*/
|
2010-06-24 16:22:41 +02:00
|
|
|
function LanguageForm() {
|
|
|
|
if($module = $this->getModule()) {
|
|
|
|
$langs = DocumentationService::get_registered_languages($module->getModuleFolder());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$langs = DocumentationService::get_registered_languages();
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = new FieldSet(
|
|
|
|
$dropdown = new DropdownField(
|
|
|
|
'LangCode',
|
|
|
|
_t('DocumentationViewer.LANGUAGE', 'Language'),
|
|
|
|
$langs,
|
|
|
|
$this->Lang
|
|
|
|
)
|
|
|
|
);
|
2010-03-04 05:39:02 +01:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
$actions = new FieldSet(
|
|
|
|
new FormAction('doLanguageForm', _t('DocumentationViewer.CHANGE', 'Change'))
|
|
|
|
);
|
2010-03-04 05:39:02 +01:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
$dropdown->setDisabled(true);
|
2010-03-04 11:18:02 +01:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
return new Form($this, 'LanguageForm', $fields, $actions);
|
2010-03-04 05:39:02 +01:00
|
|
|
}
|
2010-04-11 09:25:26 +02:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
/**
|
|
|
|
* Process the language change
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function doLanguageForm($data, $form) {
|
|
|
|
$this->Lang = (isset($data['LangCode'])) ? $data['LangCode'] : 'en';
|
|
|
|
|
|
|
|
return $this->redirect($this->Link());
|
|
|
|
}
|
2010-03-04 05:39:02 +01:00
|
|
|
}
|