ad_library {
Content Type Blog Library Procedures
@author Tom Jackson (tom@rmadilo.com)
@creation-date 19 August 2003
@cvs-id $Id: cams-procs.tcl,v 1.5 2004/06/03 17:16:35 tom Exp $
}
namespace eval ::cams {}
namespace eval ::cams::context {}
namespace eval ::cams::file {}
namespace eval ::cams::template {}
ad_proc ::cams::context::content_type {
} {
context_id:required
Procedure to choose context_id for cams_content content_type
} {
uplevel {
if {[info exists parent_id] } {
set context_id $parent_id
} else {
set context_id $package_id
}
}
}
proc ::cams::file::new {files array} {
upvar $array Array
set object_key [nsv_get qw_obj_key cams_values]
# only one file should be uploaded at a time
set attribute [lindex $files 0]
set query_var [lindex $files 1]
set tmpfile [ns_getformfile $query_var]
if {![file exists $tmpfile] || ![set filesize [ns_conn filelength $query_var]]} {
ns_log Error "::cams::file::new tmpfile missing or zero length for $attribute, $query_var"
return
}
set headers [ns_conn fileheaders $query_var]
set content_type [ns_set iget $headers "content-type"]
set content_disposition [ns_set iget $headers "content-disposition"]
set filename $Array($attribute)
set backslash "\\"
if {[set last_backslash [string last $backslash $filename]] > -1 } {
set filename [string range $filename [expr $last_backslash + 1] end]
set Array($attribute) $filename
}
set attribute_id $Array(attribute_id)
# get values about attribute
db_1row attribute "
select
content_type_id,
attribute,
attribute_type,
public_p
from
cams_attributes
where
attribute_id = :attribute_id"
# check content type
if {![string match "${attribute_type}/*" [string tolower ${content_type}]]} {
# how do you abort this?
ns_log Error "::cams::file::new Attempt to load ${content_type} into attribute $attribute of type $attribute_type"
return -code return
}
set file_extension [file extension $filename]
# Okay where to upload the file to based on input
set path [::cams::file::path $Array(content_id) $attribute]
file mkdir $path
set file_with_path ${path}$Array(attr_order)${file_extension}
file copy $tmpfile $file_with_path
ns_log Notice "::cams::file::new completed upload to '$file_with_path'"
}
proc ::cams::file::path {content_id attribute} {
return "[acs_package_root_dir "cams"]/files/[expr ${content_id} % 23]/${content_id}/${attribute}/"
}
proc ::cams::file::root {content_id attribute attr_order} {
return "[::cams::file::path $content_id $attribute ]${attr_order}"
}
proc ::cams::template::admin_link { attribute id level } {
# deal with admin_p
upvar \#$level show_admin_controls show_admin_controls ${attribute}:${id}:value_id value_id
if {[info exists show_admin_controls] && $show_admin_controls && [info exists value_id]} {
# add link to edit attribute
return " \[Edit]"
} else {
return ""
}
}
proc ::cams::template::root_dir { } {
return "[acs_package_root_dir "cams"]/templates"
}
proc attribute_tag {string tagset} {
if {[catch {
upvar content_id content_id
set Attribute(id) [ns_set get $tagset id]
set Attribute(name) [ns_set get $tagset name]
set Attribute(format) [string tolower [ns_set iget $tagset format]]
if {[string equal "" $Attribute(id)]} {
set id "1"
}
if {[string equal "" $Attribute(format)]} {
set Attribute(format) "link"
} else {
ns_set delkey $tagset "format"
}
set size [ns_set size $tagset]
set html_list [list]
for {set i 0} {$i < $size} {incr i} {
lappend html_list "[ns_set key $tagset $i]=\"[ns_quotehtml [ns_set value $tagset $i]]\""
}
set extra_html [join $html_list " " ]
set Attribute(admin_link) [::cams::template::admin_link $Attribute(name) $Attribute(id) [expr [info level] -1]]
# get attribute values
upvar $Attribute(name):$Attribute(id) __ATTRIBUTE_VALUE
switch -exact -- $Attribute(format) {
img {
set return "
$Attribute(admin_link)"
}
link - default {
set return "$string$Attribute(admin_link)"
}
}
} err ]} {
ns_log Error "attribute_tag: $err '[info script]'"
set return ""
}
return $return
}
proc var_tag {tagset} {
if {[catch {
set Attribute(name) [ns_set iget $tagset name]
upvar $Attribute(name) __ATTRIBUTE
set name_split [split $Attribute(name) ":"]
set name [lindex $name_split 0]
set id [lindex $name_split 1]
set Attribute(admin_link) [::cams::template::admin_link $name $id [expr [info level] -1]]
if {[info exists __ATTRIBUTE]} {
set return "${__ATTRIBUTE}$Attribute(admin_link)"
} else {
set return ""
}
} err ]} {
ns_log Error "var_tag: '$err' '[info script]'"
set return ""
}
return $return
}
proc elink_tag {string tagset} {
# For external links
if {[catch {
set Attribute(name) [ns_set get $tagset name]
if {[string equal "" $Attribute(name)]} {
set Attribute(name) "elink"
} else {
ns_set delkey $tagset "name"
}
set Attribute(id) [ns_set get $tagset id]
if {[string equal "" $Attribute(id)]} {
set Attribute(id) "1"
}
upvar $Attribute(name):$Attribute(id) LINK
set size [ns_set size $tagset]
set html_list [list]
for {set i $size} {$i < $size} {incr i} {
lappend html_list "[ns_set key $tagset $i]=\"[ns_quotehtml [ns_set value $tagset $i]]\""
}
set extra_html [join $html_list " " ]
set Attribute(admin_link) [::cams::template::admin_link $Attribute(name) $Attribute(id) [expr [info level] -1]]
set return "$string$Attribute(admin_link)"
} err ]} {
ns_log Error "proc elink_tag: $err"
set return ""
}
return $return
}
proc mini_album {string tagset} {
if {[catch {
set tagSize [ns_set size $tagset]
for {set i 0} {$i < $tagSize} {incr i} {
set key [ns_set key $tagset $i]
set $key [ns_set value $tagset $i]
}
if {![info exists dims]} {
set dims "2x3"
}
if {[info exists ids]} {
# pull up subset
set return "$string"
} else {
# show regular album
set return "$string"
}
} err ]} {
ns_log Error "mini_album: '$err'"
set return ""
}
return $return
}
proc image_tag {tagset} {
# For internal images
if {[catch {
upvar content_id content_id
set Attribute(name) [ns_set get $tagset name]
if {[string equal "" $Attribute(name)]} {
set Attribute(name) "elink"
} else {
ns_set delkey $tagset "name"
}
set Attribute(id) [ns_set get $tagset id]
if {[string equal "" $Attribute(id)]} {
set Attribute(id) "1"
}
set __ATTRIBUTE [ns_set get $tagset filename]
if {[string equal "" $__ATTRIBUTE]} {
set var $Attribute(name):$Attribute(id)
upvar $var filename
set __ATTRIBUTE $filename
} else {
ns_set delkey $tagset "filename"
}
set size [ns_set size $tagset]
set html_list [list]
for {set i 0} {$i < $size} {incr i} {
lappend html_list "[ns_set key $tagset $i]=\"[ns_quotehtml [ns_set value $tagset $i]]\""
}
set extra_html [join $html_list " " ]
set Attribute(admin_link) [::cams::template::admin_link $Attribute(name) $Attribute(id) [expr [info level] -1]]
set return "
$Attribute(admin_link)"
} err ]} {
ns_log Error "image_tag: '$err' '[info script]'"
set return ""
}
return $return
}
ns_register_adptag var var_tag
ns_register_adptag elink /elink elink_tag
ns_register_adptag attribute /attribute attribute_tag
ns_register_adptag image image_tag
ns_register_adptag album /album mini_album