ormcustomfieldsvalue.class.inc.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. // Copyright (C) 2016 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. * Base class to hold the value managed by CustomFieldsHandler
  20. *
  21. * @copyright Copyright (C) 2016 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class ormCustomFieldsValue
  25. {
  26. protected $oHostObject;
  27. protected $sAttCode;
  28. protected $aCurrentValues;
  29. /**
  30. * @param DBObject $oHostObject
  31. * @param $sAttCode
  32. */
  33. public function __construct(DBObject $oHostObject, $sAttCode, $aCurrentValues = null)
  34. {
  35. $this->oHostObject = $oHostObject;
  36. $this->sAttCode = $sAttCode;
  37. $this->aCurrentValues = $aCurrentValues;
  38. }
  39. public function GetValues()
  40. {
  41. return $this->aCurrentValues;
  42. }
  43. /**
  44. * Wrapper used when the only thing you have is the value...
  45. * @return \Combodo\iTop\Form\Form
  46. */
  47. public function GetForm()
  48. {
  49. $oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
  50. return $oAttDef->GetForm($this->oHostObject);
  51. }
  52. public function GetAsHTML($bLocalize = true)
  53. {
  54. $oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
  55. $oHandler = $oAttDef->GetHandler($this->GetValues());
  56. return $oHandler->GetAsHTML($this->aCurrentValues, $bLocalize);
  57. }
  58. public function GetAsXML($bLocalize = true)
  59. {
  60. $oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
  61. $oHandler = $oAttDef->GetHandler($this->GetValues());
  62. return $oHandler->GetAsXML($this->aCurrentValues, $bLocalize);
  63. }
  64. public function GetAsCSV($sSeparator = ',', $sTextQualifier = '"', $bLocalize = true)
  65. {
  66. $oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
  67. $oHandler = $oAttDef->GetHandler($this->GetValues());
  68. return $oHandler->GetAsCSV($this->aCurrentValues, $sSeparator, $sTextQualifier, $bLocalize);
  69. }
  70. /**
  71. * Get various representations of the value, for insertion into a template (e.g. in Notifications)
  72. * @param $value mixed The current value of the field
  73. * @param $sVerb string The verb specifying the representation of the value
  74. * @param $bLocalize bool Whether or not to localize the value
  75. */
  76. public function GetForTemplate($sVerb, $bLocalize = true)
  77. {
  78. $oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
  79. $oHandler = $oAttDef->GetHandler($this->GetValues());
  80. return 'template...verb='.$sVerb.' sur "'.json_encode($this->aCurrentValues).'"';
  81. }
  82. /**
  83. * @param ormCustomFieldsValue $fellow
  84. * @return bool
  85. */
  86. public function Equals(ormCustomFieldsValue $oReference)
  87. {
  88. $oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
  89. $oHandler = $oAttDef->GetHandler($this->GetValues());
  90. return $oHandler->CompareValues($this->aCurrentValues, $oReference->aCurrentValues);
  91. }
  92. }