cmdbobject.class.inc.php 14 KB

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