redundancysettings.class.inc.php 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // Copyright (C) 2015 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. * Persistent classes (internal): user settings for the redundancy
  20. *
  21. * @copyright Copyright (C) 2015 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. /**
  25. * Redundancy settings
  26. *
  27. * @package iTopORM
  28. */
  29. class RedundancySettings extends DBObject
  30. {
  31. public static function Init()
  32. {
  33. $aParams = array
  34. (
  35. "category" => "core/cmdb",
  36. "key_type" => "autoincrement",
  37. "name_attcode" => array('relation_code','from_class','neighbour','objkey'),
  38. "state_attcode" => "",
  39. "reconc_keys" => array(),
  40. "db_table" => "priv_redundancy_settings",
  41. "db_key_field" => "id",
  42. "db_finalclass_field" => "finalclass",
  43. "display_template" => "",
  44. 'indexes' => array(
  45. array('relation_code', 'from_class', 'neighbour', 'objclass', 'objkey'),
  46. )
  47. );
  48. MetaModel::Init_Params($aParams);
  49. MetaModel::Init_AddAttribute(new AttributeString("relation_code", array("allowed_values"=>null, "sql"=>"relation_code", "default_value"=>'', "is_null_allowed"=>false, "depends_on"=>array())));
  50. MetaModel::Init_AddAttribute(new AttributeString("from_class", array("allowed_values"=>null, "sql"=>"from_class", "default_value"=>'', "is_null_allowed"=>false, "depends_on"=>array())));
  51. MetaModel::Init_AddAttribute(new AttributeString("neighbour", array("allowed_values"=>null, "sql"=>"neighbour", "default_value"=>'', "is_null_allowed"=>false, "depends_on"=>array())));
  52. MetaModel::Init_AddAttribute(new AttributeString("objclass", array("allowed_values"=>null, "sql"=>"objclass", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  53. MetaModel::Init_AddAttribute(new AttributeObjectKey("objkey", array("allowed_values"=>null, "class_attcode"=>"objclass", "sql"=>"objkey", "is_null_allowed"=>false, "depends_on"=>array())));
  54. MetaModel::Init_AddAttribute(new AttributeBoolean("enabled", array("allowed_values"=>null, "sql"=>"enabled", "default_value"=>false, "is_null_allowed"=>false, "depends_on"=>array())));
  55. MetaModel::Init_AddAttribute(new AttributeEnum("min_up_type", array("allowed_values"=>new ValueSetEnum('count,percent'), "sql"=>"min_up_type", "default_value"=>"count", "is_null_allowed"=>false, "depends_on"=>array())));
  56. MetaModel::Init_AddAttribute(new AttributeInteger("min_up_count", array("allowed_values"=>null, "sql"=>"min_up_count", "default_value"=>1, "is_null_allowed"=>true, "depends_on"=>array())));
  57. MetaModel::Init_AddAttribute(new AttributeInteger("min_up_percent", array("allowed_values"=>null, "sql"=>"min_up_percent", "default_value"=>50, "is_null_allowed"=>true, "depends_on"=>array())));
  58. }
  59. public static function MakeDefault($sRelCode, $aQueryInfo, $oToObject)
  60. {
  61. $oRet = MetaModel::NewObject('RedundancySettings');
  62. $oRet->Set('relation_code', $sRelCode);
  63. $oRet->Set('from_class', $aQueryInfo['sFromClass']);
  64. $oRet->Set('neighbour', $aQueryInfo['sNeighbour']);
  65. $oRet->Set('objclass', get_class($oToObject));
  66. $oRet->Set('objkey', $oToObject->GetKey());
  67. $oRet->Set('enabled', $aQueryInfo['bRedundancyEnabledValue']);
  68. $oRet->Set('min_up_type', $aQueryInfo['sRedundancyMinUpType']);
  69. $oRet->Set('min_up_count', ($aQueryInfo['sRedundancyMinUpType'] == 'count') ? $aQueryInfo['iRedundancyMinUpValue'] : 1);
  70. $oRet->Set('min_up_percent', ($aQueryInfo['sRedundancyMinUpType'] == 'percent') ? $aQueryInfo['iRedundancyMinUpValue'] : 50);
  71. return $oRet;
  72. }
  73. public static function GetSettings($sRelCode, $aQueryInfo, $oToObject)
  74. {
  75. $oSearch = new DBObjectSearch('RedundancySettings');
  76. $oSearch->AddCondition('relation_code', $sRelCode, '=');
  77. $oSearch->AddCondition('from_class', $aQueryInfo['sFromClass'], '=');
  78. $oSearch->AddCondition('neighbour', $aQueryInfo['sNeighbour'], '=');
  79. $oSearch->AddCondition('objclass', get_class($oToObject), '=');
  80. $oSearch->AddCondition('objkey', $oToObject->GetKey(), '=');
  81. $oSet = new DBObjectSet($oSearch);
  82. $oRet = $oSet->Fetch();
  83. if (!$oRet)
  84. {
  85. $oRet = self::MakeDefault($sRelCode, $aQueryInfo, $oToObject);
  86. }
  87. return $oRet;
  88. }
  89. }