Skip to content

Commit

Permalink
Allow updating multiple plugins at once. (matomo-org#14052)
Browse files Browse the repository at this point in the history
* Allow updating multiple plugins at once.

* Disable checkbox if plugin is not downloadable.

* Fix translation typo.

* really fix typo
  • Loading branch information
diosmosis committed Mar 7, 2019
1 parent 0d5d0df commit e2b9414
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 22 deletions.
3 changes: 2 additions & 1 deletion plugins/CorePluginsAdmin/lang/en.json
Expand Up @@ -83,6 +83,7 @@
"Version": "Version",
"ViewAllMarketplacePlugins": "View all Marketplace plugins",
"WeCouldNotLoadThePluginAsItHasMissingDependencies": "The plugin %1$s could not be loaded as it has missing dependencies: %2$s",
"Websites": "Websites"
"Websites": "Websites",
"UpdateSelected": "Update Selected"
}
}
46 changes: 46 additions & 0 deletions plugins/CorePluginsAdmin/templates/macros.twig
Expand Up @@ -2,9 +2,18 @@
{% macro tablePluginUpdates(pluginsHavingUpdate, updateNonce, isMultiServerEnvironment) %}
{% import '@Marketplace/macros.twig' as marketplaceMacro %}

<div>
<a id="update-selected-plugins" href="javascript:" class="btn">{{ 'CorePluginsAdmin_UpdateSelected'|translate }}</a>
</div>
<table piwik-content-table>
<thead>
<tr>
<th>
<span class="checkbox-container">
<input type="checkbox" id="select-plugin-all" />
<label for="select-plugin-all"></label>
</span>
</th>
<th>{{ 'General_Plugin'|translate }}</th>
<th class="num">{{ 'CorePluginsAdmin_Version'|translate }}</th>
<th>{{ 'General_Description'|translate }}</th>
Expand All @@ -15,6 +24,12 @@
<tbody id="plugins">
{% for name,plugin in pluginsHavingUpdate %}
<tr {% if plugin.isActivated|default(false) %}class="active-plugin"{% else %}class="inactive-plugin"{% endif %}>
<td class="select-cell">
<span class="checkbox-container">
<input type="checkbox" id="select-plugin-{{ plugin.name|e('html_attr') }}" {% if plugin.isDownloadable is defined and not plugin.isDownloadable %}disabled="disabled"{% endif %} />
<label for="select-plugin-{{ plugin.name|e('html_attr') }}"></label>
</span>
</td>
<td class="name">
<a href="javascript:void(0);" piwik-plugin-name="{{ plugin.name|e('html_attr') }}" class="plugin-details">
{{ plugin.name }}
Expand Down Expand Up @@ -57,6 +72,37 @@
</tbody>
</table>

<script>
$(function () {
$('#update-selected-plugins').on('click', function () {
var pluginsToUpdate = [];
$('tbody#plugins td.select-cell input').each(function () {
if (!this.checked) {
return;
}
var pluginName = $(this).closest('tr').find('.name .plugin-details').attr('piwik-plugin-name');
pluginsToUpdate.push(pluginName);
});
var url = '{{ linkTo({'module':'Marketplace','action':'updatePlugin','nonce':updateNonce})|raw }}&pluginName=' + encodeURIComponent(pluginsToUpdate.join(','));
window.location.href = url;
$(this).prop('disabled', true);
});
$('#select-plugin-all').on('change', function () {
var self = this;
$('tbody#plugins td.select-cell input[type=checkbox]').each(function () {
if ($(this).prop('disabled')) {
return;
}
$(this).prop('checked', self.checked);
});
});
});
</script>

{% endmacro %}

{% macro pluginActivateDeactivateAction(name, isActivated, missingRequirements, deactivateNonce, activateNonce) -%}
Expand Down
47 changes: 27 additions & 20 deletions plugins/Marketplace/Controller.php
Expand Up @@ -393,32 +393,36 @@ private function createUpdateOrInstallView($template, $nonceName)
$this->dieIfPluginsAdminIsDisabled();
$this->displayWarningIfConfigFileNotWritable();

