<?php

class wpWidgetFormRichDate extends sfWidgetFormDate {
    /**
     *
     * @param array $options An array of options
     * @param array $attributes An array of default HTML attributes
     * @see sfWidgetForm
     */
    protected function configure($options = array(), $attributes = array()) {
        $this->setAttributes(array(
                // 'withtime' => true,
                'rich' => true,
                'calendar_button_img' => '/sf/sf_admin/images/date.png',
                'css' => 'skins/aqua/theme',
                'format' => 'dd/MM/yyyy',
                'culture' => 'fr',
                ));
        parent::configure($options, $attributes);
    }

    /**
     *
     * @param string $name The element name
     * @param string $value The value displayed in this widget
     * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
     * @param array $errors An array of errors for the field
     * @return string An HTML tag string
     * @see sfWidgetForm
     */
    public function render($name, $value = null, $attributes = array(), $errors = array()) {
        use_helper('Form');
        try {
            if (preg_match('#([0-9]+)/([0-9]+)/([0-9]+)#', $value, $tokens)) {
                $value = sprintf('%04d-%02d-%02d', $tokens[3], $tokens[2], $tokens[1]);
            }

            $result = input_date_tag($name, $value, array_merge($this->attributes, $attributes));
        }
        catch(Exception $e) {
            $result = input_date_tag($name, null, array_merge($this->attributes, $attributes));
        }
        return $result;
    }
}