Add Configuration logic

This commit is contained in:
Benoît Burnichon
2015-09-04 15:31:50 +02:00
parent 7552d33a8f
commit 52097523e4
3 changed files with 116 additions and 11 deletions

View File

@@ -9,6 +9,8 @@
*/ */
namespace Alchemy\Phrasea\Plugin; namespace Alchemy\Phrasea\Plugin;
use Assert\Assertion;
class BasePluginMetadata implements PluginMetadataInterface class BasePluginMetadata implements PluginMetadataInterface
{ {
/** @var string */ /** @var string */
@@ -19,6 +21,8 @@ class BasePluginMetadata implements PluginMetadataInterface
private $iconUrl; private $iconUrl;
/** @var string */ /** @var string */
private $localeTextDomain; private $localeTextDomain;
/** @var ConfigurationTabInterface[] */
private $configurationTabs;
/** /**
* @param string $name * @param string $name
@@ -65,4 +69,38 @@ class BasePluginMetadata implements PluginMetadataInterface
{ {
return $this->localeTextDomain; return $this->localeTextDomain;
} }
/**
* @param ConfigurationTabInterface[] $tabs
*/
public function setConfigurationTabs($tabs)
{
Assertion::allIsInstanceOf($tabs, 'Alchemy\Phrasea\Plugin\ConfigurationTabInterface');
foreach ($tabs as $name => $tab) {
$this->addConfigurationTab($name, $tab);
}
}
/**
* @param string $name
* @param ConfigurationTabInterface $tab
*/
public function addConfigurationTab($name, ConfigurationTabInterface $tab)
{
Assertion::regex($name, '/^[a-zA-Z][-_a-zA-Z0-9]*$/');
if (isset($this->configurationTabs[$name])) {
throw new \LogicException(sprintf(
'A configuration tab with name "%s" is already defined. Registered tabs: "%s"',
implode('", "', array_keys($this->configurationTabs))
));
}
$this->configurationTabs[$name] = $tab;
}
public function getConfigurationTabs()
{
return $this->configurationTabs;
}
} }

View File

@@ -0,0 +1,27 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2015 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Plugin;
interface ConfigurationTabInterface
{
/**
* Get the title translation key in plugin domain
*
* @return string
*/
public function getTitle();
/**
* Get the url where configuration tab can be retrived
*
* @return string
*/
public function getUrl();
}

View File

@@ -6,15 +6,55 @@
<h1>{% trans 'plugin.name' plugin.localeTextDomain %}</h1> <h1>{% trans 'plugin.name' plugin.localeTextDomain %}</h1>
</div> </div>
{{ block('about') }} <div>
{% endblock %} <!-- Nav tabs -->
<ul class="nav nav-tabs" id="configurationTabs">
{% for divId, configurationTab in plugin.configurationTabs %}
{% block about %} {% set tabTitle = configurationTab.title %}
<h1>{% trans 'admin::plugins: about' %}</h1> <li role="presentation"><a href="#{{ divId }}" aria-controls="{{ divId }}" role="tab" data-toggle="tab" data-url="{{ configurationTab.url }}">{% trans tabTitle plugin.localeTextDomain %}</a></li>
{% endfor %}
<ul> <li role="presentation" class="active"><a href="#about" aria-controls="about" role="tab" data-toggle="tab">{% trans 'admin::plugins: about' %}</a></li>
<li>{{ 'admin::plugins: name'|trans }}{{ plugin.name }}</li> </ul>
<li>{{ 'admin::plugins: version'|trans }}{{ plugin.version }}</li>
</ul> <!-- Tab panes -->
<div class="tab-content">
{% for divId, configurationTab in plugin.configurationTabs %}
<div role="tabpanel" class="tab-pane fade" id="{{ divId }}"></div>
{% endfor %}
<div role="tabpanel" class="tab-pane fade in active" id="about">
<ul>
<li>{{ 'admin::plugins: name'|trans }}{{ plugin.name }}</li>
<li>{{ 'admin::plugins: version'|trans }}{{ plugin.version }}</li>
</ul>
</div>
</div>
</div>
<script type="text/javascript">
var contentsDownloaded = {};
var remoteContent = function(url) {
return $.get(url);
};
var tabs = $('#configurationTabs a[data-toggle="tab"]');
tabs.on('show', function (e) {
if (e.target.hash != '#about' && contentsDownloaded[e.target.hash] === undefined) {
$(e.target.hash).empty().html('<img src="/skins/icons/main-loader.gif" alt="loading"/>');
}
});
tabs.on('shown', function (e) {
if (e.target.hash != '#about' && contentsDownloaded[e.target.hash] === undefined) {
var targetDiv = $(e.target.hash);
remoteContent($(e.target).attr('data-url')).then(function(response) {
targetDiv.empty().html(response);
contentsDownloaded[e.target.hash] = true;
}, function(error) {
console.log(error);
targetDiv.empty().html('<i class="icon-fire">{% trans 'admin::plugins: retrieveConfigurationError' %}</i>');
});
}
});
</script>
{% endblock %} {% endblock %}