phpzend-frameworkzend-formzend-date

populating hours/dates into a Zend_Form_Element_Select


I'm looking for Zend_Form elements which will allow me to integrate Zend_Date values easily.

For example, I need a hour picker and a week day picker in my forms. for example, the hour picker should display all the hour values from 12:00 AM to 11:00 PM, and the week day picker should display all the available week days.

I was looking for some kind of integration between Zend_Form_Element_Select and Zend_Date, but I couldn't find any ready to use elements. I would like to use zend_date, so the localization settings I'm already using will be used here.

Ideally, this should be a zend form element that I can set the type of value i need, something like:

    $hour = new Form_Element_DateSelect('hour');
    $hour->setDateFormat(Zend_Date:HOUR);

or

    $weekday = new Form_Element_DateSelect('hour');
    $hour->setDateFormat(Zend_Date:DAYOFWEEK);

I can accomplish it using standard Zend_Form_Element_Select and pre filling the values, however I'm curious if someone made elements for these cases or have an idea to make these elements reusable, and to support multiple date formats.

    $hour = new Zend_Form_Element_Select('hour');
    $hour->setRequired(true)
    ->setLabel('Hour:')
    $this->addElement($hour);

    $time = new Zend_Date();
    $time->setTime('00:00:00');
    for ($i=0;$i<=23;$i++) {
        $hour->addMultiOption($time->get('HH:mm'), $time->toString(Zend_Date::TIME_SHORT));
        $time->addHour(1);
    }

Solution

  • I'd take a look at Zend_Dojo as this may have some elements close to what you are looking for.

    In particular, dijit.form.TimeTextBox, possibly dijit.Calendar as you can disable weekend dates from being selected, or dijit.form.select which is an extended select box which you could put the weekdays in. Through Zend_Form and Zend_Translate, the weekday names could easily be translated to the language of the user.

    I'm sure plenty of jQuery widgets for doing the same thing would be available. If you went that route, you'd have to do a little bit more work to get it as tightly coupled with Zend_Form as you may want, but you could make your own decorators and elements as well.

    The Zend Framework reference guide has some basic examples on the Dojo form elements TimeTextBox, DateTextBox, and Combo/Select Boxes.

    Maybe using these rich UI elements are even overkill for what you want, if that's the case, a quick way to do what you want with pre-populating elements would be to make helper methods to return the array of values (weekdays or times) that you can easily feed into Zend_Form_Element_Select::setMultiOptions().

    e.g.

    public function getWeekdays()
    {
        $locale = new Zend_Locale('en_US'); // or get from registry
        $days = Zend_Locale::getTranslationList('Days', $locale);
    
        return $days['format']['wide'];
    }
    
    public function getTimes($options = array())
    {
        $start     = null;  // time to start
        $end       = null;  // time to end
        $increment = 900;   // increment in seconds
        $format    = Zend_Date::TIME_SHORT;  // date/time format
    
        if (is_array($options)) {
            if (isset($options['start']) && $options['start'] instanceof Zend_Date) {
                $start = $options['start'];
            }
            if (isset($options['end']) && $options['end'] instanceof Zend_Date) {
                $end = $options['end'];
            }
            if (isset($options['increment']) && is_int($options['increment']) && (int)$options['increment'] > 0) {
                $increment = (int)$options['increment'];
            }
            if (isset($options['format']) && is_string($options['format'])) {
                $format = $options['format'];
            }
        }
    
        if ($start == null) {
            $start = new Zend_Date('00:00:00', Zend_Date::TIME_LONG);
        }
        if ($end == null) {
            $end   = new Zend_Date('23:59:00', Zend_Date::TIME_LONG);
        }
    
        $times = array();
    
        $time = new Zend_Date($start);
    
        while($time < $end) {  // TODO: check $end > $time
            $times[] = $time->toString($format);
            $time->add($increment, Zend_Date::SECOND);
        }
    
        return $times;
    }
    

    Calling them:

    $opts = array('start' => new Zend_Date('07:00:00', Zend_Date::TIME_LONG),
                  'end'   => new Zend_Date('20:00:00', Zend_Date::TIME_LONG),
                  'increment' => 3600);
    $element->setMultiOptions($form->getTimes($opts));
    
    $element2->setMultiOptions($form->getWeekdays());