'Boolean', 'CurrencyField' => 'Currency', 'DateField' => 'Date', 'DatePickerField' => 'Date', 'CalendarDateField' => 'Date', 'NumericField' => 'Int', 'HtmlEditorField' => 'HTMLText', 'SimpleTinyMCEField' => 'HTMLText', 'SimpleWysiwygField' => 'HTMLText', 'SimpleHTMLEditorField' => 'HTMLText', 'TextField' => 'Varchar(255)', 'TextareaField' => 'Text', 'TimeField' => 'Time', 'DropdownField' => 'Enum' ); /** * A lookup for translating some basic strings into their proper * CMS fields. * * @var array $field_transformations */ static $field_corrections = array ( 'DateField' => 'DatePickerField', 'CalendarDateField' => 'DatePickerField', 'Date' => 'DatePickerField', 'Text' => 'TextField', 'Textarea' => 'TextareaField' ); static $upload_fields = array ( 'FileField', 'ImageField', 'UploadField', 'ImageUploadField' ); public static function correct_field($field) { if(in_array($field, self::$upload_fields)) { $image = stristr($field,"Image"); if((class_exists('UploadifyField')) && (Bedrock::get_default('Uploader') == "Uploadify")) { return $image ? "ImageUploadField" : "FileUploadField"; } return $image ? "ImageField" : "FileIFrameField"; } elseif(isset(self::$field_corrections[$field])) { return self::$field_corrections[$field]; } return $field; } /** * Cleanly checks the {@link $form_to_db} array for a given field type. * If it is not specified, return a default value. * * @param string $field The field type to look up * @return string */ public static function form_to_db($field) { if(isset(self::$form_to_db[$field])) return self::$form_to_db[$field]; return Bedrock::get_default('CMSField'); } /** * @var string The controller (e.g. Page) that is controlling this form field */ protected $controllerClass = null; /** * Overloads the wildcard method to check if we need to tweak the CMS field name. * {@see BedrockField::$field_corrections}. * * @return string */ public function getCMSField() { if($field = $this->get('CMSField')) { return self::correct_field($field); } } /** * Checks if a DB field has been defined for this field. If not, try to convert the * CMS field into a DB field using {@see BedrockField::$form_to_db} * * @return bool|string */ public function getDBField() { if(!$field = $this->get('DBField')) { $db = isset(self::$form_to_db[$this->getCMSField()]) ? self::$form_to_db[$this->getCMSField()] : false; if($db == "Enum") { if($this->getMap()) { $db = "Enum('".implode(',',$this->getMap()->toArray())."')"; } } return $db; } return $this->get('DBField'); } /** * Getter for the key, which represents the name of the field in this class * * @return string */ public function getFieldName() { return $this->key; } public function setControllerClass($class) { $this->controllerClass = $class; } public function getTab() { $fullPage = (is_subclass_of($this->controllerClass,"SiteTree")); $t = $this->get('Tab'); // For DataObject popups, we don't always want tabs. If not specified, leave them out. if(!$t && !$fullPage) { return false; } if(!$fullPage) { $tab = ($t) ? "Root.".$t : "Root.Main"; } else { $tab = ($t) ? $t : "Root.Content.Main"; if(false === stristr($tab,"Root.")) { $tab = "Root.Content.{$tab}"; } } return $tab; } public function getBefore() { if($this->get('Before')) { return $this->get('Before'); } if($this->getTab() == "Root.Content.Main") { return "Content"; } return null; } public function getLabel() { if($label = $this->get('Label')) { return $label; } return FormField::name_to_label($this->getFieldName()); } public function getFormField() { $class = $this->controllerClass; $type = ($f = $this->getCMSField()) ? $f : singleton($class)->dbObject($this->getFieldName())->scaffoldFormField()->class; // Check if there's a custom wrapper for this form field. Some form fields work with BedrockSetting $bedrock_class = "Bedrock".$type; if(class_exists($bedrock_class)) { return Object::create($bedrock_class, $this); } if(class_exists($type)) { return Object::create($type, $this->getFieldName(), $this->getLabel()); } else { user_error("Field $type does not exist.",E_USER_ERROR); } } /** * Updates the $statics array for the decorator, supplying "db" keys * where available * * @param array $statics A reference to the extra static vars for the decorator. */ public function updateDB(&$statics) { if($this->getDBField()) { $statics['db'][$this->getFieldName()] = $this->getDBField(); } } /** * Updates the $statics array for the decorator, supplying "has_one" keys, where available * * @param array $statics A reference to the extra static vars for the decorator */ public function updateHasOne(&$statics) { if($fieldType = $this->getCMSField()) { $fieldName = $this->getFieldName(); switch(self::correct_field($fieldType)) { case "ImageField": case "ImageUploadField": $statics['has_one'][$fieldName] = $this->getImageClass(); break; case "FileIFrameField": case "FileUploadField": case "FileField": $statics['has_one'][$fieldName] = $this->getFileClass(); break; case "RelationDropdown": $statics['has_one'][$fieldName] = $this->getMap(); break; } } } }