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:
2026-04-19 00:19:51 +02:00
parent ded82bd24d
commit db54757311
37 changed files with 2042 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
# v1.0.4
## 2/15/2018
1. [](#new)
* Update styles for Grav Admin v1.7.
# v1.0.3
## 2/15/2018
1. [](#new)
* Add column margin to media actions.
* Add border-radius to media actions.
# v1.0.2
## 2/14/2018
1. [](#new)
* Update bugs URL.
* Update addAction signature
# v1.0.1
## 2/7/2018
1. [](#new)
* Show alert dialog for sample actions.
* Update sample action log message.
# v1.0.0
## 1/24/2018
1. [](#Initial)
*
+21
View File
@@ -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.
+34
View File
@@ -0,0 +1,34 @@
# Admin Media Actions Plugin
The **Admin Media Actions** Plugin is for [Grav CMS](http://github.com/getgrav/grav). A plugin which adds an API for adding actions items to media items in the media bin.
## Installation
Installing the Admin Media Actions 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-actions
This will install the Admin Media Actions plugin into your `/user/plugins` directory within Grav. Its files can be found under `/your/site/grav/user/plugins/admin-media-actions`.
### 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-actions`. 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-actions
> 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 online [documentation](https://www.twelvetone.tv/docs/developer-tools/grav-plugins/grav-admin-media-actions-plugin)
## Credits
A big thanks to David Szabo and his development on the grav-plugin-admin-addon-media-rename for some clever approaches
on tapping into the Grav architecture.
@@ -0,0 +1,227 @@
<?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\Plugin;
/**
* Class AdminMediaActionsPlugin
* @package Grav\Plugin
*/
class AdminMediaActionsPlugin extends Plugin
{
const ROUTE = '/admin-media-actions';
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
public function getPath()
{
return '/' . trim($this->grav['admin']->base, '/') . '/' . trim(self::ROUTE, '/');
}
public function buildBaseUrl()
{
$ret = rtrim($this->grav['uri']->rootUrl(false), '/') . '/' . trim($this->getPath(), '/');
return $ret;
}
public function onPluginsInitialized()
{
if (!$this->isAdmin() || !$this->grav['user']->authenticated) {
return;
}
// Register the media actions service
$this->grav['media-actions'] = function ($c) {
return new MediaActionsController();
};
// Ignore requests to the plugin URL
if ($this->grav['uri']->path() == $this->getPath()) {
return;
}
if ($this->config->get('plugins.admin-media-actions.show_samples')) {
// Sample Actions
$this->grav['media-actions']->addAction("SampleAction1", "Sample Action 1", "play-circle", function ($page, $mediaName, $payload) {
return [
"path" => $page->path(),
"route" => $page->route(),
"mediaName" => $mediaName,
];
});
$this->grav['media-actions']->addAction("SampleAction2", "Sample Action 2", "play-circle", function ($page, $mediaName, $payload) {
return [
"path" => $page->path(),
"route" => $page->route(),
"mediaName" => $mediaName,
];
});
$this->grav['media-actions']->addAction("SampleAction3", "Sample Action 3", "play-circle", function ($page, $mediaName, $payload) {
return [
"path" => $page->path(),
"route" => $page->route(),
"mediaName" => $mediaName,
];
});
$this->grav['media-actions']->addAction("SampleForm", "Sample Form", "list", function ($page, $mediaName, $payload) {
return "ok";
});
}
$this->enable([
'onAdminTwigTemplatePaths' => ['onAdminTwigTemplatePaths', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onPagesInitialized' => ['onTwigExtensions', 0],
'onAdminTaskExecute' => ['onAdminTaskExecute', 0],
]);
}
public function onAdminTaskExecute($e)
{
$method = $e['method'];
switch ($method) {
case "taskMedia-action":
$page = $this->grav['admin']->page(false);
//$route = $page->route();
$actionId = $_POST['action_id'];
$media_name = $_POST['media_name'];
$payload = json_decode($_POST['payload'], true);
$handler = $this->grav['media-actions']->getHandlerForAction($actionId);
if ($handler) {
$json = $handler($page, $media_name, $payload);
die("{\"result\":" . json_encode($json) . "}");
} else {
die("{\"result\":{\"error\":true}}");
}
break;
default:
return false;
}
}
public function onAdminTwigTemplatePaths()
{
// $event['paths'] = __DIR__ . '/themes/grav/templates';
}
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function onTwigExtensions()
{
$page = $this->grav['admin']->page(true);
if (!$page) {
return;
}
if ($this->config->get('plugins.admin-media-actions.show_samples')) {
$this->grav['assets']->addJs('plugin://admin-media-actions/assets/samples/sample_actions.js', -1000, false);
$this->grav['assets']->addJs('plugin://admin-media-actions/assets/samples/sample_form_action.js', -1000, false);
}
$oCopy = [];
foreach ($this->grav['media-actions']->actions as $action) {
$oCopy[] = [
'actionId' => $action['actionId'],
'icon' => $action['icon'],
'caption' => $action['caption'],
];
}
$this->grav['assets']->addInlineJs('const MEDIA_ACTIONS = ' . json_encode($oCopy) . ';', -1000, false);
$taskUrl = $this->buildBaseUrl() . $page->route() . '/task:media-action';
$this->grav['assets']->addInlineJs('const MEDIA_ACTION_TASK_URL = ' . json_encode($taskUrl) . ';', -1000, false);
$this->grav['assets']->addJs('plugin://admin-media-actions/assets/admin-media-actions.js', -1000, false);
$this->grav['assets']->addCss('plugin://admin-media-actions/assets/admin-media-actions.css', -1000, false);
}
public function outputError($msg)
{
header('HTTP/1.1 400 Bad Request');
die(json_encode(['error' => ['msg' => $msg]]));
}
}
class MediaActionsController
{
public $actions = [];
/**
* @param $actionId A unique id for the action. Must be a valid Javascript function name.
* This can also be an array containing keys of the same parameter names.
*
* @param $caption The caption for the action. Used for the tooltip.
* @param $icon The font-awesome icon name. The 'fa-' prefix is optional.
* @param $handler A handler for the action. (page, mediaName, payload) => object.
*/
function addAction($actionId, $caption = null, $icon = null, $handler = null)
{
if (is_array($actionId)) {
if (isset($actionId['caption'])) {
$caption = $actionId['caption'];
}
if (isset($actionId['icon'])) {
$icon = $actionId['icon'];
}
if (isset($actionId['handler'])) {
$handler = $actionId['handler'];
}
// do this last...
$actionId = $actionId['actionId'];
}
$this->actions[$actionId] = [
'handler' => $handler,
'caption' => $caption,
'icon' => $icon,
'actionId' => $actionId,
];
}
function getHandlerForAction($actionId)
{
if (isset($this->actions[$actionId])) {
return $this->actions[$actionId]['handler'];
} else {
return null;
}
}
}
@@ -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
show_samples: false
@@ -0,0 +1,62 @@
/**
* 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.
*/
span[data-dz-name] {
cursor: text;
}
.dz-rename:hover:after {
color: #0082ba;
}
/*.dropzone .dz-preview:hover .dz-rename {*/
/*display: block;*/
/*}*/
.dropzone .dz-preview:hover .dz-media-action {
display: flex;
}
.dz-media-action {
display: none;
position: absolute;
width: 25px;
height: 25px;
cursor: pointer;
background: #e1e1e1;
justify-content: center;
align-items: center;
border-radius: 2px;
}
body.ga-theme-17x .dz-media-action {
background: #f2f2f2;
}
.dz-media-action i {
color: #737c81;
}
.dz-media-action i:hover {
color: #0082ba;
}
@@ -0,0 +1,119 @@
/*
* 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.
*/
// MEDIA_ACTIONS must be set
// MEDIA_ACTION_TASK_URL must be set
function _onMediaAction(actionId, mediaName, dz) {
let fn = "onMediaAction_" + actionId;
if (typeof window[fn] === 'function') {
window[fn].apply(null, [actionId, mediaName, dz]);
} else {
submitMediaAction(actionId, mediaName, "");
}
}
function submitMediaAction(actionId, mediaName, payload = "", callback = null, modal = null) {
if (modal) {
$('.loading', modal).removeClass('hidden');
$('.button', modal).addClass('hidden');
}
var data = new FormData();
data.append('admin-nonce', GravAdmin.config.admin_nonce);
data.append("action_id", actionId);
data.append("media_name", mediaName);
data.append("payload", JSON.stringify(payload));
fetch(MEDIA_ACTION_TASK_URL, {method: 'POST', body: data, credentials: 'same-origin'})
.then(res => res.json())
.then(result => {
if (modal) {
if (!result.error) {
modal.close();
}
}
if (callback) {
callback(result);
}
});
}
// Check for new media every 1000 ms and add actions
setInterval(function () {
const size = 25; // The action icon size
const maxRows = 5;
const colMargin = 2;
$('.dz-preview').each(function (i, dz) {
if (!dz._actions) {
dz._actions = true;
let actionsIndex = 3; //TODO hardcoded to standard action count
//let top = 72; //TODO get max top of children (they are not in order)
//let top = actionsCount * size - size; // the standard icons ar off by 1 pixel!?
const that = this;
MEDIA_ACTIONS.forEach(function (item) {
actionsIndex++;
let faIcon = item.icon;
if (!faIcon) {
faIcon = "fa-play-circle";
}
if (!faIcon.startsWith('fa-')) {
faIcon = 'fa-' + faIcon;
}
const ele = document.createElement('a');
ele.className = 'dz-media-action';
ele.style.top = (Math.floor(actionsIndex % maxRows) * size - (Math.floor(actionsIndex % maxRows)) - 1) + 'px';
let right;
const col = Math.floor(actionsIndex / maxRows);
if (col === 0) {
right = -size;
} else {
right = -((1 + Math.floor((actionsIndex) / maxRows)) * size) - (col * colMargin);
}
ele.style.right = right + 'px';
ele.href = 'javascript:undefined;';
ele.title = item.caption;
ele.innerText = "";//item.caption;
const nameEle = $(dz).find('[data-dz-name]');
ele._file_name = nameEle.text();
ele._dz_preview = dz;
$(that).append(ele);
const i = document.createElement("i");
ele.appendChild(i);
i.className = 'fa fa-fw ' + faIcon;
ele.addEventListener('click', () => _onMediaAction(item.actionId, nameEle.text(), dz));
//Invisible div to maintain hover when the mouseover is on the column margin
const ele2 = document.createElement('div');
ele2.className = 'dz-media-action';
ele2.style.background = 'transparent';
ele2.style.right = (right + colMargin) + "px";
ele2.style.top = ele.style.top;
$(ele2).insertBefore(ele);
});
dz.style.marginRight = 15 + Math.floor(actionsIndex / maxRows) * (size + colMargin) + "px";
}
});
}, 1000);
@@ -0,0 +1,47 @@
/*
* 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.
*/
function onMediaAction_SampleAction1(actionId, mediaName, mediaElement) {
console.log(`Media action ${actionId} was selected.`);
alert(`Media action ${actionId} was selected.`);
submitMediaAction(actionId, mediaName, `${actionId} was selected`, function (result) {
console.log(result);
});
}
function onMediaAction_SampleAction2(actionId, mediaName, mediaElement) {
console.log(`Media action ${actionId} was selected.`);
alert(`Media action ${actionId} was selected.`);
submitMediaAction(actionId, mediaName, `${actionId} was selected`, function (result) {
console.log(result);
});
}
function onMediaAction_SampleAction3(actionId, mediaName, mediaElement) {
console.log(`Media action ${actionId} was selected.`);
alert(`Media action ${actionId} was selected.`);
submitMediaAction(actionId, mediaName, `${actionId} was selected`, function (result) {
console.log(result);
});
}
@@ -0,0 +1,29 @@
/*
* 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.
*/
// Sample Form Action
function onMediaAction_SampleForm(actionId, mediaName, mediaElement) {
alert("This is a sample form.");
submitMediaAction(actionId, mediaName, '{"key1":"value1", "key2":"value2"}');
}
@@ -0,0 +1,66 @@
#
# 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 Actions
version: 1.0.4
description: A plugin that extends Grav with an API for adding actions to media items in the page media bin. This plugin is required by other plugins that add media actions.
icon: plug
author:
name: TwelveTone LLC
email: info@twelvetone.tv
homepage: https://www.twelvetone.tv/docs/developer-tools/grav-plugins/grav-admin-media-actions-plugin
keywords: grav, plugin, admin, media, action
bugs: https://github.com/Flamenco/grav-admin-media-actions/issues
docs: https://www.twelvetone.tv/docs/developer-tools/grav-plugins/grav-admin-media-actions-plugin
license: MIT
dependencies:
- { name: grav, version: '>=1.0.0' }
- { name: admin, version: '>=1.0.0' }
form:
validation: strict
fields:
enabled:
type: toggle
label: Plugin status
highlight: 1
default: 0
options:
1: Enabled
0: Disabled
validate:
type: bool
show_samples:
type: toggle
label: Show sample actions
description: For testing purposes, several actions will be added to each media item.
highlight: 0
default: 0
options:
1: Enabled
0: Disabled
validate:
type: bool
+17
View File
@@ -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 @@
<!--A generic form dialog that contains a loading indicator, submit button, and message -->
<div class="remodal" data-remodal-id="modal-admin-media-actions" data-remodal-options="hashTracking: false">
<form method="post" onsubmit='return false;'>
{% for field in fields %}
{% if field.type %}
{% set value = data.value(field.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="block page-media-info hidden">
<div class="form-field grid">{{ "Page Info" }}</div>
</div>
<div class="block non-page-media-info hidden">
<div class="form-field grid">{{ "Non-Page Info" }}</div>
</div>
<div class="button-bar">
<div class="loading">
{{ "Moving" }}... <i class="fa fa-spinner fa-spin"></i>
</div>
<button class="button primary" style="visibility: hidden">{{ "PLUGIN_ADMIN.CONTINUE"|tu }}</button>
</div>
</form>
</div>
+30
View File
@@ -0,0 +1,30 @@
# v1.0.4
## 12/11/2018
1. [](#bugfix)
* Update addInlineJs load order
# 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
## 2/14/2018
1. [](#new)
* Update bugs URL
* Update addAction signature
# v1.0.0
## 1/24/2018
1. [](#initial)
*
+21
View File
@@ -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.
+27
View File
@@ -0,0 +1,27 @@
# Admin Media Move Plugin
The **Admin Media Move** Plugin is for [Grav CMS](http://github.com/getgrav/grav). A plugin which adds the option to move media files in the page bin to another page.
## Installation
Installing the Admin Media Move 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-move
This will install the Admin Media Move plugin into your `/user/plugins` directory within Grav. Its files can be found under `/your/site/grav/user/plugins/admin-media-move`.
### 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-move`. 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-move
> 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
@@ -0,0 +1,234 @@
<?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\Plugin;
/**
* Class AdminMediaMovePlugin
* @package Grav\Plugin
*/
class AdminMediaMovePlugin extends Plugin
{
const ROUTE = '/admin-media-move';
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
public function getPath()
{
return '/' . trim($this->grav['admin']->base, '/') . '/' . trim(self::ROUTE, '/');
}
public function buildBaseUrl()
{
$ret = rtrim($this->grav['uri']->rootUrl(false), '/') . '/' . trim($this->getPath(), '/');
return $ret;
}
public function onPluginsInitialized()
{
if (!$this->isAdmin() || !$this->grav['user']->authenticated) {
return;
}
if ($this->grav['uri']->path() == $this->getPath()) {
return;
}
if (!self::_checkDependencies($this)) {
return;
}
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onPagesInitialized' => ['onTwigExtensions', 0],
]);
$this->grav['media-actions']->addAction([
'actionId' => "MediaMove",
'caption' => "Move",
'icon' => "arrows",
'handler' => function ($page, $mediaName, $payload) {
$destination_route = $payload['destination_route'];
if (!$destination_route || !$page || !$mediaName || !$payload) {
$this->outputError("Invalid input");
}
$basePath = $page->path() . DS;
$filePath = $basePath . $mediaName;
if (!file_exists($filePath)) {
$this->outputError("Media file not found");
}
// Locate the target page
$targetPage = $this->grav['pages']->find($destination_route);
if (!$targetPage) {
$this->outputError("Page for route $destination_route not found");
}
$path = $targetPage->path();
try {
rename($filePath, "$path/$mediaName");
$this->grav['log']->info("Moved media file '$mediaName' to '$path'");
} catch (\Exception $e) {
$this->outputError("Could not move file: " . $e);
}
$ret = [
"error" => false
];
// Redirects will not work for fetch, so send destination url in result
if (get($payload, "go", false)) {
// Get the admin edit-page url
//$url = $this->grav['twig']->twig->getExtension('Grav\Plugin\Admin\AdminTwigExtension')->getPageUrl($this, $targetPage);
$url = $this->grav['uri']->rootUrl(false) . "/admin/pages" . $targetPage->route();
$ret["destination_url"] = $url;
}
//header('HTTP/1.1 200 OK');
return $ret;
}
]);
}
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function onTwigExtensions()
{
$page = $this->grav['admin']->page(true);
if (!$page) {
return;
}
$modal_move = $this->grav['twig']->twig()->render('move-modal.twig.html', $this->config->get('plugins.admin-media-move.modal_move'));
$jsConfig_move = [
'MODAL' => $modal_move
];
$this->grav['assets']->addInlineJs('var ADMIN_ADDON_MEDIA_MOVE = ' . json_encode($jsConfig_move) . ';', -1000);
$this->grav['assets']->addJs('plugin://admin-media-move/assets/media_move_action.js', -1000, false);
}
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,44 @@
#
# 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
modal_move:
fields:
- type: section
title: "Move Media"
- type: text
label: Filename
name: file_name
readonly: true
- type: pages
label: Destination Page
name: destination_page
- type: text
label: Destination Route
name: destination_route
autofocus: on
@@ -0,0 +1,70 @@
/*
* 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.
*/
// This must be in a function call for remodal to register it
$(function () {
$('body').append(ADMIN_ADDON_MEDIA_MOVE.MODAL);
});
function onMediaAction_MediaMove(actionId, mediaName, mediaElement) {
var modal = $.remodal.lookup[$('[data-remodal-id=modal-admin-media-move]').data('remodal')];
modal.open();
var $modal = modal.$modal;
// Populate fields
$('[name=file_name]', $modal).val(mediaName);
$('[name=destination_route]', $modal).val("");
$('[name=destination_page]', $modal).val("");
// Reset loading state
$('.loading', $modal).addClass('hidden');
$('.button', $modal).removeClass('hidden').css('visibility', 'visible');
$(document).off('click', '[data-remodal-id=modal-admin-media-move] .button');
$(document).on('click', '[data-remodal-id=modal-admin-media-move] .button', function (e) {
var destination_route = $('[name=destination_route]').val();
if (!destination_route) {
destination_route = $('[name=destination_page]').val();
}
if (destination_route) {
const payload = {
destination_route
};
if (e.target.name === 'move_and_go') {
payload.go = true;
}
const callback = function (result) {
if (result.error) {
alert(result.error.msg);
} else {
$(mediaElement).remove();
if (payload.go) {
window.location = result.result.destination_url;
}
}
};
submitMediaAction(actionId, mediaName, payload, callback, modal);
}
});
}
+55
View File
@@ -0,0 +1,55 @@
#
# 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 Move
version: 1.0.4
description: Moves media from one page to another.
icon: plug
author:
name: TwelveTone LLC
email: info@twelvetone.tv
homepage: https://www.twelvetone.tv/docs/developer-tools/grav-plugins/grav-admin-media-move-plugin
keywords: grav, plugin, admin, media
bugs: https://github.com/Flamenco/grav-admin-media-move
docs: https://www.twelvetone.tv/docs/developer-tools/grav-plugins/grav-admin-media-move-plugin
license: MIT
dependencies:
- { name: grav, version: '>=1.0.0' }
- { name: admin, version: '>=1.0.0' }
- { name: admin-media-actions, version: '>=1.0.0' }
form:
validation: strict
fields:
enabled:
type: toggle
label: Plugin status
highlight: 1
default: 0
options:
1: Enabled
0: Disabled
validate:
type: bool
+17
View File
@@ -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,23 @@
<div class="remodal" data-remodal-id="modal-admin-media-move" data-remodal-options="hashTracking: false">
<form method="post" onsubmit='return false;'>
{% for field in fields %}
{% if field.type %}
{% set value = data.value(field.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">Select a destination page or enter the destination page route.</div>
<div class="button-bar">
<div class="loading">
{{ "Moving" }}... <i class="fa fa-spinner fa-spin"></i>
</div>
<button class="button primary" style="visibility: hidden" name="move">{{ "Move" }}</button>
<button class="button" style="visibility: hidden" name="move_and_go">{{ "Move And Go" }}</button>
</div>
</form>
</div>
+32
View File
@@ -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)
*
+21
View File
@@ -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.
+29
View File
@@ -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>.
+17
View File
@@ -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>