Beispiel #1
0
        if (isset($_POST["update"])) {
            //Update model
            $model->check($_POST['id'], UPDATE);
            $model->update($_POST);
            $specific_model = PluginDatainjectionModel::getInstance('csv');
            $specific_model->saveFields($_POST);
            Html::back();
            /* update order */
        } elseif (isset($_POST["validate"])) {
            $model->check($_POST['id'], UPDATE);
            $model->switchReadyToUse();
            Html::back();
        } elseif (isset($_POST['upload'])) {
            if (!empty($_FILES)) {
                $model->check($_POST['id'], UPDATE);
                if ($model->processUploadedFile(array('file_encoding' => 'csv', 'mode' => PluginDatainjectionModel::CREATION))) {
                    Session::setActiveTab('PluginDatainjectionModel', 'PluginDatainjectionModel$4');
                } else {
                    Session::addMessageAfterRedirect(__('The file could not be found', 'datainjection'), true, ERROR, true);
                }
            }
            Html::back();
        } elseif (isset($_GET['sample'])) {
            $model->check($_GET['sample'], READ);
            $modeltype = PluginDatainjectionModel::getInstance($model->getField('filetype'));
            $modeltype->getFromDBByModelID($model->getField('id'));
            $modeltype->showSample($model);
            exit(0);
        }
    }
}
if (isset($_SESSION['datainjection']['go'])) {
    $model = unserialize($_SESSION['datainjection']['currentmodel']);
    PluginDatainjectionClientInjection::showInjectionForm($model, $_SESSION['glpiactive_entity']);
} else {
    if (isset($_POST['upload'])) {
        $model = new PluginDatainjectionModel();
        $model->can($_POST['id'], READ);
        $_SESSION['datainjection']['infos'] = isset($_POST['info']) ? $_POST['info'] : array();
        //If additional informations provided : check if mandatory infos are present
        if (!$model->checkMandatoryFields($_SESSION['datainjection']['infos'])) {
            Session::addMessageAfterRedirect(__('One mandatory field is not filled', 'datainjection'), true, ERROR, true);
        } else {
            if (isset($_FILES['filename']['name']) && $_FILES['filename']['name'] && $_FILES['filename']['tmp_name'] && !$_FILES['filename']['error'] && $_FILES['filename']['size']) {
                //Read file using automatic encoding detection, and do not delete file once readed
                $options = array('file_encoding' => $_POST['file_encoding'], 'mode' => PluginDatainjectionModel::PROCESS, 'delete_file' => false);
                $response = $model->processUploadedFile($options);
                $model->cleanData();
                if ($response) {
                    //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);
            }
 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;
 }