//File uploaded successfully and matches the given model : switch to the import tab
                    $_SESSION['datainjection']['file_name'] = $_FILES['filename']['name'];
                    $_SESSION['datainjection']['step'] = PluginDatainjectionClientInjection::STEP_PROCESS;
                    //Store model in session for injection
                    $_SESSION['datainjection']['currentmodel'] = serialize($model);
                    $_SESSION['datainjection']['go'] = true;
                } else {
                    //Got back to the file upload page
                    $_SESSION['datainjection']['step'] = PluginDatainjectionClientInjection::STEP_UPLOAD;
                }
            } else {
                Session::addMessageAfterRedirect(__('The file could not be found', 'datainjection'), true, ERROR, true);
            }
        }
        Html::back();
    } else {
        if (isset($_POST['finish']) || isset($_POST['cancel'])) {
            PluginDatainjectionSession::removeParams();
            Html::redirect(Toolbox::getItemTypeFormURL('PluginDatainjectionClientInjection'));
        } else {
            if (isset($_GET['id'])) {
                // Allow link to a model
                PluginDatainjectionSession::setParam('models_id', $_GET['id']);
            }
            $clientInjection = new PluginDatainjectionClientInjection();
            $clientInjection->title();
            $clientInjection->showForm(0);
        }
    }
}
Html::footer();
Example #2
0
 static function cleanSessionVariables()
 {
     //Reset parameters stored in session
     PluginDatainjectionSession::removeParams();
     PluginDatainjectionSession::setParam('infos', array());
 }
 /**
  * @param $model        PluginDatainjectionModel object
  * @param $entities_id
  **/
 static function processInjection(PluginDatainjectionModel $model, $entities_id)
 {
     global $CFG_GLPI;
     // To prevent problem of execution time during injection
     ini_set("max_execution_time", "0");
     // Disable recording each SQL request in $_SESSION
     $CFG_GLPI["debug_sql"] = 0;
     $nblines = PluginDatainjectionSession::getParam('nblines');
     $clientinjection = new PluginDatainjectionClientInjection();
     //New injection engine
     $engine = new PluginDatainjectionEngine($model, PluginDatainjectionSession::getParam('infos'), $entities_id);
     $backend = $model->getBackend();
     $model->loadSpecificModel();
     //Open CSV file
     $backend->openFile();
     $index = 0;
     //Read CSV file
     $line = $backend->getNextLine();
     //If header is present, then get the second line
     if ($model->getSpecificModel()->isHeaderPresent()) {
         $line = $backend->getNextLine();
     }
     //While CSV file is not EOF
     $prev = '';
     $deb = time();
     while ($line != null) {
         //Inject line
         $injectionline = $index + ($model->getSpecificModel()->isHeaderPresent() ? 2 : 1);
         $clientinjection->results[] = $engine->injectLine($line[0], $injectionline);
         $pos = number_format($index * 100 / $nblines, 1);
         if ($pos != $prev) {
             $prev = $pos;
             $fin = time() - $deb;
             //TODO yllen
             Html::changeProgressBarPosition($index, $nblines, sprintf(__('%1$s (%2$s)'), sprintf(__('Injection of the file... %d%%', 'datainjection'), $pos), Html::timestampToString(time() - $deb, true)));
         }
         $line = $backend->getNextLine();
         $index++;
     }
     //EOF : change progressbar to 100% !
     Html::changeProgressBarPosition(100, 100, sprintf(__('%1$s (%2$s)'), __('Injection finished', 'datainjection'), Html::timestampToString(time() - $deb, true)));
     // Restore
     $CFG_GLPI["debug_sql"] = 1;
     //Close CSV file
     $backend->closeFile();
     //Delete CSV file
     $backend->deleteFile();
     //Change step
     $_SESSION['datainjection']['step'] = self::STEP_RESULT;
     //Display results form
     PluginDatainjectionSession::setParam('results', json_encode($clientinjection->results));
     PluginDatainjectionSession::setParam('error_lines', json_encode($engine->getLinesInError()));
     $p['models_id'] = $model->fields['id'];
     $p['nblines'] = $nblines;
     unset($_SESSION['datainjection']['go']);
     $_SESSION["MESSAGE_AFTER_REDIRECT"] = "";
     $url = $CFG_GLPI["root_doc"] . "/plugins/datainjection/ajax/results.php";
     Ajax::updateItem("span_injection", $url, $p);
 }