Skip to content

Commit

Permalink
Merge branch 'release/2.13.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Apr 13, 2018
2 parents 0299ff3 + 5a35ddc commit c4e85e8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v2.13.3
## 04/13/2018

1. [](#new)
* Added support to save form data in raw format (yaml or json)
* Added new `timestamp` action to add a timestamp field

# v2.13.2
## 04/12/2018

Expand Down
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Form
version: 2.13.2
version: 2.13.3
description: Enables the forms handling
icon: check-square
author:
Expand Down
41 changes: 41 additions & 0 deletions form.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Grav\Common\Utils;
use Grav\Common\Uri;
use Grav\Plugin\Form\Form;
use RocketTheme\Toolbox\File\JsonFile;
use RocketTheme\Toolbox\File\YamlFile;
use Symfony\Component\Yaml\Yaml;
use RocketTheme\Toolbox\File\File;
use RocketTheme\Toolbox\Event\Event;
Expand Down Expand Up @@ -268,6 +270,7 @@ public function onTwigVariables(Event $event = null)
*/
public function onFormProcessed(Event $event)
{
/** @var Form $form */
$form = $event['form'];
$action = $event['action'];
$params = $event['params'];
Expand Down Expand Up @@ -314,6 +317,16 @@ public function onFormProcessed(Event $event)
return;
}
break;
case 'timestamp':
$label = isset($params['label']) ? $params['label'] : 'Timestamp';
$format = isset($params['format']) ? $params['format'] : 'Y-m-d H:i:s';
$blueprint = $form->value()->blueprints();
$blueprint->set('form/fields/timestamp', ['name'=>'timestamp', 'label'=> $label]);
$now = new \DateTime('now');
$date_string = $now->format($format);
$form->setFields($blueprint->fields());
$form->setData('timestamp',$date_string);
break;
case 'ip':
$label = isset($params['label']) ? $params['label'] : 'User IP';
$blueprint = $form->value()->blueprints();
Expand Down Expand Up @@ -405,6 +418,34 @@ public function onFormProcessed(Event $event)
$dir = $path . DS . $form->name();
$fullFileName = $dir. DS . $filename;

if (!empty($params['raw']) || !empty($params['template'])) {
// Save data as it comes from the form.
if ($operation === 'add') {
throw new \RuntimeException('Form save: \'operation: add\' is not supported for raw files');
}
switch ($ext) {
case '.yaml':
$file = YamlFile::instance($fullFileName);
break;
case '.json':
$file = JsonFile::instance($fullFileName);
break;
default:
throw new \RuntimeException('Form save: Unsupported RAW file format, please use either yaml or json');
}

$data = [
'_data_type' => 'form',
'template' => !empty($params['template']) ? $params['template'] : null,
'name' => $form->name(),
'timestamp' => date('Y-m-d H:i:s'),
'content' => $form->getData()->toArray()
];

$file->save(array_filter($data));
break;
}

$file = File::instance($fullFileName);

if ($operation === 'create') {
Expand Down

0 comments on commit c4e85e8

Please sign in to comment.