Run PSR-2 linting

This commit is contained in:
Robbie Averill 2017-08-11 12:37:03 +12:00
parent cd314d3a3c
commit 32586b6363
18 changed files with 247 additions and 226 deletions

View File

@ -17,49 +17,49 @@ use SilverStripe\UserForms\FormField\UserFormsFieldList;
*/
class UserForm extends Form
{
/**
* @param Controller $controller
* @param string $name
*/
/**
* @param Controller $controller
* @param string $name
*/
public function __construct(Controller $controller, $name = Form::class)
{
$this->controller = $controller;
$this->setRedirectToFormOnValidationError(true);
$this->controller = $controller;
$this->setRedirectToFormOnValidationError(true);
parent::__construct(
$controller,
$name,
new FieldList(),
new FieldList()
);
parent::__construct(
$controller,
$name,
new FieldList(),
new FieldList()
);
$this->setFields($fields = $this->getFormFields());
$fields->setForm($this);
$this->setActions($actions = $this->getFormActions());
$actions->setForm($this);
$this->setValidator($this->getRequiredFields());
$this->setFields($fields = $this->getFormFields());
$fields->setForm($this);
$this->setActions($actions = $this->getFormActions());
$actions->setForm($this);
$this->setValidator($this->getRequiredFields());
// This needs to be re-evaluated since fields have been assigned
$this->setupFormErrors();
// Number each page
$stepNumber = 1;
foreach($this->getSteps() as $step) {
$step->setStepNumber($stepNumber++);
}
// Number each page
$stepNumber = 1;
foreach ($this->getSteps() as $step) {
$step->setStepNumber($stepNumber++);
}
if($controller->DisableCsrfSecurityToken) {
$this->disableSecurityToken();
}
if ($controller->DisableCsrfSecurityToken) {
$this->disableSecurityToken();
}
$data = Session::get("FormInfo.{$this->FormName()}.data");
$data = Session::get("FormInfo.{$this->FormName()}.data");
if(is_array($data)) {
$this->loadDataFrom($data);
}
if (is_array($data)) {
$this->loadDataFrom($data);
}
$this->extend('updateForm');
}
$this->extend('updateForm');
}
public function setupFormErrors()
{
@ -71,125 +71,125 @@ class UserForm extends Form
return $this;
}
/**
* Used for partial caching in the template.
*
* @return string
*/
/**
* Used for partial caching in the template.
*
* @return string
*/
public function getLastEdited()
{
return $this->controller->LastEdited;
}
return $this->controller->LastEdited;
}
/**
* @return bool
*/
/**
* @return bool
*/
public function getDisplayErrorMessagesAtTop()
{
return (bool)$this->controller->DisplayErrorMessagesAtTop;
}
return (bool)$this->controller->DisplayErrorMessagesAtTop;
}
/**
* Return the fieldlist, filtered to only contain steps
*
* @return ArrayList
*/
/**
* Return the fieldlist, filtered to only contain steps
*
* @return ArrayList
*/
public function getSteps()
{
return $this->Fields()->filterByCallback(function($field) {
return $field instanceof UserFormsStepField;
});
}
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
*/
/**
* 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
*/
public function getFormFields()
{
$fields = new UserFormsFieldList();
$target = $fields;
foreach ($this->controller->Fields() as $field) {
$target = $target->processNext($field);
}
$fields->clearEmptySteps();
$this->extend('updateFormFields', $fields);
$fields = new UserFormsFieldList();
$target = $fields;
foreach ($this->controller->Fields() as $field) {
$target = $target->processNext($field);
}
$fields->clearEmptySteps();
$this->extend('updateFormFields', $fields);
$fields->setForm($this);
return $fields;
}
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
*/
/**
* 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
*/
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');
$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');
$actions = new FieldList(
new FormAction("process", $submitText)
);
$actions = new FieldList(
new FormAction("process", $submitText)
);
if($this->controller->ShowClearButton) {
$actions->push(new ResetFormAction("clearForm", $clearText));
}
if ($this->controller->ShowClearButton) {
$actions->push(new ResetFormAction("clearForm", $clearText));
}
$this->extend('updateFormActions', $actions);
$this->extend('updateFormActions', $actions);
$actions->setForm($this);
return $actions;
}
return $actions;
}
/**
* Get the required form fields for this form.
*
* @return RequiredFields
*/
/**
* Get the required form fields for this form.
*
* @return RequiredFields
*/
public function getRequiredFields()
{
// Generate required field validator
$requiredNames = $this
// Generate required field validator
$requiredNames = $this
->getController()
->Fields()
->filter('Required', true)
->column('Name');
$required = new RequiredFields($requiredNames);
$this->extend('updateRequiredFields', $required);
->Fields()
->filter('Required', true)
->column('Name');
$required = new RequiredFields($requiredNames);
$this->extend('updateRequiredFields', $required);
$required->setForm($this);
return $required;
}
return $required;
}
/**
* Override some we can add UserForm specific attributes to the form.
*
* @return array
*/
/**
* Override some we can add UserForm specific attributes to the form.
*
* @return array
*/
public function getAttributes()
{
$attrs = parent::getAttributes();
$attrs = parent::getAttributes();
$attrs['class'] = $attrs['class'] . ' userform';
$attrs['data-livevalidation'] = (bool)$this->controller->EnableLiveValidation;
$attrs['data-toperrors'] = (bool)$this->controller->DisplayErrorMessagesAtTop;
$attrs['class'] = $attrs['class'] . ' userform';
$attrs['data-livevalidation'] = (bool)$this->controller->EnableLiveValidation;
$attrs['data-toperrors'] = (bool)$this->controller->DisplayErrorMessagesAtTop;
return $attrs;
}
return $attrs;
}
/**
* @return string
*/
public function getButtonText() {
public function getButtonText()
{
return $this->config()->get('button_text');
}
}

