Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4516fbe
Showing
30 changed files
with
838 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Grav | ||
|
||
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
grav-plugin-form | ||
================ | ||
|
||
Grav Form Plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: Form Handling Plugin | ||
version: 1.0.0 | ||
description: Enables forms. | ||
validation: strict | ||
|
||
form: | ||
fields: | ||
enabled: | ||
type: toggle | ||
label: Plugin status | ||
highlight: 1 | ||
default: 0 | ||
options: | ||
1: Enabled | ||
0: Disabled | ||
validate: | ||
type: bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
namespace Grav\Plugin; | ||
|
||
use \Grav\Common\Iterator; | ||
use \Grav\Common\Grav; | ||
use \Grav\Common\Registry; | ||
use \Grav\Common\Page\Page; | ||
|
||
class Form extends Iterator | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $values = array(); | ||
|
||
/** | ||
* @var Page $page | ||
*/ | ||
protected $page; | ||
|
||
/** | ||
* Create form for the given page. | ||
* | ||
* @param Page $page | ||
*/ | ||
public function __construct(Page $page) | ||
{ | ||
$this->page = $page; | ||
|
||
$header = $page->header(); | ||
$this->rules = isset($header->rules) ? $header->rules : array(); | ||
$this->data = isset($header->data) ? $header->data : array(); | ||
$this->items = $header->form; | ||
|
||
// Set form name if not set. | ||
if (empty($this->items['name'])) { | ||
$this->items['name'] = $page->slug(); | ||
} | ||
} | ||
|
||
/** | ||
* Return page object for the form. | ||
* | ||
* @return Page | ||
*/ | ||
public function page() | ||
{ | ||
return $this->page; | ||
} | ||
|
||
/** | ||
* Get value of given variable (or all values). | ||
* | ||
* @param string $name | ||
* @return mixed | ||
*/ | ||
public function value($name = null) | ||
{ | ||
if (!$name) { | ||
return $this->values; | ||
} | ||
return $this->getField($name, 'values'); | ||
} | ||
|
||
/** | ||
* Reset values. | ||
*/ | ||
public function reset() | ||
{ | ||
$this->values = array(); | ||
} | ||
|
||
/** | ||
* Handle form processing on POST action. | ||
*/ | ||
public function post() | ||
{ | ||
if (isset($_POST)) { | ||
$this->values = (array) $_POST; | ||
} | ||
|
||
$process = isset($this->items['process']) ? $this->items['process'] : array(); | ||
if (is_array($process)) { | ||
/** @var Grav $grav */ | ||
$grav = Registry::instance()->get('Grav'); | ||
foreach ($process as $action => $data) { | ||
if (is_numeric($action)) { | ||
$action = \key($data); | ||
$data = $data[$action]; | ||
} | ||
$grav->fireEvent('onProcessForm', $this, $action, $data); | ||
} | ||
} else { | ||
// Default action. | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @param string $name | ||
* @param string $scope | ||
* @return mixed|null | ||
* @internal | ||
*/ | ||
protected function getField($name, $scope = 'value') | ||
{ | ||
$path = explode('.', $name); | ||
|
||
$current = $this->{$scope}; | ||
foreach ($path as $field) { | ||
if (is_object($current) && isset($current->{$field})) { | ||
$current = $current->{$field}; | ||
} elseif (is_array($current) && isset($current[$field])) { | ||
$current = $current[$field]; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
return $current; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
namespace Grav\Plugin; | ||
|
||
use \Grav\Common\Plugin; | ||
use \Grav\Common\Registry; | ||
use \Grav\Common\Page\Page; | ||
use \Grav\Common\Page\Pages; | ||
use \Grav\Common\Grav; | ||
use \Grav\Common\Uri; | ||
use \Grav\Common\Filesystem\File; | ||
use \Grav\Common\Twig; | ||
use \Grav\Plugin\Form; | ||
|
||
class FormPlugin extends Plugin | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
protected $active = false; | ||
|
||
/** | ||
* @var Form | ||
*/ | ||
protected $form; | ||
|
||
/** | ||
* @var Grav | ||
*/ | ||
protected $grav; | ||
|
||
/** | ||
* Initialize form if the page has one. Also catches form processing if user posts the form. | ||
*/ | ||
public function onAfterGetPage() | ||
{ | ||
$this->grav = Registry::get('Grav'); | ||
|
||
/** @var Page $page */ | ||
$page = $this->grav->page; | ||
if (!$page) { | ||
return; | ||
} | ||
|
||
$header = $page->header(); | ||
if (isset($header->form) && is_array($header->form)) { | ||
$this->active = true; | ||
|
||
// Create form. | ||
require_once __DIR__ . '/classes/form.php'; | ||
$this->form = new Form($page); | ||
|
||
// Handle posting if needed. | ||
if (!empty($_POST)) { | ||
$this->form->post(); | ||
} | ||
|
||
$registry = Registry::instance(); | ||
$registry->store('Form', $this->form); | ||
} | ||
} | ||
|
||
/** | ||
* Add current directory to twig lookup paths. | ||
*/ | ||
public function onAfterTwigTemplatesPaths() | ||
{ | ||
Registry::get('Twig')->twig_paths[] = __DIR__ . '/templates'; | ||
} | ||
|
||
/** | ||
* Make form accessible from twig. | ||
*/ | ||
public function onAfterSiteTwigVars() | ||
{ | ||
if (!$this->active) { | ||
return; | ||
} | ||
|
||
/** @var Twig $twig */ | ||
$twig = Registry::get('Twig'); | ||
/** @var Form $form */ | ||
$form = Registry::get('Form'); | ||
|
||
$twig->twig_vars['form'] = $form; | ||
} | ||
|
||
/** | ||
* Handle form processing instructions. | ||
* | ||
* @param Form $form | ||
* @param string $task | ||
* @param string $params | ||
*/ | ||
public function onProcessForm(Form $form, $task, $params) | ||
{ | ||
if (!$this->active) { | ||
return; | ||
} | ||
|
||
switch ($task) { | ||
case 'message': | ||
$this->form->message = (string) $params; | ||
break; | ||
case 'redirect': | ||
$this->grav->redirect((string) $params); | ||
break; | ||
case 'reset': | ||
if (in_array($params, array(true, 1, '1', 'yes', 'on', 'true'), true)) { | ||
$this->form->reset(); | ||
} | ||
break; | ||
case 'display': | ||
$route = (string) $params; | ||
if (!$route || $route[0] != '/') { | ||
/** @var Uri $uri */ | ||
$uri = Registry::get('Uri'); | ||
$route = $uri->route() . ($route ? '/' . $route : ''); | ||
} | ||
/** @var Pages $pages */ | ||
$pages = Registry::get('Pages'); | ||
$this->grav->page = $pages->dispatch($route, true); | ||
break; | ||
case 'save': | ||
$prefix = !empty($params['fileprefix']) ? $params['fileprefix'] : ''; | ||
$format = !empty($params['dateformat']) ? $params['dateformat'] : 'Ymd-His-u'; | ||
$ext = !empty($params['extension']) ? '.' . trim($params['extension'], '.') : '.txt'; | ||
$filename = $prefix . $this->udate($format) . $ext; | ||
|
||
/** @var Twig $twig */ | ||
$twig = Registry::get('Twig'); | ||
$vars = array( | ||
'form' => $this->form | ||
); | ||
|
||
$file = File\General::instance(DATA_DIR . $this->form->name . '/' . $filename); | ||
$body = $twig->processString( | ||
!empty($params['body']) ? $params['body'] : '{% include "forms/data.txt.twig" %}', | ||
$vars | ||
); | ||
$file->save($body); | ||
} | ||
} | ||
|
||
/** | ||
* 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 (is_null($utimestamp)) { | ||
$utimestamp = microtime(true); | ||
} | ||
|
||
$timestamp = floor($utimestamp); | ||
$milliseconds = round(($utimestamp - $timestamp) * 1000000); | ||
|
||
return date(preg_replace('`(?<!\\\\)u`', \sprintf('%06d', $milliseconds), $format), $timestamp); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
enabled: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{% set value = (value is null ? field.default : value) %} | ||
|
||
<div class="form-field"> | ||
<div class="dynfields" data-grav-dynfields="{{ field.name|fieldName }}"> | ||
<span class="inline"> | ||
<span> | ||
{% if field.help %} | ||
<span class="tooltip" data-asTooltip-position="w" title="{{ field.help|e }}">{{ field.label }}</span> | ||
{% else %} | ||
{{ field.label }} | ||
{% endif %} | ||
</span> | ||
|
||
|
||
{% if value|length %} | ||
{% for key, text in value %} | ||
<div> | ||
<input type="text" value="{{ (scope ~ key)|fieldName }}" placeholder="" /> | ||
<input type="text" name="{{ (scope ~ field.name)|fieldName }}[{{ key }}]" value="{{ text|join(', ') }}" placeholder="{{ field.placeholder }}" /> | ||
<span data-grav-remfield class="button fa fa-minus"></span> | ||
<span data-grav-addfield class="button fa fa-plus"></span> | ||
</div> | ||
{% endfor %} | ||
{% else %} | ||
<div> | ||
<input type="text" placeholder="" /> | ||
<input type="text" placeholder="{{ field.placeholder }}" /> | ||
<span data-grav-remfield class="button fa fa-minus"></span> | ||
<span data-grav-addfield class="button fa fa-plus"></span> | ||
</div> | ||
{% endif %} | ||
</span> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
field: | ||
array: | ||
node: array | ||
key: name | ||
fields: | ||
name: | ||
type: text | ||
validation: | ||
required: true | ||
value: | ||
type: text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{% set value = (value is null ? field.default : value) %} | ||
|
||
<div class="form-field"> | ||
<label class="inline"> | ||
<input type="checkbox" | ||
name="{{ (scope ~ field.name)|fieldName }}" | ||
{% if value == true %} checked="checked" {% endif %} | ||
{% if field.autofocus in ['on', 'true', 1] %}autofocus="autofocus"{% endif %} | ||
{% if field.novalidate in ['on', 'true', 1] %}novalidate="novalidate"{% endif %} | ||
{% if field.validate.required in ['on', 'true', 1] %}required="required"{% endif %} | ||
/> | ||
{{ field.label|e }} {{ field.validate.required in ['on', 'true', 1] ? '<span class="required">*</span>' }} | ||
</label> | ||
</div> |
Oops, something went wrong.