Improve File input field for Admin

Add removing files from a blueprint to Admin, moved from Pro. Drop
`showuploaded`, `ispluginconfig`, `showuploadedpreview` options from
the File field. Added a "blueprint" option to specify the field name
and type, e.g. `plugins.admin` or `themes.antimatter`. Dropped the
multiple option, as it'd need more handling that now it's not there.
This commit is contained in:
Flavio Copes
2015-12-21 16:21:51 +01:00
parent 5cabfb815e
commit b76b79e611
3 changed files with 88 additions and 16 deletions

View File

@@ -11,6 +11,8 @@ use Grav\Common\Data;
use Grav\Common\Page;
use Grav\Common\Page\Pages;
use Grav\Common\Page\Collection;
use Grav\Common\Plugin;
use Grav\Common\Theme;
use Grav\Common\User\User;
use Grav\Common\Utils;
use Grav\Common\Backup\ZipBackup;
@@ -1402,6 +1404,84 @@ class AdminController
return true;
}
/**
* Determine if the user can edit media
*
* @return bool True if the media action is allowed
*/
protected function canEditMedia()
{
$type = 'media';
if (!$this->authorizeTask('edit media', ['admin.' . $type, 'admin.super'])) {
return false;
}
return true;
}
/**
* Handles removing a media file
*
* @return bool True if the action was performed
*/
public function taskRemoveMedia()
{
if (!$this->canEditMedia()) {
return false;
}
$filename = base64_decode($this->route);
$file = File::instance($filename);
$resultRemoveMedia = false;
$resultRemoveMediaMeta = true;
if ($file->exists()) {
$resultRemoveMedia = $file->delete();
$metaFilePath = $filename . '.meta.yaml';
$metaFilePath = str_replace('@3x', '', $metaFilePath);
$metaFilePath = str_replace('@2x', '', $metaFilePath);
if (is_file($metaFilePath)) {
$metaFile = File::instance($metaFilePath);
$resultRemoveMediaMeta = $metaFile->delete();
}
}
if ($resultRemoveMedia && $resultRemoveMediaMeta) {
$this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.REMOVE_SUCCESSFUL'), 'info');
} else {
$this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.REMOVE_FAILED'), 'error');
}
$this->post = array('_redirect' => 'media');
return true;
}
/**
* Handle deleting a file from a blueprint
*
* @return bool True if the action was performed.
*/
protected function taskRemoveFileFromBlueprint()
{
$uri = $this->grav['uri'];
$this->taskRemoveMedia();
$field = $uri->param('field');
$blueprint = $uri->param('blueprint');
$this->grav['config']->set($blueprint . '.' . $field, '');
if (substr($blueprint, 0, 7) == 'plugins') {
Plugin::saveConfig(substr($blueprint, 8));
}
if (substr($blueprint, 0, 6) == 'themes') {
Theme::saveConfig(substr($blueprint, 7));
}
$redirect = base64_decode($uri->param('redirect'));
$this->post = array('_redirect' => $redirect);
return true;
}
/**
* Prepare and return POST data.
*

View File

@@ -461,4 +461,6 @@ PLUGIN_ADMIN:
TWIG_UMASK_FIX: "Umask Fix"
TWIG_UMASK_FIX_HELP: "By default Twig creates cached files as 0755, fix switches this to 0775"
CACHE_PERMS: "Cache Permissions"
CACHE_PERMS_HELP: "Default cache folder perms. Usually 0755 or 0775 depending on setup"
CACHE_PERMS_HELP: "Default cache folder perms. Usually 0755 or 0775 depending on setup"
REMOVE_SUCCESSFUL: "Remove Successful"
REMOVE_FAILED: "Remove Failed"

View File

@@ -9,28 +9,18 @@
Use the "pagemediaselect" type instead.
{% else %}
{% if value %}
{% if files.showuploaded == true %}
{% if files.showuploadedpreview %}
<img src="{{ base_url_relative_frontend == '/' ? '/' : base_url_relative_frontend ~ '/'}}{{ value }}" />
{% else %}
{{ value|replace({(files.destination ~ '/'): ''}) }}
{% endif %}
{% if files.ispluginconfig %}
<a href="{{ uri.addNonce('/admin/media/' ~ base64_encode(base_path ~ '/' ~ value) ~ '/task' ~ config.system.param_sep ~ 'removeFileFromPluginConfig' ~ '/plugin_name' ~ config.system.param_sep ~ files.pluginname ~ '/field_name' ~ config.system.param_sep ~ files.name ~ '/redirect' ~ config.system.param_sep ~ base64_encode(uri.path), 'admin-form', 'admin-nonce') }}">
<i class="fa fa-close"></i>
</a>
{% endif %}
{% endif %}
<img src="{{ base_url_relative_frontend == '/' ? '/' : base_url_relative_frontend ~ '/'}}{{ value }}" alt="{{ value|replace({(files.destination ~ '/'): ''}) }}" />
<a href="{{ uri.addNonce(base_url_relative ~ '/media/' ~ base64_encode(base_path ~ '/' ~ value) ~ '/task' ~ config.system.param_sep ~ 'removeFileFromBlueprint' ~ '/blueprint' ~ config.system.param_sep ~ files.blueprint ~ '/field' ~ config.system.param_sep ~ files.name ~ '/redirect' ~ config.system.param_sep ~ base64_encode(uri.path), 'admin-form', 'admin-nonce') }}">
<i class="fa fa-close"></i>
</a>
{% endif %}
<div class="form-input-wrapper {{ field.size }}">
<input
{# required attribute structures #}
name="{{ (scope ~ field.name)|fieldName ~ (files.multiple ? '[]' : '[]') }}"
name="{{ (scope ~ field.name)|fieldName ~ '[]' }}"
{% block input_attributes %}
type="file"
{% if files.multiple %}multiple="multiple"{% endif %}
{% if files.accept %}accept="{{ files.accept|join(',') }}"{% endif %}
{{ parent() }}
{% endblock %}