import.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Import web service
  4. *
  5. * @package iTopORM
  6. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  7. * @author Denis Flaven <denisflave@free.fr>
  8. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  9. * @link www.itop.com
  10. * @since 1.0
  11. * @version 1.1.1.1 $
  12. */
  13. //
  14. // Known limitations
  15. // - output still in html, because the errors are not displayed in xml
  16. // - only external fields attributes could be used as reconciliation keys for external keys
  17. // - reconciliation is made on the first column
  18. // - no option to force 'always create' or 'never create'
  19. //
  20. // Known issues
  21. // - ALMOST impossible to troubleshoot when an externl key has a wrong value
  22. // - no character escaping in the xml output (yes !?!?!)
  23. // - not outputing xml when a wrong input is given (class, attribute names)
  24. // - for a bizIncidentTicket you may use the name as the reconciliation key,
  25. // but that attribute is in fact recomputed by the application! An error should be raised somewhere
  26. //
  27. require_once('../application/application.inc.php');
  28. require_once('../application/webpage.class.inc.php');
  29. require_once('../application/csvpage.class.inc.php');
  30. require_once('../application/xmlpage.class.inc.php');
  31. require_once('../application/startup.inc.php');
  32. require_once('../application/loginwebpage.class.inc.php');
  33. class WebServiceException extends Exception
  34. {
  35. }
  36. login_web_page::DoLogin(); // Check user rights and prompt if needed
  37. $oContext = new UserContext();
  38. $oAppContext = new ApplicationContext();
  39. //$iActiveNodeId = utils::ReadParam('menu', -1);
  40. //$currentOrganization = utils::ReadParam('org_id', '');
  41. // Main program
  42. //$oP = new XMLPage("iTop - Bulk import");
  43. $oP = new web_page("iTop - Bulk import");
  44. $oP->add('<warning>This is a prototype, I repeat: PRO-TO-TYPE, therefore it suffers bugs and limitations, documented in the code. Next step: specify...</warning>');
  45. try
  46. {
  47. $sClass = utils::ReadParam('class', '');
  48. $sSep = utils::ReadParam('separator', ';');
  49. $sCSVData = utils::ReadPostedParam('csvdata');
  50. $oCSVParser = new CSVParser($sCSVData);
  51. $oCSVParser->SetSeparator($sSep);
  52. $oCSVParser->SetSkipLines(1);
  53. // Limitation: as the attribute list is in the first line, we can not match external key by an third-party attribute
  54. $sRawFieldList = $oCSVParser->ListFields();
  55. $aAttList = array();
  56. $aExtKeys = array();
  57. foreach($sRawFieldList as $iFieldId => $sFieldName)
  58. {
  59. if (!MetaModel::IsValidAttCode($sClass, $sFieldName))
  60. {
  61. throw new WebServiceException("Unknown attribute '$sFieldName' (class: '$sClass')");
  62. }
  63. $oAtt = MetaModel::GetAttributeDef($sClass, $sFieldName);
  64. if ($oAtt->IsExternalKey())
  65. {
  66. $aExtKeys[$sFieldName]['id'] = $iFieldId;
  67. $aAttList[$sFieldName] = $iFieldId;
  68. }
  69. elseif ($oAtt->IsExternalField())
  70. {
  71. $sExtKeyAttCode = $oAtt->GetKeyAttCode();
  72. $sRemoteAttCode = $oAtt->GetExtAttCode();
  73. $aExtKeys[$sExtKeyAttCode][$sRemoteAttCode] = $iFieldId;
  74. }
  75. else
  76. {
  77. $aAttList[$sFieldName] = $iFieldId;
  78. }
  79. }
  80. // Limitation: the reconciliation key is the first attribute
  81. $aReconcilKeys = array($sRawFieldList[0]);
  82. // print_r($oCSVParser->ListFields());
  83. // print_r($oCSVParser->ToArray($oCSVParser->ListFields()));
  84. $aData = $oCSVParser->ToArray();
  85. $oBulk = new BulkChange(
  86. $sClass,
  87. $aData,
  88. $aAttList,
  89. $aReconcilKeys,
  90. $aExtKeys
  91. );
  92. $oMyChange = MetaModel::NewObject("CMDBChange");
  93. $oMyChange->Set("date", time());
  94. if (UserRights::GetUser() != UserRights::GetRealUser())
  95. {
  96. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  97. }
  98. else
  99. {
  100. $sUserString = UserRights::GetUser();
  101. }
  102. $oMyChange->Set("userinfo", $sUserString.' (bulk load by web service)');
  103. $iChangeId = $oMyChange->DBInsert();
  104. $aRes = $oBulk->Process($oMyChange);
  105. // Setup result presentation
  106. //
  107. $aDisplayConfig = array();
  108. $aDisplayConfig["__RECONCILIATION__"] = array("label"=>"Reconciliation", "description"=>"");
  109. $aDisplayConfig["__STATUS__"] = array("label"=>"Status", "description"=>"");
  110. if (isset($iPKeyId))
  111. {
  112. $aDisplayConfig["col$iPKeyId"] = array("label"=>"<strong>pkey</strong>", "description"=>"");
  113. }
  114. foreach($aReconcilKeys as $iCol => $sAttCode)
  115. {
  116. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  117. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  118. }
  119. foreach ($aAttList as $sAttCode => $iCol)
  120. {
  121. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  122. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  123. }
  124. $aResultDisp = array(); // to be displayed
  125. foreach($aRes as $iRow => $aRowData)
  126. {
  127. $aRowDisp = array();
  128. $aRowDisp["__RECONCILIATION__"] = $aRowData["__RECONCILIATION__"];
  129. $aRowDisp["__STATUS__"] = $aRowData["__STATUS__"]->GetDescription();
  130. foreach($aRowData as $sKey => $value)
  131. {
  132. if ($sKey == '__RECONCILIATION__') continue;
  133. if ($sKey == '__STATUS__') continue;
  134. switch (get_class($value))
  135. {
  136. case 'CellChangeSpec_Void':
  137. $sClass = '';
  138. break;
  139. case 'CellChangeSpec_Unchanged':
  140. $sClass = '';
  141. break;
  142. case 'CellChangeSpec_Modify':
  143. $sClass = 'csvimport_ok';
  144. break;
  145. case 'CellChangeSpec_Init':
  146. $sClass = 'csvimport_init';
  147. break;
  148. case 'CellChangeSpec_Issue':
  149. $sClass = 'csvimport_error';
  150. break;
  151. }
  152. if (empty($sClass))
  153. {
  154. $aRowDisp[$sKey] = $value->GetDescription();
  155. }
  156. else
  157. {
  158. $aRowDisp[$sKey] = "<div class=\"$sClass\">".$value->GetDescription()."</div>";
  159. }
  160. }
  161. $aResultDisp[$iRow] = $aRowDisp;
  162. }
  163. $oP->table($aDisplayConfig, $aResultDisp);
  164. }
  165. catch(Exception $e)
  166. {
  167. $oP->add('<error>'.((string)$e).'</error>');
  168. }
  169. $oP->output();
  170. ?>