import.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // - could not set the external keys by the mean of a reconciliation (the exact key must be given)
  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. login_web_page::DoLogin(); // Check user rights and prompt if needed
  34. $oContext = new UserContext();
  35. $oAppContext = new ApplicationContext();
  36. //$iActiveNodeId = utils::ReadParam('menu', -1);
  37. //$currentOrganization = utils::ReadParam('org_id', '');
  38. // Main program
  39. //$oP = new XMLPage("iTop - Bulk import");
  40. $oP = new web_page("iTop - Bulk import");
  41. try
  42. {
  43. $sClass = utils::ReadParam('class', '');
  44. $sSep = utils::ReadParam('separator', ';');
  45. $sCSVData = utils::ReadPostedParam('csvdata');
  46. $oCSVParser = new CSVParser($sCSVData);
  47. $oCSVParser->SetSeparator($sSep);
  48. $oCSVParser->SetSkipLines(1);
  49. // Limitation: as the attribute list is in the first line, we can not match external key by an third-party attribute
  50. $sRawFieldList = $oCSVParser->ListFields();
  51. $aAttList = array();
  52. foreach($sRawFieldList as $iField => $sFieldName)
  53. {
  54. $aAttList[$sFieldName] = $iField;
  55. }
  56. $aExtKeys = array();
  57. // Limitation: the reconciliation key is the first attribute
  58. $aReconcilKeys = array($sRawFieldList[0]);
  59. // print_r($oCSVParser->ListFields());
  60. // print_r($oCSVParser->ToArray($oCSVParser->ListFields()));
  61. $aData = $oCSVParser->ToArray();
  62. $oBulk = new BulkChange(
  63. $sClass,
  64. $aData,
  65. $aAttList,
  66. $aReconcilKeys,
  67. $aExtKeys
  68. );
  69. $oMyChange = MetaModel::NewObject("CMDBChange");
  70. $oMyChange->Set("date", time());
  71. if (UserRights::GetUser() != UserRights::GetRealUser())
  72. {
  73. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  74. }
  75. else
  76. {
  77. $sUserString = UserRights::GetUser();
  78. }
  79. $oMyChange->Set("userinfo", $sUserString.' (bulk load by web service)');
  80. $iChangeId = $oMyChange->DBInsert();
  81. $aRes = $oBulk->Process($oMyChange);
  82. // Setup result presentation
  83. //
  84. $aDisplayConfig = array();
  85. $aDisplayConfig["__RECONCILIATION__"] = array("label"=>"Reconciliation", "description"=>"");
  86. $aDisplayConfig["__STATUS__"] = array("label"=>"Status", "description"=>"");
  87. if (isset($iPKeyId))
  88. {
  89. $aDisplayConfig["col$iPKeyId"] = array("label"=>"<strong>pkey</strong>", "description"=>"");
  90. }
  91. foreach($aReconcilKeys as $iCol => $sAttCode)
  92. {
  93. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  94. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  95. }
  96. foreach ($aAttList as $sAttCode => $iCol)
  97. {
  98. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  99. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  100. }
  101. $aResultDisp = array(); // to be displayed
  102. foreach($aRes as $iRow => $aRowData)
  103. {
  104. $aRowDisp = array();
  105. $aRowDisp["__RECONCILIATION__"] = $aRowData["__RECONCILIATION__"];
  106. $aRowDisp["__STATUS__"] = $aRowData["__STATUS__"]->GetDescription();
  107. foreach($aRowData as $sKey => $value)
  108. {
  109. if ($sKey == '__RECONCILIATION__') continue;
  110. if ($sKey == '__STATUS__') continue;
  111. switch (get_class($value))
  112. {
  113. case 'CellChangeSpec_Void':
  114. $sClass = '';
  115. break;
  116. case 'CellChangeSpec_Unchanged':
  117. $sClass = '';
  118. break;
  119. case 'CellChangeSpec_Modify':
  120. $sClass = 'csvimport_ok';
  121. break;
  122. case 'CellChangeSpec_Init':
  123. $sClass = 'csvimport_init';
  124. break;
  125. case 'CellChangeSpec_Issue':
  126. $sClass = 'csvimport_error';
  127. break;
  128. }
  129. if (empty($sClass))
  130. {
  131. $aRowDisp[$sKey] = $value->GetDescription();
  132. }
  133. else
  134. {
  135. $aRowDisp[$sKey] = "<div class=\"$sClass\">".$value->GetDescription()."</div>";
  136. }
  137. }
  138. $aResultDisp[$iRow] = $aRowDisp;
  139. }
  140. $oP->table($aDisplayConfig, $aResultDisp);
  141. }
  142. catch(Exception $e)
  143. {
  144. $oP->add('<error>'.((string)$e).'</error>');
  145. }
  146. $oP->output();
  147. ?>