import.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Import web service
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. //
  25. // Known limitations
  26. // - reconciliation is made on the first column
  27. //
  28. // Known issues
  29. // - ALMOST impossible to troubleshoot when an externl key has a wrong value
  30. // - no character escaping in the xml output (yes !?!?!)
  31. // - not outputing xml when a wrong input is given (class, attribute names)
  32. //
  33. require_once('../approot.inc.php');
  34. require_once(APPROOT.'/application/application.inc.php');
  35. require_once(APPROOT.'/application/webpage.class.inc.php');
  36. require_once(APPROOT.'/application/csvpage.class.inc.php');
  37. require_once(APPROOT.'/application/clipage.class.inc.php');
  38. require_once(APPROOT.'/application/startup.inc.php');
  39. class BulkLoadException extends Exception
  40. {
  41. }
  42. $aPageParams = array
  43. (
  44. 'auth_user' => array
  45. (
  46. 'mandatory' => true,
  47. 'modes' => 'cli',
  48. 'default' => null,
  49. 'description' => 'login (must have enough rights to create objects of the given class)',
  50. ),
  51. 'auth_pwd' => array
  52. (
  53. 'mandatory' => true,
  54. 'modes' => 'cli',
  55. 'default' => null,
  56. 'description' => 'password',
  57. ),
  58. 'class' => array
  59. (
  60. 'mandatory' => true,
  61. 'modes' => 'http,cli',
  62. 'default' => null,
  63. 'description' => 'class of loaded objects',
  64. ),
  65. 'csvdata' => array
  66. (
  67. 'mandatory' => true,
  68. 'modes' => 'http',
  69. 'default' => null,
  70. 'description' => 'data',
  71. ),
  72. 'csvfile' => array
  73. (
  74. 'mandatory' => true,
  75. 'modes' => 'cli',
  76. 'default' => '',
  77. 'description' => 'local data file, replaces csvdata if specified',
  78. ),
  79. 'charset' => array
  80. (
  81. 'mandatory' => false,
  82. 'modes' => 'http,cli',
  83. 'default' => 'UTF-8',
  84. 'description' => 'Character set encoding of the CSV data: UTF-8, ISO-8859-1, WINDOWS-1251, WINDOWS-1252, ISO-8859-15',
  85. ),
  86. 'separator' => array
  87. (
  88. 'mandatory' => false,
  89. 'modes' => 'http,cli',
  90. 'default' => ',',
  91. 'description' => 'column separator in CSV data',
  92. ),
  93. 'qualifier' => array
  94. (
  95. 'mandatory' => false,
  96. 'modes' => 'http,cli',
  97. 'default' => '"',
  98. 'description' => 'test qualifier in CSV data',
  99. ),
  100. 'output' => array
  101. (
  102. 'mandatory' => false,
  103. 'modes' => 'http,cli',
  104. 'default' => 'summary',
  105. 'description' => '[retcode] to return the count of lines in error, [summary] to return a concise report, [details] to get a detailed report (each line listed)',
  106. ),
  107. /*
  108. 'reportlevel' => array
  109. (
  110. 'mandatory' => false,
  111. 'modes' => 'http,cli',
  112. 'default' => 'errors|warnings|created|changed|unchanged',
  113. 'description' => 'combination of flags to limit the detailed output',
  114. ),
  115. */
  116. 'reconciliationkeys' => array
  117. (
  118. 'mandatory' => false,
  119. 'modes' => 'http,cli',
  120. 'default' => '',
  121. 'description' => 'name of the columns used to identify existing objects and update them, or create a new one',
  122. ),
  123. 'simulate' => array
  124. (
  125. 'mandatory' => false,
  126. 'modes' => 'http,cli',
  127. 'default' => '0',
  128. 'description' => 'If set to 1, then the load will not be executed, but the expected report will be produced',
  129. ),
  130. 'comment' => array
  131. (
  132. 'mandatory' => false,
  133. 'modes' => 'http,cli',
  134. 'default' => '',
  135. 'description' => 'Comment to be added into the change log',
  136. ),
  137. );
  138. function UsageAndExit($oP)
  139. {
  140. global $aPageParams;
  141. $bModeCLI = utils::IsModeCLI();
  142. $oP->p("USAGE:\n");
  143. foreach($aPageParams as $sParam => $aParamData)
  144. {
  145. $aModes = explode(',', $aParamData['modes']);
  146. if ($bModeCLI)
  147. {
  148. if (in_array('cli', $aModes))
  149. {
  150. $sDesc = $aParamData['description'].', '.($aParamData['mandatory'] ? 'mandatory' : 'optional, defaults to ['.$aParamData['default'].']');
  151. $oP->p("$sParam = $sDesc");
  152. }
  153. }
  154. else
  155. {
  156. if (in_array('http', $aModes))
  157. {
  158. $sDesc = $aParamData['description'].', '.($aParamData['mandatory'] ? 'mandatory' : 'optional, defaults to ['.$aParamData['default'].']');
  159. $oP->p("$sParam = $sDesc");
  160. }
  161. }
  162. }
  163. $oP->output();
  164. exit;
  165. }
  166. function ReadParam($oP, $sParam)
  167. {
  168. global $aPageParams;
  169. assert(isset($aPageParams[$sParam]));
  170. assert(!$aPageParams[$sParam]['mandatory']);
  171. $sValue = utils::ReadParam($sParam, $aPageParams[$sParam]['default'], true /* Allow CLI */);
  172. return trim($sValue);
  173. }
  174. function ReadMandatoryParam($oP, $sParam)
  175. {
  176. global $aPageParams;
  177. assert(isset($aPageParams[$sParam]));
  178. assert($aPageParams[$sParam]['mandatory']);
  179. $sValue = utils::ReadParam($sParam, null, true /* Allow CLI */);
  180. if (is_null($sValue))
  181. {
  182. $oP->p("ERROR: Missing argument '$sParam'\n");
  183. UsageAndExit($oP);
  184. }
  185. return trim($sValue);
  186. }
  187. /////////////////////////////////
  188. // Main program
  189. if (utils::IsModeCLI())
  190. {
  191. $oP = new CLIPage("iTop - Bulk import");
  192. }
  193. else
  194. {
  195. $oP = new CSVPage("iTop - Bulk import");
  196. }
  197. try
  198. {
  199. utils::UseParamFile();
  200. }
  201. catch(Exception $e)
  202. {
  203. $oP->p("Error: ".$e->GetMessage());
  204. $oP->output();
  205. exit -2;
  206. }
  207. if (utils::IsModeCLI())
  208. {
  209. // Next steps:
  210. // specific arguments: 'csvfile'
  211. //
  212. $sAuthUser = ReadMandatoryParam($oP, 'auth_user');
  213. $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd');
  214. $sCsvFile = ReadMandatoryParam($oP, 'csvfile');
  215. if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
  216. {
  217. UserRights::Login($sAuthUser); // Login & set the user's language
  218. }
  219. else
  220. {
  221. $oP->p("Access restricted or wrong credentials ('$sAuthUser')");
  222. $oP->output();
  223. exit -1;
  224. }
  225. if (!is_readable($sCsvFile))
  226. {
  227. $oP->p("Input file could not be found or could not be read: '$sCsvFile'");
  228. $oP->output();
  229. exit -1;
  230. }
  231. $sCSVData = file_get_contents($sCsvFile);
  232. }
  233. else
  234. {
  235. $_SESSION['login_mode'] = 'basic';
  236. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  237. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  238. $sCSVData = utils::ReadPostedParam('csvdata');
  239. }
  240. try
  241. {
  242. //////////////////////////////////////////////////
  243. //
  244. // Read parameters
  245. //
  246. $sClass = ReadMandatoryParam($oP, 'class');
  247. $sSep = ReadParam($oP, 'separator');
  248. $sQualifier = ReadParam($oP, 'qualifier');
  249. $sCharSet = ReadParam($oP, 'charset');
  250. $sOutput = ReadParam($oP, 'output');
  251. // $sReportLevel = ReadParam($oP, 'reportlevel');
  252. $sReconcKeys = ReadParam($oP, 'reconciliationkeys');
  253. $sSimulate = ReadParam($oP, 'simulate');
  254. $sComment = ReadParam($oP, 'comment');
  255. //////////////////////////////////////////////////
  256. //
  257. // Check parameters format/consistency
  258. //
  259. if (strlen($sCSVData) == 0)
  260. {
  261. throw new ExchangeException("Missing data - at least one line is expected");
  262. }
  263. if (!MetaModel::IsValidClass($sClass))
  264. {
  265. throw new BulkLoadException("Unknown class: '$sClass'");
  266. }
  267. if (strlen($sSep) > 1)
  268. {
  269. throw new BulkLoadException("Separator is limited to one character, found '$sSep'");
  270. }
  271. if (strlen($sQualifier) > 1)
  272. {
  273. throw new BulkLoadException("Text qualifier is limited to one character, found '$sQualifier'");
  274. }
  275. if (!in_array($sOutput, array('retcode', 'summary', 'details')))
  276. {
  277. throw new BulkLoadException("Unknown output format: '$sOutput'");
  278. }
  279. /*
  280. $aReportLevels = explode('|', $sReportLevel);
  281. foreach($aReportLevels as $sLevel)
  282. {
  283. if (!in_array($sLevel, explode('|', 'errors|warnings|created|changed|unchanged')))
  284. {
  285. throw new BulkLoadException("Unknown level in reporting level: '$sLevel'");
  286. }
  287. }
  288. */
  289. if ($sSimulate == '1')
  290. {
  291. $bSimulate = true;
  292. }
  293. else
  294. {
  295. $bSimulate = false;
  296. }
  297. if (($sOutput == "summary") || ($sOutput == 'details'))
  298. {
  299. $oP->add_comment("Output format: ".$sOutput);
  300. $oP->add_comment("Class: ".$sClass);
  301. $oP->add_comment("Separator: ".$sSep);
  302. $oP->add_comment("Qualifier: ".$sQualifier);
  303. $oP->add_comment("Charset Encoding:".$sCharSet);
  304. $oP->add_comment("Data Size: ".strlen($sCSVData));
  305. }
  306. //////////////////////////////////////////////////
  307. //
  308. // Security
  309. //
  310. if (!UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_MODIFY))
  311. {
  312. throw new SecurityException(Dict::Format('UI:Error:BulkModifyNotAllowedOn_Class', $sClass));
  313. }
  314. //////////////////////////////////////////////////
  315. //
  316. // Make translated header reference
  317. //
  318. $aFriendlyToInternalAttCode = array();
  319. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  320. {
  321. $aFriendlyToInternalAttCode[strtolower(BulkChange::GetFriendlyAttCodeName($sClass, $sAttCode))] = $sAttCode;
  322. if ($oAttDef->IsExternalKey(EXTKEY_RELATIVE))
  323. {
  324. $sRemoteClass = $oAttDef->GetTargetClass();
  325. foreach(MetaModel::ListAttributeDefs($sRemoteClass) as $sRemoteAttCode => $oRemoteAttDef)
  326. {
  327. $sAttCodeEx = $sAttCode.'->'.$sRemoteAttCode;
  328. $aFriendlyToInternalAttCode[strtolower(BulkChange::GetFriendlyAttCodeName($sClass, $sAttCodeEx))] = $sAttCodeEx;
  329. }
  330. }
  331. }
  332. //////////////////////////////////////////////////
  333. //
  334. // Parse first line, check attributes, analyse the request
  335. //
  336. if ($sCharSet == 'UTF-8')
  337. {
  338. $sUTF8Data = $sCSVData;
  339. }
  340. else
  341. {
  342. $sUTF8Data = iconv($sCharSet, 'UTF-8//IGNORE//TRANSLIT', $sCSVData);
  343. }
  344. $oCSVParser = new CSVParser($sUTF8Data, $sSep, $sQualifier);
  345. // Limitation: as the attribute list is in the first line, we can not match external key by a third-party attribute
  346. $aRawFieldList = $oCSVParser->ListFields();
  347. $iColCount = count($aRawFieldList);
  348. // Translate into internal names
  349. $aFieldList = array();
  350. foreach($aRawFieldList as $iFieldId => $sFieldName)
  351. {
  352. $sFieldName = trim($sFieldName);
  353. if (array_key_exists(strtolower($sFieldName), $aFriendlyToInternalAttCode))
  354. {
  355. $aFieldList[$iFieldId] = $aFriendlyToInternalAttCode[strtolower($sFieldName)];
  356. }
  357. else
  358. {
  359. $aFieldList[$iFieldId] = $sFieldName;
  360. }
  361. }
  362. $aAttList = array();
  363. $aExtKeys = array();
  364. foreach($aFieldList as $iFieldId => $sFieldName)
  365. {
  366. $aMatches = array();
  367. if (preg_match('/^(.+)\*$/', $sFieldName, $aMatches))
  368. {
  369. // Ignore any trailing "star" (*) that simply indicates a mandatory field
  370. $sFieldName = $aMatches[1];
  371. }
  372. if (preg_match('/^(.+)->(.+)$/', trim($sFieldName), $aMatches))
  373. {
  374. // The column has been specified as "extkey->attcode"
  375. //
  376. $sExtKeyAttCode = $aMatches[1];
  377. $sRemoteAttCode = $aMatches[2];
  378. if (!MetaModel::IsValidAttCode($sClass, $sExtKeyAttCode))
  379. {
  380. throw new BulkLoadException("Unknown attribute '$sExtKeyAttCode' (class: '$sClass')");
  381. }
  382. $oAtt = MetaModel::GetAttributeDef($sClass, $sExtKeyAttCode);
  383. if (!$oAtt->IsExternalKey())
  384. {
  385. throw new BulkLoadException("Not an external key '$sExtKeyAttCode' (class: '$sClass')");
  386. }
  387. $sTargetClass = $oAtt->GetTargetClass();
  388. if (!MetaModel::IsValidAttCode($sTargetClass, $sRemoteAttCode))
  389. {
  390. throw new BulkLoadException("Unknown attribute '$sRemoteAttCode' (key: '$sExtKeyAttCode', class: '$sTargetClass')");
  391. }
  392. $aExtKeys[$sExtKeyAttCode][$sRemoteAttCode] = $iFieldId;
  393. }
  394. elseif ($sFieldName == 'id')
  395. {
  396. $aAttList[$sFieldName] = $iFieldId;
  397. }
  398. else
  399. {
  400. // The column has been specified as "attcode"
  401. //
  402. if (!MetaModel::IsValidAttCode($sClass, $sFieldName))
  403. {
  404. throw new BulkLoadException("Unknown attribute '$sFieldName' (class: '$sClass')");
  405. }
  406. $oAtt = MetaModel::GetAttributeDef($sClass, $sFieldName);
  407. if ($oAtt->IsExternalKey())
  408. {
  409. $aExtKeys[$sFieldName]['id'] = $iFieldId;
  410. $aAttList[$sFieldName] = $iFieldId;
  411. }
  412. elseif ($oAtt->IsExternalField())
  413. {
  414. $sExtKeyAttCode = $oAtt->GetKeyAttCode();
  415. $sRemoteAttCode = $oAtt->GetExtAttCode();
  416. $aExtKeys[$sExtKeyAttCode][$sRemoteAttCode] = $iFieldId;
  417. }
  418. else
  419. {
  420. $aAttList[$sFieldName] = $iFieldId;
  421. }
  422. }
  423. }
  424. // Make sure there are some reconciliation keys
  425. //
  426. if (empty($sReconcKeys))
  427. {
  428. $aReconcSpec = array();
  429. // Base reconciliation scheme on the default one
  430. // The reconciliation attributes not present in the data will be ignored
  431. foreach(MetaModel::GetReconcKeys($sClass) as $sReconcKeyAttCode)
  432. {
  433. if (in_array($sReconcKeyAttCode, $aFieldList))
  434. {
  435. $aReconcSpec[] = $sReconcKeyAttCode;
  436. }
  437. }
  438. if (count($aReconcSpec) == 0)
  439. {
  440. throw new BulkLoadException("No reconciliation scheme could be defined, please add a column corresponding to one defined reconciliation key (class: '$sClass', reconciliation:".implode(',', MetaModel::GetReconcKeys($sClass)).")");
  441. }
  442. $sReconcKeys = implode(',', $aReconcSpec);
  443. }
  444. // Interpret the list of reconciliation keys
  445. //
  446. $aFinalReconcilKeys = array();
  447. $aReconcilKeysReport = array();
  448. foreach (explode(',', $sReconcKeys) as $sReconcKey)
  449. {
  450. $sReconcKey = trim($sReconcKey);
  451. if (empty($sReconcKey)) continue; // skip empty spec
  452. if (!in_array($sReconcKey, $aFieldList))
  453. {
  454. throw new BulkLoadException("Reconciliation keys not found in the input columns '$sReconcKey' (class: '$sClass')");
  455. }
  456. if (preg_match('/^(.+)->(.+)$/', trim($sReconcKey), $aMatches))
  457. {
  458. // The column has been specified as "extkey->attcode"
  459. //
  460. $sExtKeyAttCode = $aMatches[1];
  461. $sRemoteAttCode = $aMatches[2];
  462. $aFinalReconcilKeys[] = $sExtKeyAttCode;
  463. $aReconcilKeysReport[$sExtKeyAttCode][] = $sRemoteAttCode;
  464. }
  465. else
  466. {
  467. if (!MetaModel::IsValidAttCode($sClass, $sReconcKey))
  468. {
  469. // Safety net: should never happen, but...
  470. throw new BulkLoadException("Unknown reconciliation attribute '$sReconcKey' (class: '$sClass')");
  471. }
  472. $oAtt = MetaModel::GetAttributeDef($sClass, $sReconcKey);
  473. if ($oAtt->IsExternalKey())
  474. {
  475. $aFinalReconcilKeys[] = $sReconcKey;
  476. $aReconcilKeysReport[$sReconcKey][] = 'id';
  477. }
  478. elseif ($oAtt->IsExternalField())
  479. {
  480. $sReconcAttCode = $oAtt->GetKeyAttCode();
  481. $sReconcKeyReport = "$sReconcAttCode ($sReconcKey)";
  482. $aFinalReconcilKeys[] = $sReconcAttCode;
  483. $aReconcilKeysReport[$sReconcAttCode][] = $sReconcKeyReport;
  484. }
  485. else
  486. {
  487. $aFinalReconcilKeys[] = $sReconcKey;
  488. $aReconcilKeysReport[$sReconcKey] = array();
  489. }
  490. }
  491. }
  492. //////////////////////////////////////////////////
  493. //
  494. // Go for parsing and interpretation
  495. //
  496. $aData = $oCSVParser->ToArray();
  497. $iLineCount = count($aData);
  498. if (($sOutput == "summary") || ($sOutput == 'details'))
  499. {
  500. $oP->add_comment("Data Lines: ".$iLineCount);
  501. $oP->add_comment("Simulate: ".($bSimulate ? '1' : '0'));
  502. $oP->add_comment("Columns: ".implode(', ', $aFieldList));
  503. $aReconciliationReport = array();
  504. foreach($aReconcilKeysReport as $sKey => $aKeyDetails)
  505. {
  506. if (count($aKeyDetails) > 0)
  507. {
  508. $aReconciliationReport[] = $sKey.' ('.implode(',', $aKeyDetails).')';
  509. }
  510. else
  511. {
  512. $aReconciliationReport[] = $sKey;
  513. }
  514. }
  515. $oP->add_comment("Reconciliation Keys: ".implode(', ', $aReconciliationReport));
  516. }
  517. $oBulk = new BulkChange(
  518. $sClass,
  519. $aData,
  520. $aAttList,
  521. $aExtKeys,
  522. $aFinalReconcilKeys
  523. );
  524. if ($bSimulate)
  525. {
  526. $oMyChange = null;
  527. }
  528. else
  529. {
  530. $oMyChange = MetaModel::NewObject("CMDBChange");
  531. $oMyChange->Set("date", time());
  532. $sUserString = CMDBChange::GetCurrentUserName();
  533. if (strlen($sComment) > 0)
  534. {
  535. $sMoreInfo = 'Web Service (CSV) - '.$sComment;
  536. }
  537. else
  538. {
  539. $sMoreInfo = 'Web Service (CSV)';
  540. }
  541. $oMyChange->Set("userinfo", $sUserString.', '.$sMoreInfo);
  542. $iChangeId = $oMyChange->DBInsert();
  543. }
  544. $aRes = $oBulk->Process($oMyChange);
  545. //////////////////////////////////////////////////
  546. //
  547. // Compute statistics
  548. //
  549. $iCountErrors = 0;
  550. $iCountWarnings = 0;
  551. $iCountCreations = 0;
  552. $iCountUpdates = 0;
  553. $iCountUnchanged = 0;
  554. foreach($aRes as $iRow => $aRowData)
  555. {
  556. $bWritten = false;
  557. $oStatus = $aRowData["__STATUS__"];
  558. switch(get_class($oStatus))
  559. {
  560. case 'RowStatus_NoChange':
  561. $iCountUnchanged++;
  562. break;
  563. case 'RowStatus_Modify':
  564. $iCountUpdates++;
  565. $bWritten = true;
  566. break;
  567. case 'RowStatus_NewObj':
  568. $iCountCreations++;
  569. $bWritten = true;
  570. break;
  571. case 'RowStatus_Issue':
  572. $iCountErrors++;
  573. break;
  574. }
  575. if ($bWritten)
  576. {
  577. // Something has been done, still there may be some issues to report
  578. foreach($aRowData as $key => $value)
  579. {
  580. if (!is_object($value)) continue;
  581. switch (get_class($value))
  582. {
  583. case 'CellStatus_Void':
  584. case 'CellStatus_Modify':
  585. break;
  586. case 'CellStatus_Issue':
  587. case 'CellStatus_SearchIssue':
  588. case 'CellStatus_NullIssue':
  589. case 'CellStatus_Ambiguous':
  590. $iCountWarnings++;
  591. break;
  592. }
  593. }
  594. }
  595. }
  596. //////////////////////////////////////////////////
  597. //
  598. // Summary of settings and results
  599. //
  600. if ($sOutput == 'retcode')
  601. {
  602. $oP->add($iCountErrors);
  603. }
  604. if (($sOutput == "summary") || ($sOutput == 'details'))
  605. {
  606. // $oP->add_comment("Report level: ".$sReportLevel);
  607. $oP->add_comment("Change tracking comment: ".$sComment);
  608. $oP->add_comment("Issues: ".$iCountErrors);
  609. $oP->add_comment("Warnings: ".$iCountWarnings);
  610. $oP->add_comment("Created: ".$iCountCreations);
  611. $oP->add_comment("Updated: ".$iCountUpdates);
  612. $oP->add_comment("Unchanged: ".$iCountUnchanged);
  613. }
  614. if ($sOutput == 'details')
  615. {
  616. // Setup result presentation
  617. //
  618. $aDisplayConfig = array();
  619. $aDisplayConfig["__LINE__"] = array("label"=>"Line", "description"=>"");
  620. $aDisplayConfig["__STATUS__"] = array("label"=>"Status", "description"=>"");
  621. $aDisplayConfig["__OBJECT_CLASS__"] = array("label"=>"Object Class", "description"=>"");
  622. $aDisplayConfig["__OBJECT_ID__"] = array("label"=>"Object Id", "description"=>"");
  623. foreach($aExtKeys as $sExtKeyAttCode => $aRemoteAtt)
  624. {
  625. $sLabel = MetaModel::GetAttributeDef($sClass, $sExtKeyAttCode)->GetLabel();
  626. $aDisplayConfig["$sExtKeyAttCode"] = array("label"=>$sExtKeyAttCode, "description"=>$sLabel." - ext key");
  627. }
  628. foreach($aFinalReconcilKeys as $iCol => $sAttCode)
  629. {
  630. // $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  631. // $aDisplayConfig["$iCol"] = array("label"=>"$sLabel", "description"=>"");
  632. }
  633. foreach ($aAttList as $sAttCode => $iCol)
  634. {
  635. if ($sAttCode == 'id')
  636. {
  637. $sLabel = Dict::S('UI:CSVImport:idField');
  638. $aDisplayConfig["$iCol"] = array("label"=>$sAttCode, "description"=>$sLabel);
  639. }
  640. else
  641. {
  642. $sLabel = MetaModel::GetAttributeDef($sClass, $sAttCode)->GetLabel();
  643. $aDisplayConfig["$iCol"] = array("label"=>$sAttCode, "description"=>$sLabel);
  644. }
  645. }
  646. $aResultDisp = array(); // to be displayed
  647. foreach($aRes as $iRow => $aRowData)
  648. {
  649. $aRowDisp = array();
  650. $aRowDisp["__LINE__"] = $iRow;
  651. if (is_object($aRowData["__STATUS__"]))
  652. {
  653. $aRowDisp["__STATUS__"] = $aRowData["__STATUS__"]->GetDescription();
  654. }
  655. else
  656. {
  657. $aRowDisp["__STATUS__"] = "*No status available*";
  658. }
  659. if (isset($aRowData["finalclass"]) && isset($aRowData["id"]))
  660. {
  661. $aRowDisp["__OBJECT_CLASS__"] = $aRowData["finalclass"];
  662. $aRowDisp["__OBJECT_ID__"] = $aRowData["id"]->GetDisplayableValue();
  663. }
  664. else
  665. {
  666. $aRowDisp["__OBJECT_CLASS__"] = "n/a";
  667. $aRowDisp["__OBJECT_ID__"] = "n/a";
  668. }
  669. foreach($aRowData as $key => $value)
  670. {
  671. $sKey = (string) $key;
  672. if ($sKey == '__STATUS__') continue;
  673. if ($sKey == 'finalclass') continue;
  674. if ($sKey == 'id') continue;
  675. if (is_object($value))
  676. {
  677. $aRowDisp["$sKey"] = $value->GetDisplayableValue().$value->GetDescription();
  678. }
  679. else
  680. {
  681. $aRowDisp["$sKey"] = $value;
  682. }
  683. }
  684. $aResultDisp[$iRow] = $aRowDisp;
  685. }
  686. $oP->table($aDisplayConfig, $aResultDisp);
  687. }
  688. }
  689. catch(BulkLoadException $e)
  690. {
  691. $oP->add_comment($e->getMessage());
  692. }
  693. catch(SecurityException $e)
  694. {
  695. $oP->add_comment($e->getMessage());
  696. }
  697. catch(Exception $e)
  698. {
  699. $oP->add_comment((string)$e);
  700. }
  701. $oP->output();
  702. ?>