InitReviewStatusesAndData.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Setup\Patch\Data;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\Setup\Patch\DataPatchInterface;
  9. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  10. class InitReviewStatusesAndData implements DataPatchInterface, PatchVersionInterface
  11. {
  12. /**
  13. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  14. */
  15. private $moduleDataSetup;
  16. /**
  17. * PatchInitial constructor.
  18. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  19. */
  20. public function __construct(
  21. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  22. ) {
  23. $this->moduleDataSetup = $moduleDataSetup;
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function apply()
  29. {
  30. //Fill table review/review_entity
  31. $reviewEntityCodes = [
  32. \Magento\Review\Model\Review::ENTITY_PRODUCT_CODE,
  33. \Magento\Review\Model\Review::ENTITY_CUSTOMER_CODE,
  34. \Magento\Review\Model\Review::ENTITY_CATEGORY_CODE,
  35. ];
  36. foreach ($reviewEntityCodes as $entityCode) {
  37. $this->moduleDataSetup->getConnection()->insert(
  38. $this->moduleDataSetup->getTable('review_entity'),
  39. ['entity_code' => $entityCode]
  40. );
  41. }
  42. //Fill table review/review_entity
  43. $reviewStatuses = [
  44. \Magento\Review\Model\Review::STATUS_APPROVED => 'Approved',
  45. \Magento\Review\Model\Review::STATUS_PENDING => 'Pending',
  46. \Magento\Review\Model\Review::STATUS_NOT_APPROVED => 'Not Approved',
  47. ];
  48. foreach ($reviewStatuses as $k => $v) {
  49. $bind = ['status_id' => $k, 'status_code' => $v];
  50. $this->moduleDataSetup->getConnection()->insertForce(
  51. $this->moduleDataSetup->getTable('review_status'),
  52. $bind
  53. );
  54. }
  55. $data = [
  56. \Magento\Review\Model\Rating::ENTITY_PRODUCT_CODE => [
  57. ['rating_code' => 'Quality', 'position' => 0],
  58. ['rating_code' => 'Value', 'position' => 0],
  59. ['rating_code' => 'Price', 'position' => 0],
  60. ],
  61. \Magento\Review\Model\Rating::ENTITY_PRODUCT_REVIEW_CODE => [],
  62. \Magento\Review\Model\Rating::ENTITY_REVIEW_CODE => [],
  63. ];
  64. foreach ($data as $entityCode => $ratings) {
  65. //Fill table rating/rating_entity
  66. $this->moduleDataSetup->getConnection()->insert(
  67. $this->moduleDataSetup->getTable('rating_entity'),
  68. ['entity_code' => $entityCode]
  69. );
  70. $entityId = $this->moduleDataSetup->getConnection()->lastInsertId(
  71. $this->moduleDataSetup->getTable('rating_entity')
  72. );
  73. foreach ($ratings as $bind) {
  74. //Fill table rating/rating
  75. $bind['entity_id'] = $entityId;
  76. $this->moduleDataSetup->getConnection()->insert(
  77. $this->moduleDataSetup->getTable('rating'),
  78. $bind
  79. );
  80. //Fill table rating/rating_option
  81. $ratingId = $this->moduleDataSetup->getConnection()->lastInsertId(
  82. $this->moduleDataSetup->getTable('rating')
  83. );
  84. $optionData = [];
  85. for ($i = 1; $i <= 5; $i++) {
  86. $optionData[] = ['rating_id' => $ratingId, 'code' => (string)$i, 'value' => $i, 'position' => $i];
  87. }
  88. $this->moduleDataSetup->getConnection()->insertMultiple(
  89. $this->moduleDataSetup->getTable('rating_option'),
  90. $optionData
  91. );
  92. }
  93. }
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public static function getDependencies()
  99. {
  100. return [];
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public static function getVersion()
  106. {
  107. return '2.0.0';
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function getAliases()
  113. {
  114. return [];
  115. }
  116. }