Add admin-media-move, admin-media-replace and admin-media-actions plugins
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
# v1.0.4
|
||||
## 12/11/2018
|
||||
|
||||
1. [](#feature)
|
||||
* Update documentation
|
||||
|
||||
# v1.0.3
|
||||
## 2/24/2018
|
||||
|
||||
1. [](#new)
|
||||
* Check that dependencies are enabled before loading
|
||||
|
||||
# v1.0.2
|
||||
## 2/24/2018
|
||||
|
||||
1. [](#new)
|
||||
* Check for dependencies before loading
|
||||
|
||||
# v1.0.1
|
||||
## 1/25/2018
|
||||
|
||||
1. [](#new)
|
||||
* Add dialog
|
||||
* Add quicksend option
|
||||
* Enforce extension, image, and file-rename options
|
||||
* Refactor project layout
|
||||
|
||||
# v1.0.0
|
||||
## 1/24/2018
|
||||
|
||||
1. [](#initial)
|
||||
*
|
||||
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2018 TwelveTone LLC
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,29 @@
|
||||
# Admin Media Replace Plugin
|
||||
|
||||
The **Admin Media Replace** Plugin is for [Grav CMS](http://github.com/getgrav/grav). A plugin which adds the option to replace media.
|
||||
|
||||
## Installation
|
||||
|
||||
Installing the Admin Media Replace plugin can be done in one of two ways. The GPM (Grav Package Manager) installation method enables you to quickly and easily install the plugin with a simple terminal command, while the manual method enables you to do so via a zip file.
|
||||
|
||||
### GPM Installation (Preferred)
|
||||
|
||||
The simplest way to install this plugin is via the [Grav Package Manager (GPM)](http://learn.getgrav.org/advanced/grav-gpm) through your system's terminal (also called the command line). From the root of your Grav install type:
|
||||
|
||||
bin/gpm install admin-media-replace
|
||||
|
||||
This will install the Admin Media Replace plugin into your `/user/plugins` directory within Grav. Its files can be found under `/your/site/grav/user/plugins/admin-media-replace`.
|
||||
|
||||
### Manual Installation
|
||||
|
||||
To install this plugin, just download the zip version of this repository and unzip it under `/your/site/grav/user/plugins`. Then, rename the folder to `admin-media-replace`. You can find these files on [GitHub](https://github.com) or via [GetGrav.org](http://getgrav.org/downloads/plugins#extras).
|
||||
|
||||
You should now have all the plugin files under
|
||||
|
||||
/your/site/grav/user/plugins/admin-media-replace
|
||||
|
||||
> NOTE: This plugin is a modular component for Grav which requires [Grav](http://github.com/getgrav/grav) and the [Admin](https://github.com/getgrav/grav-plugin-admin) plugin to operate.
|
||||
|
||||
## Usage
|
||||
|
||||
See [Official Documentation](https://www.twelvetone.tv/docs/developer-tools/grav-plugins/admin-media-replace-plugin)
|
||||
@@ -0,0 +1,297 @@
|
||||
<?php
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018 TwelveTone LLC
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Grav\Plugin;
|
||||
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Page\Media;
|
||||
use Grav\Common\Plugin;
|
||||
use Grav\Common\Utils;
|
||||
|
||||
include_once 'classes/DialogUtil.php';
|
||||
|
||||
function array_get($arr, $key, $default = null)
|
||||
{
|
||||
if (!isset($arr[$key])) {
|
||||
if ($default === null) {
|
||||
throw new \Exception("A key is missing: " . $key);
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
return $arr[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* Class AdminMediaReplacePlugin
|
||||
* @package Grav\Plugin
|
||||
*/
|
||||
class AdminMediaReplacePlugin extends Plugin
|
||||
{
|
||||
|
||||
const ROUTE = '/admin-media-replace';
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
'onPluginsInitialized' => ['onPluginsInitialized', 0]
|
||||
];
|
||||
}
|
||||
|
||||
public function onPluginsInitialized()
|
||||
{
|
||||
if (!$this->isAdmin() || !$this->grav['user']->authenticated) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!self::_checkDependencies($this)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->enable([
|
||||
'onAdminTwigTemplatePaths' => ['onAdminTwigTemplatePaths', 0],
|
||||
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
|
||||
'onTwigInitialized' => ['onTwigInitialized', 0],
|
||||
'onTwigExtensions' => ['onTwigExtensions', -1],
|
||||
'onPageNotFound' => ['onPageNotFound', 1],
|
||||
]);
|
||||
|
||||
$this->grav['media-actions']->addAction([
|
||||
'actionId' => "MediaReplace",
|
||||
'caption' => "Replace",
|
||||
'icon' => "exchange",
|
||||
'handler' => function ($page, $mediaName, $payload) {
|
||||
$ret = [
|
||||
"error" => false
|
||||
];
|
||||
return $ret;
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
public function onPageNotFound($e)
|
||||
{
|
||||
if (!$this->isAdmin()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$route = $this->grav['admin']->location . "/" . $this->grav['admin']->route;
|
||||
switch ($route) {
|
||||
case "admin-media-replace/replace":
|
||||
try {
|
||||
$filename = array_get($_POST, "media-new-filename");
|
||||
$route = array_get($_POST, "media-route");
|
||||
$media = array_get($_POST, "media-filename");
|
||||
|
||||
$media_rename = array_get($_POST, "media-rename", "1") === "1";
|
||||
$require_image = array_get($_POST, "media-require-image", "1") === "1";
|
||||
$match_extension = array_get($_POST, "media-match-extension", "1") === "1";
|
||||
|
||||
$page = $this->grav['pages']->find($route);
|
||||
if (!$page) {
|
||||
throw new \Exception("Page not found.");
|
||||
}
|
||||
$mediaPath = $page->path() . "/" . $media;
|
||||
if (!is_file($mediaPath)) {
|
||||
throw new \Exception("Media not found.");
|
||||
}
|
||||
|
||||
$tmp_name = $_FILES['mediaupload']['tmp_name'];
|
||||
|
||||
if ($match_extension) {
|
||||
if (basename($filename) !== basename($media)) {
|
||||
throw new \Exception("Media extensions do not match.");
|
||||
}
|
||||
}
|
||||
|
||||
if ($require_image) {
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$mime = finfo_file($finfo, $_FILES['mediaupload']['tmp_name']);
|
||||
finfo_close($finfo);
|
||||
if (!Utils::startsWith($mime, "image/")) {
|
||||
throw new \Exception("Media must be an image.");
|
||||
}
|
||||
}
|
||||
|
||||
if ($media_rename) {
|
||||
// overwrite current file
|
||||
$finalName = basename($mediaPath);
|
||||
} else {
|
||||
// delete current file
|
||||
unlink($page->path() . '/' . $media);
|
||||
$finalName = basename($filename);
|
||||
}
|
||||
|
||||
move_uploaded_file($tmp_name, $page->path() . '/' . $finalName);
|
||||
//$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
|
||||
// basename() may prevent filesystem traversal attacks;
|
||||
// further validation/sanitation of the filename may be appropriate
|
||||
//$name = basename($_FILES["pictures"]["name"][$key]);
|
||||
|
||||
$media1 = new Media($page->path());
|
||||
$medium = $media1[basename($finalName)];
|
||||
$url = $medium->display($medium->get('extension') === 'svg' ? 'source' : 'thumbnail')->cropZoom(400, 300)->url();
|
||||
|
||||
$ret = ["thumbnail" => $url];
|
||||
$ret['newName'] = $finalName;
|
||||
// if (!$media_rename) {
|
||||
// $ret['toast'] = "Refresh the page to update the new page media name.";
|
||||
// }
|
||||
die(json_encode($ret));
|
||||
|
||||
// Get original name
|
||||
//$source = $medium->higherQualityAlternative()->get('filename');
|
||||
//$media_list[$name] = ['url' => $medium->display($medium->get('extension') === 'svg' ? 'source' : 'thumbnail')->cropZoom(400, 300)->url(), 'size' => $medium->get('size'), 'metadata' => $metadata, 'original' => $source->get('filename')];
|
||||
|
||||
} catch
|
||||
(\Exception $exception) {
|
||||
// die(print_r($_SERVER));
|
||||
die(json_encode(["error" => $exception->getMessage()]));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public
|
||||
function onTwigTemplatePaths()
|
||||
{
|
||||
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
|
||||
}
|
||||
|
||||
public
|
||||
function onTwigInitialized()
|
||||
{
|
||||
$this->grav['assets']->addJs('plugin://admin-media-replace/assets/dialog_util.js', -1000, false);
|
||||
$this->grav['assets']->addJs('plugin://admin-media-replace/assets/media_replace_action.js', -1000, false);
|
||||
if ($this->config->get("plugins.admin-media-replace.quicksend", false)) {
|
||||
$this->grav['assets']->addInlineJs("const _media_replace_isQuicksend = true;");
|
||||
}
|
||||
}
|
||||
|
||||
public
|
||||
function onTwigExtensions()
|
||||
{
|
||||
if (!$this->isAdmin()) {
|
||||
return;
|
||||
}
|
||||
addModalForm("MediaReplace", "generic-modal.twig.html");
|
||||
}
|
||||
|
||||
public
|
||||
function onAdminTwigTemplatePaths($event)
|
||||
{
|
||||
$event['paths'] = array_merge($event['paths'], [__DIR__ . '/templates']);
|
||||
return $event;
|
||||
}
|
||||
|
||||
public
|
||||
function outputError($msg)
|
||||
{
|
||||
header('HTTP/1.1 400 Bad Request');
|
||||
die(json_encode(['error' => ['msg' => $msg]]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks plugin dependencies. Call this after all plugins have been loaded and are enabled.
|
||||
*
|
||||
* @param $plugin
|
||||
* @param $issues array Receives issues as strings. If null, grav['messages'] is used.
|
||||
* @return bool true if dependencies are met.
|
||||
*/
|
||||
public static function _checkDependencies($plugin, &$issues = null)
|
||||
{
|
||||
$grav = Grav::instance();
|
||||
$errors = 0;
|
||||
$messages = $grav['messages'];
|
||||
$plugins = $grav['plugins'];
|
||||
|
||||
$deps = $plugin->getBlueprint()->dependencies;
|
||||
if ($deps) {
|
||||
foreach ($deps as $dep) {
|
||||
$name = $dep['name'];
|
||||
if ($name === 'grav') {
|
||||
//TODO check grav version
|
||||
continue;
|
||||
}
|
||||
$version = $dep['version'];
|
||||
if (!preg_match("#^([<>=]+)?(.*)#", $version, $m)) {
|
||||
continue;
|
||||
}
|
||||
$compare = $m[1];
|
||||
$version = $m[2];
|
||||
if (!$compare) {
|
||||
$compare = '=';
|
||||
}
|
||||
|
||||
$found = $plugins->get($name);
|
||||
if (!$found) {
|
||||
$msg = "Missing Dependency: '$name'";
|
||||
if (is_array($issues)) {
|
||||
$issues[] = $msg;
|
||||
} else {
|
||||
$messages->add($msg, 'error');
|
||||
}
|
||||
$errors++;
|
||||
continue;
|
||||
}
|
||||
if (!$grav['config']->get("plugins.$name.enabled")) {
|
||||
//BUG admin should always be enabled if installed
|
||||
if ($name !== 'admin') {
|
||||
$msg = "Dependency Not Enabled: '$name'";
|
||||
if (is_array($issues)) {
|
||||
$issues[] = $msg;
|
||||
} else {
|
||||
$messages->add($msg, 'error');
|
||||
}
|
||||
$errors++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$realVersion = $found->blueprints()->version;
|
||||
if (!version_compare($realVersion, $version, $compare)) {
|
||||
$msg = "Missing Dependency: '$name' $version";
|
||||
if (is_array($issues)) {
|
||||
$issues[] = $msg;
|
||||
} else {
|
||||
$messages->add($msg, 'error');
|
||||
}
|
||||
$errors++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($errors > 0) {
|
||||
$msg = "Plugin '$plugin->name' was not loaded due to dependency issues";
|
||||
if (is_array($issues)) {
|
||||
$issues[] = $msg;
|
||||
} else {
|
||||
$messages->add($msg, 'error');
|
||||
}
|
||||
}
|
||||
return $errors === 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#
|
||||
# The MIT License (MIT)
|
||||
#
|
||||
# Copyright (c) 2018 TwelveTone LLC
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
#
|
||||
|
||||
enabled: true
|
||||
quicksend: false
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* A re-modal wrapper to simplify usage.
|
||||
*/
|
||||
|
||||
function openModalDialog(remodalId) {
|
||||
const dlgElement = $(`[data-remodal-id=${remodalId}]`);
|
||||
const modal = $.remodal.lookup[dlgElement.data('remodal')];
|
||||
modal.open();
|
||||
|
||||
$(dlgElement).find('input[temporary=true]').remove();
|
||||
|
||||
return {
|
||||
show: function () {
|
||||
modal.open();
|
||||
},
|
||||
close: function () {
|
||||
modal.close();
|
||||
},
|
||||
/**
|
||||
* Replaces a message listener
|
||||
* @param selector
|
||||
* @param message
|
||||
* @param callback
|
||||
*/
|
||||
on: function (selector, message, callback) {
|
||||
const found = dlgElement.find(selector);
|
||||
found.off(message);
|
||||
found.on(message, callback);
|
||||
},
|
||||
/**
|
||||
*
|
||||
* @param selector {string}
|
||||
* @returns {element} The HTML element
|
||||
*/
|
||||
get: function (selector) {
|
||||
const found = dlgElement.find(selector);
|
||||
return found[0];
|
||||
},
|
||||
/**
|
||||
*
|
||||
* @param selector {string}
|
||||
* @returns {jquery element}
|
||||
*/
|
||||
jget: function (selector) {
|
||||
const found = dlgElement.find(selector);
|
||||
return found;
|
||||
},
|
||||
setHiddenField: function (name, value, temporary = true) {
|
||||
const form = $(dlgElement).find('form');
|
||||
form.append(`<input type='hidden' temporary='${temporary}' name='${name}' value='${value}' />`);
|
||||
}
|
||||
}
|
||||
// For getting/setting values
|
||||
// var $modal = modal.$modal;
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018 TwelveTone LLC
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
//TODO refactor AJAX code
|
||||
|
||||
function onMediaAction_MediaReplace(actionId, mediaName, mediaElement) {
|
||||
|
||||
if (window._media_replace_isQuicksend) {
|
||||
doQuicksend(actionId, mediaName, mediaElement);
|
||||
return;
|
||||
}
|
||||
|
||||
const form = openModalDialog('MediaReplace');
|
||||
|
||||
form.on('.button[name=cancel]', 'click', () => {
|
||||
form.close();
|
||||
});
|
||||
|
||||
form.on('.button[name=continue]', 'click', () => {
|
||||
form.close();
|
||||
|
||||
const input = form.get('input[type=file]');
|
||||
if (input.files.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
let file = input.files[0];
|
||||
let data = new FormData();
|
||||
data.append('mediaupload', file, file.name);
|
||||
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", GravAdmin.config.base_url_relative + '/admin-media-replace/replace', true);
|
||||
// xhr.setRequestHeader("X_FILENAME", file.name);
|
||||
// Grav is stripping out X_ from $_SERVER
|
||||
xhr.setRequestHeader("X-MEDIA-NEW-FILENAME", file.name);
|
||||
xhr.setRequestHeader("X-MEDIA-ROUTE", '/' + GravAdmin.config.route);
|
||||
xhr.setRequestHeader("X-MEDIA-FILENAME", mediaName);
|
||||
|
||||
data.append("media-new-filename", file.name);
|
||||
data.append("media-route", '/' + GravAdmin.config.route);
|
||||
data.append("media-filename", mediaName);
|
||||
data.append("media-rename", form.jget('input[name=rename_file]:checked').val());
|
||||
data.append("media-match-extension", form.jget('input[name=match_extension]:checked').val());
|
||||
data.append("media-require-image", form.jget('input[name=require_image]:checked').val());
|
||||
|
||||
xhr.onload = function () {
|
||||
//get response and show the uploading status
|
||||
if (xhr.status === 200) {
|
||||
let response = JSON.parse(xhr.responseText);
|
||||
if (response.error) {
|
||||
alert(response.error);
|
||||
}
|
||||
else if (response.thumbnail) {
|
||||
let img = mediaElement.querySelector('img');
|
||||
if (!img) {
|
||||
return;
|
||||
}
|
||||
img.src = response.thumbnail + "?refresh=" + new Date().getTime();
|
||||
|
||||
if (response.toast) {
|
||||
Grav.default.Utils.toastr.info(response.toast);
|
||||
}
|
||||
if (response.newName) {
|
||||
const nameEle = $(mediaElement).find('[data-dz-name]');
|
||||
nameEle.text(response.newName);
|
||||
}
|
||||
} else {
|
||||
location.reload();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(data);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function doQuicksend(actionId, mediaName, mediaElement) {
|
||||
let form = document.querySelector('form[id="replace-media"]');
|
||||
if (!form) {
|
||||
form = $("<form id='replace-media' action='MediaReplace' method='post'>" +
|
||||
"<input type='file' style='visibility: hidden; position: absolute; top: 0px; left: 0px; height: 0px; width: 0px;'></input>" +
|
||||
"</form>")[0];
|
||||
document.body.appendChild(form);
|
||||
|
||||
let input = form.querySelector('input[type=file]');
|
||||
|
||||
input.addEventListener('change', function () {
|
||||
if (input.files.length !== 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
let file = input.files[0];
|
||||
let data = new FormData();
|
||||
data.append('mediaupload', file, file.name);
|
||||
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", GravAdmin.config.base_url_relative + '/admin-media-replace/replace', true);
|
||||
// xhr.setRequestHeader("X_FILENAME", file.name);
|
||||
// Grav is stripping out X_ from $_SERVER
|
||||
xhr.setRequestHeader("X-MEDIA-NEW-FILENAME", file.name);
|
||||
xhr.setRequestHeader("X-MEDIA-ROUTE", '/' + GravAdmin.config.route);
|
||||
xhr.setRequestHeader("X-MEDIA-FILENAME", mediaName);
|
||||
|
||||
data.append("media-new-filename", file.name);
|
||||
data.append("media-route", '/' + GravAdmin.config.route);
|
||||
data.append("media-filename", mediaName);
|
||||
data.append("media-rename", "1");
|
||||
data.append("media-match-extension", "1");
|
||||
data.append("media-require-image", "1");
|
||||
|
||||
xhr.onload = function () {
|
||||
//get response and show the uploading status
|
||||
if (xhr.status === 200) {
|
||||
let response = JSON.parse(xhr.responseText);
|
||||
if (response.error) {
|
||||
alert(response.error);
|
||||
}
|
||||
else if (response.thumbnail) {
|
||||
let img = mediaElement.querySelector('img');
|
||||
if (!img) {
|
||||
return;
|
||||
}
|
||||
img.src = response.thumbnail + "?refresh=" + new Date().getTime();
|
||||
} else {
|
||||
location.reload();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(data);
|
||||
});
|
||||
}
|
||||
let input = form.querySelector('input[type=file]');
|
||||
input.click();
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
#
|
||||
# The MIT License (MIT)
|
||||
#
|
||||
# Copyright (c) 2018 TwelveTone LLC
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
#
|
||||
|
||||
name: Admin Media Replace
|
||||
version: 1.0.4
|
||||
description: Replaces media in the page media bin.
|
||||
icon: plug
|
||||
author:
|
||||
name: TwelveTone LLC
|
||||
email: info@twelvetone.tv
|
||||
homepage: https://www.twelvetone.tv/docs/developer-tools/grav-plugins/grav-admin-media-replace-plugin
|
||||
keywords: grav, plugin, admin, media
|
||||
bugs: https://www.twelvetone.tv/docs/developer-tools/grav-plugins/grav-admin-media-replace-plugin
|
||||
docs: https://www.twelvetone.tv/docs/developer-tools/grav-plugins/grav-admin-media-replace-plugin
|
||||
license: MIT
|
||||
|
||||
dependencies:
|
||||
- { name: grav, version: '>=1.0.0' }
|
||||
- { name: admin, version: '>=1.0.0' }
|
||||
- { name: admin-media-actions, version: '>=1.0.2' }
|
||||
|
||||
form:
|
||||
validation: strict
|
||||
fields:
|
||||
enabled:
|
||||
type: toggle
|
||||
label: Plugin status
|
||||
highlight: 1
|
||||
default: 0
|
||||
options:
|
||||
1: Enabled
|
||||
0: Disabled
|
||||
validate:
|
||||
type: bool
|
||||
|
||||
quicksend:
|
||||
type: toggle
|
||||
label: Quick send
|
||||
description: Bypasses the upload dialog.
|
||||
highlight: 1
|
||||
default: 1
|
||||
options:
|
||||
1: Enabled
|
||||
0: Disabled
|
||||
validate:
|
||||
type: bool
|
||||
@@ -0,0 +1,51 @@
|
||||
form:
|
||||
fields:
|
||||
|
||||
foobar:
|
||||
type: section
|
||||
title: Replace Media
|
||||
|
||||
rename_file:
|
||||
type: toggle
|
||||
label: Rename file
|
||||
description: Renames the uploaded file to the target file name. This includes the basename and extension.
|
||||
highlight: 1
|
||||
default: 1
|
||||
options:
|
||||
1: Yes
|
||||
0: No
|
||||
validate:
|
||||
type: bool
|
||||
|
||||
require_image:
|
||||
type: toggle
|
||||
label: Require image
|
||||
description: Require upload to be an image MIME type.
|
||||
highlight: 0
|
||||
default: 0
|
||||
options:
|
||||
1: Yes
|
||||
0: No
|
||||
validate:
|
||||
type: bool
|
||||
|
||||
match_extension:
|
||||
type: toggle
|
||||
label: Match Extension
|
||||
description: Require new file extension to match current file extension.
|
||||
highlight: 0
|
||||
default: 0
|
||||
options:
|
||||
1: Yes
|
||||
0: No
|
||||
validate:
|
||||
type: bool
|
||||
|
||||
file:
|
||||
type: singlefile
|
||||
label: File
|
||||
description: You can also drop a file on the <em>choose file button</em>.
|
||||
|
||||
spacer:
|
||||
type: spacer
|
||||
text: To bypass this dialog and simply <em>pick and send</em> the file using default values, go to the plugin settings and enable <em>quicksend</em>.
|
||||
@@ -0,0 +1,17 @@
|
||||
plugins {
|
||||
id("com.github.hierynomus.license").version("0.14.0")
|
||||
}
|
||||
apply plugin:'java'
|
||||
|
||||
sourceSets {
|
||||
grav {
|
||||
resources {
|
||||
srcDirs += "."
|
||||
include "**/*.yaml"
|
||||
include "**/*.php"
|
||||
include "**/*.css"
|
||||
include "**/*.js"
|
||||
include "**/*.twig"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
// Do not call until Grav 'twig' is initialized and the base twig extensions are loaded.
|
||||
function addModalForm($jsid, $twigName)
|
||||
{
|
||||
//TODO parametize
|
||||
$bpNewPage = new \Grav\Common\Data\Blueprint("media-replace.yaml");
|
||||
$bpNewPage->setContext(__DIR__ . "/../blueprints");
|
||||
$bpNewPage = $bpNewPage->load()->init();
|
||||
|
||||
$params = [];
|
||||
$params["remodalId"] = $jsid;
|
||||
$params["fields"] = $bpNewPage->toArray()['form']['fields'];
|
||||
|
||||
$grav = \Grav\Common\Grav::instance();
|
||||
$rendered = $grav['twig']->twig()->render($twigName, $params);
|
||||
$arr = [
|
||||
'MODAL' => $rendered
|
||||
];
|
||||
|
||||
$grav['assets']->addInlineJs("var $jsid = " . json_encode($arr) . ';', -1000, false);
|
||||
|
||||
$modalReg = "
|
||||
$(function () {
|
||||
$('body').append($jsid.MODAL);
|
||||
});";
|
||||
$grav['assets']->addInlineJs($modalReg, -999, false);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{% extends "forms/field.html.twig" %}
|
||||
|
||||
{% block label %}
|
||||
{{ label ?: "File" }}
|
||||
{% endblock %}
|
||||
|
||||
{% block input %}
|
||||
<input name="{{ name }}" type="file" style="width: 100%"/>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,22 @@
|
||||
<div class="remodal" data-remodal-id="{{remodalId}}" data-remodal-options="hashTracking: false">
|
||||
<form method="post" onsubmit='return false;'>
|
||||
{% for name, field in fields %}
|
||||
{% if field.type %}
|
||||
{% set value = data.value(name) %}
|
||||
<div class="block block-{{field.type}}">
|
||||
{% include ["forms/fields/#{field.type}/#{field.type}.html.twig", 'forms/fields/text/text.html.twig'] %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
<div class="form-field grid">{{message}}</div>
|
||||
|
||||
<div class="button-bar">
|
||||
<!--<div class="loading">-->
|
||||
<!--{{ "Moving" }}... <i class="fa fa-spinner fa-spin"></i>-->
|
||||
<!--</div>-->
|
||||
<button class="button" style="visibility: visible" name="cancel">{{ "Cancel" }}</button>
|
||||
<button class="button primary" style="visibility: visible" name="continue">{{ "Continue" }}</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
Reference in New Issue
Block a user