template::list::prepare (public)

 template::list::prepare -name name [ -ulevel ulevel ]

Defined in packages/acs-templating/tcl/list-procs.tcl

Prepare list for rendering

Switches:
-name
(required)
-ulevel
(defaults to "1") (optional)

Partial Call Graph (max 5 caller/called nodes):
%3 template::list::create template::list::create (public) template::list::prepare template::list::prepare template::list::create->template::list::prepare ad_conn ad_conn (public) template::list::prepare->ad_conn ad_page_contract_handle_datasource_error ad_page_contract_handle_datasource_error (public) template::list::prepare->ad_page_contract_handle_datasource_error ad_script_abort ad_script_abort (public) template::list::prepare->ad_script_abort ad_try ad_try (public) template::list::prepare->ad_try template::list::get_reference template::list::get_reference (public) template::list::prepare->template::list::get_reference

Testcases:
No testcase defined.
Source code:
    # Get an upvar'd reference to list_properties
    get_reference -name $name

    # Default the display_elements property to be all elements
    if { [llength $list_properties(display_elements)] == 0 } {
        set list_properties(display_elements) $list_properties(elements)
    }

    # Set the bulk_action_export_chunk
    if { $list_properties(bulk_action_export_vars) ne "" } {
        set list_properties(bulk_action_export_chunk) [uplevel $list_properties(ulevel)  [list export_vars -form $list_properties(bulk_action_export_vars)]]
    }

    # This sets urls, selected_p, etc., for filters, plus sets the (filter,name) var in list_properties
    upvar filter_form filter_form
    if {$filter_form} {
        prepare_filter_form -name $name
    }

    prepare_filters  -name $name

    # Split the current ordering info into name and direction
    # name is the string before the comma, order (asc/desc) is what's after
    if { [info exists list_properties(filter,$list_properties(orderby_name))] } {
        lassign [lrange [split $list_properties(filter,$list_properties(orderby_name)","] 0 1] orderby_name orderby_direction

        set list_properties(orderby_selected_name) $orderby_name
        if { $orderby_direction eq "" } {

            ad_try {
                template::list::orderby::get_reference  -list_name $name  -orderby_name $orderby_name
            } on error {errorMsg} {
                ad_page_contract_handle_datasource_error $errorMsg
                ad_script_abort
            }

            set orderby_direction $orderby_properties(default_direction)
        }
        set list_properties(orderby_selected_direction) $orderby_direction
    }

    # This sets orderby, etc., for filters
    prepare_elements  -name $name  -ulevel [expr {$ulevel + 1}]

    # Make group_by information available to templates
    if { [info exists list_properties(filter,groupby)] && $list_properties(filter,groupby) ne "" } {
        set list_properties(groupby) $list_properties(filter,groupby)
    }
    if { [info exists list_properties(filter_label,groupby)] && $list_properties(filter_label,groupby) ne "" } {
        set list_properties(groupby_label) $list_properties(filter_label,groupby)
    }

    # Create the paginator
    if { $list_properties(page_size_variable_p) == 1 } {
        set list_properties(page_size) $list_properties(filter,page_size)
        set list_properties(url) [ad_conn url]
        set list_properties(page_size_export_chunk)  [uplevel $list_properties(ulevel) [list export_vars -form -exclude {page_size page} $list_properties(filters_export)]]
    }

    if { $list_properties(page_size) ne "" && $list_properties(page_size) != 0 } {

        if {$list_properties(page_query) eq ""} {
            # We need to uplevel db_map it to get the query from the right context
            set list_properties(page_query_substed)  [uplevel $list_properties(ulevel) [list db_map $list_properties(page_query_name)]]
        } else {
            # We need to uplevel subst it so we get the filters evaluated
            set list_properties(page_query_substed)  [uplevel $list_properties(ulevel)  [list subst -nobackslashes $list_properties(page_query)]]
        }

        # Use some short variable names to make the expr readable
        set page $list_properties(filter,page)
        set groupsize   $list_properties(page_groupsize)
        set page_size   $list_properties(page_size)
        set page_group  [expr {($page - 1 - (($page - 1) % $groupsize)) / $groupsize + 1}]
        set first_row   [expr {($page_group - 1) * $groupsize * $page_size + 1}]
        set last_row    [expr {$first_row + ($groupsize + 1) * $page_size - 1}]
        set page_offset [expr {($page_group - 1) * $groupsize}]

        # Antonio Pisano 2015-11-17: From now on, the original query
        # will be tampered with the limit information, so this is our
        # last chance to save it and use it to get the full row count in
        # the paginator.
        set list_properties(page_query_original) $list_properties(page_query_substed)

        # Now wrap the provided query with the limit information
        set list_properties(page_query_substed) [subst {
            $list_properties(page_query_substed) offset [expr {$first_row - 1}]
            fetch first [expr {$last_row - $first_row + 1}] rows only
        }]

        # Generate a paginator name which includes the page group we're in
        # and all the filter values, so the paginator caching works properly
        # Antonio Pisano 2015-11-17: it is important that the paginator_name starts with the list's
        # name, because we count on this in template::paginator::create to retrieve the count_query
        set paginator_name $list_properties(name)

        foreach filter $list_properties(filters) {
            if { $filter ne "page" && [info exists list_properties(filter,$filter)] } {
                append paginator_name ",$filter=$list_properties(filter,$filter)"
            }
        }

        append paginator_name ",page_group=$page_group"
        set list_properties(paginator_name) $paginator_name

        set flush_p f
        if { [string is true -strict $list_properties(page_flush_p)] } {
            set flush_p t
        }

        # We need this uplevel so that the bind variables in the query
        # will get bound at the caller's level
        # we pass in a dummy query name because the query text was
        # already retrieved previously with db_map so this call
        # always passes the full query text and not the query name
        # this was failing if the template::list call contained a
        # page_query with an empty page_query_name
        uplevel $ulevel [list template::paginator create  templating-paginator-$name  $list_properties(paginator_name)  $list_properties(page_query_substed)  -pagesize $list_properties(page_size)  -groupsize $list_properties(page_groupsize)  -page_offset $page_offset  -flush_p $flush_p  -contextual]

        if { $list_properties(filter,page) > [template::paginator get_page_count $list_properties(paginator_name)] } {
            set list_properties(filter,page) [template::paginator get_page_count $list_properties(paginator_name)]
        }

    }
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: