cmdbchangeop.class.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /**
  3. * Various atomic change operations, to be tracked
  4. *
  5. * @package iTopORM
  6. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  7. * @author Denis Flaven <denisflave@free.fr>
  8. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  9. * @link www.itop.com
  10. * @since 1.0
  11. * @version 1.1.1.1 $
  12. */
  13. class CMDBChangeOp extends DBObject
  14. {
  15. public static function Init()
  16. {
  17. $aParams = array
  18. (
  19. "category" => "core/cmdb",
  20. "name" => "change operation",
  21. "description" => "Change operations tracking",
  22. "key_type" => "autoincrement",
  23. "key_label" => "",
  24. "name_attcode" => "change",
  25. "state_attcode" => "",
  26. "reconc_keys" => array(),
  27. "db_table" => "priv_changeop",
  28. "db_key_field" => "id",
  29. "db_finalclass_field" => "optype",
  30. );
  31. MetaModel::Init_Params($aParams);
  32. //MetaModel::Init_InheritAttributes();
  33. MetaModel::Init_AddAttribute(new AttributeExternalKey("change", array("label"=>"change", "description"=>"change", "allowed_values"=>null, "sql"=>"changeid", "targetclass"=>"CMDBChange", "is_null_allowed"=>false, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));
  34. MetaModel::Init_AddAttribute(new AttributeExternalField("date", array("label"=>"date", "description"=>"date and time of the change", "allowed_values"=>null, "extkey_attcode"=>"change", "target_attcode"=>"date")));
  35. MetaModel::Init_AddAttribute(new AttributeExternalField("userinfo", array("label"=>"user", "description"=>"who made this change", "allowed_values"=>null, "extkey_attcode"=>"change", "target_attcode"=>"userinfo")));
  36. MetaModel::Init_AddAttribute(new AttributeString("objclass", array("label"=>"object class", "description"=>"object class", "allowed_values"=>null, "sql"=>"objclass", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  37. MetaModel::Init_AddAttribute(new AttributeString("objkey", array("label"=>"object id", "description"=>"object id", "allowed_values"=>null, "sql"=>"objkey", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  38. MetaModel::Init_AddFilterFromAttribute("objclass");
  39. MetaModel::Init_AddFilterFromAttribute("objkey");
  40. MetaModel::Init_AddFilterFromAttribute("date");
  41. MetaModel::Init_AddFilterFromAttribute("userinfo");
  42. MetaModel::Init_SetZListItems('details', array('change', 'date', 'userinfo')); // Attributes to be displayed for the complete details
  43. MetaModel::Init_SetZListItems('list', array('change', 'date', 'userinfo')); // Attributes to be displayed for the complete details
  44. }
  45. /**
  46. * Describe (as a text string) the modifications corresponding to this change
  47. */
  48. public function GetDescription()
  49. {
  50. return '';
  51. }
  52. }
  53. /**
  54. * Record the creation of an object
  55. *
  56. * @package iTopORM
  57. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  58. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  59. * @link www.itop.com
  60. * @since 1.0
  61. * @version $itopversion$
  62. */
  63. class CMDBChangeOpCreate extends CMDBChangeOp
  64. {
  65. public static function Init()
  66. {
  67. $aParams = array
  68. (
  69. "category" => "core/cmdb",
  70. "name" => "object creation",
  71. "description" => "Object creation tracking",
  72. "key_type" => "",
  73. "key_label" => "",
  74. "name_attcode" => "change",
  75. "state_attcode" => "",
  76. "reconc_keys" => array(),
  77. "db_table" => "priv_changeop_create",
  78. "db_key_field" => "id",
  79. "db_finalclass_field" => "",
  80. );
  81. MetaModel::Init_Params($aParams);
  82. MetaModel::Init_InheritAttributes();
  83. MetaModel::Init_InheritFilters();
  84. }
  85. /**
  86. * Describe (as a text string) the modifications corresponding to this change
  87. */
  88. public function GetDescription()
  89. {
  90. return 'Object created';
  91. }
  92. }
  93. /**
  94. * Record the deletion of an object
  95. *
  96. * @package iTopORM
  97. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  98. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  99. * @link www.itop.com
  100. * @since 1.0
  101. * @version $itopversion$
  102. */
  103. class CMDBChangeOpDelete extends CMDBChangeOp
  104. {
  105. public static function Init()
  106. {
  107. $aParams = array
  108. (
  109. "category" => "core/cmdb",
  110. "name" => "object deletion",
  111. "description" => "Object deletion tracking",
  112. "key_type" => "",
  113. "key_label" => "",
  114. "name_attcode" => "change",
  115. "state_attcode" => "",
  116. "reconc_keys" => array(),
  117. "db_table" => "priv_changeop_delete",
  118. "db_key_field" => "id",
  119. "db_finalclass_field" => "",
  120. );
  121. MetaModel::Init_Params($aParams);
  122. MetaModel::Init_InheritAttributes();
  123. MetaModel::Init_InheritFilters();
  124. }
  125. /**
  126. * Describe (as a text string) the modifications corresponding to this change
  127. */
  128. public function GetDescription()
  129. {
  130. return 'Object deleted';
  131. }
  132. }
  133. /**
  134. * Record the modification of an attribute (abstract)
  135. *
  136. * @package iTopORM
  137. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  138. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  139. * @link www.itop.com
  140. * @since 1.0
  141. * @version $itopversion$
  142. */
  143. class CMDBChangeOpSetAttribute extends CMDBChangeOp
  144. {
  145. public static function Init()
  146. {
  147. $aParams = array
  148. (
  149. "category" => "core/cmdb",
  150. "name" => "object change",
  151. "description" => "Object properties change tracking",
  152. "key_type" => "",
  153. "key_label" => "",
  154. "name_attcode" => "change",
  155. "state_attcode" => "",
  156. "reconc_keys" => array(),
  157. "db_table" => "priv_changeop_setatt",
  158. "db_key_field" => "id",
  159. "db_finalclass_field" => "",
  160. );
  161. MetaModel::Init_Params($aParams);
  162. MetaModel::Init_InheritAttributes();
  163. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("label"=>"Attribute", "description"=>"code of the modified property", "allowed_values"=>null, "sql"=>"attcode", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  164. MetaModel::Init_InheritFilters();
  165. MetaModel::Init_AddFilterFromAttribute("attcode");
  166. // Display lists
  167. MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for the complete details
  168. MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for a list
  169. }
  170. }
  171. /**
  172. * Record the modification of a scalar attribute
  173. *
  174. * @package iTopORM
  175. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  176. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  177. * @link www.itop.com
  178. * @since 1.0
  179. * @version $itopversion$
  180. */
  181. class CMDBChangeOpSetAttributeScalar extends CMDBChangeOpSetAttribute
  182. {
  183. public static function Init()
  184. {
  185. $aParams = array
  186. (
  187. "category" => "core/cmdb",
  188. "name" => "property change",
  189. "description" => "Object scalar properties change tracking",
  190. "key_type" => "",
  191. "key_label" => "",
  192. "name_attcode" => "change",
  193. "state_attcode" => "",
  194. "reconc_keys" => array(),
  195. "db_table" => "priv_changeop_setatt_scalar",
  196. "db_key_field" => "id",
  197. "db_finalclass_field" => "",
  198. );
  199. MetaModel::Init_Params($aParams);
  200. MetaModel::Init_InheritAttributes();
  201. MetaModel::Init_AddAttribute(new AttributeString("oldvalue", array("label"=>"Previous value", "description"=>"previous value of the attribute", "allowed_values"=>null, "sql"=>"oldvalue", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  202. MetaModel::Init_AddAttribute(new AttributeString("newvalue", array("label"=>"New value", "description"=>"new value of the attribute", "allowed_values"=>null, "sql"=>"newvalue", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  203. MetaModel::Init_InheritFilters();
  204. MetaModel::Init_AddFilterFromAttribute("oldvalue");
  205. MetaModel::Init_AddFilterFromAttribute("newvalue");
  206. // Display lists
  207. MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'attcode', 'oldvalue', 'newvalue')); // Attributes to be displayed for the complete details
  208. MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'attcode', 'oldvalue', 'newvalue')); // Attributes to be displayed for a list
  209. }
  210. /**
  211. * Describe (as a text string) the modifications corresponding to this change
  212. */
  213. public function GetDescription()
  214. {
  215. // Temporary, until we change the options of GetDescription() -needs a more global revision
  216. $bIsHtml = true;
  217. $sResult = '';
  218. $oTargetObjectClass = $this->Get('objclass');
  219. $oTargetObjectKey = $this->Get('objkey');
  220. $oTargetSearch = new DBObjectSearch($oTargetObjectClass);
  221. $oTargetSearch->AddCondition('id', $oTargetObjectKey);
  222. $oMonoObjectSet = new DBObjectSet($oTargetSearch);
  223. if (UserRights::IsActionAllowedOnAttribute($this->Get('objclass'), $this->Get('attcode'), UR_ACTION_READ, $oMonoObjectSet) == UR_ALLOWED_YES)
  224. {
  225. $oAttDef = MetaModel::GetAttributeDef($this->Get('objclass'), $this->Get('attcode'));
  226. $sAttName = $oAttDef->GetLabel();
  227. $sNewValue = $this->Get('newvalue');
  228. $sOldValue = $this->Get('oldvalue');
  229. if ( (($oAttDef->GetType() == 'String') || ($oAttDef->GetType() == 'Text')) &&
  230. (strlen($sNewValue) > strlen($sOldValue)) )
  231. {
  232. // Check if some text was not appended to the field
  233. if (substr($sNewValue,0, strlen($sOldValue)) == $sOldValue) // Text added at the end
  234. {
  235. $sDelta = substr($sNewValue, strlen($sOldValue));
  236. $sResult = "$sDelta appended to $sAttName";
  237. }
  238. else if (substr($sNewValue, -strlen($sOldValue)) == $sOldValue) // Text added at the beginning
  239. {
  240. $sDelta = substr($sNewValue, 0, strlen($sNewValue) - strlen($sOldValue));
  241. $sResult = "$sDelta appended to $sAttName";
  242. }
  243. else
  244. {
  245. $sResult = "$sAttName set to $sNewValue (previous value: $sOldValue)";
  246. }
  247. }
  248. elseif($bIsHtml && $oAttDef->IsExternalKey())
  249. {
  250. $sTargetClass = $oAttDef->GetTargetClass();
  251. $sFrom = MetaModel::GetHyperLink($sTargetClass, $sOldValue);
  252. $sTo = MetaModel::GetHyperLink($sTargetClass, $sNewValue);
  253. $sResult = "$sAttName set to $sTo (previous: $sFrom)";
  254. }
  255. elseif ($oAttDef instanceOf AttributeBlob)
  256. {
  257. $sResult = "#@# Issue... found an attribute for which other type of tracking should be made";
  258. }
  259. else
  260. {
  261. $sResult = "$sAttName set to $sNewValue (previous value: $sOldValue)";
  262. }
  263. }
  264. return $sResult;
  265. }
  266. }
  267. /**
  268. * Record the modification of a blob
  269. *
  270. * @package iTopORM
  271. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  272. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  273. * @link www.itop.com
  274. * @since 1.0
  275. * @version $itopversion$
  276. */
  277. class CMDBChangeOpSetAttributeBlob extends CMDBChangeOpSetAttribute
  278. {
  279. public static function Init()
  280. {
  281. $aParams = array
  282. (
  283. "category" => "core/cmdb",
  284. "name" => "object data change",
  285. "description" => "Object data change tracking",
  286. "key_type" => "",
  287. "key_label" => "",
  288. "name_attcode" => "change",
  289. "state_attcode" => "",
  290. "reconc_keys" => array(),
  291. "db_table" => "priv_changeop_setatt_data",
  292. "db_key_field" => "id",
  293. "db_finalclass_field" => "",
  294. );
  295. MetaModel::Init_Params($aParams);
  296. MetaModel::Init_InheritAttributes();
  297. MetaModel::Init_AddAttribute(new AttributeBlob("prevdata", array("label"=>"Previous data", "description"=>"previous contents of the attribute", "depends_on"=>array())));
  298. MetaModel::Init_InheritFilters();
  299. // Display lists
  300. MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for the complete details
  301. MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for a list
  302. }
  303. /**
  304. * Describe (as a text string) the modifications corresponding to this change
  305. */
  306. public function GetDescription()
  307. {
  308. // Temporary, until we change the options of GetDescription() -needs a more global revision
  309. $bIsHtml = true;
  310. $sResult = '';
  311. $oTargetObjectClass = $this->Get('objclass');
  312. $oTargetObjectKey = $this->Get('objkey');
  313. $oTargetSearch = new DBObjectSearch($oTargetObjectClass);
  314. $oTargetSearch->AddCondition('id', $oTargetObjectKey);
  315. $oMonoObjectSet = new DBObjectSet($oTargetSearch);
  316. if (UserRights::IsActionAllowedOnAttribute($this->Get('objclass'), $this->Get('attcode'), UR_ACTION_READ, $oMonoObjectSet) == UR_ALLOWED_YES)
  317. {
  318. $oAttDef = MetaModel::GetAttributeDef($this->Get('objclass'), $this->Get('attcode'));
  319. $sAttName = $oAttDef->GetLabel();
  320. $oPrevDoc = $this->Get('prevdata');
  321. $sDocView = $oPrevDoc->GetAsHtml();
  322. $sDocView .= "<br/>Open in New Window: ".$oPrevDoc->GetDisplayLink(get_class($this), $this->GetKey(), 'prevdata').", \n";
  323. $sDocView .= "Download: ".$oPrevDoc->GetDownloadLink(get_class($this), $this->GetKey(), 'prevdata')."\n";
  324. //$sDocView = $oPrevDoc->GetDisplayInline(get_class($this), $this->GetKey(), 'prevdata');
  325. $sResult = "$sAttName changed, previous value: $sDocView";
  326. }
  327. return $sResult;
  328. }
  329. }
  330. ?>