ad_page_contract {
    This page accepts an object_id and uses the FtsContentProvider url
    method for the content type of that item to redirect to the page
    to display the object.

    @author Jeff Davis davis@xarg.net
    @creation-date 2003-12-29
    @cvs-id $Id: o.vuh,v 1.6 2010/10/22 14:31:14 raulm Exp $
} {}

if {![regexp {^/([0-9]+)(-(.+))?$} [ad_conn path_info] match object_id dummy anchor]} {
    ad_return_warning "Invalid object id" [subst {
        The identifier given for this object is invalid.  Please check your url 
        or contact the webmaster if you think it should work.
    }]
    return
}

if {![empty_string_p $anchor]} { 
    set anchor "#$anchor"
}

if {![ db_0or1row object_data {
    select o.object_type, o.title, o.package_id
    from acs_objects o
    where o.object_id = :object_id
}]} { 
    ns_returnnotfound
    return
}

# Users are handled as a special case since we want to preserve the subsite
# we are loading from...
if {[string eq $object_type user]} {
    ad_returnredirect [acs_community_member_url -user_id $object_id]
    ad_script_abort
}


if {[string eq $object_type content_item]} {
    db_0or1row object_data {
        select o.object_type, o.object_id, o.package_id
        from acs_objects o, cr_items i
        where i.item_id = :object_id
          and o.object_id = coalesce(i.live_revision, i.latest_revision, i.item_id)
    }
    if {[string eq $object_type content_item]} {
        # punt, assume file storage extlink for now
        # JCDXXX TODO: figure out what to do...
        ad_returnredirect [apm_package_url_from_id $package_id]simple?object_id=$object_id&todo=fixthis
    }

}

if {![info exists object_type]} {
    ad_return_warning "Invalid object ID" [subst {
        The object ID $object_id doesn't exist.
        <p>We are sorry for this inconvenience.
    }]
    return
}

# Try to retrieve the page url using a callback that makes use of upvar

set page_url [lindex [callback -catch -impl $object_type subsite::url -object_id $object_id -package_id $package_id -type "display"] 0]

# If this did not work, try again with the Service contract
if {[empty_string_p $page_url]} {
	set page_url [acs_sc::invoke -contract FtsContentProvider -operation url -impl $object_type -call_args [list $object_id]]
}

if {![empty_string_p $page_url]} {
    ad_returnredirect $page_url$anchor
} else {
    ad_return_warning "Invalid object ID" [subst {
        <p>The object ID $object_id is a $object_type but getting 
        it's real url failed.</p>
        <p>We are sorry for this inconvenience.</p>
    }]
}
