示例#1
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;
}
/**
 * Returns a variable number of <select> tags populated with date and time related select boxes.
 *
 * The select_datetime_tag is the culmination of both the select_date_tag and the select_time_tag.
 * By default, the <i>$value</i> parameter is set to the current date and time. To override this, simply pass a valid 
 * date, time, datetime string or correctly formatted array (see example) to the <i>$value</i> parameter. 
 * You can also set the <i>$value</i> parameter to null which will disable the <i>$value</i>, however this 
 * will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i> parameter. 
 * To include seconds to the result, use set the 'include_second' option in the <i>$options</i> parameter to true. 
 * <b>Note:</b> The <i>$name</i> parameter will automatically converted to array names. 
 * For example, a <i>$name</i> of "datetime" becomes:
 * <samp>
 *  <select name="datetime[month]">...</select>
 *  <select name="datetime[day]">...</select>
 *  <select name="datetime[year]">...</select>
 *  <select name="datetime[hour]">...</select>
 *  <select name="datetime[minute]">...</select>
 *  <select name="datetime[second]">...</select>
 * </samp>
 *  
 * <b>Options:</b>
 * - include_blank     - Includes a blank <option> tag at the beginning of the string with an empty value.
 * - include_custom    - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
 * - include_second    - If set to true, includes the "seconds" select tag as part of the result.
 * - discard_month     - If set to true, will only return select tags for day and year.
 * - discard_day       - If set to true, will only return select tags for month and year.
 * - discard_year      - If set to true, will only return select tags for month and day.
 * - use_month_numbers - If set to true, will show the month's numerical value (1 - 12) instead of the months full name.
 * - use_short_month   - If set to true, will show the month's short name (i.e. Jan, Feb, Mar) instead of its full name. 
 * - year_start        - If set, the range of years will begin at this four-digit date (i.e. 1979)
 * - year_end          - If set, the range of years will end at this four-digit date (i.e. 2025)
 * - second_step       - If set, the seconds will be incremented in blocks of X, where X = 'second_step'
 * - minute_step       - If set, the minutes will be incremented in blocks of X, where X = 'minute_step'
 * - 12hour_time       - If set to true, will return integers 1 through 12 instead of the default 0 through 23.
 * - date_seperator    - Includes a string of defined text between each generated select tag
 * - time_seperator    - Includes a string of defined text between each generated select tag
 * - ampm_seperator    - Includes a string of defined text between the minute/second select box and the AM/PM select box 
 *  
 * <b>Examples:</b>
 * <code>
 *  echo select_datetime_tag('datetime');
 * </code>
 *
 * <code>
 *  echo select_datetime_tag('datetime', '1979-10-30');
 * </code>
 *
 * <code>
 *  $datetime = array('year' => '1979', 'month' => 10, 'day' => 30, 'hour' => '15', 'minute' => 46);
 *  echo select_datetime_tag('time', $datetime, array('use_short_month' => true, '12hour_time' => true));
 * </code>
 *
 * @param  string field name (automatically becomes an array of date and time parts)
 * @param  mixed  accepts a valid time string or properly formatted time array
 * @param  array  additional HTML compliant <select> tag parameters
 * @return string a variable number of <select> tags populated with date and time related select boxes
 * @see select date_tag, select_time_tag
 */
function select_datetime_tag($name, $value = null, $options = array(), $html_options = array())
{
    $options = _parse_attributes($options);
    $datetime_seperator = _get_option($options, 'datetime_seperator', '');
    $date = select_date_tag($name, $value, $options, $html_options);
    $time = select_time_tag($name, $value, $options, $html_options);
    return $date . $datetime_seperator . $time;
}
示例#3
0
function select_date($path, $options = array(), $html_options = array())
{
    $name = _get_complete_bind_path($path);
    $bindStatus =& new BindStatus(_get_bind_path($path));
    $boundValue = $bindStatus->getDisplayValue();
    return select_date_tag($name, $boundValue, $options, $html_options);
}