Skip to content

Commit

Permalink
Merge branch 'release/4.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Jul 29, 2020
2 parents c630333 + c04935b commit f71681b
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 409 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# v4.1.0
## 07/29/2020

1. [](#new)
* Support JSON based form submissions
1. [](#improved)
* Improved handling of error messages with more details + translation [#428](https://github.com/getgrav/grav-plugin-form/pull/428) [#429](https://github.com/getgrav/grav-plugin-form/pull/429)
* Various improvements for nested form data in `data.html.twig` and `data.txt.twig`
* Use `|length` rather than `|count` twig filter
* Various language updates
1. [](#bugfix)
* Disabled the EXIF library for Dropzone for fixing the orientation as it was getting applied twice [#1923](https://github.com/getgrav/grav-plugin-admin/issues/1923)
* Forked Dropzone fo fix issue with Resize + EXIF orientation [#1923](https://github.com/getgrav/grav-plugin-admin/issues/1923)

# v4.0.10
## 06/08/2020

Expand Down
4 changes: 2 additions & 2 deletions app/fields/file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import $ from 'jquery';
import Dropzone from 'dropzone';
import EXIF from 'exif-js';
// import EXIF from 'exif-js';
import {config, translations} from 'grav-form';

// translations
Expand Down Expand Up @@ -28,7 +28,7 @@ const DropzoneMediaConfig = {
previewTemplate: ''
};

window.EXIF = EXIF;
// window.EXIF = EXIF;

export default class FilesField {
constructor({container = '.dropzone.files-upload', options = {}} = {}) {
Expand Down
257 changes: 11 additions & 246 deletions assets/form.min.js

Large diffs are not rendered by default.

60 changes: 12 additions & 48 deletions assets/form.vendor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Form
slug: form
type: plugin
version: 4.0.10
version: 4.1.0
testing: false
description: Enables the forms handling
icon: check-square
Expand Down
55 changes: 53 additions & 2 deletions classes/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
* @property-read array $files
* @property-read Data $value
* @property array $errors
* @property array $upload_errors
* @property-read array $fields
* @property-read Blueprint $blueprint
* @property-read PageInterface $page
Expand Down Expand Up @@ -548,7 +547,11 @@ public function uploadFiles()
// json_response
return [
'status' => 'error',
'message' => sprintf($language->translate('PLUGIN_FORM.FILEUPLOAD_UNABLE_TO_UPLOAD', null, true), $filename, $this->upload_errors[$upload['file']['error']])
'message' => sprintf(
$language->translate('PLUGIN_FORM.FILEUPLOAD_UNABLE_TO_UPLOAD', null, true),
$filename,
$this->getFileUploadError($upload['file']['error'], $language)
)
];
}

Expand Down Expand Up @@ -700,6 +703,54 @@ public function uploadFiles()
exit;
}

/**
* Return an error message for a PHP file upload error code
* https://www.php.net/manual/en/features.file-upload.errors.php
*
* @param int $error PHP file upload error code
* @param Language|null $language
* @return string File upload error message
*/
public function getFileUploadError(int $error, Language $language = null): string
{
if (!$language) {
$grav = Grav::instance();

/** @var Language $language */
$language = $grav['language'];
}

switch ($error) {
case UPLOAD_ERR_OK:
$item = 'FILEUPLOAD_ERR_OK';
break;
case UPLOAD_ERR_INI_SIZE:
$item = 'FILEUPLOAD_ERR_INI_SIZE';
break;
case UPLOAD_ERR_FORM_SIZE:
$item = 'FILEUPLOAD_ERR_FORM_SIZE';
break;
case UPLOAD_ERR_PARTIAL:
$item = 'FILEUPLOAD_ERR_PARTIAL';
break;
case UPLOAD_ERR_NO_FILE:
$item = 'FILEUPLOAD_ERR_NO_FILE';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$item = 'FILEUPLOAD_ERR_NO_TMP_DIR';
break;
case UPLOAD_ERR_CANT_WRITE:
$item = 'FILEUPLOAD_ERR_CANT_WRITE';
break;
case UPLOAD_ERR_EXTENSION:
$item = 'FILEUPLOAD_ERR_EXTENSION';
break;
default:
$item = 'FILEUPLOAD_ERR_UNKNOWN';
}
return $language->translate('PLUGIN_FORM.'.$item);
}

/**
* Removes a file from the flash object session, before it gets saved.
*/
Expand Down
47 changes: 25 additions & 22 deletions form.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Grav\Plugin;

use Composer\Autoload\ClassLoader;
Expand Down Expand Up @@ -219,9 +220,9 @@ public function onPageInitialized()
// Enable form events if there's a POST
if ($this->shouldProcessForm()) {
$this->enable([
'onFormProcessed' => ['onFormProcessed', 0],
'onFormProcessed' => ['onFormProcessed', 0],
'onFormValidationError' => ['onFormValidationError', 0],
'onFormFieldTypes' => ['onFormFieldTypes', 0],
'onFormFieldTypes' => ['onFormFieldTypes', 0],
]);

/** @var Uri $uri */
Expand All @@ -234,8 +235,9 @@ public function onPageInitialized()
if ($form instanceof Form) {
// Post the form
$isJson = $uri->extension() === 'json';
$task = $uri->post('task') ?? $uri->param('task');

if ($isJson) {
$task = $uri->post('task') ?? $uri->param('task');
if ($task === 'store-state') {
$this->json_response = $form->storeState();
} elseif ($task === 'clear-state') {
Expand All @@ -245,8 +247,9 @@ public function onPageInitialized()
} elseif ($task === 'file-upload' || $uri->post('__form-file-uploader__')) {
$this->json_response = $form->uploadFiles();
}
} else {
$task = $uri->post('task');
}

if (empty($this->json_response)) {
if ($task === 'clear-state') {
$form->getFlash()->delete();
$redirect = $form->getBlueprint()->get('form/clear_redirect_url') ?? $page->route();
Expand Down Expand Up @@ -330,9 +333,9 @@ public function onTwigInitialized()
new \Twig_SimpleFunction('forms', [$this, 'getForm'])
);

$this->grav['twig']->twig()->getExtension('Twig_Extension_Core')->setEscaper('yaml', function($twig, $string, $charset) {
$this->grav['twig']->twig()->getExtension('Twig_Extension_Core')->setEscaper('yaml', function ($twig, $string, $charset) {
return Yaml::dump($string);
}
}
);

}
Expand Down Expand Up @@ -400,7 +403,7 @@ public function onFormProcessed(Event $event)
$ip = Uri::ip();

$recaptcha = new ReCaptcha($secret);
if(extension_loaded('curl')){
if (extension_loaded('curl')) {
$recaptcha = new ReCaptcha($secret, new CurlPost());
}

Expand Down Expand Up @@ -431,13 +434,13 @@ public function onFormProcessed(Event $event)
$type = $field['type'] ?? 'text';
$field_message = $field['recaptcha_not_validated'] ?? null;
if ($type === 'captcha' && $field_message) {
$message = $field_message;
$message = $field_message;
break;
}
}

$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $form,
'form' => $form,
'message' => $message
]));

Expand All @@ -452,16 +455,16 @@ public function onFormProcessed(Event $event)
$label = $params['label'] ?? 'Timestamp';
$format = $params['format'] ?? 'Y-m-d H:i:s';
$blueprint = $form->value()->blueprints();
$blueprint->set('form/fields/timestamp', ['name'=>'timestamp', 'label'=> $label, 'type'=>'hidden']);
$blueprint->set('form/fields/timestamp', ['name' => 'timestamp', 'label' => $label, 'type' => 'hidden']);
$now = new \DateTime('now');
$date_string = $now->format($format);
$form->setFields($blueprint->fields());
$form->setData('timestamp',$date_string);
$form->setData('timestamp', $date_string);
break;
case 'ip':
$label = $params['label'] ?? 'User IP';
$blueprint = $form->value()->blueprints();
$blueprint->set('form/fields/ip', ['name'=>'ip', 'label'=> $label, 'type'=>'hidden']);
$blueprint->set('form/fields/ip', ['name' => 'ip', 'label' => $label, 'type' => 'hidden']);
$form->setFields($blueprint->fields());
$form->setData('ip', Uri::ip());
break;
Expand Down Expand Up @@ -507,7 +510,7 @@ public function onFormProcessed(Event $event)
if (!$route || $route[0] !== '/') {
/** @var Uri $uri */
$uri = $this->grav['uri'];
$route = rtrim($uri->route(), '/'). '/' . ($route ?: '');
$route = rtrim($uri->route(), '/') . '/' . ($route ?: '');
}

/** @var Twig $twig */
Expand All @@ -527,8 +530,8 @@ public function onFormProcessed(Event $event)
break;
case 'remember':
foreach ($params as $remember_field) {
$field_cookie = 'forms-'.$form['name'].'-'.$remember_field;
setcookie($field_cookie, $form->value($remember_field), time()+60*60*24*60);
$field_cookie = 'forms-' . $form['name'] . '-' . $remember_field;
setcookie($field_cookie, $form->value($remember_field), time() + 60 * 60 * 24 * 60);
}
break;
case 'upload':
Expand All @@ -551,7 +554,7 @@ public function onFormProcessed(Event $event)
throw new \RuntimeException('Form save: \'operation: add\' is only supported with a static filename');
}

$filename = $prefix . $this->udate($format, $raw_format) . $postfix. $ext;
$filename = $prefix . $this->udate($format, $raw_format) . $postfix . $ext;
}

/** @var Twig $twig */
Expand All @@ -566,7 +569,7 @@ public function onFormProcessed(Event $event)
$locator = $this->grav['locator'];
$path = $locator->findResource('user-data://', true);
$dir = $path . DS . $folder;
$fullFileName = $dir. DS . $filename;
$fullFileName = $dir . DS . $filename;

if (!empty($params['raw']) || !empty($params['template'])) {
// Save data as it comes from the form.
Expand Down Expand Up @@ -677,7 +680,7 @@ public function onFormValidationProcessed(Event $event)
/**
* Handle form validation error
*
* @param Event $event An event object
* @param Event $event An event object
* @throws \Exception
*/
public function onFormValidationError(Event $event)
Expand Down Expand Up @@ -821,7 +824,7 @@ public function getFormFieldTypes()
'key' => [
'input@' => false
],
'section' => [
'section' => [
'input@' => false
],
'spacer' => [
Expand Down Expand Up @@ -923,7 +926,7 @@ protected function shouldProcessForm()
}

if (isset($form->refresh_prevention)) {
$refresh_prevention = (bool) $form->refresh_prevention;
$refresh_prevention = (bool)$form->refresh_prevention;
} else {
$refresh_prevention = $this->config->get('plugins.form.refresh_prevention', false);
}
Expand Down Expand Up @@ -1080,7 +1083,7 @@ protected function getFormCacheId()
* Create unix timestamp for storing the data into the filesystem.
*
* @param string $format
* @param bool $raw
* @param bool $raw
*
* @return string
*/
Expand Down
66 changes: 33 additions & 33 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
'use strict';

var gulp = require('gulp'),
util = require('util'),
path = require('path'),
immutable = require('immutable'),
gulpWebpack = require('gulp-webpack'),
webpack = require('webpack'),
sourcemaps = require('gulp-sourcemaps'),
exec = require('child_process').execSync,
sass = require('gulp-sass'),
cleancss = require('gulp-clean-css'),
csscomb = require('gulp-csscomb'),
rename = require('gulp-rename'),
autoprefixer = require('gulp-autoprefixer'),
pwd = exec('pwd').toString();
var gulp = require('gulp');
var util = require('util');
var path = require('path');
var immutable = require('immutable');
var gulpWebpack = require('gulp-webpack');
var webpack = require('webpack');
var sourcemaps = require('gulp-sourcemaps');
var exec = require('child_process').execSync;
var sass = require('gulp-sass');
var cleancss = require('gulp-clean-css');
var csscomb = require('gulp-csscomb');
var rename = require('gulp-rename');
var autoprefixer = require('gulp-autoprefixer');
var pwd = exec('pwd').toString();

// configure the paths
var watch_dir = './scss/**/*.scss';
Expand All @@ -24,23 +24,23 @@ var paths = {
source: src_dir
};

var plugins = {},
base = immutable.fromJS(require('./webpack.conf.js')),
options = {
prod: base.mergeDeep({
devtool: 'source-map',
optimization: {minimize: true},
plugins: [
new webpack.DefinePlugin({
'process.env': { NODE_ENV: '"production"' }
}),
new webpack.ProvidePlugin(plugins)
],
output: {
filename: 'form.min.js'
}
})
};
var plugins = {};
var base = immutable.fromJS(require('./webpack.conf.js'));
var options = {
prod: base.mergeDeep({
devtool: 'source-map',
optimization: {minimize: true},
plugins: [
new webpack.DefinePlugin({
'process.env': { NODE_ENV: '"production"' }
}),
new webpack.ProvidePlugin(plugins)
],
output: {
filename: 'form.min.js'
}
})
};

// var compileJS = function(watch) {
// var prodOpts = options.prod.set('watch', watch);
Expand All @@ -65,7 +65,7 @@ var compileCSS = function() {
suffix: '.min'
}))
.pipe(gulp.dest(dest_dir));
}
};

// gulp.task('js', function() {
// compileJS(false);
Expand All @@ -81,4 +81,4 @@ gulp.task('watch', function() {
});

gulp.task('all', ['css']);
gulp.task('default', ['all']);
gulp.task('default', ['all']);
Loading

0 comments on commit f71681b

Please sign in to comment.