AbstractResource.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Rule\Model\ResourceModel;
  7. /**
  8. * Abstract Rule entity resource model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. abstract class AbstractResource extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  14. {
  15. /**
  16. * Store associated with rule entities information map
  17. *
  18. * Example:
  19. * array(
  20. * 'entity_type1' => array(
  21. * 'associations_table' => 'table_name',
  22. * 'rule_id_field' => 'rule_id',
  23. * 'entity_id_field' => 'entity_id'
  24. * ),
  25. * 'entity_type2' => array(
  26. * 'associations_table' => 'table_name',
  27. * 'rule_id_field' => 'rule_id',
  28. * 'entity_id_field' => 'entity_id'
  29. * )
  30. * ....
  31. * )
  32. *
  33. * @var array
  34. */
  35. protected $_associatedEntitiesMap = [];
  36. /**
  37. * Prepare rule's active "from" and "to" dates
  38. *
  39. * @param \Magento\Framework\Model\AbstractModel $object
  40. * @return $this
  41. */
  42. public function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  43. {
  44. $this->resolveDate($object, 'from_date');
  45. $this->resolveDate($object, 'to_date');
  46. parent::_beforeSave($object);
  47. return $this;
  48. }
  49. /**
  50. * @param \Magento\Framework\Model\AbstractModel $object
  51. * @param string $dateIdentifier
  52. * @return void
  53. */
  54. private function resolveDate(\Magento\Framework\Model\AbstractModel $object, $dateIdentifier)
  55. {
  56. $date = $object->getData($dateIdentifier);
  57. if ($date instanceof \DateTimeInterface) {
  58. $object->setData($dateIdentifier, $date->format('Y-m-d H:i:s'));
  59. } elseif (!is_string($date) || empty($date)) {
  60. $object->setData($dateIdentifier, null);
  61. }
  62. }
  63. /**
  64. * Bind specified rules to entities
  65. *
  66. * @param int[]|int|string $ruleIds
  67. * @param int[]|int|string $entityIds
  68. * @param string $entityType
  69. * @return $this
  70. * @throws \Exception
  71. */
  72. public function bindRuleToEntity($ruleIds, $entityIds, $entityType)
  73. {
  74. $this->getConnection()->beginTransaction();
  75. try {
  76. $this->_multiplyBunchInsert($ruleIds, $entityIds, $entityType);
  77. } catch (\Exception $e) {
  78. $this->getConnection()->rollBack();
  79. throw $e;
  80. }
  81. $this->getConnection()->commit();
  82. return $this;
  83. }
  84. /**
  85. * Multiply rule ids by entity ids and insert
  86. *
  87. * @param int|[] $ruleIds
  88. * @param int|[] $entityIds
  89. * @param string $entityType
  90. * @return $this
  91. */
  92. protected function _multiplyBunchInsert($ruleIds, $entityIds, $entityType)
  93. {
  94. if (empty($ruleIds) || empty($entityIds)) {
  95. return $this;
  96. }
  97. if (!is_array($ruleIds)) {
  98. $ruleIds = [(int)$ruleIds];
  99. }
  100. if (!is_array($entityIds)) {
  101. $entityIds = [(int)$entityIds];
  102. }
  103. $data = [];
  104. $count = 0;
  105. $entityInfo = $this->_getAssociatedEntityInfo($entityType);
  106. foreach ($ruleIds as $ruleId) {
  107. foreach ($entityIds as $entityId) {
  108. $data[] = [
  109. $entityInfo['entity_id_field'] => $entityId,
  110. $entityInfo['rule_id_field'] => $ruleId,
  111. ];
  112. $count++;
  113. if ($count % 1000 == 0) {
  114. $this->getConnection()->insertOnDuplicate(
  115. $this->getTable($entityInfo['associations_table']),
  116. $data,
  117. [$entityInfo['rule_id_field']]
  118. );
  119. $data = [];
  120. }
  121. }
  122. }
  123. if (!empty($data)) {
  124. $this->getConnection()->insertOnDuplicate(
  125. $this->getTable($entityInfo['associations_table']),
  126. $data,
  127. [$entityInfo['rule_id_field']]
  128. );
  129. }
  130. $this->getConnection()->delete(
  131. $this->getTable($entityInfo['associations_table']),
  132. $this->getConnection()->quoteInto(
  133. $entityInfo['rule_id_field'] . ' IN (?) AND ',
  134. $ruleIds
  135. ) . $this->getConnection()->quoteInto(
  136. $entityInfo['entity_id_field'] . ' NOT IN (?)',
  137. $entityIds
  138. )
  139. );
  140. return $this;
  141. }
  142. /**
  143. * Unbind specified rules from entities
  144. *
  145. * @param int[]|int|string $ruleIds
  146. * @param int[]|int|string $entityIds
  147. * @param string $entityType
  148. * @return $this
  149. */
  150. public function unbindRuleFromEntity($ruleIds, $entityIds, $entityType)
  151. {
  152. $connection = $this->getConnection();
  153. $entityInfo = $this->_getAssociatedEntityInfo($entityType);
  154. if (!is_array($entityIds)) {
  155. $entityIds = [(int)$entityIds];
  156. }
  157. if (!is_array($ruleIds)) {
  158. $ruleIds = [(int)$ruleIds];
  159. }
  160. $where = [];
  161. if (!empty($ruleIds)) {
  162. $where[] = $connection->quoteInto($entityInfo['rule_id_field'] . ' IN (?)', $ruleIds);
  163. }
  164. if (!empty($entityIds)) {
  165. $where[] = $connection->quoteInto($entityInfo['entity_id_field'] . ' IN (?)', $entityIds);
  166. }
  167. $connection->delete($this->getTable($entityInfo['associations_table']), implode(' AND ', $where));
  168. return $this;
  169. }
  170. /**
  171. * Retrieve rule's associated entity Ids by entity type
  172. *
  173. * @param int $ruleId
  174. * @param string $entityType
  175. * @return array
  176. */
  177. public function getAssociatedEntityIds($ruleId, $entityType)
  178. {
  179. $entityInfo = $this->_getAssociatedEntityInfo($entityType);
  180. $select = $this->getConnection()->select()->from(
  181. $this->getTable($entityInfo['associations_table']),
  182. [$entityInfo['entity_id_field']]
  183. )->where(
  184. $entityInfo['rule_id_field'] . ' = ?',
  185. $ruleId
  186. );
  187. return $this->getConnection()->fetchCol($select);
  188. }
  189. /**
  190. * Retrieve website ids of specified rule
  191. *
  192. * @param int $ruleId
  193. * @return array
  194. */
  195. public function getWebsiteIds($ruleId)
  196. {
  197. return $this->getAssociatedEntityIds($ruleId, 'website');
  198. }
  199. /**
  200. * Retrieve customer group ids of specified rule
  201. *
  202. * @param int $ruleId
  203. * @return array
  204. */
  205. public function getCustomerGroupIds($ruleId)
  206. {
  207. return $this->getAssociatedEntityIds($ruleId, 'customer_group');
  208. }
  209. /**
  210. * Retrieve correspondent entity information (associations table name, columns names)
  211. * of rule's associated entity by specified entity type
  212. *
  213. * @param string $entityType
  214. * @return array
  215. * @throws \Magento\Framework\Exception\LocalizedException
  216. */
  217. protected function _getAssociatedEntityInfo($entityType)
  218. {
  219. if (isset($this->_associatedEntitiesMap[$entityType])) {
  220. return $this->_associatedEntitiesMap[$entityType];
  221. }
  222. throw new \Magento\Framework\Exception\LocalizedException(
  223. __('There is no information about associated entity type "%1".', $entityType)
  224. );
  225. }
  226. }