Forum OpenACS Q&A: Response to date & time entry widget

Collapse
Posted by Jerry Asher on
Hi David,

Looking back at it, what you are seeing is three widgets: one outputs a date entry fields, another the time entry fields, and the third the duration fields. I think this should basically work. I simplified it somewhat from what is actually at the link I gave you, but haven't tested the results. It's a start at the least, but let me know if you need more.

What this doesn't show you is how to process those fields. That's left as an exercise for the reader (for now at least.) The plus side, is that should make it easier for you to modify the widgets for 24 hour time.

There's an example at the end of the code block.

# a widget to specify a date
# used in /reserve/find rs_reservation_form
proc_doc rs_date_entry_widget {
    db formdate startdate
} {
    <pre>
    Returns an html fragment for a date entry widget in a form.

    All dates must be formatted: YYYY-MM-DD.

    formdate contains the <i>default</i>, <i>selected</i> date for the form.
    startdate is the earliest date that can be entered into the form.

    </pre>
} {
    set widget ""

    # all dates are of the form yyyy-mm-dd
    # form date is date to set form to
    # start date is minimum date of form

    set formfrags  [split $formdate -]
    set formyear   [lindex $formfrags 0]
    set formmonth  [lindex $formfrags 1]
    set formday    [lindex $formfrags 2]

    set monthindex [expr [string trimleft $formmonth 0] - 1]
    set dayindex   [expr [string trimleft $formday 0] - 1]

    # determine end year by examining target date and the "end date"
    # (which may be less than start date!)

    set startyear [lindex [split $startdate -] 0]

    if {$formyear > (1 + $startyear)} {
  	set endyear $formyear
    } else {
  	set endyear [expr $startyear + 1]
    }

    set months [long_month_list]
    set days [day_list]

    set monthwidget "<select name=month>
[ad_generic_optionlist $months $months [lindex $months $monthindex]]</select>
"
    set daywidget "<select name=day>
[ad_generic_optionlist $days $days [lindex $days $dayindex]]</select>
"
    set yearwidget "<select name=year>
[ad_integer_optionlist $startyear $endyear $formyear]</select>
"

    set widget "$monthwidget$daywidget$yearwidget"

    return $widget
}

# a widget to specify a time 
# used in /reserve/find rs_reservation_form
proc_doc rs_time_entry_widget {
    db
    {minuteinterval 15}
    {formtime ""}
} {
  <pre>
    Returns an html fragment for a time entry widget

    The minuteinterval represents the mininum number of minutes resolved
    within the widget.

    formtime contains the <i>default</i>, <i>selected</i> time for the form.
    The time must be of the format: HH.MI.am where HH is a number from 1 to 12,
    MI ranges from 0 to 59, and am can be either "am" or "pm".

   </pre>
} {

    if {[string equal "" $formtime]} {
	set formtime [clock format [clock seconds] -format "%I.%M.%p"]
	set timelist [split $formtime .]
    } else {
	set timelist [split $formtime .]
    }

    set hour   [lindex $timelist 0]
    set minute [lindex $timelist 1]
    set ampm   [lindex $timelist 2]

    if {[string equal -nocase "am" $ampm]} {
	set amselect selected
	set pmselect ""
    } else {
	set amselect ""
	set pmselect selected
    }

    set hourwidget "
        <select name=hour>
        [ad_integer_optionlist 1 12 $hour]
        </select>
    "

    if {$minuteinterval < 60} {

        set minutelist ""
        for {set minutes 0} {$minutes < 60} {incr minutes $minuteinterval} {
            if { $minutes >= 0 && $minutes < 10 } {
                set value "0$minutes"
            } else {
                set value $minutes
            }
            lappend minutelist $value
        }
        
        set minutewidget "
            <select name=minutes>
            [ad_generic_optionlist $minutelist $minutelist $minute]
            </select>
        "

    } else {
        set minutewidget ""
    }

    set ampmwidget "
<select name=ampm>
<OPTION $amselect> AM
<OPTION $pmselect> PM
</SELECT>
"
    set widget "$hourwidget<b>:</b>$minutewidget$ampmwidget"
    return $widget
}

