csvimport.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. $aOptions = array();
  64. $aOptions['id'] = array(
  65. 'LabelHtml' => "Private key",
  66. 'LabelRef' => "Private key",
  67. 'IsReconcKey' => false,
  68. 'Tip' => '',
  69. );
  70. $sFoundAttCode = ""; // quick and dirty way to remind if a match has been found and suggest a reconciliation key if possible
  71. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAtt)
  72. {
  73. if ($oAtt->IsExternalField()) continue;
  74. $bIsThatField = (strcasecmp($sFieldName, $oAtt->GetLabel()) == 0);
  75. $sFoundAttCode = (MetaModel::IsValidFilterCode($sClass, $sAttCode) && $bIsThatField) ? $sAttCode : $sFoundAttCode;
  76. if ($oAtt->IsExternalKey())
  77. {
  78. // An external key might be loaded by
  79. // the pkey or a reconciliation key
  80. //
  81. $aOptions[MakeExtFieldSelectValue($sAttCode, 'id')] = array(
  82. 'LabelHtml' => "<em>".$oAtt->GetLabel()."</em> (id)",
  83. 'LabelRef' => $oAtt->GetLabel()." (id)",
  84. 'IsReconcKey' => MetaModel::IsReconcKey($sClass, $sAttCode),
  85. 'Tip' => '',
  86. );
  87. $sRemoteClass = $oAtt->GetTargetClass();
  88. foreach(MetaModel::GetReconcKeys($sRemoteClass) as $sExtAttCode)
  89. {
  90. $sValue = MakeExtFieldSelectValue($sAttCode, $sExtAttCode);
  91. // Create two entries:
  92. // - generic syntax (ext key label -> remote field label)
  93. // - if an ext field exists that corresponds to it, allow its label
  94. $sLabel1 = MakeExtFieldLabel($sClass, $sAttCode, $sExtAttCode);
  95. $bFoundTwin = false;
  96. foreach (MetaModel::GetExternalFields($sClass, $sAttCode) as $oExtFieldAtt)
  97. {
  98. if ($oExtFieldAtt->GetExtAttCode() == $sExtAttCode)
  99. {
  100. $aOptions[$sValue] = array(
  101. 'LabelHtml' => htmlentities($oExtFieldAtt->GetLabel()),
  102. 'LabelRef' => $oExtFieldAtt->GetLabel(),
  103. 'IsReconcKey' => false,
  104. 'Tip' => "equivalent to '".htmlentities($sLabel1)."'",
  105. );
  106. $bFoundTwin = true;
  107. $sLabel2 = $oExtFieldAtt->GetLabel();
  108. break;
  109. }
  110. }
  111. $aOptions[$sValue] = array(
  112. 'LabelHtml' => htmlentities($sLabel1),
  113. 'LabelRef' => $sLabel1,
  114. 'IsReconcKey' => false,
  115. 'Tip' => $bFoundTwin ? "equivalent to '".htmlentities($sLabel2)."'" : "",
  116. );
  117. }
  118. }
  119. else
  120. {
  121. $aOptions[$sAttCode] = array(
  122. 'LabelHtml' => htmlentities($oAtt->GetLabel()),
  123. 'LabelRef' => $oAtt->GetLabel(),
  124. 'IsReconcKey' => MetaModel::IsReconcKey($sClass, $sAttCode),
  125. 'Tip' => '',
  126. );
  127. }
  128. }
  129. // Find the best match
  130. $iMin = strlen($sFieldName);
  131. $sBestValue = null;
  132. foreach ($aOptions as $sValue => $aData)
  133. {
  134. $iDist = levenshtein(strtolower($sFieldName), strtolower($aData['LabelRef']));
  135. if (($iDist != -1) && ($iDist < $iMin))
  136. {
  137. $iMin = $iDist;
  138. $sBestValue = $sValue;
  139. }
  140. }
  141. $sSelField = "<select name=\"fmap[field$iFieldIndex]\">";
  142. foreach ($aOptions as $sValue => $aData)
  143. {
  144. $sStyle = '';
  145. $sComment = '';
  146. $sSELECTED = '';
  147. if ($sValue == $sBestValue)
  148. {
  149. $sSELECTED = ' SELECTED';
  150. if ($iMin > 0)
  151. {
  152. $sStyle = " style=\"background-color: #ffdddd;\"";
  153. $sComment = '- suggested';
  154. }
  155. }
  156. $sIsRecondKey = $aData['IsReconcKey'] ? " [rk!]" : "";
  157. $sSelField .= "<option value=\"$sValue\" title=\"".$aData['Tip']."\"$sStyle$sSELECTED>".$aData['LabelHtml']."$sComment$sIsRecondKey</option>\n";
  158. }
  159. $sSelField .= "</select>";
  160. $aFields["field$iFieldIndex"]["label"] = $sSelField;
  161. $sCHECKED = ($sFieldName == "id" || MetaModel::IsReconcKey($sClass, $sFoundAttCode)) ? " CHECKED" : "";
  162. $aFields["field$iFieldIndex"]["label"] .= "<input type=\"checkbox\" name=\"iskey[field$iFieldIndex]\" value=\"yes\" $sCHECKED>";
  163. if (array_key_exists($iFieldIndex, $aColToRow))
  164. {
  165. $aFields["field$iFieldIndex"]["value"] = $aColToRow[$iFieldIndex];
  166. }
  167. }
  168. $oPage->details($aFields);
  169. }
  170. function ProcessData($oPage, $sClass, $oCSVParser, $aFieldMap, $aIsReconcKey, CMDBChange $oChange = null)
  171. {
  172. // Note: $oChange can be null, in which case the aim is to check what would be done
  173. // Setup field mapping: sort out between values and other specific columns
  174. //
  175. $iPKeyId = null;
  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 ($sColDesc == "id")
  183. {
  184. // Skip !
  185. $iPKeyId = $iFieldId;
  186. }
  187. elseif ($sColDesc == "__none__")
  188. {
  189. // Skip !
  190. }
  191. elseif (IsExtKeyField($sColDesc))
  192. {
  193. // This field is value to search on, to find a value for an external key
  194. list($sExtKeyAttCode, $sExtReconcKeyAttCode) = GetExtKeyFieldCodes($sColDesc);
  195. $aExtKeys[$sExtKeyAttCode][$sExtReconcKeyAttCode] = $iFieldId;
  196. }
  197. elseif (array_key_exists($sFieldId, $aIsReconcKey))
  198. {
  199. // This value is a reconciliation key
  200. $aReconcilKeys[$sColDesc] = $iFieldId;
  201. $aAttList[$sColDesc] = $iFieldId; // A reconciliation key is also a field
  202. }
  203. else
  204. {
  205. // $sColDesc is an attribute code
  206. $aAttList[$sColDesc] = $iFieldId;
  207. }
  208. }
  209. // Setup result presentation
  210. //
  211. $aDisplayConfig = array();
  212. $aDisplayConfig["__RECONCILIATION__"] = array("label"=>"Reconciliation", "description"=>"");
  213. $aDisplayConfig["__STATUS__"] = array("label"=>"Import status", "description"=>"");
  214. if (isset($iPKeyId))
  215. {
  216. $aDisplayConfig["col$iPKeyId"] = array("label"=>"<strong>id</strong>", "description"=>"");
  217. }
  218. foreach($aReconcilKeys as $sAttCode => $iCol)
  219. {
  220. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  221. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  222. }
  223. foreach($aExtKeys as $sAttCode=>$aKeyConfig)
  224. {
  225. $oExtKeyAtt = MetaModel::GetAttributeDef($sClass, $sAttCode);
  226. $sLabel = $oExtKeyAtt->GetLabel();
  227. $aDisplayConfig[$sAttCode] = array("label"=>"$sLabel", "description"=>"");
  228. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  229. {
  230. // The foreign attribute is one of our reconciliation key
  231. $sLabel = MakeExtFieldLabel($sClass, $sAttCode, $sForeignAttCode);
  232. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  233. }
  234. }
  235. foreach ($aAttList as $sAttCode => $iCol)
  236. {
  237. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  238. $aDisplayConfig["col$iCol"] = array("label"=>"$sLabel", "description"=>"");
  239. }
  240. // Compute the results
  241. //
  242. $aData = $oCSVParser->ToArray();
  243. $oBulk = new BulkChange(
  244. $sClass,
  245. $aData,
  246. $aAttList,
  247. array_keys($aReconcilKeys),
  248. $aExtKeys
  249. );
  250. $aRes = $oBulk->Process($oChange);
  251. $aResultDisp = array(); // to be displayed
  252. foreach($aRes as $iRow => $aRowData)
  253. {
  254. $aRowDisp = array();
  255. $aRowDisp["__RECONCILIATION__"] = $aRowData["__RECONCILIATION__"];
  256. $aRowDisp["__STATUS__"] = $aRowData["__STATUS__"]->GetDescription(true);
  257. foreach($aRowData as $sKey => $value)
  258. {
  259. if ($sKey == '__RECONCILIATION__') continue;
  260. if ($sKey == '__STATUS__') continue;
  261. switch (get_class($value))
  262. {
  263. case 'CellChangeSpec_Void':
  264. $sClass = '';
  265. break;
  266. case 'CellChangeSpec_Unchanged':
  267. $sClass = '';
  268. break;
  269. case 'CellChangeSpec_Modify':
  270. $sClass = 'csvimport_ok';
  271. break;
  272. case 'CellChangeSpec_Init':
  273. $sClass = 'csvimport_init';
  274. break;
  275. case 'CellChangeSpec_Issue':
  276. $sClass = 'csvimport_error';
  277. break;
  278. }
  279. if (empty($sClass))
  280. {
  281. $aRowDisp[$sKey] = $value->GetDescription(true);
  282. }
  283. else
  284. {
  285. $aRowDisp[$sKey] = "<div class=\"$sClass\">".$value->GetDescription(true)."</div>";
  286. }
  287. }
  288. $aResultDisp[$iRow] = $aRowDisp;
  289. }
  290. $oPage->table($aDisplayConfig, $aResultDisp);
  291. }
  292. ///////////////////////////////////////////////////////////////////////////////
  293. // Wizard entry points
  294. ///////////////////////////////////////////////////////////////////////////////
  295. function Do_Welcome($oPage, $sClass)
  296. {
  297. $sWiztep = "1_welcome";
  298. $oPage->p("<h1>Bulk load from CSV data / step 1</h1>");
  299. $sCSVData = utils::ReadPostedParam('csvdata');
  300. $oPage->add("<form method=\"post\" action=\"\">");
  301. $oPage->MakeClassesSelect("class", $sClass, 50);
  302. //$oPage->Add("<input type=\"text\" size=\"40\" name=\"initialsituation\" value=\"\">");
  303. $oPage->add("</br>");
  304. $oPage->add("<textarea rows=\"25\" cols=\"80\" 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("Fields for this object: <textarea readonly id=fields rows=\"3\" cols=\"60\" wrap=\"soft\">$sCurrentList</textarea>");
  346. }
  347. function Do_Format($oPage, $sClass)
  348. {
  349. $oPage->p("<h1>Bulk load from CSV data / step 2</h1>");
  350. $sWiztep = "2_format";
  351. $sCSVData = utils::ReadPostedParam('csvdata');
  352. $oCSVParser = new CSVParser($sCSVData);
  353. $sSep = $oCSVParser->GuessSeparator();
  354. $iSkip = $oCSVParser->GuessSkipLines();
  355. // No data ?
  356. $aData = $oCSVParser->ToArray(null);
  357. $iTarget = count($aData);
  358. if ($iTarget == 0)
  359. {
  360. $oPage->p("Empty data set..., please provide some data!");
  361. $oPage->add("<button onClick=\"window.history.back();\">Back</button>\n");
  362. return;
  363. }
  364. // Guess the format :
  365. $oPage->p("Guessed separator: '<strong>$sSep</strong>' (ASCII=".ord($sSep).")");
  366. $oPage->p("Guessed # of lines to skip: $iSkip");
  367. $oPage->p("Target: $iTarget rows");
  368. $oPage->Add("<form method=\"post\" action=\"\">");
  369. ShowTableForm($oPage, $oCSVParser, $sClass);
  370. $oPage->Add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  371. $oPage->Add("<input type=\"hidden\" name=\"csvdata\" value=\"$sCSVData\">");
  372. $oPage->Add("<input type=\"hidden\" name=\"separator\" value=\"$sSep\">");
  373. $oPage->Add("<input type=\"hidden\" name=\"skiplines\" value=\"$iSkip\">");
  374. $oPage->Add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
  375. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Back\">");
  376. $oPage->Add("<input type=\"submit\" name=\"todo\" value=\"Next\">");
  377. $oPage->Add("</form>");
  378. }
  379. function DoProcessOrVerify($oPage, $sClass, CMDBChange $oChange = null)
  380. {
  381. $sCSVData = utils::ReadPostedParam('csvdata');
  382. $sSep = utils::ReadPostedParam('separator');
  383. $iSkip = utils::ReadPostedParam('skiplines');
  384. $aFieldMap = utils::ReadPostedParam('fmap');
  385. $aIsReconcKey = utils::ReadPostedParam('iskey');
  386. $oCSVParser = new CSVParser($sCSVData);
  387. $oCSVParser->SetSeparator($sSep);
  388. $oCSVParser->SetSkipLines($iSkip);
  389. $aData = $oCSVParser->ToArray(null);
  390. $iTarget = count($aData);
  391. $oPage->p("<h2>Goal summary</h2>");
  392. $oPage->p("Target: $iTarget rows");
  393. $aSampleData = $oCSVParser->ToArray(array_keys($aFieldMap), 5);
  394. $aDisplayConfig = array();
  395. $aExtKeys = array();
  396. foreach ($aFieldMap as $sFieldId=>$sColDesc)
  397. {
  398. if (array_key_exists($sFieldId, $aIsReconcKey))
  399. {
  400. $sReconcKey = " [search]";
  401. }
  402. else
  403. {
  404. $sReconcKey = "";
  405. }
  406. if ($sColDesc == "id")
  407. {
  408. $aDisplayConfig[$sFieldId] = array("label"=>"Private key $sReconcKey", "description"=>"");
  409. }
  410. elseif ($sColDesc == "__none__")
  411. {
  412. // Skip !
  413. }
  414. else if (MetaModel::IsValidAttCode($sClass, $sColDesc))
  415. {
  416. $sAttCode = $sColDesc;
  417. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  418. $aDisplayConfig[$sFieldId] = array("label"=>"$sLabel$sReconcKey", "description"=>"");
  419. if (MetaModel::IsValidKeyAttCode($sClass, $sAttCode))
  420. {
  421. $aExtKeys[] = $sAttCode;
  422. }
  423. }
  424. elseif (IsExtKeyField($sColDesc))
  425. {
  426. list($sExtKeyAttCode, $sForeignAttCode) = GetExtKeyFieldCodes($sColDesc);
  427. $aDisplayConfig[$sFieldId] = array("label"=>MakeExtFieldLabel($sClass, $sExtKeyAttCode, $sForeignAttCode), "description"=>"");
  428. $aExtKeys[] = $sExtKeyAttCode;
  429. }
  430. else
  431. {
  432. // ???
  433. $aDisplayConfig[$sFieldId] = array("label"=>"-?-?-$sColDesc-?-?-", "description"=>"");
  434. }
  435. }
  436. $oPage->table($aDisplayConfig, $aSampleData);
  437. if ($oChange)
  438. {
  439. $oPage->p("<h2>Processing...</h2>");
  440. }
  441. else
  442. {
  443. $oPage->p("<h2>Column consistency</h2>");
  444. $aMissingKeys = array();
  445. foreach (MetaModel::GetExternalKeys($sClass) as $sExtKeyAttCode => $oExtKey)
  446. {
  447. if (!in_array($sExtKeyAttCode, $aExtKeys) && !$oExtKey->IsNullAllowed())
  448. {
  449. $aMissingKeys[$sExtKeyAttCode] = $oExtKey;
  450. }
  451. }
  452. if (count($aMissingKeys) > 0)
  453. {
  454. $oPage->p("Warning: the objects could not be created, due to some missing mandatory external keys in the field list: ");
  455. $oPage->add("<ul>");
  456. foreach($aMissingKeys as $sAttCode => $oAttDef)
  457. {
  458. $oPage->add("<li>".$oAttDef->GetLabel()."</li>");
  459. }
  460. $oPage->add("</ul>");
  461. }
  462. else
  463. {
  464. $oPage->p("ok - required external keys (if any) have been found in the field list");
  465. }
  466. $oPage->p("<h2>Check...</h2>");
  467. }
  468. ProcessData($oPage, $sClass, $oCSVParser, $aFieldMap, $aIsReconcKey, $oChange);
  469. $oPage->add("<form method=\"post\" action=\"\">");
  470. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  471. $oPage->add("<input type=\"hidden\" name=\"csvdata\" value=\"$sCSVData\">");
  472. $oPage->add("<input type=\"hidden\" name=\"separator\" value=\"$sSep\">");
  473. $oPage->add("<input type=\"hidden\" name=\"skiplines\" value=\"$iSkip\">");
  474. $oPage->add_input_hidden("fmap", $aFieldMap);
  475. $oPage->add_input_hidden("iskey", $aIsReconcKey);
  476. return;
  477. }
  478. function Do_Verify($oPage, $sClass)
  479. {
  480. $oPage->p("<h1>Bulk load from CSV data / step 3</h1>");
  481. $sWiztep = "3_verify";
  482. DoProcessOrVerify($oPage, $sClass, null);
  483. // FORM started by DoProcessOrVerify...
  484. $oPage->add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
  485. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Back\">");
  486. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Next\">");
  487. $oPage->add("</form>");
  488. }
  489. function Do_Execute($oPage, $sClass)
  490. {
  491. $oPage->p("<h1>Bulk load from CSV data / step 4</h1>");
  492. $sWiztep = "4_execute";
  493. $oMyChange = MetaModel::NewObject("CMDBChange");
  494. $oMyChange->Set("date", time());
  495. $oMyChange->Set("userinfo", "CSV Import");
  496. $iChangeId = $oMyChange->DBInsert();
  497. DoProcessOrVerify($oPage, $sClass, $oMyChange);
  498. // FORM started by DoProcessOrVerify...
  499. $oPage->add("<input type=\"hidden\" name=\"fromwiztep\" value=\"$sWiztep\">");
  500. $oPage->add("<input type=\"submit\" name=\"todo\" value=\"Back\">");
  501. $oPage->add("</form>");
  502. }
  503. ///////////////////////////////////////////////////////////////////////////////////////////////////
  504. //
  505. // M a i n P r o g r a m
  506. //
  507. ///////////////////////////////////////////////////////////////////////////////////////////////////
  508. $sFromWiztep = utils::ReadPostedParam('fromwiztep', '');
  509. $sClass = utils::ReadPostedParam('class', '');
  510. $sTodo = utils::ReadPostedParam('todo', '');
  511. switch($sFromWiztep)
  512. {
  513. case '':
  514. Do_Welcome($oPage, $sClass);
  515. break;
  516. case '1_welcome':
  517. if ($sTodo == "Next") Do_Format($oPage, $sClass);
  518. else trigger_error("Wrong argument todo='$sTodo'", E_USER_ERROR);
  519. break;
  520. case '2_format':
  521. if ($sTodo == "Next") Do_Verify($oPage, $sClass);
  522. else Do_Welcome($oPage, $sClass);
  523. break;
  524. case '3_verify':
  525. if ($sTodo == "Next") Do_Execute($oPage, $sClass);
  526. else Do_Format($oPage, $sClass);
  527. break;
  528. case '4_execute':
  529. if ($sTodo == "Next") trigger_error("Wrong argument todo='$sTodo'", E_USER_ERROR);
  530. else Do_Verify($oPage, $sClass);
  531. break;
  532. default:
  533. trigger_error("Wrong argument fromwiztep='$sFromWiztep'", E_USER_ERROR);
  534. }
  535. $oPage->output();
  536. ?>