2015-07-24 04:37:48 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Form;
|
|
|
|
|
|
|
|
use ResetFormAction;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\Session;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\FormAction;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\UserForms\FormField\UserFormsStepField;
|
|
|
|
use SilverStripe\UserForms\FormField\UserFormsFieldList;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2015-07-24 04:37:48 +02:00
|
|
|
/**
|
|
|
|
* @package userforms
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class UserForm extends Form
|
|
|
|
{
|
2017-09-20 06:31:40 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $button_text = '';
|
|
|
|
|
2017-08-11 02:37:03 +02:00
|
|
|
/**
|
|
|
|
* @param Controller $controller
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2017-08-09 01:55:09 +02:00
|
|
|
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-10-15 23:13:01 +02:00
|
|
|
|
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
|
2017-08-13 23:26:53 +02:00
|
|
|
$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
|
|
|
|
2017-08-13 23:26:53 +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
|
|
|
|
2017-08-13 23:26:53 +02:00
|
|
|
public function restoreFormState()
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
2017-08-13 23:26:53 +02:00
|
|
|
// Suppress restoreFormState if fields haven't been bootstrapped
|
2016-07-21 07:53:59 +02:00
|
|
|
if ($this->fields && $this->fields->exists()) {
|
2017-08-13 23:26:53 +02:00
|
|
|
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
|
|
|
|
*
|
2017-09-04 23:45:16 +02:00
|
|
|
* @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()
|
|
|
|
{
|
2017-08-11 02:37:03 +02:00
|
|
|
$fields = new UserFormsFieldList();
|
|
|
|
$target = $fields;
|
2017-10-15 23:13:01 +02:00
|
|
|
|
2017-10-31 23:43:53 +01:00
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @todo Make form actions editable via their own field editor.
|
|
|
|
*
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
public function getFormActions()
|
|
|
|
{
|
2017-08-13 23:26:53 +02:00
|
|
|
$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
|
|
|
|
2017-08-13 23:26:53 +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) {
|
2017-08-13 23:26:53 +02:00
|
|
|
$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
|
|
|
|
*/
|
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()
|
2017-10-31 23:43:53 +01:00
|
|
|
->data()
|
2017-08-11 02:37:03 +02:00
|
|
|
->Fields()
|
|
|
|
->filter('Required', true)
|
|
|
|
->column('Name');
|
2018-02-13 17:24:53 +01:00
|
|
|
$requiredNames = array_merge($requiredNames, $this->getEmailRecipientRequiredFields());
|
2017-08-11 02:37:03 +02:00
|
|
|
$required = new RequiredFields($requiredNames);
|
|
|
|
$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;
|
|
|
|
}
|
2016-07-14 16:29:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-08-11 02:37:03 +02:00
|
|
|
public function getButtonText()
|
|
|
|
{
|
2016-07-14 16:29:25 +02:00
|
|
|
return $this->config()->get('button_text');
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2018-02-13 17:24:53 +01: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()) {
|
2018-02-13 17:24:53 +01:00
|
|
|
$requiredFields[] = $recipient->getComponent($dynamicFormField)->Name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $requiredFields;
|
|
|
|
}
|
2015-07-24 04:37:48 +02:00
|
|
|
}
|