cmdbobject.class.inc.php 15 KB

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