$date_max = CValue::get("_date_max");
$page = (int) CValue::get("page", 0);
$where = array();
if ($status) {
    $where["status"] = "= '{$status}'";
}
if ($error) {
    $where["error"] = "LIKE '%{$error}%'";
}
if ($cronjob_id) {
    $where["cronjob_id"] = "= '{$cronjob_id}'";
}
if ($date_min) {
    $where["start_datetime"] = ">= '{$date_min}'";
}
if ($date_max) {
    $where["start_datetime"] = $date_min ? $where["start_datetime"] . "AND start_datetime <= '{$date_max}'" : "<= '{$date_max}'";
}
$log = new CCronJobLog();
/** @var CCronJobLog[] $logs */
$nb_log = $log->countList($where);
$logs = $log->loadList($where, "start_datetime DESC", "{$page}, 30");
$cronjobs = CCronJobLog::massLoadFwdRef($logs, "cronjob_id");
foreach ($logs as $_log) {
    $_log->_ref_cronjob = CMbArray::get($cronjobs, $_log->cronjob_id);
}
$smarty = new CSmartyDP();
$smarty->assign("logs", $logs);
$smarty->assign("page", $page);
$smarty->assign("nb_log", $nb_log);
$smarty->display("inc_cronjobs_logs.tpl");
$do_purge = CValue::get("do_purge");
$date_max = CValue::get("_date_max");
$months = CValue::get("months");
$max = CValue::get("max", 1);
$delete = CValue::get("delete");
if ($months) {
    $date_max = CMbDT::date("- {$months} MONTHS");
}
if (!$date_max) {
    CAppUI::stepAjax("Merci d'indiquer une date fin de recherche.", UI_MSG_ERROR);
}
$cronjob_log = new CCronJobLog();
$ds = $cronjob_log->_spec->ds;
// comptage des echanges à supprimer
$count_delete = 0;
$date_max_delete = $delete ? CMbDT::date("-6 MONTHS", $date_max) : $date_max;
$where = array();
$where["start_datetime"] = "< '{$date_max_delete}'";
$count_to_delete = $cronjob_log->countList($where);
CAppUI::stepAjax("{$cronjob_log->_class}-msg-delete_count", UI_MSG_OK, $count_to_delete);
if (!$do_purge) {
    return;
}
$query = "DELETE FROM `{$cronjob_log->_spec->table}`\r\n  WHERE `start_datetime` < '{$date_max_delete}'\r\n  LIMIT {$max}";
$ds->exec($query);
$count_delete = $ds->affectedRows();
CAppUI::stepAjax("{$cronjob_log->_class}-msg-deleted_count", UI_MSG_OK, $count_delete);
// on continue si on est en auto
if ($count_to_delete + $count_delete) {
    echo "<script type='text/javascript'>CronJob.purge();</script>";
}