silverstripe-frameworktest/code/BasicFieldsTestPage.php

264 lines
8.6 KiB
PHP
Raw Normal View History

2008-10-03 00:05:45 +02:00
<?php
class BasicFieldsTestPage extends TestPage {
2013-03-26 11:44:04 +01:00
private static $db = array(
'Required' => 'Text',
'Validated' => 'Text',
2008-10-03 00:05:45 +02:00
'Checkbox' => 'Boolean',
'Readonly' => 'Varchar',
'Textarea' => 'Text',
'Text' => 'Varchar',
'CalendarDate' => 'Date',
'CompositeDate' => 'Date',
'Date' => 'Date',
'DMYCalendarDate' => 'Date',
'DMYDate' => 'Date',
2010-07-10 04:30:23 +02:00
'DateTime' => 'Datetime',
'DateTimeWithCalendar' => 'Datetime',
2008-10-03 00:05:45 +02:00
'Time' => 'Time',
2010-07-10 04:30:23 +02:00
'TimeWithDropdown' => 'Time',
2008-10-03 00:05:45 +02:00
'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',
2008-10-03 00:05:45 +02:00
);
2013-03-26 11:44:04 +01:00
private static $has_one = array(
2008-10-03 00:05:45 +02:00
'Dropdown' => 'TestCategory',
'GroupedDropdown' => 'TestCategory',
'ListboxField' => 'TestCategory',
'File' => 'File',
'AttachedFile' => 'File',
'Image' => 'Image',
);
2013-03-26 11:44:04 +01:00
private static $has_many = array(
'HasManyFiles' => 'File',
);
2013-03-26 11:44:04 +01:00
private static $many_many = array(
'ManyManyFiles' => 'File',
'MultipleListboxField' => 'TestCategory',
2008-10-03 00:05:45 +02:00
);
private static $defaults = array(
'Validated' => 2
);
function requireDefaultRecords() {
parent::requireDefaultRecords();
if($inst = DataObject::get_one('BasicFieldsTestPage')) {
$data = $this->getDefaultData();
$inst->update($data);
$inst->write();
}
}
function getDefaultData() {
$cats = TestCategory::get();
if(!$cats->Count()) return array(); // not initialized yet
$firstCat = $cats->offsetGet(0);
$thirdCat = $cats->offsetGet(2);
return array(
'Readonly' => 'My value (ä!)',
'Textarea' => 'My value (ä!)',
'Required' => 'My required value (delete to test)',
'Validated' => '1',
'Text' => 'My value (ä!)',
'Textarea' => 'My value (ä!)',
'HTMLField' => 'My value (ä!)',
'Email' => 'test@test.com',
'Password' => 'My value (ä!)',
'Number' => 99,
'Price' => 99.99,
'PhoneNumber' => '021 1235',
'CreditCard' => '4000400040004111',
'Checkbox' => 1,
'CheckboxSetID' => $firstCat->ID,
'DropdownID' => $firstCat->ID,
'GroupedDropdownID' => $firstCat->ID,
'ListboxFieldID' => $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",
'TimeWithDropdown' => "23:59",
'DateTime' => "2002-10-23 23:59",
'DateTimeWithCalendar' => "2002-10-23 23:59",
);
}
2008-10-03 00:05:45 +02:00
function getCMSFields() {
2008-10-03 00:05:45 +02:00
$fields = parent::getCMSFields();
$description = 'This is <strong>bold</strong> help text';
2008-10-06 08:50:10 +02:00
$fields->addFieldsToTab('Root.Text', array(
Object::create('TextField', 'Required', 'Required field'),
Object::create('TextField', 'Validated', 'Validated field (checks range between 1 and 3)'),
Object::create('ReadonlyField', 'Readonly', 'ReadonlyField'),
Object::create('TextareaField', 'Textarea', 'TextareaField - 8 rows')
->setRows(8),
Object::create('TextField', 'Text', 'TextField'),
Object::create('HtmlEditorField', 'HTMLField', 'HtmlEditorField'),
Object::create('EmailField', 'Email', 'EmailField'),
Object::create('PasswordField', 'Password', 'PasswordField')
2008-10-06 08:50:10 +02:00
));
$fields->addFieldsToTab('Root.Numeric', array(
Object::create('NumericField', 'Number', 'NumericField'),
Object::create('CurrencyField', 'Price', 'CurrencyField'),
Object::create('PhoneNumberField', 'PhoneNumber', 'PhoneNumberField'),
Object::create('CreditCardField', 'CreditCard', 'CreditCardField')
2008-10-03 00:05:45 +02:00
));
2008-10-06 08:50:10 +02:00
$fields->addFieldsToTab('Root.Option', array(
Object::create('CheckboxField', 'Checkbox', 'CheckboxField'),
Object::create('CheckboxSetField', 'CheckboxSet', 'CheckboxSetField', TestCategory::map()),
Object::create('DropdownField', 'DropdownID', 'DropdownField', TestCategory::map())
->setHasEmptyDefault(true),
Object::create('GroupedDropdownField', 'GroupedDropdownID',
'GroupedDropdown', array('Test Categorys' => TestCategory::map())
),
Object::create('ListboxField', 'ListboxFieldID', 'ListboxField', TestCategory::map())
->setSize(3),
Object::create('ListboxField', 'MultipleListboxFieldID', 'ListboxField (multiple)', TestCategory::map())
->setMultiple(true)
->setSize(3),
Object::create('OptionsetField', 'OptionSet', 'OptionSetField', TestCategory::map()),
2008-10-06 08:50:10 +02:00
));
2008-10-03 00:05:45 +02:00
// All these date/time fields generally have issues saving directly in the CMS
$fields->addFieldsToTab('Root.DateTime', array(
$calendarDateField = Object::create('DateField', 'CalendarDate','DateField with calendar'),
Object::create('DateField', 'Date','DateField'),
$dmyDateField = Object::create('DateField', 'DMYDate','DateField with separate fields'),
Object::create('TimeField', 'Time','TimeField'),
$timeFieldDropdown = Object::create('TimeField', 'TimeWithDropdown','TimeField with dropdown'),
Object::create('DatetimeField', 'DateTime', 'DateTime'),
$dateTimeShowCalendar = Object::create('DatetimeField', 'DateTimeWithCalendar', 'DateTime with calendar')
2010-01-27 02:40:18 +01:00
));
2010-07-10 04:30:23 +02:00
$calendarDateField->setConfig('showcalendar', true);
$dmyDateField->setConfig('dmyfields', true);
$timeFieldDropdown->setConfig('showdropdown', true);
$dateTimeShowCalendar->getDateField()->setConfig('showcalendar', true);
$dateTimeShowCalendar->getTimeField()->setConfig('showdropdown', true);
2008-10-03 00:05:45 +02:00
$fields->addFieldsToTab('Root.File', array(
$bla = UploadField::create('File','FileUploadField')
->setDescription($description)
->setConfig('allowedMaxFileNumber', 1)
->setConfig('canPreviewFolder', false),
UploadField::create('AttachedFile','UploadField with canUpload=false')
->setDescription($description)
->setConfig('canUpload', false),
UploadField::create('Image','UploadField for image')
->setDescription($description),
UploadField::create('HasManyFiles','UploadField for has_many')
->setDescription($description),
UploadField::create('ManyManyFiles','UploadField for many_many')
->setDescription($description)
2008-10-03 00:05:45 +02:00
));
2008-11-13 22:32:37 +01:00
$data = $this->getDefaultData();
foreach($fields->dataFields() as $field) {
$name = $field->getName();
if(isset($data[$name])) {
$field->setValue($data[$name]);
}
}
$blacklist = array('DMYDate', 'Required', 'Validated');
$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
->setDescription($description);
// ->addExtraClass('cms-description-tooltip');
if(in_array($field->getName(), $blacklist)) continue;
$disabledField = $field->performDisabledTransformation();
$disabledField->setTitle($disabledField->Title() . ' (disabled)');
2012-03-02 01:03:02 +01:00
$disabledField->setName($disabledField->getName() . '_disabled');
$tabObj->insertAfter($disabledField, $field->getName());
$readonlyField = $field->performReadonlyTransformation();
$readonlyField->setTitle($readonlyField->Title() . ' (readonly)');
2012-03-02 01:03:02 +01:00
$readonlyField->setName($readonlyField->getName() . '_readonly');
$tabObj->insertAfter($readonlyField, $field->getName());
}
}
$noLabelField = new TextField('Text_NoLabel', false, 'TextField without label');
$noLabelField->setDescription($description);
$fields->addFieldToTab('Root.Text', $noLabelField, 'Text_disabled');
2012-03-08 10:36:12 +01:00
2013-01-16 10:30:00 +01:00
$fields->addFieldToTab('Root.Text',
FieldGroup::create(
TextField::create('MyFieldGroup1'),
TextField::create('MyFieldGroup2'),
DropdownField::create('MyFieldGroup3', false, TestCategory::map())
)
);
$fields->addFieldToTab('Root.Text',
FieldGroup::create(
'MyLabelledFieldGroup',
array(
TextField::create('MyLabelledFieldGroup1'),
TextField::create('MyLabelledFieldGroup2'),
DropdownField::create('MyLabelledFieldGroup3', null, TestCategory::map())
)
)->setTitle('My Labelled Field Group')
);
2008-10-03 00:05:45 +02:00
return $fields;
2008-11-13 22:32:37 +01:00
2008-10-03 00:05:45 +02:00
}
public function getCMSValidator() {
return new RequiredFields('Required');
}
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
}
class BasicFieldsTestPage_Controller extends TestPage_Controller {
function AutoCompleteItems() {
$items = array(
'TestItem1',
'TestItem2',
'TestItem3',
'TestItem4',
);
return implode(',', $items);
}
}
?>