<?php

/**
 * $Id$
 *
 * @package    Mediboard
 * @subpackage Patients
 * @author     SARL OpenXtrem <*****@*****.**>
 * @license    GNU General Public License, see http://www.gnu.org/licenses/gpl.html
 * @version    $Revision$
 */
CCanDo::checkRead();
$patient_id = CValue::get("patient_id");
$type = CValue::get("type");
$nom = @$_POST["{$type}"];
$corresp = new CCorrespondantPatient();
$where = array();
$where[] = "`nom` LIKE '%{$nom}%' OR `surnom` LIKE '%{$nom}%'";
$where["patient_id"] = " = '{$patient_id}'";
$correspondants = $corresp->loadList($where, "nom");
// Création du template
$smarty = new CSmartyDP();
$smarty->assign("nom", $nom);
$smarty->assign("correspondants", $correspondants);
$smarty->display("ajax_correspondant_autocomplete.tpl");
    $where["surnom"] = $ds->prepareLike("%{$corres_surnom}");
}
if ($corres_relation) {
    $where["relation"] = " = '{$corres_relation}'";
}
if ($corres_cp && $corres_cp != "00") {
    $cps = preg_split("/\\s*[\\s\\|,]\\s*/", $corres_cp);
    CMbArray::removeValue("", $cps);
    $where_cp = array();
    foreach ($cps as $cp) {
        $where_cp[] = "cp LIKE '" . $cp . "%'";
    }
    $where[] = implode(" OR ", $where_cp);
}
if ($corres_ville) {
    $where["ville"] = $ds->prepareLike("%{$corres_ville}%");
}
$order = "surnom, nom";
$nb_correspondants = $correspondant->countList($where);
/** @var CCorrespondantPatient[] $correspondants */
$correspondants = $correspondant->loadList($where, $order, "{$start_corres}, {$step_corres}");
foreach ($correspondants as $_corresp) {
    $_corresp->loadRefFunction();
}
$smarty = new CSmartyDP();
$smarty->assign("is_admin", $is_admin);
$smarty->assign("nb_correspondants", $nb_correspondants);
$smarty->assign("correspondants", $correspondants);
$smarty->assign("start_corres", $start_corres);
$smarty->assign("step_corres", $step_corres);
$smarty->display("inc_list_correspondants_modele.tpl");