PageRenderTime 27ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/packages/intranet-contacts/www/message.tcl

https://github.com/cognovis/project-open
TCL | 410 lines | 338 code | 50 blank | 22 comment | 70 complexity | d8fb0b6d5720873a6166f9aeea6f41d9 MD5 | raw file
  1. ad_page_contract {
  2. List and manage contacts.
  3. @author Matthew Geddert openacs@geddert.com
  4. @creation-date 2004-07-28
  5. @cvs-id $Id: message.tcl,v 1.3 2010/07/13 17:54:55 cvs Exp $
  6. } {
  7. {attachment_id:integer,multiple,optional}
  8. {object_id:integer,multiple,optional}
  9. {party_id:multiple,optional}
  10. {party_ids ""}
  11. {search_id:integer ""}
  12. {message_type ""}
  13. {message:optional}
  14. {header_id:integer ""}
  15. {footer_id:integer ""}
  16. {return_url "./"}
  17. {file_ids ""}
  18. {files_extend:integer,multiple,optional ""}
  19. {item_id:integer ""}
  20. {folder_id:integer ""}
  21. {signature_id:integer ""}
  22. {subject ""}
  23. {content_body:html ""}
  24. {to:integer,multiple,optional ""}
  25. {page:optional 1}
  26. {context_id:integer ""}
  27. {cc ""}
  28. {bcc ""}
  29. } -validate {
  30. valid_message_type -requires {message_type} {
  31. if { ![db_0or1row check_for_it { select 1 from contact_message_types where message_type = :message_type and message_type not in ('header','footer') }] } {
  32. ad_complain "[_ intranet-contacts.lt_Your_provided_an_inva]"
  33. }
  34. }
  35. }
  36. if { [exists_and_not_null message] && ![exists_and_not_null message_type] } {
  37. set message_type [lindex [split $message "."] 0]
  38. set item_id [lindex [split $message "."] 1]
  39. }
  40. if {[empty_string_p $party_ids]} {
  41. set party_ids [list]
  42. }
  43. set invalid_party_ids [list]
  44. set package_id [ad_conn package_id]
  45. set recipients [list]
  46. set recipients_label [_ intranet-contacts.Recipients]
  47. if { $search_id ne "" } {
  48. set return_url [export_vars -base [apm_package_url_from_id $package_id] -url {search_id}]
  49. if {[contact::group::mapped_p -group_id $search_id]} {
  50. # Make sure the user has write permission on the group
  51. permission::require_permission -object_id $search_id -privilege "write"
  52. lappend recipients "<a href=\"$return_url\">[contact::group::name -group_id $search_id]</a>"
  53. if { [contact::group::notifications_p -group_id $search_id] } {
  54. set recipients_label [_ intranet-contacts.Notify]
  55. }
  56. } else {
  57. lappend recipients "<a href=\"$return_url\">[contact::search::title -search_id $search_id]</a>"
  58. }
  59. # We do the check in the search template
  60. set valid_party_ids "0"
  61. } else {
  62. set valid_party_ids ""
  63. if { [exists_and_not_null party_id] } {
  64. foreach p_id $party_id {
  65. if {[lsearch $party_ids $p_id] < 0} {
  66. lappend party_ids $p_id
  67. }
  68. }
  69. }
  70. # Deal with object_ids passed in
  71. if { [exists_and_not_null object_id] } {
  72. foreach p_id $object_id {
  73. if {[lsearch $party_ids $p_id] < 0} {
  74. lappend party_ids $p_id
  75. }
  76. }
  77. }
  78. if { [exists_and_not_null to] } {
  79. foreach party_id $to {
  80. lappend party_ids $party_id
  81. }
  82. }
  83. # Make sure the parties are visible to the user
  84. foreach id $party_ids {
  85. ds_comment "PARTY: $party_ids"
  86. if {[contact::visible_p -party_id $id -package_id $package_id]} {
  87. lappend valid_party_ids $id
  88. }
  89. }
  90. }
  91. set party_count [llength $valid_party_ids]
  92. set title "[_ intranet-contacts.Messages]"
  93. set user_id [ad_conn user_id]
  94. set context [list $title]
  95. if {![exists_and_not_null valid_party_ids]} {
  96. ad_return_error "[_ intranet-contacts.No_valid_parties]" "[_ intranet-contacts.No_valid_parties_lt]"
  97. ad_script_abort
  98. }
  99. set invalid_recipients [list]
  100. set party_ids [list]
  101. # Make sure that we can actually send the message
  102. foreach party_id $valid_party_ids {
  103. if { [lsearch [list "letter" "label" "envelope"] $message_type] >= 0 } {
  104. # Check if we can send a letter to this party
  105. set letter_p [contact::message::mailing_address_exists_p -party_id $party_id]
  106. if { $letter_p } {
  107. lappend party_ids $party_id
  108. } else {
  109. lappend invalid_party_ids $party_id
  110. }
  111. } elseif { $message_type == "email" } {
  112. if { [party::email -party_id $party_id] eq "" } {
  113. # We are going to check if there is an employee relationship
  114. # if there is we are going to check if the employer has an
  115. # email adrres, if it does we are going to use that address
  116. set employer_id [lindex [contact::util::get_employee_organization -employee_id $party_id] 0]
  117. if { ![empty_string_p $employer_id] } {
  118. set emp_addr [contact::email -party_id $employer_id]
  119. if { ![empty_string_p $emp_addr] } {
  120. lappend party_ids $employer_id
  121. } else {
  122. lappend invalid_party_ids $party_id
  123. }
  124. } else {
  125. lappend invalid_party_ids $party_id
  126. }
  127. } else {
  128. lappend party_ids $party_id
  129. }
  130. } else {
  131. # We are unsure what to send, so just assume for the time being we can send it to them
  132. lappend party_ids $party_id
  133. }
  134. }
  135. # If we are passing in a group, do not show the individual users
  136. if { [empty_string_p $search_id] } {
  137. # Prepare the recipients
  138. foreach party_id $party_ids {
  139. set contact_name [contact::name -party_id $party_id]
  140. set contact_url [contact::url -party_id $party_id]
  141. lappend recipients "<a href=\"${contact_url}\">${contact_name}</a>"
  142. }
  143. set form_elements "party_ids:text(hidden)"
  144. } else {
  145. set form_elements ""
  146. }
  147. # Deal with the invalid recipients
  148. foreach party_id $invalid_party_ids {
  149. set contact_name [contact::name -party_id $party_id]
  150. set contact_url [contact::url -party_id $party_id]
  151. lappend invalid_recipients "<a href=\"${contact_url}\">${contact_name}</a>"
  152. }
  153. set recipients [join $recipients ", "]
  154. set invalid_recipients [join $invalid_recipients ", "]
  155. if { [llength $invalid_recipients] > 0 } {
  156. switch $message_type {
  157. letter {
  158. set error_message [_ intranet-contacts.lt_You_cannot_send_a_letter_to_invalid_recipients]
  159. }
  160. email {
  161. set error_message [_ intranet-contacts.lt_You_cannot_send_an_email_to_invalid_recipients]
  162. }
  163. default {
  164. set error_message [_ intranet-contacts.lt_You_cannot_send_a_message_to_invalid_recipients]
  165. }
  166. }
  167. if { $party_ids != "" } {
  168. util_user_message -html -message $error_message
  169. }
  170. }
  171. if {[exists_and_not_null attachment_id]} {
  172. foreach object $attachment_id {
  173. if {[fs::folder_p -object_id $object]} {
  174. db_foreach files {select r.revision_id
  175. from cr_revisions r, cr_items i
  176. where r.item_id = i.item_id and i.parent_id = :object} {
  177. lappend file_list $revision_id
  178. }
  179. } else {
  180. set revision_id [content::item::get_best_revision -item_id $object]
  181. if {[empty_string_p $revision_id]} {
  182. # so already is a revision
  183. lappend file_list $object
  184. } else {
  185. # append revision of content item
  186. lappend file_list $revision_id
  187. }
  188. }
  189. }
  190. # If we have files we need to unset the attachment_id
  191. set attachment_id ""
  192. } else {
  193. set attachment_id ""
  194. }
  195. if {[exists_and_not_null file_list]} {
  196. set file_ids [join $file_list " "]
  197. }
  198. append form_elements {
  199. file_ids:text(hidden)
  200. search_id:text(hidden)
  201. return_url:text(hidden)
  202. folder_id:text(hidden)
  203. attachment_id:text(hidden)
  204. context_id:text(hidden)
  205. {to_name:text(inform),optional {label "$recipients_label"} {value $recipients}}
  206. }
  207. if { ![exists_and_not_null message_type] } {
  208. set public_text [_ intranet-contacts.Public]
  209. set package_id [ad_conn package_id]
  210. set message_type_options [ams::util::localize_and_sort_list_of_lists \
  211. -list [db_list_of_lists get_message_types { select pretty_name, message_type from contact_message_types }] \
  212. ]
  213. foreach op $message_type_options {
  214. set [lindex ${op} 1]_options [list]
  215. set [lindex ${op} 1]_text [lindex ${op} 0]
  216. }
  217. db_foreach get_messages {
  218. select
  219. CASE WHEN owner_id = :package_id THEN :public_text ELSE contact__name(owner_id) END as public_display,
  220. title,
  221. to_char(item_id,'FM9999999999999999999999') as item_id,
  222. message_type
  223. from
  224. contact_messages
  225. where
  226. owner_id in ( select party_id from parties )
  227. or owner_id = :package_id
  228. order by
  229. CASE WHEN owner_id = :package_id THEN '000000000' ELSE upper(contact__name(owner_id)) END,
  230. message_type, upper(title)
  231. } {
  232. # Remove special TCL characters
  233. regsub -all {\$} $title {&#36;} title
  234. regsub -all {\[} $title {&#91;} title
  235. regsub -all {\]} $title {&#93;} title
  236. # The oo_mailing message type is used if you have a mailing template as defined in /lib/oo_mailing
  237. if {$message_type == "letter" || $message_type == "email" || $message_type == "oo_mailing"} {
  238. lappend ${message_type}_options [list "$public_display [set ${message_type}_text]:$title" "${message_type}.$item_id"]
  239. } else {
  240. lappend ${message_type}_options [list "$public_display:$title" "$item_id"]
  241. }
  242. }
  243. # Only Email can be used without a template
  244. set message_options [list [list "-- [_ intranet-contacts.New] Email --" email]]
  245. foreach op $message_type_options {
  246. if { [lsearch [list "header" "footer"] [lindex $op 1]] < 0 } {
  247. set message_options [concat $message_options [set [lindex $op 1]_options]]
  248. }
  249. }
  250. if {[exists_and_not_null header_options]} {
  251. lappend form_elements [list \
  252. header_id:text(select) \
  253. [list label "[_ intranet-contacts.Header]"] \
  254. [list options $header_options] \
  255. ]
  256. }
  257. if { [llength $message_options] == 1 } {
  258. lappend form_elements [list message:text(hidden) [list value "email"]]
  259. set message_type "email"
  260. } else {
  261. lappend form_elements [list \
  262. message:text(select) \
  263. [list label "[_ intranet-contacts.Message]"] \
  264. [list options $message_options] \
  265. ]
  266. set message_type ""
  267. }
  268. set title [_ intranet-contacts.create_a_message]
  269. set message_create_p 0
  270. } else {
  271. set message_create_p 1
  272. }
  273. set context [list $title]
  274. set signature_options_p 0
  275. if { [string is false [exists_and_not_null message]] } {
  276. set signature_list [list]
  277. set reset_title $title
  278. set reset_signature_id $signature_id
  279. db_foreach signatures "select title, signature_id, default_p
  280. from contact_signatures
  281. where party_id = :user_id
  282. order by default_p, upper(title), upper(signature)" {
  283. lappend signature_list [list $title $signature_id]
  284. if { $default_p == "t" } {
  285. set default_signature_id $signature_id
  286. }
  287. }
  288. set title $reset_title
  289. set signature_id $reset_signature_id
  290. if { [llength $signature_list] > 1 } {
  291. append form_elements {
  292. {signature_id:text(select)
  293. {label "[_ intranet-contacts.Signature]"}
  294. {options {$signature_list}}
  295. }
  296. }
  297. set signature_options_p 1
  298. } elseif { [llength $signature_list] >= 1 } {
  299. set signature_id [lindex [lindex $signature_list 0] 1]
  300. set signature_label [lindex [lindex $signature_list 0] 0]
  301. append form_elements {
  302. {signature_pretty:text(inform)
  303. {label "[_ intranet-contacts.Signature]"}
  304. {value {<a href="[export_vars -base "signature" -url {signature_id}]">$signature_label</a>}}
  305. }
  306. {signature_id:text(hidden)
  307. {value {$signature_id}}
  308. }
  309. }
  310. }
  311. set signature_id $reset_signature_id
  312. }
  313. if {[exists_and_not_null footer_options]} {
  314. lappend form_elements [list \
  315. footer_id:text(select) \
  316. [list label "[_ intranet-contacts.Footer]"] \
  317. [list options $footer_options] \
  318. ]
  319. }
  320. if { ![exists_and_not_null header_options] && \
  321. ![exists_and_not_null footer_options] && \
  322. [string is false $signature_options_p] && \
  323. $message_type eq "email" } {
  324. # there is nothing for them to select so we select next for them
  325. set message_create_p 1
  326. }
  327. if { $message_create_p } {
  328. set title [_ intranet-contacts.create_$message_type]
  329. if {$search_id ne ""} {
  330. # Get the search template
  331. set message_src "/packages/intranet-contacts/lib/${message_type}-search"
  332. } else {
  333. set message_src "/packages/intranet-contacts/lib/${message_type}"
  334. }
  335. }
  336. set edit_buttons [list [list "[_ intranet-contacts.Next]" create]]
  337. # the message form will reset party_ids so we need to carry it over
  338. set new_party_ids $party_ids
  339. ad_form -action message \
  340. -name message \
  341. -cancel_label "[_ intranet-contacts.Cancel]" \
  342. -cancel_url $return_url \
  343. -edit_buttons $edit_buttons \
  344. -form $form_elements \
  345. -on_request {
  346. if { [exists_and_not_null default_signature_id] } {
  347. set signature_id $default_signature_id
  348. } else {
  349. set signature_id ""
  350. }
  351. } -new_request {
  352. } -edit_request {
  353. } -on_submit {
  354. }
  355. set party_ids $new_party_ids
  356. if {[exists_and_not_null signature_id]} {
  357. set signature [contact::signature::get -signature_id $signature_id]
  358. } else {
  359. set signature ""
  360. }