Added segment field

This commit is contained in:
Christopher Pitt 2015-08-12 08:57:19 +12:00 committed by Damian Mooyman
parent 766555928e
commit 85f47e3623
10 changed files with 173 additions and 48 deletions

View File

@ -484,7 +484,7 @@ class UserDefinedForm_Controller extends Page_Controller {
$watch[$fieldToWatch] = array(); $watch[$fieldToWatch] = array();
} }
$watch[$fieldToWatch][] = array( $watch[$fieldToWatch][] = array(
'expression' => $expression, 'expression' => $expression,
'holder_selector' => $holderSelector, 'holder_selector' => $holderSelector,
'view' => $view, 'view' => $view,
@ -505,7 +505,7 @@ class UserDefinedForm_Controller extends Page_Controller {
foreach($values as $rule) { foreach($values as $rule) {
// Register conditional behaviour with an element, so it can be triggered from many places. // Register conditional behaviour with an element, so it can be triggered from many places.
$logic[] = sprintf( $logic[] = sprintf(
'if(%s) { %s.%s(); } else { %2$s.%s(); }', 'if(%s) { %s.%s(); } else { %2$s.%s(); }',
$rule['expression'], $rule['expression'],
$rule['holder_selector'], $rule['holder_selector'],
$rule['view'], $rule['view'],
@ -574,7 +574,7 @@ JS
} }
if( if(
!isset($data[$field->Name]) || !isset($data[$field->Name]) ||
!$data[$field->Name] || !$data[$field->Name] ||
!$formField->validate($form->getValidator()) !$formField->validate($form->getValidator())
) { ) {

View File

@ -15,6 +15,7 @@ class EditableCheckboxGroupField extends EditableMultipleOptionField {
public function getFormField() { public function getFormField() {
$field = new UserFormsCheckboxSetField($this->Name, $this->EscapedTitle, $this->getOptionsMap()); $field = new UserFormsCheckboxSetField($this->Name, $this->EscapedTitle, $this->getOptionsMap());
$field->setTemplate('forms/UserFormsCheckboxSetField');
// Set the default checked items // Set the default checked items
$defaultCheckedItems = $this->getDefaultOptions(); $defaultCheckedItems = $this->getDefaultOptions();

View File

@ -1,9 +1,17 @@
<?php <?php
use SilverStripe\Forms\SegmentField;
use SilverStripe\Forms\SegmentFieldModifier\IDSegmentFieldModifier;
use SilverStripe\Forms\SegmentFieldModifier\SlugSegmentFieldModifier;
/** /**
* Represents the base class of a editable form field * Represents the base class of a editable form field
* object like {@link EditableTextField}. * object like {@link EditableTextField}.
* *
* @package userforms * @package userforms
*
* @property string Name
*
* @method DataList DisplayRules() List of EditableCustomRule objects * @method DataList DisplayRules() List of EditableCustomRule objects
*/ */
class EditableFormField extends DataObject { class EditableFormField extends DataObject {
@ -39,7 +47,7 @@ class EditableFormField extends DataObject {
* @var string * @var string
*/ */
private static $default_sort = '"Sort"'; private static $default_sort = '"Sort"';
/** /**
* A list of CSS classes that can be added * A list of CSS classes that can be added
* *
@ -111,14 +119,14 @@ class EditableFormField extends DataObject {
* Set the visibility of an individual form field * Set the visibility of an individual form field
* *
* @param bool * @param bool
*/ */
public function setReadonly($readonly = true) { public function setReadonly($readonly = true) {
$this->readonly = $readonly; $this->readonly = $readonly;
} }
/** /**
* Returns whether this field is readonly * Returns whether this field is readonly
* *
* @return bool * @return bool
*/ */
private function isReadonly() { private function isReadonly() {
@ -154,7 +162,11 @@ class EditableFormField extends DataObject {
), ),
TextField::create('Title'), TextField::create('Title'),
TextField::create('Default', _t('EditableFormField.DEFAULT', 'Default value')), TextField::create('Default', _t('EditableFormField.DEFAULT', 'Default value')),
TextField::create('RightTitle', _t('EditableFormField.RIGHTTITLE', 'Right title')) TextField::create('RightTitle', _t('EditableFormField.RIGHTTITLE', 'Right title')),
SegmentField::create('Name')->setModifiers(array(
UnderscoreSegmentFieldModifier::create()->setDefault('Field'),
DisambiguationSegmentFieldModifier::create(),
))->setPreview($this->Name)
) )
); );
@ -281,20 +293,20 @@ class EditableFormField extends DataObject {
// Set a field name. // Set a field name.
if(!$this->Name) { if(!$this->Name) {
$this->Name = $this->RecordClassName . $this->ID; $this->Name = get_class($this) . '_' . $this->ID;
$this->write(); $this->write();
} }
} }
/** /**
* Flag indicating that this field will set its own error message via data-msg='' attributes * Flag indicating that this field will set its own error message via data-msg='' attributes
* *
* @return bool * @return bool
*/ */
public function getSetsOwnError() { public function getSetsOwnError() {
return false; return false;
} }
/** /**
* Return whether a user can delete this form field * Return whether a user can delete this form field
* based on whether they can edit the page * based on whether they can edit the page
@ -308,7 +320,7 @@ class EditableFormField extends DataObject {
return true; return true;
} }
/** /**
* Return whether a user can edit this form field * Return whether a user can edit this form field
* based on whether they can edit the page * based on whether they can edit the page
@ -322,10 +334,10 @@ class EditableFormField extends DataObject {
return true; return true;
} }
/** /**
* Publish this Form Field to the live site * Publish this Form Field to the live site
* *
* Wrapper for the {@link Versioned} publish function * Wrapper for the {@link Versioned} publish function
*/ */
public function doPublish($fromStage, $toStage, $createNewVersion = false) { public function doPublish($fromStage, $toStage, $createNewVersion = false) {
@ -336,7 +348,7 @@ class EditableFormField extends DataObject {
$rule->doPublish($fromStage, $toStage, $createNewVersion); $rule->doPublish($fromStage, $toStage, $createNewVersion);
} }
} }
/** /**
* Delete this form from a given stage * Delete this form from a given stage
* *
@ -350,7 +362,7 @@ class EditableFormField extends DataObject {
$rule->deleteFromStage($stage); $rule->deleteFromStage($stage);
} }
} }
/** /**
* checks wether record is new, copied from Sitetree * checks wether record is new, copied from Sitetree
*/ */
@ -375,7 +387,7 @@ class EditableFormField extends DataObject {
return ($stageVersion && $stageVersion != $liveVersion); return ($stageVersion && $stageVersion != $liveVersion);
} }
/** /**
* @deprecated since version 4.0 * @deprecated since version 4.0
*/ */
@ -383,7 +395,7 @@ class EditableFormField extends DataObject {
Deprecation::notice('4.0', 'getSettings is deprecated'); Deprecation::notice('4.0', 'getSettings is deprecated');
return (!empty($this->CustomSettings)) ? unserialize($this->CustomSettings) : array(); return (!empty($this->CustomSettings)) ? unserialize($this->CustomSettings) : array();
} }
/** /**
* @deprecated since version 4.0 * @deprecated since version 4.0
*/ */
@ -391,7 +403,7 @@ class EditableFormField extends DataObject {
Deprecation::notice('4.0', 'setSettings is deprecated'); Deprecation::notice('4.0', 'setSettings is deprecated');
$this->CustomSettings = serialize($settings); $this->CustomSettings = serialize($settings);
} }
/** /**
* @deprecated since version 4.0 * @deprecated since version 4.0
*/ */
@ -399,13 +411,13 @@ class EditableFormField extends DataObject {
Deprecation::notice('4.0', "setSetting({$key}) is deprecated"); Deprecation::notice('4.0', "setSetting({$key}) is deprecated");
$settings = $this->getSettings(); $settings = $this->getSettings();
$settings[$key] = $value; $settings[$key] = $value;
$this->setSettings($settings); $this->setSettings($settings);
} }
/** /**
* Set the allowed css classes for the extraClass custom setting * Set the allowed css classes for the extraClass custom setting
* *
* @param array The permissible CSS classes to add * @param array The permissible CSS classes to add
*/ */
public function setAllowedCss(array $allowed) { public function setAllowedCss(array $allowed) {
@ -430,7 +442,7 @@ class EditableFormField extends DataObject {
} }
return ''; return '';
} }
/** /**
* Get the path to the icon for this field type, relative to the site root. * Get the path to the icon for this field type, relative to the site root.
* *
@ -439,7 +451,7 @@ class EditableFormField extends DataObject {
public function getIcon() { public function getIcon() {
return USERFORMS_DIR . '/images/' . strtolower($this->class) . '.png'; return USERFORMS_DIR . '/images/' . strtolower($this->class) . '.png';
} }
/** /**
* Return whether or not this field has addable options * Return whether or not this field has addable options
* such as a dropdown field or radio set * such as a dropdown field or radio set
@ -449,11 +461,11 @@ class EditableFormField extends DataObject {
public function getHasAddableOptions() { public function getHasAddableOptions() {
return false; return false;
} }
/** /**
* Return whether or not this field needs to show the extra * Return whether or not this field needs to show the extra
* options dropdown list * options dropdown list
* *
* @return bool * @return bool
*/ */
public function showExtraOptions() { public function showExtraOptions() {
@ -519,21 +531,21 @@ class EditableFormField extends DataObject {
Deprecation::notice('4.0', "getFieldName({$field}) is deprecated"); Deprecation::notice('4.0', "getFieldName({$field}) is deprecated");
return ($field) ? "Fields[".$this->ID."][".$field."]" : "Fields[".$this->ID."]"; return ($field) ? "Fields[".$this->ID."][".$field."]" : "Fields[".$this->ID."]";
} }
/** /**
* @deprecated since version 4.0 * @deprecated since version 4.0
*/ */
public function getSettingName($field) { public function getSettingName($field) {
Deprecation::notice('4.0', "getSettingName({$field}) is deprecated"); Deprecation::notice('4.0', "getSettingName({$field}) is deprecated");
$name = $this->getFieldName('CustomSettings'); $name = $this->getFieldName('CustomSettings');
return $name . '[' . $field .']'; return $name . '[' . $field .']';
} }
/** /**
* Append custom validation fields to the default 'Validation' * Append custom validation fields to the default 'Validation'
* section in the editable options view * section in the editable options view
* *
* @return FieldList * @return FieldList
*/ */
public function getFieldValidationOptions() { public function getFieldValidationOptions() {
@ -543,12 +555,12 @@ class EditableFormField extends DataObject {
); );
$this->extend('updateFieldValidationOptions', $fields); $this->extend('updateFieldValidationOptions', $fields);
return $fields; return $fields;
} }
/** /**
* Return a FormField to appear on the front end. Implement on * Return a FormField to appear on the front end. Implement on
* your subclass. * your subclass.
* *
* @return FormField * @return FormField
@ -596,13 +608,13 @@ class EditableFormField extends DataObject {
$field->setTitle($title); $field->setTitle($title);
} }
} }
// if this field has an extra class // if this field has an extra class
if($this->ExtraClass) { if($this->ExtraClass) {
$field->addExtraClass($this->ExtraClass); $field->addExtraClass($this->ExtraClass);
} }
} }
/** /**
* Return the instance of the submission field class * Return the instance of the submission field class
* *
@ -611,8 +623,8 @@ class EditableFormField extends DataObject {
public function getSubmittedFormField() { public function getSubmittedFormField() {
return new SubmittedFormField(); return new SubmittedFormField();
} }
/** /**
* Show this form field (and its related value) in the reports and in emails. * Show this form field (and its related value) in the reports and in emails.
* *
@ -631,10 +643,10 @@ class EditableFormField extends DataObject {
public function getErrorMessage() { public function getErrorMessage() {
$title = strip_tags("'". ($this->Title ? $this->Title : $this->Name) . "'"); $title = strip_tags("'". ($this->Title ? $this->Title : $this->Name) . "'");
$standard = sprintf(_t('Form.FIELDISREQUIRED', '%s is required').'.', $title); $standard = sprintf(_t('Form.FIELDISREQUIRED', '%s is required').'.', $title);
// only use CustomErrorMessage if it has a non empty value // only use CustomErrorMessage if it has a non empty value
$errorMessage = (!empty($this->CustomErrorMessage)) ? $this->CustomErrorMessage : $standard; $errorMessage = (!empty($this->CustomErrorMessage)) ? $this->CustomErrorMessage : $standard;
return DBField::create_field('Varchar', $errorMessage); return DBField::create_field('Varchar', $errorMessage);
} }
@ -655,7 +667,7 @@ class EditableFormField extends DataObject {
} }
if( if(
!isset($data[$this->Name]) || !isset($data[$this->Name]) ||
!$data[$this->Name] || !$data[$this->Name] ||
!$formField->validate($form->getValidator()) !$formField->validate($form->getValidator())
) { ) {
@ -678,7 +690,7 @@ class EditableFormField extends DataObject {
$this->ShowOnLoad = $data['ShowOnLoad'] === '' || ($data['ShowOnLoad'] && $data['ShowOnLoad'] !== 'Hide'); $this->ShowOnLoad = $data['ShowOnLoad'] === '' || ($data['ShowOnLoad'] && $data['ShowOnLoad'] !== 'Hide');
unset($data['ShowOnLoad']); unset($data['ShowOnLoad']);
} }
// Migrate all other settings // Migrate all other settings
foreach($data as $key => $value) { foreach($data as $key => $value) {
if($this->hasField($key)) { if($this->hasField($key)) {

View File

@ -0,0 +1,54 @@
<?php
use SilverStripe\Forms\SegmentFieldModifier\AbstractSegmentFieldModifier;
class DisambiguationSegmentFieldModifier extends AbstractSegmentFieldModifier {
/**
* @inheritdoc
*
* @param string $value
*
* @return string
*/
public function getPreview($value) {
if($this->form instanceof Form && $record = $this->form->getRecord()) {
$parent = $record->Parent();
$try = $value;
$sibling = EditableformField::get()
->filter('ParentID', $parent->ID)
->filter('Name', $try)
->where('"ID" != ' . $record->ID)
->first();
$counter = 1;
while($sibling !== null) {
$try = $value . '_' . $counter++;
$sibling = EditableformField::get()
->filter('ParentID', $parent->ID)
->filter('Name', $try)
->first();
}
if ($try !== $value) {
return $try;
}
}
return $value;
}
/**
* @inheritdoc
*
* @param string $value
*
* @return string
*/
public function getSuggestion($value) {
return $this->getPreview($value);
}
}

View File

@ -0,0 +1,27 @@
<?php
use SilverStripe\Forms\SegmentFieldModifier\SlugSegmentFieldModifier;
class UnderscoreSegmentFieldModifier extends SlugSegmentFieldModifier {
/**
* @inheritdoc
*
* @param string $value
*
* @return string
*/
public function getPreview($value) {
return str_replace('-', '_', parent::getPreview($value));
}
/**
* @inheritdoc
*
* @param string $value
*
* @return string
*/
public function getSuggestion($value) {
return str_replace('-', '_', parent::getSuggestion($value));
}
}

View File

@ -11,7 +11,8 @@
"require": { "require": {
"silverstripe/framework": ">=3.1.0", "silverstripe/framework": ">=3.1.0",
"silverstripe/cms": ">=3.1.0", "silverstripe/cms": ">=3.1.0",
"silverstripe-australia/gridfieldextensions": "1.1.0" "silverstripe-australia/gridfieldextensions": "1.1.0",
"silverstripe/segment-field": "^1.0"
}, },
"suggest": { "suggest": {
"colymba/gridfield-bulk-editing-tools": "Allows for bulk management of form submissions" "colymba/gridfield-bulk-editing-tools": "Allows for bulk management of form submissions"

View File

@ -0,0 +1,6 @@
<div data-id="$ID" class="field<% if extraClass %> $extraClass<% end_if %>">
$Field
<label class="right" for="$Name">$Title</label>
<% if $Message %><span class="message $MessageType">$Message</span><% end_if %>
<% if $Description %><span class="description">$Description</span><% end_if %>
</div>

View File

@ -0,0 +1,12 @@
<ul data-id="$ID" class="$extraClass">
<% if $Options.Count %>
<% loop $Options %>
<li class="$Class">
<input id="$Name[$Value]" class="checkbox" name="$Name" type="checkbox" value="$Value"<% if $isChecked %> checked="checked"<% end_if %><% if $isDisabled %> disabled="disabled"<% end_if %> />
<label for="$Name[$Value]">$Title</label>
</li>
<% end_loop %>
<% else %>
<li>No options available</li>
<% end_if %>
</ul>

View File

@ -0,0 +1,9 @@
<div data-id="$ID" class="field<% if $extraClass %> $extraClass<% end_if %>">
<% if $Title %><label class="left" for="$Name">$Title</label><% end_if %>
<div class="middleColumn">
$Field
</div>
<% if $RightTitle %><label class="right" for="$Name">$RightTitle</label><% end_if %>
<% if $Message %><span class="message $MessageType">$Message</span><% end_if %>
<% if $Description %><span class="description">$Description</span><% end_if %>
</div>

View File

@ -15,10 +15,13 @@ class UserDefinedFormControllerTest extends FunctionalTest {
$this->autoFollowRedirection = false; $this->autoFollowRedirection = false;
$this->clearEmails(); $this->clearEmails();
// load the form // load the form
$this->get($form->URLSegment); $this->get($form->URLSegment);
$response = $this->submitForm('UserForm_Form', null, array('basic-text-name' => 'Basic Value'));
$field = $this->objFromFixture('EditableTextField', 'basic-text');
$response = $this->submitForm('UserForm_Form', null, array($field->Name => 'Basic Value'));
// should have a submitted form field now // should have a submitted form field now
$submitted = DataObject::get('SubmittedFormField', "\"Name\" = 'basic-text-name'"); $submitted = DataObject::get('SubmittedFormField', "\"Name\" = 'basic-text-name'");