cmdbobject.class.inc.php 13 KB

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