# /packages/intranet-core/companies/primary-contact.tcl
#
# Copyright (C) 1998-2004 various parties
# The code is based on ArsDigita ACS 3.4
#
# This program is free software. You can redistribute it
# and/or modify it under the terms of the GNU General
# Public License as published by the Free Software Foundation;
# either version 2 of the License, or (at your option)
# any later version. This program is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.

ad_page_contract {
    Lets you select a primary contact from users assigned to this group

    @param company_id company's group id
    @author mbryzek@arsdigita.com
    @author frank.bergmann@project-open.com
} {
    company_id:integer,notnull
}

set user_id [auth::require_login]
set page_title "[_ intranet-core.Add_primary_contact]"
set return_url [im_url_with_query]

set company_name [db_string company_name {
select
	c.company_name
from 
	im_companies c
where
	c.company_id = :company_id
}]

set sql "
select distinct
        u.user_id,
        im_name_from_user_id(u.user_id) as name,
        im_email_from_user_id(u.user_id) as email
from
        users u,
        acs_rels r
where
        r.object_id_one = :company_id
        and r.object_id_two = u.user_id
"

if {$company_id != [im_company_internal]} {
    append sql "
	        and not exists (
			select	member_id
			from	group_member_map m,
				membership_rels mr
			where	m.member_id = u.user_id
				and m.rel_id = mr.rel_id
				and mr.member_state = 'approved'
				and m.group_id = [im_employee_group_id]
		)       
    "
}


set contact_info ""
db_foreach address_book_info $sql  {
    append contact_info "<li>$name, $email  </a>(<a href=[export_vars -base primary-contact-2 {company_id user_id}]>[_ intranet-core.make_primary_contact]</a>)"
} 

db_release_unused_handles


if { $contact_info eq "" } {
    set profile "Customers"
    set new_client_contact_link "<A HREF=/intranet/users/[export_vars -base new { return_url profile}]>[_ intranet-core.new_client_contact]</A>"
    set company_employee_link "<A HREF=\"/intranet/member-add?object_id=$company_id&also_add_to_group_id=461&return_url=/intranet/companies/view?company%5fid=$company_id\">[_ intranet-core.company_employee]</A>"
    set page_body "
<H3>[_ intranet-core.lt_No_Company_Employees_]</H3>
[_ intranet-core.lt_We_have_no_contacts_i]<BR>
<OL>
  <LI>[_ intranet-core.lt_Please_create_a_new_c_1].

  <LI>[_ intranet-core.lt_Make_the_new_client_c_1]

  <LI>[_ intranet-core.lt_Finally_revisit_this_]

</OL>

[_ intranet-core.lt_Also_please_make_sure]
"

    ad_return_template
    return
}

set return_url "[im_url_stub]/companies/[export_vars -base view {company_id}]"

set page_title "[_ intranet-core.lt_Select_primary_contac]"

set page_body "
<ul>
$contact_info
</ul>
"

ad_return_template
