cmdbchangeop.class.inc.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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
  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_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())));
  165. 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())));
  166. MetaModel::Init_InheritFilters();
  167. MetaModel::Init_AddFilterFromAttribute("attcode");
  168. MetaModel::Init_AddFilterFromAttribute("oldvalue");
  169. MetaModel::Init_AddFilterFromAttribute("newvalue");
  170. // Display lists
  171. MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'attcode', 'oldvalue', 'newvalue')); // Attributes to be displayed for the complete details
  172. MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'attcode', 'oldvalue', 'newvalue')); // Attributes to be displayed for a list
  173. }
  174. /**
  175. * Describe (as a text string) the modifications corresponding to this change
  176. */
  177. public function GetDescription()
  178. {
  179. // Temporary, until we change the options of GetDescription() -needs a more global revision
  180. $bIsHtml = true;
  181. $sResult = '';
  182. $oTargetObjectClass = $this->Get('objclass');
  183. $oTargetObjectKey = $this->Get('objkey');
  184. $oTargetSearch = new DBObjectSearch($oTargetObjectClass);
  185. $oTargetSearch->AddCondition('id', $oTargetObjectKey);
  186. $oMonoObjectSet = new DBObjectSet($oTargetSearch);
  187. if (UserRights::IsActionAllowedOnAttribute($this->Get('objclass'), $this->Get('attcode'), UR_ACTION_READ, $oMonoObjectSet) == UR_ALLOWED_YES)
  188. {
  189. $oAttDef = MetaModel::GetAttributeDef($this->Get('objclass'), $this->Get('attcode'));
  190. $sAttName = $oAttDef->GetLabel();
  191. $sNewValue = $this->Get('newvalue');
  192. $sOldValue = $this->Get('oldvalue');
  193. if ( (($oAttDef->GetType() == 'String') || ($oAttDef->GetType() == 'Text')) &&
  194. (strlen($sNewValue) > strlen($sOldValue)) )
  195. {
  196. // Check if some text was not appended to the field
  197. if (substr($sNewValue,0, strlen($sOldValue)) == $sOldValue) // Text added at the end
  198. {
  199. $sDelta = substr($sNewValue, strlen($sOldValue));
  200. $sResult = "$sDelta appended to $sAttName";
  201. }
  202. else if (substr($sNewValue, -strlen($sOldValue)) == $sOldValue) // Text added at the beginning
  203. {
  204. $sDelta = substr($sNewValue, 0, strlen($sNewValue) - strlen($sOldValue));
  205. $sResult = "$sDelta appended to $sAttName";
  206. }
  207. else
  208. {
  209. $sResult = "$sAttName set to $sNewValue (previous value: $sOldValue)";
  210. }
  211. }
  212. elseif($bIsHtml && $oAttDef->IsExternalKey())
  213. {
  214. $sTargetClass = $oAttDef->GetTargetClass();
  215. $sFrom = MetaModel::GetHyperLink($sTargetClass, $sOldValue);
  216. $sTo = MetaModel::GetHyperLink($sTargetClass, $sNewValue);
  217. $sResult = "$sAttName set to $sTo (previous: $sFrom)";
  218. }
  219. else
  220. {
  221. $sResult = "$sAttName set too $sNewValue (previous value: $sOldValue)";
  222. }
  223. }
  224. return $sResult;
  225. }
  226. }
  227. ?>