Advertisement
be_well

Untitled

Jan 19th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.78 KB | None | 0 0
  1. <?php
  2.  
  3. namespace shop\readModels\Shop;
  4.  
  5. use shop\entities\Shop\Brand;
  6. use shop\entities\Shop\Category;
  7. use shop\entities\Shop\Order\Order;
  8. use shop\entities\Shop\Product\ModificationsCategory;
  9. use shop\entities\Shop\Product\Product;
  10. use shop\entities\Shop\Tag;
  11. use yii\data\ActiveDataProvider;
  12. use yii\data\DataProviderInterface;
  13. use yii\db\ActiveQuery;
  14. use yii\db\Expression;
  15. use yii\helpers\ArrayHelper;
  16. use yii\helpers\Json;
  17.  
  18. class ProductReadRepository
  19. {
  20.     public function getAll(): DataProviderInterface
  21.     {
  22.         $query = Product::find()->alias('p')->active('p')->with('photos', 'category');
  23.         return $this->getProvider($query);
  24.     }
  25.  
  26.     public function getFeaturedProducts($limit, $type)
  27.     {
  28.         return Product::find()->andWhere(['type' => $type])->limit($limit)->all();
  29.     }
  30.  
  31.     public function getByIds($ids)
  32.     {
  33.         return Product::find()->active()->andWhere(['id' => $ids])->with(['photos', 'category'])->all();
  34.     }
  35.  
  36.     public function getByIdsWithoutNotInProduction($ids)
  37.     {
  38.         return Product::find()->activeWithoutNotInProduction()->andWhere(['id' => $ids])->with(['photos', 'category'])->all();
  39.     }
  40.  
  41.     public function getAllByCategory(Category $category): DataProviderInterface
  42.     {
  43.         $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
  44.         // сливаем массивы id текущей категории и дочерних (nested sets)
  45.         $ids = ArrayHelper::merge([$category->id], $category->getLeaves()->select('id')->column());
  46.         // 05 00:47
  47.         $query->joinWith(['categoryAssignments ca'], false);
  48.         $query->andWhere(['or', ['p.category_id' => $ids], ['ca.category_id' => $ids]]);
  49.         $query->orderBy(['sale'  => SORT_DESC]);
  50.         $query->orderBy(['order'  => SORT_DESC]);
  51.         $query->orderBy([new \yii\db\Expression('FIELD (status, ' . implode(',', [1, 5, 4, 2, 3]) . ')')]);
  52.         $query->groupBy('p.id');
  53.  
  54.         return $this->getProvider($query);
  55.     }
  56.  
  57.     public function getMoreByCategory($page, $request, $categoryId, $priceLow, $priceHigh, $sort, $sortVal): DataProviderInterface
  58.     {
  59.         $category = Category::findOne(['id' => $categoryId]);
  60.         $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
  61.         $ids = ArrayHelper::merge([$category->id], $category->getLeaves()->select('id')->column());
  62.         $query->joinWith(['categoryAssignments ca'], false);
  63.         $query->andWhere(['or', ['p.category_id' => $ids], ['ca.category_id' => $ids]]);
  64.  
  65.         $query->andWhere(['BETWEEN', 'client_price', $priceLow, $priceHigh]);
  66.  
  67.         if($request != null) {
  68.             foreach (array_filter($request) as $key => $items) {
  69.                 $j = 1;
  70.                 $condition = ['OR'];
  71.                 $query->joinWith(['filterValue fv' . $key], false);
  72.                 if(is_array($items)) {
  73.                     foreach ($items as $i => $item) {
  74.                         $condition[] = ['and', ['fv' . $key . '.characteristic_id' => $key], ['fv' . $key . '.value' => $item]];
  75.                     }
  76.                 }
  77.                 $query->andWhere($condition);
  78.             }
  79.             /*
  80.              * @var $j represents the marker to show whether $request is empty
  81.              */
  82.             if (isset($j)) {
  83.                 $query->andWhere($condition);
  84.             }
  85.         }
  86.  
  87.         $query->orderBy(['order'  => SORT_DESC]);
  88.         $query->orderBy([new \yii\db\Expression('FIELD (status, ' . implode(',', [1, 5, 4, 2, 3]) . ')')]);
  89.         $query->groupBy('p.id');
  90.  
  91.         return $this->getProviderMore($query, $page, $sort, $sortVal);
  92.     }
  93.  
  94.     public function getMoreByCategoryTag($page, $request, $tagId, $priceLow, $priceHigh, $sort, $sortVal): DataProviderInterface
  95.     {
  96.         $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
  97.         $query->joinWith(['tagAssignments ta'], false);
  98.         $query->andWhere(['ta.tag_id' => $tagId]);
  99.  
  100.         $query->andWhere(['BETWEEN', 'client_price', $priceLow, $priceHigh]);
  101.  
  102.         if($request != null) {
  103.             foreach (array_filter($request) as $key => $items) {
  104.                 $j = 1;
  105.                 $condition = ['OR'];
  106.                 $query->joinWith(['filterValue fv' . $key], false);
  107.                 if(is_array($items)) {
  108.                     foreach ($items as $i => $item) {
  109.                         $condition[] = ['and', ['fv' . $key . '.characteristic_id' => $key], ['fv' . $key . '.value' => $item]];
  110.                     }
  111.                 }
  112.                 $query->andWhere($condition);
  113.             }
  114.             /*
  115.              * @var $j represents the marker to show whether $request is empty
  116.              */
  117.             if (isset($j)) {
  118.                 $query->andWhere($condition);
  119.             }
  120.         }
  121.  
  122.         $query->orderBy(['order'  => SORT_DESC]);
  123.         $query->orderBy([new \yii\db\Expression('FIELD (status, ' . implode(',', [1, 5, 4, 2, 3]) . ')')]);
  124.         $query->groupBy('p.id');
  125.  
  126.         return $this->getProviderMore($query, $page, $sort, $sortVal);
  127.     }
  128.  
  129.     public function getAllBySubCategory(Category $category): DataProviderInterface
  130.     {
  131.         $query = Product::find()
  132.             ->alias('p')
  133.             ->active('p')
  134.             ->with('category', 'photos')
  135.             ->andWhere(['id' => Json::decode($category->featured_products)]);
  136.         return $this->getProvider($query);
  137.     }
  138.  
  139.     public function getAllByBrand(Brand $brand): DataProviderInterface
  140.     {
  141.         $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
  142.         $query->andWhere(['p.brand_id' => $brand->id]);
  143.  
  144.         return $this->getProvider($query);
  145.     }
  146.  
  147.     public function getAllByTag(Tag $tag): DataProviderInterface
  148.     {
  149.         $query = Product::find()->alias('p')->activeWithoutNotInProduction('p')->with('category', 'photos');
  150.         $query->joinWith(['tagAssignments ta'], false);
  151.         $query->andWhere(['ta.tag_id' => $tag->id]);
  152.         $query->orderBy(['sale'  => SORT_DESC]);
  153.         $query->orderBy(['order'  => SORT_DESC]);
  154.         $query->orderBy([new \yii\db\Expression('FIELD (status, ' . implode(',', [1, 5, 4, 2, 3]) . ')')]);
  155.         $query->groupBy('p.id');
  156.  
  157.         return $this->getProvider($query);
  158.     }
  159.  
  160.     public function getProductCharacteristics($slug)
  161.     {
  162.         $product = Product::find()->active()->where(['slug' => $slug])->one();
  163.         if ($product->product_characteristics) {
  164.             $productChars = array_slice(Json::decode($product->product_characteristics), 1);
  165.         } else {
  166.             $productChars = [];
  167.         }
  168.         return $productChars;
  169.     }
  170.  
  171.     public function getProductModifications($slug)
  172.     {
  173.         return $product = Product::find()->active()->where(['slug' => $slug])->one();
  174.     }
  175.  
  176.     public function getProductModificationsCategory($modificationsCategoryId)
  177.     {
  178.         $modificationsCategory = ModificationsCategory::find()->where(['id' => $modificationsCategoryId])->one();
  179.         if ($modificationsCategory) {
  180.             return $modificationsCategory->modifications_json;
  181.         }
  182.  
  183.         return null;
  184.     }
  185.  
  186.     public function getFeatured($limit, $type): array
  187.     {
  188.         return Product::find()->active()->andWhere(['type' => $type])->with('category', 'photos')->orderBy(['order' => SORT_DESC])->limit($limit)->all();
  189.     }
  190.  
  191.     public function getFeaturedWithoutNotInProduction($limit, $type): array
  192.     {
  193.         return Product::find()->activeWithoutNotInProduction()->andWhere(['type' => $type])->with('category', 'photos')->orderBy(['order' => SORT_DESC])->limit($limit)->all();
  194.     }
  195.  
  196.     public function getSearchRequest($searchRequest, $limit): array
  197.     {
  198.         return Product::find()
  199.             ->active()
  200.             ->where(['like', 'name', $searchRequest])
  201.             ->orWhere(['like', 'title', $searchRequest])
  202.             ->orWhere(['like', 'code', $searchRequest])
  203.             ->with('category', 'photos')
  204.             ->orderBy(['order' => SORT_DESC, 'name' => SORT_ASC])
  205.             ->limit($limit)
  206.             ->all();
  207.     }
  208.  
  209.     public function getAllBySearchRequest($request): DataProviderInterface
  210.     {
  211.         $query = Product::find()
  212.             ->alias('p')
  213.             ->active('p')
  214.             ->where(['like', 'name', $request])
  215.             ->orWhere(['like', 'title', $request])
  216.             ->orWhere(['like', 'code', $request])
  217.             ->with('category', 'photos')
  218.             ->limit(100);
  219.  
  220.         return $this->getProvider($query, 100);
  221.     }
  222.  
  223.     /*
  224.      * @var $request filter ids
  225.      */
  226.     public function getAllByFilterRequest($request, $categoryId, $priceLow, $priceHigh, $sort, $sortVal): DataProviderInterface
  227.     {
  228.         $category = Category::findOne(['id' => $categoryId]);
  229.         $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
  230.         $ids = ArrayHelper::merge([$category->id], $category->getLeaves()->select('id')->column());
  231.         $query->joinWith(['categoryAssignments ca'], false);
  232.         $query->andWhere(['or', ['p.category_id' => $ids], ['ca.category_id' => $ids]]);
  233.  
  234.         $query->andWhere(['BETWEEN', 'client_price', $priceLow, $priceHigh]);
  235.  
  236.         if($request != null) {
  237.             foreach (array_filter($request) as $key => $items) {
  238.                 $j = 1;
  239.                 $condition = ['OR'];
  240.                 $query->joinWith(['filterValue fv' . $key], false);
  241.                 if(is_array($items)) {
  242.                     foreach ($items as $i => $item) {
  243.                         $condition[] = ['and', ['fv' . $key . '.characteristic_id' => $key], ['fv' . $key . '.value' => $item]];
  244.                     }
  245.                 }
  246.                 $query->andWhere($condition);
  247.             }
  248.             /*
  249.              * @var $j represents the marker to show whether $request is empty
  250.              */
  251.             if (isset($j)) {
  252.                 $query->andWhere($condition);
  253.             }
  254.         }
  255.         $query->orderBy([new \yii\db\Expression('FIELD (status, ' . implode(',', [1, 5, 4, 2, 3]) . ')')]);
  256.         $query->groupBy('p.id');
  257.  
  258.         return $this->getProvider($query, 51, $sort, $sortVal);
  259.     }
  260.  
  261.     /*
  262.      * @var $request filter ids
  263.      */
  264.     public function getAllByFilterRequestTag($request , $categoryId, $priceLow, $priceHigh, $sort, $sortVal): DataProviderInterface
  265.     {
  266.         $tag = Tag::findOne(['id' => $categoryId]);
  267.         $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
  268.         $query->joinWith(['tagAssignments ta'], false);
  269.         $query->andWhere(['ta.tag_id' => $tag->id]);
  270.  
  271.         // пока не сделал систему автопересчета для комплектов комментим поиск по цене
  272.         $query->andWhere(['BETWEEN', 'client_price', $priceLow, $priceHigh]);
  273.  
  274.         if($request != null) {
  275.             foreach (array_filter($request) as $key => $items) {
  276.                 $j = 1;
  277.                 $condition = ['OR'];
  278.                 $query->joinWith(['filterValue fv' . $key], false);
  279.                 if(is_array($items)) {
  280.                     foreach ($items as $i => $item) {
  281.                         $condition[] = ['and', ['fv' . $key . '.characteristic_id' => $key], ['fv' . $key . '.value' => $item]];
  282.                     }
  283.                 }
  284.                 $query->andWhere($condition);
  285.             }
  286.             /*
  287.              * @var $j represents the marker to show whether $request is empty
  288.              */
  289.             if (isset($j)) {
  290.                 $query->andWhere($condition);
  291.             }
  292.         }
  293.         $query->orderBy([new \yii\db\Expression('FIELD (status, ' . implode(',', [1, 5, 4, 2, 3]) . ')')]);
  294.         $query->groupBy('p.id');
  295.  
  296.         return $this->getProvider($query, 51, $sort, $sortVal);
  297.     }
  298.  
  299.     public function find($id): ?Product
  300.     {
  301.         return Product::find()->active()->andWhere(['id' => $id])->one();
  302.     }
  303.  
  304.     public function findBySlug($slug): ?Product
  305.     {
  306.         return Product::find()->active()->andWhere(['slug' => $slug])->with('modifications', 'comments')->one();
  307.     }
  308.  
  309.     public function getMoreBySearchRequest($page, $searchQuery): DataProviderInterface
  310.     {
  311.         $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
  312.         $query->andWhere(['like', 'name', $searchQuery]);
  313.         $query->orWhere(['like', 'title', $searchQuery]);
  314.         $query->orWhere(['like', 'code', $searchQuery]);
  315.         $query->groupBy('p.id');
  316.  
  317.         return $this->getProviderMore($query, $page, 'order', 'desc', 100);
  318.     }
  319.  
  320.     private function getProvider(ActiveQuery $query, $pageSize = 51, $sort = 'order', $sortValue = 'desc'): ActiveDataProvider
  321.     {
  322.         return new ActiveDataProvider([
  323.             'query' => $query,
  324.             'sort' => [
  325.                 'defaultOrder' => [
  326.                     'status' => SORT_ASC,
  327.                     $sort => $sortValue, //
  328.                     'name' => SORT_ASC
  329.                 ],
  330.                 'attributes' => [
  331.                     'order' => [
  332.                         'asc' => ['p.order' => SORT_ASC],
  333.                         'desc' => ['p.order' => SORT_DESC],
  334.                     ],
  335.                     'status' => [
  336.                         'asc' => ['p.status' => SORT_ASC],
  337.                         'desc' => ['p.status' => SORT_DESC],
  338.                     ],
  339.                     'price' => [
  340.                         'asc' => ['p.client_price' => SORT_ASC],
  341.                         'desc' => ['p.client_price' => SORT_DESC],
  342.                     ],
  343.                     'rating' => [
  344.                         'asc' => ['p.rating' => SORT_ASC],
  345.                         'desc' => ['p.rating' => SORT_DESC],
  346.                     ],
  347.                     'name' => [
  348.                         'asc' => ['p.name' => SORT_ASC],
  349.                         'desc' => ['p.name' => SORT_DESC],
  350.                     ],
  351.                 ],
  352.             ],
  353.  
  354.             'pagination' => [
  355.                 'pageSizeLimit' => [1, 51],
  356.                 'pageSize' => $pageSize,
  357.             ]
  358.         ]);
  359.     }
  360.  
  361.     private function getProviderMore(ActiveQuery $query, $page, $sort = 'order', $sortValue = 'desc', $pageSize = 51): ActiveDataProvider
  362.     {
  363.         return new ActiveDataProvider([
  364.             'query' => $query,
  365.             'sort' => [
  366.                 'defaultOrder' => [
  367.                     'status' => SORT_ASC,
  368.                     $sort => $sortValue,
  369.                     'name' => SORT_ASC
  370.                 ],
  371.                 'attributes' => [
  372.                     'order' => [
  373.                         'asc' => ['p.order' => SORT_ASC],
  374.                         'desc' => ['p.order' => SORT_DESC],
  375.                     ],
  376.                     'status' => [
  377.                         'asc' => ['p.status' => SORT_ASC],
  378.                         'desc' => ['p.status' => SORT_DESC],
  379.                     ],
  380.                     'price' => [
  381.                         'asc' => ['p.client_price' => SORT_ASC],
  382.                         'desc' => ['p.client_price' => SORT_DESC],
  383.                     ],
  384.                     'rating' => [
  385.                         'asc' => ['p.rating' => SORT_ASC],
  386.                         'desc' => ['p.rating' => SORT_DESC],
  387.                     ],
  388.                     'name' => [
  389.                         'asc' => ['p.name' => SORT_ASC],
  390.                         'desc' => ['p.name' => SORT_DESC],
  391.                     ],
  392.                 ],
  393.             ],
  394.             'pagination' => [
  395.                 'pageSizeLimit' => [1, $pageSize],
  396.                 'pageSize' => $pageSize,
  397.                 'page' => $page
  398.             ]
  399.         ]);
  400.     }
  401.  
  402.     public function getWishlist($userId): ActiveDataProvider
  403.     {
  404.         return new ActiveDataProvider([
  405.             'query' => Product::find()
  406.                 ->alias('p')->active('p')
  407.                 ->joinWith('wishlistItems w', false, 'INNER JOIN')
  408.                 ->andWhere(['w.user_id' => $userId]),
  409.             'sort' => false,
  410.         ]);
  411.     }
  412.  
  413.     public function getWishlistProducts($userId)
  414.     {
  415.         $products = Product::find()
  416.             ->alias('p')->active('p')
  417.             ->joinWith('wishlistItems w', false, 'INNER JOIN')
  418.             ->andWhere(['w.user_id' => $userId])
  419.             ->all();
  420.  
  421.         return $products;
  422.     }
  423.  
  424.     public function getOrderItems($orderId)
  425.     {
  426.         $orderItems = Order::find()
  427.             ->where(['id' => $orderId])
  428.             ->all();
  429.  
  430.         return $orderItems;
  431.     }
  432.  
  433.     public function getProductSetPrice($ids)
  434.     {
  435.         $price = 0;
  436.  
  437.         foreach (Json::decode($ids) as $id) {
  438.             $product = Product::find()->where(['id' => $id])->one();
  439.             $price = $price + $product->client_price;
  440.         }
  441.  
  442.         return $price;
  443.     }
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement