Variable.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Variable\Model;
  7. /**
  8. * Custom variable model
  9. *
  10. * @method string getCode()
  11. * @method \Magento\Variable\Model\Variable setCode(string $value)
  12. * @method string getName()
  13. * @method \Magento\Variable\Model\Variable setName(string $value)
  14. *
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Variable extends \Magento\Framework\Model\AbstractModel
  19. {
  20. const TYPE_TEXT = 'text';
  21. const TYPE_HTML = 'html';
  22. /**
  23. * @var int
  24. */
  25. protected $_storeId = 0;
  26. /**
  27. * @var \Magento\Framework\Escaper
  28. */
  29. protected $_escaper = null;
  30. /**
  31. * @param \Magento\Framework\Model\Context $context
  32. * @param \Magento\Framework\Registry $registry
  33. * @param \Magento\Framework\Escaper $escaper
  34. * @param \Magento\Variable\Model\ResourceModel\Variable $resource
  35. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  36. * @param array $data
  37. */
  38. public function __construct(
  39. \Magento\Framework\Model\Context $context,
  40. \Magento\Framework\Registry $registry,
  41. \Magento\Framework\Escaper $escaper,
  42. \Magento\Variable\Model\ResourceModel\Variable $resource,
  43. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  44. array $data = []
  45. ) {
  46. $this->_escaper = $escaper;
  47. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  48. }
  49. /**
  50. * Internal Constructor
  51. *
  52. * @return void
  53. */
  54. protected function _construct()
  55. {
  56. parent::_construct();
  57. $this->_init(\Magento\Variable\Model\ResourceModel\Variable::class);
  58. }
  59. /**
  60. * Setter
  61. *
  62. * @param integer $storeId
  63. * @return $this
  64. * @codeCoverageIgnore
  65. */
  66. public function setStoreId($storeId)
  67. {
  68. $this->_storeId = $storeId;
  69. return $this;
  70. }
  71. /**
  72. * Getter
  73. *
  74. * @return integer
  75. * @codeCoverageIgnore
  76. */
  77. public function getStoreId()
  78. {
  79. return $this->_storeId;
  80. }
  81. /**
  82. * Load variable by code
  83. *
  84. * @param string $code
  85. * @return $this
  86. * @codeCoverageIgnore
  87. */
  88. public function loadByCode($code)
  89. {
  90. $this->getResource()->loadByCode($this, $code);
  91. return $this;
  92. }
  93. /**
  94. * Return variable value depend on given type
  95. *
  96. * @param string $type
  97. * @return string
  98. */
  99. public function getValue($type = null)
  100. {
  101. if ($type === null) {
  102. $type = self::TYPE_HTML;
  103. }
  104. if ($type == self::TYPE_TEXT || !strlen((string)$this->getData('html_value'))) {
  105. $value = $this->getData('plain_value');
  106. //escape html if type is html, but html value is not defined
  107. if ($type == self::TYPE_HTML) {
  108. $value = nl2br($this->_escaper->escapeHtml($value));
  109. }
  110. return $value;
  111. }
  112. return $this->getData('html_value');
  113. }
  114. /**
  115. * Validation of object data. Checking for unique variable code
  116. *
  117. * @return \Magento\Framework\Phrase|bool
  118. */
  119. public function validate()
  120. {
  121. if ($this->getCode() && $this->getName()) {
  122. $variable = $this->getResource()->getVariableByCode($this->getCode());
  123. if (!empty($variable) && $variable['variable_id'] != $this->getId()) {
  124. return __('Variable Code must be unique.');
  125. }
  126. return true;
  127. }
  128. return __('Validation has failed.');
  129. }
  130. /**
  131. * Retrieve variables option array
  132. * @todo: extract method as separate class
  133. * @param bool $withGroup
  134. * @return array
  135. */
  136. public function getVariablesOptionArray($withGroup = false)
  137. {
  138. /* @var $collection \Magento\Variable\Model\ResourceModel\Variable\Collection */
  139. $collection = $this->getCollection();
  140. $variables = [];
  141. foreach ($collection->toOptionArray() as $variable) {
  142. $variables[] = [
  143. 'value' => '{{customVar code=' . $variable['value'] . '}}',
  144. 'label' => __('%1', $this->_escaper->escapeHtml($variable['label'])),
  145. ];
  146. }
  147. if ($withGroup && $variables) {
  148. $variables = [['label' => __('Custom Variables'), 'value' => $variables]];
  149. }
  150. return $variables;
  151. }
  152. }