# a widget to specify a time duration
# used in /reserve/find rs_reservation_form
proc_doc rs_dayhourminduration_entry_widget {
    timeperiod
    {formduration ""}
    {minuteinterval 15}
} {

<pre>
Returns an html fragment allowing the user to entry a duration in
terms of days, hours, and minutes.

Timeperiod is one of {day day/hour hour hour/minute} and determines 
the fields the widget displays.

The minuteinterval represents the mininum number of minutes resolved
within the widget.
</pre>
} {

    
    if {![string equal "" $formduration]} {
        set duration [split $formduration .]
    } else {
        set duration ""
    }

    set days 31

    set showminutes 1
    set showhours 1
    set showdays 1

    set mindays 0
    set minhours 0
    set defaultdays 0
    set defaulthours 0
    set defaultmins $minuteinterval

    switch -exact -- $timeperiod {
        day { 
            set showhours 0
            set showminutes 0
            set mindays 1
            set defaultdays 1
        }
        day/hour { 
            set showminutes 0
            set minhours 0
            set defaulthours 1
        }
        hour { 
            set showdays 0
            set showminutes 0
            set minhours 1
            set defaulthours 1
        }
        hour/minute { 
            set showdays 0
        }

    }

    set temp [lindex $duration 0]
    if {![string equal "" $temp]} {
        set defaultdays $temp
    }

    set temp [lindex $duration 1]
    if {![string equal "" $temp]} {
        set defaulthours $temp
    }

    set temp [lindex $duration 2]
    if {![string equal "" $temp]} {
        set defaultmins $temp
    }

    set widget ""
    set daywidget ""
    set hourwidget ""
    set minutewidget ""
    if {$showdays} {
	set daywidget "Days: <select name=numdays>
[ad_integer_optionlist $mindays $days $defaultdays]</select> 
"
    } else {
	set daywidget "<input type=hidden name=numdays value='0'>
"
    }

    if {$showhours} {
	set hourwidget "Hours: <select name=numhours>
[ad_integer_optionlist $minhours 23 $defaulthours]</select> 
"
    } else { 
	set hourwidget "<input type=hidden name=numhours value='0'>
"
    }
    
    if {$showminutes} {
	set minutelist ""
	for {set minutes 0} {$minutes < 60} {incr minutes $minuteinterval} {
	    lappend minutelist $minutes
	}
	set minutewidget "Minutes: <select name=numminutes>
[ad_generic_optionlist $minutelist $minutelist $defaultmins]</select>
"
    } else {
	set minutewidget "<input type=hidden name=numminutes value='0'>
"
    }
       set widget "$daywidget $hourwidget $minutewidget"

    return $widget

}

####
####
 EXAMPLE
####
####

<form method=get action=$url>
      <table border=0 cellspacing=0 cellpadding=10 bgcolor=lightblue>

        <tr align=left>

          <td rowspan=30>
               
          </td>

          <td >
            Date: [rs_date_entry_widget $db $formdate $startdate]
          </td>

          <td rowspan=30>
               
          </td>

        </tr>

        <tr align=left>
          <td nowrap>
            Time: [rs_time_entry_widget $db 15 $formtime]
          </td>
        </tr>

        <tr align=left>
          <td>
            [rs_dayhourminduration_entry_widget $timeperiod $formduration]
          </td>
        </tr>

        $capacityclause

        <tr>
          <td align=center>
          $hiddenvars
          <input type=hidden value='$timeperiod' name=timeperiod>
          <input type=hidden value='${resourcetypeid}:${resourcetypename}:' name=resourcedescriptor>
          <input type=submit value='          Find Available Resources          '>
          </td>
        </tr>


      </table>
</form>