csvimport.php 21 KB

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