Ejemplo n.º 1
0
function select_datetime($path, $options = array(), $html_options = array())
{
    $name = _get_complete_bind_path($path);
    $bindStatus =& new BindStatus(_get_bind_path($path));
    $boundValue = $bindStatus->getDisplayValue();
    return select_datetime_tag($name, $boundValue, $options, $html_options);
}
Ejemplo n.º 2
0
/**
 * Returns an XHTML compliant <input> tag to be used as a free-text date field.
 *
 * You can easily implement a JavaScript calendar by enabling the 'rich' option in the
 * <i>$options</i> parameter.  This includes a button next to the field that when clicked,
 * will open an inline JavaScript calendar.  When a date is selected, it will automatically
 * populate the <input> tag with the proper date, formatted to the user's culture setting.
 * Symfony also conveniently offers the input_date_range_tag, that allows you to specify a to
 * and from date.
 *
 * <b>Options:</b>
 * - rich - If set to true, includes an inline JavaScript calendar can auto-populate the date field with the chosen date
 *
 * <b>Examples:</b>
 * <code>
 *  echo input_date_tag('date', null, array('rich' => true));
 * </code>
 *
 * @param  string field name
 * @param  string date
 * @param  array  additional HTML compliant <input> tag parameters
 * @return string XHTML compliant <input> tag with optional JS calendar integration
 * @see input_date_range_tag
 */
function input_date_tag($name, $value = null, $options = array())
{
    $options = _parse_attributes($options);
    $context = sfContext::getInstance();
    $culture = _get_option($options, 'culture', $context->getUser()->getCulture());
    $withTime = _get_option($options, 'withtime', false);
    // rich control?
    if (!_get_option($options, 'rich', false)) {
        use_helper('DateForm');
        // set culture for month tag
        $options['culture'] = $culture;
        if ($withTime) {
            return select_datetime_tag($name, $value, $options, isset($options['html']) ? $options['html'] : array());
        } else {
            return select_date_tag($name, $value, $options, isset($options['html']) ? $options['html'] : array());
        }
    }
    $pattern = _get_option($options, 'format', $withTime ? 'g' : 'd');
    $dateFormat = new sfDateFormat($culture);
    $pattern = $dateFormat->getInputPattern($pattern);
    // parse date
    if ($value === null || $value === '') {
        $value = '';
    } else {
        $value = $dateFormat->format($value, $pattern);
    }
    // register our javascripts and stylesheets
    $langFile = sfConfig::get('sf_calendar_web_dir') . '/lang/calendar-' . strtolower(substr($culture, 0, 2));
    $jss = array(sfConfig::get('sf_calendar_web_dir') . '/calendar', is_readable(sfConfig::get('sf_symfony_data_dir') . '/web/' . $langFile . '.js') || is_readable(sfConfig::get('sf_web_dir') . '/' . $langFile . '.js') ? $langFile : sfConfig::get('sf_calendar_web_dir') . '/lang/calendar-en', sfConfig::get('sf_calendar_web_dir') . '/calendar-setup');
    foreach ($jss as $js) {
        $context->getResponse()->addJavascript($js);
    }
    // css
    if ($calendar_style = _get_option($options, 'css', 'skins/depsos/theme')) {
        $context->getResponse()->addStylesheet(sfConfig::get('sf_calendar_web_dir') . '/' . $calendar_style);
    }
    // date format
    $date_format = $dateFormat->getPattern($pattern);
    // calendar date format
    $calendar_date_format = $date_format;
    $calendar_date_format = strtr($date_format, array('yyyy' => 'Y', 'yy' => 'y', 'MM' => 'm', 'M' => 'm', 'dd' => 'd', 'd' => 'e', 'HH' => 'H', 'H' => 'k', 'hh' => 'I', 'h' => 'l', 'mm' => 'M', 'ss' => 'S', 'a' => 'p'));
    $calendar_date_format = preg_replace('/([mdyhklspe])+/i', '%\\1', $calendar_date_format);
    $id_inputField = isset($options['id']) ? $options['id'] : get_id_from_name($name);
    $id_calendarButton = 'trigger_' . $id_inputField;
    $js = '
    document.getElementById("' . $id_calendarButton . '").disabled = false;
    Calendar.setup({
      inputField : "' . $id_inputField . '",
      ifFormat : "' . $calendar_date_format . '",
      daFormat : "' . $calendar_date_format . '",
      button : "' . $id_calendarButton . '"';
    if ($withTime) {
        $js .= ",\n showsTime : true";
    }
    // calendar options
    if ($calendar_options = _get_option($options, 'calendar_options')) {
        $js .= ",\n" . $calendar_options;
    }
    $js .= '
    });
  ';
    // calendar button
    $calendar_button = '...';
    $calendar_button_type = 'txt';
    if ($calendar_button_img = _get_option($options, 'calendar_button_img')) {
        $calendar_button = $calendar_button_img;
        $calendar_button_type = 'img';
    } else {
        if ($calendar_button_txt = _get_option($options, 'calendar_button_txt')) {
            $calendar_button = $calendar_button_txt;
            $calendar_button_type = 'txt';
        }
    }
    // construct html
    if (!isset($options['size'])) {
        // educated guess about the size
        $options['size'] = strlen($date_format) + 2;
    }
    $html = input_tag($name, $value, $options);
    if ($calendar_button_type == 'img') {
        $html .= image_tag($calendar_button, array('id' => $id_calendarButton, 'style' => 'cursor: pointer; vertical-align: middle'));
    } else {
        $html .= content_tag('button', $calendar_button, array('type' => 'button', 'disabled' => 'disabled', 'onclick' => 'return false', 'id' => $id_calendarButton));
    }
    if (_get_option($options, 'with_format')) {
        $html .= '(' . $date_format . ')';
    }
    // add javascript
    $html .= content_tag('script', $js, array('type' => 'text/javascript'));
    return $html;
}
Ejemplo n.º 3
0
function object_datetime_tag($document, $fieldname)
{
    $date = $document->get('date_time');
    if ($date == null) {
        $date = '';
    }
    $out = start_group_tag();
    $out .= label_tag($fieldname, '');
    $out .= form_error($fieldname) . ' <div style="display:inline">' . select_datetime_tag('date_time', $date, array('include_second' => true, 'include_blank' => true, 'year_start' => date('Y'), 'year_end' => sfConfig::get('app_date_year_min'))) . '</div>';
    $out .= end_group_tag();
    return $out;
}