Actions

Source Code of This Project

/lib/widget/myWidgetFormDateRangeJQueryUI.class.php

<?php
/*
 * This file is part of the pwp package.
 * (c) 2009-2010 Victor Rad' <victor.v.rad[at]gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
*/

/**
 * Widget of Date Range for HTML Forms base on JQueryUI
 *
 * @package    form
 * @author     Victor Rad'
 */
class myWidgetFormDateRangeJQueryUI extends sfWidgetForm
{
    /**
     * Configures the current widget.
     *
     * Available options:
     *
     * @param string   separator          The delimiter of dates
     * @param string   earliestDate       Earliest date allowed in range
     * @param string   latestDate         Latest date allowed in range
     *
     * @see sfWidgetForm
     */
    protected function configure($options = array(), $attributes = array())
    {
        $this->addOption('separator', '-');
        $this->addOption('earliestDate',  null);
        $this->addOption('latestDate',  null);

        parent::configure($options, $attributes);
    }

    /**
     * @param  string $name        The element name
     * @param  string $value       The date 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())
    {
        $attributes = $this->getAttributes();
        $value = (!empty($attributes['value']) ? $attributes['value'] : $value);

        $input = new sfWidgetFormInput(array(), $attributes);
        $html = $input->render($name, $value);
        $id = $input->generateId($name);

        $html .= <<<EOHTML
      <script type="text/javascript">
        $(function() {
          var params = {
            presetRanges: [
              {text: 'Today', dateStart: 'Today', dateEnd: 'Today' },
              {text: 'Last 3 days', dateStart: 'Last 3 days', dateEnd: 'Today' },
              {text: 'Last 7 days', dateStart: 'Last 7 days', dateEnd: 'Today' },
              {text: 'Last month', dateStart: 'Last month', dateEnd: 'Today' }
            ],
//            presets: {allDatesBefore: 'Before', allDatesAfter: 'After', dateRange: 'Range'},
            presets: {dateRange: 'Range'},
            posX: $('#$id').offset().left - 6,
            posY: $('#$id').offset().top - 160,
            rangeSplitter: '{$this->getOption('separator')}',
            dateFormat: 'dd.mm.yy',
            earliestDate: '{$this->getOption('earliestDate')}',
            latestDate: '{$this->getOption('latestDate')}'
            };
          $("#$id").daterangepicker(params);
        });
      </script>
EOHTML;

        return $html;
    }

    /**
     * Gets the stylesheet paths associated with the widget.
     *
     * @return array An array of stylesheet paths
     */
    public function getStylesheets()
    {
        return array('/js/jquery/ui/css/start/ui.daterangepicker.css' => 'screen');
    }

    /**
     * Gets the JavaScript paths associated with the widget.
     *
     * @return array An array of JavaScript paths
     */
    public function getJavaScripts()
    {
        return array('/js/jquery/ui/daterangepicker.jQuery.js');
    }

}