Version: current

     1 <?php
     2
     3 class DatePickerField extends DateField
     4 {
     5
     6     static $dateFormat = "dd/mm/yy";
     7
     8     static function HTMLField( $id, $name, $val ) {
     9         return <<<HTML
    10             <input type="text" readonly="readonly" id="$id" name="$name" value="$val" /> (<a href="javascript:void(0)" rel="clear-btn">clear</a>)
    11 HTML;
    12     }
    13
    14     public static function set_date_format($format)
    15     {
    16         switch($format) {
    17             case "mdy":
    18             self::$dateFormat = "mm/dd/yy";
    19             break;
    20
    21             case "dmy":
    22             self::$dateFormat = "dd/mm/yy";
    23             break;
    24
    25             default:
    26             self::$dateFormat = "dd/mm/yy";
    27             break;
    28         }
    29     }
    30
    31     public static function dmy()
    32     {
    33         return self::$dateFormat == "dd/mm/yy";
    34     }
    35
    36     public static function mdy()
    37     {
    38         return self::$dateFormat == "mm/dd/yy";
    39     }
    40
    41     function setValue($val) {
    42         if(is_string($val) && preg_match('/^([\d]{2,4})-([\d]{1,2})-([\d]{1,2})/', $val)) {
    43             $this->value = self::mdy() ?
    44                 preg_replace('/^([\d]{2,4})-([\d]{1,2})-([\d]{1,2})/','\\2/\\3/\\1', $val) :
    45                 preg_replace('/^([\d]{2,4})-([\d]{1,2})-([\d]{1,2})/','\\3/\\2/\\1', $val);
    46         } else {
    47             $this->value = $val;
    48         }
    49     }
    50
    51     function dataValue() {
    52         if(is_array($this->value)) {
    53             if(isset($this->value['Year']) && isset($this->value['Month']) && isset($this->value['Day'])) {
    54                 return $this->value['Year'] . '-' . $this->value['Month'] . '-' . $this->value['Day'];
    55             } else {
    56                 user_error("Bad DateField value " . var_export($this->value,true), E_USER_WARNING);
    57             }
    58         } elseif(preg_match('/^([\d]{1,2})\/([\d]{1,2})\/([\d]{2,4})/', $this->value, $parts)) {
    59             return self::mdy() ? "$parts[3]-$parts[1]-$parts[2]" : "$parts[3]-$parts[2]-$parts[1]";
    60         } elseif(!empty($this->value)) {
    61             return date('Y-m-d', strtotime($this->value));
    62         } else {
    63             return null;
    64         }
    65     }
    66
    67
    68     function Field() {
    69         Requirements::javascript(THIRDPARTY_DIR."/jquery-livequery/jquery.livequery.js");
    70         Requirements::javascript(THIRDPARTY_DIR."/jquery-metadata/jquery.metadata.js");
    71         Requirements::javascript("dataobject_manager/javascript/dom_jquery_ui.js");
    72         Requirements::javascript("dataobject_manager/code/date_picker_field/datepicker_init.js");
    73         Requirements::css("dataobject_manager/css/ui/dom_jquery_ui.css");
    74         $id = $this->id();
    75         $val = $this->attrValue();
    76         $field = parent::Field();
    77
    78         $innerHTML = self::HTMLField( $id, $this->name, $val );
    79
    80         return "
    81                     <div class=\"datepicker field {dateFormat : '".self::$dateFormat."'}\">
    82                         $innerHTML
    83                     </div>
    84         ";
    85         }
    86
    87 }
    88
    89 class DatePickerField_Controller extends Controller
    90 {
    91     function dateformat()
    92     {
    93       echo DatePickerField::$dateFormat;
    94     }
    95 }
    96
    97 ?>

[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. -