NEW: Add set_edit_link() and getEditLink() to DocumentViewer to allow adding edit links to be displayed on documentation pages

This commit is contained in:
Will Rossiter 2012-09-03 22:02:42 +12:00
parent 08dda0a403
commit 3a9a384ca4
3 changed files with 88 additions and 0 deletions

View File

@ -55,6 +55,12 @@ class DocumentationViewer extends Controller {
* @var String|array Optional permission check * @var String|array Optional permission check
*/ */
static $check_permission = 'ADMIN'; static $check_permission = 'ADMIN';
/**
* @var array map of modules to edit links.
* @see {@link getEditLink()}
*/
private static $edit_links = array();
function init() { function init() {
parent::init(); parent::init();
@ -932,6 +938,76 @@ class DocumentationViewer extends Controller {
return false; 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:
*
* <code>
* DocumentationViewer::set_edit_link(
* 'framework',
* 'https://github.com/silverstripe/%entity%/edit/%version%/docs/%lang%/%path%',
* $opts
* ));
* </code>
*
* @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 * Flattens an array

View File

@ -5,6 +5,12 @@
<div id="documentation-page"> <div id="documentation-page">
<div id="content-column"> <div id="content-column">
$Content $Content
<% if EditLink %>
<div id="edit-link">
<p><a target="_blank" href="$EditLink">Edit this page</a></p>
</div>
<% end_if %>
</div> </div>
<% if Content %> <% if Content %>

View File

@ -6,6 +6,12 @@
<div id="content-column"> <div id="content-column">
<% if Content %> <% if Content %>
$Content $Content
<% if EditLink %>
<div id="edit-link">
<p><a target="_blank" href="$EditLink">Edit this page</a></p>
</div>
<% end_if %>
<% else %> <% else %>
<h2>$Title</h2> <h2>$Title</h2>
<% end_if %> <% end_if %>