Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. <?php
  2.  
  3. class RebuildArticleCategoryIndex
  4. {
  5. /**
  6. * Clears the Category Assigment for the Article and rebuilds the Category Assignment Index
  7. */
  8. public function rebuildArticleCategories($articleId, array $categoryIds)
  9. {
  10. $this->clearArticleCategoryAssignment($articleId);
  11.  
  12. foreach ($categoryIds as $categoryId) {
  13. $this->rebuildArticleCategoryAssignment($articleId, $categoryId);
  14. }
  15. }
  16.  
  17. /**
  18. * @param int $articleId
  19. */
  20. public function clearArticleCategoryAssignment($articleId)
  21. {
  22. $clearQuery = 'DELETE FROM s_articles_categories_ro WHERE `articleID` = ' . $articleId;
  23. try {
  24. Shopware()->Models()->getConnection()->executeQuery($clearQuery);
  25. } catch (DBALException $e) {
  26. }
  27. }
  28.  
  29. /**
  30. * Inserts missing assignments in s_articles_categories_ro
  31. *
  32. *
  33. * @param int $articleId
  34. * @param int $categoryId
  35. *
  36. * @return int
  37. */
  38. public function rebuildArticleCategoryAssignment($articleId, $categoryId)
  39. {
  40. $count = 0;
  41.  
  42. $parents = $this->getParentCategoryIds($categoryId);
  43. if (empty($parents)) {
  44. return $count;
  45. }
  46.  
  47. $selectSql = '
  48. SELECT id
  49. FROM s_articles_categories_ro
  50. WHERE categoryID = :categoryId
  51. AND articleID = :articleId
  52. AND parentCategoryId = :parentCategoryId
  53. ';
  54.  
  55. /** @var \Enlight_Components_Db_Adapter_Pdo_Mysql $db */
  56. $db = Shopware()->Container()->get('shopware.db');
  57.  
  58. $selectStmt = $db->prepare($selectSql);
  59.  
  60. $insertSql = 'INSERT INTO s_articles_categories_ro (articleID, categoryID, parentCategoryID) VALUES (:articleId, :categoryId, :parentCategoryId)';
  61. $insertStmt = $db->prepare($insertSql);
  62.  
  63. foreach ($parents as $parentId) {
  64. $selectStmt->execute([
  65. ':articleId' => $articleId,
  66. ':categoryId' => $parentId,
  67. ':parentCategoryId' => $categoryId,
  68. ]);
  69.  
  70. if ($selectStmt->fetchColumn() === false) {
  71. ++$count;
  72.  
  73. $insertStmt->execute([
  74. ':articleId' => $articleId,
  75. ':categoryId' => $parentId,
  76. ':parentCategoryId' => $categoryId,
  77. ]);
  78. }
  79. }
  80.  
  81. return $count;
  82. }
  83.  
  84. /**
  85. * Returns an array of all categoryIds the given $id has as parent
  86. *
  87. * Example:
  88. * $id = 9
  89. *
  90. * <code>
  91. * Array
  92. * (
  93. * [0] => 9
  94. * [1] => 5
  95. * [2] => 10
  96. * [3] => 3
  97. * )
  98. * <code>
  99. *
  100. * @param int $id
  101. *
  102. * @return array
  103. */
  104. public function getParentCategoryIds($id)
  105. {
  106. /** @var \Enlight_Components_Db_Adapter_Pdo_Mysql $db */
  107. $db = Shopware()->Container()->get('shopware.db');
  108.  
  109. /** @var \PDOStatement $stmt */
  110. $stmt = $db->prepare('SELECT id, parent FROM s_categories WHERE id = :id AND parent IS NOT NULL');
  111. $stmt->execute([':id' => $id]);
  112.  
  113. $parent = $stmt->fetch(\PDO::FETCH_ASSOC);
  114. if (!$parent) {
  115. return [];
  116. }
  117.  
  118. $result = [$parent['id']];
  119.  
  120. $parent = $this->getParentCategoryIds($parent['parent']);
  121. if ($parent) {
  122. $result = array_merge($result, $parent);
  123. }
  124.  
  125. $cache[$id] = $result;
  126.  
  127. return $result;
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement