/** * Display Sample * * @param $model PluginDatainjectionModel object **/ function showSample(PluginDatainjectionModel $model) { $headers = PluginDatainjectionMapping::getMappingsSortedByRank($model->fields['id']); $sample = '"' . implode('"' . $this->getDelimiter() . '"', $headers) . "\"\n"; header('Content-disposition: attachment; filename="' . str_replace(' ', '_', $model->getName()) . '.csv"'); header('Content-Type: text/comma-separated-values'); header('Content-Transfer-Encoding: UTF-8'); header('Content-Length: ' . mb_strlen($sample, 'UTF-8')); header('Pragma: no-cache'); header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); header('Expires: 0'); echo $sample; }
/** * Inject one line of datas * * @param $line one line of data to import * @param $index the line number is the file **/ function injectLine($line, $index) { //Store all fields to injection, sorted by itemtype $fields_toinject = array(); $mandatory_fields = array(); //Get the injectionclass associated to the itemtype $itemtype = $this->getModel()->getItemtype(); $injectionClass = PluginDatainjectionCommonInjectionLib::getInjectionClassInstance($itemtype); $several = PluginDatainjectionMapping::getSeveralMappedField($this->getModel()->fields['id']); //First of all : transform $line which is an array of values to inject into another array //which looks like this : //array(itemtype=>array(field=>value,field2=>value2)) //Note : ignore values which are not mapped with a glpi's field $searchOptions = $injectionClass->getOptions($itemtype); for ($i = 0; $i < count($line); $i++) { $mapping = $this->getModel()->getMappingByRank($i); //If field is mapped with a value in glpi if ($mapping != null && $mapping->getItemtype() != PluginDatainjectionInjectionType::NO_VALUE) { $this->addValueToInject($fields_toinject, $searchOptions, $mapping, $line[$i], $several); } } //Create an array with the mandatory mappings foreach ($this->getModel()->getMappings() as $mapping) { if ($mapping->isMandatory()) { $mandatory_fields[$mapping->getItemtype()][$mapping->getValue()] = $mapping->isMandatory(); } } //Add fields needed for injection $this->addRequiredFields($itemtype, $fields_toinject); //Optional data to be added to the fields to inject (won't be checked !) $optional_data = $this->addAdditionalInformations($this->infos); //--------------- Set all needed options ------------------// //Check options $checks = array('ip' => true, 'mac' => true, 'integer' => true, 'yes' => true, 'bool' => true, 'date' => $this->getModel()->getDateFormat(), 'float' => $this->getModel()->getFloatFormat(), 'string' => true, 'right_r' => true, 'right_rw' => true, 'interface' => true, 'auth_method' => true, 'port_unicity' => $this->getModel()->getPortUnicity()); //Rights options $rights = array('add_dropdown' => $this->getModel()->getCanAddDropdown(), 'overwrite_notempty_fields' => $this->getModel()->getCanOverwriteIfNotEmpty(), 'can_add' => $this->model->getBehaviorAdd(), 'can_update' => $this->model->getBehaviorUpdate(), 'can_delete' => false); //Field format options $formats = array('date_format' => $this->getModel()->getDateFormat(), 'float_format' => $this->getModel()->getFloatFormat()); //Check options : by default check all types $options = array('checks' => $checks, 'entities_id' => $this->getEntity(), 'rights' => $rights, 'formats' => $formats, 'mandatory_fields' => $mandatory_fields, 'optional_data' => $optional_data); //Will manage add or update $results = $injectionClass->addOrUpdateObject($fields_toinject, $options); //Add injected line number to the result array $results['line'] = $index; if ($results['status'] != PluginDatainjectionCommonInjectionLib::SUCCESS) { $this->error_lines[] = $line; } return $results; }
function populateSeveraltimesMappedFields() { $this->severaltimes_mapped = PluginDatainjectionMapping::getSeveralMappedField($this->fields['id']); }
along with datainjection. If not, see <http://www.gnu.org/licenses/>. -------------------------------------------------------------------------- @package datainjection @author the datainjection plugin team @copyright Copyright (c) 2010-2013 Datainjection plugin team @license GPLv2+ http://www.gnu.org/licenses/gpl.txt @link https://forge.indepnet.net/projects/datainjection @link http://www.glpi-project.org/ @since 2009 ---------------------------------------------------------------------- */ include '../../../inc/includes.php'; /* Update mappings */ if (isset($_POST["update"])) { $at_least_one_mandatory = false; $mapping = new PluginDatainjectionMapping(); foreach ($_POST['data'] as $id => $mapping_infos) { $mapping_infos['id'] = $id; //If no field selected, reset other values if ($mapping_infos['value'] == PluginDatainjectionInjectionType::NO_VALUE) { $mapping_infos['itemtype'] = PluginDatainjectionInjectionType::NO_VALUE; $mapping_infos['is_mandatory'] = 0; } else { $mapping_infos['is_mandatory'] = isset($mapping_infos['is_mandatory']) ? 1 : 0; } if ($mapping_infos['is_mandatory']) { $at_least_one_mandatory = true; } $mapping->update($mapping_infos); } if (!$at_least_one_mandatory) {
static function exportErrorsInCSV() { $error_lines = json_decode(PluginDatainjectionSession::getParam('error_lines'), true); self::stripslashes_array($error_lines); if (!empty($error_lines)) { $model = unserialize(PluginDatainjectionSession::getParam('currentmodel')); $file = PLUGIN_DATAINJECTION_UPLOAD_DIR . PluginDatainjectionSession::getParam('file_name'); $mappings = $model->getMappings(); $tmpfile = fopen($file, 'w'); //If headers present if ($model->getBackend()->isHeaderPresent()) { $headers = PluginDatainjectionMapping::getMappingsSortedByRank($model->fields['id']); fputcsv($tmpfile, $headers, $model->getBackend()->getDelimiter()); } //Write lines foreach ($error_lines as $line) { fputcsv($tmpfile, $line, $model->getBackend()->getDelimiter()); } fclose($tmpfile); $name = "Error-" . PluginDatainjectionSession::getParam('file_name'); $name = str_replace(' ', '', $name); header('Content-disposition: attachment; filename=' . $name); header('Content-Type: application/octet-stream'); header('Content-Transfer-Encoding: fichier'); header('Content-Length: ' . filesize($file)); header('Pragma: no-cache'); header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); header('Expires: 0'); readfile($file); unlink($file); } }
function deleteMappingsFromDB($model_id) { $mapping = new PluginDatainjectionMapping(); $mapping->deleteByCriteria(array('models_id' => $model_id)); }