Forum OpenACS Q&A: ad_form problem with multiple dates

Collapse
Posted by Jade Rubick on
Dave showed me a neat trick for creating multiple items on a single ad_form page, using arrays. I can get everything to work except for the dates, for some reason. I bet the solution to this is pretty simple, but I've been working on it for more than an hour, and I seem to be having no luck.

The idea here is to create multiple Tasks in one ad_form page.

If I leave the end_date out, it works fine, but when I try to reference end_date($i) inside the ns_log statement, it returns this error:

can't read "end_date(1)": no such element in array
    while executing
"ns_log Notice "end_date: $end_date($i)""
    invoked from within
"if {[info exists start_date]} {
    for {set i 1} {$i <= $number} {incr i} {
        ns_log Notice "end_date: $end_date($i)"
    }
}"

Is there any reason dates are different than other items?

I've tried a lot of things, but none of them worked.

Here is the simplified .adp page:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<table>
<formtemplate id="add_edit" style="standard-lars">
<multiple name="num">

<tr>

<td>@num.rownum@</td>

<td><formwidget id="mailto:task_title.@num.rownum@">
<td><formwidget id="mailto:end_date.@num.rownum@"></td>

</multiple>

<tr><td colspan="99" align="center"><input type="submit" name="formbutton:ok" value = "      OK      "></td>

</formtemplate>
</table>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here is the simplified .tcl page:

ad_page_contract {

    Add/edit form for tasks

    @author mailto:jader@bread.com
    @creation-date 2003-07-28
    @cvs-id $Id: task-add-edit.tcl,v 1.4 2003/08/01 20:51:04 jader Exp $

    @return num num is used as a multirow datasource to iterate over the ad_form elements

    @param number The number of Tasks to create
    @param task_id Specifies the item for the task (every revision of a task shares the same task_id)
} {

    {number:integer "1"}

    task_id:multiple,integer,optional
    task_title:array,optional
    end_date:array,optional

} -properties {

    num:multirow

}

template::multirow create num number

for {set i 1} {$i <= $number} {incr i} {
    template::multirow append num $i
}

if {[info exists start_date]} {
    for {set i 1} {$i <= $number} {incr i} {
        ns_log Notice "end_date: $end_date($i)"
    }
}

# the unique identifier for this package
set package_id [ad_conn package_id]
set user_id    [ad_maybe_redirect_for_registration]

ad_form -name add_edit -form {
    task_id:key

    {number:text(hidden)
    {value $number}
    }

} -new_data {

    for {set i 1} {$i <= $number} {incr i} {
        # ns_log Notice "task_title($i) = $task_title($i)"
        db_exec_plsql new_task_item { *SQL* }
    }

} -select_query_name task_query -after_submit {

    ad_returnredirect "one?item_id=$project_item_id&project_id=$project_id"
    ad_script_abort
}

for {set i 0} {$i <= $number} {incr i} {

    # reading this code, you may wonder why we put the .$i at the end.
    # DaveB showed me this trick. It lets you make a multiple out of
    # the items by stuffing them in an array. Long live DaveB.

    append add_edit_definition "
        {task_title.$i:text
            {label \"Subject \#$i\"}
            {html {size 30}}
        }


        {end_date.$i:date
            {label \"End date\"}
            {format \"MONTH DD YYYY\"}
            {help}
        }

    "

}

ad_form -extend -name add_edit -form $add_edit_definition -select_query_name task_query

Collapse
Posted by Jon Griffin on
The quick answer is yes dates are different. Why it doesn't work I would guess it is because dates are returned as fragments (along with time).

That is why the sql conversions are necessary.

Collapse
Posted by Jade Rubick on
Has anybody used ad_form with multiple dates of the same name? What did they do?

I tried modifying the date definitions to look like what Jon has on his ad_form webpage, but to no avail:

        {start_date.$i:date,to_sql(linear_date),from_sql(sql_date)
            {label \"Start date\"}
            {format \"MONTH DD YYYY\"}
            {help}
        }

        {end_date.$i:date,to_sql(linear_date),from_sql(sql_date)
            {label \"End date\"}
            {format \"MONTH DD YYYY\"}
            {help}
        }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here is the relevant portion of the .xql file:

<?xml version="1.0"?>
<queryset>
  <fullquery name="new_task_item">
    <querytext>
    select pm_task__new_task_item (
                null,
        :project_item_id,
                '$task_title($i)',
                '$description($i)',
        to_timestamp('$start_date($i)','YYYY MM DD HH24 MI SS'),
        to_timestamp('$end_date($i)','YYYY MM DD HH24 MI SS'),
                '0',
        now(),
        :user_id,
        :peeraddr,
        :package_id
    );
    </querytext>
  </fullquery>

...

Same error as before.

Collapse
Posted by Jon Griffin on
I use multiple dates in several packages, so I know it works.
Collapse
Posted by Brad Duell on
I've used multiple dates as well.

The only problem I've noted with the date widgets is that Month and Day values for the selects didn't work (I would get 0-9 for each, and empty values for greater ranges).

See https://openacs.org/bugtracker/openacs/com/acs-templating/patch?patch_number=251

Collapse
Posted by Jade Rubick on
I'm working on this again, and again running into difficulty. Does anybody have any publicly accessible code that contains multiple dates? Where?

For example, a form that takes three date values, each called deadline?

I'm very curious how anybody has done this.

Thanks in advance. I'll add this to my ad_form notes page, so you'll never have to answer this question again, hopefully.

Collapse
Posted by Jade Rubick on
I've hacked my way around this problem. I'd welcome a more elegant solution.

My ad_form notes page documents how to get around multiple dates in ad_form.

As always, comments are welcome (except in this case, comments saying my hack is a crude hack are not welcome! I already know that!)