bulkchange.class.inc.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. <?php
  2. // Copyright (C) 2010-2013 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Bulk change facility (common to interactive and batch usages)
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. // The BOM is added at the head of exported UTF-8 CSV data, and removed (if present) from input UTF-8 data.
  25. // This helps MS-Excel (Version > 2007, Windows only) in changing its interpretation of a CSV file (by default Excel reads data as ISO-8859-1 -not 100% sure!)
  26. define('UTF8_BOM', chr(239).chr(187).chr(191)); // 0xEF, 0xBB, 0xBF
  27. /**
  28. * BulkChange
  29. * Interpret a given data set and update the DB accordingly (fake mode avail.)
  30. *
  31. * @package iTopORM
  32. */
  33. class BulkChangeException extends CoreException
  34. {
  35. }
  36. /**
  37. * CellChangeSpec
  38. * A series of classes, keeping the information about a given cell: could it be changed or not (and why)?
  39. *
  40. * @package iTopORM
  41. */
  42. abstract class CellChangeSpec
  43. {
  44. protected $m_proposedValue;
  45. protected $m_sOql; // in case of ambiguity
  46. public function __construct($proposedValue, $sOql = '')
  47. {
  48. $this->m_proposedValue = $proposedValue;
  49. $this->m_sOql = $sOql;
  50. }
  51. public function GetPureValue()
  52. {
  53. // Todo - distinguish both values
  54. return $this->m_proposedValue;
  55. }
  56. public function GetDisplayableValue()
  57. {
  58. return $this->m_proposedValue;
  59. }
  60. public function GetOql()
  61. {
  62. return $this->m_sOql;
  63. }
  64. abstract public function GetDescription();
  65. }
  66. class CellStatus_Void extends CellChangeSpec
  67. {
  68. public function GetDescription()
  69. {
  70. return '';
  71. }
  72. }
  73. class CellStatus_Modify extends CellChangeSpec
  74. {
  75. protected $m_previousValue;
  76. public function __construct($proposedValue, $previousValue = null)
  77. {
  78. // Unused (could be costly to know -see the case of reconciliation on ext keys)
  79. //$this->m_previousValue = $previousValue;
  80. parent::__construct($proposedValue);
  81. }
  82. public function GetDescription()
  83. {
  84. return Dict::S('UI:CSVReport-Value-Modified');
  85. }
  86. //public function GetPreviousValue()
  87. //{
  88. // return $this->m_previousValue;
  89. //}
  90. }
  91. class CellStatus_Issue extends CellStatus_Modify
  92. {
  93. protected $m_sReason;
  94. public function __construct($proposedValue, $previousValue, $sReason)
  95. {
  96. $this->m_sReason = $sReason;
  97. parent::__construct($proposedValue, $previousValue);
  98. }
  99. public function GetDescription()
  100. {
  101. if (is_null($this->m_proposedValue))
  102. {
  103. return Dict::Format('UI:CSVReport-Value-SetIssue', $this->m_sReason);
  104. }
  105. return Dict::Format('UI:CSVReport-Value-ChangeIssue', $this->m_proposedValue, $this->m_sReason);
  106. }
  107. }
  108. class CellStatus_SearchIssue extends CellStatus_Issue
  109. {
  110. public function __construct()
  111. {
  112. parent::__construct(null, null, null);
  113. }
  114. public function GetDescription()
  115. {
  116. return Dict::S('UI:CSVReport-Value-NoMatch');
  117. }
  118. }
  119. class CellStatus_NullIssue extends CellStatus_Issue
  120. {
  121. public function __construct()
  122. {
  123. parent::__construct(null, null, null);
  124. }
  125. public function GetDescription()
  126. {
  127. return Dict::S('UI:CSVReport-Value-Missing');
  128. }
  129. }
  130. class CellStatus_Ambiguous extends CellStatus_Issue
  131. {
  132. protected $m_iCount;
  133. public function __construct($previousValue, $iCount, $sOql)
  134. {
  135. $this->m_iCount = $iCount;
  136. $this->m_sQuery = $sOql;
  137. parent::__construct(null, $previousValue, '');
  138. }
  139. public function GetDescription()
  140. {
  141. $sCount = $this->m_iCount;
  142. return Dict::Format('UI:CSVReport-Value-Ambiguous', $sCount);
  143. }
  144. }
  145. /**
  146. * RowStatus
  147. * A series of classes, keeping the information about a given row: could it be changed or not (and why)?
  148. *
  149. * @package iTopORM
  150. */
  151. abstract class RowStatus
  152. {
  153. public function __construct()
  154. {
  155. }
  156. abstract public function GetDescription();
  157. }
  158. class RowStatus_NoChange extends RowStatus
  159. {
  160. public function GetDescription()
  161. {
  162. return Dict::S('UI:CSVReport-Row-Unchanged');
  163. }
  164. }
  165. class RowStatus_NewObj extends RowStatus
  166. {
  167. public function GetDescription()
  168. {
  169. return Dict::S('UI:CSVReport-Row-Created');
  170. }
  171. }
  172. class RowStatus_Modify extends RowStatus
  173. {
  174. protected $m_iChanged;
  175. public function __construct($iChanged)
  176. {
  177. $this->m_iChanged = $iChanged;
  178. }
  179. public function GetDescription()
  180. {
  181. return Dict::Format('UI:CSVReport-Row-Updated', $this->m_iChanged);
  182. }
  183. }
  184. class RowStatus_Disappeared extends RowStatus_Modify
  185. {
  186. public function GetDescription()
  187. {
  188. return Dict::Format('UI:CSVReport-Row-Disappeared', $this->m_iChanged);
  189. }
  190. }
  191. class RowStatus_Issue extends RowStatus
  192. {
  193. protected $m_sReason;
  194. public function __construct($sReason)
  195. {
  196. $this->m_sReason = $sReason;
  197. }
  198. public function GetDescription()
  199. {
  200. return Dict::Format('UI:CSVReport-Row-Issue', $this->m_sReason);
  201. }
  202. }
  203. /**
  204. * BulkChange
  205. *
  206. * @package iTopORM
  207. */
  208. class BulkChange
  209. {
  210. protected $m_sClass;
  211. protected $m_aData; // Note: hereafter, iCol maybe actually be any acceptable key (string)
  212. // #@# todo: rename the variables to sColIndex
  213. protected $m_aAttList; // attcode => iCol
  214. protected $m_aExtKeys; // aExtKeys[sExtKeyAttCode][sExtReconcKeyAttCode] = iCol;
  215. protected $m_aReconcilKeys; // attcode (attcode = 'id' for the pkey)
  216. protected $m_sSynchroScope; // OQL - if specified, then the missing items will be reported
  217. protected $m_aOnDisappear; // array of attcode => value, values to be set when an object gets out of scope (ignored if no scope has been defined)
  218. protected $m_sDateFormat; // Date format specification, see utils::StringToTime()
  219. protected $m_bLocalizedValues; // Values in the data set are localized (see AttributeEnum)
  220. public function __construct($sClass, $aData, $aAttList, $aExtKeys, $aReconcilKeys, $sSynchroScope = null, $aOnDisappear = null, $sDateFormat = null, $bLocalize = false)
  221. {
  222. $this->m_sClass = $sClass;
  223. $this->m_aData = $aData;
  224. $this->m_aAttList = $aAttList;
  225. $this->m_aReconcilKeys = $aReconcilKeys;
  226. $this->m_aExtKeys = $aExtKeys;
  227. $this->m_sSynchroScope = $sSynchroScope;
  228. $this->m_aOnDisappear = $aOnDisappear;
  229. $this->m_sDateFormat = $sDateFormat;
  230. $this->m_bLocalizedValues = $bLocalize;
  231. }
  232. protected $m_bReportHtml = false;
  233. protected $m_sReportCsvSep = ',';
  234. protected $m_sReportCsvDelimiter = '"';
  235. public function SetReportHtml()
  236. {
  237. $this->m_bReportHtml = true;
  238. }
  239. public function SetReportCsv($sSeparator = ',', $sDelimiter = '"')
  240. {
  241. $this->m_bReportHtml = false;
  242. $this->m_sReportCsvSep = $sSeparator;
  243. $this->m_sReportCsvDelimiter = $sDelimiter;
  244. }
  245. protected function ResolveExternalKey($aRowData, $sAttCode, &$aResults)
  246. {
  247. $oExtKey = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  248. $oReconFilter = new CMDBSearchFilter($oExtKey->GetTargetClass());
  249. foreach ($this->m_aExtKeys[$sAttCode] as $sForeignAttCode => $iCol)
  250. {
  251. if ($sForeignAttCode == 'id')
  252. {
  253. $value = (int) $aRowData[$iCol];
  254. }
  255. else
  256. {
  257. // The foreign attribute is one of our reconciliation key
  258. $oForeignAtt = MetaModel::GetAttributeDef($oExtKey->GetTargetClass(), $sForeignAttCode);
  259. $value = $oForeignAtt->MakeValueFromString($aRowData[$iCol], $this->m_bLocalizedValues);
  260. }
  261. $oReconFilter->AddCondition($sForeignAttCode, $value, '=');
  262. $aResults[$iCol] = new CellStatus_Void($aRowData[$iCol]);
  263. }
  264. $oExtObjects = new CMDBObjectSet($oReconFilter);
  265. $aKeys = $oExtObjects->ToArray();
  266. return array($oReconFilter->ToOql(), $aKeys);
  267. }
  268. // Returns true if the CSV data specifies that the external key must be left undefined
  269. protected function IsNullExternalKeySpec($aRowData, $sAttCode)
  270. {
  271. $oExtKey = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  272. foreach ($this->m_aExtKeys[$sAttCode] as $sForeignAttCode => $iCol)
  273. {
  274. // The foreign attribute is one of our reconciliation key
  275. if (strlen($aRowData[$iCol]) > 0)
  276. {
  277. return false;
  278. }
  279. }
  280. return true;
  281. }
  282. protected function PrepareObject(&$oTargetObj, $aRowData, &$aErrors)
  283. {
  284. $aResults = array();
  285. $aErrors = array();
  286. // External keys reconciliation
  287. //
  288. foreach($this->m_aExtKeys as $sAttCode => $aKeyConfig)
  289. {
  290. // Skip external keys used for the reconciliation process
  291. // if (!array_key_exists($sAttCode, $this->m_aAttList)) continue;
  292. $oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
  293. if ($this->IsNullExternalKeySpec($aRowData, $sAttCode))
  294. {
  295. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  296. {
  297. // Default reporting
  298. $aResults[$iCol] = new CellStatus_Void($aRowData[$iCol]);
  299. }
  300. if ($oExtKey->IsNullAllowed())
  301. {
  302. $oTargetObj->Set($sAttCode, $oExtKey->GetNullValue());
  303. $aResults[$sAttCode]= new CellStatus_Void($oExtKey->GetNullValue());
  304. }
  305. else
  306. {
  307. $aErrors[$sAttCode] = Dict::S('UI:CSVReport-Value-Issue-Null');
  308. $aResults[$sAttCode]= new CellStatus_Issue(null, $oTargetObj->Get($sAttCode), Dict::S('UI:CSVReport-Value-Issue-Null'));
  309. }
  310. }
  311. else
  312. {
  313. $oReconFilter = new CMDBSearchFilter($oExtKey->GetTargetClass());
  314. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  315. {
  316. // The foreign attribute is one of our reconciliation key
  317. if ($sForeignAttCode == 'id')
  318. {
  319. $value = $aRowData[$iCol];
  320. }
  321. else
  322. {
  323. $oForeignAtt = MetaModel::GetAttributeDef($oExtKey->GetTargetClass(), $sForeignAttCode);
  324. $value = $oForeignAtt->MakeValueFromString($aRowData[$iCol], $this->m_bLocalizedValues);
  325. }
  326. $oReconFilter->AddCondition($sForeignAttCode, $value, '=');
  327. $aResults[$iCol] = new CellStatus_Void($aRowData[$iCol]);
  328. }
  329. $oExtObjects = new CMDBObjectSet($oReconFilter);
  330. switch($oExtObjects->Count())
  331. {
  332. case 0:
  333. $aErrors[$sAttCode] = Dict::S('UI:CSVReport-Value-Issue-NotFound');
  334. $aResults[$sAttCode]= new CellStatus_SearchIssue();
  335. break;
  336. case 1:
  337. // Do change the external key attribute
  338. $oForeignObj = $oExtObjects->Fetch();
  339. $oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
  340. break;
  341. default:
  342. $aErrors[$sAttCode] = Dict::Format('UI:CSVReport-Value-Issue-FoundMany', $oExtObjects->Count());
  343. $aResults[$sAttCode]= new CellStatus_Ambiguous($oTargetObj->Get($sAttCode), $oExtObjects->Count(), $oReconFilter->ToOql());
  344. }
  345. }
  346. // Report
  347. if (!array_key_exists($sAttCode, $aResults))
  348. {
  349. $iForeignObj = $oTargetObj->Get($sAttCode);
  350. if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
  351. {
  352. if ($oTargetObj->IsNew())
  353. {
  354. $aResults[$sAttCode]= new CellStatus_Void($iForeignObj);
  355. }
  356. else
  357. {
  358. $aResults[$sAttCode]= new CellStatus_Modify($iForeignObj, $oTargetObj->GetOriginal($sAttCode));
  359. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  360. {
  361. // Report the change on reconciliation values as well
  362. $aResults[$iCol] = new CellStatus_Modify($aRowData[$iCol]);
  363. }
  364. }
  365. }
  366. else
  367. {
  368. $aResults[$sAttCode]= new CellStatus_Void($iForeignObj);
  369. }
  370. }
  371. }
  372. // Set the object attributes
  373. //
  374. foreach ($this->m_aAttList as $sAttCode => $iCol)
  375. {
  376. // skip the private key, if any
  377. if ($sAttCode == 'id') continue;
  378. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  379. $aReasons = array();
  380. $iFlags = $oTargetObj->GetAttributeFlags($sAttCode, $aReasons);
  381. if ( (($iFlags & OPT_ATT_READONLY) == OPT_ATT_READONLY) && ( $oTargetObj->Get($sAttCode) != $aRowData[$iCol]) )
  382. {
  383. $aErrors[$sAttCode] = Dict::Format('UI:CSVReport-Value-Issue-Readonly', $sAttCode, $oTargetObj->Get($sAttCode), $aRowData[$iCol]);
  384. }
  385. else if ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
  386. {
  387. try
  388. {
  389. $oSet = $oAttDef->MakeValueFromString($aRowData[$iCol], $this->m_bLocalizedValues);
  390. $oTargetObj->Set($sAttCode, $oSet);
  391. }
  392. catch(CoreException $e)
  393. {
  394. $aErrors[$sAttCode] = Dict::Format('UI:CSVReport-Value-Issue-Format', $e->getMessage());
  395. }
  396. }
  397. else
  398. {
  399. $value = $oAttDef->MakeValueFromString($aRowData[$iCol], $this->m_bLocalizedValues);
  400. if (is_null($value) && (strlen($aRowData[$iCol]) > 0))
  401. {
  402. $aErrors[$sAttCode] = Dict::Format('UI:CSVReport-Value-Issue-NoMatch', $sAttCode);
  403. }
  404. else
  405. {
  406. $res = $oTargetObj->CheckValue($sAttCode, $value);
  407. if ($res === true)
  408. {
  409. $oTargetObj->Set($sAttCode, $value);
  410. }
  411. else
  412. {
  413. // $res is a string with the error description
  414. $aErrors[$sAttCode] = Dict::Format('UI:CSVReport-Value-Issue-Unknown', $sAttCode, $res);
  415. }
  416. }
  417. }
  418. }
  419. // Reporting on fields
  420. //
  421. $aChangedFields = $oTargetObj->ListChanges();
  422. foreach ($this->m_aAttList as $sAttCode => $iCol)
  423. {
  424. if ($sAttCode == 'id')
  425. {
  426. $aResults[$iCol]= new CellStatus_Void($aRowData[$iCol]);
  427. }
  428. else
  429. {
  430. if ($this->m_bReportHtml)
  431. {
  432. $sCurValue = $oTargetObj->GetAsHTML($sAttCode, $this->m_bLocalizedValues);
  433. $sOrigValue = $oTargetObj->GetOriginalAsHTML($sAttCode, $this->m_bLocalizedValues);
  434. $sInput = htmlentities($aRowData[$iCol], ENT_QUOTES, 'UTF-8');
  435. }
  436. else
  437. {
  438. $sCurValue = $oTargetObj->GetAsCSV($sAttCode, $this->m_sReportCsvSep, $this->m_sReportCsvDelimiter, $this->m_bLocalizedValues);
  439. $sOrigValue = $oTargetObj->GetOriginalAsCSV($sAttCode, $this->m_sReportCsvSep, $this->m_sReportCsvDelimiter, $this->m_bLocalizedValues);
  440. $sInput = $aRowData[$iCol];
  441. }
  442. if (isset($aErrors[$sAttCode]))
  443. {
  444. $aResults[$iCol]= new CellStatus_Issue($aRowData[$iCol], $sOrigValue, $aErrors[$sAttCode]);
  445. }
  446. elseif (array_key_exists($sAttCode, $aChangedFields))
  447. {
  448. if ($oTargetObj->IsNew())
  449. {
  450. $aResults[$iCol]= new CellStatus_Void($sCurValue);
  451. }
  452. else
  453. {
  454. $aResults[$iCol]= new CellStatus_Modify($sCurValue, $sOrigValue);
  455. }
  456. }
  457. else
  458. {
  459. // By default... nothing happens
  460. $aResults[$iCol]= new CellStatus_Void($aRowData[$iCol]);
  461. }
  462. }
  463. }
  464. // Checks
  465. //
  466. $res = $oTargetObj->CheckConsistency();
  467. if ($res !== true)
  468. {
  469. // $res contains the error description
  470. $aErrors["GLOBAL"] = Dict::Format('UI:CSVReport-Row-Issue-Inconsistent', $res);
  471. }
  472. return $aResults;
  473. }
  474. protected function PrepareMissingObject(&$oTargetObj, &$aErrors)
  475. {
  476. $aResults = array();
  477. $aErrors = array();
  478. // External keys
  479. //
  480. foreach($this->m_aExtKeys as $sAttCode => $aKeyConfig)
  481. {
  482. //$oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
  483. $aResults[$sAttCode]= new CellStatus_Void($oTargetObj->Get($sAttCode));
  484. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  485. {
  486. $aResults[$iCol] = new CellStatus_Void('?');
  487. }
  488. }
  489. // Update attributes
  490. //
  491. foreach($this->m_aOnDisappear as $sAttCode => $value)
  492. {
  493. if (!MetaModel::IsValidAttCode(get_class($oTargetObj), $sAttCode))
  494. {
  495. throw new BulkChangeException('Invalid attribute code', array('class' => get_class($oTargetObj), 'attcode' => $sAttCode));
  496. }
  497. $oTargetObj->Set($sAttCode, $value);
  498. if (!array_key_exists($sAttCode, $this->m_aAttList))
  499. {
  500. // #@# will be out of the reporting... (counted anyway)
  501. }
  502. }
  503. // Reporting on fields
  504. //
  505. $aChangedFields = $oTargetObj->ListChanges();
  506. foreach ($this->m_aAttList as $sAttCode => $iCol)
  507. {
  508. if ($sAttCode == 'id')
  509. {
  510. $aResults[$iCol]= new CellStatus_Void($oTargetObj->GetKey());
  511. }
  512. if (array_key_exists($sAttCode, $aChangedFields))
  513. {
  514. $aResults[$iCol]= new CellStatus_Modify($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode));
  515. }
  516. else
  517. {
  518. // By default... nothing happens
  519. $aResults[$iCol]= new CellStatus_Void($oTargetObj->Get($sAttCode));
  520. }
  521. }
  522. // Checks
  523. //
  524. $res = $oTargetObj->CheckConsistency();
  525. if ($res !== true)
  526. {
  527. // $res contains the error description
  528. $aErrors["GLOBAL"] = Dict::Format('UI:CSVReport-Row-Issue-Inconsistent', $res);
  529. }
  530. return $aResults;
  531. }
  532. protected function CreateObject(&$aResult, $iRow, $aRowData, CMDBChange $oChange = null)
  533. {
  534. $oTargetObj = MetaModel::NewObject($this->m_sClass);
  535. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  536. if (count($aErrors) > 0)
  537. {
  538. $sErrors = implode(', ', $aErrors);
  539. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue(Dict::S('UI:CSVReport-Row-Issue-Attribute'));
  540. return $oTargetObj;
  541. }
  542. // Check that any external key will have a value proposed
  543. $aMissingKeys = array();
  544. foreach (MetaModel::GetExternalKeys($this->m_sClass) as $sExtKeyAttCode => $oExtKey)
  545. {
  546. if (!$oExtKey->IsNullAllowed())
  547. {
  548. if (!array_key_exists($sExtKeyAttCode, $this->m_aExtKeys) && !array_key_exists($sExtKeyAttCode, $this->m_aAttList))
  549. {
  550. $aMissingKeys[] = $oExtKey->GetLabel();
  551. }
  552. }
  553. }
  554. if (count($aMissingKeys) > 0)
  555. {
  556. $sMissingKeys = implode(', ', $aMissingKeys);
  557. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue(Dict::Format('UI:CSVReport-Row-Issue-MissingExtKey', $sMissingKeys));
  558. return $oTargetObj;
  559. }
  560. // Optionaly record the results
  561. //
  562. if ($oChange)
  563. {
  564. $newID = $oTargetObj->DBInsertTrackedNoReload($oChange);
  565. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj($this->m_sClass, $newID);
  566. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  567. $aResult[$iRow]["id"] = new CellStatus_Void($newID);
  568. }
  569. else
  570. {
  571. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj();
  572. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  573. $aResult[$iRow]["id"] = new CellStatus_Void(0);
  574. }
  575. return $oTargetObj;
  576. }
  577. protected function UpdateObject(&$aResult, $iRow, $oTargetObj, $aRowData, CMDBChange $oChange = null)
  578. {
  579. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  580. // Reporting
  581. //
  582. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  583. $aResult[$iRow]["id"] = new CellStatus_Void($oTargetObj->GetKey());
  584. if (count($aErrors) > 0)
  585. {
  586. $sErrors = implode(', ', $aErrors);
  587. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue(Dict::S('UI:CSVReport-Row-Issue-Attribute'));
  588. return;
  589. }
  590. $aChangedFields = $oTargetObj->ListChanges();
  591. if (count($aChangedFields) > 0)
  592. {
  593. $aResult[$iRow]["__STATUS__"] = new RowStatus_Modify(count($aChangedFields));
  594. // Optionaly record the results
  595. //
  596. if ($oChange)
  597. {
  598. try
  599. {
  600. $oTargetObj->DBUpdateTracked($oChange);
  601. }
  602. catch(CoreException $e)
  603. {
  604. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue($e->getMessage());
  605. }
  606. }
  607. }
  608. else
  609. {
  610. $aResult[$iRow]["__STATUS__"] = new RowStatus_NoChange();
  611. }
  612. }
  613. protected function UpdateMissingObject(&$aResult, $iRow, $oTargetObj, CMDBChange $oChange = null)
  614. {
  615. $aResult[$iRow] = $this->PrepareMissingObject($oTargetObj, $aErrors);
  616. // Reporting
  617. //
  618. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  619. $aResult[$iRow]["id"] = new CellStatus_Void($oTargetObj->GetKey());
  620. if (count($aErrors) > 0)
  621. {
  622. $sErrors = implode(', ', $aErrors);
  623. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue(Dict::S('UI:CSVReport-Row-Issue-Attribute'));
  624. return;
  625. }
  626. $aChangedFields = $oTargetObj->ListChanges();
  627. if (count($aChangedFields) > 0)
  628. {
  629. $aResult[$iRow]["__STATUS__"] = new RowStatus_Disappeared(count($aChangedFields));
  630. // Optionaly record the results
  631. //
  632. if ($oChange)
  633. {
  634. try
  635. {
  636. $oTargetObj->DBUpdateTracked($oChange);
  637. }
  638. catch(CoreException $e)
  639. {
  640. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue($e->getMessage());
  641. }
  642. }
  643. }
  644. else
  645. {
  646. $aResult[$iRow]["__STATUS__"] = new RowStatus_Disappeared(0);
  647. }
  648. }
  649. public function Process(CMDBChange $oChange = null)
  650. {
  651. // Note: $oChange can be null, in which case the aim is to check what would be done
  652. // Debug...
  653. //
  654. if (false)
  655. {
  656. echo "<pre>\n";
  657. echo "Attributes:\n";
  658. print_r($this->m_aAttList);
  659. echo "ExtKeys:\n";
  660. print_r($this->m_aExtKeys);
  661. echo "Reconciliation:\n";
  662. print_r($this->m_aReconcilKeys);
  663. echo "Synchro scope:\n";
  664. print_r($this->m_sSynchroScope);
  665. echo "Synchro changes:\n";
  666. print_r($this->m_aOnDisappear);
  667. //echo "Data:\n";
  668. //print_r($this->m_aData);
  669. echo "</pre>\n";
  670. exit;
  671. }
  672. $aResult = array();
  673. if (!is_null($this->m_sDateFormat) && (strlen($this->m_sDateFormat) > 0))
  674. {
  675. // Translate dates from the source data
  676. //
  677. foreach ($this->m_aAttList as $sAttCode => $iCol)
  678. {
  679. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  680. if ($oAttDef instanceof AttributeDateTime)
  681. {
  682. foreach($this->m_aData as $iRow => $aRowData)
  683. {
  684. $sNewDate = utils::StringToTime($this->m_aData[$iRow][$iCol], $this->m_sDateFormat);
  685. if ($sNewDate !== false)
  686. {
  687. // Todo - improve the reporting
  688. $this->m_aData[$iRow][$iCol] = $sNewDate;
  689. }
  690. else
  691. {
  692. // Leave the cell unchanged
  693. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue(Dict::S('UI:CSVReport-Row-Issue-DateFormat'));
  694. $aResult[$iRow][$sAttCode] = new CellStatus_Issue(null, $this->m_aData[$iRow][$iCol], Dict::S('UI:CSVReport-Row-Issue-DateFormat'));
  695. }
  696. }
  697. }
  698. }
  699. }
  700. // Compute the results
  701. //
  702. if (!is_null($this->m_sSynchroScope))
  703. {
  704. $aVisited = array();
  705. }
  706. $iPreviousTimeLimit = ini_get('max_execution_time');
  707. foreach($this->m_aData as $iRow => $aRowData)
  708. {
  709. set_time_limit(5);
  710. if (isset($aResult[$iRow]["__STATUS__"]))
  711. {
  712. // An issue at the earlier steps - skip the rest
  713. continue;
  714. }
  715. try
  716. {
  717. $oReconciliationFilter = new CMDBSearchFilter($this->m_sClass);
  718. $bSkipQuery = false;
  719. foreach($this->m_aReconcilKeys as $sAttCode)
  720. {
  721. $valuecondition = null;
  722. if (array_key_exists($sAttCode, $this->m_aExtKeys))
  723. {
  724. if ($this->IsNullExternalKeySpec($aRowData, $sAttCode))
  725. {
  726. $oExtKey = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  727. if ($oExtKey->IsNullAllowed())
  728. {
  729. $valuecondition = $oExtKey->GetNullValue();
  730. $aResult[$iRow][$sAttCode] = new CellStatus_Void($oExtKey->GetNullValue());
  731. }
  732. else
  733. {
  734. $aResult[$iRow][$sAttCode] = new CellStatus_NullIssue();
  735. }
  736. }
  737. else
  738. {
  739. // The value has to be found or verified
  740. list($sQuery, $aMatches) = $this->ResolveExternalKey($aRowData, $sAttCode, $aResult[$iRow]);
  741. if (count($aMatches) == 1)
  742. {
  743. $oRemoteObj = reset($aMatches); // first item
  744. $valuecondition = $oRemoteObj->GetKey();
  745. $aResult[$iRow][$sAttCode] = new CellStatus_Void($oRemoteObj->GetKey());
  746. }
  747. elseif (count($aMatches) == 0)
  748. {
  749. $aResult[$iRow][$sAttCode] = new CellStatus_SearchIssue();
  750. }
  751. else
  752. {
  753. $aResult[$iRow][$sAttCode] = new CellStatus_Ambiguous(null, count($aMatches), $sQuery);
  754. }
  755. }
  756. }
  757. else
  758. {
  759. // The value is given in the data row
  760. $iCol = $this->m_aAttList[$sAttCode];
  761. if ($sAttCode == 'id')
  762. {
  763. $valuecondition = $aRowData[$iCol];
  764. }
  765. else
  766. {
  767. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  768. $valuecondition = $oAttDef->MakeValueFromString($aRowData[$iCol], $this->m_bLocalizedValues);
  769. }
  770. }
  771. if (is_null($valuecondition))
  772. {
  773. $bSkipQuery = true;
  774. }
  775. else
  776. {
  777. $oReconciliationFilter->AddCondition($sAttCode, $valuecondition, '=');
  778. }
  779. }
  780. if ($bSkipQuery)
  781. {
  782. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue(Dict::S('UI:CSVReport-Row-Issue-Reconciliation'));
  783. }
  784. else
  785. {
  786. $oReconciliationSet = new CMDBObjectSet($oReconciliationFilter);
  787. switch($oReconciliationSet->Count())
  788. {
  789. case 0:
  790. $oTargetObj = $this->CreateObject($aResult, $iRow, $aRowData, $oChange);
  791. // $aResult[$iRow]["__STATUS__"]=> set in CreateObject
  792. $aVisited[] = $oTargetObj->GetKey();
  793. break;
  794. case 1:
  795. $oTargetObj = $oReconciliationSet->Fetch();
  796. $this->UpdateObject($aResult, $iRow, $oTargetObj, $aRowData, $oChange);
  797. // $aResult[$iRow]["__STATUS__"]=> set in UpdateObject
  798. if (!is_null($this->m_sSynchroScope))
  799. {
  800. $aVisited[] = $oTargetObj->GetKey();
  801. }
  802. break;
  803. default:
  804. // Found several matches, ambiguous
  805. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue(Dict::S('UI:CSVReport-Row-Issue-Ambiguous'));
  806. $aResult[$iRow]["id"]= new CellStatus_Ambiguous(0, $oReconciliationSet->Count(), $oReconciliationFilter->ToOql());
  807. $aResult[$iRow]["finalclass"]= 'n/a';
  808. }
  809. }
  810. }
  811. catch (Exception $e)
  812. {
  813. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue(Dict::Format('UI:CSVReport-Row-Issue-Internal', get_class($e), $e->getMessage()));
  814. }
  815. }
  816. if (!is_null($this->m_sSynchroScope))
  817. {
  818. // Compute the delta between the scope and visited objects
  819. $oScopeSearch = DBObjectSearch::FromOQL($this->m_sSynchroScope);
  820. $oScopeSet = new DBObjectSet($oScopeSearch);
  821. while ($oObj = $oScopeSet->Fetch())
  822. {
  823. $iObj = $oObj->GetKey();
  824. if (!in_array($iObj, $aVisited))
  825. {
  826. set_time_limit(5);
  827. $iRow++;
  828. $this->UpdateMissingObject($aResult, $iRow, $oObj, $oChange);
  829. }
  830. }
  831. }
  832. set_time_limit($iPreviousTimeLimit);
  833. // Fill in the blanks - the result matrix is expected to be 100% complete
  834. //
  835. foreach($this->m_aData as $iRow => $aRowData)
  836. {
  837. foreach($this->m_aAttList as $iCol)
  838. {
  839. if (!array_key_exists($iCol, $aResult[$iRow]))
  840. {
  841. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  842. }
  843. }
  844. foreach($this->m_aExtKeys as $sAttCode => $aForeignAtts)
  845. {
  846. if (!array_key_exists($sAttCode, $aResult[$iRow]))
  847. {
  848. $aResult[$iRow][$sAttCode] = new CellStatus_Void('n/a');
  849. }
  850. foreach ($aForeignAtts as $sForeignAttCode => $iCol)
  851. {
  852. if (!array_key_exists($iCol, $aResult[$iRow]))
  853. {
  854. // The foreign attribute is one of our reconciliation key
  855. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  856. }
  857. }
  858. }
  859. }
  860. return $aResult;
  861. }
  862. /**
  863. * Display the history of bulk imports
  864. */
  865. static function DisplayImportHistory(WebPage $oPage, $bFromAjax = false, $bShowAll = false)
  866. {
  867. $sAjaxDivId = "CSVImportHistory";
  868. if (!$bFromAjax)
  869. {
  870. $oPage->add('<div id="'.$sAjaxDivId.'">');
  871. }
  872. $oPage->p(Dict::S('UI:History:BulkImports+'));
  873. $oBulkChangeSearch = DBObjectSearch::FromOQL("SELECT CMDBChange WHERE userinfo LIKE '%(CSV)'");
  874. $iQueryLimit = $bShowAll ? 0 : MetaModel::GetConfig()->GetMaxDisplayLimit() + 1;
  875. $oBulkChanges = new DBObjectSet($oBulkChangeSearch, array('date' => false), array(), null, $iQueryLimit);
  876. $oAppContext = new ApplicationContext();
  877. $bLimitExceeded = false;
  878. if ($oBulkChanges->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit())
  879. {
  880. $bLimitExceeded = true;
  881. if (!$bShowAll)
  882. {
  883. $iMaxObjects = appUserPreferences::GetPref('default_page_size', MetaModel::GetConfig()->GetMinDisplayLimit());
  884. $oBulkChanges->SetLimit($iMaxObjects);
  885. }
  886. }
  887. $oBulkChanges->Seek(0);
  888. $aDetails = array();
  889. while ($oChange = $oBulkChanges->Fetch())
  890. {
  891. $sDate = '<a href="csvimport.php?step=10&changeid='.$oChange->GetKey().'&'.$oAppContext->GetForLink().'">'.$oChange->Get('date').'</a>';
  892. $sUser = $oChange->GetUserName();
  893. if (preg_match('/^(.*)\\(CSV\\)$/i', $oChange->Get('userinfo'), $aMatches))
  894. {
  895. $sUser = $aMatches[1];
  896. }
  897. else
  898. {
  899. $sUser = $oChange->Get('userinfo');
  900. }
  901. $oOpSearch = DBObjectSearch::FromOQL("SELECT CMDBChangeOpCreate WHERE change = :change_id");
  902. $oOpSet = new DBObjectSet($oOpSearch, array(), array('change_id' => $oChange->GetKey()));
  903. $iCreated = $oOpSet->Count();
  904. // Get the class from the first item found (assumption: a CSV load is done for a single class)
  905. if ($oCreateOp = $oOpSet->Fetch())
  906. {
  907. $sClass = $oCreateOp->Get('objclass');
  908. }
  909. $oOpSearch = DBObjectSearch::FromOQL("SELECT CMDBChangeOpSetAttribute WHERE change = :change_id");
  910. $oOpSet = new DBObjectSet($oOpSearch, array(), array('change_id' => $oChange->GetKey()));
  911. $aModified = array();
  912. $aAttList = array();
  913. while ($oModified = $oOpSet->Fetch())
  914. {
  915. // Get the class (if not done earlier on object creation)
  916. $sClass = $oModified->Get('objclass');
  917. $iKey = $oModified->Get('objkey');
  918. $sAttCode = $oModified->Get('attcode');
  919. $aAttList[$sClass][$sAttCode] = true;
  920. $aModified["$sClass::$iKey"] = true;
  921. }
  922. $iModified = count($aModified);
  923. // Assumption: there is only one class of objects being loaded
  924. // Then the last class found gives us the class for every object
  925. if ( ($iModified > 0) || ($iCreated > 0))
  926. {
  927. $aDetails[] = array('date' => $sDate, 'user' => $sUser, 'class' => $sClass, 'created' => $iCreated, 'modified' => $iModified);
  928. }
  929. }
  930. $aConfig = array( 'date' => array('label' => Dict::S('UI:History:Date'), 'description' => Dict::S('UI:History:Date+')),
  931. 'user' => array('label' => Dict::S('UI:History:User'), 'description' => Dict::S('UI:History:User+')),
  932. 'class' => array('label' => Dict::S('Core:AttributeClass'), 'description' => Dict::S('Core:AttributeClass+')),
  933. 'created' => array('label' => Dict::S('UI:History:StatsCreations'), 'description' => Dict::S('UI:History:StatsCreations+')),
  934. 'modified' => array('label' => Dict::S('UI:History:StatsModifs'), 'description' => Dict::S('UI:History:StatsModifs+')),
  935. );
  936. if ($bLimitExceeded)
  937. {
  938. if ($bShowAll)
  939. {
  940. // Collapsible list
  941. $oPage->add('<p>'.Dict::Format('UI:CountOfResults', $oBulkChanges->Count()).'&nbsp;&nbsp;<a class="truncated" onclick="OnTruncatedHistoryToggle(false);">'.Dict::S('UI:CollapseList').'</a></p>');
  942. }
  943. else
  944. {
  945. // Truncated list
  946. $iMinDisplayLimit = MetaModel::GetConfig()->GetMinDisplayLimit();
  947. $sCollapsedLabel = Dict::Format('UI:TruncatedResults', $iMinDisplayLimit, $oBulkChanges->Count());
  948. $sLinkLabel = Dict::S('UI:DisplayAll');
  949. $oPage->add('<p>'.$sCollapsedLabel.'&nbsp;&nbsp;<a class="truncated" onclick="OnTruncatedHistoryToggle(true);">'.$sLinkLabel.'</p>');
  950. $oPage->add_ready_script(
  951. <<<EOF
  952. $('#$sAjaxDivId table.listResults').addClass('truncated');
  953. $('#$sAjaxDivId table.listResults tr:last td').addClass('truncated');
  954. EOF
  955. );
  956. $sAppContext = $oAppContext->GetForLink();
  957. $oPage->add_script(
  958. <<<EOF
  959. function OnTruncatedHistoryToggle(bShowAll)
  960. {
  961. $.get(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?{$sAppContext}', {operation: 'displayCSVHistory', showall: bShowAll}, function(data)
  962. {
  963. $('#$sAjaxDivId').html(data);
  964. var table = $('#$sAjaxDivId .listResults');
  965. table.tableHover(); // hover tables
  966. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  967. }
  968. );
  969. }
  970. EOF
  971. );
  972. }
  973. }
  974. else
  975. {
  976. // Normal display - full list without any decoration
  977. }
  978. $oPage->table($aConfig, $aDetails);
  979. if (!$bFromAjax)
  980. {
  981. $oPage->add('</div>');
  982. }
  983. }
  984. /**
  985. * Display the details of an import
  986. */
  987. static function DisplayImportHistoryDetails(iTopWebPage $oPage, $iChange)
  988. {
  989. if ($iChange == 0)
  990. {
  991. throw new Exception("Missing parameter changeid");
  992. }
  993. $oChange = MetaModel::GetObject('CMDBChange', $iChange, false);
  994. if (is_null($oChange))
  995. {
  996. throw new Exception("Unknown change: $iChange");
  997. }
  998. $oPage->add("<div><p><h1>".Dict::Format('UI:History:BulkImportDetails', $oChange->Get('date'), $oChange->GetUserName())."</h1></p></div>\n");
  999. // Assumption : change made one single class of objects
  1000. $aObjects = array();
  1001. $aAttributes = array(); // array of attcode => occurences
  1002. $oOpSearch = DBObjectSearch::FromOQL("SELECT CMDBChangeOp WHERE change = :change_id");
  1003. $oOpSet = new DBObjectSet($oOpSearch, array(), array('change_id' => $iChange));
  1004. while ($oOperation = $oOpSet->Fetch())
  1005. {
  1006. $sClass = $oOperation->Get('objclass');
  1007. $iKey = $oOperation->Get('objkey');
  1008. $iObjId = "$sClass::$iKey";
  1009. if (!isset($aObjects[$iObjId]))
  1010. {
  1011. $aObjects[$iObjId] = array();
  1012. $aObjects[$iObjId]['__class__'] = $sClass;
  1013. $aObjects[$iObjId]['__id__'] = $iKey;
  1014. }
  1015. if (get_class($oOperation) == 'CMDBChangeOpCreate')
  1016. {
  1017. $aObjects[$iObjId]['__created__'] = true;
  1018. }
  1019. elseif ($oOperation instanceof CMDBChangeOpSetAttribute)
  1020. {
  1021. $sAttCode = $oOperation->Get('attcode');
  1022. if (get_class($oOperation) == 'CMDBChangeOpSetAttributeScalar')
  1023. {
  1024. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1025. if ($oAttDef->IsExternalKey())
  1026. {
  1027. $sOldValue = Dict::S('UI:UndefinedObject');
  1028. if ($oOperation->Get('oldvalue') != 0)
  1029. {
  1030. $oOldTarget = MetaModel::GetObject($oAttDef->GetTargetClass(), $oOperation->Get('oldvalue'));
  1031. $sOldValue = $oOldTarget->GetHyperlink();
  1032. }
  1033. $sNewValue = Dict::S('UI:UndefinedObject');
  1034. if ($oOperation->Get('newvalue') != 0)
  1035. {
  1036. $oNewTarget = MetaModel::GetObject($oAttDef->GetTargetClass(), $oOperation->Get('newvalue'));
  1037. $sNewValue = $oNewTarget->GetHyperlink();
  1038. }
  1039. }
  1040. else
  1041. {
  1042. $sOldValue = $oOperation->GetAsHTML('oldvalue');
  1043. $sNewValue = $oOperation->GetAsHTML('newvalue');
  1044. }
  1045. $aObjects[$iObjId][$sAttCode] = $sOldValue.' -&gt; '.$sNewValue;
  1046. }
  1047. else
  1048. {
  1049. $aObjects[$iObjId][$sAttCode] = 'n/a';
  1050. }
  1051. if (isset($aAttributes[$sAttCode]))
  1052. {
  1053. $aAttributes[$sAttCode]++;
  1054. }
  1055. else
  1056. {
  1057. $aAttributes[$sAttCode] = 1;
  1058. }
  1059. }
  1060. }
  1061. $aDetails = array();
  1062. foreach($aObjects as $iUId => $aObjData)
  1063. {
  1064. $aRow = array();
  1065. $oObject = MetaModel::GetObject($aObjData['__class__'], $aObjData['__id__'], false);
  1066. if (is_null($oObject))
  1067. {
  1068. $aRow['object'] = $aObjData['__class__'].'::'.$aObjData['__id__'].' (deleted)';
  1069. }
  1070. else
  1071. {
  1072. $aRow['object'] = $oObject->GetHyperlink();
  1073. }
  1074. if (isset($aObjData['__created__']))
  1075. {
  1076. $aRow['operation'] = Dict::S('Change:ObjectCreated');
  1077. }
  1078. else
  1079. {
  1080. $aRow['operation'] = Dict::S('Change:ObjectModified');
  1081. }
  1082. foreach ($aAttributes as $sAttCode => $iOccurences)
  1083. {
  1084. if (isset($aObjData[$sAttCode]))
  1085. {
  1086. $aRow[$sAttCode] = $aObjData[$sAttCode];
  1087. }
  1088. elseif (!is_null($oObject))
  1089. {
  1090. // This is the current vaslue: $oObject->GetAsHtml($sAttCode)
  1091. // whereas we are displaying the value that was set at the time
  1092. // the object was created
  1093. // This requires addtional coding...let's do that later
  1094. $aRow[$sAttCode] = '';
  1095. }
  1096. else
  1097. {
  1098. $aRow[$sAttCode] = '';
  1099. }
  1100. }
  1101. $aDetails[] = $aRow;
  1102. }
  1103. $aConfig = array();
  1104. $aConfig['object'] = array('label' => MetaModel::GetName($sClass), 'description' => MetaModel::GetClassDescription($sClass));
  1105. $aConfig['operation'] = array('label' => Dict::S('UI:History:Changes'), 'description' => Dict::S('UI:History:Changes+'));
  1106. foreach ($aAttributes as $sAttCode => $iOccurences)
  1107. {
  1108. $aConfig[$sAttCode] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'description' => MetaModel::GetDescription($sClass, $sAttCode));
  1109. }
  1110. $oPage->table($aConfig, $aDetails);
  1111. }
  1112. }
  1113. ?>