import.php 22 KB

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