cmdbobject.class.inc.php 13 KB

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