cmdbchangeop.class.inc.php 13 KB

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