/**
  * @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);
 }
示例#2
0
 static function methodInject($params, $protocol)
 {
     if (isset($params['help'])) {
         return array('uri' => 'string,mandatory', 'base64' => 'string,optional', 'additional' => 'array,optional', 'models_id' => 'integer, mandatory', 'entities_id' => 'integer,mandatory', 'mandatory' => 'array,optional', 'uri' => 'uri,mandatory', 'help' => 'bool,optional');
     }
     $model = new PluginDatainjectionModel();
     //-----------------------------------------------------------------
     //-------------------------- Check parameters ---------------------
     //-----------------------------------------------------------------
     if (!isset($_SESSION['glpiID'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
     }
     if (!isset($params['uri']) && !isset($params['base64'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, '', 'uri or base64');
     }
     if (!isset($params['models_id'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, 'models_id');
     }
     if (!$model->getFromDB($params['models_id'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTFOUND, __('Model unknown', 'datainjection'));
     }
     if (!$model->can($params['models_id'], 'r')) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, __('You cannot access this model', 'datainjection'));
     }
     if ($model->fields['step'] < PluginDatainjectionModel::READY_TO_USE_STEP) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, __('You cannot access this model', 'datainjection'));
     }
     //Check entity
     if (!isset($params['entities_id'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, 'entities_id');
     }
     $entities_id = $params['entities_id'];
     if ($entities_id > 0) {
         $entity = new Entity();
         if (!$entity->getFromDB($entities_id)) {
             return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTFOUND, __('Entity unknown', 'datainjection'));
         }
         if (!Session::haveAccessToEntity($entities_id)) {
             return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, __('You cannot access this entity', 'datainjection'));
         }
     }
     //Mandatory fields
     $additional_infos = array();
     if (isset($params['additional']) && is_array($params['additional'])) {
         $additional_infos = $params['additional'];
     }
     //Upload CSV file
     $document_name = basename($params['uri']);
     $filename = tempnam(PLUGIN_DATAINJECTION_UPLOAD_DIR, 'PWS');
     $response = PluginWebservicesMethodCommon::uploadDocument($params, $protocol, $filename, $document_name);
     if (PluginWebservicesMethodCommon::isError($protocol, $response)) {
         return $response;
     }
     //Uploade successful : now perform import !
     $options = array('file_encoding' => PluginDatainjectionBackend::ENCODING_AUTO, 'webservice' => true, 'original_filename' => $params['uri'], 'unique_filename' => $filename, 'mode' => PluginDatainjectionModel::PROCESS, 'delete_file' => false, 'protocol' => $protocol);
     //The Webservice protocol used
     $results = array();
     $response = $model->processUploadedFile($options);
     if (!PluginWebservicesMethodCommon::isError($protocol, $response)) {
         $engine = new PluginDatainjectionEngine($model, $additional_infos, $params['entities_id']);
         //Remove first line if header is present
         $first = true;
         foreach ($model->injectionData->getDatas() as $id => $data) {
             if ($first && $model->getSpecificModel()->isHeaderPresent()) {
                 $first = false;
             } else {
                 $results[] = $engine->injectLine($data[0], $id);
             }
         }
         $model->cleanData();
         return $results;
     }
     return $response;
 }