Example #1
0
		include ("general/noaccess.php");
		exit;
	}
}

$new_contact = (bool) get_parameter ('new_contact');
$create_contact = (bool) get_parameter ('create_contact');
$update_contact = (bool) get_parameter ('update_contact');
$delete_contact = (bool) get_parameter ('delete_contact');
$get_contacts = (bool) get_parameter ('get_contacts');
$offset = get_parameter ('offset', 0);

if ($get_contacts && $id) {
	$contract = get_contract ($id);
	$company = get_company ($contract['id']);
	$contacts = get_company_contacts ($company['id'], false);
	
	echo json_encode ($contacts);
	if (defined ('AJAX'))
		return;
}

// Create
if ($create_contact) {

	if (!$id_company) {
		echo ui_print_error_message (__('Error creating contact. Company is empty'), '', true, 'h3', true);
	} else {
		if (!$write_permission && !$manage_permission) {
			audit_db($config["id_user"], $config["REMOTE_ADDR"], "ACL Violation","Trying to create a new contact in a group without access");
			require ("general/noaccess.php");
/**
 * Get all the contacts relative to an inventory object.
 *
 * There are two ways to get the list. By default, all the contacts in the
 * company that has the inventory contract will be returned. Anyway, if the
 * contacts list was changed manually when updating or creating the
 * inventory object, then these are the contacts of the object.
 *
 * @param int Inventory id.
 * @param bool Whether to return only contact names (default) or all the fields.
 *
 * @return array List of contacts relative to an inventory object.
 */
function get_inventory_contacts($id_inventory, $only_names = false)
{
    global $config;
    include_once "include/functions_crm.php";
    /* First try to get only defined contacts */
    $owner = get_db_value('owner', 'tinventory', 'id', $id_inventory);
    $owner_info = get_db_row("tusuario", "id_usuario", $owner);
    $all_contacts = array();
    $contact = array("id" => $owner, "type" => "user", "id_company" => $owner_info["id_company"], "fullname" => $owner_info["nombre_real"], "email" => $owner_info["direccion"], "phone" => $owner_info["telefono"], "mobile" => __("N/A"), "position" => __("N/A"), "description" => $owner_info["comentarios"], "disabled" => $owner_info["disabled"]);
    $all_contacts[$contact["id"]] = $contact;
    //Get all users associated to the inventory object
    $inv_users = enterprise_hook('inventory_get_users', array($id_inventory, false));
    if ($inv_users === ENTERPRISE_NOT_HOOK) {
        $inv_users = array();
    }
    foreach ($inv_users as $user) {
        $contact = array("id" => $user["id_usuario"], "type" => "user", "id_company" => $user["id_company"], "fullname" => $user["nombre_real"], "email" => $user["direccion"], "phone" => $user["telefono"], "mobile" => __("N/A"), "position" => __("N/A"), "description" => $user["comentarios"], "disabled" => $user["disabled"]);
        $all_contacts[$contact["id"]] = $contact;
    }
    $inv_companies = enterprise_hook('inventory_get_companies', array($id_inventory, false));
    if ($inv_companies === ENTERPRISE_NOT_HOOK) {
        $inv_companies = array();
    }
    foreach ($inv_companies as $comp) {
        $where_clause = sprintf("WHERE id_company = %d", $comp["id"]);
        $contacts = crm_get_all_contacts($where_clause);
        if (!$contacts) {
            $contacts = array();
        }
        foreach ($contacts as $contact) {
            $all_contacts[$contact['id']] = $contact;
        }
    }
    $contracts = get_inventory_contracts($id_inventory, false);
    if ($contracts === false) {
        return array();
    }
    foreach ($contracts as $contract) {
        $company = get_company($contract['id_company']);
        if ($company === false) {
            continue;
        }
        if (!give_acl($config['id_user'], $contract['id_group'], "IR")) {
            continue;
        }
        $contacts = get_company_contacts($company['id'], false);
        foreach ($contacts as $contact) {
            if (isset($all_contacts[$contact['id']])) {
                continue;
            }
            $all_contacts[$contact['id']] = $contact;
        }
    }
    if (!$only_names) {
        return $all_contacts;
    }
    $retval = array();
    foreach ($all_contacts as $contact) {
        $retval[$contact['id']] = $contact['fullname'];
    }
    return $retval;
}