SoftwareLicense::cronSoftware PHP Méthode

cronSoftware() static public méthode

Cron action on softwares : alert on expired licences
static public cronSoftware ( $task = NULL )
$task to log, if NULL display (default NULL)
    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;
    }