Index: openacs-4/packages/contacts/www/contact-add.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/contacts/www/contact-add.tcl,v diff -u -r1.5 -r1.6 --- openacs-4/packages/contacts/www/contact-add.tcl 8 Jun 2005 05:10:59 -0000 1.5 +++ openacs-4/packages/contacts/www/contact-add.tcl 9 Jun 2005 16:05:59 -0000 1.6 @@ -7,6 +7,7 @@ } { {object_type "person"} + {group_id ""} } -validate { valid_type -requires {object_type} { if { [lsearch [list organization person] $object_type] < 0 } { @@ -16,26 +17,60 @@ } set package_id [ad_conn package_id] -set form "$package_id\__[contacts::default_group]" -set form_elements [ams::ad_form::elements -package_key "contacts" -object_type $object_type -list_name $form -key party_id] +set form_elements {party_id:key} lappend form_elements {object_type:text(hidden)} +set default_group_id [contacts::default_group -package_id $package_id] +set application_group_id [application_group::group_id_from_package_id -package_id [ad_conn subsite_id]] + +set groups [list $default_group_id] +if {![empty_string_p group_id] && $group_id != $default_group_id} { + lappend groups $group_id + lappend form_elements {group_id:text(hidden) {value $group_id}} +} + +# Save Group Information +if {$default_group_id != $application_group_id} { + lappend groups $application_group_id +} + + +foreach group [contact::groups -expand "all" -privilege_required "read"] { + set group_id [lindex $group 1] + if { [lsearch $groups $group_id] >= 0 } { + lappend ams_forms "${package_id}__${group_id}" + } +} + +foreach form $ams_forms { + append form_elements " " + append form_elements [ams::ad_form::elements -package_key "contacts" -object_type $object_type -list_name $form] +} + +# Append the option to create a user who get's a welcome message send +# Furthermore set the title. + if { $object_type == "person" } { + lappend form_elements {create_user_p:text(radio) \ + {label "[_ contacts.Create_user]"}\ + {options {{[_ acs-kernel.common_Yes] "t"} {[_ acs-kernel.common_no] "f"}}} \ + {values "f"} + } set title "[_ contacts.Add_a_Person]" } else { set title "[_ contacts.Add_an_Organization]" } set user_id [ad_conn user_id] +set peeraddr [ad_conn peeraddr] set context [list $title] - ad_form -name party_ae \ -mode "edit" \ -cancel_label "[_ contacts.Cancel]" \ -cancel_url [export_vars -base contact -url {party_id}] \ - -edit_buttons [list [list Save save] [list "[_ contacts.Save_and_Add_Another]" save_add_another]] \ + -edit_buttons [list [list "[_ acs-kernel.common_Save]" save] [list "[_ contacts.Save_and_Add_Another]" save_add_another]] \ -form $form_elements callback contact::contact_form -package_id $package_id -form party_ae -object_type $object_type @@ -86,98 +121,104 @@ } -new_data { if { $object_type == "person" } { - if { ![exists_and_not_null email]] } { + if { ![exists_and_not_null email] } { set email "$party_id@bogusdomain.com" set username $party_id } - if { ![exists_and_not_null username]] } { + if { ![exists_and_not_null username] } { set username $email } - if { ![exists_and_not_nul url]] } { + if { ![exists_and_not_null url] } { set url "" } - db_transaction { - array set creation_info [auth::create_user \ - -user_id $party_id \ - -verify_password_confirm \ - -username $email \ - -email $email \ - -first_names $first_names \ - -last_name $last_name \ - -screen_name "" \ - -password "" \ - -password_confirm "" \ - -url $url \ - -secret_question "" \ - -secret_answer ""] + + if {$create_user_p == "f"} { + # Initialize Party Entry + # We do not want to create a new user with each contact + template::form create add_party + template::element create add_party email -value "$email" + template::element create add_party first_names -value "$first_names" + template::element create add_party last_name -value "$last_name" + template::element create add_party url -value "$url" + set party_id [party::new -party_id $party_id -form_id add_party person] - if { "$email" == "$party_id@bogusdomain.com" } { - # we need to delete the party email address - party::update -party_id $party_id -email "" -url $url - } + } else { - if { [string equal $creation_info(creation_status) "ok"] } { - group::add_member \ - -group_id [application_group::group_id_from_package_id -package_id [ad_conn subsite_id]] \ - -user_id $party_id \ - -rel_type "membership_rel" - } else { - ns_log warning "contacts/www/contact add user error: \n creation_status \n $creation_info(creation_status) \n creation_message \n $creation_info(creation_message) \n element_messages \n $creation_info(element_messages)" - error $creation_info(creation_status) - } - } on_error { - ad_return_error "[_ contacts.Error]" "[_ contacts.The_error_was_errmsg]" + array set creation_info [auth::create_user \ + -user_id $party_id \ + -verify_password_confirm \ + -username $email \ + -email $email \ + -first_names $first_names \ + -last_name $last_name \ + -screen_name "" \ + -password "" \ + -password_confirm "" \ + -url $url \ + -secret_question "" \ + -secret_answer ""] + if { ![string equal $creation_info(creation_status) "ok"] } { + ad_return_error "Error" "contacts/www/contact add user error: \n creation_status \n $creation_info(creation_status) \n creation_message \n $creation_info(creation_message) \n element_messages \n $creation_info(element_messages)" + error $creation_info(creation_status) + } } + + + if { "$email" == "$party_id@bogusdomain.com" } { + # we need to delete the party email address + party::update -party_id $party_id -email "" -url $url + } + + + foreach group_id $groups { + group::add_member \ + -group_id $group_id \ + -user_id $party_id \ + -rel_type "membership_rel" + } + + } else { - # name is not included in this list because its required and checked for above - set elements_for_insert [list legal_name notes reg_number email url] - foreach element_for_insert $elements_for_insert { - if { [string is false [exists_and_not_null $element_for_insert]] } { - set $element_for_insert "" + + # Initialize Party Entry for organization + set party_id [organizations::new -organization_id $party_id -name $name] + + foreach group_id $groups { + if {![empty_string_p $group_id]} { + + # relation-add does not work as there is no + # special procedure for organizations at the moment. + set rel_id [db_string insert_rels { select acs_rel__new (NULL::integer,'organization_rel',:group_id,:party_id,NULL,:user_id,:peeraddr) as org_rel_id }] + db_dml insert_state { insert into membership_rels (rel_id,member_state) values (:rel_id,'approved') } } } - set peeraddr [ad_conn peeraddr] - - db_transaction { - set party_id [db_exec_plsql do_insert_org { - select organization__new ( - :legal_name, - :name, - :notes, - null, - null, - :reg_number, - :email, - :url, - :user_id, - :peeraddr, - :package_id - ) - }] - set group_id [application_group::group_id_from_package_id -package_id [ad_conn subsite_id]] - set rel_id [db_string insert_rels { select acs_rel__new (NULL::integer,'organization_rel',:group_id,:party_id,NULL,:user_id,:peeraddr) as org_rel_id }] - # db_1row insert_member { select acs_rel__new (NULL::integer,'membership_rel',:group_id,:party_id,NULL,:user_id,:peeraddr) } - db_dml insert_state { insert into membership_rels (rel_id,member_state) values (:rel_id,'approved') } - } } - + + # Save the contact information + # No clue why this is not part of the db_transaction though .... contact::special_attributes::ad_form_save -party_id $party_id -form "party_ae" - ams::ad_form::save -package_key "contacts" \ - -object_type $object_type \ - -list_name $form \ - -form_name "party_ae" \ - -object_id [contact::revision::new -party_id $party_id] + set revision_id [contact::revision::new -party_id $party_id] + foreach form $ams_forms { + ams::ad_form::save -package_key "contacts" \ + -object_type $object_type \ + -list_name $form \ + -form_name "party_ae" \ + -object_id $revision_id + } + + callback contact::contact_new_form -package_id $package_id -contact_id $party_id -form party_ae -object_type $object_type + # Add the user to the util_user_message -html -message "The $object_type [contact::name -party_id $party_id] was added" } -after_submit { if { [exists_and_not_null formbutton\:save_add_another] } { - ad_returnredirect "contact-add?object_type=${object_type}" + ad_returnredirect [export_vars -base "contact-add" -url {object_type group_id}] } else { - ad_returnredirect "./" + ad_returnredirect [export_vars -base "." -url {{query_id $group_id}}] } ad_script_abort }