dbproperty.class.inc.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Database properties - manage database instances in a complex installation
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. /**
  25. * A database property
  26. *
  27. * @package iTopORM
  28. */
  29. class DBProperty extends DBObject
  30. {
  31. public static function Init()
  32. {
  33. $aParams = array
  34. (
  35. "category" => "cloud",
  36. "key_type" => "autoincrement",
  37. "name_attcode" => "name",
  38. "state_attcode" => "",
  39. "reconc_keys" => array(),
  40. "db_table" => "priv_db_properties",
  41. "db_key_field" => "id",
  42. "db_finalclass_field" => "",
  43. );
  44. MetaModel::Init_Params($aParams);
  45. //MetaModel::Init_InheritAttributes();
  46. MetaModel::Init_AddAttribute(new AttributeString("name", array("allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  47. MetaModel::Init_AddAttribute(new AttributeString("description", array("allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  48. MetaModel::Init_AddAttribute(new AttributeString("value", array("allowed_values"=>null, "sql"=>"value", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  49. MetaModel::Init_AddAttribute(new AttributeDateTime("change_date", array("allowed_values"=>null, "sql"=>"change_date", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  50. MetaModel::Init_AddAttribute(new AttributeString("change_comment", array("allowed_values"=>null, "sql"=>"change_comment", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  51. }
  52. /**
  53. * Helper to check wether the table has been created into the DB
  54. * (this table did not exist in 1.0.1 and older versions)
  55. */
  56. public static function IsInstalled()
  57. {
  58. $sTable = MetaModel::DBGetTable(__CLASS__);
  59. if (CMDBSource::IsTable($sTable))
  60. {
  61. return true;
  62. }
  63. else
  64. {
  65. return false;
  66. }
  67. return false;
  68. }
  69. public static function SetProperty($sName, $sValue, $sComment = '', $sDescription = null)
  70. {
  71. try
  72. {
  73. $oSearch = DBObjectSearch::FromOQL('SELECT DBProperty WHERE name = :name');
  74. $oSet = new DBObjectSet($oSearch, array(), array('name' => $sName));
  75. if ($oSet->Count() == 0)
  76. {
  77. $oProp = new DBProperty();
  78. $oProp->Set('name', $sName);
  79. $oProp->Set('description', $sDescription);
  80. $oProp->Set('value', $sValue);
  81. $oProp->Set('change_date', time());
  82. $oProp->Set('change_comment', $sComment);
  83. $oProp->DBInsert();
  84. }
  85. elseif ($oSet->Count() == 1)
  86. {
  87. $oProp = $oSet->fetch();
  88. if (!is_null($sDescription))
  89. {
  90. $oProp->Set('description', $sDescription);
  91. }
  92. $oProp->Set('value', $sValue);
  93. $oProp->Set('change_date', time());
  94. $oProp->Set('change_comment', $sComment);
  95. $oProp->DBUpdate();
  96. }
  97. else
  98. {
  99. // Houston...
  100. throw new CoreException('duplicate db property');
  101. }
  102. }
  103. catch (MySQLException $e)
  104. {
  105. // This might be because the table could not be found,
  106. // let's check it and discard silently if this is really the case
  107. if (self::IsInstalled())
  108. {
  109. throw $e;
  110. }
  111. IssueLog::Error('Attempting to write a DBProperty while the module has not been installed');
  112. }
  113. }
  114. public static function GetProperty($sName, $default = null)
  115. {
  116. try
  117. {
  118. $oSearch = DBObjectSearch::FromOQL('SELECT DBProperty WHERE name = :name');
  119. $oSet = new DBObjectSet($oSearch, array(), array('name' => $sName));
  120. $iCount = $oSet->Count();
  121. if ($iCount == 0)
  122. {
  123. //throw new CoreException('unknown db property', array('name' => $sName));
  124. $sValue = $default;
  125. }
  126. elseif ($iCount == 1)
  127. {
  128. $oProp = $oSet->fetch();
  129. $sValue = $oProp->Get('value');
  130. }
  131. else
  132. {
  133. // $iCount > 1
  134. // Houston...
  135. throw new CoreException('duplicate db property', array('name' => $sName, 'count' => $iCount));
  136. }
  137. }
  138. catch (MySQLException $e)
  139. {
  140. // This might be because the table could not be found,
  141. // let's check it and discard silently if this is really the case
  142. if (self::IsInstalled())
  143. {
  144. throw $e;
  145. }
  146. $sValue = $default;
  147. }
  148. return $sValue;
  149. }
  150. }
  151. ?>