bulkchange.class.inc.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  708. foreach($this->m_aData as $iRow => $aRowData)
  709. {
  710. set_time_limit($iLoopTimeLimit);
  711. if (isset($aResult[$iRow]["__STATUS__"]))
  712. {
  713. // An issue at the earlier steps - skip the rest
  714. continue;
  715. }
  716. try
  717. {
  718. $oReconciliationFilter = new CMDBSearchFilter($this->m_sClass);
  719. $bSkipQuery = false;
  720. foreach($this->m_aReconcilKeys as $sAttCode)
  721. {
  722. $valuecondition = null;
  723. if (array_key_exists($sAttCode, $this->m_aExtKeys))
  724. {
  725. if ($this->IsNullExternalKeySpec($aRowData, $sAttCode))
  726. {
  727. $oExtKey = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  728. if ($oExtKey->IsNullAllowed())
  729. {
  730. $valuecondition = $oExtKey->GetNullValue();
  731. $aResult[$iRow][$sAttCode] = new CellStatus_Void($oExtKey->GetNullValue());
  732. }
  733. else
  734. {
  735. $aResult[$iRow][$sAttCode] = new CellStatus_NullIssue();
  736. }
  737. }
  738. else
  739. {
  740. // The value has to be found or verified
  741. list($sQuery, $aMatches) = $this->ResolveExternalKey($aRowData, $sAttCode, $aResult[$iRow]);
  742. if (count($aMatches) == 1)
  743. {
  744. $oRemoteObj = reset($aMatches); // first item
  745. $valuecondition = $oRemoteObj->GetKey();
  746. $aResult[$iRow][$sAttCode] = new CellStatus_Void($oRemoteObj->GetKey());
  747. }
  748. elseif (count($aMatches) == 0)
  749. {
  750. $aResult[$iRow][$sAttCode] = new CellStatus_SearchIssue();
  751. }
  752. else
  753. {
  754. $aResult[$iRow][$sAttCode] = new CellStatus_Ambiguous(null, count($aMatches), $sQuery);
  755. }
  756. }
  757. }
  758. else
  759. {
  760. // The value is given in the data row
  761. $iCol = $this->m_aAttList[$sAttCode];
  762. if ($sAttCode == 'id')
  763. {
  764. $valuecondition = $aRowData[$iCol];
  765. }
  766. else
  767. {
  768. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  769. $valuecondition = $oAttDef->MakeValueFromString($aRowData[$iCol], $this->m_bLocalizedValues);
  770. }
  771. }
  772. if (is_null($valuecondition))
  773. {
  774. $bSkipQuery = true;
  775. }
  776. else
  777. {
  778. $oReconciliationFilter->AddCondition($sAttCode, $valuecondition, '=');
  779. }
  780. }
  781. if ($bSkipQuery)
  782. {
  783. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue(Dict::S('UI:CSVReport-Row-Issue-Reconciliation'));
  784. }
  785. else
  786. {
  787. $oReconciliationSet = new CMDBObjectSet($oReconciliationFilter);
  788. switch($oReconciliationSet->Count())
  789. {
  790. case 0:
  791. $oTargetObj = $this->CreateObject($aResult, $iRow, $aRowData, $oChange);
  792. // $aResult[$iRow]["__STATUS__"]=> set in CreateObject
  793. $aVisited[] = $oTargetObj->GetKey();
  794. break;
  795. case 1:
  796. $oTargetObj = $oReconciliationSet->Fetch();
  797. $this->UpdateObject($aResult, $iRow, $oTargetObj, $aRowData, $oChange);
  798. // $aResult[$iRow]["__STATUS__"]=> set in UpdateObject
  799. if (!is_null($this->m_sSynchroScope))
  800. {
  801. $aVisited[] = $oTargetObj->GetKey();
  802. }
  803. break;
  804. default:
  805. // Found several matches, ambiguous
  806. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue(Dict::S('UI:CSVReport-Row-Issue-Ambiguous'));
  807. $aResult[$iRow]["id"]= new CellStatus_Ambiguous(0, $oReconciliationSet->Count(), $oReconciliationFilter->ToOql());
  808. $aResult[$iRow]["finalclass"]= 'n/a';
  809. }
  810. }
  811. }
  812. catch (Exception $e)
  813. {
  814. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue(Dict::Format('UI:CSVReport-Row-Issue-Internal', get_class($e), $e->getMessage()));
  815. }
  816. }
  817. if (!is_null($this->m_sSynchroScope))
  818. {
  819. // Compute the delta between the scope and visited objects
  820. $oScopeSearch = DBObjectSearch::FromOQL($this->m_sSynchroScope);
  821. $oScopeSet = new DBObjectSet($oScopeSearch);
  822. while ($oObj = $oScopeSet->Fetch())
  823. {
  824. $iObj = $oObj->GetKey();
  825. if (!in_array($iObj, $aVisited))
  826. {
  827. set_time_limit($iLoopTimeLimit);
  828. $iRow++;
  829. $this->UpdateMissingObject($aResult, $iRow, $oObj, $oChange);
  830. }
  831. }
  832. }
  833. set_time_limit($iPreviousTimeLimit);
  834. // Fill in the blanks - the result matrix is expected to be 100% complete
  835. //
  836. foreach($this->m_aData as $iRow => $aRowData)
  837. {
  838. foreach($this->m_aAttList as $iCol)
  839. {
  840. if (!array_key_exists($iCol, $aResult[$iRow]))
  841. {
  842. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  843. }
  844. }
  845. foreach($this->m_aExtKeys as $sAttCode => $aForeignAtts)
  846. {
  847. if (!array_key_exists($sAttCode, $aResult[$iRow]))
  848. {
  849. $aResult[$iRow][$sAttCode] = new CellStatus_Void('n/a');
  850. }
  851. foreach ($aForeignAtts as $sForeignAttCode => $iCol)
  852. {
  853. if (!array_key_exists($iCol, $aResult[$iRow]))
  854. {
  855. // The foreign attribute is one of our reconciliation key
  856. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  857. }
  858. }
  859. }
  860. }
  861. return $aResult;
  862. }
  863. /**
  864. * Display the history of bulk imports
  865. */
  866. static function DisplayImportHistory(WebPage $oPage, $bFromAjax = false, $bShowAll = false)
  867. {
  868. $sAjaxDivId = "CSVImportHistory";
  869. if (!$bFromAjax)
  870. {
  871. $oPage->add('<div id="'.$sAjaxDivId.'">');
  872. }
  873. $oPage->p(Dict::S('UI:History:BulkImports+'));
  874. $oBulkChangeSearch = DBObjectSearch::FromOQL("SELECT CMDBChange WHERE userinfo LIKE '%(CSV)'");
  875. $iQueryLimit = $bShowAll ? 0 : MetaModel::GetConfig()->GetMaxDisplayLimit() + 1;
  876. $oBulkChanges = new DBObjectSet($oBulkChangeSearch, array('date' => false), array(), null, $iQueryLimit);
  877. $oAppContext = new ApplicationContext();
  878. $bLimitExceeded = false;
  879. if ($oBulkChanges->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit())
  880. {
  881. $bLimitExceeded = true;
  882. if (!$bShowAll)
  883. {
  884. $iMaxObjects = appUserPreferences::GetPref('default_page_size', MetaModel::GetConfig()->GetMinDisplayLimit());
  885. $oBulkChanges->SetLimit($iMaxObjects);
  886. }
  887. }
  888. $oBulkChanges->Seek(0);
  889. $aDetails = array();
  890. while ($oChange = $oBulkChanges->Fetch())
  891. {
  892. $sDate = '<a href="csvimport.php?step=10&changeid='.$oChange->GetKey().'&'.$oAppContext->GetForLink().'">'.$oChange->Get('date').'</a>';
  893. $sUser = $oChange->GetUserName();
  894. if (preg_match('/^(.*)\\(CSV\\)$/i', $oChange->Get('userinfo'), $aMatches))
  895. {
  896. $sUser = $aMatches[1];
  897. }
  898. else
  899. {
  900. $sUser = $oChange->Get('userinfo');
  901. }
  902. $oOpSearch = DBObjectSearch::FromOQL("SELECT CMDBChangeOpCreate WHERE change = :change_id");
  903. $oOpSet = new DBObjectSet($oOpSearch, array(), array('change_id' => $oChange->GetKey()));
  904. $iCreated = $oOpSet->Count();
  905. // Get the class from the first item found (assumption: a CSV load is done for a single class)
  906. if ($oCreateOp = $oOpSet->Fetch())
  907. {
  908. $sClass = $oCreateOp->Get('objclass');
  909. }
  910. $oOpSearch = DBObjectSearch::FromOQL("SELECT CMDBChangeOpSetAttribute WHERE change = :change_id");
  911. $oOpSet = new DBObjectSet($oOpSearch, array(), array('change_id' => $oChange->GetKey()));
  912. $aModified = array();
  913. $aAttList = array();
  914. while ($oModified = $oOpSet->Fetch())
  915. {
  916. // Get the class (if not done earlier on object creation)
  917. $sClass = $oModified->Get('objclass');
  918. $iKey = $oModified->Get('objkey');
  919. $sAttCode = $oModified->Get('attcode');
  920. $aAttList[$sClass][$sAttCode] = true;
  921. $aModified["$sClass::$iKey"] = true;
  922. }
  923. $iModified = count($aModified);
  924. // Assumption: there is only one class of objects being loaded
  925. // Then the last class found gives us the class for every object
  926. if ( ($iModified > 0) || ($iCreated > 0))
  927. {
  928. $aDetails[] = array('date' => $sDate, 'user' => $sUser, 'class' => $sClass, 'created' => $iCreated, 'modified' => $iModified);
  929. }
  930. }
  931. $aConfig = array( 'date' => array('label' => Dict::S('UI:History:Date'), 'description' => Dict::S('UI:History:Date+')),
  932. 'user' => array('label' => Dict::S('UI:History:User'), 'description' => Dict::S('UI:History:User+')),
  933. 'class' => array('label' => Dict::S('Core:AttributeClass'), 'description' => Dict::S('Core:AttributeClass+')),
  934. 'created' => array('label' => Dict::S('UI:History:StatsCreations'), 'description' => Dict::S('UI:History:StatsCreations+')),
  935. 'modified' => array('label' => Dict::S('UI:History:StatsModifs'), 'description' => Dict::S('UI:History:StatsModifs+')),
  936. );
  937. if ($bLimitExceeded)
  938. {
  939. if ($bShowAll)
  940. {
  941. // Collapsible list
  942. $oPage->add('<p>'.Dict::Format('UI:CountOfResults', $oBulkChanges->Count()).'&nbsp;&nbsp;<a class="truncated" onclick="OnTruncatedHistoryToggle(false);">'.Dict::S('UI:CollapseList').'</a></p>');
  943. }
  944. else
  945. {
  946. // Truncated list
  947. $iMinDisplayLimit = MetaModel::GetConfig()->GetMinDisplayLimit();
  948. $sCollapsedLabel = Dict::Format('UI:TruncatedResults', $iMinDisplayLimit, $oBulkChanges->Count());
  949. $sLinkLabel = Dict::S('UI:DisplayAll');
  950. $oPage->add('<p>'.$sCollapsedLabel.'&nbsp;&nbsp;<a class="truncated" onclick="OnTruncatedHistoryToggle(true);">'.$sLinkLabel.'</p>');
  951. $oPage->add_ready_script(
  952. <<<EOF
  953. $('#$sAjaxDivId table.listResults').addClass('truncated');
  954. $('#$sAjaxDivId table.listResults tr:last td').addClass('truncated');
  955. EOF
  956. );
  957. $sAppContext = $oAppContext->GetForLink();
  958. $oPage->add_script(
  959. <<<EOF
  960. function OnTruncatedHistoryToggle(bShowAll)
  961. {
  962. $.get(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?{$sAppContext}', {operation: 'displayCSVHistory', showall: bShowAll}, function(data)
  963. {
  964. $('#$sAjaxDivId').html(data);
  965. var table = $('#$sAjaxDivId .listResults');
  966. table.tableHover(); // hover tables
  967. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  968. }
  969. );
  970. }
  971. EOF
  972. );
  973. }
  974. }
  975. else
  976. {
  977. // Normal display - full list without any decoration
  978. }
  979. $oPage->table($aConfig, $aDetails);
  980. if (!$bFromAjax)
  981. {
  982. $oPage->add('</div>');
  983. }
  984. }
  985. /**
  986. * Display the details of an import
  987. */
  988. static function DisplayImportHistoryDetails(iTopWebPage $oPage, $iChange)
  989. {
  990. if ($iChange == 0)
  991. {
  992. throw new Exception("Missing parameter changeid");
  993. }
  994. $oChange = MetaModel::GetObject('CMDBChange', $iChange, false);
  995. if (is_null($oChange))
  996. {
  997. throw new Exception("Unknown change: $iChange");
  998. }
  999. $oPage->add("<div><p><h1>".Dict::Format('UI:History:BulkImportDetails', $oChange->Get('date'), $oChange->GetUserName())."</h1></p></div>\n");
  1000. // Assumption : change made one single class of objects
  1001. $aObjects = array();
  1002. $aAttributes = array(); // array of attcode => occurences
  1003. $oOpSearch = DBObjectSearch::FromOQL("SELECT CMDBChangeOp WHERE change = :change_id");
  1004. $oOpSet = new DBObjectSet($oOpSearch, array(), array('change_id' => $iChange));
  1005. while ($oOperation = $oOpSet->Fetch())
  1006. {
  1007. $sClass = $oOperation->Get('objclass');
  1008. $iKey = $oOperation->Get('objkey');
  1009. $iObjId = "$sClass::$iKey";
  1010. if (!isset($aObjects[$iObjId]))
  1011. {
  1012. $aObjects[$iObjId] = array();
  1013. $aObjects[$iObjId]['__class__'] = $sClass;
  1014. $aObjects[$iObjId]['__id__'] = $iKey;
  1015. }
  1016. if (get_class($oOperation) == 'CMDBChangeOpCreate')
  1017. {
  1018. $aObjects[$iObjId]['__created__'] = true;
  1019. }
  1020. elseif ($oOperation instanceof CMDBChangeOpSetAttribute)
  1021. {
  1022. $sAttCode = $oOperation->Get('attcode');
  1023. if (get_class($oOperation) == 'CMDBChangeOpSetAttributeScalar')
  1024. {
  1025. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1026. if ($oAttDef->IsExternalKey())
  1027. {
  1028. $sOldValue = Dict::S('UI:UndefinedObject');
  1029. if ($oOperation->Get('oldvalue') != 0)
  1030. {
  1031. $oOldTarget = MetaModel::GetObject($oAttDef->GetTargetClass(), $oOperation->Get('oldvalue'));
  1032. $sOldValue = $oOldTarget->GetHyperlink();
  1033. }
  1034. $sNewValue = Dict::S('UI:UndefinedObject');
  1035. if ($oOperation->Get('newvalue') != 0)
  1036. {
  1037. $oNewTarget = MetaModel::GetObject($oAttDef->GetTargetClass(), $oOperation->Get('newvalue'));
  1038. $sNewValue = $oNewTarget->GetHyperlink();
  1039. }
  1040. }
  1041. else
  1042. {
  1043. $sOldValue = $oOperation->GetAsHTML('oldvalue');
  1044. $sNewValue = $oOperation->GetAsHTML('newvalue');
  1045. }
  1046. $aObjects[$iObjId][$sAttCode] = $sOldValue.' -&gt; '.$sNewValue;
  1047. }
  1048. else
  1049. {
  1050. $aObjects[$iObjId][$sAttCode] = 'n/a';
  1051. }
  1052. if (isset($aAttributes[$sAttCode]))
  1053. {
  1054. $aAttributes[$sAttCode]++;
  1055. }
  1056. else
  1057. {
  1058. $aAttributes[$sAttCode] = 1;
  1059. }
  1060. }
  1061. }
  1062. $aDetails = array();
  1063. foreach($aObjects as $iUId => $aObjData)
  1064. {
  1065. $aRow = array();
  1066. $oObject = MetaModel::GetObject($aObjData['__class__'], $aObjData['__id__'], false);
  1067. if (is_null($oObject))
  1068. {
  1069. $aRow['object'] = $aObjData['__class__'].'::'.$aObjData['__id__'].' (deleted)';
  1070. }
  1071. else
  1072. {
  1073. $aRow['object'] = $oObject->GetHyperlink();
  1074. }
  1075. if (isset($aObjData['__created__']))
  1076. {
  1077. $aRow['operation'] = Dict::S('Change:ObjectCreated');
  1078. }
  1079. else
  1080. {
  1081. $aRow['operation'] = Dict::S('Change:ObjectModified');
  1082. }
  1083. foreach ($aAttributes as $sAttCode => $iOccurences)
  1084. {
  1085. if (isset($aObjData[$sAttCode]))
  1086. {
  1087. $aRow[$sAttCode] = $aObjData[$sAttCode];
  1088. }
  1089. elseif (!is_null($oObject))
  1090. {
  1091. // This is the current vaslue: $oObject->GetAsHtml($sAttCode)
  1092. // whereas we are displaying the value that was set at the time
  1093. // the object was created
  1094. // This requires addtional coding...let's do that later
  1095. $aRow[$sAttCode] = '';
  1096. }
  1097. else
  1098. {
  1099. $aRow[$sAttCode] = '';
  1100. }
  1101. }
  1102. $aDetails[] = $aRow;
  1103. }
  1104. $aConfig = array();
  1105. $aConfig['object'] = array('label' => MetaModel::GetName($sClass), 'description' => MetaModel::GetClassDescription($sClass));
  1106. $aConfig['operation'] = array('label' => Dict::S('UI:History:Changes'), 'description' => Dict::S('UI:History:Changes+'));
  1107. foreach ($aAttributes as $sAttCode => $iOccurences)
  1108. {
  1109. $aConfig[$sAttCode] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'description' => MetaModel::GetDescription($sClass, $sAttCode));
  1110. }
  1111. $oPage->table($aConfig, $aDetails);
  1112. }
  1113. }
  1114. ?>