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.9 2018/04/11 21:35:07 hectorr 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 {$anchor ne ""} { 
    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 {$object_type eq "user"} {
    ad_returnredirect [acs_community_member_url -user_id $object_id]
    ad_script_abort
}


if {$object_type eq "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 {$object_type eq "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 {$page_url eq ""} {
	set page_url [acs_sc::invoke -contract FtsContentProvider -operation url -impl $object_type -call_args [list $object_id]]
}

if {$page_url ne ""} {
    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
        its real url failed.</p>
        <p>We are sorry for this inconvenience.</p>
    }]
}

# Local variables:
#    mode: tcl
#    tcl-indent-level: 4
#    indent-tabs-mode: nil
# End: