2008-10-03 00:05:45 +02:00
< ? php
2016-07-01 04:37:50 +02:00
use SilverStripe\ORM\DataObject ;
use SilverStripe\FrameworkTest\Model\TestCategory ;
use SilverStripe\FrameworkTest\Model\TestPage ;
use SilverStripe\FrameworkTest\Model\TestPage_Controller ;
2016-09-07 07:10:55 +02:00
use SilverStripe\Core\Object ;
use SilverStripe\Forms\TextField ;
use SilverStripe\Forms\SelectionGroup_Item ;
use SilverStripe\Forms\FieldList ;
use SilverStripe\Forms\AssetField ;
use SilverStripe\Forms\UploadField ;
use SilverStripe\Forms\LabelField ;
use SilverStripe\Forms\LiteralField ;
use SilverStripe\Forms\DropdownField ;
use SilverStripe\Forms\CheckboxField ;
use SilverStripe\Forms\FieldGroup ;
use SilverStripe\Forms\CompositeField ;
use SilverStripe\Forms\RequiredFields ;
2016-07-01 04:37:50 +02:00
2015-12-17 21:20:49 +01:00
class BasicFieldsTestPage extends TestPage
{
private static $db = array (
'Required' => 'Text' ,
'Validated' => 'Text' ,
'Checkbox' => 'Boolean' ,
'Readonly' => 'Varchar' ,
'Textarea' => 'Text' ,
'Text' => 'Varchar' ,
'CalendarDate' => 'Date' ,
'CompositeDate' => 'Date' ,
'Date' => 'Date' ,
'DMYCalendarDate' => 'Date' ,
'DMYDate' => 'Date' ,
'DateTime' => 'Datetime' ,
'DateTimeWithCalendar' => 'Datetime' ,
'Time' => 'Time' ,
2016-06-16 03:35:41 +02:00
2015-12-17 21:20:49 +01:00
'Money' => 'Money' ,
'Number' => 'Int' ,
'Price' => 'Double' ,
'Email' => 'Varchar' ,
'Password' => 'Varchar' ,
'ConfirmedPassword' => 'Varchar' ,
'HTMLField' => 'HTMLText' ,
'HTMLOneLine' => 'HTMLVarchar' ,
'UniqueText' => 'Varchar' ,
'AjaxUniqueText' => 'Varchar' ,
'UniqueRestrictedText' => 'Varchar' ,
'BankNumber' => 'Varchar' ,
'PhoneNumber' => 'Varchar' ,
'Autocomplete' => 'Varchar' ,
'CreditCard' => 'Varchar' ,
'GSTNumber' => 'Varchar' ,
'OptionSet' => 'Int' ,
2016-06-16 03:31:34 +02:00
'DBFile' => 'DBFile' ,
2015-12-17 21:20:49 +01:00
);
2016-04-18 23:27:29 +02:00
2015-12-17 21:20:49 +01:00
private static $has_one = array (
2016-07-01 04:37:50 +02:00
'Dropdown' => 'SilverStripe\\FrameworkTest\\Model\\TestCategory' ,
'GroupedDropdown' => 'SilverStripe\\FrameworkTest\\Model\\TestCategory' ,
2016-09-07 07:10:55 +02:00
'File' => 'SilverStripe\\Assets\\File' ,
'AttachedFile' => 'SilverStripe\\Assets\\File' ,
'Image' => 'SilverStripe\\Assets\\Image' ,
2015-12-17 21:20:49 +01:00
);
2012-02-29 14:27:36 +01:00
2015-12-17 21:20:49 +01:00
private static $has_many = array (
2016-09-07 07:10:55 +02:00
'HasManyFiles' => 'SilverStripe\\Assets\\File' ,
2015-12-17 21:20:49 +01:00
);
2012-02-29 14:27:36 +01:00
2015-12-17 21:20:49 +01:00
private static $many_many = array (
2016-09-07 07:10:55 +02:00
'ManyManyFiles' => 'SilverStripe\\Assets\\File' ,
2016-07-01 04:37:50 +02:00
'MultipleListboxField' => 'SilverStripe\\FrameworkTest\\Model\\TestCategory' ,
2015-12-17 21:20:49 +01:00
);
2012-12-13 14:59:32 +01:00
2015-12-17 21:20:49 +01:00
private static $defaults = array (
'Validated' => 2
);
2013-09-18 13:01:56 +02:00
2015-12-17 21:20:49 +01:00
public function requireDefaultRecords ()
{
parent :: requireDefaultRecords ();
2012-12-13 14:59:32 +01:00
2015-12-17 21:20:49 +01:00
if ( $inst = DataObject :: get_one ( 'BasicFieldsTestPage' )) {
$data = $this -> getDefaultData ();
$inst -> update ( $data );
$inst -> write ();
}
}
2012-12-13 14:59:32 +01:00
2015-12-17 21:20:49 +01:00
public function getDefaultData ()
{
$cats = TestCategory :: get ();
if ( ! $cats -> Count ()) {
return array ();
} // not initialized yet
2012-12-13 14:59:32 +01:00
2015-12-17 21:20:49 +01:00
$firstCat = $cats -> offsetGet ( 0 );
$thirdCat = $cats -> offsetGet ( 2 );
2012-12-13 14:59:32 +01:00
2015-12-17 21:20:49 +01:00
return array (
'Readonly' => 'My value (ä!)' ,
'Textarea' => 'My value (ä!)' ,
'Required' => 'My required value (delete to test)' ,
'Validated' => '1' ,
'Text' => 'My value (ä!)' ,
'Textarea' => 'My value (ä!)' ,
2016-10-13 20:46:00 +02:00
'HTMLField' => 'My <strong>value</strong> (ä!)' ,
2015-12-17 21:20:49 +01:00
'Email' => 'test@test.com' ,
'Password' => 'My value (ä!)' ,
'Number' => 99 ,
'Price' => 99.99 ,
'MoneyAmount' => 99.99 ,
'MoneyCurrency' => 'EUR' ,
'PhoneNumber' => '021 1235' ,
'CreditCard' => '4000400040004111' ,
'Checkbox' => 1 ,
'CheckboxSetID' => $firstCat -> ID ,
'DropdownID' => $firstCat -> ID ,
'GroupedDropdownID' => $firstCat -> ID ,
'MultipleListboxFieldID' => join ( ',' , array ( $thirdCat -> ID , $firstCat -> ID )),
'OptionSet' => join ( ',' , array ( $thirdCat -> ID , $firstCat -> ID )),
'Date' => " 2002-10-23 " ,
'CalendarDate' => " 2002-10-23 " ,
'DMYDate' => " 2002-10-23 " ,
'Time' => " 23:59 " ,
'DateTime' => " 2002-10-23 23:59 " ,
'DateTimeWithCalendar' => " 2002-10-23 23:59 " ,
2016-06-16 03:11:29 +02:00
'MyFieldGroup1' => 'My value (ä!)' ,
'MyFieldGroup2' => 'My value (ä!)' ,
'MyFieldGroup3' => 'My value (ä!)' ,
'MyFieldGroupCheckbox' => true ,
'MyCompositeField1' => 'My value (ä!)' ,
'MyCompositeField2' => 'My value (ä!)' ,
'MyCompositeField3' => 'My value (ä!)' ,
'MyCompositeFieldCheckbox' => true ,
2015-12-17 21:20:49 +01:00
);
}
2016-04-18 23:27:29 +02:00
2015-12-17 21:20:49 +01:00
public function getCMSFields ()
{
$fields = parent :: getCMSFields ();
2012-12-13 14:59:32 +01:00
2015-12-17 21:20:49 +01:00
$description = 'This is <strong>bold</strong> help text' ;
2016-07-21 03:29:18 +02:00
$rightTitle = 'This is right title' ;
2016-04-18 23:27:29 +02:00
2015-12-17 21:20:49 +01:00
$fields -> addFieldsToTab ( 'Root.Text' , array (
2016-09-07 07:10:55 +02:00
Object :: create ( 'SilverStripe\\Forms\\TextField' , 'Required' , 'Required field' )
2016-07-21 03:29:18 +02:00
-> setRightTitle ( 'right title' ),
2016-09-07 07:10:55 +02:00
Object :: create ( 'SilverStripe\\Forms\\TextField' , 'Validated' , 'Validated field (checks range between 1 and 3)' ),
2016-09-14 06:31:58 +02:00
Object :: create ( 'SilverStripe\\Forms\\ReadonlyField' , 'Readonly' , 'ReadonlyField' ),
2016-09-07 07:10:55 +02:00
Object :: create ( 'SilverStripe\\Forms\\TextareaField' , 'Textarea' , 'TextareaField - 8 rows' )
2015-12-17 21:20:49 +01:00
-> setRows ( 8 ),
2016-09-14 06:31:58 +02:00
Object :: create ( 'SilverStripe\\Forms\\TextField' , 'Text' ),
2016-10-13 20:46:13 +02:00
Object :: create ( 'SilverStripe\\Forms\\HTMLEditor\\HTMLEditorField' , 'HTMLField' , 'HTMLField' ),
2016-09-14 06:31:58 +02:00
Object :: create ( 'SilverStripe\\Forms\\EmailField' , 'Email' ),
Object :: create ( 'SilverStripe\\Forms\\PasswordField' , 'Password' ),
Object :: create ( 'SilverStripe\\Forms\\ConfirmedPasswordField' , 'ConfirmedPassword' )
2015-12-17 21:20:49 +01:00
));
2012-12-13 14:59:32 +01:00
2015-12-17 21:20:49 +01:00
$fields -> addFieldsToTab ( 'Root.Numeric' , array (
2016-09-14 06:31:58 +02:00
Object :: create ( 'SilverStripe\\Forms\\NumericField' , 'Number' ),
Object :: create ( 'SilverStripe\\Forms\\CurrencyField' , 'Price' ),
Object :: create ( 'SilverStripe\\Forms\\MoneyField' , 'Money' , 'Money' , array ( 'Amount' => 99.99 , 'Currency' => 'EUR' )),
Object :: create ( 'SilverStripe\\Forms\\PhoneNumberField' , 'PhoneNumber' ),
Object :: create ( 'SilverStripe\\Forms\\CreditCardField' , 'CreditCard' )
2015-12-17 21:20:49 +01:00
));
2016-04-18 23:27:29 +02:00
2015-12-17 21:20:49 +01:00
$fields -> addFieldsToTab ( 'Root.Option' , array (
2016-09-14 06:31:58 +02:00
Object :: create ( 'SilverStripe\\Forms\\CheckboxField' , 'Checkbox' ),
Object :: create ( 'SilverStripe\\Forms\\CheckboxSetField' , 'CheckboxSet' , 'CheckboxSet' , TestCategory :: map ()),
Object :: create ( 'SilverStripe\\Forms\\DropdownField' , 'DropdownID' , 'DropdownField' , TestCategory :: map ())
2015-12-17 21:20:49 +01:00
-> setHasEmptyDefault ( true ),
2016-09-07 07:10:55 +02:00
Object :: create ( 'SilverStripe\\Forms\\GroupedDropdownField' , 'GroupedDropdownID' ,
2016-09-14 06:31:58 +02:00
'GroupedDropdown' , array ( 'Test Categories' => TestCategory :: map ())
2015-12-17 21:20:49 +01:00
),
2016-09-07 07:10:55 +02:00
Object :: create ( 'SilverStripe\\Forms\\ListboxField' , 'MultipleListboxFieldID' , 'ListboxField (multiple)' , TestCategory :: map ())
2015-12-17 21:20:49 +01:00
-> setSize ( 3 ),
2016-09-07 07:10:55 +02:00
Object :: create ( 'SilverStripe\\Forms\\OptionsetField' , 'OptionSet' , 'OptionSetField' , TestCategory :: map ()),
2016-09-14 06:31:58 +02:00
Object :: create ( 'SilverStripe\\Forms\\SelectionGroup' , 'SelectionGroup' , array (
2016-06-16 03:11:29 +02:00
new SelectionGroup_Item (
'one' ,
2016-06-16 04:14:13 +02:00
TextField :: create ( 'SelectionGroupOne' , 'one view' ),
2016-06-16 03:11:29 +02:00
'SelectionGroup Option One'
),
new SelectionGroup_Item (
'two' ,
2016-06-16 04:14:13 +02:00
TextField :: create ( 'SelectionGroupOneTwo' , 'two view' ),
2016-06-16 03:11:29 +02:00
'SelectionGroup Option Two'
)
)),
2016-09-14 06:31:58 +02:00
Object :: create ( 'SilverStripe\\Forms\\ToggleCompositeField' , 'ToggleCompositeField' , 'ToggleCompositeField' , new FieldList (
2016-09-07 07:10:55 +02:00
Object :: create ( 'SilverStripe\\Forms\\TextField' , 'ToggleCompositeTextField1' ),
Object :: create ( 'SilverStripe\\Forms\\TextField' , 'ToggleCompositeTextField2' ),
Object :: create ( 'SilverStripe\\Forms\\DropdownField' , 'ToggleCompositeDropdownField' , 'ToggleCompositeDropdownField' , TestCategory :: map ()),
Object :: create ( 'SilverStripe\\Forms\\TextField' , 'ToggleCompositeTextField3' )
2015-12-17 21:20:49 +01:00
))
));
2008-10-03 00:05:45 +02:00
2015-12-17 21:20:49 +01:00
// All these date/time fields generally have issues saving directly in the CMS
$fields -> addFieldsToTab ( 'Root.DateTime' , array (
2016-09-07 07:10:55 +02:00
$calendarDateField = Object :: create ( 'SilverStripe\\Forms\\DateField' , 'CalendarDate' , 'DateField with calendar' ),
2016-09-14 06:31:58 +02:00
Object :: create ( 'SilverStripe\\Forms\\DateField' , 'Date' , 'DateField' ),
2016-09-07 07:10:55 +02:00
$dmyDateField = Object :: create ( 'SilverStripe\\Forms\\DateField' , 'DMYDate' , 'DateField with separate fields' ),
2016-09-14 06:31:58 +02:00
Object :: create ( 'SilverStripe\\Forms\\TimeField' , 'Time' , 'TimeField' ),
2016-09-07 07:10:55 +02:00
Object :: create ( 'SilverStripe\\Forms\\DatetimeField' , 'DateTime' , 'DateTime' ),
$dateTimeShowCalendar = Object :: create ( 'SilverStripe\\Forms\\DatetimeField' , 'DateTimeWithCalendar' , 'DateTime with calendar' )
2015-12-17 21:20:49 +01:00
));
$calendarDateField -> setConfig ( 'showcalendar' , true );
$dmyDateField -> setConfig ( 'dmyfields' , true );
$dateTimeShowCalendar -> getDateField () -> setConfig ( 'showcalendar' , true );
$dateTimeShowCalendar -> getTimeField () -> setConfig ( 'showdropdown' , true );
2016-07-21 03:29:18 +02:00
$dateTimeShowCalendar -> setRightTitle ( 'Right title' );
2008-10-03 00:05:45 +02:00
2015-12-17 21:20:49 +01:00
$fields -> addFieldsToTab ( 'Root.File' , array (
2016-06-16 03:31:34 +02:00
AssetField :: create ( 'DBFile' ),
2016-09-10 12:14:11 +02:00
$bla = UploadField :: create ( 'File' , 'FileUploadField' )
2015-12-17 21:20:49 +01:00
-> setDescription ( $description )
2016-07-21 03:29:18 +02:00
-> setRightTitle ( $rightTitle )
2015-12-17 21:20:49 +01:00
-> setConfig ( 'allowedMaxFileNumber' , 1 )
-> setConfig ( 'canPreviewFolder' , false ),
UploadField :: create ( 'AttachedFile' , 'UploadField with canUpload=false' )
-> setDescription ( $description )
2016-07-21 03:29:18 +02:00
-> setRightTitle ( $rightTitle )
2015-12-17 21:20:49 +01:00
-> setConfig ( 'canUpload' , false ),
2016-09-10 12:14:11 +02:00
UploadField :: create ( 'Image' , 'UploadField for image' )
2016-07-21 03:29:18 +02:00
-> setDescription ( $description )
-> setRightTitle ( $rightTitle ),
2015-12-17 21:20:49 +01:00
UploadField :: create ( 'HasManyFiles' , 'UploadField for has_many' )
2016-07-21 03:29:18 +02:00
-> setRightTitle ( $rightTitle )
2015-12-17 21:20:49 +01:00
-> setDescription ( $description ),
UploadField :: create ( 'ManyManyFiles' , 'UploadField for many_many' )
-> setDescription ( $description )
2016-07-21 03:29:18 +02:00
-> setRightTitle ( $rightTitle ),
2015-12-17 21:20:49 +01:00
));
2008-11-13 22:32:37 +01:00
2015-12-17 21:20:49 +01:00
$data = $this -> getDefaultData ();
foreach ( $fields -> dataFields () as $field ) {
$name = $field -> getName ();
if ( isset ( $data [ $name ])) {
$field -> setValue ( $data [ $name ]);
}
}
2012-12-13 14:59:32 +01:00
2015-12-17 21:20:49 +01:00
$blacklist = array (
2016-09-14 06:31:58 +02:00
'DMYDate' , 'Required' , 'Validated' , 'ToggleCompositeField' , 'SelectionGroup'
2015-12-17 21:20:49 +01:00
);
2012-12-13 14:59:32 +01:00
2015-12-17 21:20:49 +01:00
$tabs = array ( 'Root.Text' , 'Root.Numeric' , 'Root.Option' , 'Root.DateTime' , 'Root.File' );
foreach ( $tabs as $tab ) {
$tabObj = $fields -> fieldByName ( $tab );
foreach ( $tabObj -> FieldList () as $field ) {
$field
2016-07-21 03:29:18 +02:00
-> setDescription ( $description )
-> setRightTitle ( $rightTitle );
2015-12-17 21:20:49 +01:00
// ->addExtraClass('cms-description-tooltip');
2012-12-13 14:59:32 +01:00
2015-12-17 21:20:49 +01:00
if ( in_array ( $field -> getName (), $blacklist )) {
continue ;
}
2012-11-30 12:08:46 +01:00
2015-12-17 21:20:49 +01:00
$disabledField = $field -> performDisabledTransformation ();
$disabledField -> setTitle ( $disabledField -> Title () . ' (disabled)' );
$disabledField -> setName ( $disabledField -> getName () . '_disabled' );
2016-10-13 20:47:57 +02:00
$disabledField -> setValue ( $field -> Value ());
2015-12-17 21:20:49 +01:00
$tabObj -> insertAfter ( $disabledField , $field -> getName ());
2012-02-29 14:39:11 +01:00
2015-12-17 21:20:49 +01:00
$readonlyField = $field -> performReadonlyTransformation ();
$readonlyField -> setTitle ( $readonlyField -> Title () . ' (readonly)' );
$readonlyField -> setName ( $readonlyField -> getName () . '_readonly' );
2016-10-13 20:47:57 +02:00
$readonlyField -> setValue ( $field -> Value ());
2015-12-17 21:20:49 +01:00
$tabObj -> insertAfter ( $readonlyField , $field -> getName ());
}
}
2012-02-29 14:39:11 +01:00
2015-12-17 21:20:49 +01:00
$noLabelField = new TextField ( 'Text_NoLabel' , false , 'TextField without label' );
$noLabelField -> setDescription ( $description );
2016-07-21 03:29:18 +02:00
$noLabelField -> setRightTitle ( $rightTitle );
2015-12-17 21:20:49 +01:00
$fields -> addFieldToTab ( 'Root.Text' , $noLabelField , 'Text_disabled' );
2016-07-21 03:29:18 +02:00
$fields -> addFieldToTab ( 'Root.Text' ,
2016-09-14 06:31:58 +02:00
LabelField :: create ( 'SilverStripe\\Forms\\LabelField' , 'LabelField' )
2016-06-16 03:11:29 +02:00
);
2016-07-21 03:29:18 +02:00
$fields -> addFieldToTab ( 'Root.Text' ,
2016-09-07 07:10:55 +02:00
LiteralField :: create ( 'SilverStripe\\Forms\\LiteralField' , '<div class="form__divider">LiteralField with <b>some bold text</b> and <a href="http://silverstripe.com">a link</a></div>' )
2016-06-16 03:31:34 +02:00
);
2012-03-08 10:36:12 +01:00
2015-12-17 21:20:49 +01:00
$fields -> addFieldToTab ( 'Root.Text' ,
FieldGroup :: create (
TextField :: create ( 'MyFieldGroup1' ),
TextField :: create ( 'MyFieldGroup2' ),
2016-06-16 03:11:29 +02:00
DropdownField :: create ( 'MyFieldGroup3' , false , TestCategory :: map ()),
CheckboxField :: create ( 'MyFieldGroupCheckbox' )
2015-12-17 21:20:49 +01:00
)
2016-07-21 03:29:18 +02:00
-> setDescription ( $description )
-> setRightTitle ( $rightTitle )
2015-12-17 21:20:49 +01:00
);
$fields -> addFieldToTab ( 'Root.Text' ,
FieldGroup :: create (
'MyLabelledFieldGroup' ,
array (
TextField :: create ( 'MyLabelledFieldGroup1' ),
TextField :: create ( 'MyLabelledFieldGroup2' ),
2016-06-16 03:11:29 +02:00
DropdownField :: create ( 'MyLabelledFieldGroup3' , null , TestCategory :: map ()),
CheckboxField :: create ( 'MyLabelledFieldGroupCheckbox' )
2015-12-17 21:20:49 +01:00
)
2016-07-21 03:29:18 +02:00
)
-> setTitle ( 'My Labelled Field Group' )
-> setDescription ( $description )
-> setRightTitle ( $rightTitle )
2015-12-17 21:20:49 +01:00
);
2016-07-21 03:29:18 +02:00
2016-06-16 03:11:29 +02:00
$fields -> addFieldToTab ( 'Root.Text' ,
CompositeField :: create (
TextField :: create ( 'MyCompositeField1' ),
TextField :: create ( 'MyCompositeField2' ),
DropdownField :: create ( 'MyCompositeField3' , 'MyCompositeField3' , TestCategory :: map ()),
CheckboxField :: create ( 'MyCompositeFieldCheckbox' )
)
);
2013-01-16 10:30:00 +01:00
2015-12-17 21:20:49 +01:00
return $fields ;
}
2008-11-13 22:32:37 +01:00
2015-12-17 21:20:49 +01:00
public function getCMSValidator ()
{
return new RequiredFields ( 'Required' );
}
2013-09-18 13:01:56 +02:00
2015-12-17 21:20:49 +01:00
public function validate ()
{
$result = parent :: validate ();
if ( ! $this -> Validated || $this -> Validated < 1 || $this -> Validated > 3 ) {
$result -> error ( '"Validated" field needs to be between 1 and 3' );
}
return $result ;
}
2008-10-03 00:05:45 +02:00
}
2015-12-17 21:20:49 +01:00
class BasicFieldsTestPage_Controller extends TestPage_Controller
{
public function AutoCompleteItems ()
{
$items = array (
'TestItem1' ,
'TestItem2' ,
'TestItem3' ,
'TestItem4' ,
);
return implode ( ',' , $items );
}
2008-10-03 00:05:45 +02:00
}