cmdbobject.class.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. /**
  3. * cmdbObjectClass
  4. * the file to include, then the core is yours
  5. *
  6. * @package iTopORM
  7. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  8. * @author Denis Flaven <denisflave@free.fr>
  9. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  10. * @link www.itop.com
  11. * @since 1.0
  12. * @version 1.1.1.1 $
  13. */
  14. require_once('coreexception.class.inc.php');
  15. require_once('config.class.inc.php');
  16. require_once('dict.class.inc.php');
  17. require_once('attributedef.class.inc.php');
  18. require_once('filterdef.class.inc.php');
  19. require_once('stimulus.class.inc.php');
  20. require_once('valuesetdef.class.inc.php');
  21. require_once('MyHelpers.class.inc.php');
  22. require_once('expression.class.inc.php');
  23. require_once('cmdbsource.class.inc.php');
  24. require_once('sqlquery.class.inc.php');
  25. require_once('oql/oqlquery.class.inc.php');
  26. require_once('oql/oqlexception.class.inc.php');
  27. require_once('oql/oql-parser.php');
  28. require_once('oql/oql-lexer.php');
  29. require_once('oql/oqlinterpreter.class.inc.php');
  30. require_once('dbobject.class.php');
  31. require_once('dbobjectsearch.class.php');
  32. require_once('dbobjectset.class.php');
  33. // db change tracking data model
  34. require_once('cmdbchange.class.inc.php');
  35. require_once('cmdbchangeop.class.inc.php');
  36. // customization data model
  37. // Romain: temporary moved into application.inc.php (see explanations there)
  38. //require_once('trigger.class.inc.php');
  39. //require_once('action.class.inc.php');
  40. // application log
  41. // Romain: temporary moved into application.inc.php (see explanations there)
  42. //require_once('event.class.inc.php');
  43. require_once('csvparser.class.inc.php');
  44. require_once('bulkchange.class.inc.php');
  45. require_once('userrights.class.inc.php');
  46. //
  47. // Error handling
  48. // To be finalized... or removed ?
  49. //
  50. function cmdbErrorHandler($errno, $errstr, $errfile, $errline)
  51. {
  52. // font-family: Courier-New, Courier, Arial, Helevtica;
  53. $sErrorStyle = "
  54. background-color: #ffaaaa;
  55. color: #000000;
  56. border: 1px dashed #000000;
  57. padding: 0.25em;
  58. margin-top: 1em;
  59. ";
  60. $sCallStackStyle = "
  61. font-size: smaller;
  62. background-color: #ffcccc;
  63. color: #000000;
  64. border: 1px dashed #000000;
  65. padding: 0.25em;
  66. margin-top: 1em;
  67. ";
  68. switch ($errno)
  69. {
  70. case E_USER_ERROR:
  71. case E_ERROR:
  72. echo "<div style=\"$sErrorStyle\">\n";
  73. echo "<b>Error</b> [$errno] $errstr<br />\n";
  74. echo "<div style=\"$sCallStackStyle\">\n";
  75. MyHelpers::dump_callstack(1);
  76. echo "</div>\n";
  77. echo "Hereafter the biz model internals:<br />\n";
  78. echo "<pre>\n";
  79. MetaModel::static_var_dump();
  80. echo "</pre>\n";
  81. echo "Aborting...<br />\n";
  82. echo "</div>\n";
  83. exit(1);
  84. break;
  85. case E_USER_WARNING:
  86. case E_WARNING:
  87. echo "<div style=\"background-color:#FAA;\">\n";
  88. echo "<b>Warning</b> [$errno] $errstr<br />\n";
  89. echo "<div style=\"background-color:#FCC;\">\n";
  90. MyHelpers::dump_callstack(1);
  91. echo "</div>\n";
  92. echo "</div>\n";
  93. break;
  94. case E_USER_NOTICE:
  95. case E_NOTICE:
  96. echo "<div style=\"background-color:#FAA;\">\n";
  97. echo "<b>Notice</b> [$errno] $errstr<br />\n";
  98. echo "<div style=\"background-color:#FCC;\">\n";
  99. MyHelpers::dump_callstack(1);
  100. echo "</div>\n";
  101. echo "</div>\n";
  102. break;
  103. default:
  104. echo "Unknown error type: [$errno] $errstr<br />\n";
  105. MyHelpers::dump_callstack(1);
  106. break;
  107. }
  108. }
  109. error_reporting(E_ALL | E_STRICT);
  110. //set_error_handler("cmdbErrorHandler");
  111. //
  112. //
  113. //
  114. /**
  115. * A persistent object, which changes are accurately recorded
  116. *
  117. * @package iTopORM
  118. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  119. * @author Denis Flaven <denisflave@free.fr>
  120. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  121. * @link www.itop.com
  122. * @since 1.0
  123. * @version 1.1.1.1 $
  124. */
  125. abstract class CMDBObject extends DBObject
  126. {
  127. protected $m_datCreated;
  128. protected $m_datUpdated;
  129. protected static $m_oCurrChange = null;
  130. private function RecordObjCreation(CMDBChange $oChange)
  131. {
  132. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpCreate");
  133. $oMyChangeOp->Set("change", $oChange->GetKey());
  134. $oMyChangeOp->Set("objclass", get_class($this));
  135. $oMyChangeOp->Set("objkey", $this->GetKey());
  136. $iId = $oMyChangeOp->DBInsertNoReload();
  137. }
  138. private function RecordObjDeletion(CMDBChange $oChange, $objkey)
  139. {
  140. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpDelete");
  141. $oMyChangeOp->Set("change", $oChange->GetKey());
  142. $oMyChangeOp->Set("objclass", get_class($this));
  143. $oMyChangeOp->Set("objkey", $objkey);
  144. $iId = $oMyChangeOp->DBInsertNoReload();
  145. }
  146. private function RecordAttChanges(CMDBChange $oChange, array $aValues, array $aOrigValues)
  147. {
  148. // $aValues is an array of $sAttCode => $value
  149. //
  150. foreach ($aValues as $sAttCode=> $value)
  151. {
  152. $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  153. if ($oAttDef->IsLinkSet()) continue; // #@# temporary
  154. if ($oAttDef instanceOf AttributeBlob)
  155. {
  156. // Data blobs
  157. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeBlob");
  158. $oMyChangeOp->Set("change", $oChange->GetKey());
  159. $oMyChangeOp->Set("objclass", get_class($this));
  160. $oMyChangeOp->Set("objkey", $this->GetKey());
  161. $oMyChangeOp->Set("attcode", $sAttCode);
  162. if (array_key_exists($sAttCode, $aOrigValues))
  163. {
  164. $original = $aOrigValues[$sAttCode];
  165. }
  166. else
  167. {
  168. $original = new ormDocument();
  169. }
  170. $oMyChangeOp->Set("prevdata", $original);
  171. $iId = $oMyChangeOp->DBInsertNoReload();
  172. }
  173. elseif ($oAttDef instanceOf AttributeText)
  174. {
  175. // Data blobs
  176. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeText");
  177. $oMyChangeOp->Set("change", $oChange->GetKey());
  178. $oMyChangeOp->Set("objclass", get_class($this));
  179. $oMyChangeOp->Set("objkey", $this->GetKey());
  180. $oMyChangeOp->Set("attcode", $sAttCode);
  181. if (array_key_exists($sAttCode, $aOrigValues))
  182. {
  183. $original = $aOrigValues[$sAttCode];
  184. }
  185. else
  186. {
  187. $original = null;
  188. }
  189. $oMyChangeOp->Set("prevdata", $original);
  190. $iId = $oMyChangeOp->DBInsertNoReload();
  191. }
  192. else
  193. {
  194. // Scalars
  195. //
  196. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar");
  197. $oMyChangeOp->Set("change", $oChange->GetKey());
  198. $oMyChangeOp->Set("objclass", get_class($this));
  199. $oMyChangeOp->Set("objkey", $this->GetKey());
  200. $oMyChangeOp->Set("attcode", $sAttCode);
  201. if (array_key_exists($sAttCode, $aOrigValues))
  202. {
  203. $sOriginalValue = $aOrigValues[$sAttCode];
  204. }
  205. else
  206. {
  207. $sOriginalValue = 'undefined';
  208. }
  209. $oMyChangeOp->Set("oldvalue", $sOriginalValue);
  210. $oMyChangeOp->Set("newvalue", $value);
  211. $iId = $oMyChangeOp->DBInsertNoReload();
  212. }
  213. }
  214. }
  215. public function DBInsert()
  216. {
  217. if(!is_object(self::$m_oCurrChange))
  218. {
  219. throw new CoreException("DBInsert() could not be used here, please use DBInsertTracked() instead");
  220. }
  221. return $this->DBInsertTracked_Internal();
  222. }
  223. public function DBInsertTracked(CMDBChange $oChange)
  224. {
  225. self::$m_oCurrChange = $oChange;
  226. $ret = $this->DBInsertTracked_Internal();
  227. self::$m_oCurrChange = null;
  228. return $ret;
  229. }
  230. public function DBInsertTrackedNoReload(CMDBChange $oChange)
  231. {
  232. self::$m_oCurrChange = $oChange;
  233. $ret = $this->DBInsertTracked_Internal(true);
  234. self::$m_oCurrChange = null;
  235. return $ret;
  236. }
  237. protected function DBInsertTracked_Internal($bDoNotReload = false)
  238. {
  239. if ($bDoNotReload)
  240. {
  241. $ret = parent::DBInsertNoReload();
  242. }
  243. else
  244. {
  245. $ret = parent::DBInsert();
  246. }
  247. $this->RecordObjCreation(self::$m_oCurrChange);
  248. return $ret;
  249. }
  250. public function DBClone($newKey = null)
  251. {
  252. if(!self::$m_oCurrChange)
  253. {
  254. throw new CoreException("DBClone() could not be used here, please use DBCloneTracked() instead");
  255. }
  256. return $this->DBCloneTracked_Internal();
  257. }
  258. public function DBCloneTracked(CMDBChange $oChange, $newKey = null)
  259. {
  260. self::$m_oCurrChange = $oChange;
  261. $this->DBCloneTracked_Internal($newKey);
  262. self::$m_oCurrChange = null;
  263. }
  264. protected function DBCloneTracked_Internal($newKey = null)
  265. {
  266. $newKey = parent::DBClone($newKey);
  267. $oClone = MetaModel::GetObject(get_class($this), $newKey);
  268. $oClone->RecordObjCreation(self::$m_oCurrChange);
  269. return $newKey;
  270. }
  271. public function DBUpdate()
  272. {
  273. if(!self::$m_oCurrChange)
  274. {
  275. throw new CoreException("DBUpdate() could not be used here, please use DBUpdateTracked() instead");
  276. }
  277. return $this->DBUpdateTracked_internal();
  278. }
  279. public function DBUpdateTracked(CMDBChange $oChange)
  280. {
  281. self::$m_oCurrChange = $oChange;
  282. $this->DBUpdateTracked_Internal();
  283. self::$m_oCurrChange = null;
  284. }
  285. protected function DBUpdateTracked_Internal()
  286. {
  287. // Copy the changes list before the update (the list should be reset afterwards)
  288. $aChanges = $this->ListChanges();
  289. if (count($aChanges) == 0)
  290. {
  291. throw new CoreWarning("Attempting to update an unchanged object");
  292. return;
  293. }
  294. // Save the original values (will be reset to the new values when the object get written to the DB)
  295. $aOriginalValues = $this->m_aOrigValues;
  296. $ret = parent::DBUpdate();
  297. $this->RecordAttChanges(self::$m_oCurrChange, $aChanges, $aOriginalValues);
  298. return $ret;
  299. }
  300. public function DBDelete()
  301. {
  302. if(!self::$m_oCurrChange)
  303. {
  304. throw new CoreException("DBDelete() could not be used here, please use DBDeleteTracked() instead");
  305. }
  306. return $this->DBDeleteTracked_Internal();
  307. }
  308. public function DBDeleteTracked(CMDBChange $oChange)
  309. {
  310. self::$m_oCurrChange = $oChange;
  311. $this->DBDeleteTracked_Internal();
  312. self::$m_oCurrChange = null;
  313. }
  314. protected function DBDeleteTracked_Internal()
  315. {
  316. $prevkey = $this->GetKey();
  317. $ret = parent::DBDelete();
  318. $this->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  319. return $ret;
  320. }
  321. public static function BulkDelete(DBObjectSearch $oFilter)
  322. {
  323. if(!self::$m_oCurrChange)
  324. {
  325. throw new CoreException("BulkDelete() could not be used here, please use BulkDeleteTracked() instead");
  326. }
  327. return $this->BulkDeleteTracked_Internal($oFilter);
  328. }
  329. public static function BulkDeleteTracked(CMDBChange $oChange, DBObjectSearch $oFilter)
  330. {
  331. self::$m_oCurrChange = $oChange;
  332. $this->BulkDeleteTracked_Internal($oFilter);
  333. self::$m_oCurrChange = null;
  334. }
  335. protected static function BulkDeleteTracked_Internal(DBObjectSearch $oFilter)
  336. {
  337. throw new CoreWarning("Change tracking not tested for bulk operations");
  338. // Get the list of objects to delete (and record data before deleting the DB records)
  339. $oObjSet = new CMDBObjectSet($oFilter);
  340. $aObjAndKeys = array(); // array of pkey=>object
  341. while ($oItem = $oObjSet->Fetch())
  342. {
  343. $aObjAndKeys[$oItem->GetKey()] = $oItem;
  344. }
  345. $oObjSet->FreeResult();
  346. // Delete in one single efficient query
  347. $ret = parent::BulkDelete($oFilter);
  348. // Record... in many queries !!!
  349. foreach($aObjAndKeys as $prevkey=>$oItem)
  350. {
  351. $oItem->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  352. }
  353. return $ret;
  354. }
  355. public static function BulkUpdate(DBObjectSearch $oFilter, array $aValues)
  356. {
  357. if(!self::$m_oCurrChange)
  358. {
  359. throw new CoreException("BulkUpdate() could not be used here, please use BulkUpdateTracked() instead");
  360. }
  361. return $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  362. }
  363. public static function BulkUpdateTracked(CMDBChange $oChange, DBObjectSearch $oFilter, array $aValues)
  364. {
  365. self::$m_oCurrChange = $oChange;
  366. $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  367. self::$m_oCurrChange = null;
  368. }
  369. protected static function BulkUpdateTracked_Internal(DBObjectSearch $oFilter, array $aValues)
  370. {
  371. // $aValues is an array of $sAttCode => $value
  372. // Get the list of objects to update (and load it before doing the change)
  373. $oObjSet = new CMDBObjectSet($oFilter);
  374. $oObjSet->Load();
  375. // Keep track of the previous values (will be overwritten when the objects are synchronized with the DB)
  376. $aOriginalValues = array();
  377. $oObjSet->Rewind();
  378. while ($oItem = $oObjSet->Fetch())
  379. {
  380. $aOriginalValues[$oItem->GetKey()] = $oItem->m_aOrigValues;
  381. }
  382. // Update in one single efficient query
  383. $ret = parent::BulkUpdate($oFilter, $aValues);
  384. // Record... in many queries !!!
  385. $oObjSet->Rewind();
  386. while ($oItem = $oObjSet->Fetch())
  387. {
  388. $aChangedValues = $oItem->ListChangedValues($aValues);
  389. $oItem->RecordAttChanges(self::$m_oCurrChange, $aChangedValues, $aOriginalValues[$oItem->GetKey()]);
  390. }
  391. return $ret;
  392. }
  393. }
  394. /**
  395. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  396. *
  397. * @package iTopORM
  398. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  399. * @author Denis Flaven <denisflave@free.fr>
  400. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  401. * @link www.itop.com
  402. * @since 1.0
  403. * @version 1.1.1.1 $
  404. */
  405. class CMDBObjectSet extends DBObjectSet
  406. {
  407. // this is the public interface (?)
  408. // I have to define those constructors here... :-(
  409. // just to get the right object class in return.
  410. // I have to think again to those things: maybe it will work fine if a have a constructor define here (?)
  411. static public function FromScratch($sClass)
  412. {
  413. $oFilter = new CMDBSearchFilter($sClass);
  414. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  415. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  416. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  417. $oRetSet->m_bLoaded = true; // no DB load
  418. return $oRetSet;
  419. }
  420. static public function FromArray($sClass, $aObjects)
  421. {
  422. $oFilter = new CMDBSearchFilter($sClass);
  423. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  424. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  425. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  426. $oRetSet->m_bLoaded = true; // no DB load
  427. $oRetSet->AddObjectArray($aObjects);
  428. return $oRetSet;
  429. }
  430. }
  431. /**
  432. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  433. *
  434. * @package iTopORM
  435. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  436. * @author Denis Flaven <denisflave@free.fr>
  437. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  438. * @link www.itop.com
  439. * @since 1.0
  440. * @version 1.1.1.1 $
  441. */
  442. class CMDBSearchFilter extends DBObjectSearch
  443. {
  444. // this is the public interface (?)
  445. }
  446. ?>