cmdbobject.class.inc.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. <?php
  2. // Copyright (C) 2010-2016 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. * Class cmdbObject
  20. *
  21. * @copyright Copyright (C) 2010-2016 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. /**
  25. * cmdbObjectClass
  26. * the file to include, then the core is yours
  27. *
  28. * @package iTopORM
  29. */
  30. require_once('coreexception.class.inc.php');
  31. require_once('config.class.inc.php');
  32. require_once('log.class.inc.php');
  33. require_once('kpi.class.inc.php');
  34. require_once('dict.class.inc.php');
  35. require_once('attributedef.class.inc.php');
  36. require_once('filterdef.class.inc.php');
  37. require_once('stimulus.class.inc.php');
  38. require_once('valuesetdef.class.inc.php');
  39. require_once('MyHelpers.class.inc.php');
  40. require_once('oql/expression.class.inc.php');
  41. require_once('oql/oqlquery.class.inc.php');
  42. require_once('oql/oqlexception.class.inc.php');
  43. require_once('oql/oql-parser.php');
  44. require_once('oql/oql-lexer.php');
  45. require_once('oql/oqlinterpreter.class.inc.php');
  46. require_once('cmdbsource.class.inc.php');
  47. require_once('sqlquery.class.inc.php');
  48. require_once('sqlobjectquery.class.inc.php');
  49. require_once('sqlunionquery.class.inc.php');
  50. require_once('dbobject.class.php');
  51. require_once('dbsearch.class.php');
  52. require_once('dbobjectset.class.php');
  53. require_once('backgroundprocess.inc.php');
  54. require_once('asynctask.class.inc.php');
  55. require_once('dbproperty.class.inc.php');
  56. // db change tracking data model
  57. require_once('cmdbchange.class.inc.php');
  58. require_once('cmdbchangeop.class.inc.php');
  59. // customization data model
  60. // Romain: temporary moved into application.inc.php (see explanations there)
  61. //require_once('trigger.class.inc.php');
  62. //require_once('action.class.inc.php');
  63. // application log
  64. // Romain: temporary moved into application.inc.php (see explanations there)
  65. //require_once('event.class.inc.php');
  66. require_once('templatestring.class.inc.php');
  67. require_once('csvparser.class.inc.php');
  68. require_once('bulkchange.class.inc.php');
  69. /**
  70. * A persistent object, which changes are accurately recorded
  71. *
  72. * @package iTopORM
  73. */
  74. abstract class CMDBObject extends DBObject
  75. {
  76. protected $m_datCreated;
  77. protected $m_datUpdated;
  78. // Note: this value is static, but that could be changed because it is sometimes a real issue (see update of interfaces / connected_to
  79. protected static $m_oCurrChange = null;
  80. protected static $m_sInfo = null; // null => the information is built in a standard way
  81. protected static $m_sOrigin = null; // null => the origin is 'interactive'
  82. /**
  83. * Specify another change (this is mainly for backward compatibility)
  84. */
  85. public static function SetCurrentChange(CMDBChange $oChange)
  86. {
  87. self::$m_oCurrChange = $oChange;
  88. }
  89. //
  90. // Todo: simplify the APIs and do not pass the current change as an argument anymore
  91. // SetTrackInfo to be invoked in very few cases (UI.php, CSV import, Data synchro)
  92. // SetCurrentChange is an alternative to SetTrackInfo (csv ?)
  93. // GetCurrentChange to be called ONCE (!) by CMDBChangeOp::OnInsert ($this->Set('change', ..GetCurrentChange())
  94. // GetCurrentChange to create a default change if not already done in the current context
  95. //
  96. /**
  97. * Get a change record (create it if not existing)
  98. */
  99. public static function GetCurrentChange($bAutoCreate = true)
  100. {
  101. if ($bAutoCreate && is_null(self::$m_oCurrChange))
  102. {
  103. self::CreateChange();
  104. }
  105. return self::$m_oCurrChange;
  106. }
  107. /**
  108. * Override the additional information (defaulting to user name)
  109. * A call to this verb should replace every occurence of
  110. * $oMyChange = MetaModel::NewObject("CMDBChange");
  111. * $oMyChange->Set("date", time());
  112. * $oMyChange->Set("userinfo", 'this is done by ... for ...');
  113. * $iChangeId = $oMyChange->DBInsert();
  114. */
  115. public static function SetTrackInfo($sInfo)
  116. {
  117. self::$m_sInfo = $sInfo;
  118. }
  119. /**
  120. * Provides information about the origin of the change
  121. * @param $sOrigin String: one of: interactive, csv-interactive, csv-import.php, webservice-soap, webservice-rest, syncho-data-source, email-processing, custom-extension
  122. */
  123. public static function SetTrackOrigin($sOrigin)
  124. {
  125. self::$m_sOrigin = $sOrigin;
  126. }
  127. /**
  128. * Get the additional information (defaulting to user name)
  129. */
  130. protected static function GetTrackInfo()
  131. {
  132. if (is_null(self::$m_sInfo))
  133. {
  134. return CMDBChange::GetCurrentUserName();
  135. }
  136. else
  137. {
  138. return self::$m_sInfo;
  139. }
  140. }
  141. /**
  142. * Get the 'origin' information (defaulting to 'interactive')
  143. */
  144. protected static function GetTrackOrigin()
  145. {
  146. if (is_null(self::$m_sOrigin))
  147. {
  148. return 'interactive';
  149. }
  150. else
  151. {
  152. return self::$m_sOrigin;
  153. }
  154. }
  155. /**
  156. * Create a standard change record (done here 99% of the time, and nearly once per page)
  157. */
  158. protected static function CreateChange()
  159. {
  160. self::$m_oCurrChange = MetaModel::NewObject("CMDBChange");
  161. self::$m_oCurrChange->Set("date", time());
  162. self::$m_oCurrChange->Set("userinfo", self::GetTrackInfo());
  163. self::$m_oCurrChange->Set("origin", self::GetTrackOrigin());
  164. self::$m_oCurrChange->DBInsert();
  165. }
  166. protected function RecordObjCreation()
  167. {
  168. // Delete any existing change tracking about the current object (IDs can be reused due to InnoDb bug; see TRAC #886)
  169. //
  170. // 1 - remove the deletion record(s)
  171. // Note that objclass contain the ROOT class
  172. $oFilter = new DBObjectSearch('CMDBChangeOpDelete');
  173. $oFilter->AddCondition('objclass', MetaModel::GetRootClass(get_class($this)), '=');
  174. $oFilter->AddCondition('objkey', $this->GetKey(), '=');
  175. MetaModel::PurgeData($oFilter);
  176. // 2 - any other change tracking information left prior to 2.0.3 (when the purge of the history has been implemented in RecordObjDeletion
  177. // In that case, objclass is the final class of the object
  178. $oFilter = new DBObjectSearch('CMDBChangeOp');
  179. $oFilter->AddCondition('objclass', get_class($this), '=');
  180. $oFilter->AddCondition('objkey', $this->GetKey(), '=');
  181. MetaModel::PurgeData($oFilter);
  182. parent::RecordObjCreation();
  183. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpCreate");
  184. $oMyChangeOp->Set("objclass", get_class($this));
  185. $oMyChangeOp->Set("objkey", $this->GetKey());
  186. $iId = $oMyChangeOp->DBInsertNoReload();
  187. }
  188. protected function RecordObjDeletion($objkey)
  189. {
  190. $sRootClass = MetaModel::GetRootClass(get_class($this));
  191. // Delete any existing change tracking about the current object
  192. $oFilter = new DBObjectSearch('CMDBChangeOp');
  193. $oFilter->AddCondition('objclass', get_class($this), '=');
  194. $oFilter->AddCondition('objkey', $objkey, '=');
  195. MetaModel::PurgeData($oFilter);
  196. parent::RecordObjDeletion($objkey);
  197. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpDelete");
  198. $oMyChangeOp->Set("objclass", MetaModel::GetRootClass(get_class($this)));
  199. $oMyChangeOp->Set("objkey", $objkey);
  200. $oMyChangeOp->Set("fclass", get_class($this));
  201. $oMyChangeOp->Set("fname", substr($this->GetRawName(), 0, 255)); // Protect against very long friendly names
  202. $iId = $oMyChangeOp->DBInsertNoReload();
  203. }
  204. protected function RecordAttChanges(array $aValues, array $aOrigValues)
  205. {
  206. parent::RecordAttChanges($aValues, $aOrigValues);
  207. // $aValues is an array of $sAttCode => $value
  208. //
  209. foreach ($aValues as $sAttCode=> $value)
  210. {
  211. $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  212. if ($oAttDef->IsExternalField()) continue;
  213. if ($oAttDef->IsLinkSet()) continue;
  214. if ($oAttDef->GetTrackingLevel() == ATTRIBUTE_TRACKING_NONE) continue;
  215. if (array_key_exists($sAttCode, $aOrigValues))
  216. {
  217. $original = $aOrigValues[$sAttCode];
  218. }
  219. else
  220. {
  221. $original = null;
  222. }
  223. if ($oAttDef instanceOf AttributeOneWayPassword)
  224. {
  225. // One Way encrypted passwords' history is stored -one way- encrypted
  226. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeOneWayPassword");
  227. $oMyChangeOp->Set("objclass", get_class($this));
  228. $oMyChangeOp->Set("objkey", $this->GetKey());
  229. $oMyChangeOp->Set("attcode", $sAttCode);
  230. if (is_null($original))
  231. {
  232. $original = '';
  233. }
  234. $oMyChangeOp->Set("prev_pwd", $original);
  235. $iId = $oMyChangeOp->DBInsertNoReload();
  236. }
  237. elseif ($oAttDef instanceOf AttributeEncryptedString)
  238. {
  239. // Encrypted string history is stored encrypted
  240. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeEncrypted");
  241. $oMyChangeOp->Set("objclass", get_class($this));
  242. $oMyChangeOp->Set("objkey", $this->GetKey());
  243. $oMyChangeOp->Set("attcode", $sAttCode);
  244. if (is_null($original))
  245. {
  246. $original = '';
  247. }
  248. $oMyChangeOp->Set("prevstring", $original);
  249. $iId = $oMyChangeOp->DBInsertNoReload();
  250. }
  251. elseif ($oAttDef instanceOf AttributeBlob)
  252. {
  253. // Data blobs
  254. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeBlob");
  255. $oMyChangeOp->Set("objclass", get_class($this));
  256. $oMyChangeOp->Set("objkey", $this->GetKey());
  257. $oMyChangeOp->Set("attcode", $sAttCode);
  258. if (is_null($original))
  259. {
  260. $original = new ormDocument();
  261. }
  262. $oMyChangeOp->Set("prevdata", $original);
  263. $iId = $oMyChangeOp->DBInsertNoReload();
  264. }
  265. elseif ($oAttDef instanceOf AttributeStopWatch)
  266. {
  267. // Stop watches - record changes for sub items only (they are visible, the rest is not visible)
  268. //
  269. foreach ($oAttDef->ListSubItems() as $sSubItemAttCode => $oSubItemAttDef)
  270. {
  271. $item_value = $oSubItemAttDef->GetValue($this);
  272. $item_original = $oSubItemAttDef->GetValue($this, true);
  273. if ($item_value != $item_original)
  274. {
  275. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar");
  276. $oMyChangeOp->Set("objclass", get_class($this));
  277. $oMyChangeOp->Set("objkey", $this->GetKey());
  278. $oMyChangeOp->Set("attcode", $sSubItemAttCode);
  279. $oMyChangeOp->Set("oldvalue", $item_original);
  280. $oMyChangeOp->Set("newvalue", $item_value);
  281. $iId = $oMyChangeOp->DBInsertNoReload();
  282. }
  283. }
  284. }
  285. elseif ($oAttDef instanceOf AttributeCaseLog)
  286. {
  287. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeCaseLog");
  288. $oMyChangeOp->Set("objclass", get_class($this));
  289. $oMyChangeOp->Set("objkey", $this->GetKey());
  290. $oMyChangeOp->Set("attcode", $sAttCode);
  291. $oMyChangeOp->Set("lastentry", $value->GetLatestEntryIndex());
  292. $iId = $oMyChangeOp->DBInsertNoReload();
  293. }
  294. elseif ($oAttDef instanceOf AttributeLongText)
  295. {
  296. // Data blobs
  297. if ($oAttDef->GetFormat() == 'html')
  298. {
  299. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeHTML");
  300. }
  301. else
  302. {
  303. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeLongText");
  304. }
  305. $oMyChangeOp->Set("objclass", get_class($this));
  306. $oMyChangeOp->Set("objkey", $this->GetKey());
  307. $oMyChangeOp->Set("attcode", $sAttCode);
  308. if (!is_null($original) && ($original instanceof ormCaseLog))
  309. {
  310. $original = $original->GetText();
  311. }
  312. $oMyChangeOp->Set("prevdata", $original);
  313. $iId = $oMyChangeOp->DBInsertNoReload();
  314. }
  315. elseif ($oAttDef instanceOf AttributeText)
  316. {
  317. // Data blobs
  318. if ($oAttDef->GetFormat() == 'html')
  319. {
  320. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeHTML");
  321. }
  322. else
  323. {
  324. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeText");
  325. }
  326. $oMyChangeOp->Set("objclass", get_class($this));
  327. $oMyChangeOp->Set("objkey", $this->GetKey());
  328. $oMyChangeOp->Set("attcode", $sAttCode);
  329. if (!is_null($original) && ($original instanceof ormCaseLog))
  330. {
  331. $original = $original->GetText();
  332. }
  333. $oMyChangeOp->Set("prevdata", $original);
  334. $iId = $oMyChangeOp->DBInsertNoReload();
  335. }
  336. elseif ($oAttDef instanceOf AttributeBoolean)
  337. {
  338. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar");
  339. $oMyChangeOp->Set("objclass", get_class($this));
  340. $oMyChangeOp->Set("objkey", $this->GetKey());
  341. $oMyChangeOp->Set("attcode", $sAttCode);
  342. $oMyChangeOp->Set("oldvalue", $original ? 1 : 0);
  343. $oMyChangeOp->Set("newvalue", $value ? 1 : 0);
  344. $iId = $oMyChangeOp->DBInsertNoReload();
  345. }
  346. elseif ($oAttDef instanceOf AttributeHierarchicalKey)
  347. {
  348. // Hierarchical keys
  349. //
  350. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar");
  351. $oMyChangeOp->Set("objclass", get_class($this));
  352. $oMyChangeOp->Set("objkey", $this->GetKey());
  353. $oMyChangeOp->Set("attcode", $sAttCode);
  354. $oMyChangeOp->Set("oldvalue", $original);
  355. $oMyChangeOp->Set("newvalue", $value[$sAttCode]);
  356. $iId = $oMyChangeOp->DBInsertNoReload();
  357. }
  358. elseif ($oAttDef instanceOf AttributeCustomFields)
  359. {
  360. // Custom fields
  361. //
  362. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeCustomFields");
  363. $oMyChangeOp->Set("objclass", get_class($this));
  364. $oMyChangeOp->Set("objkey", $this->GetKey());
  365. $oMyChangeOp->Set("attcode", $sAttCode);
  366. $oMyChangeOp->Set("prevdata", json_encode($original->GetValues()));
  367. $iId = $oMyChangeOp->DBInsertNoReload();
  368. }
  369. else
  370. {
  371. // Scalars
  372. //
  373. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar");
  374. $oMyChangeOp->Set("objclass", get_class($this));
  375. $oMyChangeOp->Set("objkey", $this->GetKey());
  376. $oMyChangeOp->Set("attcode", $sAttCode);
  377. $oMyChangeOp->Set("oldvalue", $original);
  378. $oMyChangeOp->Set("newvalue", $value);
  379. $iId = $oMyChangeOp->DBInsertNoReload();
  380. }
  381. }
  382. }
  383. /**
  384. * Helper to ultimately check user rights before writing (Insert, Update or Delete)
  385. * The check should never fail, because the UI should prevent from such a usage
  386. * Anyhow, if the user has found a workaround... the security gets enforced here
  387. */
  388. protected function CheckUserRights($bSkipStrongSecurity, $iActionCode)
  389. {
  390. if (is_null($bSkipStrongSecurity))
  391. {
  392. // This is temporary
  393. // We have implemented this safety net right before releasing iTop 1.0
  394. // and we decided that it was too risky to activate it
  395. // Anyhow, users willing to have a very strong security could set
  396. // skip_strong_security = 0, in the config file
  397. $bSkipStrongSecurity = MetaModel::GetConfig()->Get('skip_strong_security');
  398. }
  399. if (!$bSkipStrongSecurity)
  400. {
  401. $sClass = get_class($this);
  402. $oSet = DBObjectSet::FromObject($this);
  403. if (!UserRights::IsActionAllowed($sClass, $iActionCode, $oSet))
  404. {
  405. // Intrusion detected
  406. throw new SecurityException('You are not allowed to modify objects of class: '.$sClass);
  407. }
  408. }
  409. }
  410. public function DBInsertTracked(CMDBChange $oChange, $bSkipStrongSecurity = null)
  411. {
  412. self::SetCurrentChange($oChange);
  413. $this->CheckUserRights($bSkipStrongSecurity, UR_ACTION_MODIFY);
  414. $ret = $this->DBInsertTracked_Internal();
  415. return $ret;
  416. }
  417. public function DBInsertTrackedNoReload(CMDBChange $oChange, $bSkipStrongSecurity = null)
  418. {
  419. self::SetCurrentChange($oChange);
  420. $this->CheckUserRights($bSkipStrongSecurity, UR_ACTION_MODIFY);
  421. $ret = $this->DBInsertTracked_Internal(true);
  422. return $ret;
  423. }
  424. /**
  425. * To Be Obsoleted: DO NOT rely on an overload of this method since
  426. * DBInsertTracked (resp. DBInsertTrackedNoReload) may call directly
  427. * DBInsert (resp. DBInsertNoReload) in future versions of iTop.
  428. * @param bool $bDoNotReload
  429. * @return integer Identifier of the created object
  430. */
  431. protected function DBInsertTracked_Internal($bDoNotReload = false)
  432. {
  433. if ($bDoNotReload)
  434. {
  435. $ret = $this->DBInsertNoReload();
  436. }
  437. else
  438. {
  439. $ret = $this->DBInsert();
  440. }
  441. return $ret;
  442. }
  443. public function DBClone($newKey = null)
  444. {
  445. return $this->DBCloneTracked_Internal();
  446. }
  447. public function DBCloneTracked(CMDBChange $oChange, $newKey = null)
  448. {
  449. self::SetCurrentChange($oChange);
  450. $this->DBCloneTracked_Internal($newKey);
  451. }
  452. protected function DBCloneTracked_Internal($newKey = null)
  453. {
  454. $newKey = parent::DBClone($newKey);
  455. $oClone = MetaModel::GetObject(get_class($this), $newKey);
  456. return $newKey;
  457. }
  458. public function DBUpdate()
  459. {
  460. // Copy the changes list before the update (the list should be reset afterwards)
  461. $aChanges = $this->ListChanges();
  462. if (count($aChanges) == 0)
  463. {
  464. return;
  465. }
  466. $ret = parent::DBUpdate();
  467. return $ret;
  468. }
  469. public function DBUpdateTracked(CMDBChange $oChange, $bSkipStrongSecurity = null)
  470. {
  471. self::SetCurrentChange($oChange);
  472. $this->CheckUserRights($bSkipStrongSecurity, UR_ACTION_MODIFY);
  473. $this->DBUpdate();
  474. }
  475. public function DBDelete(&$oDeletionPlan = null)
  476. {
  477. return $this->DBDeleteTracked_Internal($oDeletionPlan);
  478. }
  479. public function DBDeleteTracked(CMDBChange $oChange, $bSkipStrongSecurity = null, &$oDeletionPlan = null)
  480. {
  481. self::SetCurrentChange($oChange);
  482. $this->CheckUserRights($bSkipStrongSecurity, UR_ACTION_DELETE);
  483. $this->DBDeleteTracked_Internal($oDeletionPlan);
  484. }
  485. protected function DBDeleteTracked_Internal(&$oDeletionPlan = null)
  486. {
  487. $prevkey = $this->GetKey();
  488. $ret = parent::DBDelete($oDeletionPlan);
  489. return $ret;
  490. }
  491. public static function BulkUpdate(DBSearch $oFilter, array $aValues)
  492. {
  493. return static::BulkUpdateTracked_Internal($oFilter, $aValues);
  494. }
  495. public static function BulkUpdateTracked(CMDBChange $oChange, DBSearch $oFilter, array $aValues)
  496. {
  497. self::SetCurrentChange($oChange);
  498. static::BulkUpdateTracked_Internal($oFilter, $aValues);
  499. }
  500. protected static function BulkUpdateTracked_Internal(DBSearch $oFilter, array $aValues)
  501. {
  502. // $aValues is an array of $sAttCode => $value
  503. // Get the list of objects to update (and load it before doing the change)
  504. $oObjSet = new CMDBObjectSet($oFilter);
  505. $oObjSet->Load();
  506. // Keep track of the previous values (will be overwritten when the objects are synchronized with the DB)
  507. $aOriginalValues = array();
  508. $oObjSet->Rewind();
  509. while ($oItem = $oObjSet->Fetch())
  510. {
  511. $aOriginalValues[$oItem->GetKey()] = $oItem->m_aOrigValues;
  512. }
  513. // Update in one single efficient query
  514. $ret = parent::BulkUpdate($oFilter, $aValues);
  515. // Record... in many queries !!!
  516. $oObjSet->Rewind();
  517. while ($oItem = $oObjSet->Fetch())
  518. {
  519. $aChangedValues = $oItem->ListChangedValues($aValues);
  520. $oItem->RecordAttChanges($aChangedValues, $aOriginalValues[$oItem->GetKey()]);
  521. }
  522. return $ret;
  523. }
  524. }
  525. /**
  526. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  527. *
  528. * @package iTopORM
  529. */
  530. class CMDBObjectSet extends DBObjectSet
  531. {
  532. // this is the public interface (?)
  533. // I have to define those constructors here... :-(
  534. // just to get the right object class in return.
  535. // I have to think again to those things: maybe it will work fine if a have a constructor define here (?)
  536. static public function FromScratch($sClass)
  537. {
  538. $oFilter = new DBObjectSearch($sClass);
  539. $oFilter->AddConditionExpression(new FalseExpression());
  540. $oRetSet = new self($oFilter);
  541. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private in the base class (and you will not get any error message)
  542. $oRetSet->m_bLoaded = true; // no DB load
  543. return $oRetSet;
  544. }
  545. // create an object set ex nihilo
  546. // input = array of objects
  547. static public function FromArray($sClass, $aObjects)
  548. {
  549. $oRetSet = self::FromScratch($sClass);
  550. $oRetSet->AddObjectArray($aObjects, $sClass);
  551. return $oRetSet;
  552. }
  553. static public function FromArrayAssoc($aClasses, $aObjects)
  554. {
  555. // In a perfect world, we should create a complete tree of DBObjectSearch,
  556. // but as we lack most of the information related to the objects,
  557. // let's create one search definition
  558. $sClass = reset($aClasses);
  559. $sAlias = key($aClasses);
  560. $oFilter = new DBObjectSearch($sClass, $sAlias);
  561. $oRetSet = new CMDBObjectSet($oFilter);
  562. $oRetSet->m_bLoaded = true; // no DB load
  563. foreach($aObjects as $rowIndex => $aObjectsByClassAlias)
  564. {
  565. $oRetSet->AddObjectExtended($aObjectsByClassAlias);
  566. }
  567. return $oRetSet;
  568. }
  569. }