cmdbobject.class.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. elseif ($oAttDef instanceOf AttributeText)
  173. {
  174. // Data blobs
  175. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeText");
  176. $oMyChangeOp->Set("change", $oChange->GetKey());
  177. $oMyChangeOp->Set("objclass", get_class($this));
  178. $oMyChangeOp->Set("objkey", $this->GetKey());
  179. $oMyChangeOp->Set("attcode", $sAttCode);
  180. if (array_key_exists($sAttCode, $aOrigValues))
  181. {
  182. $original = $aOrigValues[$sAttCode];
  183. }
  184. else
  185. {
  186. $original = null;
  187. }
  188. $oMyChangeOp->Set("prevdata", $original);
  189. $iId = $oMyChangeOp->DBInsertNoReload();
  190. }
  191. else
  192. {
  193. // Scalars
  194. //
  195. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar");
  196. $oMyChangeOp->Set("change", $oChange->GetKey());
  197. $oMyChangeOp->Set("objclass", get_class($this));
  198. $oMyChangeOp->Set("objkey", $this->GetKey());
  199. $oMyChangeOp->Set("attcode", $sAttCode);
  200. if (array_key_exists($sAttCode, $aOrigValues))
  201. {
  202. $sOriginalValue = $aOrigValues[$sAttCode];
  203. }
  204. else
  205. {
  206. $sOriginalValue = 'undefined';
  207. }
  208. $oMyChangeOp->Set("oldvalue", $sOriginalValue);
  209. $oMyChangeOp->Set("newvalue", $value);
  210. $iId = $oMyChangeOp->DBInsertNoReload();
  211. }
  212. }
  213. }
  214. public function DBInsert()
  215. {
  216. if(!is_object(self::$m_oCurrChange))
  217. {
  218. throw new CoreException("DBInsert() could not be used here, please use DBInsertTracked() instead");
  219. }
  220. return $this->DBInsertTracked_Internal();
  221. }
  222. public function DBInsertTracked(CMDBChange $oChange)
  223. {
  224. self::$m_oCurrChange = $oChange;
  225. $ret = $this->DBInsertTracked_Internal();
  226. self::$m_oCurrChange = null;
  227. return $ret;
  228. }
  229. public function DBInsertTrackedNoReload(CMDBChange $oChange)
  230. {
  231. self::$m_oCurrChange = $oChange;
  232. $ret = $this->DBInsertTracked_Internal(true);
  233. self::$m_oCurrChange = null;
  234. return $ret;
  235. }
  236. protected function DBInsertTracked_Internal($bDoNotReload = false)
  237. {
  238. if ($bDoNotReload)
  239. {
  240. $ret = parent::DBInsertNoReload();
  241. }
  242. else
  243. {
  244. $ret = parent::DBInsert();
  245. }
  246. $this->RecordObjCreation(self::$m_oCurrChange);
  247. return $ret;
  248. }
  249. public function DBClone($newKey = null)
  250. {
  251. if(!self::$m_oCurrChange)
  252. {
  253. throw new CoreException("DBClone() could not be used here, please use DBCloneTracked() instead");
  254. }
  255. return $this->DBCloneTracked_Internal();
  256. }
  257. public function DBCloneTracked(CMDBChange $oChange, $newKey = null)
  258. {
  259. self::$m_oCurrChange = $oChange;
  260. $this->DBCloneTracked_Internal($newKey);
  261. self::$m_oCurrChange = null;
  262. }
  263. protected function DBCloneTracked_Internal($newKey = null)
  264. {
  265. $newKey = parent::DBClone($newKey);
  266. $oClone = MetaModel::GetObject(get_class($this), $newKey);
  267. $oClone->RecordObjCreation(self::$m_oCurrChange);
  268. return $newKey;
  269. }
  270. public function DBUpdate()
  271. {
  272. if(!self::$m_oCurrChange)
  273. {
  274. throw new CoreException("DBUpdate() could not be used here, please use DBUpdateTracked() instead");
  275. }
  276. return $this->DBUpdateTracked_internal();
  277. }
  278. public function DBUpdateTracked(CMDBChange $oChange)
  279. {
  280. self::$m_oCurrChange = $oChange;
  281. $this->DBUpdateTracked_Internal();
  282. self::$m_oCurrChange = null;
  283. }
  284. protected function DBUpdateTracked_Internal()
  285. {
  286. // Copy the changes list before the update (the list should be reset afterwards)
  287. $aChanges = $this->ListChanges();
  288. if (count($aChanges) == 0)
  289. {
  290. throw new CoreWarning("Attempting to update an unchanged object");
  291. return;
  292. }
  293. // Save the original values (will be reset to the new values when the object get written to the DB)
  294. $aOriginalValues = $this->m_aOrigValues;
  295. $ret = parent::DBUpdate();
  296. $this->RecordAttChanges(self::$m_oCurrChange, $aChanges, $aOriginalValues);
  297. return $ret;
  298. }
  299. public function DBDelete()
  300. {
  301. if(!self::$m_oCurrChange)
  302. {
  303. throw new CoreException("DBDelete() could not be used here, please use DBDeleteTracked() instead");
  304. }
  305. return $this->DBDeleteTracked_Internal();
  306. }
  307. public function DBDeleteTracked(CMDBChange $oChange)
  308. {
  309. self::$m_oCurrChange = $oChange;
  310. $this->DBDeleteTracked_Internal();
  311. self::$m_oCurrChange = null;
  312. }
  313. protected function DBDeleteTracked_Internal()
  314. {
  315. $prevkey = $this->GetKey();
  316. $ret = parent::DBDelete();
  317. $this->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  318. return $ret;
  319. }
  320. public static function BulkDelete(DBObjectSearch $oFilter)
  321. {
  322. if(!self::$m_oCurrChange)
  323. {
  324. throw new CoreException("BulkDelete() could not be used here, please use BulkDeleteTracked() instead");
  325. }
  326. return $this->BulkDeleteTracked_Internal($oFilter);
  327. }
  328. public static function BulkDeleteTracked(CMDBChange $oChange, DBObjectSearch $oFilter)
  329. {
  330. self::$m_oCurrChange = $oChange;
  331. $this->BulkDeleteTracked_Internal($oFilter);
  332. self::$m_oCurrChange = null;
  333. }
  334. protected static function BulkDeleteTracked_Internal(DBObjectSearch $oFilter)
  335. {
  336. throw new CoreWarning("Change tracking not tested for bulk operations");
  337. // Get the list of objects to delete (and record data before deleting the DB records)
  338. $oObjSet = new CMDBObjectSet($oFilter);
  339. $aObjAndKeys = array(); // array of pkey=>object
  340. while ($oItem = $oObjSet->Fetch())
  341. {
  342. $aObjAndKeys[$oItem->GetKey()] = $oItem;
  343. }
  344. $oObjSet->FreeResult();
  345. // Delete in one single efficient query
  346. $ret = parent::BulkDelete($oFilter);
  347. // Record... in many queries !!!
  348. foreach($aObjAndKeys as $prevkey=>$oItem)
  349. {
  350. $oItem->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  351. }
  352. return $ret;
  353. }
  354. public static function BulkUpdate(DBObjectSearch $oFilter, array $aValues)
  355. {
  356. if(!self::$m_oCurrChange)
  357. {
  358. throw new CoreException("BulkUpdate() could not be used here, please use BulkUpdateTracked() instead");
  359. }
  360. return $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  361. }
  362. public static function BulkUpdateTracked(CMDBChange $oChange, DBObjectSearch $oFilter, array $aValues)
  363. {
  364. self::$m_oCurrChange = $oChange;
  365. $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  366. self::$m_oCurrChange = null;
  367. }
  368. protected static function BulkUpdateTracked_Internal(DBObjectSearch $oFilter, array $aValues)
  369. {
  370. // $aValues is an array of $sAttCode => $value
  371. // Get the list of objects to update (and load it before doing the change)
  372. $oObjSet = new CMDBObjectSet($oFilter);
  373. $oObjSet->Load();
  374. // Keep track of the previous values (will be overwritten when the objects are synchronized with the DB)
  375. $aOriginalValues = array();
  376. $oObjSet->Rewind();
  377. while ($oItem = $oObjSet->Fetch())
  378. {
  379. $aOriginalValues[$oItem->GetKey()] = $oItem->m_aOrigValues;
  380. }
  381. // Update in one single efficient query
  382. $ret = parent::BulkUpdate($oFilter, $aValues);
  383. // Record... in many queries !!!
  384. $oObjSet->Rewind();
  385. while ($oItem = $oObjSet->Fetch())
  386. {
  387. $aChangedValues = $oItem->ListChangedValues($aValues);
  388. $oItem->RecordAttChanges(self::$m_oCurrChange, $aChangedValues, $aOriginalValues[$oItem->GetKey()]);
  389. }
  390. return $ret;
  391. }
  392. }
  393. /**
  394. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  395. *
  396. * @package iTopORM
  397. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  398. * @author Denis Flaven <denisflave@free.fr>
  399. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  400. * @link www.itop.com
  401. * @since 1.0
  402. * @version 1.1.1.1 $
  403. */
  404. class CMDBObjectSet extends DBObjectSet
  405. {
  406. // this is the public interface (?)
  407. // I have to define those constructors here... :-(
  408. // just to get the right object class in return.
  409. // I have to think again to those things: maybe it will work fine if a have a constructor define here (?)
  410. static public function FromScratch($sClass)
  411. {
  412. $oFilter = new CMDBSearchFilter($sClass);
  413. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  414. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  415. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  416. $oRetSet->m_bLoaded = true; // no DB load
  417. return $oRetSet;
  418. }
  419. static public function FromArray($sClass, $aObjects)
  420. {
  421. $oFilter = new CMDBSearchFilter($sClass);
  422. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  423. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  424. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  425. $oRetSet->m_bLoaded = true; // no DB load
  426. $oRetSet->AddObjectArray($aObjects);
  427. return $oRetSet;
  428. }
  429. }
  430. /**
  431. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  432. *
  433. * @package iTopORM
  434. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  435. * @author Denis Flaven <denisflave@free.fr>
  436. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  437. * @link www.itop.com
  438. * @since 1.0
  439. * @version 1.1.1.1 $
  440. */
  441. class CMDBSearchFilter extends DBObjectSearch
  442. {
  443. // this is the public interface (?)
  444. }
  445. ?>