Skip to content

Commit

Permalink
Merge branch 'release/2.12.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Feb 22, 2018
2 parents 03d1b50 + 609e4f0 commit 34a631c
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 119 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# v2.12.0
## 02/22/2018

1. [](#new)
* Added toggle to enable/disable client-side HTML5 validation
* Added toggle to enable/disable inline-error messages
1. [](#bugfix)
* Fixed an issue with in-content Twig forms not working because forms were not initialized yet
1. [](#improved)
* Reformatted `form.php` plugin class for better readability

# v2.11.5
## 02/16/2018

Expand Down
27 changes: 8 additions & 19 deletions assets/form-styles.css
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
.form-errors {
background: #fdf7f7;
color: #b52b27;
padding: 0 5px;
.form-group.has-errors {
background: rgba(255,0,0,0.05);
border: 1px solid rgba(255,0,0,0.2);
border-radius: 3px;
margin-bottom: 10px;
}

.form-errors p {
margin: 0;
line-height: 2;
}

.form-field.has-errors .form-errors {
margin-top: -5px;
margin: 0 -5px;
padding: 0 5px;
}

.form-field.has-errors label {
.form-errors {
color: #b52b27;
}

.form-field.has-errors .form-input-wrapper input,
.form-field.has-errors .form-input-wrapper select,
.form-field.has-errors .form-input-wrapper textarea {
border: 1px solid #d9534f;
.form-errors p {
margin: 0;
}

.form-input-file.dropzone {
Expand Down
26 changes: 25 additions & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Form
version: 2.11.5
version: 2.12.0
description: Enables the forms handling
icon: check-square
author:
Expand Down Expand Up @@ -55,6 +55,30 @@ form:
validate:
type: bool

client_side_validation:
type: toggle
label: PLUGIN_FORM.CLIENT_SIDE_VALIDATION
help: PLUGIN_FORM.CLIENT_SIDE_VALIDATION_HELP
highlight: 1
default: 1
options:
1: Enabled
0: Disabled
validate:
type: bool

inline_errors:
type: toggle
label: PLUGIN_FORM.INLINE_ERRORS
help: PLUGIN_FORM.INLINE_ERRORS_HELP
highlight: 0
default: 0
options:
1: Enabled
0: Disabled
validate:
type: bool

files:
type: section
title: PLUGIN_FORM.FILES
Expand Down
224 changes: 130 additions & 94 deletions form.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class_alias('Grav\Plugin\Form\Form', 'Grav\Plugin\Form');

$this->enable([
'onPageProcessed' => ['onPageProcessed', 0],
'onPagesInitialized' => ['onPagesInitialized', 0],
'onPageInitialized' => ['onPageInitialized', 0],
'onTwigInitialized' => ['onTwigInitialized', 0],
'onTwigPageVariables' => ['onTwigVariables', 0],
Expand Down Expand Up @@ -121,53 +122,24 @@ public function onPageProcessed(Event $e)
}

/**
* Add a form to the forms plugin
*
* @param $page_route
* @param $form
* Initialize all the forms
*/
public function addForm($page_route, $form)
public function onPagesInitialized()
{

$form_array = [$form['name'] => $form];
if (array_key_exists($page_route, $this->forms)) {
if (!isset($this->form[$page_route][$form['name']])) {
$this->forms[$page_route] = array_merge($this->forms[$page_route], $form_array);
}
} else {
$this->forms[$page_route] = $form_array;

}

$this->flattenForms();
$this->recache_forms = true;
$this->loadCachedForms();
}

/**
* Initialize form if the page has one. Also catches form processing if user posts the form.
* Catches form processing if user posts the form.
*/
public function onPageInitialized()
{
$submitted = false;
$this->json_response = [];
$cache_id = $this->grav['pages']->getPagesCacheId() . '-form-plugin';

// Get and set the cache of forms if it exists
list($forms, $flat_forms) = $this->grav['cache']->fetch($cache_id);

// Only store the forms if they are an array
if (is_array($forms)) {
$this->forms = array_merge($this->forms, $forms);
}

// Only store the flat_forms if they are an array
if (is_array($flat_forms)) {
$this->flat_forms = array_merge($this->flat_forms, $flat_forms);
}

// Save the current state of the forms to cache
// Save cached forms
if ($this->recache_forms) {
$this->grav['cache']->save($cache_id, [$this->forms, $this->flat_forms]);
$this->saveCachedForms();
}

// Enable form events if there's a POST
Expand Down Expand Up @@ -497,73 +469,26 @@ public function onFormValidationError(Event $event)
}

/**
* Get list of form field types specified in this plugin. Only special types needs to be listed.
* Add a form to the forms plugin
*
* @return array
* @param $page_route
* @param $form
*/
public function getFormFieldTypes()
public function addForm($page_route, $form)
{
return [
'column' => [
'input@' => false
],
'columns' => [
'input@' => false
],
'fieldset' => [
'input@' => false
],
'conditional' => [
'input@' => false
],
'display' => [
'input@' => false
],
'spacer' => [
'input@' => false
],
'captcha' => [
'input@' => false
]
];
}

/**
* Process a form
*
* Currently available processing tasks:
*
* - fillWithCurrentDateTime
*
* @param Form $form
*/
protected function process($form)
{
foreach ($form->fields as $field) {
if (!empty($field['process']['fillWithCurrentDateTime'])) {
$form->setData($field['name'], gmdate('D, d M Y H:i:s', time()));
$form_array = [$form['name'] => $form];
if (array_key_exists($page_route, $this->forms)) {
if (!isset($this->form[$page_route][$form['name']])) {
$this->forms[$page_route] = array_merge($this->forms[$page_route], $form_array);
}
}
}
} else {
$this->forms[$page_route] = $form_array;

/**
* Create unix timestamp for storing the data into the filesystem.
*
* @param string $format
* @param int $utimestamp
*
* @return string
*/
private function udate($format = 'u', $utimestamp = null)
{
if (null === $utimestamp) {
$utimestamp = microtime(true);
}

$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000000);

return date(preg_replace('`(?<!\\\\)u`', \sprintf('%06d', $milliseconds), $format), $timestamp);
$this->flattenForms();
$this->recache_forms = true;
}

/**
Expand Down Expand Up @@ -616,6 +541,56 @@ public function getForm($data = null)
return $this->getFormByName($form_name);
}

/**
* Get list of form field types specified in this plugin. Only special types needs to be listed.
*
* @return array
*/
public function getFormFieldTypes()
{
return [
'column' => [
'input@' => false
],
'columns' => [
'input@' => false
],
'fieldset' => [
'input@' => false
],
'conditional' => [
'input@' => false
],
'display' => [
'input@' => false
],
'spacer' => [
'input@' => false
],
'captcha' => [
'input@' => false
]
];
}

/**
* Process a form
*
* Currently available processing tasks:
*
* - fillWithCurrentDateTime
*
* @param Form $form
*/
protected function process($form)
{
foreach ($form->fields as $field) {
if (!empty($field['process']['fillWithCurrentDateTime'])) {
$form->setData($field['name'], gmdate('D, d M Y H:i:s', time()));
}
}
}

/**
* Get current page's route
*
Expand Down Expand Up @@ -726,4 +701,65 @@ protected function form($page = null)

return $this->form;
}

/**
* Load cached forms and merge with any currently found forms
*/
protected function loadCachedForms()
{
// Get and set the cache of forms if it exists
list($forms, $flat_forms) = $this->grav['cache']->fetch($this->getFormCacheId());

// Only store the forms if they are an array
if (is_array($forms)) {
$this->forms = array_merge($this->forms, $forms);
}

// Only store the flat_forms if they are an array
if (is_array($flat_forms)) {
$this->flat_forms = array_merge($this->flat_forms, $flat_forms);
}
}

/**
* Save the current state of the forms
*/
protected function saveCachedForms()
{
// Save the current state of the forms to cache
if ($this->recache_forms) {
$this->grav['cache']->save($this->getFormCacheId(), [$this->forms, $this->flat_forms]);
}
}

/**
* Get the current page cache based id for the forms cache
*
* @return string
*/
protected function getFormCacheId()
{
return $this->grav['pages']->getPagesCacheId() . '-form-plugin';
}

/**
* Create unix timestamp for storing the data into the filesystem.
*
* @param string $format
* @param int $utimestamp
*
* @return string
*/
protected function udate($format = 'u', $utimestamp = null)
{
if (null === $utimestamp) {
$utimestamp = microtime(true);
}

$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000000);

return date(preg_replace('`(?<!\\\\)u`', \sprintf('%06d', $milliseconds), $format), $timestamp);
}

}
2 changes: 2 additions & 0 deletions form.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
enabled: true
built_in_css: true
refresh_prevention: false
client_side_validation: true
inline_errors: false
files:
multiple: false # To allow multiple files, default is single
limit: 10 # Number of allowed files per field (multiple required)
Expand Down
4 changes: 4 additions & 0 deletions languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ en:
AVOID_OVERWRITING_HELP: "Prevent files with the same name to be overridden. Date prefix will be added"
RANDOM_NAME: "Random name"
RANDOM_NAME_HELP: "Generate a random 15 long string name for the uploaded files"
CLIENT_SIDE_VALIDATION: "Client-side validation"
CLIENT_SIDE_VALIDATION_HELP: "By default forms will use HTML5 client validation as first line of defense"
INLINE_ERRORS: "Inline errors"
INLINE_ERRORS_HELP: "Useful with 'client-side validation' off, shows inline contextual form errors"

de:
PLUGIN_FORM:
Expand Down
Loading

0 comments on commit 34a631c

Please sign in to comment.