mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2025-11-01 02:46:04 +01:00
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:
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user