![]() |
|
Snippets |
|
select_date_tag is not available in 'rich' version. Waiting for a real enhancement, this is my workaround.
This is the RichDateHelper.php file:
<?php use_helper('Form'); function rich_select_date_tag($name, $value = null, $options = array(), $html_options = array()) { $context = sfContext::getInstance(); if (isset($options['culture'])) { $culture = $options['culture']; unset($options['culture']); } else { $culture = $context->getUser()->getCulture(); } // register our javascripts and stylesheets $langFile = '/sf/js/calendar/lang/calendar-'.strtolower(substr($culture, 0, 2)); $jss = array( '/sf/js/calendar/calendar', is_readable(sfConfig::get('sf_symfony_data_dir').'/web/'.$langFile.'.js') ? $langFile : '/sf/js/calendar/lang/calendar-en', '/sf/js/calendar/calendar-setup', ); foreach ($jss as $js) { $context->getResponse()->addJavascript($js); } $js = ' function updateSelect(cal) { var date = cal.date; var selectMonth = document.getElementById("'.get_id_from_name($name).'_month"); selectMonth.selectedIndex = date.getMonth(); var selectDay = document.getElementById("'.get_id_from_name($name).'_day"); selectDay.selectedIndex = (date.getDate() - 1); var selectYear = document.getElementById("'.get_id_from_name($name).'_year"); selectYear.selectedIndex = (date.getFullYear() - '.$options['year_start'].'); } document.getElementById("trigger_'.$name.'").disabled = false; Calendar.setup({ inputField : "'.$name.'_rich_sel_date", ifFormat : "%Y-%m-%d", button : "trigger_'.$name.'", singleClick : true, onUpdate : updateSelect, showsTime : false, range : ['.$options['year_start'].', '.$options['year_end'].'], showOthers : false, cache : 1, weekNumbers : false, firstDay : 1 }); '; $html = select_date_tag($name, $value, $options, $html_options); // calendar button $calendar_button = '...'; $calendar_button_type = 'txt'; if (isset($options['calendar_button_img'])) { $calendar_button = $options['calendar_button_img']; $calendar_button_type = 'img'; unset($options['calendar_button_img']); } else if (isset($options['calendar_button_txt'])) { $calendar_button = $options['calendar_button_txt']; $calendar_button_type = 'txt'; unset($options['calendar_button_txt']); } if ($calendar_button_type == 'img') { $html .= image_tag($calendar_button, array('id' => 'trigger_'.$name, 'style' => 'cursor: pointer')); } else { $html .= content_tag('button', $calendar_button, array('type' => 'button', 'disabled' => 'disabled', 'onclick' => 'return false', 'id' => 'trigger_'.$name)); } // add javascript $html .= content_tag('script', $js, array('type' => 'text/javascript')); $html .= '<div id="'.$name.'_rich_sel_date" style="display: inline;"></div>'; return $html; }
Comments on this snippet
What exactly is the benefit over
In our reservation systems we found - by statistics - that many people don't use the Javascript pop-up calendar (we don't know yet whether this happens by distraction or even by preference). This led many users to insert wrongly formatted dates and in the "long" run to fly away from the site. (Yes, we clearly stated the right date format to be inserted right next the text field, but people is even like that, don't ask me why. Had I the answer I'd probably save the world :) )
So we opted to keep the best of two worlds: SELECT tags and JS pop-up calendar. The SELECT forces user to input the right data while smart nerds like us can use the JS calendar. That's my best short explanation about the reason-why of this snippet. Hope it's clear, ask for more if it was not. Ciao!