Version: current

     1 <?php
     2 /**
     3  * EditablePhoneField
     4  *
     5  * Allow users to define a validating phone email field for a UserDefinedForm
     6  *
     7  * @package userforms
     8  */
     9
    10 class EditablePhoneField extends EditableFormField {
    11
    12     static $singular_name = 'Phone Field';
    13
    14     static $plural_name = 'Phone Fields';
    15
    16     static $phone_types = array('all', 'russian', 'rusMobile', 'rusSpaces', 'rusBrackets');
    17
    18     static function get_phone_types() {
    19         $types = array('' => _t('EditableFormField.NOTSELECTED'));
    20         if (count(self::$phone_types)){
    21             foreach(self::$phone_types as $phone_type) {
    22                 $types[$phone_type] = _t("EditablePhoneField.PhoneTypeTitle_{$phone_type}");
    23             }
    24         }
    25         return $types;
    26     }
    27
    28     function getFieldConfiguration() {
    29         $fields = parent::getFieldConfiguration();
    30
    31         //$mask = ($this->getSetting('Mask')) ? $this->getSetting('Mask') : '';
    32         //$pattern = ($this->getSetting('Pattern')) ? $this->getSetting('Pattern') : '';
    33
    34         if (count(self::$phone_types)) {
    35             $phoneType = ($this->getSetting('PhoneType')) ? $this->getSetting('PhoneType') : '';
    36             $fields->push($f = new DropdownField($this->getSettingName('PhoneType'), _t('EditablePhoneField.PHONETYPE', 'Phone Type'), self::get_phone_types(), $phoneType));
    37         }
    38
    39         return $fields;
    40     }
    41
    42     function getFormField() {
    43         $fields = new PhoneField($this->Name, $this->Title);
    44         if($this->Mask) {
    45             $fields->setHTML5Attribute('data-mask', $this->Mask);
    46             $fields->setMaxLength(100);
    47         }
    48         if($this->Pattern) {
    49             $fields->setFieldPattern($this->Pattern);
    50         }
    51         if($this->getSetting('MinLength')) {
    52             $fields->setHTML5Attribute('min', $this->getSetting('MinLength'));
    53         }
    54         if($this->getSetting('MaxLength')) {
    55             $fields->setHTML5Attribute('max', $this->getSetting('MaxLength'));
    56         }
    57
    58         // ??? указывать ???
    59         //$fields->setHTML5Attribute('fixed', $fixed);
    60
    61         return $fields;
    62     }
    63
    64     function getMask() {
    65         $phoneType = ($this->getSetting('PhoneType')) ? $this->getSetting('PhoneType') : '';
    66         if ($phoneType) {
    67             return _t("EditablePhoneField.PhoneTypeMask_{$phoneType}");
    68         }
    69         return false;
    70     }
    71
    72     // паттерн совпадает с маской
    73     function getPattern() {
    74         if ($this->Mask) {
    75             return $this->Mask;
    76         }
    77         return _t("EditablePhoneField.PhoneTypeMask_DEFAULT");
    78     }
    79
    80     public function getValueFromData($data) {
    81         return (isset($data[$this->Name])) ? PhoneField::cleanup_phone($data[$this->Name]) : '';
    82     }
    83 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -