silverstripe-userforms/code/Form/UserForm.php

231 lines
6.5 KiB
PHP
Raw Permalink Normal View History

2015-07-24 04:37:48 +02:00
<?php
namespace SilverStripe\UserForms\Form;
use ResetFormAction;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Session;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\UserForms\FormField\UserFormsStepField;
use SilverStripe\UserForms\FormField\UserFormsFieldList;
2015-07-24 04:37:48 +02:00
/**
* @package userforms
*/
2016-07-21 07:53:59 +02:00
class UserForm extends Form
{
/**
* @config
* @var string
*/
private static $button_text = '';
2017-08-11 02:37:03 +02:00
/**
* @param Controller $controller
* @param string $name
*/
public function __construct(Controller $controller, $name = Form::class)
2016-07-21 07:53:59 +02:00
{
2017-08-11 02:37:03 +02:00
$this->controller = $controller;
$this->setRedirectToFormOnValidationError(true);
parent::__construct(
$controller,
$name,
new FieldList(),
new FieldList()
);
$this->setFields($fields = $this->getFormFields());
2017-08-11 02:37:03 +02:00
$fields->setForm($this);
$this->setActions($actions = $this->getFormActions());
$actions->setForm($this);
$this->setValidator($this->getRequiredFields());
2016-07-21 07:53:59 +02:00
// This needs to be re-evaluated since fields have been assigned
$this->restoreFormState();
2016-07-21 07:53:59 +02:00
2017-08-11 02:37:03 +02:00
// Number each page
$stepNumber = 1;
foreach ($this->getSteps() as $step) {
$step->setStepNumber($stepNumber++);
}
2016-07-21 07:53:59 +02:00
2017-08-11 02:37:03 +02:00
if ($controller->DisableCsrfSecurityToken) {
$this->disableSecurityToken();
}
2016-07-21 07:53:59 +02:00
$data = $this->getRequest()->getSession()->get("FormInfo.{$this->FormName()}.data");
2016-07-21 07:53:59 +02:00
2017-08-11 02:37:03 +02:00
if (is_array($data)) {
$this->loadDataFrom($data);
}
2016-07-21 07:53:59 +02:00
2017-08-11 02:37:03 +02:00
$this->extend('updateForm');
}
2016-07-21 07:53:59 +02:00
public function restoreFormState()
2016-07-21 07:53:59 +02:00
{
// Suppress restoreFormState if fields haven't been bootstrapped
2016-07-21 07:53:59 +02:00
if ($this->fields && $this->fields->exists()) {
return parent::restoreFormState();
2016-07-21 07:53:59 +02:00
}
return $this;
}
2017-08-11 02:37:03 +02:00
/**
* Used for partial caching in the template.
*
* @return string
*/
2016-07-21 07:53:59 +02:00
public function getLastEdited()
{
2017-08-11 02:37:03 +02:00
return $this->controller->LastEdited;
}
2016-07-21 07:53:59 +02:00
2017-08-11 02:37:03 +02:00
/**
* @return bool
*/
2016-07-21 07:53:59 +02:00
public function getDisplayErrorMessagesAtTop()
{
2017-08-11 02:37:03 +02:00
return (bool)$this->controller->DisplayErrorMessagesAtTop;
}
/**
* Return the fieldlist, filtered to only contain steps
*
* @return \SilverStripe\ORM\ArrayList
2017-08-11 02:37:03 +02:00
*/
2016-07-21 07:53:59 +02:00
public function getSteps()
{
2017-08-11 02:37:03 +02:00
return $this->Fields()->filterByCallback(function ($field) {
return $field instanceof UserFormsStepField;
});
}
/**
* Get the form fields for the form on this page. Can modify this FieldSet
* by using {@link updateFormFields()} on an {@link Extension} subclass which
* is applied to this controller.
*
* This will be a list of top level composite steps
*
* @return FieldList
*/
2016-07-21 07:53:59 +02:00
public function getFormFields()
{
$fields = UserFormsFieldList::create();
2017-08-11 02:37:03 +02:00
$target = $fields;
foreach ($this->controller->data()->Fields() as $field) {
2017-08-11 02:37:03 +02:00
$target = $target->processNext($field);
}
$fields->clearEmptySteps();
$this->extend('updateFormFields', $fields);
2016-07-21 07:53:59 +02:00
$fields->setForm($this);
2017-08-11 02:37:03 +02:00
return $fields;
}
/**
* Generate the form actions for the UserDefinedForm. You
* can manipulate these by using {@link updateFormActions()} on
* a decorator.
*
* @return FieldList
*/
2016-07-21 07:53:59 +02:00
public function getFormActions()
{
$submitText = ($this->controller->SubmitButtonText)
? $this->controller->SubmitButtonText
: _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SUBMITBUTTON', 'Submit');
$clearText = ($this->controller->ClearButtonText)
? $this->controller->ClearButtonText
: _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.CLEARBUTTON', 'Clear');
2016-07-21 07:53:59 +02:00
$actions = FieldList::create(FormAction::create('process', $submitText));
2016-07-21 07:53:59 +02:00
2017-08-11 02:37:03 +02:00
if ($this->controller->ShowClearButton) {
$actions->push(FormAction::create('clearForm', $clearText)->setAttribute('type', 'reset'));
2017-08-11 02:37:03 +02:00
}
2016-07-21 07:53:59 +02:00
2017-08-11 02:37:03 +02:00
$this->extend('updateFormActions', $actions);
2016-07-21 07:53:59 +02:00
$actions->setForm($this);
2017-08-11 02:37:03 +02:00
return $actions;
}
/**
* Get the required form fields for this form.
*
* @return RequiredFields
2017-08-11 02:37:03 +02:00
*/
2016-07-21 07:53:59 +02:00
public function getRequiredFields()
{
2017-08-11 02:37:03 +02:00
// Generate required field validator
$requiredNames = $this
2016-07-21 07:53:59 +02:00
->getController()
->data()
2017-08-11 02:37:03 +02:00
->Fields()
->filter('Required', true)
->column('Name');
$requiredNames = array_merge($requiredNames, $this->getEmailRecipientRequiredFields());
$required = UserFormsRequiredFields::create($requiredNames);
2017-08-11 02:37:03 +02:00
$this->extend('updateRequiredFields', $required);
2016-07-21 07:53:59 +02:00
$required->setForm($this);
2017-08-11 02:37:03 +02:00
return $required;
}
/**
* Override some we can add UserForm specific attributes to the form.
*
* @return array
*/
2016-07-21 07:53:59 +02:00
public function getAttributes()
{
2017-08-11 02:37:03 +02:00
$attrs = parent::getAttributes();
2016-07-21 07:53:59 +02:00
2017-08-11 02:37:03 +02:00
$attrs['class'] = $attrs['class'] . ' userform';
$attrs['data-livevalidation'] = (bool)$this->controller->EnableLiveValidation;
$attrs['data-toperrors'] = (bool)$this->controller->DisplayErrorMessagesAtTop;
2016-07-21 07:53:59 +02:00
2017-08-11 02:37:03 +02:00
return $attrs;
}
/**
* @return string
*/
2017-08-11 02:37:03 +02:00
public function getButtonText()
{
return $this->config()->get('button_text');
2016-07-21 07:53:59 +02:00
}
/**
* Push fields into the RequiredFields array if they are used by any Email recipients.
* Ignore if there is a backup i.e. the plain string field is set
*
* @return array required fields names
*/
protected function getEmailRecipientRequiredFields()
{
$requiredFields = [];
$recipientFieldsMap = [
'EmailAddress' => 'SendEmailToField',
'EmailSubject' => 'SendEmailSubjectField',
'EmailReplyTo' => 'SendEmailFromField'
];
foreach ($this->getController()->data()->EmailRecipients() as $recipient) {
foreach ($recipientFieldsMap as $textField => $dynamicFormField) {
2018-02-13 18:21:03 +01:00
if (empty($recipient->$textField) && $recipient->getComponent($dynamicFormField)->exists()) {
$requiredFields[] = $recipient->getComponent($dynamicFormField)->Name;
}
}
}
return $requiredFields;
}
2015-07-24 04:37:48 +02:00
}