cmdbchangeop.class.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. "name_attcode" => "change",
  38. "state_attcode" => "",
  39. "reconc_keys" => array(),
  40. "db_table" => "priv_changeop",
  41. "db_key_field" => "id",
  42. "db_finalclass_field" => "optype",
  43. );
  44. MetaModel::Init_Params($aParams);
  45. //MetaModel::Init_InheritAttributes();
  46. 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())));
  47. MetaModel::Init_AddAttribute(new AttributeExternalField("date", array("allowed_values"=>null, "extkey_attcode"=>"change", "target_attcode"=>"date")));
  48. MetaModel::Init_AddAttribute(new AttributeExternalField("userinfo", array("allowed_values"=>null, "extkey_attcode"=>"change", "target_attcode"=>"userinfo")));
  49. MetaModel::Init_AddAttribute(new AttributeString("objclass", array("allowed_values"=>null, "sql"=>"objclass", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  50. MetaModel::Init_AddAttribute(new AttributeString("objkey", array("allowed_values"=>null, "sql"=>"objkey", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  51. MetaModel::Init_SetZListItems('details', array('change', 'date', 'userinfo')); // Attributes to be displayed for the complete details
  52. MetaModel::Init_SetZListItems('list', array('change', 'date', 'userinfo')); // Attributes to be displayed for the complete details
  53. }
  54. static public function IsReadOnly()
  55. {
  56. return true;
  57. }
  58. /**
  59. * Describe (as a text string) the modifications corresponding to this change
  60. */
  61. public function GetDescription()
  62. {
  63. return '';
  64. }
  65. }
  66. /**
  67. * Record the creation of an object
  68. *
  69. * @package iTopORM
  70. */
  71. class CMDBChangeOpCreate extends CMDBChangeOp
  72. {
  73. public static function Init()
  74. {
  75. $aParams = array
  76. (
  77. "category" => "core/cmdb",
  78. "key_type" => "",
  79. "name_attcode" => "change",
  80. "state_attcode" => "",
  81. "reconc_keys" => array(),
  82. "db_table" => "priv_changeop_create",
  83. "db_key_field" => "id",
  84. "db_finalclass_field" => "",
  85. );
  86. MetaModel::Init_Params($aParams);
  87. MetaModel::Init_InheritAttributes();
  88. }
  89. /**
  90. * Describe (as a text string) the modifications corresponding to this change
  91. */
  92. public function GetDescription()
  93. {
  94. return 'Object created';
  95. }
  96. }
  97. /**
  98. * Record the deletion of an object
  99. *
  100. * @package iTopORM
  101. */
  102. class CMDBChangeOpDelete extends CMDBChangeOp
  103. {
  104. public static function Init()
  105. {
  106. $aParams = array
  107. (
  108. "category" => "core/cmdb",
  109. "key_type" => "",
  110. "name_attcode" => "change",
  111. "state_attcode" => "",
  112. "reconc_keys" => array(),
  113. "db_table" => "priv_changeop_delete",
  114. "db_key_field" => "id",
  115. "db_finalclass_field" => "",
  116. );
  117. MetaModel::Init_Params($aParams);
  118. MetaModel::Init_InheritAttributes();
  119. }
  120. /**
  121. * Describe (as a text string) the modifications corresponding to this change
  122. */
  123. public function GetDescription()
  124. {
  125. return 'Object deleted';
  126. }
  127. }
  128. /**
  129. * Record the modification of an attribute (abstract)
  130. *
  131. * @package iTopORM
  132. */
  133. class CMDBChangeOpSetAttribute extends CMDBChangeOp
  134. {
  135. public static function Init()
  136. {
  137. $aParams = array
  138. (
  139. "category" => "core/cmdb",
  140. "key_type" => "",
  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. "name_attcode" => "change",
  170. "state_attcode" => "",
  171. "reconc_keys" => array(),
  172. "db_table" => "priv_changeop_setatt_scalar",
  173. "db_key_field" => "id",
  174. "db_finalclass_field" => "",
  175. );
  176. MetaModel::Init_Params($aParams);
  177. MetaModel::Init_InheritAttributes();
  178. MetaModel::Init_AddAttribute(new AttributeString("oldvalue", array("allowed_values"=>null, "sql"=>"oldvalue", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  179. MetaModel::Init_AddAttribute(new AttributeString("newvalue", array("allowed_values"=>null, "sql"=>"newvalue", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  180. // Display lists
  181. MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'attcode', 'oldvalue', 'newvalue')); // Attributes to be displayed for the complete details
  182. MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'attcode', 'oldvalue', 'newvalue')); // Attributes to be displayed for a list
  183. }
  184. /**
  185. * Describe (as a text string) the modifications corresponding to this change
  186. */
  187. public function GetDescription()
  188. {
  189. // Temporary, until we change the options of GetDescription() -needs a more global revision
  190. $bIsHtml = true;
  191. $sResult = '';
  192. $oTargetObjectClass = $this->Get('objclass');
  193. $oTargetObjectKey = $this->Get('objkey');
  194. $oTargetSearch = new DBObjectSearch($oTargetObjectClass);
  195. $oTargetSearch->AddCondition('id', $oTargetObjectKey);
  196. $oMonoObjectSet = new DBObjectSet($oTargetSearch);
  197. if (UserRights::IsActionAllowedOnAttribute($this->Get('objclass'), $this->Get('attcode'), UR_ACTION_READ, $oMonoObjectSet) == UR_ALLOWED_YES)
  198. {
  199. $oAttDef = MetaModel::GetAttributeDef($this->Get('objclass'), $this->Get('attcode'));
  200. $sAttName = $oAttDef->GetLabel();
  201. $sNewValue = $this->Get('newvalue');
  202. $sOldValue = $this->Get('oldvalue');
  203. if ( (($oAttDef->GetType() == 'String') || ($oAttDef->GetType() == 'Text')) &&
  204. (strlen($sNewValue) > strlen($sOldValue)) )
  205. {
  206. // Check if some text was not appended to the field
  207. if (substr($sNewValue,0, strlen($sOldValue)) == $sOldValue) // Text added at the end
  208. {
  209. $sDelta = substr($sNewValue, strlen($sOldValue));
  210. $sResult = "$sDelta appended to $sAttName";
  211. }
  212. else if (substr($sNewValue, -strlen($sOldValue)) == $sOldValue) // Text added at the beginning
  213. {
  214. $sDelta = substr($sNewValue, 0, strlen($sNewValue) - strlen($sOldValue));
  215. $sResult = "$sDelta appended to $sAttName";
  216. }
  217. else
  218. {
  219. $sResult = "$sAttName set to $sNewValue (previous value: $sOldValue)";
  220. }
  221. }
  222. elseif($bIsHtml && $oAttDef->IsExternalKey())
  223. {
  224. $sTargetClass = $oAttDef->GetTargetClass();
  225. $sFrom = MetaModel::GetHyperLink($sTargetClass, $sOldValue);
  226. $sTo = MetaModel::GetHyperLink($sTargetClass, $sNewValue);
  227. $sResult = "$sAttName set to $sTo (previous: $sFrom)";
  228. }
  229. elseif ($oAttDef instanceOf AttributeBlob)
  230. {
  231. $sResult = "#@# Issue... found an attribute for which other type of tracking should be made";
  232. }
  233. else
  234. {
  235. $sResult = "$sAttName set to $sNewValue (previous value: $sOldValue)";
  236. }
  237. }
  238. return $sResult;
  239. }
  240. }
  241. /**
  242. * Record the modification of a blob
  243. *
  244. * @package iTopORM
  245. */
  246. class CMDBChangeOpSetAttributeBlob extends CMDBChangeOpSetAttribute
  247. {
  248. public static function Init()
  249. {
  250. $aParams = array
  251. (
  252. "category" => "core/cmdb",
  253. "key_type" => "",
  254. "name_attcode" => "change",
  255. "state_attcode" => "",
  256. "reconc_keys" => array(),
  257. "db_table" => "priv_changeop_setatt_data",
  258. "db_key_field" => "id",
  259. "db_finalclass_field" => "",
  260. );
  261. MetaModel::Init_Params($aParams);
  262. MetaModel::Init_InheritAttributes();
  263. MetaModel::Init_AddAttribute(new AttributeBlob("prevdata", array("depends_on"=>array())));
  264. // Display lists
  265. MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for the complete details
  266. MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for a list
  267. }
  268. /**
  269. * Describe (as a text string) the modifications corresponding to this change
  270. */
  271. public function GetDescription()
  272. {
  273. // Temporary, until we change the options of GetDescription() -needs a more global revision
  274. $bIsHtml = true;
  275. $sResult = '';
  276. $oTargetObjectClass = $this->Get('objclass');
  277. $oTargetObjectKey = $this->Get('objkey');
  278. $oTargetSearch = new DBObjectSearch($oTargetObjectClass);
  279. $oTargetSearch->AddCondition('id', $oTargetObjectKey);
  280. $oMonoObjectSet = new DBObjectSet($oTargetSearch);
  281. if (UserRights::IsActionAllowedOnAttribute($this->Get('objclass'), $this->Get('attcode'), UR_ACTION_READ, $oMonoObjectSet) == UR_ALLOWED_YES)
  282. {
  283. $oAttDef = MetaModel::GetAttributeDef($this->Get('objclass'), $this->Get('attcode'));
  284. $sAttName = $oAttDef->GetLabel();
  285. $oPrevDoc = $this->Get('prevdata');
  286. $sDocView = $oPrevDoc->GetAsHtml();
  287. $sDocView .= "<br/>Open in New Window: ".$oPrevDoc->GetDisplayLink(get_class($this), $this->GetKey(), 'prevdata').", \n";
  288. $sDocView .= "Download: ".$oPrevDoc->GetDownloadLink(get_class($this), $this->GetKey(), 'prevdata')."\n";
  289. //$sDocView = $oPrevDoc->GetDisplayInline(get_class($this), $this->GetKey(), 'prevdata');
  290. $sResult = "$sAttName changed, previous value: $sDocView";
  291. }
  292. return $sResult;
  293. }
  294. }
  295. /**
  296. * Record the modification of a multiline string (text)
  297. *
  298. * @package iTopORM
  299. */
  300. class CMDBChangeOpSetAttributeText extends CMDBChangeOpSetAttribute
  301. {
  302. public static function Init()
  303. {
  304. $aParams = array
  305. (
  306. "category" => "core/cmdb",
  307. "key_type" => "",
  308. "name_attcode" => "change",
  309. "state_attcode" => "",
  310. "reconc_keys" => array(),
  311. "db_table" => "priv_changeop_setatt_text",
  312. "db_key_field" => "id",
  313. "db_finalclass_field" => "",
  314. );
  315. MetaModel::Init_Params($aParams);
  316. MetaModel::Init_InheritAttributes();
  317. MetaModel::Init_AddAttribute(new AttributeText("prevdata", array("allowed_values"=>null, "sql"=>"prevdata", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  318. // Display lists
  319. MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for the complete details
  320. MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'attcode')); // Attributes to be displayed for a list
  321. }
  322. /**
  323. * Describe (as a text string) the modifications corresponding to this change
  324. */
  325. public function GetDescription()
  326. {
  327. // Temporary, until we change the options of GetDescription() -needs a more global revision
  328. $bIsHtml = true;
  329. $sResult = '';
  330. $oTargetObjectClass = $this->Get('objclass');
  331. $oTargetObjectKey = $this->Get('objkey');
  332. $oTargetSearch = new DBObjectSearch($oTargetObjectClass);
  333. $oTargetSearch->AddCondition('id', $oTargetObjectKey);
  334. $oMonoObjectSet = new DBObjectSet($oTargetSearch);
  335. if (UserRights::IsActionAllowedOnAttribute($this->Get('objclass'), $this->Get('attcode'), UR_ACTION_READ, $oMonoObjectSet) == UR_ALLOWED_YES)
  336. {
  337. $oAttDef = MetaModel::GetAttributeDef($this->Get('objclass'), $this->Get('attcode'));
  338. $sAttName = $oAttDef->GetLabel();
  339. $sTextView = '<div>'.$this->GetAsHtml('prevdata').'</div>';
  340. //$sDocView = $oPrevDoc->GetDisplayInline(get_class($this), $this->GetKey(), 'prevdata');
  341. $sResult = "$sAttName changed, previous value: $sTextView";
  342. }
  343. return $sResult;
  344. }
  345. }
  346. ?>