getAlertDate() static public method

static public getAlertDate ( $itemtype = '', $items_id = '', $type = '' )
$itemtype (default '')
$items_id (default '')
$type (default '')
Exemplo n.º 1
0
 public function testAddDelete()
 {
     $alert = new Alert();
     $nb = countElementsInTable($alert->getTable());
     $comp = getItemByTypeName('Computer', '_test_pc01');
     $date = '2016-09-01 12:34:56';
     // Add
     $id = $alert->add(['itemtype' => $comp->getType(), 'items_id' => $comp->getID(), 'type' => Alert::END, 'date' => $date]);
     $this->assertGreaterThan(0, $id);
     $this->assertGreaterThan($nb, countElementsInTable($alert->getTable()));
     // Getters
     $this->assertFalse(Alert::alertExists($comp->getType(), $comp->getID(), Alert::NOTICE));
     $this->assertEquals($id, Alert::alertExists($comp->getType(), $comp->getID(), Alert::END));
     $this->assertEquals($date, Alert::getAlertDate($comp->getType(), $comp->getID(), Alert::END));
     // Delete
     $this->assertTrue($alert->clear($comp->getType(), $comp->getID(), Alert::END));
     $this->assertEquals($nb, countElementsInTable($alert->getTable()));
     // Still true, nothing to delete but no error
     $this->assertTrue($alert->clear($comp->getType(), $comp->getID(), Alert::END));
 }
Exemplo n.º 2
0
 /**
  * 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;
 }