Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Project: Nadzor
- * Author: Dmitry Filatov
- * E-mail: other.mind@gmail.com
- * Date: 12/24/17
- * Time: 1:13 PM
- */
- namespace shop\readModels\Shop;
- use shop\entities\Shop\Brand;
- use shop\entities\Shop\Category;
- use shop\entities\Shop\Order\Order;
- use shop\entities\Shop\Product\ModificationsCategory;
- use shop\entities\Shop\Product\Product;
- use shop\entities\Shop\Tag;
- use yii\data\ActiveDataProvider;
- use yii\data\DataProviderInterface;
- use yii\db\ActiveQuery;
- use yii\db\Expression;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Json;
- class ProductReadRepository
- {
- public function getAll(): DataProviderInterface
- {
- $query = Product::find()->alias('p')->active('p')->with('photos', 'category');
- return $this->getProvider($query);
- }
- public function getFeaturedProducts($limit, $type)
- {
- return Product::find()->andWhere(['type' => $type])->limit($limit)->all();
- }
- public function getByIds($ids)
- {
- return Product::find()->active()->andWhere(['id' => $ids])->with(['photos', 'category'])->all();
- }
- public function getByIdsWithoutNotInProduction($ids)
- {
- return Product::find()->activeWithoutNotInProduction()->andWhere(['id' => $ids])->with(['photos', 'category'])->all();
- }
- public function getAllByCategory(Category $category): DataProviderInterface
- {
- $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
- // сливаем массивы id текущей категории и дочерних (nested sets)
- $ids = ArrayHelper::merge([$category->id], $category->getLeaves()->select('id')->column());
- // 05 00:47
- $query->joinWith(['categoryAssignments ca'], false);
- $query->andWhere(['or', ['p.category_id' => $ids], ['ca.category_id' => $ids]]);
- $query->orderBy(['sale' => SORT_DESC]);
- $query->orderBy(['order' => SORT_DESC]);
- $query->orderBy([new \yii\db\Expression('FIELD (status, ' . implode(',', [1, 5, 4, 2, 3]) . ')')]);
- $query->groupBy('p.id');
- return $this->getProvider($query);
- }
- public function getMoreByCategory($page, $request, $categoryId, $priceLow, $priceHigh, $sort, $sortVal): DataProviderInterface
- {
- $category = Category::findOne(['id' => $categoryId]);
- $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
- $ids = ArrayHelper::merge([$category->id], $category->getLeaves()->select('id')->column());
- $query->joinWith(['categoryAssignments ca'], false);
- $query->andWhere(['or', ['p.category_id' => $ids], ['ca.category_id' => $ids]]);
- $query->andWhere(['BETWEEN', 'client_price', $priceLow, $priceHigh]);
- if($request != null) {
- foreach (array_filter($request) as $key => $items) {
- $j = 1;
- $condition = ['OR'];
- $query->joinWith(['filterValue fv' . $key], false);
- if(is_array($items)) {
- foreach ($items as $i => $item) {
- $condition[] = ['and', ['fv' . $key . '.characteristic_id' => $key], ['fv' . $key . '.value' => $item]];
- }
- }
- $query->andWhere($condition);
- }
- /*
- * @var $j represents the marker to show whether $request is empty
- */
- if (isset($j)) {
- $query->andWhere($condition);
- }
- }
- $query->orderBy(['order' => SORT_DESC]);
- $query->orderBy([new \yii\db\Expression('FIELD (status, ' . implode(',', [1, 5, 4, 2, 3]) . ')')]);
- $query->groupBy('p.id');
- return $this->getProviderMore($query, $page, $sort, $sortVal);
- }
- public function getMoreByCategoryTag($page, $request, $tagId, $priceLow, $priceHigh, $sort, $sortVal): DataProviderInterface
- {
- $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
- $query->joinWith(['tagAssignments ta'], false);
- $query->andWhere(['ta.tag_id' => $tagId]);
- $query->andWhere(['BETWEEN', 'client_price', $priceLow, $priceHigh]);
- if($request != null) {
- foreach (array_filter($request) as $key => $items) {
- $j = 1;
- $condition = ['OR'];
- $query->joinWith(['filterValue fv' . $key], false);
- if(is_array($items)) {
- foreach ($items as $i => $item) {
- $condition[] = ['and', ['fv' . $key . '.characteristic_id' => $key], ['fv' . $key . '.value' => $item]];
- }
- }
- $query->andWhere($condition);
- }
- /*
- * @var $j represents the marker to show whether $request is empty
- */
- if (isset($j)) {
- $query->andWhere($condition);
- }
- }
- $query->orderBy(['order' => SORT_DESC]);
- $query->orderBy([new \yii\db\Expression('FIELD (status, ' . implode(',', [1, 5, 4, 2, 3]) . ')')]);
- $query->groupBy('p.id');
- return $this->getProviderMore($query, $page, $sort, $sortVal);
- }
- public function getAllBySubCategory(Category $category): DataProviderInterface
- {
- $query = Product::find()
- ->alias('p')
- ->active('p')
- ->with('category', 'photos')
- ->andWhere(['id' => Json::decode($category->featured_products)]);
- return $this->getProvider($query);
- }
- public function getAllByBrand(Brand $brand): DataProviderInterface
- {
- $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
- $query->andWhere(['p.brand_id' => $brand->id]);
- return $this->getProvider($query);
- }
- public function getAllByTag(Tag $tag): DataProviderInterface
- {
- $query = Product::find()->alias('p')->activeWithoutNotInProduction('p')->with('category', 'photos');
- $query->joinWith(['tagAssignments ta'], false);
- $query->andWhere(['ta.tag_id' => $tag->id]);
- $query->orderBy(['sale' => SORT_DESC]);
- $query->orderBy(['order' => SORT_DESC]);
- $query->orderBy([new \yii\db\Expression('FIELD (status, ' . implode(',', [1, 5, 4, 2, 3]) . ')')]);
- $query->groupBy('p.id');
- return $this->getProvider($query);
- }
- public function getProductCharacteristics($slug)
- {
- $product = Product::find()->active()->where(['slug' => $slug])->one();
- if ($product->product_characteristics) {
- $productChars = array_slice(Json::decode($product->product_characteristics), 1);
- } else {
- $productChars = [];
- }
- return $productChars;
- }
- public function getProductModifications($slug)
- {
- return $product = Product::find()->active()->where(['slug' => $slug])->one();
- }
- public function getProductModificationsCategory($modificationsCategoryId)
- {
- $modificationsCategory = ModificationsCategory::find()->where(['id' => $modificationsCategoryId])->one();
- if ($modificationsCategory) {
- return $modificationsCategory->modifications_json;
- }
- return null;
- }
- public function getFeatured($limit, $type): array
- {
- return Product::find()->active()->andWhere(['type' => $type])->with('category', 'photos')->orderBy(['order' => SORT_DESC])->limit($limit)->all();
- }
- public function getFeaturedWithoutNotInProduction($limit, $type): array
- {
- return Product::find()->activeWithoutNotInProduction()->andWhere(['type' => $type])->with('category', 'photos')->orderBy(['order' => SORT_DESC])->limit($limit)->all();
- }
- public function getSearchRequest($searchRequest, $limit): array
- {
- return Product::find()
- ->active()
- ->where(['like', 'name', $searchRequest])
- ->orWhere(['like', 'title', $searchRequest])
- ->orWhere(['like', 'code', $searchRequest])
- ->with('category', 'photos')
- ->orderBy(['order' => SORT_DESC, 'name' => SORT_ASC])
- ->limit($limit)
- ->all();
- }
- public function getAllBySearchRequest($request): DataProviderInterface
- {
- $query = Product::find()
- ->alias('p')
- ->active('p')
- ->where(['like', 'name', $request])
- ->orWhere(['like', 'title', $request])
- ->orWhere(['like', 'code', $request])
- ->with('category', 'photos')
- ->limit(100);
- return $this->getProvider($query, 100);
- }
- /*
- * @var $request filter ids
- */
- public function getAllByFilterRequest($request, $categoryId, $priceLow, $priceHigh, $sort, $sortVal): DataProviderInterface
- {
- $category = Category::findOne(['id' => $categoryId]);
- $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
- $ids = ArrayHelper::merge([$category->id], $category->getLeaves()->select('id')->column());
- $query->joinWith(['categoryAssignments ca'], false);
- $query->andWhere(['or', ['p.category_id' => $ids], ['ca.category_id' => $ids]]);
- $query->andWhere(['BETWEEN', 'client_price', $priceLow, $priceHigh]);
- if($request != null) {
- foreach (array_filter($request) as $key => $items) {
- $j = 1;
- $condition = ['OR'];
- $query->joinWith(['filterValue fv' . $key], false);
- if(is_array($items)) {
- foreach ($items as $i => $item) {
- $condition[] = ['and', ['fv' . $key . '.characteristic_id' => $key], ['fv' . $key . '.value' => $item]];
- }
- }
- $query->andWhere($condition);
- }
- /*
- * @var $j represents the marker to show whether $request is empty
- */
- if (isset($j)) {
- $query->andWhere($condition);
- }
- }
- $query->orderBy([new \yii\db\Expression('FIELD (status, ' . implode(',', [1, 5, 4, 2, 3]) . ')')]);
- $query->groupBy('p.id');
- return $this->getProvider($query, 51, $sort, $sortVal);
- }
- /*
- * @var $request filter ids
- */
- public function getAllByFilterRequestTag($request , $categoryId, $priceLow, $priceHigh, $sort, $sortVal): DataProviderInterface
- {
- $tag = Tag::findOne(['id' => $categoryId]);
- $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
- $query->joinWith(['tagAssignments ta'], false);
- $query->andWhere(['ta.tag_id' => $tag->id]);
- // пока не сделал систему автопересчета для комплектов комментим поиск по цене
- $query->andWhere(['BETWEEN', 'client_price', $priceLow, $priceHigh]);
- if($request != null) {
- foreach (array_filter($request) as $key => $items) {
- $j = 1;
- $condition = ['OR'];
- $query->joinWith(['filterValue fv' . $key], false);
- if(is_array($items)) {
- foreach ($items as $i => $item) {
- $condition[] = ['and', ['fv' . $key . '.characteristic_id' => $key], ['fv' . $key . '.value' => $item]];
- }
- }
- $query->andWhere($condition);
- }
- /*
- * @var $j represents the marker to show whether $request is empty
- */
- if (isset($j)) {
- $query->andWhere($condition);
- }
- }
- $query->orderBy([new \yii\db\Expression('FIELD (status, ' . implode(',', [1, 5, 4, 2, 3]) . ')')]);
- $query->groupBy('p.id');
- return $this->getProvider($query, 51, $sort, $sortVal);
- }
- public function find($id): ?Product
- {
- return Product::find()->active()->andWhere(['id' => $id])->one();
- }
- public function findBySlug($slug): ?Product
- {
- return Product::find()->active()->andWhere(['slug' => $slug])->with('modifications', 'comments')->one();
- }
- public function getMoreBySearchRequest($page, $searchQuery): DataProviderInterface
- {
- $query = Product::find()->alias('p')->active('p')->with('category', 'photos');
- $query->andWhere(['like', 'name', $searchQuery]);
- $query->orWhere(['like', 'title', $searchQuery]);
- $query->orWhere(['like', 'code', $searchQuery]);
- $query->groupBy('p.id');
- return $this->getProviderMore($query, $page, 'order', 'desc', 100);
- }
- private function getProvider(ActiveQuery $query, $pageSize = 51, $sort = 'order', $sortValue = 'desc'): ActiveDataProvider
- {
- return new ActiveDataProvider([
- 'query' => $query,
- 'sort' => [
- 'defaultOrder' => [
- 'status' => SORT_ASC,
- $sort => $sortValue, //
- 'name' => SORT_ASC
- ],
- 'attributes' => [
- 'order' => [
- 'asc' => ['p.order' => SORT_ASC],
- 'desc' => ['p.order' => SORT_DESC],
- ],
- 'status' => [
- 'asc' => ['p.status' => SORT_ASC],
- 'desc' => ['p.status' => SORT_DESC],
- ],
- 'price' => [
- 'asc' => ['p.client_price' => SORT_ASC],
- 'desc' => ['p.client_price' => SORT_DESC],
- ],
- 'rating' => [
- 'asc' => ['p.rating' => SORT_ASC],
- 'desc' => ['p.rating' => SORT_DESC],
- ],
- 'name' => [
- 'asc' => ['p.name' => SORT_ASC],
- 'desc' => ['p.name' => SORT_DESC],
- ],
- ],
- ],
- 'pagination' => [
- 'pageSizeLimit' => [1, 51],
- 'pageSize' => $pageSize,
- ]
- ]);
- }
- private function getProviderMore(ActiveQuery $query, $page, $sort = 'order', $sortValue = 'desc', $pageSize = 51): ActiveDataProvider
- {
- return new ActiveDataProvider([
- 'query' => $query,
- 'sort' => [
- 'defaultOrder' => [
- 'status' => SORT_ASC,
- $sort => $sortValue,
- 'name' => SORT_ASC
- ],
- 'attributes' => [
- 'order' => [
- 'asc' => ['p.order' => SORT_ASC],
- 'desc' => ['p.order' => SORT_DESC],
- ],
- 'status' => [
- 'asc' => ['p.status' => SORT_ASC],
- 'desc' => ['p.status' => SORT_DESC],
- ],
- 'price' => [
- 'asc' => ['p.client_price' => SORT_ASC],
- 'desc' => ['p.client_price' => SORT_DESC],
- ],
- 'rating' => [
- 'asc' => ['p.rating' => SORT_ASC],
- 'desc' => ['p.rating' => SORT_DESC],
- ],
- 'name' => [
- 'asc' => ['p.name' => SORT_ASC],
- 'desc' => ['p.name' => SORT_DESC],
- ],
- ],
- ],
- 'pagination' => [
- 'pageSizeLimit' => [1, $pageSize],
- 'pageSize' => $pageSize,
- 'page' => $page
- ]
- ]);
- }
- public function getWishlist($userId): ActiveDataProvider
- {
- return new ActiveDataProvider([
- 'query' => Product::find()
- ->alias('p')->active('p')
- ->joinWith('wishlistItems w', false, 'INNER JOIN')
- ->andWhere(['w.user_id' => $userId]),
- 'sort' => false,
- ]);
- }
- public function getWishlistProducts($userId)
- {
- $products = Product::find()
- ->alias('p')->active('p')
- ->joinWith('wishlistItems w', false, 'INNER JOIN')
- ->andWhere(['w.user_id' => $userId])
- ->all();
- return $products;
- }
- public function getOrderItems($orderId)
- {
- $orderItems = Order::find()
- ->where(['id' => $orderId])
- ->all();
- return $orderItems;
- }
- public function getProductSetPrice($ids)
- {
- $price = 0;
- foreach (Json::decode($ids) as $id) {
- $product = Product::find()->where(['id' => $id])->one();
- $price = $price + $product->client_price;
- }
- return $price;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement