Variable.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Variable\Model\ResourceModel;
  7. /**
  8. * Custom variable resource model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Variable extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  13. {
  14. /**
  15. * Constructor
  16. *
  17. * @return void
  18. */
  19. protected function _construct()
  20. {
  21. $this->_init('variable', 'variable_id');
  22. }
  23. /**
  24. * Load variable by code
  25. *
  26. * @param \Magento\Variable\Model\Variable $object
  27. * @param string $code
  28. * @return $this
  29. */
  30. public function loadByCode(\Magento\Variable\Model\Variable $object, $code)
  31. {
  32. if ($result = $this->getVariableByCode($code, true, $object->getStoreId())) {
  33. $object->setData($result);
  34. }
  35. return $this;
  36. }
  37. /**
  38. * Retrieve variable data by code
  39. *
  40. * @param string $code
  41. * @param bool $withValue
  42. * @param integer $storeId
  43. * @return array
  44. */
  45. public function getVariableByCode($code, $withValue = false, $storeId = 0)
  46. {
  47. $select = $this->getConnection()->select()->from(
  48. $this->getMainTable()
  49. )->where(
  50. $this->getMainTable() . '.code = ?',
  51. $code
  52. );
  53. if ($withValue) {
  54. $this->_addValueToSelect($select, $storeId);
  55. }
  56. return $this->getConnection()->fetchRow($select);
  57. }
  58. /**
  59. * Perform actions after object save
  60. *
  61. * @param \Magento\Framework\Model\AbstractModel $object
  62. * @return $this
  63. */
  64. protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
  65. {
  66. parent::_afterSave($object);
  67. if ($object->getUseDefaultValue()) {
  68. /*
  69. * remove store value
  70. */
  71. $this->getConnection()->delete(
  72. $this->getTable('variable_value'),
  73. ['variable_id = ?' => $object->getId(), 'store_id = ?' => $object->getStoreId()]
  74. );
  75. } else {
  76. $data = [
  77. 'variable_id' => $object->getId(),
  78. 'store_id' => $object->getStoreId(),
  79. 'plain_value' => $object->getPlainValue(),
  80. 'html_value' => $object->getHtmlValue(),
  81. ];
  82. $data = $this->_prepareDataForTable(
  83. new \Magento\Framework\DataObject($data),
  84. $this->getTable('variable_value')
  85. );
  86. $this->getConnection()->insertOnDuplicate(
  87. $this->getTable('variable_value'),
  88. $data,
  89. ['plain_value', 'html_value']
  90. );
  91. }
  92. return $this;
  93. }
  94. /**
  95. * Retrieve select object for load object data
  96. *
  97. * @param string $field
  98. * @param mixed $value
  99. * @param \Magento\Framework\Model\AbstractModel $object
  100. * @return $this
  101. */
  102. protected function _getLoadSelect($field, $value, $object)
  103. {
  104. $select = parent::_getLoadSelect($field, $value, $object);
  105. $this->_addValueToSelect($select, $object->getStoreId());
  106. return $select;
  107. }
  108. /**
  109. * Add variable store and default value to select
  110. *
  111. * @param \Magento\Framework\DB\Select $select
  112. * @param integer $storeId
  113. * @return \Magento\Variable\Model\ResourceModel\Variable
  114. */
  115. protected function _addValueToSelect(
  116. \Magento\Framework\DB\Select $select,
  117. $storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID
  118. ) {
  119. $connection = $this->getConnection();
  120. $ifNullPlainValue = $connection->getCheckSql(
  121. 'store.plain_value IS NULL',
  122. 'def.plain_value',
  123. 'store.plain_value'
  124. );
  125. $ifNullHtmlValue = $connection->getCheckSql('store.html_value IS NULL', 'def.html_value', 'store.html_value');
  126. $select->joinLeft(
  127. ['def' => $this->getTable('variable_value')],
  128. 'def.variable_id = ' . $this->getMainTable() . '.variable_id AND def.store_id = 0',
  129. []
  130. )->joinLeft(
  131. ['store' => $this->getTable('variable_value')],
  132. 'store.variable_id = def.variable_id AND store.store_id = ' . $connection->quote($storeId),
  133. []
  134. )->columns(
  135. [
  136. 'plain_value' => $ifNullPlainValue,
  137. 'html_value' => $ifNullHtmlValue,
  138. 'store_plain_value' => 'store.plain_value',
  139. 'store_html_value' => 'store.html_value',
  140. ]
  141. );
  142. return $this;
  143. }
  144. }