csvimport.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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()." (id)",
  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. $iPKeyId = null;
  177. $aReconcilKeys = array();
  178. $aAttList = array();
  179. $aExtKeys = array();
  180. foreach($aFieldMap as $sFieldId=>$sColDesc)
  181. {
  182. $iFieldId = (int) substr($sFieldId, strlen("field"));
  183. if ($sColDesc == "id")
  184. {
  185. // Skip !
  186. $iPKeyId = $iFieldId;
  187. }
  188. elseif ($sColDesc == "__none__")
  189. {
  190. // Skip !
  191. }
  192. elseif (IsExtKeyField($sColDesc))
  193. {
  194. // This field is value to search on, to find a value for an external key
  195. list($sExtKeyAttCode, $sExtReconcKeyAttCode) = GetExtKeyFieldCodes($sColDesc);
  196. $aExtKeys[$sExtKeyAttCode][$sExtReconcKeyAttCode] = $iFieldId;
  197. }
  198. elseif (array_key_exists($sFieldId, $aIsReconcKey))
  199. {
  200. // This value is a reconciliation key
  201. $aReconcilKeys[$sColDesc] = $iFieldId;
  202. $aAttList[$sColDesc] = $iFieldId; // A reconciliation key is also a field
  203. }
  204. else
  205. {
  206. // $sColDesc is an attribute code
  207. $aAttList[$sColDesc] = $iFieldId;
  208. }
  209. }
  210. // Setup result presentation
  211. //
  212. $aDisplayConfig = array();
  213. $aDisplayConfig["__RECONCILIATION__"] = array("label"=>"Reconciliation", "description"=>"");
  214. $aDisplayConfig["__STATUS__"] = array("label"=>"Import status", "description"=>"");
  215. if (isset($iPKeyId))
  216. {
  217. $aDisplayConfig["col$iPKeyId"] = array("label"=>"<strong>id</strong>", "description"=>"");
  218. }
  219. foreach($aReconcilKeys as $sAttCode => $iCol)
  220. {
  221. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  222. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  223. }
  224. foreach($aExtKeys as $sAttCode=>$aKeyConfig)
  225. {
  226. $oExtKeyAtt = MetaModel::GetAttributeDef($sClass, $sAttCode);
  227. $sLabel = $oExtKeyAtt->GetLabel();
  228. $aDisplayConfig[$sAttCode] = array("label"=>"$sLabel", "description"=>"");
  229. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  230. {
  231. // The foreign attribute is one of our reconciliation key
  232. $sLabel = MakeExtFieldLabel($sClass, $sAttCode, $sForeignAttCode);
  233. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  234. }
  235. }
  236. foreach ($aAttList as $sAttCode => $iCol)
  237. {
  238. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  239. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  240. }
  241. // Compute the results
  242. //
  243. $aData = $oCSVParser->ToArray();
  244. $oBulk = new BulkChange(
  245. $sClass,
  246. $aData,
  247. $aAttList,
  248. array_keys($aReconcilKeys),
  249. $aExtKeys
  250. );
  251. $aRes = $oBulk->Process($oChange);
  252. $aResultDisp = array(); // to be displayed
  253. foreach($aRes as $iRow => $aRowData)
  254. {
  255. $aRowDisp = array();
  256. $aRowDisp["__RECONCILIATION__"] = $aRowData["__RECONCILIATION__"];
  257. $aRowDisp["__STATUS__"] = $aRowData["__STATUS__"]->GetDescription(true);
  258. foreach($aRowData as $sKey => $value)
  259. {
  260. if ($sKey == '__RECONCILIATION__') continue;
  261. if ($sKey == '__STATUS__') continue;
  262. switch (get_class($value))
  263. {
  264. case 'CellChangeSpec_Void':
  265. $sClass = '';
  266. break;
  267. case 'CellChangeSpec_Unchanged':
  268. $sClass = '';
  269. break;
  270. case 'CellChangeSpec_Modify':
  271. $sClass = 'csvimport_ok';
  272. break;
  273. case 'CellChangeSpec_Init':
  274. $sClass = 'csvimport_init';
  275. break;
  276. case 'CellChangeSpec_Issue':
  277. $sClass = 'csvimport_error';
  278. break;
  279. }
  280. if (empty($sClass))
  281. {
  282. $aRowDisp[$sKey] = $value->GetDescription(true);
  283. }
  284. else
  285. {
  286. $aRowDisp[$sKey] = "<div class=\"$sClass\">".$value->GetDescription(true)."</div>";
  287. }
  288. }
  289. $aResultDisp[$iRow] = $aRowDisp;
  290. }
  291. $oPage->table($aDisplayConfig, $aResultDisp);
  292. }
  293. ///////////////////////////////////////////////////////////////////////////////
  294. // Wizard entry points
  295. ///////////////////////////////////////////////////////////////////////////////
  296. function Do_Welcome($oPage, $sClass)
  297. {
  298. $sWiztep = "1_welcome";
  299. $oPage->p("<h1>Bulk load from CSV data / step 1</h1>");
  300. $sCSVData = utils::ReadPostedParam('csvdata');
  301. $oPage->add("<form method=\"post\" action=\"\">");
  302. $oPage->MakeClassesSelect("class", $sClass, 50, UR_ACTION_MODIFY);
  303. $oPage->add("<br/>");
  304. $oPage->add("<textarea rows=\"25\" cols=\"100\" name=\"csvdata\" wrap=\"soft\">$sCSVData</textarea>");
  305. $oPage->add("<br/>");
  306. $oPage->add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
  307. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Next\"><br/>\n");
  308. $oPage->add("</form>");
  309. // As a help to the end-user, let's display the list of possible fields
  310. // for a class, that can be copied/pasted into the CSV area.
  311. $sCurrentList = "";
  312. $aHeadersList = array();
  313. foreach(MetaModel::GetClasses('bizmodel') as $sClassName)
  314. {
  315. $aList = MetaModel::GetZListItems($sClassName, 'details');
  316. $aHeader = array();
  317. // $aHeader[] = MetaModel::GetKeyLabel($sClassName);
  318. $aHeader[] = 'id'; // Should be what's coded on the line above... but there is a bug
  319. foreach($aList as $sAttCode)
  320. {
  321. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode);
  322. }
  323. $sAttributes = implode(",", $aHeader);
  324. $aHeadersList[$sClassName] = $sAttributes;
  325. if($sClassName == $sClass)
  326. {
  327. // this class is currently selected
  328. $sCurrentList = $sAttributes;
  329. }
  330. }
  331. // Store all the values in a variable client-side
  332. $aScript = array();
  333. foreach($aHeadersList as $sClassName => $sAttributes)
  334. {
  335. $aScript[] = "'$sClassName':'$sAttributes'";
  336. }
  337. $oPage->add("<script>
  338. var oAttributes = {".implode(',', $aScript)."};
  339. function DisplayFields(className)
  340. {
  341. $('#fields').val(oAttributes[className]);
  342. }
  343. </script>\n");
  344. $oPage->add_ready_script("$('#select_class').change( function() {DisplayFields(this.value);} );");
  345. $oPage->add("<br/>");
  346. $oPage->add("Fields for this object<br/><textarea readonly id=fields rows=\"3\" cols=\"100\" wrap=\"soft\">$sCurrentList</textarea>");
  347. }
  348. function Do_Format($oPage, $sClass)
  349. {
  350. $oPage->p("<h1>Bulk load from CSV data / step 2</h1>");
  351. $sWiztep = "2_format";
  352. $sCSVData = utils::ReadPostedParam('csvdata');
  353. $oCSVParser = new CSVParser($sCSVData);
  354. $sSep = $oCSVParser->GuessSeparator();
  355. $iSkip = $oCSVParser->GuessSkipLines();
  356. // No data ?
  357. $aData = $oCSVParser->ToArray(null);
  358. $iTarget = count($aData);
  359. if ($iTarget == 0)
  360. {
  361. $oPage->p("Empty data set..., please provide some data!");
  362. $oPage->add("<button onClick=\"window.history.back();\">Back</button>\n");
  363. return;
  364. }
  365. // Guess the format :
  366. $oPage->p("Guessed separator: '<strong>$sSep</strong>' (ASCII=".ord($sSep).")");
  367. $oPage->p("Guessed # of lines to skip: $iSkip");
  368. $oPage->p("Target: $iTarget rows");
  369. $oPage->Add("<form method=\"post\" action=\"\">");
  370. ShowTableForm($oPage, $oCSVParser, $sClass);
  371. $oPage->Add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  372. $oPage->Add("<input type=\"hidden\" name=\"csvdata\" value=\"$sCSVData\">");
  373. $oPage->Add("<input type=\"hidden\" name=\"separator\" value=\"$sSep\">");
  374. $oPage->Add("<input type=\"hidden\" name=\"skiplines\" value=\"$iSkip\">");
  375. $oPage->Add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
  376. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Back\">");
  377. $oPage->Add("<input type=\"submit\" name=\"todo\" value=\"Next\">");
  378. $oPage->Add("</form>");
  379. }
  380. function DoProcessOrVerify($oPage, $sClass, CMDBChange $oChange = null)
  381. {
  382. $sCSVData = utils::ReadPostedParam('csvdata');
  383. $sSep = utils::ReadPostedParam('separator');
  384. $iSkip = utils::ReadPostedParam('skiplines');
  385. $aFieldMap = utils::ReadPostedParam('fmap');
  386. $aIsReconcKey = utils::ReadPostedParam('iskey');
  387. if (empty($aIsReconcKey))
  388. {
  389. $oPage->p("Error: no reconciliation key has been specified. Please specify which field(s) will be used to identify the object");
  390. $oPage->add("<button onClick=\"window.history.back();\">Back</button>\n");
  391. $oPage->add("<button disabled>Next</button>\n");
  392. return;
  393. }
  394. $oCSVParser = new CSVParser($sCSVData);
  395. $oCSVParser->SetSeparator($sSep);
  396. $oCSVParser->SetSkipLines($iSkip);
  397. $aData = $oCSVParser->ToArray(null);
  398. $iTarget = count($aData);
  399. $oPage->p("<h2>Goal summary</h2>");
  400. $oPage->p("Target: $iTarget rows");
  401. $aSampleData = $oCSVParser->ToArray(array_keys($aFieldMap), 5);
  402. $aDisplayConfig = array();
  403. $aExtKeys = array();
  404. foreach ($aFieldMap as $sFieldId=>$sColDesc)
  405. {
  406. if (array_key_exists($sFieldId, $aIsReconcKey))
  407. {
  408. $sReconcKey = " [search]";
  409. }
  410. else
  411. {
  412. $sReconcKey = "";
  413. }
  414. if ($sColDesc == "id")
  415. {
  416. $aDisplayConfig[$sFieldId] = array("label"=>"Private key $sReconcKey", "description"=>"");
  417. }
  418. elseif ($sColDesc == "__none__")
  419. {
  420. // Skip !
  421. }
  422. else if (MetaModel::IsValidAttCode($sClass, $sColDesc))
  423. {
  424. $sAttCode = $sColDesc;
  425. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  426. $aDisplayConfig[$sFieldId] = array("label"=>"$sLabel$sReconcKey", "description"=>"");
  427. if (MetaModel::IsValidKeyAttCode($sClass, $sAttCode))
  428. {
  429. $aExtKeys[] = $sAttCode;
  430. }
  431. }
  432. elseif (IsExtKeyField($sColDesc))
  433. {
  434. list($sExtKeyAttCode, $sForeignAttCode) = GetExtKeyFieldCodes($sColDesc);
  435. $aDisplayConfig[$sFieldId] = array("label"=>MakeExtFieldLabel($sClass, $sExtKeyAttCode, $sForeignAttCode), "description"=>"");
  436. $aExtKeys[] = $sExtKeyAttCode;
  437. }
  438. else
  439. {
  440. // ???
  441. $aDisplayConfig[$sFieldId] = array("label"=>"-?-?-$sColDesc-?-?-", "description"=>"");
  442. }
  443. }
  444. $oPage->table($aDisplayConfig, $aSampleData);
  445. if ($oChange)
  446. {
  447. $oPage->p("<h2>Processing...</h2>");
  448. }
  449. else
  450. {
  451. $oPage->p("<h2>Column consistency</h2>");
  452. $aMissingKeys = array();
  453. foreach (MetaModel::GetExternalKeys($sClass) as $sExtKeyAttCode => $oExtKey)
  454. {
  455. if (!in_array($sExtKeyAttCode, $aExtKeys) && !$oExtKey->IsNullAllowed())
  456. {
  457. $aMissingKeys[$sExtKeyAttCode] = $oExtKey;
  458. }
  459. }
  460. if (count($aMissingKeys) > 0)
  461. {
  462. $oPage->p("Warning: the objects could not be created, due to some missing mandatory external keys in the field list: ");
  463. $oPage->add("<ul>");
  464. foreach($aMissingKeys as $sAttCode => $oAttDef)
  465. {
  466. $oPage->add("<li>".$oAttDef->GetLabel()."</li>");
  467. }
  468. $oPage->add("</ul>");
  469. }
  470. else
  471. {
  472. $oPage->p("ok - required external keys (if any) have been found in the field list");
  473. }
  474. $oPage->p("Note: the procedure will fail if any line has not the same number of columns as the first line");
  475. $oPage->p("<h2>Check...</h2>");
  476. }
  477. ProcessData($oPage, $sClass, $oCSVParser, $aFieldMap, $aIsReconcKey, $oChange);
  478. $oPage->add("<form method=\"post\" action=\"\">");
  479. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  480. $oPage->add("<input type=\"hidden\" name=\"csvdata\" value=\"$sCSVData\">");
  481. $oPage->add("<input type=\"hidden\" name=\"separator\" value=\"$sSep\">");
  482. $oPage->add("<input type=\"hidden\" name=\"skiplines\" value=\"$iSkip\">");
  483. $oPage->add_input_hidden("fmap", $aFieldMap);
  484. $oPage->add_input_hidden("iskey", $aIsReconcKey);
  485. return true;
  486. }
  487. function Do_Verify($oPage, $sClass)
  488. {
  489. $oPage->p("<h1>Bulk load from CSV data / step 3</h1>");
  490. $sWiztep = "3_verify";
  491. if (DoProcessOrVerify($oPage, $sClass, null))
  492. {
  493. // FORM started by DoProcessOrVerify...
  494. $oPage->add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
  495. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Back\">");
  496. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Next\">");
  497. $oPage->add("</form>");
  498. }
  499. }
  500. function Do_Execute($oPage, $sClass)
  501. {
  502. $oPage->p("<h1>Bulk load from CSV data / step 4</h1>");
  503. $sWiztep = "4_execute";
  504. $oMyChange = MetaModel::NewObject("CMDBChange");
  505. $oMyChange->Set("date", time());
  506. $iUser = UserRights::GetContactId();
  507. if ($iUser != null)
  508. {
  509. // Ok, that's dirty, I admit :-)
  510. $oUser = MetaModel::GetObject('bizContact', $iUser);
  511. $sUser = $oUser->GetName();
  512. $oMyChange->Set("userinfo", "CSV Import, by ".$sUser);
  513. }
  514. else
  515. {
  516. $oMyChange->Set("userinfo", "CSV Import");
  517. }
  518. $iChangeId = $oMyChange->DBInsert();
  519. if (DoProcessOrVerify($oPage, $sClass, $oMyChange))
  520. {
  521. // FORM started by DoProcessOrVerify...
  522. $oPage->add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
  523. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Back\">");
  524. $oPage->add("</form>");
  525. }
  526. }
  527. ///////////////////////////////////////////////////////////////////////////////////////////////////
  528. //
  529. // M a i n P r o g r a m
  530. //
  531. ///////////////////////////////////////////////////////////////////////////////////////////////////
  532. $sFromWiztep = utils::ReadPostedParam('fromwiztep', '');
  533. $sClass = utils::ReadPostedParam('class', '');
  534. $sTodo = utils::ReadPostedParam('todo', '');
  535. switch($sFromWiztep)
  536. {
  537. case '':
  538. Do_Welcome($oPage, $sClass);
  539. break;
  540. case '1_welcome':
  541. if ($sTodo == "Next") Do_Format($oPage, $sClass);
  542. else trigger_error("Wrong argument todo='$sTodo'", E_USER_ERROR);
  543. break;
  544. case '2_format':
  545. if ($sTodo == "Next") Do_Verify($oPage, $sClass);
  546. else Do_Welcome($oPage, $sClass);
  547. break;
  548. case '3_verify':
  549. if ($sTodo == "Next") Do_Execute($oPage, $sClass);
  550. else Do_Format($oPage, $sClass);
  551. break;
  552. case '4_execute':
  553. if ($sTodo == "Next") trigger_error("Wrong argument todo='$sTodo'", E_USER_ERROR);
  554. else Do_Verify($oPage, $sClass);
  555. break;
  556. default:
  557. trigger_error("Wrong argument fromwiztep='$sFromWiztep'", E_USER_ERROR);
  558. }
  559. $oPage->output();
  560. ?>