| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace LeKoala\FormElements; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Forms\HiddenField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Forms\TextField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\View\Requirements; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\ORM\FieldType\DBHTMLText; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Store phone number in full international format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Have a proper db type to format phone number accordingly | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | class TelInputField extends TextField | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     const FORMAT_E164 = "E164"; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     const FORMAT_INTERNATIONAL = "INTERNATIONAL"; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     const FORMAT_NATIONAL = "NATIONAL"; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     const FORMAT_RFC3966 = "RFC3966"; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     use BaseElement; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @config | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @var array<string,mixed> | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     private static $default_config = []; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @config | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @var boolean | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     private static $enable_requirements = true; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * Format to use for storage | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     private static $defaut_dataformat = null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     protected HiddenField $hiddenField; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     public function __construct($name, $title = null, $value = '', $maxLength = null, $form = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $this->hiddenField = new HiddenField($name, $title, $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         parent::__construct($name, $title, $value, $maxLength, $form); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $this->mergeDefaultConfig(); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         if (self::config()->default_dataformat) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             $this->setDataFormat(self::config()->default_dataformat); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function getHiddenField(): HiddenField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |         return $this->hiddenField; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     public function Type() | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         return 'telinput'; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     public function extraClass() | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         return 'text ' . parent::extraClass(); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     public function getDataFormat() | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         return $this->getElementAttribute('data-dataformat'); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * The value you want when unmasking to hidden field | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @param string $dataformat The alias or "masked" to get the masked value as is | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     public function setDataFormat($dataformat) | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         return $this->setElementAttribute('data-dataformat', $dataformat); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     public function setTitle($title) | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         $this->hiddenField->setTitle($title); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         return parent::setTitle($title); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     public function setName($name) | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         $this->hiddenField->setName($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         return parent::setName($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     public function setValue($value, $data = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         $this->hiddenField->setValue($value, $data); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         return parent::setValue($value, $data); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     protected function createHiddenInput($properties = []) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $html = $this->hiddenField->forTemplate(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  |         return $html; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                        
                                                                
            
                                    
            
            
                | 113 |  |  |      * @param array<string,mixed> $properties | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      * @return DBHTMLText|string | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     public function Field($properties = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         $extraHtml = $this->createHiddenInput($properties); | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         return $this->wrapInElement('tel-input', $properties, $extraHtml); | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public static function requirements(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  |         Requirements::javascript("lekoala/silverstripe-form-elements: client/custom-elements/tel-input.min.js"); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 126 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 127 |  |  |  |