View File

@ -77,8 +77,8 @@ class UserFormsGridFieldFilterHeader extends GridFieldFilterHeader
$valueField->addExtraClass('no-change-track');
$valueField->setAttribute(
'placeholder',
_t(__CLASS__.'.WHEREVALUEIS', 'where value is..'
));
_t(__CLASS__.'.WHEREVALUEIS', 'where value is..')
);
$fields->push(FieldGroup::create(CompositeField::create(
$columnField,
@ -110,8 +110,7 @@ class UserFormsGridFieldFilterHeader extends GridFieldFilterHeader
->addExtraClass('ss-gridfield-button-close')
->setAttribute('title', _t('SilverStripe\\Forms\\GridField\\GridField.ResetFilter', "Reset"))
->setAttribute('id', 'action_reset_' . $gridField->getModelClass() . '_' . $columnField)
)
);
));
$actions->addExtraClass('filter-buttons');
$actions->addExtraClass('no-change-track');
@ -132,14 +131,14 @@ class UserFormsGridFieldFilterHeader extends GridFieldFilterHeader
if ($filter = $state->UserFormsGridField->toArray()) {
if (isset($filter['filter']) && $filter['filter'] && isset($filter['value']) && $filter['value']) {
$dataList = $dataList->where(sprintf("
$dataList = $dataList->where(sprintf(
"
SELECT COUNT(*) FROM SubmittedFormField
WHERE (
ParentID = SubmittedForm.ID AND
Name = '%s' AND
Value LIKE '%s'
) > 0",
Convert::raw2sql($filter['filter']),
Convert::raw2sql($filter['value'])
));

View File

@ -264,7 +264,8 @@ class EditableFormField extends DataObject
}
}
$fields->addFieldToTab('Root.Main',
$fields->addFieldToTab(
'Root.Main',
DropdownField::create(
'ExtraClass',
_t(__CLASS__.'.EXTRACLASS_TITLE', 'Extra Styling/Layout'),
@ -275,7 +276,8 @@ class EditableFormField extends DataObject
))
);
} else {
$fields->addFieldToTab('Root.Main',
$fields->addFieldToTab(
'Root.Main',
TextField::create(
'ExtraClass',
_t(__CLASS__.'.EXTRACLASS_Title', 'Extra CSS classes')
@ -327,9 +329,12 @@ class EditableFormField extends DataObject
return new FieldList(
LabelField::create(
_t(
__CLASS__.'.DISPLAY_RULES_DISABLED',
'Display rules are not enabled for required fields. Please uncheck "Is this field Required?" under "Validation" to re-enable.'))
->addExtraClass('message warning'));
__CLASS__.'.DISPLAY_RULES_DISABLED',
'Display rules are not enabled for required fields. Please uncheck "Is this field Required?" under "Validation" to re-enable.'
)
)
->addExtraClass('message warning')
);
}
$allowedClasses = array_keys($this->getEditableFieldClasses(false));
@ -364,14 +369,16 @@ class EditableFormField extends DataObject
);
return new FieldList(
DropdownField::create('ShowOnLoad',
DropdownField::create(
'ShowOnLoad',
_t(__CLASS__.'.INITIALVISIBILITY', 'Initial visibility'),
[
1 => 'Show',
0 => 'Hide',
]
),
DropdownField::create('DisplayRulesConjunction',
DropdownField::create(
'DisplayRulesConjunction',
_t(__CLASS__.'.DISPLAYIF', 'Toggle visibility when'),
[
'Or' => _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDIFOR', 'Any conditions are true'),

View File

@ -73,7 +73,8 @@ class EditableCheckbox extends EditableFormField
parent::migrateSettings($data);
}
public function isCheckBoxField() {
return true;
}
public function isCheckBoxField()
{
return true;
}
}

View File

@ -70,11 +70,12 @@ class EditableCheckboxGroupField extends EditableMultipleOptionField
}
}
public function isCheckBoxField() {
return true;
}
public function isCheckBoxField()
{
return true;
}
public function getSelectorFieldOnly()
public function getSelectorFieldOnly()
{
return "[name='{$this->Name}[]']";
}

View File

@ -59,17 +59,14 @@ class EditableFileField extends EditableFormField
)
);
$fields->addFieldToTab(
"Root.Main",
LiteralField::create(
'FileUploadWarning',
'<p class="message notice">' . _t(
'SilverStripe\\UserForms\\Model\\UserDefinedForm.FileUploadWarning',
'Files uploaded through this field could be publicly accessible if the exact URL is known'
) . '</p>'
),
'Type'
);
$fields->addFieldToTab("Root.Main", LiteralField::create(
'FileUploadWarning',
'<p class="message notice">' . _t(
'SilverStripe\\UserForms\\Model\\UserDefinedForm.FileUploadWarning',
'Files uploaded through this field could be publicly accessible if the exact URL is known'
)
. '</p>'
), 'Type');
$fields->addFieldToTab(
'Root.Main',
@ -109,7 +106,7 @@ class EditableFileField extends EditableFormField
$field->getValidator()->setAllowedExtensions(
array_diff(
// filter out '' since this would be a regex problem on JS end
// filter out '' since this would be a regex problem on JS end
array_filter(Config::inst()->get(File::class, 'allowed_extensions')),
$this->config()->get('allowed_extensions_blacklist')
)
@ -176,5 +173,4 @@ class EditableFileField extends EditableFormField
{
return round(static::get_php_max_file_size() / 1024.0, 1);
}
}

View File

@ -53,7 +53,7 @@ class EditableMultipleOptionField extends EditableFormField
*/
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function($fields) {
$this->beforeUpdateCMSFields(function ($fields) {
$editableColumns = new GridFieldEditableColumns();
$editableColumns->setDisplayFields([
'Title' => [

View File

@ -18,34 +18,34 @@ use SilverStripe\Versioned\Versioned;
*/
class EditableOption extends DataObject
{
private static $default_sort = 'Sort';
private static $default_sort = 'Sort';
private static $db = [
'Name' => 'Varchar(255)',
'Title' => 'Varchar(255)',
'Default' => 'Boolean',
private static $db = [
'Name' => 'Varchar(255)',
'Title' => 'Varchar(255)',
'Default' => 'Boolean',
'Sort' => 'Int',
'Value' => 'Varchar(255)',
];
];
private static $has_one = [
'Parent' => EditableMultipleOptionField::class,
];
private static $has_one = [
'Parent' => EditableMultipleOptionField::class,
];
private static $extensions = [
Versioned::class . "('Stage', 'Live')"
];
private static $extensions = [
Versioned::class . "('Stage', 'Live')"
];
private static $summary_fields = [
'Title',
'Default'
];
private static $summary_fields = [
'Title',
'Default'
];
protected static $allow_empty_values = false;
private static $table_name = 'EditableOption';
/**
/**
* Returns whether to allow empty values or not.
*
* @return boolean
@ -66,61 +66,61 @@ class EditableOption extends DataObject
}
/**
* @param Member $member
*
* @return boolean
*/
* @param Member $member
*
* @return boolean
*/
public function canEdit($member = null)
{
return $this->Parent()->canEdit($member);
}
return $this->Parent()->canEdit($member);
}
/**
* @param Member $member
*
* @return boolean
*/
/**
* @param Member $member
*
* @return boolean
*/
public function canDelete($member = null)
{
return $this->canEdit($member);
}
return $this->canEdit($member);
}
/**
* @deprecated 5.0 Use "$Title.XML" in templates instead
* @return string
*/
/**
* @deprecated 5.0 Use "$Title.XML" in templates instead
* @return string
*/
public function getEscapedTitle()
{
return Convert::raw2att($this->Title);
}
return Convert::raw2att($this->Title);
}
/**
/**
* @param Member $member
* @return bool
*/
public function canView($member = null)
{
return $this->Parent()->canView($member);
}
return $this->Parent()->canView($member);
}
/**
* Return whether a user can create an object of this type
*
/**
* Return whether a user can create an object of this type
*
* @param Member $member
* @param array $context Virtual parameter to allow context to be passed in to check
* @return bool
*/
* @return bool
*/
public function canCreate($member = null, $context = [])
{
// Check parent page
// Check parent page
$parent = $this->getCanCreateContext(func_get_args());
if($parent) {
if ($parent) {
return $parent->canEdit($member);
}
// Fall back to secure admin permissions
return parent::canCreate($member);
}
}
/**
* Helper method to check the parent for this object
@ -131,11 +131,11 @@ class EditableOption extends DataObject
protected function getCanCreateContext($args)
{
// Inspect second parameter to canCreate for a 'Parent' context
if(isset($args[1]['Parent'])) {
if (isset($args[1]['Parent'])) {
return $args[1]['Parent'];
}
// Hack in currently edited page if context is missing
if(Controller::has_curr() && Controller::curr() instanceof CMSMain) {
if (Controller::has_curr() && Controller::curr() instanceof CMSMain) {
return Controller::curr()->currentPage();
}

View File

@ -55,7 +55,8 @@ class EditableRadioField extends EditableMultipleOptionField
return "$(\"input[name='{$this->Name}']{$first}\")";
}
public function isRadioField() {
return true;
}
public function isRadioField()
{
return true;
}
}

View File

@ -166,6 +166,5 @@ class EditableTextField extends EditableFormField
if ($this->Autocomplete) {
$field->setAttribute('autocomplete', $this->Autocomplete);
}
}
}

View File

@ -526,7 +526,8 @@ class EmailRecipient extends DataObject
*
* @return ValidationResult
*/
public function validate() {
public function validate()
{
$result = parent::validate();
$checkEmail = [
'EmailAddress' => 'EMAILADDRESSINVALID',
@ -540,8 +541,10 @@ class EmailRecipient extends DataObject
foreach ($addresses as $address) {
$trimAddress = trim($address);
if ($trimAddress && !Email::is_valid_address($trimAddress)) {
$error = _t(__CLASS.".$translation",
"Invalid email address $trimAddress");
$error = _t(
__CLASS.".$translation",
"Invalid email address $trimAddress"
);
$result->error($error . " ($trimAddress)");
}
}

View File

@ -35,7 +35,9 @@ class SubmittedFileField extends SubmittedFormField
if ($link) {
return DBField::create_field('HTMLText', sprintf(
'%s - <a href="%s" target="_blank">%s</a>',
$name, $link, $title
$name,
$link,
$title
));
}

View File

@ -71,7 +71,7 @@ class SubmittedForm extends DataObject
$fields->removeByName('Values');
//check to ensure there is a Member to extract an Email from else null value
if($this->SubmittedBy() && $this->SubmittedBy()->exists()){
if ($this->SubmittedBy() && $this->SubmittedBy()->exists()) {
$submitter = $this->SubmittedBy()->Email;
} else {
$submitter = null;

View File

@ -253,8 +253,8 @@ SQL;
'Created' => 'Created',
'LastEdited' => 'Last Edited'
);
foreach(EditableFormField::get()->filter(array('ParentID' => $parentID)) as $eff) {
if($eff->ShowInSummary) {
foreach (EditableFormField::get()->filter(array('ParentID' => $parentID)) as $eff) {
if ($eff->ShowInSummary) {
$summaryarray[$eff->Name] = $eff->Title ?: $eff->Name;
}
}
@ -305,10 +305,14 @@ SQL;
$fields = parent::getCMSFields();
if ($this->EmailRecipients()->Count() == 0 && static::config()->recipients_warning_enabled) {
$fields->addFieldToTab('Root.Main', LiteralField::create('EmailRecipientsWarning',
'<p class="message warning">' . _t(__CLASS__.'.NORECIPIENTS',
'Warning: You have not configured any recipients. Form submissions may be missed.')
. '</p>'), 'Title');
$fields->addFieldToTab('Root.Main', LiteralField::create(
'EmailRecipientsWarning',
'<p class="message warning">' . _t(
__CLASS__.'.NORECIPIENTS',
'Warning: You have not configured any recipients. Form submissions may be missed.'
)
. '</p>'
), 'Title');
}
return $fields;

View File

@ -153,7 +153,7 @@ class UserDefinedFormController extends PageController
});
})(jQuery);
JS
, 'UserFormsConditional');
, 'UserFormsConditional');
}
}

View File

@ -8,7 +8,8 @@ use SilverStripe\UserForms\FormField\UserFormsCheckboxSetField;
class UserFormsCheckboxSetFieldTest extends SapphireTest
{
public function testValidate() {
public function testValidate()
{
$field = new UserFormsCheckboxSetField('Field', 'My field', ['One' => 'One', 'Two' => 'Two']);
$validator = new RequiredFields();

View File

@ -64,5 +64,4 @@ class EditableFileFieldTest extends SapphireTest
$formField = $fileField->getFormField();
$this->assertEquals($formField->getValidator()->getAllowedMaxFileSize(), 262144);
}
}

View File

@ -394,7 +394,8 @@ class UserDefinedFormTest extends FunctionalTest
'street' => 'Anything',
'city' => 'Matches Not Equals',
'colours' => ['Red'] // matches 2
], null
],
null
)
->sort('EmailAddress')
->column('EmailAddress');
@ -414,7 +415,8 @@ class UserDefinedFormTest extends FunctionalTest
'street' => 'Matches Equals',
'city' => 'Anything',
'colours' => ['Red', 'Blue'] // matches 2
], null
],
null
)
->sort('EmailAddress')
->column('EmailAddress');
@ -434,7 +436,8 @@ class UserDefinedFormTest extends FunctionalTest
'street' => 'Matches Equals',
'city' => 'Anything',
'colours' => ['Blue']
], null
],
null
)->column('EmailAddress');
$this->assertEquals(
[
@ -451,7 +454,8 @@ class UserDefinedFormTest extends FunctionalTest
'street' => 'Wrong value for this field',
'city' => '',
'colours' => ['Blue', 'Green']
], null
],
null
)->column('EmailAddress');
$this->assertEquals(
['unfiltered@example.com'],
@ -481,16 +485,20 @@ class UserDefinedFormTest extends FunctionalTest
$this->logInWithPermission('ADMIN');
// test invalid email addresses fail validation
$recipient = $this->objFromFixture(EmailRecipient::class,
'invalid-recipient-list');
$recipient = $this->objFromFixture(
EmailRecipient::class,
'invalid-recipient-list'
);
$result = $recipient->validate();
$this->assertFalse($result->valid());
$this->assertContains('filtered.example.com', $result->message());
$this->assertNotContains('filtered2@example.com', $result->message());
// test valid email addresses pass validation
$recipient = $this->objFromFixture(EmailRecipient::class,
'valid-recipient-list');
$recipient = $this->objFromFixture(
EmailRecipient::class,
'valid-recipient-list'
);
$result = $recipient->validate();
$this->assertTrue($result->valid());
$this->assertEmpty($result->message());