csvimport.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <?php
  2. require_once('../application/application.inc.php');
  3. require_once('../application/itopwebpage.class.inc.php');
  4. require_once('../application/startup.inc.php');
  5. require_once('../application/loginwebpage.class.inc.php');
  6. login_web_page::DoLogin(); // Check user rights and prompt if needed
  7. $oContext = new UserContext();
  8. $oAppContext = new ApplicationContext();
  9. $iActiveNodeId = utils::ReadParam('menu', -1);
  10. $currentOrganization = utils::ReadParam('org_id', 1);
  11. $oPage = new iTopWebPage("iTop - Bulk import", $currentOrganization);
  12. define ('EXTKEY_SEP', '::::');
  13. define ('EXTKEY_LABELSEP', ' -> ');
  14. ///////////////////////////////////////////////////////////////////////////////
  15. // External key/field naming conventions (sharing the naming space with std attributes
  16. ///////////////////////////////////////////////////////////////////////////////
  17. function IsExtKeyField($sColDesc)
  18. {
  19. return ($iPos = strpos($sColDesc, EXTKEY_SEP));
  20. }
  21. function GetExtKeyFieldCodes($sColDesc)
  22. {
  23. $iPos = strpos($sColDesc, EXTKEY_SEP);
  24. return array(
  25. substr($sColDesc, 0, $iPos),
  26. substr($sColDesc, $iPos + strlen(EXTKEY_SEP))
  27. );
  28. }
  29. function MakeExtFieldLabel($sClass, $sExtKeyAttCode, $sForeignAttCode)
  30. {
  31. $oExtKeyAtt = MetaModel::GetAttributeDef($sClass, $sExtKeyAttCode);
  32. if ($sForeignAttCode == 'id')
  33. {
  34. $sForeignAttLabel = 'id';
  35. }
  36. else
  37. {
  38. $oForeignAtt = MetaModel::GetAttributeDef($oExtKeyAtt->GetTargetClass(), $sForeignAttCode);
  39. $sForeignAttLabel = $oForeignAtt->GetLabel();
  40. }
  41. return $oExtKeyAtt->GetLabel().EXTKEY_LABELSEP.$sForeignAttLabel;
  42. }
  43. function MakeExtFieldSelectValue($sAttCode, $sExtAttCode)
  44. {
  45. return $sAttCode.EXTKEY_SEP.$sExtAttCode;
  46. }
  47. ///////////////////////////////////////////////////////////////////////////////
  48. ///////////////////////////////////////////////////////////////////////////////
  49. function ShowTableForm($oPage, $oCSVParser, $sClass)
  50. {
  51. $aData = $oCSVParser->ToArray(null, 3);
  52. $aColToRow = array();
  53. foreach($aData as $aRow)
  54. {
  55. foreach ($aRow as $sFieldId=>$sValue)
  56. {
  57. $aColToRow[$sFieldId][] = $sValue;
  58. }
  59. }
  60. $aFields = array();
  61. foreach($oCSVParser->ListFields() as $iFieldIndex=>$sFieldName)
  62. {
  63. $sFieldName = trim($sFieldName);
  64. $aOptions = array();
  65. $aOptions['id'] = array(
  66. 'LabelHtml' => "Private key",
  67. 'LabelRef' => "Private key",
  68. 'IsReconcKey' => false,
  69. 'Tip' => '',
  70. );
  71. $sFoundAttCode = ""; // quick and dirty way to remind if a match has been found and suggest a reconciliation key if possible
  72. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAtt)
  73. {
  74. if ($oAtt->IsExternalField()) continue;
  75. $bIsThatField = (strcasecmp($sFieldName, $oAtt->GetLabel()) == 0);
  76. $sFoundAttCode = (MetaModel::IsValidFilterCode($sClass, $sAttCode) && $bIsThatField) ? $sAttCode : $sFoundAttCode;
  77. if ($oAtt->IsExternalKey())
  78. {
  79. // An external key might be loaded by
  80. // the pkey or a reconciliation key
  81. //
  82. $aOptions[MakeExtFieldSelectValue($sAttCode, 'id')] = array(
  83. 'LabelHtml' => "<em>".$oAtt->GetLabel()."</em> (id)",
  84. 'LabelRef' => $oAtt->GetLabel(),
  85. 'IsReconcKey' => MetaModel::IsReconcKey($sClass, $sAttCode),
  86. 'Tip' => '',
  87. );
  88. $sRemoteClass = $oAtt->GetTargetClass();
  89. foreach(MetaModel::GetReconcKeys($sRemoteClass) as $sExtAttCode)
  90. {
  91. $sValue = MakeExtFieldSelectValue($sAttCode, $sExtAttCode);
  92. // Create two entries:
  93. // - generic syntax (ext key label -> remote field label)
  94. // - if an ext field exists that corresponds to it, allow its label
  95. $sLabel1 = MakeExtFieldLabel($sClass, $sAttCode, $sExtAttCode);
  96. $bFoundTwin = false;
  97. foreach (MetaModel::GetExternalFields($sClass, $sAttCode) as $oExtFieldAtt)
  98. {
  99. if ($oExtFieldAtt->GetExtAttCode() == $sExtAttCode)
  100. {
  101. $aOptions[$sValue] = array(
  102. 'LabelHtml' => htmlentities($oExtFieldAtt->GetLabel()),
  103. 'LabelRef' => $oExtFieldAtt->GetLabel(),
  104. 'IsReconcKey' => false,
  105. 'Tip' => "equivalent to '".htmlentities($sLabel1)."'",
  106. );
  107. $bFoundTwin = true;
  108. $sLabel2 = $oExtFieldAtt->GetLabel();
  109. break;
  110. }
  111. }
  112. $aOptions[$sValue] = array(
  113. 'LabelHtml' => htmlentities($sLabel1),
  114. 'LabelRef' => $sLabel1,
  115. 'IsReconcKey' => false,
  116. 'Tip' => $bFoundTwin ? "equivalent to '".htmlentities($sLabel2)."'" : "",
  117. );
  118. }
  119. }
  120. else
  121. {
  122. $aOptions[$sAttCode] = array(
  123. 'LabelHtml' => htmlentities($oAtt->GetLabel()),
  124. 'LabelRef' => $oAtt->GetLabel(),
  125. 'IsReconcKey' => MetaModel::IsReconcKey($sClass, $sAttCode),
  126. 'Tip' => '',
  127. );
  128. }
  129. }
  130. // Find the best match
  131. $iMin = strlen($sFieldName);
  132. $sBestValue = null;
  133. foreach ($aOptions as $sValue => $aData)
  134. {
  135. $iDist = levenshtein(strtolower($sFieldName), strtolower($aData['LabelRef']));
  136. if (($iDist != -1) && ($iDist < $iMin))
  137. {
  138. $iMin = $iDist;
  139. $sBestValue = $sValue;
  140. }
  141. }
  142. $sSelField = "<select name=\"fmap[field$iFieldIndex]\">";
  143. foreach ($aOptions as $sValue => $aData)
  144. {
  145. $sStyle = '';
  146. $sComment = '';
  147. $sSELECTED = '';
  148. if ($sValue == $sBestValue)
  149. {
  150. $sSELECTED = ' SELECTED';
  151. if ($iMin > 0)
  152. {
  153. $sStyle = " style=\"background-color: #ffdddd;\"";
  154. $sComment = '- suggested';
  155. }
  156. }
  157. $sIsRecondKey = $aData['IsReconcKey'] ? " [rk!]" : "";
  158. $sSelField .= "<option value=\"$sValue\" title=\"".$aData['Tip']."\"$sStyle$sSELECTED>".$aData['LabelHtml']."$sComment$sIsRecondKey</option>\n";
  159. }
  160. $sSelField .= "</select>";
  161. $aFields["field$iFieldIndex"]["label"] = $sSelField;
  162. $sCHECKED = ($sFieldName == "id" || MetaModel::IsReconcKey($sClass, $sFoundAttCode)) ? " CHECKED" : "";
  163. $aFields["field$iFieldIndex"]["label"] .= "<input type=\"checkbox\" name=\"iskey[field$iFieldIndex]\" value=\"yes\" $sCHECKED>";
  164. if (array_key_exists($iFieldIndex, $aColToRow))
  165. {
  166. $aFields["field$iFieldIndex"]["value"] = $aColToRow[$iFieldIndex];
  167. }
  168. }
  169. $oPage->details($aFields);
  170. }
  171. function ProcessData($oPage, $sClass, $oCSVParser, $aFieldMap, $aIsReconcKey, CMDBChange $oChange = null)
  172. {
  173. // Note: $oChange can be null, in which case the aim is to check what would be done
  174. // Setup field mapping: sort out between values and other specific columns
  175. //
  176. $aReconcilKeys = array();
  177. $aAttList = array();
  178. $aExtKeys = array();
  179. foreach($aFieldMap as $sFieldId=>$sColDesc)
  180. {
  181. $iFieldId = (int) substr($sFieldId, strlen("field"));
  182. if (array_key_exists($sFieldId, $aIsReconcKey))
  183. {
  184. // This column will be used as a reconciliation key
  185. if (IsExtKeyField($sColDesc))
  186. {
  187. list($sAttCode, $sExtReconcKeyAttCode) = GetExtKeyFieldCodes($sColDesc);
  188. }
  189. else
  190. {
  191. $sAttCode = $sColDesc;
  192. }
  193. $aReconcilKeys[$sAttCode] = $iFieldId;
  194. }
  195. if ($sColDesc == "id")
  196. {
  197. $aAttList['id'] = $iFieldId;
  198. }
  199. elseif ($sColDesc == "__none__")
  200. {
  201. // Skip !
  202. }
  203. elseif (IsExtKeyField($sColDesc))
  204. {
  205. // This field is value to search on, to find a value for an external key
  206. list($sExtKeyAttCode, $sExtReconcKeyAttCode) = GetExtKeyFieldCodes($sColDesc);
  207. if ($sExtKeyAttCode == $sExtReconcKeyAttCode)
  208. {
  209. $aAttList[$sExtKeyAttCode] = $iFieldId;
  210. }
  211. $aExtKeys[$sExtKeyAttCode][$sExtReconcKeyAttCode] = $iFieldId;
  212. }
  213. else
  214. {
  215. // $sColDesc is an attribute code
  216. $aAttList[$sColDesc] = $iFieldId;
  217. }
  218. }
  219. // Setup result presentation
  220. //
  221. $aDisplayConfig = array();
  222. $aDisplayConfig["__RECONCILIATION__"] = array("label"=>"Reconciliation", "description"=>"");
  223. $aDisplayConfig["__STATUS__"] = array("label"=>"Import status", "description"=>"");
  224. if (array_key_exists('id', $aAttList))
  225. {
  226. $sPKeyCol = 'col'.$aAttList['id'];
  227. $aDisplayConfig[$sPKeyCol] = array("label"=>"<strong>id</strong>", "description"=>"");
  228. }
  229. foreach($aReconcilKeys as $sAttCode => $iCol)
  230. {
  231. if ($sAttCode == 'id') continue;
  232. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  233. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  234. }
  235. foreach($aExtKeys as $sAttCode=>$aKeyConfig)
  236. {
  237. $oExtKeyAtt = MetaModel::GetAttributeDef($sClass, $sAttCode);
  238. $sLabel = $oExtKeyAtt->GetLabel();
  239. $aDisplayConfig[$sAttCode] = array("label"=>"$sLabel", "description"=>"");
  240. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  241. {
  242. // The foreign attribute is one of our reconciliation key
  243. $sLabel = MakeExtFieldLabel($sClass, $sAttCode, $sForeignAttCode);
  244. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  245. }
  246. }
  247. foreach ($aAttList as $sAttCode => $iCol)
  248. {
  249. if ($sAttCode != 'id')
  250. {
  251. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  252. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  253. }
  254. }
  255. // Compute the results
  256. //
  257. $aData = $oCSVParser->ToArray();
  258. $oBulk = new BulkChange(
  259. $sClass,
  260. $aData,
  261. $aAttList,
  262. array_keys($aReconcilKeys),
  263. $aExtKeys
  264. );
  265. $aRes = $oBulk->Process($oChange);
  266. $aResultDisp = array(); // to be displayed
  267. foreach($aRes as $iRow => $aRowData)
  268. {
  269. $aRowDisp = array();
  270. $aRowDisp["__RECONCILIATION__"] = $aRowData["__RECONCILIATION__"];
  271. $aRowDisp["__STATUS__"] = $aRowData["__STATUS__"]->GetDescription(true);
  272. foreach($aRowData as $sKey => $value)
  273. {
  274. if ($sKey == '__RECONCILIATION__') continue;
  275. if ($sKey == '__STATUS__') continue;
  276. switch (get_class($value))
  277. {
  278. case 'CellChangeSpec_Unchanged':
  279. $sClass = '';
  280. break;
  281. case 'CellChangeSpec_Modify':
  282. $sClass = 'csvimport_ok';
  283. break;
  284. case 'CellChangeSpec_Init':
  285. $sClass = 'csvimport_init';
  286. break;
  287. case 'CellChangeSpec_Issue':
  288. $sClass = 'csvimport_error';
  289. break;
  290. case 'CellChangeSpec_Void':
  291. default:
  292. $sClass = '';
  293. }
  294. if (empty($sClass))
  295. {
  296. $aRowDisp[$sKey] = $value->GetDescription(true);
  297. }
  298. else
  299. {
  300. $aRowDisp[$sKey] = "<div class=\"$sClass\">".$value->GetDescription(true)."</div>";
  301. }
  302. }
  303. $aResultDisp[$iRow] = $aRowDisp;
  304. }
  305. $oPage->table($aDisplayConfig, $aResultDisp);
  306. }
  307. ///////////////////////////////////////////////////////////////////////////////
  308. // Wizard entry points
  309. ///////////////////////////////////////////////////////////////////////////////
  310. function Do_Welcome($oPage, $sClass)
  311. {
  312. $sWiztep = "1_welcome";
  313. $oPage->p("<h1>Bulk load from CSV data / step 1</h1>");
  314. $sCSVData = utils::ReadPostedParam('csvdata');
  315. $oPage->add("<form method=\"post\" action=\"\">");
  316. $oPage->MakeClassesSelect("class", $sClass, 50, UR_ACTION_BULK_MODIFY);
  317. $oPage->add("<br/>");
  318. $oPage->add("<textarea rows=\"25\" cols=\"100\" name=\"csvdata\" wrap=\"soft\">$sCSVData</textarea>");
  319. $oPage->add("<br/>");
  320. $oPage->add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
  321. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Next\"><br/>\n");
  322. $oPage->add("</form>");
  323. // As a help to the end-user, let's display the list of possible fields
  324. // for a class, that can be copied/pasted into the CSV area.
  325. $sCurrentList = "";
  326. $aHeadersList = array();
  327. foreach(MetaModel::GetClasses('bizmodel') as $sClassName)
  328. {
  329. $aList = MetaModel::GetZListItems($sClassName, 'details');
  330. $aHeader = array();
  331. // $aHeader[] = MetaModel::GetKeyLabel($sClassName);
  332. $aHeader[] = 'id'; // Should be what's coded on the line above... but there is a bug
  333. foreach($aList as $sAttCode)
  334. {
  335. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode);
  336. }
  337. $sAttributes = implode(",", $aHeader);
  338. $aHeadersList[$sClassName] = $sAttributes;
  339. if($sClassName == $sClass)
  340. {
  341. // this class is currently selected
  342. $sCurrentList = $sAttributes;
  343. }
  344. }
  345. // Store all the values in a variable client-side
  346. $aScript = array();
  347. foreach($aHeadersList as $sClassName => $sAttributes)
  348. {
  349. $aScript[] = "'$sClassName':'$sAttributes'";
  350. }
  351. $oPage->add("<script>
  352. var oAttributes = {".implode(',', $aScript)."};
  353. function DisplayFields(className)
  354. {
  355. $('#fields').val(oAttributes[className]);
  356. }
  357. </script>\n");
  358. $oPage->add_ready_script("$('#select_class').change( function() {DisplayFields(this.value);} );");
  359. $oPage->add("<br/>");
  360. $oPage->add("Fields for this object<br/><textarea readonly id=fields rows=\"3\" cols=\"100\" wrap=\"soft\">$sCurrentList</textarea>");
  361. }
  362. function Do_Format($oPage, $sClass)
  363. {
  364. $oPage->p("<h1>Bulk load from CSV data / step 2</h1>");
  365. $sWiztep = "2_format";
  366. $sCSVData = utils::ReadPostedParam('csvdata');
  367. $oCSVParser = new CSVParser($sCSVData);
  368. $sSep = $oCSVParser->GuessSeparator();
  369. $iSkip = $oCSVParser->GuessSkipLines();
  370. // No data ?
  371. $aData = $oCSVParser->ToArray(null);
  372. $iTarget = count($aData);
  373. if ($iTarget == 0)
  374. {
  375. $oPage->p("Empty data set..., please provide some data!");
  376. $oPage->add("<button onClick=\"window.history.back();\">Back</button>\n");
  377. return;
  378. }
  379. // Guess the format :
  380. $oPage->p("Guessed separator: '<strong>$sSep</strong>' (ASCII=".ord($sSep).")");
  381. $oPage->p("Guessed # of lines to skip: $iSkip");
  382. $oPage->p("Target: $iTarget rows");
  383. $oPage->Add("<form method=\"post\" action=\"\">");
  384. ShowTableForm($oPage, $oCSVParser, $sClass);
  385. $oPage->Add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  386. $oPage->Add("<input type=\"hidden\" name=\"csvdata\" value=\"$sCSVData\">");
  387. $oPage->Add("<input type=\"hidden\" name=\"separator\" value=\"$sSep\">");
  388. $oPage->Add("<input type=\"hidden\" name=\"skiplines\" value=\"$iSkip\">");
  389. $oPage->Add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
  390. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Back\">");
  391. $oPage->Add("<input type=\"submit\" name=\"todo\" value=\"Next\">");
  392. $oPage->Add("</form>");
  393. }
  394. function DoProcessOrVerify($oPage, $sClass, CMDBChange $oChange = null)
  395. {
  396. $sCSVData = utils::ReadPostedParam('csvdata');
  397. $sSep = utils::ReadPostedParam('separator');
  398. $iSkip = utils::ReadPostedParam('skiplines');
  399. $aFieldMap = utils::ReadPostedParam('fmap');
  400. $aIsReconcKey = utils::ReadPostedParam('iskey');
  401. if (empty($aIsReconcKey))
  402. {
  403. $oPage->p("Error: no reconciliation key has been specified. Please specify which field(s) will be used to identify the object");
  404. $oPage->add("<button onClick=\"window.history.back();\">Back</button>\n");
  405. $oPage->add("<button disabled>Next</button>\n");
  406. return;
  407. }
  408. $oCSVParser = new CSVParser($sCSVData);
  409. $oCSVParser->SetSeparator($sSep);
  410. $oCSVParser->SetSkipLines($iSkip);
  411. $aData = $oCSVParser->ToArray(null);
  412. $iTarget = count($aData);
  413. $oPage->p("<h2>Goal summary</h2>");
  414. $oPage->p("Target: $iTarget rows");
  415. $aSampleData = $oCSVParser->ToArray(array_keys($aFieldMap), 5);
  416. $aDisplayConfig = array();
  417. $aExtKeys = array();
  418. foreach ($aFieldMap as $sFieldId=>$sColDesc)
  419. {
  420. if (array_key_exists($sFieldId, $aIsReconcKey))
  421. {
  422. $sReconcKey = " <br/><span title=\"the value found in this column will be used as a search condition for the reconciliation\" style=\"background-color: #aaaa00; color: #dddddd;\">[key]</span>";
  423. }
  424. else
  425. {
  426. $sReconcKey = "";
  427. }
  428. if ($sColDesc == "id")
  429. {
  430. $aDisplayConfig[$sFieldId] = array("label"=>"Private key $sReconcKey", "description"=>"");
  431. }
  432. elseif ($sColDesc == "__none__")
  433. {
  434. // Skip !
  435. }
  436. else if (MetaModel::IsValidAttCode($sClass, $sColDesc))
  437. {
  438. $sAttCode = $sColDesc;
  439. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  440. $aDisplayConfig[$sFieldId] = array("label"=>"$sLabel$sReconcKey", "description"=>"");
  441. if (MetaModel::IsValidKeyAttCode($sClass, $sAttCode))
  442. {
  443. $aExtKeys[] = $sAttCode;
  444. }
  445. }
  446. elseif (IsExtKeyField($sColDesc))
  447. {
  448. list($sExtKeyAttCode, $sForeignAttCode) = GetExtKeyFieldCodes($sColDesc);
  449. $sLabel = MakeExtFieldLabel($sClass, $sExtKeyAttCode, $sForeignAttCode);
  450. $aDisplayConfig[$sFieldId] = array("label"=>"$sLabel$sReconcKey", "description"=>"");
  451. $aExtKeys[] = $sExtKeyAttCode;
  452. }
  453. else
  454. {
  455. // ???
  456. $aDisplayConfig[$sFieldId] = array("label"=>"-?-?-$sColDesc-?-?-", "description"=>"");
  457. }
  458. }
  459. $oPage->table($aDisplayConfig, $aSampleData);
  460. if ($oChange)
  461. {
  462. $oPage->p("<h2>Processing...</h2>");
  463. }
  464. else
  465. {
  466. $oPage->p("<h2>Column consistency</h2>");
  467. $aMissingKeys = array();
  468. foreach (MetaModel::GetExternalKeys($sClass) as $sExtKeyAttCode => $oExtKey)
  469. {
  470. if (!in_array($sExtKeyAttCode, $aExtKeys) && !$oExtKey->IsNullAllowed())
  471. {
  472. $aMissingKeys[$sExtKeyAttCode] = $oExtKey;
  473. }
  474. }
  475. if (count($aMissingKeys) > 0)
  476. {
  477. $oPage->p("Warning: the objects could not be created, due to some missing mandatory external keys in the field list: ");
  478. $oPage->add("<ul>");
  479. foreach($aMissingKeys as $sAttCode => $oAttDef)
  480. {
  481. $oPage->add("<li>".$oAttDef->GetLabel()."</li>");
  482. }
  483. $oPage->add("</ul>");
  484. }
  485. else
  486. {
  487. $oPage->p("ok - required external keys (if any) have been found in the field list");
  488. }
  489. $oPage->p("Note: the procedure will fail if any line has not the same number of columns as the first line");
  490. $oPage->p("<h2>Check...</h2>");
  491. }
  492. ProcessData($oPage, $sClass, $oCSVParser, $aFieldMap, $aIsReconcKey, $oChange);
  493. $oPage->add("<form method=\"post\" action=\"\">");
  494. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  495. $oPage->add("<input type=\"hidden\" name=\"csvdata\" value=\"$sCSVData\">");
  496. $oPage->add("<input type=\"hidden\" name=\"separator\" value=\"$sSep\">");
  497. $oPage->add("<input type=\"hidden\" name=\"skiplines\" value=\"$iSkip\">");
  498. $oPage->add_input_hidden("fmap", $aFieldMap);
  499. $oPage->add_input_hidden("iskey", $aIsReconcKey);
  500. return true;
  501. }
  502. function Do_Verify($oPage, $sClass)
  503. {
  504. $oPage->p("<h1>Bulk load from CSV data / step 3</h1>");
  505. $sWiztep = "3_verify";
  506. if (DoProcessOrVerify($oPage, $sClass, null))
  507. {
  508. // FORM started by DoProcessOrVerify...
  509. $oPage->add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
  510. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Back\">");
  511. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Next\">");
  512. $oPage->add("</form>");
  513. }
  514. }
  515. function Do_Execute($oPage, $sClass)
  516. {
  517. $oPage->p("<h1>Bulk load from CSV data / step 4</h1>");
  518. $sWiztep = "4_execute";
  519. $oMyChange = MetaModel::NewObject("CMDBChange");
  520. $oMyChange->Set("date", time());
  521. $iUser = UserRights::GetContactId();
  522. if ($iUser != null)
  523. {
  524. // Ok, that's dirty, I admit :-)
  525. $oUser = MetaModel::GetObject('bizContact', $iUser);
  526. $sUser = $oUser->GetName();
  527. $oMyChange->Set("userinfo", "CSV Import, by ".$sUser);
  528. }
  529. else
  530. {
  531. $oMyChange->Set("userinfo", "CSV Import");
  532. }
  533. $iChangeId = $oMyChange->DBInsert();
  534. if (DoProcessOrVerify($oPage, $sClass, $oMyChange))
  535. {
  536. // FORM started by DoProcessOrVerify...
  537. $oPage->add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
  538. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Back\">");
  539. $oPage->add("</form>");
  540. }
  541. }
  542. ///////////////////////////////////////////////////////////////////////////////////////////////////
  543. //
  544. // M a i n P r o g r a m
  545. //
  546. ///////////////////////////////////////////////////////////////////////////////////////////////////
  547. $sFromWiztep = utils::ReadPostedParam('fromwiztep', '');
  548. $sClass = utils::ReadPostedParam('class', '');
  549. $sTodo = utils::ReadPostedParam('todo', '');
  550. switch($sFromWiztep)
  551. {
  552. case '':
  553. Do_Welcome($oPage, $sClass);
  554. break;
  555. case '1_welcome':
  556. if ($sTodo == "Next") Do_Format($oPage, $sClass);
  557. else trigger_error("Wrong argument todo='$sTodo'", E_USER_ERROR);
  558. break;
  559. case '2_format':
  560. if ($sTodo == "Next") Do_Verify($oPage, $sClass);
  561. else Do_Welcome($oPage, $sClass);
  562. break;
  563. case '3_verify':
  564. if ($sTodo == "Next") Do_Execute($oPage, $sClass);
  565. else Do_Format($oPage, $sClass);
  566. break;
  567. case '4_execute':
  568. if ($sTodo == "Next") trigger_error("Wrong argument todo='$sTodo'", E_USER_ERROR);
  569. else Do_Verify($oPage, $sClass);
  570. break;
  571. default:
  572. trigger_error("Wrong argument fromwiztep='$sFromWiztep'", E_USER_ERROR);
  573. }
  574. $oPage->output();
  575. ?>