cmdbchangeop.class.inc.php 13 KB

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