import.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Import web service
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. //
  25. // Known limitations
  26. // - output still in html, because the errors are not displayed in xml
  27. // - reconciliation is made on the first column
  28. // - no option to force 'always create' or 'never create'
  29. // - text qualifier hardcoded to "
  30. //
  31. // Known issues
  32. // - ALMOST impossible to troubleshoot when an externl key has a wrong value
  33. // - no character escaping in the xml output (yes !?!?!)
  34. // - not outputing xml when a wrong input is given (class, attribute names)
  35. // - for a bizIncidentTicket you may use the name as the reconciliation key,
  36. // but that attribute is in fact recomputed by the application! An error should be raised somewhere
  37. //
  38. require_once('../application/application.inc.php');
  39. require_once('../application/webpage.class.inc.php');
  40. require_once('../application/csvpage.class.inc.php');
  41. require_once('../application/xmlpage.class.inc.php');
  42. require_once('../application/startup.inc.php');
  43. require_once('../application/loginwebpage.class.inc.php');
  44. class WebServiceException extends Exception
  45. {
  46. }
  47. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  48. $oAppContext = new ApplicationContext();
  49. //$iActiveNodeId = utils::ReadParam('menu', -1);
  50. //$currentOrganization = utils::ReadParam('org_id', '');
  51. // Main program
  52. //$oP = new XMLPage("iTop - Bulk import");
  53. $oP = new WebPage("iTop - Bulk import");
  54. $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>');
  55. try
  56. {
  57. $sClass = utils::ReadParam('class', '');
  58. $sSep = utils::ReadParam('separator', ';');
  59. $sCSVData = utils::ReadPostedParam('csvdata');
  60. $oCSVParser = new CSVParser($sCSVData, $sSep, $sDelimiter = '"');
  61. // Limitation: as the attribute list is in the first line, we can not match external key by a third-party attribute
  62. $sRawFieldList = $oCSVParser->ListFields();
  63. $aAttList = array();
  64. $aExtKeys = array();
  65. foreach($sRawFieldList as $iFieldId => $sFieldName)
  66. {
  67. if (!MetaModel::IsValidAttCode($sClass, $sFieldName))
  68. {
  69. throw new WebServiceException("Unknown attribute '$sFieldName' (class: '$sClass')");
  70. }
  71. $oAtt = MetaModel::GetAttributeDef($sClass, $sFieldName);
  72. if ($oAtt->IsExternalKey())
  73. {
  74. $aExtKeys[$sFieldName]['id'] = $iFieldId;
  75. $aAttList[$sFieldName] = $iFieldId;
  76. }
  77. elseif ($oAtt->IsExternalField())
  78. {
  79. $sExtKeyAttCode = $oAtt->GetKeyAttCode();
  80. $sRemoteAttCode = $oAtt->GetExtAttCode();
  81. $aExtKeys[$sExtKeyAttCode][$sRemoteAttCode] = $iFieldId;
  82. }
  83. else
  84. {
  85. $aAttList[$sFieldName] = $iFieldId;
  86. }
  87. }
  88. // Limitation: the reconciliation key is the first attribute
  89. $aReconcilKeys = array($sRawFieldList[0]);
  90. $aData = $oCSVParser->ToArray();
  91. $oBulk = new BulkChange(
  92. $sClass,
  93. $aData,
  94. $aAttList,
  95. $aExtKeys,
  96. $aReconcilKeys
  97. );
  98. $oMyChange = MetaModel::NewObject("CMDBChange");
  99. $oMyChange->Set("date", time());
  100. if (UserRights::IsImpersonated())
  101. {
  102. $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
  103. }
  104. else
  105. {
  106. $sUserString = UserRights::GetUser();
  107. }
  108. $oMyChange->Set("userinfo", $sUserString.' (bulk load by web service)');
  109. $iChangeId = $oMyChange->DBInsert();
  110. $aRes = $oBulk->Process($oMyChange);
  111. // Setup result presentation
  112. //
  113. $aDisplayConfig = array();
  114. $aDisplayConfig["__RECONCILIATION__"] = array("label"=>"Reconciliation", "description"=>"");
  115. $aDisplayConfig["__STATUS__"] = array("label"=>"Status", "description"=>"");
  116. if (isset($iPKeyId))
  117. {
  118. $aDisplayConfig["col$iPKeyId"] = array("label"=>"<strong>id</strong>", "description"=>"");
  119. }
  120. foreach($aReconcilKeys as $iCol => $sAttCode)
  121. {
  122. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  123. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  124. }
  125. foreach ($aAttList as $sAttCode => $iCol)
  126. {
  127. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  128. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  129. }
  130. $aResultDisp = array(); // to be displayed
  131. foreach($aRes as $iRow => $aRowData)
  132. {
  133. $aRowDisp = array();
  134. $aRowDisp["__RECONCILIATION__"] = $aRowData["__RECONCILIATION__"];
  135. $aRowDisp["__STATUS__"] = $aRowData["__STATUS__"]->GetDescription();
  136. foreach($aRowData as $sKey => $value)
  137. {
  138. if ($sKey == '__RECONCILIATION__') continue;
  139. if ($sKey == '__STATUS__') continue;
  140. switch (get_class($value))
  141. {
  142. case 'CellStatus_Void':
  143. $sClass = '';
  144. break;
  145. case 'CellStatus_Modify':
  146. $sClass = 'csvimport_ok';
  147. break;
  148. case 'CellStatus_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. ?>