Merge pull request #347 from getgrav/feature/form-file-admin

Feature/form file admin
This commit is contained in:
Flavio Copes
2015-12-16 10:23:08 +01:00
4 changed files with 105 additions and 4 deletions

View File

@@ -963,6 +963,95 @@ class AdminController
return true;
}
private function cleanFilesData($key, $file)
{
$config = $this->grav['config'];
$blueprint = isset($this->items['fields'][$key]['files']) ? $this->items['fields'][$key]['files'] : [];
/** @var Page $page */
$page = null;
$cleanFiles[$key] = [];
if (!isset($blueprint)) {
return false;
}
$type = trim("{$this->view}/{$this->admin->route}", '/');
$data = $this->admin->data($type, $this->post);
$fields = $data->blueprints()->fields();
$blueprint = isset($fields[$key]) ? $fields[$key] : [];
$cleanFiles = [$key => []];
foreach ((array)$file['error'] as $index => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $file['tmp_name'][$index];
$name = $file['name'][$index];
$type = $file['type'][$index];
$destination = Folder::getRelativePath(rtrim($blueprint['destination'], '/'));
if (!$this->match_in_array($type, $blueprint['accept'])) {
throw new \RuntimeException('File "' . $name . '" is not an accepted MIME type.');
}
if (Utils::startsWith($destination, '@page:')) {
$parts = explode(':', $destination);
$route = $parts[1];
$page = $this->grav['page']->find($route);
if (!$page) {
throw new \RuntimeException('Unable to upload file to destination. Page route not found.');
}
$destination = $page->relativePagePath();
} else if ($destination == '@self') {
$page = $this->admin->page(true);
$destination = $page->relativePagePath();
} else {
Folder::mkdir($destination);
}
if (move_uploaded_file($tmp_name, "$destination/$name")) {
$path = $page ? $this->grav['uri']->convertUrl($page, $page->route() . '/' . $name) : $destination . '/' . $name;
$cleanFiles[$key][$path] = [
'name' => $file['name'][$index],
'type' => $file['type'][$index],
'size' => $file['size'][$index],
'file' => $destination . '/' . $name,
'route' => $page ? $path : null
];
} else {
throw new \RuntimeException("Unable to upload file(s) to $destination/$name");
}
}
}
return $cleanFiles[$key];
}
private function match_in_array($needle, $haystack)
{
foreach ((array)$haystack as $item) {
if (true == preg_match("#^" . strtr(preg_quote($item, '#'), array('\*' => '.*', '\?' => '.')) . "$#i", $needle)) {
return true;
}
}
return false;
}
private function processFiles($obj)
{
foreach ((array)$_FILES as $key => $file) {
$cleanFiles = $this->cleanFilesData($key, $file);
if ($cleanFiles) {
$obj->set($key, reset($cleanFiles)['name']);
}
}
return $obj;
}
/**
* Handles form and saves the input data if its valid.
*
@@ -1020,6 +1109,7 @@ class AdminController
} else {
// Handle standard data types.
$obj = $this->prepareData();
$obj = $this->processFiles($obj);
$obj->validate();
$obj->filter();
}

View File

@@ -289,13 +289,13 @@
values = {};
// Get form values that are not handled by JS framework
Form.findElements(this.form, 'input, textarea', '', false).each(function(input) {
Form.findElements(this.form, 'input:not([type="file"]), textarea', '', false).each(function(input) {
var input = $(this),
name = input.attr('name'),
parent = input.parent('[data-grav-disabled]'),
value = input.val();
if (input.is(':disabled') || (parent && parent.data('grav-disabled') == 'true')) { return; }
if (input.is(':disabled') || (parent && parent.data('grav-disabled') == 'true') || e.attr('type') != 'file') { return; }
if (name) {
values[name] = value;

View File

@@ -1,6 +1,13 @@
{% set form_id = form_id ? form_id : 'blueprints' %}
<form id="{{ form_id }}" method="post" data-grav-form="{{ form_id }}" data-grav-keepalive="true">
{% set multipart = '' %}
{% for field in blueprints.fields %}
{% if field.type == 'file' %}
{% set multipart = ' enctype="multipart/form-data"' %}
{% endif %}
{% endfor %}
<form id="{{ form_id }}" method="post" data-grav-form="{{ form_id }}" data-grav-keepalive="true"{{ multipart }}>
{% for field in blueprints.fields %}
{% if field.type %}
{% set value = data.value(field.name) %}

View File

@@ -1,6 +1,10 @@
<nav id="admin-sidebar">
<div id="admin-logo">
<h3><a href="{{ base_url_relative }}">{{ "PLUGIN_ADMIN.GRAV_ADMIN"|tu }}</a> <a target="_blank" href="{{ base_url_relative_frontend }}"> <i class="fa fa-fw fa-arrow-circle-right"></i></a></h3>
{% if config.plugins["admin-pro"].custom_logo_top_left %}
<a target="_blank" href="{{ base_url_relative_frontend }}"><img src="{{ base_url_relative_frontend }}/user/plugins/admin-pro/assets/{{ config.plugins["admin-pro"].custom_logo_top_left|first }}" /></a>
{% else %}
<h3><a href="{{ base_url_relative }}">{{ "PLUGIN_ADMIN.GRAV_ADMIN"|tu }}</a> <a target="_blank" href="{{ base_url_relative_frontend }}"> <i class="fa fa-fw fa-arrow-circle-right"></i></a></h3>
{% endif %}
</div>
{#{% if admin.authorize %}#}