From 3a9a384ca4eb1adaa5b5ceea9cc0e031e0c38243 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Mon, 3 Sep 2012 22:02:42 +1200 Subject: [PATCH] NEW: Add set_edit_link() and getEditLink() to DocumentViewer to allow adding edit links to be displayed on documentation pages --- code/controllers/DocumentationViewer.php | 76 +++++++++++++++++++ templates/Layout/DocumentationViewer.ss | 6 ++ .../Layout/DocumentationViewer_folder.ss | 6 ++ 3 files changed, 88 insertions(+) diff --git a/code/controllers/DocumentationViewer.php b/code/controllers/DocumentationViewer.php index fc35fdb..7b842cb 100755 --- a/code/controllers/DocumentationViewer.php +++ b/code/controllers/DocumentationViewer.php @@ -55,6 +55,12 @@ class DocumentationViewer extends Controller { * @var String|array Optional permission check */ static $check_permission = 'ADMIN'; + + /** + * @var array map of modules to edit links. + * @see {@link getEditLink()} + */ + private static $edit_links = array(); function init() { parent::init(); @@ -932,6 +938,76 @@ class DocumentationViewer extends Controller { return false; } + + /** + * Sets the mapping between a entity name and the link for the end user + * to jump into editing the documentation. + * + * Some variables are replaced: + * - %version% + * - %entity% + * - %path% + * - %lang% + * + * For example to provide an edit link to the framework module in github: + * + * + * DocumentationViewer::set_edit_link( + * 'framework', + * 'https://github.com/silverstripe/%entity%/edit/%version%/docs/%lang%/%path%', + * $opts + * )); + * + * + * @param string module name + * @param string link + * @param array options ('rewritetrunktomaster') + */ + public static function set_edit_link($module, $link, $options = array()) { + self::$edit_links[$module] = array( + 'url' => $link, + 'options' => $options + ); + } + + /** + * Returns an edit link to the current page (optional). + * + * @return string + */ + public function getEditLink() { + $page = $this->getPage(); + + if($page) { + $entity = $page->getEntity(); + + if($entity && isset(self::$edit_links[$entity->title])) { + // build the edit link, using the version defined + $url = self::$edit_links[$entity->title]; + $version = $page->getVersion(); + + if($version == "trunk" && (isset($url['options']['rewritetrunktomaster']))) { + if($url['options']['rewritetrunktomaster']) { + $version = "master"; + } + } + + return str_replace( + array('%entity%', '%lang%', '%version%', '%path%'), + array( + $entity->getFolder(), + $page->getLang(), + $version, + ltrim($page->getRelativePath(), '/') + ), + + $url['url'] + ); + } + } + + return false; + } /** * Flattens an array diff --git a/templates/Layout/DocumentationViewer.ss b/templates/Layout/DocumentationViewer.ss index aa626da..d6ff78f 100755 --- a/templates/Layout/DocumentationViewer.ss +++ b/templates/Layout/DocumentationViewer.ss @@ -5,6 +5,12 @@
$Content + + <% if EditLink %> + + <% end_if %>
<% if Content %> diff --git a/templates/Layout/DocumentationViewer_folder.ss b/templates/Layout/DocumentationViewer_folder.ss index ab073c2..6aeb9b8 100755 --- a/templates/Layout/DocumentationViewer_folder.ss +++ b/templates/Layout/DocumentationViewer_folder.ss @@ -6,6 +6,12 @@
<% if Content %> $Content + + <% if EditLink %> + + <% end_if %> <% else %>

$Title

<% end_if %>