get all entities with a notification option set
manage CONFIG_PARENT (or NULL) value
static public getEntitiesToNotify ( $field ) : Array | ||
$field | String name of the field to search (>0) | |
return | Array | of id => value |
/** * Cron action on reservation : alert on end of reservations * * @param $task to log, if NULL use display (default NULL) * * @return 0 : nothing to do 1 : done with success **/ static function cronReservation($task = NULL) { global $DB, $CFG_GLPI; if (!$CFG_GLPI["use_mailing"]) { return 0; } $message = array(); $cron_status = 0; $items_infos = array(); $items_messages = array(); foreach (Entity::getEntitiesToNotify('use_reservations_alert') as $entity => $value) { $secs = $value * HOUR_TIMESTAMP; // Reservation already begin and reservation ended in $value hours $query_end = "SELECT `glpi_reservationitems`.*,\n `glpi_reservations`.`end` AS `end`,\n `glpi_reservations`.`id` AS `resaid`\n FROM `glpi_reservations`\n LEFT JOIN `glpi_alerts`\n ON (`glpi_reservations`.`id` = `glpi_alerts`.`items_id`\n AND `glpi_alerts`.`itemtype` = 'Reservation'\n AND `glpi_alerts`.`type` = '" . Alert::END . "')\n LEFT JOIN `glpi_reservationitems`\n ON (`glpi_reservations`.`reservationitems_id`\n = `glpi_reservationitems`.`id`)\n WHERE `glpi_reservationitems`.`entities_id` = '{$entity}'\n AND (UNIX_TIMESTAMP(`glpi_reservations`.`end`) - {$secs}) < UNIX_TIMESTAMP()\n AND `glpi_reservations`.`begin` < NOW()\n AND `glpi_alerts`.`date` IS NULL"; foreach ($DB->request($query_end) as $data) { if ($item_resa = getItemForItemtype($data['itemtype'])) { if ($item_resa->getFromDB($data["items_id"])) { $data['item_name'] = $item_resa->getName(); $data['entity'] = $entity; $items_infos[$entity][$data['resaid']] = $data; if (!isset($items_messages[$entity])) { $items_messages[$entity] = __('Device reservations expiring today') . "<br>"; } $items_messages[$entity] .= sprintf(__('%1$s - %2$s'), $item_resa->getTypeName(), $item_resa->getName()) . "<br>"; } } } } foreach ($items_infos as $entity => $items) { $resitem = new self(); if (NotificationEvent::raiseEvent("alert", new Reservation(), array('entities_id' => $entity, 'items' => $items))) { $message = $items_messages[$entity]; $cron_status = 1; if ($task) { $task->addVolume(1); $task->log(sprintf(__('%1$s: %2$s') . "\n", Dropdown::getDropdownName("glpi_entities", $entity), $message)); } else { //TRANS: %1$s is a name, %2$s is text of message Session::addMessageAfterRedirect(sprintf(__('%1$s: %2$s'), Dropdown::getDropdownName("glpi_entities", $entity), $message)); } $alert = new Alert(); $input["itemtype"] = 'Reservation'; $input["type"] = Alert::END; foreach ($items as $resaid => $item) { $input["items_id"] = $resaid; $alert->add($input); unset($alert->fields['id']); } } else { $entityname = Dropdown::getDropdownName('glpi_entities', $entity); //TRANS: %s is entity name $msg = sprintf(__('%1$s: %2$s'), $entityname, __('Send reservation alert failed')); if ($task) { $task->log($msg); } else { Session::addMessageAfterRedirect($msg, false, ERROR); } } } return $cron_status; }
/** * Cron for alert old tickets which are not solved * * @param $task : crontask object * * @return integer (0 : nothing done - 1 : done) **/ static function cronAlertNotClosed($task) { global $DB, $CFG_GLPI; if (!$CFG_GLPI["use_mailing"]) { return 0; } // Recherche des entités $tot = 0; foreach (Entity::getEntitiesToNotify('notclosed_delay') as $entity => $value) { $query = "SELECT `glpi_tickets`.*\n FROM `glpi_tickets`\n WHERE `glpi_tickets`.`entities_id` = '" . $entity . "'\n AND `glpi_tickets`.`is_deleted` = 0\n AND `glpi_tickets`.`status` IN ('" . self::INCOMING . "',\n '" . self::ASSIGNED . "',\n '" . self::PLANNED . "',\n '" . self::WAITING . "')\n AND `glpi_tickets`.`closedate` IS NULL\n AND ADDDATE(`glpi_tickets`.`date`, INTERVAL " . $value . " DAY) < NOW()"; $tickets = array(); foreach ($DB->request($query) as $tick) { $tickets[] = $tick; } if (!empty($tickets)) { if (NotificationEvent::raiseEvent('alertnotclosed', new self(), array('items' => $tickets, 'entities_id' => $entity))) { $tot += count($tickets); $task->addVolume(count($tickets)); $task->log(sprintf(__('%1$s: %2$s'), Dropdown::getDropdownName('glpi_entities', $entity), count($tickets))); } } } return $tot > 0; }
/** * Cron action on softwares : alert on expired licences * * @param $task to log, if NULL display (default NULL) * * @return 0 : nothing to do 1 : done with success **/ static function cronSoftware($task = NULL) { global $DB, $CFG_GLPI; $cron_status = 1; if (!$CFG_GLPI['use_mailing']) { return 0; } $message = array(); $items_notice = array(); $items_end = array(); foreach (Entity::getEntitiesToNotify('use_licenses_alert') as $entity => $value) { $before = Entity::getUsedConfig('send_licenses_alert_before_delay', $entity); // Check licenses $query = "SELECT `glpi_softwarelicenses`.*,\n `glpi_softwares`.`name` AS softname\n FROM `glpi_softwarelicenses`\n INNER JOIN `glpi_softwares`\n ON (`glpi_softwarelicenses`.`softwares_id` = `glpi_softwares`.`id`)\n LEFT JOIN `glpi_alerts`\n ON (`glpi_softwarelicenses`.`id` = `glpi_alerts`.`items_id`\n AND `glpi_alerts`.`itemtype` = 'SoftwareLicense'\n AND `glpi_alerts`.`type` = '" . Alert::END . "')\n WHERE `glpi_alerts`.`date` IS NULL\n AND `glpi_softwarelicenses`.`expire` IS NOT NULL\n AND DATEDIFF(`glpi_softwarelicenses`.`expire`,\n CURDATE()) < '{$before}'\n AND `glpi_softwares`.`is_template` = '0'\n AND `glpi_softwares`.`is_deleted` = '0'\n AND `glpi_softwares`.`entities_id` = '" . $entity . "'"; $message = ""; $items = array(); foreach ($DB->request($query) as $license) { $name = $license['softname'] . ' - ' . $license['name'] . ' - ' . $license['serial']; //TRANS: %1$s the license name, %2$s is the expiration date $message .= sprintf(__('License %1$s expired on %2$s'), Html::convDate($license["expire"]), $name) . "<br>\n"; $items[$license['id']] = $license; } if (!empty($items)) { $alert = new Alert(); $options['entities_id'] = $entity; $options['licenses'] = $items; if (NotificationEvent::raiseEvent('alert', new self(), $options)) { $entityname = Dropdown::getDropdownName("glpi_entities", $entity); if ($task) { //TRANS: %1$s is the entity, %2$s is the message $task->log(sprintf(__('%1$s: %2$s') . "\n", $entityname, $message)); $task->addVolume(1); } else { Session::addMessageAfterRedirect(sprintf(__('%1$s: %2$s'), $entityname, $message)); } $input["type"] = Alert::END; $input["itemtype"] = 'SoftwareLicense'; // add alerts foreach ($items as $ID => $consumable) { $input["items_id"] = $ID; $alert->add($input); unset($alert->fields['id']); } } else { $entityname = Dropdown::getDropdownName('glpi_entities', $entity); //TRANS: %s is entity name $msg = sprintf(__('%1$s: %2$s'), $entityname, __('Send licenses alert failed')); if ($task) { $task->log($msg); } else { Session::addMessageAfterRedirect($msg, false, ERROR); } } } } return $cron_status; }
/** * Cron for alert old tickets which are not solved * * @param $task : crontask object * * @return integer (0 : nothing done - 1 : done) **/ static function cronAlertNotClosed($task) { global $DB, $CFG_GLPI; if (!$CFG_GLPI["use_mailing"]) { return 0; } // Recherche des entités $tot = 0; foreach (Entity::getEntitiesToNotify('notclosed_delay') as $entity => $value) { /* $query = "SELECT `glpi_tickets`.* FROM `glpi_tickets` LEFT JOIN `glpi_alerts` ON (`glpi_tickets`.`id` = `glpi_alerts`.`items_id` AND `glpi_alerts`.`itemtype` = 'Ticket' AND `glpi_alerts`.`type`='".Alert::NOTCLOSED."') WHERE `glpi_tickets`.`entities_id` = '".$entity."' AND `glpi_tickets`.`status` IN ('new','assign','plan','waiting') AND `glpi_tickets`.`closedate` IS NULL AND ADDDATE(`glpi_tickets`.`date`, INTERVAL ".$value." DAY) < CURDATE() AND `glpi_alerts`.`date` IS NULL";*/ $query = "SELECT `glpi_tickets`.*\n FROM `glpi_tickets`\n WHERE `glpi_tickets`.`entities_id` = '" . $entity . "'\n AND `glpi_tickets`.`status` IN ('new','assign','plan','waiting')\n AND `glpi_tickets`.`closedate` IS NULL\n AND ADDDATE(`glpi_tickets`.`date`, INTERVAL " . $value . " DAY) < CURDATE()"; $tickets = array(); foreach ($DB->request($query) as $tick) { $tickets[] = $tick; } if (!empty($tickets)) { if (NotificationEvent::raiseEvent('alertnotclosed', new self(), array('tickets' => $tickets, 'entities_id' => $entity))) { // To be clean : do not mark ticket as already send : always send all // $alert = new Alert(); // $input["itemtype"] = 'Ticket'; // $input["type"] = Alert::NOTCLOSED; // foreach ($tickets as $ticket) { // $input["items_id"] = $ticket['id']; // $alert->add($input); // unset($alert->fields['id']); // } // To be clean : do not mark ticket as already send : always send all // $alert = new Alert(); // $input["itemtype"] = 'Ticket'; // $input["type"] = Alert::NOTCLOSED; // foreach ($tickets as $ticket) { // $input["items_id"] = $ticket['id']; // $alert->add($input); // unset($alert->fields['id']); // } $tot += count($tickets); $task->addVolume(count($tickets)); $task->log(Dropdown::getDropdownName('glpi_entities', $entity) . " : " . count($tickets)); } } } return $tot > 0; }
/** * Cron action on infocom : alert on expired warranty * * @param $task to log, if NULL use display (default NULL) * * @return 0 : nothing to do 1 : done with success **/ static function cronInfocom($task = NULL) { global $DB, $CFG_GLPI; if (!$CFG_GLPI["use_mailing"]) { return 0; } $message = array(); $cron_status = 0; $items_infos = array(); $items_messages = array(); foreach (Entity::getEntitiesToNotify('use_infocoms_alert') as $entity => $value) { $before = Entity::getUsedConfig('send_infocoms_alert_before_delay', $entity); $query_end = "SELECT `glpi_infocoms`.*\n FROM `glpi_infocoms`\n LEFT JOIN `glpi_alerts` ON (`glpi_infocoms`.`id` = `glpi_alerts`.`items_id`\n AND `glpi_alerts`.`itemtype` = 'Infocom'\n AND `glpi_alerts`.`type`='" . Alert::END . "')\n WHERE (`glpi_infocoms`.`alert` & " . pow(2, Alert::END) . ") >'0'\n AND `glpi_infocoms`.`entities_id`='" . $entity . "'\n AND `glpi_infocoms`.`warranty_duration`>'0'\n AND `glpi_infocoms`.`warranty_date` IS NOT NULL\n AND DATEDIFF(ADDDATE(`glpi_infocoms`.`warranty_date`,\n INTERVAL (`glpi_infocoms`.`warranty_duration`)\n MONTH),\n CURDATE() ) < '{$before}'\n AND `glpi_alerts`.`date` IS NULL"; foreach ($DB->request($query_end) as $data) { if ($item_infocom = getItemForItemtype($data["itemtype"])) { if ($item_infocom->getFromDB($data["items_id"])) { $entity = $data['entities_id']; $warranty = self::getWarrantyExpir($data["warranty_date"], $data["warranty_duration"]); //TRANS: %1$s is a type, %2$s is a name (used in croninfocom) $name = sprintf(__('%1$s - %2$s'), $item_infocom->getTypeName(1), $item_infocom->getName()); //TRANS: %1$s is the warranty end date and %2$s the name of the item $message = sprintf(__('Item reaching the end of warranty on %1$s: %2$s'), $warranty, $name) . "<br>"; $data['warrantyexpiration'] = $warranty; $data['item_name'] = $item_infocom->getName(); $items_infos[$entity][$data['id']] = $data; if (!isset($items_messages[$entity])) { $items_messages[$entity] = __('No item reaching the end of warranty.') . "<br>"; } $items_messages[$entity] .= $message; } } } } foreach ($items_infos as $entity => $items) { if (NotificationEvent::raiseEvent("alert", new self(), array('entities_id' => $entity, 'items' => $items))) { $message = $items_messages[$entity]; $cron_status = 1; if ($task) { $task->log(sprintf(__('%1$s: %2$s') . "\n", Dropdown::getDropdownName("glpi_entities", $entity), $message)); $task->addVolume(1); } else { Session::addMessageAfterRedirect(sprintf(__('%1$s: %2$s'), Dropdown::getDropdownName("glpi_entities", $entity), $message)); } $alert = new Alert(); $input["itemtype"] = 'Infocom'; $input["type"] = Alert::END; foreach ($items as $id => $item) { $input["items_id"] = $id; $alert->add($input); unset($alert->fields['id']); } } else { $entityname = Dropdown::getDropdownName('glpi_entities', $entity); //TRANS: %s is entity name $msg = sprintf(__('%1$s: %2$s'), $entityname, __('send infocom alert failed')); if ($task) { $task->log($msg); } else { Session::addMessageAfterRedirect($msg, false, ERROR); } } } return $cron_status; }
/** * Cron action on consumables : alert if a stock is behind the threshold * * @param $task to log, if NULL display (default NULL) * * @return 0 : nothing to do 1 : done with success **/ static function cronConsumable($task = NULL) { global $DB, $CFG_GLPI; $cron_status = 1; if ($CFG_GLPI["use_mailing"]) { $message = array(); $items = array(); $alert = new Alert(); foreach (Entity::getEntitiesToNotify('consumables_alert_repeat') as $entity => $repeat) { $query_alert = "SELECT `glpi_consumableitems`.`id` AS consID,\n `glpi_consumableitems`.`entities_id` AS entity,\n `glpi_consumableitems`.`ref` AS ref,\n `glpi_consumableitems`.`name` AS name,\n `glpi_consumableitems`.`alarm_threshold` AS threshold,\n `glpi_alerts`.`id` AS alertID,\n `glpi_alerts`.`date`\n FROM `glpi_consumableitems`\n LEFT JOIN `glpi_alerts`\n ON (`glpi_consumableitems`.`id` = `glpi_alerts`.`items_id`\n AND `glpi_alerts`.`itemtype`='ConsumableItem')\n WHERE `glpi_consumableitems`.`is_deleted` = '0'\n AND `glpi_consumableitems`.`alarm_threshold` >= '0'\n AND `glpi_consumableitems`.`entities_id` = '" . $entity . "'\n AND (`glpi_alerts`.`date` IS NULL\n OR (`glpi_alerts`.date+{$repeat}) < CURRENT_TIMESTAMP());"; $message = ""; $items = array(); foreach ($DB->request($query_alert) as $consumable) { if (($unused = Consumable::getUnusedNumber($consumable["consID"])) <= $consumable["threshold"]) { // define message alert //TRANS: %1$s is the consumable name, %2$s its reference, %3$d the remaining number $message .= sprintf(__('Threshold of alarm reached for the type of consumable: %1$s - Reference %2$s - Remaining %3$d'), $consumable['name'], $consumable['ref'], $unused); $message .= '<br>'; $items[$consumable["consID"]] = $consumable; // if alert exists -> delete if (!empty($consumable["alertID"])) { $alert->delete(array("id" => $consumable["alertID"])); } } } if (!empty($items)) { $options['entities_id'] = $entity; $options['items'] = $items; if (NotificationEvent::raiseEvent('alert', new ConsumableItem(), $options)) { if ($task) { $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . " : {$message}\n"); $task->addVolume(1); } else { Session::addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . " : {$message}"); } $input["type"] = Alert::THRESHOLD; $input["itemtype"] = 'ConsumableItem'; // add alerts foreach ($items as $ID => $consumable) { $input["items_id"] = $ID; $alert->add($input); unset($alert->fields['id']); } } else { $entityname = Dropdown::getDropdownName('glpi_entities', $entity); //TRANS: %s is entity name $msg = sprintf(__('%s: send consumable alert failed'), $entityname); if ($task) { $task->log($msg); } else { Session::addMessageAfterRedirect($msg, false, ERROR); } } } } } return $cron_status; }
/** * Cron action on contracts : alert depending of the config : on notice and expire * * @param $task for log, if NULL display (default NULL) **/ static function cronContract($task = NULL) { global $DB, $CFG_GLPI; if (!$CFG_GLPI["use_mailing"]) { return 0; } $message = array(); $items_notice = array(); $items_end = array(); $cron_status = 0; $contract_infos[Alert::END] = array(); $contract_infos[Alert::NOTICE] = array(); $contract_messages = array(); foreach (Entity::getEntitiesToNotify('use_contracts_alert') as $entity => $value) { $before = Entity::getUsedConfig('send_contracts_alert_before_delay', $entity); $query_notice = "SELECT `glpi_contracts`.*\n FROM `glpi_contracts`\n LEFT JOIN `glpi_alerts`\n ON (`glpi_contracts`.`id` = `glpi_alerts`.`items_id`\n AND `glpi_alerts`.`itemtype` = 'Contract'\n AND `glpi_alerts`.`type`='" . Alert::NOTICE . "')\n WHERE (`glpi_contracts`.`alert` & " . pow(2, Alert::NOTICE) . ") >'0'\n AND `glpi_contracts`.`is_deleted` = '0'\n AND `glpi_contracts`.`begin_date` IS NOT NULL\n AND `glpi_contracts`.`duration` <> '0'\n AND `glpi_contracts`.`notice` <> '0'\n AND DATEDIFF(ADDDATE(`glpi_contracts`.`begin_date`,\n INTERVAL `glpi_contracts`.`duration` MONTH),\n CURDATE()) > '0'\n AND DATEDIFF(ADDDATE(`glpi_contracts`.`begin_date`,\n INTERVAL (`glpi_contracts`.`duration`\n -`glpi_contracts`.`notice`) MONTH),\n CURDATE()) < '{$before}'\n AND `glpi_alerts`.`date` IS NULL\n AND `glpi_contracts`.`entities_id` = '" . $entity . "'"; $query_end = "SELECT `glpi_contracts`.*\n FROM `glpi_contracts`\n LEFT JOIN `glpi_alerts`\n ON (`glpi_contracts`.`id` = `glpi_alerts`.`items_id`\n AND `glpi_alerts`.`itemtype` = 'Contract'\n AND `glpi_alerts`.`type`='" . Alert::END . "')\n WHERE (`glpi_contracts`.`alert` & " . pow(2, Alert::END) . ") > '0'\n AND `glpi_contracts`.`is_deleted` = '0'\n AND `glpi_contracts`.`begin_date` IS NOT NULL\n AND `glpi_contracts`.`duration` <> '0'\n AND DATEDIFF(ADDDATE(`glpi_contracts`.`begin_date`,\n INTERVAL (`glpi_contracts`.`duration`) MONTH),\n CURDATE()) < '{$before}'\n AND `glpi_alerts`.`date` IS NULL\n AND `glpi_contracts`.`entities_id` = '" . $entity . "'"; $querys = array('notice' => $query_notice, 'end' => $query_end); foreach ($querys as $type => $query) { foreach ($DB->request($query) as $data) { $entity = $data['entities_id']; $message = sprintf(__('%1$s: %2$s') . "<br>\n", $data["name"], Infocom::getWarrantyExpir($data["begin_date"], $data["duration"], $data["notice"])); $contract_infos[$type][$entity][$data['id']] = $data; if (!isset($contract_messages[$type][$entity])) { switch ($type) { case 'notice': $contract_messages[$type][$entity] = __('Contract entered in notice time') . "<br>"; break; case 'end': $contract_messages[$type][$entity] = __('Contract ended') . "<br>"; break; } } $contract_messages[$type][$entity] .= $message; } } // Get contrats with periodicity alerts $query_periodicity = "SELECT `glpi_contracts`.*\n FROM `glpi_contracts`\n WHERE `glpi_contracts`.`alert` & " . pow(2, Alert::PERIODICITY) . " > '0'\n AND `glpi_contracts`.`entities_id` = '" . $entity . "' "; // Foreach ones : foreach ($DB->request($query_periodicity) as $data) { $entity = $data['entities_id']; // Compute end date + 12 month : do not send alerts after $end_alert = date('Y-m-d', strtotime($data['begin_date'] . " +" . ($data['duration'] + 12) . " month")); if (!empty($data['begin_date']) && $data['periodicity'] && $end_alert > date('Y-m-d')) { $todo = array('periodicity' => Alert::PERIODICITY); if ($data['alert'] & pow(2, Alert::NOTICE)) { $todo['periodicitynotice'] = Alert::NOTICE; } // Get previous alerts foreach ($todo as $type => $event) { $previous_alerts[$type] = Alert::getAlertDate(__CLASS__, $data['id'], $event); } // compute next alert date based on already send alerts (or not) foreach ($todo as $type => $event) { $next_alerts[$type] = date('Y-m-d', strtotime($data['begin_date'] . " -" . $before . " day")); if ($type == Alert::NOTICE) { $next_alerts[$type] = date('Y-m-d', strtotime($next_alerts[$type] . " -" . $data['notice'] . " month")); } $today_limit = date('Y-m-d', strtotime(date('Y-m-d') . " -" . $data['periodicity'] . " month")); // Init previous by begin date if not set if (empty($previous_alerts[$type])) { $previous_alerts[$type] = $today_limit; } while ($next_alerts[$type] < $previous_alerts[$type] && $next_alerts[$type] < $end_alert) { $next_alerts[$type] = date('Y-m-d', strtotime($next_alerts[$type] . " +" . $data['periodicity'] . " month")); } // If this date is passed : clean alerts and send again if ($next_alerts[$type] <= date('Y-m-d')) { $alert = new Alert(); $alert->clear(__CLASS__, $data['id'], $event); $real_alert_date = date('Y-m-d', strtotime($next_alerts[$type] . " +" . $before . " day")); $message = sprintf(__('%1$s: %2$s') . "<br>\n", $data["name"], Html::convDate($real_alert_date)); $data['alert_date'] = $real_alert_date; $data['items'] = Contract_Item::getItemsForContract($data['id'], $entity); $contract_infos[$type][$entity][$data['id']] = $data; switch ($type) { case 'periodicitynotice': $contract_messages[$type][$entity] = __('Contract entered in notice time for period') . "<br>"; break; case 'periodicity': $contract_messages[$type][$entity] = __('Contract period ended') . "<br>"; break; } $contract_messages[$type][$entity] .= $message; } } } } } foreach (array('notice' => Alert::NOTICE, 'end' => Alert::END, 'periodicity' => Alert::PERIODICITY, 'periodicitynotice' => Alert::NOTICE) as $event => $type) { if (isset($contract_infos[$event]) && count($contract_infos[$event])) { foreach ($contract_infos[$event] as $entity => $contracts) { if (NotificationEvent::raiseEvent($event, new self(), array('entities_id' => $entity, 'items' => $contracts))) { $message = $contract_messages[$event][$entity]; $cron_status = 1; $entityname = Dropdown::getDropdownName("glpi_entities", $entity); if ($task) { $task->log(sprintf(__('%1$s: %2$s') . "\n", $entityname, $message)); $task->addVolume(1); } else { Session::addMessageAfterRedirect(sprintf(__('%1$s: %2$s'), $entityname, $message)); } $alert = new Alert(); $input["itemtype"] = __CLASS__; $input["type"] = $type; foreach ($contracts as $id => $contract) { $input["items_id"] = $id; $alert->add($input); unset($alert->fields['id']); } } else { $entityname = Dropdown::getDropdownName('glpi_entities', $entity); //TRANS: %1$s is entity name, %2$s is the message $msg = sprintf(__('%1$s: %2$s'), $entityname, __('send contract alert failed')); if ($task) { $task->log($msg); } else { Session::addMessageAfterRedirect($msg, false, ERROR); } } } } } return $cron_status; }
/** * Cron action on softwares : alert on expired licences * * @param $task to log, if NULL display * * @return 0 : nothing to do 1 : done with success **/ static function cronSoftware($task = NULL) { global $DB, $CFG_GLPI, $LANG; $cron_status = 1; if (!$CFG_GLPI['use_mailing']) { return 0; } $message = array(); $items_notice = array(); $items_end = array(); foreach (Entity::getEntitiesToNotify('use_licenses_alert') as $entity => $value) { // Check licenses $query = "SELECT `glpi_softwarelicenses`.*,\n `glpi_softwares`.`name` AS softname\n FROM `glpi_softwarelicenses`\n INNER JOIN `glpi_softwares`\n ON (`glpi_softwarelicenses`.`softwares_id` = `glpi_softwares`.`id`)\n LEFT JOIN `glpi_alerts`\n ON (`glpi_softwarelicenses`.`id` = `glpi_alerts`.`items_id`\n AND `glpi_alerts`.`itemtype` = 'SoftwareLicense'\n AND `glpi_alerts`.`type` = '" . Alert::END . "')\n WHERE `glpi_alerts`.`date` IS NULL\n AND `glpi_softwarelicenses`.`expire` IS NOT NULL\n AND `glpi_softwarelicenses`.`expire` < CURDATE()\n AND `glpi_softwares`.`is_template` = '0'\n AND `glpi_softwares`.`is_deleted` = '0'\n AND `glpi_softwares`.`entities_id` = '" . $entity . "'"; $message = ""; $items = array(); foreach ($DB->request($query) as $license) { $name = $license['softname'] . ' - ' . $license['name'] . ' - ' . $license['serial']; $message .= $LANG['mailing'][51] . " " . $name . ": " . convDate($license["expire"]) . "<br>\n"; $items[$license['id']] = $license; } if (!empty($items)) { $alert = new Alert(); $options['entities_id'] = $entity; $options['licenses'] = $items; if (NotificationEvent::raiseEvent('alert', new SoftwareLicense(), $options)) { if ($task) { $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . " : {$message}\n"); $task->addVolume(1); } else { addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . " : {$message}"); } $input["type"] = Alert::END; $input["itemtype"] = 'SoftwareLicense'; // add alerts foreach ($items as $ID => $consumable) { $input["items_id"] = $ID; $alert->add($input); unset($alert->fields['id']); } } else { if ($task) { $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . " : Send licenses alert failed\n"); } else { addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . " : Send licenses alert failed", false, ERROR); } } } } return $cron_status; }
/** * Cron action on cartridges : alert if a stock is behind the threshold * * @param $task for log, display informations if NULL? * * @return 0 : nothing to do 1 : done with success * **/ static function cronCartridge($task = NULL) { global $DB, $CFG_GLPI, $LANG; $cron_status = 1; if ($CFG_GLPI["use_mailing"]) { $message = array(); $alert = new Alert(); foreach (Entity::getEntitiesToNotify('cartridges_alert_repeat') as $entity => $repeat) { // if you change this query, please don't forget to also change in showDebug() $query_alert = "SELECT `glpi_cartridgeitems`.`id` AS cartID,\n `glpi_cartridgeitems`.`entities_id` AS entity,\n `glpi_cartridgeitems`.`ref` AS cartref,\n `glpi_cartridgeitems`.`name` AS cartname,\n `glpi_cartridgeitems`.`alarm_threshold` AS threshold,\n `glpi_alerts`.`id` AS alertID,\n `glpi_alerts`.`date`\n FROM `glpi_cartridgeitems`\n LEFT JOIN `glpi_alerts`\n ON (`glpi_cartridgeitems`.`id` = `glpi_alerts`.`items_id`\n AND `glpi_alerts`.`itemtype` = 'CartridgeItem')\n WHERE `glpi_cartridgeitems`.`is_deleted` = '0'\n AND `glpi_cartridgeitems`.`alarm_threshold` >= '0'\n AND `glpi_cartridgeitems`.`entities_id` = '" . $entity . "'\n AND (`glpi_alerts`.`date` IS NULL\n OR (`glpi_alerts`.date+{$repeat}) < CURRENT_TIMESTAMP());"; $message = ""; $items = array(); foreach ($DB->request($query_alert) as $cartridge) { if (($unused = Cartridge::getUnusedNumber($cartridge["cartID"])) <= $cartridge["threshold"]) { // define message alert $message .= $LANG['mailing'][35] . " " . $cartridge["cartname"] . " - " . $LANG['consumables'][2] . " : " . $cartridge["cartref"] . " - " . $LANG['software'][20] . " : " . $unused . "<br>"; $items[$cartridge["cartID"]] = $cartridge; // if alert exists -> delete if (!empty($cartridge["alertID"])) { $alert->delete(array("id" => $cartridge["alertID"])); } } } if (!empty($items)) { $options['entities_id'] = $entity; $options['cartridges'] = $items; if (NotificationEvent::raiseEvent('alert', new Cartridge(), $options)) { if ($task) { $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . " : {$message}\n"); $task->addVolume(1); } else { addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . " : {$message}"); } $input["type"] = Alert::THRESHOLD; $input["itemtype"] = 'CartridgeItem'; // add alerts foreach ($items as $ID => $consumable) { $input["items_id"] = $ID; $alert->add($input); unset($alert->fields['id']); } } else { if ($task) { $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . " : Send cartidge alert failed\n"); } else { addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . " : Send cartidge alert failed", false, ERROR); } } } } } }
/** * Cron action on infocom : alert on expired warranty * * @param $task to log, if NULL use display * * @return 0 : nothing to do 1 : done with success **/ static function cronInfocom($task = NULL) { global $DB, $CFG_GLPI, $LANG; if (!$CFG_GLPI["use_mailing"]) { return 0; } $message = array(); $cron_status = 0; $items_infos = array(); $items_messages = array(); foreach (Entity::getEntitiesToNotify('use_infocoms_alert') as $entity => $value) { $query_end = "SELECT `glpi_infocoms`.*\n FROM `glpi_infocoms`\n LEFT JOIN `glpi_alerts` ON (`glpi_infocoms`.`id` = `glpi_alerts`.`items_id`\n AND `glpi_alerts`.`itemtype` = 'Infocom'\n AND `glpi_alerts`.`type`='" . Alert::END . "')\n WHERE (`glpi_infocoms`.`alert` & " . pow(2, Alert::END) . ") >'0'\n AND `glpi_infocoms`.`entities_id`='" . $entity . "'\n AND `glpi_infocoms`.`warranty_duration`>'0'\n AND `glpi_infocoms`.`warranty_date` IS NOT NULL\n AND DATEDIFF(ADDDATE(`glpi_infocoms`.`warranty_date`,\n INTERVAL (`glpi_infocoms`.`warranty_duration`) MONTH),\n CURDATE() )<'0'\n AND `glpi_alerts`.`date` IS NULL"; foreach ($DB->request($query_end) as $data) { $item_infocom = new $data["itemtype"](); if ($item_infocom->getFromDB($data["items_id"])) { $entity = $data['entities_id']; $warranty = getWarrantyExpir($data["warranty_date"], $data["warranty_duration"]); $message = $LANG['mailing'][40] . " " . $item_infocom->getTypeName() . " - " . $item_infocom->getName() . " : " . $warranty . "<br>"; $data['warrantyexpiration'] = $warranty; $data['item_name'] = $item_infocom->getName(); $items_infos[$entity][$data['id']] = $data; if (!isset($items_messages[$entity])) { $items_messages[$entity] = $LANG['mailing'][40] . "<br />"; } $items_messages[$entity] .= $message; } } } foreach ($items_infos as $entity => $items) { if (NotificationEvent::raiseEvent("alert", new self(), array('entities_id' => $entity, 'items' => $items))) { $message = $items_messages[$entity]; $cron_status = 1; if ($task) { $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . ": {$message}\n"); $task->addVolume(1); } else { addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . ": {$message}"); } $alert = new Alert(); $input["itemtype"] = 'Infocom'; $input["type"] = Alert::END; foreach ($items as $id => $item) { $input["items_id"] = $id; $alert->add($input); unset($alert->fields['id']); } } else { if ($task) { $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . ": Send infocom alert failed\n"); } else { addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . ": Send infocom alert failed", false, ERROR); } } } return $cron_status; }
/** * Cron action on reservation : alert on end of reservations * * @param $task to log, if NULL use display * * @return 0 : nothing to do 1 : done with success **/ static function cronReservation($task = NULL) { global $DB, $CFG_GLPI, $LANG; if (!$CFG_GLPI["use_mailing"]) { return 0; } $message = array(); $cron_status = 0; $items_infos = array(); $items_messages = array(); foreach (Entity::getEntitiesToNotify('use_reservations_alert') as $entity => $value) { $secs = $value * HOUR_TIMESTAMP; // Reservation already begin and reservation ended in $value hours $query_end = "SELECT `glpi_reservationitems`.*,\n `glpi_reservations`.`end` AS `end`,\n `glpi_reservations`.`id` AS `resaid`\n FROM `glpi_reservations`\n LEFT JOIN `glpi_alerts`\n ON (`glpi_reservations`.`id` = `glpi_alerts`.`items_id`\n AND `glpi_alerts`.`itemtype` = 'Reservation'\n AND `glpi_alerts`.`type` = '" . Alert::END . "')\n LEFT JOIN `glpi_reservationitems`\n ON (`glpi_reservations`.`reservationitems_id` = `glpi_reservationitems`.`id`)\n WHERE `glpi_reservationitems`.`entities_id` = '{$entity}'\n AND (UNIX_TIMESTAMP(`glpi_reservations`.`end`) - {$secs}) < UNIX_TIMESTAMP()\n AND `glpi_reservations`.`begin` < NOW()\n AND `glpi_alerts`.`date` IS NULL"; foreach ($DB->request($query_end) as $data) { $item_resa = new $data["itemtype"](); if ($item_resa->getFromDB($data["items_id"])) { $message .= $LANG['reservation'][40] . " " . $item_resa->getTypeName() . " - " . $item_resa->getName() . "<br />"; $data['item_name'] = $item_resa->getName(); $data['entity'] = $entity; $items_infos[$entity][$data['resaid']] = $data; if (!isset($items_messages[$entity])) { $items_messages[$entity] = $LANG['reservation'][40] . "<br />"; } $items_messages[$entity] .= $message; } } } foreach ($items_infos as $entity => $items) { $resitem = new ReservationItem(); if (NotificationEvent::raiseEvent("alert", new Reservation(), array('entities_id' => $entity, 'items' => $items))) { $message = $items_messages[$entity]; $cron_status = 1; if ($task) { $task->addVolume(1); $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . ": {$message}\n"); } else { addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . ": {$message}"); } $alert = new Alert(); $input["itemtype"] = 'Reservation'; $input["type"] = Alert::END; foreach ($items as $resaid => $item) { $input["items_id"] = $resaid; $alert->add($input); unset($alert->fields['id']); } } else { if ($task) { $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . ": Send reservationitem alert failed\n"); } else { addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . ": Send reservationitem alert failed", false, ERROR); } } } return $cron_status; }
/** * Cron action on contracts : alert depending of the config : on notice and expire * * @param $task for log, if NULL display * **/ static function cronContract($task = NULL) { global $DB, $CFG_GLPI, $LANG; if (!$CFG_GLPI["use_mailing"]) { return 0; } $message = array(); $items_notice = array(); $items_end = array(); $cron_status = 0; $contract_infos[Alert::END] = array(); $contract_infos[Alert::NOTICE] = array(); $contract_messages = array(); foreach (Entity::getEntitiesToNotify('use_contracts_alert') as $entity => $value) { $query_notice = "SELECT `glpi_contracts`.*\n FROM `glpi_contracts`\n LEFT JOIN `glpi_alerts`\n ON (`glpi_contracts`.`id` = `glpi_alerts`.`items_id`\n AND `glpi_alerts`.`itemtype` = 'Contract'\n AND `glpi_alerts`.`type`='" . Alert::NOTICE . "')\n WHERE (`glpi_contracts`.`alert` & " . pow(2, Alert::NOTICE) . ") >'0'\n AND `glpi_contracts`.`is_deleted` = '0'\n AND `glpi_contracts`.`begin_date` IS NOT NULL\n AND `glpi_contracts`.`duration` <> '0'\n AND `glpi_contracts`.`notice` <> '0'\n AND DATEDIFF(ADDDATE(`glpi_contracts`.`begin_date`,\n INTERVAL `glpi_contracts`.`duration` MONTH),\n CURDATE()) > '0'\n AND DATEDIFF(ADDDATE(`glpi_contracts`.`begin_date`,\n INTERVAL (`glpi_contracts`.`duration`\n -`glpi_contracts`.`notice`) MONTH),\n CURDATE()) < '0'\n AND `glpi_alerts`.`date` IS NULL\n AND `glpi_contracts`.`entities_id` = '" . $entity . "'"; $query_end = "SELECT `glpi_contracts`.*\n FROM `glpi_contracts`\n LEFT JOIN `glpi_alerts`\n ON (`glpi_contracts`.`id` = `glpi_alerts`.`items_id`\n AND `glpi_alerts`.`itemtype` = 'Contract'\n AND `glpi_alerts`.`type`='" . Alert::END . "')\n WHERE (`glpi_contracts`.`alert` & " . pow(2, Alert::END) . ") > '0'\n AND `glpi_contracts`.`is_deleted` = '0'\n AND `glpi_contracts`.`begin_date` IS NOT NULL\n AND `glpi_contracts`.`duration` <> '0'\n AND DATEDIFF(ADDDATE(`glpi_contracts`.`begin_date`,\n INTERVAL (`glpi_contracts`.`duration`) MONTH),\n CURDATE()) < '0'\n AND `glpi_alerts`.`date` IS NULL\n AND `glpi_contracts`.`entities_id` = '" . $entity . "'"; $querys = array(Alert::NOTICE => $query_notice, Alert::END => $query_end); foreach ($querys as $type => $query) { foreach ($DB->request($query) as $data) { $entity = $data['entities_id']; $message = $data["name"] . ": " . getWarrantyExpir($data["begin_date"], $data["duration"], $data["notice"]) . "<br>\n"; $contract_infos[$type][$entity][$data['id']] = $data; if (!isset($contract_messages[$type][$entity])) { $contract_messages[$type][$entity] = $LANG['mailing'][37] . "<br />"; } $contract_messages[$type][$entity] .= $message; } } } foreach (array(Alert::NOTICE => "notice", Alert::END => "end") as $type => $event) { foreach ($contract_infos[$type] as $entity => $contracts) { if (NotificationEvent::raiseEvent($event, new Contract(), array('entities_id' => $entity, 'contracts' => $contracts))) { $message = $contract_messages[$type][$entity]; $cron_status = 1; if ($task) { $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . ": {$message}\n"); $task->addVolume(1); } else { addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . ": {$message}"); } $alert = new Alert(); $input["itemtype"] = 'Contract'; $input["type"] = $type; foreach ($contracts as $id => $contract) { $input["items_id"] = $id; $alert->add($input); unset($alert->fields['id']); } } else { if ($task) { $task->log(Dropdown::getDropdownName("glpi_entities", $entity) . ": Send contract alert failed\n"); } else { addMessageAfterRedirect(Dropdown::getDropdownName("glpi_entities", $entity) . ": Send contract alert failed", false, ERROR); } } } } return $cron_status; }