PageRenderTime 36ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/glpi-data-injection-2.2.2/datainjection/inc/engine.class.php

#
PHP | 252 lines | 108 code | 43 blank | 101 comment | 15 complexity | 5ec3f01c550d89c7f658724b0058989b MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0
  1. <?php
  2. /*
  3. * @version $Id: HEADER 14684 2011-06-11 06:32:40Z remi $
  4. LICENSE
  5. This file is part of the datainjection plugin.
  6. Datainjection plugin is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. Datainjection plugin is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with datainjection. If not, see <http://www.gnu.org/licenses/>.
  16. --------------------------------------------------------------------------
  17. @package datainjection
  18. @author the datainjection plugin team
  19. @copyright Copyright (c) 2010-2011 Order plugin team
  20. @license GPLv2+
  21. http://www.gnu.org/licenses/gpl.txt
  22. @link https://forge.indepnet.net/projects/datainjection
  23. @link http://www.glpi-project.org/
  24. @since 2009
  25. ---------------------------------------------------------------------- */
  26. class PluginDatainjectionEngine {
  27. //Model informations
  28. private $model;
  29. //Current entity
  30. private $entity;
  31. //Additional infos to be added
  32. private $infos = array();
  33. //Lines in error
  34. private $error_lines = array();
  35. function __construct($model, $infos = array(), $entity = 0) {
  36. //Instanciate model
  37. $this->model = $model;
  38. //Load model and mappings informations
  39. $this->getModel()->loadMappings();
  40. $this->getModel()->populateSeveraltimesMappedFields();
  41. $this->getModel()->loadInfos();
  42. $this->infos = $infos;
  43. $this->entity = $entity;
  44. }
  45. /**
  46. * Inject one line of datas
  47. *
  48. * @param line one line of data to import
  49. * @param index the line number is the file
  50. **/
  51. function injectLine($line, $index) {
  52. //logDebug("------------- injectLine($index) ----------");
  53. //logDebug("Line=", $line);
  54. //Store all fields to injection, sorted by itemtype
  55. $fields_toinject = array();
  56. $mandatory_fields = array();
  57. //Get the injectionclass associated to the itemtype
  58. $itemtype = $this->getModel()->getItemtype();
  59. $injectionClass = PluginDatainjectionCommonInjectionLib::getInjectionClassInstance($itemtype);
  60. $several = PluginDatainjectionMapping::getSeveralMappedField($this->getModel()->fields['id']);
  61. //First of all : transform $line which is an array of values to inject into another array
  62. //which looks like this :
  63. //array(itemtype=>array(field=>value,field2=>value2))
  64. //Note : ignore values which are not mapped with a glpi's field
  65. $searchOptions = $injectionClass->getOptions($itemtype);
  66. for ($i=0 ; $i<count($line) ; $i++) {
  67. $mapping = $this->getModel()->getMappingByRank($i);
  68. //If field is mapped with a value in glpi
  69. if ($mapping!=null
  70. && $mapping->getItemtype() != PluginDatainjectionInjectionType::NO_VALUE) {
  71. $this->addValueToInject($fields_toinject, $searchOptions, $mapping, $line[$i], $several);
  72. }
  73. }
  74. //Create an array with the mandatory mappings
  75. foreach ($this->getModel()->getMappings() as $mapping) {
  76. if ($mapping->isMandatory()) {
  77. $mandatory_fields[$mapping->getItemtype()][$mapping->getValue()] = $mapping->isMandatory();
  78. }
  79. }
  80. //Add fields needed for injection
  81. $this->addRequiredFields($itemtype, $fields_toinject);
  82. //Optional data to be added to the fields to inject (won't be checked !)
  83. $optional_data = $this->addAdditionalInformations($this->infos);
  84. //--------------- Set all needed options ------------------//
  85. //Check options
  86. $checks = array('ip' => true,
  87. 'mac' => true,
  88. 'integer' => true,
  89. 'yes' => true,
  90. 'bool' => true,
  91. 'date' => $this->getModel()->getDateFormat(),
  92. 'float' => $this->getModel()->getFloatFormat(),
  93. 'string' => true,
  94. 'right_r' => true,
  95. 'right_rw' => true,
  96. 'interface' => true,
  97. 'auth_method' => true,
  98. 'port_unicity' => $this->getModel()->getPortUnicity());
  99. //Rights options
  100. $rights = array('add_dropdown' => $this->getModel()->getCanAddDropdown(),
  101. 'overwrite_notempty_fields' => $this->getModel()->getCanOverwriteIfNotEmpty(),
  102. 'can_add' => $this->model->getBehaviorAdd(),
  103. 'can_update' => $this->model->getBehaviorUpdate(),
  104. 'can_delete' => false);
  105. //Field format options
  106. $formats = array('date_format' => $this->getModel()->getDateFormat(),
  107. 'float_format' => $this->getModel()->getFloatFormat());
  108. //Check options : by default check all types
  109. $options = array('checks' => $checks,
  110. 'entities_id' => $this->getEntity(),
  111. 'rights' => $rights,
  112. 'formats' => $formats,
  113. 'mandatory_fields' => $mandatory_fields,
  114. 'optional_data' => $optional_data);
  115. //Will manage add or update
  116. $results = $injectionClass->addOrUpdateObject($fields_toinject, $options);
  117. //Add injected line number to the result array
  118. $results['line'] = $index;
  119. if ($results['status'] != PluginDatainjectionCommonInjectionLib::SUCCESS) {
  120. $this->error_lines[] = $line;
  121. }
  122. return $results;
  123. }
  124. /**
  125. * Add fields needed for injection
  126. *
  127. * @param itemtype the itemtype to inject
  128. * @param fields_toinject the list of fields representing the object
  129. *
  130. * @return nothing
  131. **/
  132. function addRequiredFields($itemtype, &$fields_toinject = array()) {
  133. //Add entity to the primary type
  134. $fields_toinject[$itemtype]['entities_id'] = $this->entity;
  135. }
  136. /**
  137. * Add a value to the fields to inject
  138. *
  139. * @param $fields_toinject the fields
  140. * @param searchOptions options related to the itemtype to inject
  141. * @param mapping the mapping which matches the field
  142. * @param value the value for this field, as readed from the CSV file
  143. * @param several array of all fields which can be mapping more than one time in the model
  144. *
  145. * @return nothing
  146. **/
  147. function addValueToInject(&$fields_toinject, $searchOptions, $mapping, $value,
  148. $several = array()) {
  149. // Option will be found only for "main" type.
  150. $option = PluginDatainjectionCommonInjectionLib::findSearchOption($searchOptions,
  151. $mapping->getValue());
  152. $return_value = $value;
  153. if ($option['displaytype'] == 'multiline_text'
  154. && in_array($mapping->getValue(), $several)
  155. && $value != PluginDatainjectionCommonInjectionLib::EMPTY_VALUE) {
  156. $return_value = '';
  157. if (isset($fields_toinject[$mapping->getItemtype()][$mapping->getValue()])) {
  158. $return_value .= $fields_toinject[$mapping->getItemtype()][$mapping->getValue()];
  159. }
  160. $return_value .= $mapping->getMappingName()."=".$value."\n";
  161. }
  162. /*
  163. else {
  164. $return_value = $value;
  165. //Value is not empty
  166. if ($value != PluginDatainjectionCommonInjectionLib::EMPTY_VALUE) {
  167. $return_value = $value;
  168. }
  169. else {
  170. //Value is empty : use the right default value regarding the field's type
  171. if (!in_array($option['displaytype'],array('text','multiline_text'))) {
  172. $return_value = PluginDatainjectionCommonInjectionLib::Dropdown::EMPTY_VALUE;
  173. }
  174. }
  175. }
  176. */
  177. $fields_toinject[$mapping->getItemtype()][$mapping->getValue()] = $return_value;
  178. }
  179. /**
  180. * Add additonal informations, as selected by the user which performs the CSV file import
  181. *
  182. * @return additional informations to inject
  183. **/
  184. function addAdditionalInformations() {
  185. $additional_infos = array();
  186. foreach ($this->model->getInfos() as $info) {
  187. if (isset($this->infos[$info->getValue()])
  188. && PluginDatainjectionInfo::keepInfo($info, $this->infos[$info->getValue()])) {
  189. $additional_infos[$info->getInfosType()][$info->getValue()] = $this->infos[$info->getValue()];
  190. }
  191. }
  192. return $additional_infos;
  193. }
  194. //--------- Getters -------------------------//
  195. function getModel() {
  196. return $this->model;
  197. }
  198. function getEntity() {
  199. return $this->entity;
  200. }
  201. function getLinesInError() {
  202. return $this->error_lines;
  203. }
  204. }
  205. ?>