$pluginName = $this->getPluginNameIfNonceValid($nonceName);
$plugins = $this->getPluginNameIfNonceValid($nonceName);

$view = new View('@Marketplace/' . $template);
$this->setBasicVariablesView($view);
$view->errorMessage = '';
$view->plugin = array('name' => $pluginName);

try {
$this->pluginInstaller->installOrUpdatePluginFromMarketplace($pluginName);
$pluginInfos = [];
foreach ($plugins as $pluginName) {
$pluginInfos[] = $this->plugins->getPluginInfo($pluginName);

} catch (\Exception $e) {
try {
$this->pluginInstaller->installOrUpdatePluginFromMarketplace($pluginName);

$notification = new Notification($e->getMessage());
$notification->context = Notification::CONTEXT_ERROR;
$notification->type = Notification::TYPE_PERSISTENT;
$notification->flags = Notification::FLAG_CLEAR;
if (method_exists($e, 'isHtmlMessage') && $e->isHtmlMessage()) {
$notification->raw = true;
}
Notification\Manager::notify('CorePluginsAdmin_InstallPlugin', $notification);
} catch (\Exception $e) {

Url::redirectToReferrer();
return;
$notification = new Notification($e->getMessage());
$notification->context = Notification::CONTEXT_ERROR;
$notification->type = Notification::TYPE_PERSISTENT;
$notification->flags = Notification::FLAG_CLEAR;
if (method_exists($e, 'isHtmlMessage') && $e->isHtmlMessage()) {
$notification->raw = true;
}
Notification\Manager::notify('CorePluginsAdmin_InstallPlugin', $notification);

Url::redirectToReferrer();
return;
}
}

$view->plugin = $this->plugins->getPluginInfo($pluginName);
$view->plugins = $pluginInfos;

return $view;
}
Expand All @@ -435,11 +439,14 @@ private function getPluginNameIfNonceValid($nonceName)

$pluginName = Common::getRequestVar('pluginName', null, 'string');

if (!$this->pluginManager->isValidPluginName($pluginName)) {
throw new Exception('Invalid plugin name');
$plugins = explode(',', $pluginName);
$plugins = array_map('trim', $plugins);
foreach ($plugins as $name) {
if (!$this->pluginManager->isValidPluginName($name)) {
throw new Exception('Invalid plugin name: ' . $name);
}
}

return $pluginName;
return $plugins;
}

private function dieIfPluginsAdminIsDisabled()
Expand Down
2 changes: 2 additions & 0 deletions plugins/Marketplace/templates/installPlugin.twig
Expand Up @@ -4,6 +4,7 @@

<div style="max-width:980px;">

{% for plugin in plugins %}
<h2>{{ 'Marketplace_InstallingPlugin'|translate(plugin.name) }}</h2>

<div>
Expand Down Expand Up @@ -36,6 +37,7 @@

{% endif %}
</div>
{% endfor %}
</div>

{% endblock %}
5 changes: 4 additions & 1 deletion plugins/Marketplace/templates/updatePlugin.twig
Expand Up @@ -4,6 +4,7 @@

<div style="max-width:980px;">

{% for plugin in plugins %}
<h2>{{ 'Marketplace_UpdatingPlugin'|translate(plugin.name) }}</h2>

<div>
Expand All @@ -29,7 +30,9 @@
<p>{{ 'Marketplace_StepPluginSuccessfullyUpdated'|translate(plugin.name, plugin.latestVersion) }}</p>

{% endif %}

</div>
{% endfor %}
<div>
<p><a href="{{ linkTo({'module': 'CorePluginsAdmin', 'action': 'plugins'}) }}">{{ 'General_Plugins'|translate }}</a>
|
<a href="{{ linkTo({'module': 'CorePluginsAdmin', 'action': 'themes'}) }}">{{ 'CorePluginsAdmin_Themes'|translate }}</a>
Expand Down

0 comments on commit e2b9414

Please sign in to comment.