<?php

class MCalendarField extends MTextField
{
    public function __construct( $name='', $value='', $label='', $size=20, $hint='', $type='text', $class='m-text-field' )
    {
        parent::__construct( $name, $value, $label, $size, $hint);

        $this->type = $type;
        $this->setClass($class);

        // dijit.form.DateTextBox has a standard date format (ansi) : yyyy-mm-dd
        // so, build a new Dijit Widget to force date format to 'dd/mm/yyyy' on submit

        $js = <<< HERE
dojo.declare("MDateTextBox", [dijit.form.DateTextBox], {
   serialize: function(d, options) {
     return dojo.date.locale.format(d, {selector:'date', datePattern:'dd/MM/yyyy'}).toLowerCase();
   }
});

HERE;
        $this->page->addJsCode($js);

        $this->addAttribute('dojoType', 'MDateTextBox');
        $this->addAttribute('promptMessage',"dd/mm/yyyy");
        // this attribute is valid only to "show" the date (it doesn't define the internal format)
        $this->addAttribute('constraints',"{datePattern:'dd/MM/yyyy'}");

        $this->page->addDojoRequire("dijit.form.DateTextBox");
//        $this->page->onload("console.log(dojo.byId('{$name}'));");
        $this->page->onload("miolo.parse('{$name}');");
//        $this->page->onload("dojo.byId('{$name}').value = '';");

        $this->setValidator( new MDATEDMYValidator( $name, $label ) );
    }


    public function generateInner()
    {
        // dijit.form.DateTextBox has a standard date format (ansi) : yyyy-mm-dd
        // convert Miolo internal date format (dd/mm/yyyy) to Dojo date format (yyyy-mm-dd)
        $k = new MKrono();
        if ($this->value != '')
        {
            $this->setValue($k->KDate('%Y-%m-%d', $k->dateToTimestamp($this->value)));
        }

        parent::generateInner();

        if ( ! $this->readonly )
        {
            $text = $this->getRender('inputtext');
            $this->inner = $this->generateLabel() . $text;
        }

    }
}

?>