Advertisement
Guest User

Untitled

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