Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
zanderwar committed Feb 23, 2018
0 parents commit c401567
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# For more information about the properties used in
# this file, please see the EditorConfig documentation:
# http://editorconfig.org/

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,css,scss}]
indent_size = 2
27 changes: 27 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
language: php

matrix:
include:
- php: 5.6
env: DB=MYSQL PHPCS_TEST=1 PHPUNIT_TEST=1
- php: 7.0
env: DB=PGSQL PHPUNIT_TEST=1
- php: 7.1
env: DB=MYSQL PHPUNIT_COVERAGE_TEST=1

before_script:
- phpenv rehash
- phpenv config-rm xdebug.ini

- composer validate
- composer require --prefer-dist --no-update silverstripe/recipe-core:^1.0
- if [[ $DB == PGSQL ]]; then composer require --prefer-dist --no-update silverstripe/postgresql:^2.0; fi
- composer update

script:
- if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit; fi
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml; fi
- if [[ $PHPCS_TEST ]]; then vendor/bin/phpcs --standard=vendor/silverstripe/framework/phpcs.xml.dist src/ tests/ ; fi

after_success:
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then bash <(curl -s https://codecov.io/bash) -f coverage.xml; fi
11 changes: 11 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Copyright (c) 2017, Vulcan Digital Ltd. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# silverstripe-formpreserve
Easily preserve and clear user entered form data contents via session in the event of errors

# Requirements
* silverstripe/framework: ^4

# Usage
```php
class YourForm extends Form
{
private static $extensions = [
FormPreserveExtension::class
];

public function __construct(RequestHandler $controller = null, $name = self::DEFAULT_NAME, FieldList $fields = null, FieldList $actions = null, $validator = null)
{
/** @var ArrayData $preserved */
$preserved = $this->retrievePreserved();

$fields = FieldList::create([
TextField::create('FirstName', 'First name:', $preserved->FirstName)->setAttribute('required', true),
EmailField::create('Email', 'Your email:', $preserved->Email)->setAttribute('placeholder', 'Where do we send our response?')->setAttribute('required', true),
]);

$actions = FieldList::create([
FormAction::create('doSubmit', 'Send')->setUseButtonTag(true)->setButtonContent('<i class="fal fa-paper-plane"></i> Send')->addExtraClass('btn btn-primary')
]);

parent::__construct($controller, $name, $fields, $actions, $validator);
}
}
```

**Your form submit action:**

```php
class ContactPageController extends \PageController
{
public function doSubmit($data, Form $form)
{
FormPreserve::preserve($form, $data);

if (!isset($data['Email']) || !filter_var($data['Email'], FILTER_VALIDATE_EMAIL)) {
return $this->redirectBack();
}

FormPreserve::clear($form);
return $this->redirectBack();
}
}
```
Empty file added _config.php
Empty file.
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "vulcandigital/silverstripe-formpreserve",
"description": "Easily preserve and clear user entered form data contents via session in the event of errors",
"type": "silverstripe-vendormodule",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Reece Alexander",
"email": "reece@vulcandigital.co.nz"
}
],
"require": {
"silverstripe/framework": "^4.0"
},
"require-dev": {
"phpunit/PHPUnit": "^5.7",
"squizlabs/php_codesniffer": "3.*"
},
"autoload": {
"psr-4": {
"Vulcan\\FormPreserve\\": "src/",
"Vulcan\\FormPreserve\\Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
}
}
14 changes: 14 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests</directory>
</testsuite>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
27 changes: 27 additions & 0 deletions src/Extensions/FormPreserveExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Vulcan\FormPreserve\Extensions;

use SilverStripe\Core\Extension;
use SilverStripe\Forms\Form;
use SilverStripe\View\ArrayData;
use Vulcan\FormPreserve\FormPreserve;

/**
* Class FormPreserveExtension
* @package Vulcan\FormPreserve\Extensions
*
* @method Form getOwner()
*/
class FormPreserveExtension extends Extension
{
public function retrievePreserved()
{
$data = FormPreserve::retrieve($this->getOwner());
if ($data) {
return ArrayData::create($data);
}

return ArrayData::create([]);
}
}
55 changes: 55 additions & 0 deletions src/FormPreserve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Vulcan\FormPreserve;

use SilverStripe\Control\Controller;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Forms\Form;
use SilverStripe\View\Parsers\URLSegmentFilter;

class FormPreserve
{
use Injectable, Configurable;

public static function preserve(Form $form, array $data)
{
$session = static::getSession();
$key = static::getFilteredFormClass($form);

$session->set("FormPreserve.{$key}", $data);
}

public static function retrieve(Form $form)
{
$session = static::getSession();
$key = static::getFilteredFormClass($form);

return $session->get("FormPreserve.{$key}");
}

public static function clear(Form $form = null)
{
$session = static::getSession();

if (!$form) {
$session->clear("FormPreserve");
unset($_SESSION["FormPreserve"]);
return;
}

$key = static::getFilteredFormClass($form);
$session->clear("FormPreserve.{$key}");
unset($_SESSION["FormPreserve"][$key]);
}

private static function getSession()
{
return Controller::curr()->getRequest()->getSession();
}

private static function getFilteredFormClass(Form $form)
{
return URLSegmentFilter::create()->filter(get_class($form));
}
}
41 changes: 41 additions & 0 deletions tests/FormPreserveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Vulcan\FormPreserve\Tests;

use SilverStripe\Control\RequestHandler;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\Validator;
use SilverStripe\View\ArrayData;
use Vulcan\FormPreserve\Extensions\FormPreserveExtension;
use Vulcan\FormPreserve\FormPreserve;

class FormPreserveTest extends FunctionalTest
{
public function testPreserve()
{
$this->assertTrue(true);
}
}

class TestForm extends Form implements TestOnly
{
private static $extensions = [
FormPreserveExtension::class
];

public function __construct(RequestHandler $controller = null, $name = self::DEFAULT_NAME, FieldList $fields = null, FieldList $actions = null, $validator = null)
{
/** @var ArrayData $preserve */
$preserve = $this->retrievePreserved();

$fields = FieldList::create([
TextField::create('TestField', 'Test', $preserve->TestField)
]);

parent::__construct($controller, $name, $fields, $actions, $validator);
}
}

0 comments on commit c401567

Please sign in to comment.