import.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // - text qualifier hardcoded to "
  20. //
  21. // Known issues
  22. // - ALMOST impossible to troubleshoot when an externl key has a wrong value
  23. // - no character escaping in the xml output (yes !?!?!)
  24. // - not outputing xml when a wrong input is given (class, attribute names)
  25. // - for a bizIncidentTicket you may use the name as the reconciliation key,
  26. // but that attribute is in fact recomputed by the application! An error should be raised somewhere
  27. //
  28. require_once('../application/application.inc.php');
  29. require_once('../application/webpage.class.inc.php');
  30. require_once('../application/csvpage.class.inc.php');
  31. require_once('../application/xmlpage.class.inc.php');
  32. require_once('../application/startup.inc.php');
  33. require_once('../application/loginwebpage.class.inc.php');
  34. class WebServiceException extends Exception
  35. {
  36. }
  37. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  38. $oContext = new UserContext();
  39. $oAppContext = new ApplicationContext();
  40. //$iActiveNodeId = utils::ReadParam('menu', -1);
  41. //$currentOrganization = utils::ReadParam('org_id', '');
  42. // Main program
  43. //$oP = new XMLPage("iTop - Bulk import");
  44. $oP = new WebPage("iTop - Bulk import");
  45. $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>');
  46. try
  47. {
  48. $sClass = utils::ReadParam('class', '');
  49. $sSep = utils::ReadParam('separator', ';');
  50. $sCSVData = utils::ReadPostedParam('csvdata');
  51. $oCSVParser = new CSVParser($sCSVData, $sSep, $sDelimiter = '"');
  52. // Limitation: as the attribute list is in the first line, we can not match external key by a third-party attribute
  53. $sRawFieldList = $oCSVParser->ListFields();
  54. $aAttList = array();
  55. $aExtKeys = array();
  56. foreach($sRawFieldList as $iFieldId => $sFieldName)
  57. {
  58. if (!MetaModel::IsValidAttCode($sClass, $sFieldName))
  59. {
  60. throw new WebServiceException("Unknown attribute '$sFieldName' (class: '$sClass')");
  61. }
  62. $oAtt = MetaModel::GetAttributeDef($sClass, $sFieldName);
  63. if ($oAtt->IsExternalKey())
  64. {
  65. $aExtKeys[$sFieldName]['id'] = $iFieldId;
  66. $aAttList[$sFieldName] = $iFieldId;
  67. }
  68. elseif ($oAtt->IsExternalField())
  69. {
  70. $sExtKeyAttCode = $oAtt->GetKeyAttCode();
  71. $sRemoteAttCode = $oAtt->GetExtAttCode();
  72. $aExtKeys[$sExtKeyAttCode][$sRemoteAttCode] = $iFieldId;
  73. }
  74. else
  75. {
  76. $aAttList[$sFieldName] = $iFieldId;
  77. }
  78. }
  79. // Limitation: the reconciliation key is the first attribute
  80. $aReconcilKeys = array($sRawFieldList[0]);
  81. $aData = $oCSVParser->ToArray();
  82. $oBulk = new BulkChange(
  83. $sClass,
  84. $aData,
  85. $aAttList,
  86. $aExtKeys,
  87. $aReconcilKeys
  88. );
  89. $oMyChange = MetaModel::NewObject("CMDBChange");
  90. $oMyChange->Set("date", time());
  91. if (UserRights::GetUser() != UserRights::GetRealUser())
  92. {
  93. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  94. }
  95. else
  96. {
  97. $sUserString = UserRights::GetUser();
  98. }
  99. $oMyChange->Set("userinfo", $sUserString.' (bulk load by web service)');
  100. $iChangeId = $oMyChange->DBInsert();
  101. $aRes = $oBulk->Process($oMyChange);
  102. // Setup result presentation
  103. //
  104. $aDisplayConfig = array();
  105. $aDisplayConfig["__RECONCILIATION__"] = array("label"=>"Reconciliation", "description"=>"");
  106. $aDisplayConfig["__STATUS__"] = array("label"=>"Status", "description"=>"");
  107. if (isset($iPKeyId))
  108. {
  109. $aDisplayConfig["col$iPKeyId"] = array("label"=>"<strong>id</strong>", "description"=>"");
  110. }
  111. foreach($aReconcilKeys as $iCol => $sAttCode)
  112. {
  113. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  114. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  115. }
  116. foreach ($aAttList as $sAttCode => $iCol)
  117. {
  118. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  119. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  120. }
  121. $aResultDisp = array(); // to be displayed
  122. foreach($aRes as $iRow => $aRowData)
  123. {
  124. $aRowDisp = array();
  125. $aRowDisp["__RECONCILIATION__"] = $aRowData["__RECONCILIATION__"];
  126. $aRowDisp["__STATUS__"] = $aRowData["__STATUS__"]->GetDescription();
  127. foreach($aRowData as $sKey => $value)
  128. {
  129. if ($sKey == '__RECONCILIATION__') continue;
  130. if ($sKey == '__STATUS__') continue;
  131. switch (get_class($value))
  132. {
  133. case 'CellStatus_Void':
  134. $sClass = '';
  135. break;
  136. case 'CellStatus_Modify':
  137. $sClass = 'csvimport_ok';
  138. break;
  139. case 'CellStatus_Issue':
  140. $sClass = 'csvimport_error';
  141. break;
  142. }
  143. if (empty($sClass))
  144. {
  145. $aRowDisp[$sKey] = $value->GetDescription();
  146. }
  147. else
  148. {
  149. $aRowDisp[$sKey] = "<div class=\"$sClass\">".$value->GetDescription()."</div>";
  150. }
  151. }
  152. $aResultDisp[$iRow] = $aRowDisp;
  153. }
  154. $oP->table($aDisplayConfig, $aResultDisp);
  155. }
  156. catch(Exception $e)
  157. {
  158. $oP->add('<error>'.((string)$e).'</error>');
  159. }
  160. $oP->output();
  161. ?>