import.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. $oContext = new UserContext();
  49. $oAppContext = new ApplicationContext();
  50. //$iActiveNodeId = utils::ReadParam('menu', -1);
  51. //$currentOrganization = utils::ReadParam('org_id', '');
  52. // Main program
  53. //$oP = new XMLPage("iTop - Bulk import");
  54. $oP = new WebPage("iTop - Bulk import");
  55. $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>');
  56. try
  57. {
  58. $sClass = utils::ReadParam('class', '');
  59. $sSep = utils::ReadParam('separator', ';');
  60. $sCSVData = utils::ReadPostedParam('csvdata');
  61. $oCSVParser = new CSVParser($sCSVData, $sSep, $sDelimiter = '"');
  62. // Limitation: as the attribute list is in the first line, we can not match external key by a third-party attribute
  63. $sRawFieldList = $oCSVParser->ListFields();
  64. $aAttList = array();
  65. $aExtKeys = array();
  66. foreach($sRawFieldList as $iFieldId => $sFieldName)
  67. {
  68. if (!MetaModel::IsValidAttCode($sClass, $sFieldName))
  69. {
  70. throw new WebServiceException("Unknown attribute '$sFieldName' (class: '$sClass')");
  71. }
  72. $oAtt = MetaModel::GetAttributeDef($sClass, $sFieldName);
  73. if ($oAtt->IsExternalKey())
  74. {
  75. $aExtKeys[$sFieldName]['id'] = $iFieldId;
  76. $aAttList[$sFieldName] = $iFieldId;
  77. }
  78. elseif ($oAtt->IsExternalField())
  79. {
  80. $sExtKeyAttCode = $oAtt->GetKeyAttCode();
  81. $sRemoteAttCode = $oAtt->GetExtAttCode();
  82. $aExtKeys[$sExtKeyAttCode][$sRemoteAttCode] = $iFieldId;
  83. }
  84. else
  85. {
  86. $aAttList[$sFieldName] = $iFieldId;
  87. }
  88. }
  89. // Limitation: the reconciliation key is the first attribute
  90. $aReconcilKeys = array($sRawFieldList[0]);
  91. $aData = $oCSVParser->ToArray();
  92. $oBulk = new BulkChange(
  93. $sClass,
  94. $aData,
  95. $aAttList,
  96. $aExtKeys,
  97. $aReconcilKeys
  98. );
  99. $oMyChange = MetaModel::NewObject("CMDBChange");
  100. $oMyChange->Set("date", time());
  101. if (UserRights::GetUser() != UserRights::GetRealUser())
  102. {
  103. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  104. }
  105. else
  106. {
  107. $sUserString = UserRights::GetUser();
  108. }
  109. $oMyChange->Set("userinfo", $sUserString.' (bulk load by web service)');
  110. $iChangeId = $oMyChange->DBInsert();
  111. $aRes = $oBulk->Process($oMyChange);
  112. // Setup result presentation
  113. //
  114. $aDisplayConfig = array();
  115. $aDisplayConfig["__RECONCILIATION__"] = array("label"=>"Reconciliation", "description"=>"");
  116. $aDisplayConfig["__STATUS__"] = array("label"=>"Status", "description"=>"");
  117. if (isset($iPKeyId))
  118. {
  119. $aDisplayConfig["col$iPKeyId"] = array("label"=>"<strong>id</strong>", "description"=>"");
  120. }
  121. foreach($aReconcilKeys as $iCol => $sAttCode)
  122. {
  123. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  124. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  125. }
  126. foreach ($aAttList as $sAttCode => $iCol)
  127. {
  128. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  129. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  130. }
  131. $aResultDisp = array(); // to be displayed
  132. foreach($aRes as $iRow => $aRowData)
  133. {
  134. $aRowDisp = array();
  135. $aRowDisp["__RECONCILIATION__"] = $aRowData["__RECONCILIATION__"];
  136. $aRowDisp["__STATUS__"] = $aRowData["__STATUS__"]->GetDescription();
  137. foreach($aRowData as $sKey => $value)
  138. {
  139. if ($sKey == '__RECONCILIATION__') continue;
  140. if ($sKey == '__STATUS__') continue;
  141. switch (get_class($value))
  142. {
  143. case 'CellStatus_Void':
  144. $sClass = '';
  145. break;
  146. case 'CellStatus_Modify':
  147. $sClass = 'csvimport_ok';
  148. break;
  149. case 'CellStatus_Issue':
  150. $sClass = 'csvimport_error';
  151. break;
  152. }
  153. if (empty($sClass))
  154. {
  155. $aRowDisp[$sKey] = $value->GetDescription();
  156. }
  157. else
  158. {
  159. $aRowDisp[$sKey] = "<div class=\"$sClass\">".$value->GetDescription()."</div>";
  160. }
  161. }
  162. $aResultDisp[$iRow] = $aRowDisp;
  163. }
  164. $oP->table($aDisplayConfig, $aResultDisp);
  165. }
  166. catch(Exception $e)
  167. {
  168. $oP->add('<error>'.((string)$e).'</error>');
  169. }
  170. $oP->output();
  171. ?>