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.52 -r1.53 --- openacs-4/packages/contacts/www/contact-add.tcl 8 Nov 2006 18:31:29 -0000 1.52 +++ openacs-4/packages/contacts/www/contact-add.tcl 16 Dec 2006 11:27:20 -0000 1.53 @@ -6,22 +6,21 @@ } { - {object_type "person"} + {object_type "employee"} {group_ids ""} {rel_type ""} {role_two ""} {object_id_two ""} } -validate { valid_type -requires {object_type} { - if { [lsearch [list organization person] $object_type] < 0 } { + if { [lsearch [list organization person employee] $object_type] < 0 } { ad_complain "[_ contacts.lt_You_have_not_specifie]" } } } set master_src [parameter::get -parameter "ContactsMaster"] set default_group [contacts::default_group] - set group_list [concat [list [list [_ contacts.All_Contacts] $default_group "0"]] [contact::groups]] if {[empty_string_p $group_ids] && [llength $group_list] > 1} { @@ -35,10 +34,12 @@ set path_info [ad_conn path_info] -if { $path_info == "add/person" } { +if { $path_info eq "add/person" } { set object_type "person" -} elseif { $path_info == "add/organization" } { +} elseif { $path_info eq "add/organization" } { set object_type "organization" +} elseif { $path_info eq "add/employee" } { + set object_type "employee" } set package_id [ad_conn package_id] @@ -80,7 +81,7 @@ # Adding the list_name to get the elements in the form lappend list_names [list ${package_id}__${group_id}] - + # Add the category widget(s) set element_name "category_ids$group_id" if {$group_id < 0} { @@ -97,11 +98,24 @@ } -set form_definition [ams::ad_form::elements \ - -package_key "contacts" \ - -object_type $object_type \ - -list_names $list_names] +if {$object_type eq "employee"} { + + # Make sure the relationship is set correctly + set rel_type "contact_rels_employment" + set role_two "" + + # Employee is like adding a business card. First company information, then person information + set form_definition [ams::ad_form::elements \ + -package_key "contacts" \ + -object_types "[list person organization]" \ + -list_names $list_names] +} else { + set form_definition [ams::ad_form::elements \ + -package_key "contacts" \ + -object_type $object_type \ + -list_names $list_names] +} if { [parameter::get -boolean -package_id $package_id -parameter "ContactPrivacyEnabledP" -default "0"] } { set privacy_setting_options [list] @@ -113,7 +127,7 @@ lappend privacy_setting_options [list [_ contacts.Do_not_email] email_p] lappend privacy_setting_options [list [_ contacts.Do_not_mail] mail_p] lappend privacy_setting_options [list [_ contacts.Do_not_phone] phone_p] - + lappend form_definition [list contact_privacy_settings:boolean(checkbox),multiple,optional \ [list label [_ contacts.Privacy_Settings]] \ [list options $privacy_setting_options] \ @@ -122,8 +136,6 @@ -#ad_return_error "$object_type" "$list_names :: $form_definition" - # Creating the form ad_form -extend -name party_ae -form $form_definition @@ -145,25 +157,33 @@ # Append the option to create a user who get's a welcome message send # Furthermore set the title. -if { $object_type == "person" } { +if { $object_type eq "person" } { set title "[_ contacts.Add_a_Person]" -} else { +} elseif { $object_type eq "organization"} { set title "[_ contacts.Add_an_Organization]" +} else { + set title "[_ contacts.Add_an_Employee]" } set context [list $title] -callback contact::contact_form -package_id $package_id -form party_ae -object_type $object_type -group_ids $group_ids -rel_type $rel_type +if {$object_type eq "employee"} { + callback contact::contact_form -package_id $package_id -form party_ae -object_type "organization" -group_ids $group_ids -rel_type $rel_type + callback contact::contact_form -package_id $package_id -form party_ae -object_type "person" -group_ids $group_ids -rel_type $rel_type +} else { + callback contact::contact_form -package_id $package_id -form party_ae -object_type $object_type -group_ids $group_ids -rel_type $rel_type +} ad_form -extend -name party_ae \ -on_request { - - if { $object_type == "person" } { + + if { $object_type eq "person" } { set required_attributes [list first_names last_name email] - } else { + } elseif {$object_type eq "organization"} { set required_attributes [list name] + } else { + set required_attributes [list first_names last_name email name] } - set missing_elements [list] foreach attribute $required_attributes { if { [string is false [template::element::exists party_ae $attribute]] } { @@ -189,132 +209,153 @@ } } } -on_submit { - + # for orgs name needs to be unique # for users username needs to be unique # for all of them email needs to be unique - + if { $object_type == "person" } { if { ![exists_and_not_null first_names] } { template::element::set_error party_ae first_names "[_ contacts.lt_First_Names_is_requir]" } if { ![exists_and_not_null last_name] } { template::element::set_error party_ae last_name "[_ contacts.lt_Last_Name_is_required]" } + } elseif {$object_type eq "organization"} { + if { ![exists_and_not_null name] } { + template::element::set_error party_ae name "[_ contacts.Name_is_required]" + } } else { if { ![exists_and_not_null name] } { template::element::set_error party_ae name "[_ contacts.Name_is_required]" - } else { - # We (cognovis) got rid of the unique constraints on organization name as this does not make any sense - # An organization could be in multiple locations and independend units of the same large organization could be - # different organizations / customers. After all we deal with seperate business units and these should be treated seperately. - -# set other_organization_id [organization::get_by_name -name $name] -# if { ![empty_string_p $other_organization_id] } { -# set another_organization [contact::link -party_id $other_organization_id] -# template::element::set_error party_ae name "[_ contacts.lt_-another_organization-_already_uses_this_name]" -# } - } + } + if { ![exists_and_not_null first_names] } { + template::element::set_error party_ae first_names "[_ contacts.lt_First_Names_is_requir]" + } + if { ![exists_and_not_null last_name] } { + template::element::set_error party_ae last_name "[_ contacts.lt_Last_Name_is_required]" + } } - if { [exists_and_not_null email] } { - set other_party_id [party::get_by_email -email $email] - if { ![empty_string_p $other_party_id] } { - set another_contact [contact::link -party_id $other_party_id] + if { [exists_and_not_null email] } { + set other_party_id [party::get_by_email -email $email] + if { ![empty_string_p $other_party_id] } { + set another_contact [contact::link -party_id $other_party_id] template::element::set_error party_ae email "[_ contacts.lt_-another_contact-_already_uses_this_email]" } } - + if { ![template::form::is_valid party_ae] } { break } } -new_data { - if { $object_type == "person" } { - - if { ![exists_and_not_null url] } { - set url "" - } + if {$object_type eq "employee"} { + set object_types [list person organization] + set person_party_id $party_id + set organization_party_id "" + } else { + set object_types $object_type + set ${object_type}_party_id $party_id + } + set orig_object_type $object_type - # Initialize Person - 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] - # party::new does not correctly save email address - party::update -party_id $party_id -email $email -url $url - - # in order to create a user we need a valid unique username (i.e. their email address). - # the on_submit block has already validated that this is in fact a valid and unique - # email address which will serve as their username - callback contact::person_add -package_id $package_id -person_id $party_id + foreach object_type $object_types { + if { $object_type eq "person" } { + + if { ![exists_and_not_null url] } { + set url "" + } + + # Initialize Person + 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 person_party_id [party::new -party_id $person_party_id -form_id add_party person] - # Add the new categories and enter the Party into the groups - set cat_ids [list] - - foreach group_id $group_ids { - group::add_member \ - -group_id $group_id \ - -user_id $party_id \ - -rel_type "membership_rel" + # party::new does not correctly save email address + party::update -party_id $person_party_id -email $email -url $url + + # in order to create a user we need a valid unique username (i.e. their email address). + # the on_submit block has already validated that this is in fact a valid and unique + # email address which will serve as their username + callback contact::person_add -package_id $package_id -person_id $person_party_id - set element_name "category_ids$group_id" - if {$group_id < 0} { - set element_name "category_ids[expr - $group_id]" + # Add the new categories and enter the Party into the groups + set cat_ids [list] + + foreach group_id $group_ids { + group::add_member \ + -group_id $group_id \ + -user_id $person_party_id \ + -rel_type "membership_rel" + + set element_name "category_ids$group_id" + if {$group_id < 0} { + set element_name "category_ids[expr - $group_id]" + } + + set cat_ids [concat $cat_ids \ + [category::ad_form::get_categories \ + -container_object_id $group_id \ + -element_name $element_name]] } - set cat_ids [concat $cat_ids \ - [category::ad_form::get_categories \ - -container_object_id $group_id \ - -element_name $element_name]] + category::map_object -remove_old -object_id $person_party_id $cat_ids + callback contact::special_attributes::ad_form_save -party_id $person_party_id -form "party_ae" -object_type $object_type + } else { + + # Initialize Party Entry for organization + set organization_party_id [organization::new -organization_id $organization_party_id -name $name] + + foreach group_id $group_ids { + 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,:organization_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') } + } + } + + callback contact::special_attributes::ad_form_save -party_id $organization_party_id -form "party_ae" -object_type $object_type + callback contact::organization_new -package_id $package_id -contact_id $organization_party_id -name $name } - - category::map_object -remove_old -object_id $party_id $cat_ids - + + # Save the contact information + # No clue why this is not part of the db_transaction though .... - - } else { - - # Initialize Party Entry for organization - set party_id [organization::new -organization_id $party_id -name $name] - + set revision_id [contact::revision::new -party_id [set ${object_type}_party_id]] foreach group_id $group_ids { - if {![empty_string_p $group_id]} { + ams::ad_form::save -package_key "contacts" \ + -object_type $object_type \ + -list_name "${package_id}__${group_id}" \ + -form_name "party_ae" \ + -object_id $revision_id + + callback contact::${object_type}_new_group -${object_type}_id [set ${object_type}_party_id] -group_id $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') } + # execute group specific callbacks + group::get -group_id $group_id -array group_array + set group_name ${group_array(group_name)} + regsub -all " " $group_name "_" group_name + regsub -all {[^-a-zA-Z0-9_]} $group_name "" group_name + + if {[info exists contact::${object_type}_${group_array(group_name)}_new]} { + callback contact::${object_type}_${group_array(group_name)}_new -package_id $package_id -contact_id [set ${object_type}_party_id] } } - - callback contact::organization_new -package_id $package_id -contact_id $party_id -name $name } - - # Save the contact information - # No clue why this is not part of the db_transaction though .... - callback contact::special_attributes::ad_form_save -party_id $party_id -form "party_ae" - set revision_id [contact::revision::new -party_id $party_id] - foreach group_id $group_ids { - ams::ad_form::save -package_key "contacts" \ - -object_type $object_type \ - -list_name "${package_id}__${group_id}" \ - -form_name "party_ae" \ - -object_id $revision_id - callback contact::${object_type}_new_group -${object_type}_id $party_id -group_id $group_id - # execute group specific callbacks - group::get -group_id $group_id -array group_array - set group_name ${group_array(group_name)} - regsub -all " " $group_name "_" group_name - regsub -all {[^-a-zA-Z0-9_]} $group_name "" group_name - - if {[info exists contact::${object_type}_${group_array(group_name)}_new]} { - callback contact::${object_type}_${group_array(group_name)}_new -package_id $package_id -contact_id $party_id - } + # For employees we need to correctly save the relationship + if {$orig_object_type eq "employee"} { + set rel_type "contact_rels_employment" + set party_id $person_party_id + set object_id_two $organization_party_id + set object_type "person" } # Insert the relationship @@ -385,8 +426,9 @@ contact::search::flush_results_counts callback contact::contact_form_after_submit -party_id $party_id -package_id $package_id -object_type $object_type -form "party_ae" - # Now we have the organization or person created. Redirect as AMS can be saved in the background. - #the formbutton does not work. No clue how to fix it. + if {$orig_object_type eq "employee"} { + callback contact::contact_form_after_submit -party_id $organization_party_id -package_id $package_id -object_type organization -form "party_ae" + } if {[empty_string_p $object_id_two]} { ad_returnredirect [contact::url -party_id $party_id] } else {