/**
  * Purge task and taskjob related with method
  *
  * @param $method value name of the method
  *
  * @return nothing
  *
  **/
 static function cleanTasksbyMethod($method)
 {
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $pfTask = new PluginFusioninventoryTask();
     $a_taskjobs = $pfTaskjob->find("`method`='" . $method . "'");
     $task_id = 0;
     foreach ($a_taskjobs as $a_taskjob) {
         $pfTaskjob->delete($a_taskjob, 1);
         if ($task_id != $a_taskjob['plugin_fusioninventory_tasks_id'] and $task_id != '0') {
             // Search if this task have other taskjobs, if not, we will delete it
             $findtaskjobs = $pfTaskjob->find("`plugin_fusioninventory_tasks_id`='" . $task_id . "'");
             if (count($findtaskjobs) == '0') {
                 $pfTask->delete(array('id' => $task_id), 1);
             }
         }
         $task_id = $a_taskjob['plugin_fusioninventory_tasks_id'];
     }
     if ($task_id != '0') {
         // Search if this task have other taskjobs, if not, we will delete it
         $findtaskjobs = $pfTaskjob->find("`plugin_fusioninventory_tasks_id`='" . $task_id . "'");
         if (count($findtaskjobs) == '0') {
             $pfTask->delete(array('id' => $task_id), 1);
         }
     }
 }
 public function submitForm($postvars)
 {
     if (isset($postvars['forcestart'])) {
         Session::checkRight('plugin_fusioninventory_task', UPDATE);
         /**
          * TODO: forcing the task execution should be done in the task object
          */
         $pfTaskjob = new PluginFusioninventoryTaskjob();
         $pfTaskjob->forceRunningTask($postvars['id']);
         Html::back();
     } else {
         if (isset($postvars["add"])) {
             Session::checkRight('plugin_fusioninventory_task', CREATE);
             $items_id = $this->add($postvars);
             Html::redirect(str_replace("add=1", "", $_SERVER['HTTP_REFERER']) . "?id=" . $items_id);
         } else {
             if (isset($postvars["purge"])) {
                 Session::checkRight('plugin_fusioninventory_task', PURGE);
                 $pfTaskJob = new PluginFusioninventoryTaskjob();
                 $taskjobs = $pfTaskJob->find("`plugin_fusioninventory_tasks_id` = '" . $postvars['id'] . "' ");
                 foreach ($taskjobs as $taskjob) {
                     $pfTaskJob->delete($taskjob);
                 }
                 $this->delete($postvars);
                 Html::redirect(Toolbox::getItemTypeSearchURL(get_class($this)));
             } else {
                 if (isset($_POST["update"])) {
                     Session::checkRight('plugin_fusioninventory_task', UPDATE);
                     $this->getFromDB($postvars['id']);
                     //Ensure empty value are set to NULL for datetime fields
                     if (isset($postvars['datetime_start']) and $postvars['datetime_start'] === '') {
                         $postvars['datetime_start'] = 'NULL';
                     }
                     if (isset($postvars['datetime_end']) and $postvars['datetime_end'] === '') {
                         $postvars['datetime_end'] = 'NULL';
                     }
                     $this->update($postvars);
                     Html::back();
                 }
             }
         }
     }
 }
 function pre_deleteItem()
 {
     global $CFG_GLPI;
     //if task active, delete denied
     if ($this->getField('is_active') == 1) {
         Session::addMessageAfterRedirect(__('This task is active. delete denied', 'fusioninventory'));
         Html::redirect($CFG_GLPI["root_doc"] . "/plugins/fusinvdeploy/front/task.form.php?id=" . $this->getField('id'));
         return FALSE;
     }
     $task_id = $this->getField('id');
     $job = new PluginFusioninventoryTaskjob();
     $status = new PluginFusioninventoryTaskjobstate();
     $log = new PluginFusioninventoryTaskjoblog();
     // clean all sub-tables
     $a_taskjobs = $job->find("`plugin_fusioninventory_tasks_id`='{$task_id}'");
     foreach ($a_taskjobs as $a_taskjob) {
         $a_taskjobstatuss = $status->find("`plugin_fusioninventory_taskjobs_id`='" . $a_taskjob['id'] . "'");
         foreach ($a_taskjobstatuss as $a_taskjobstatus) {
             $a_taskjoblogs = $log->find("`plugin_fusioninventory_taskjobstates_id`='" . $a_taskjobstatus['id'] . "'");
             foreach ($a_taskjoblogs as $a_taskjoblog) {
                 $log->delete($a_taskjoblog, 1);
             }
             $status->delete($a_taskjobstatus, 1);
         }
         $job->delete($a_taskjob, 1);
     }
     return TRUE;
 }