HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot//www.shooperm.com/Modules/Review/Repositories/ProductReviewRepository.php
<?php

namespace Modules\Review\Repositories;

use Modules\Review\Entities\ProductReview;
use App\Traits\ImageStore;
use App\Traits\Notification;
use Modules\GeneralSetting\Entities\EmailTemplateType;
use Modules\GeneralSetting\Entities\GeneralSetting;
use Modules\Review\Entities\ReviewImage;

class ProductReviewRepository
{
    use ImageStore, Notification;

    public function getAll()
    {
        return ProductReview::with('product', 'giftcard');
    }

    public function getPendingAll()
    {
        return ProductReview::with('product', 'giftcard')->where('status', 0);
    }
    public function getDeclinedAll()
    {
        return ProductReview::with('product', 'giftcard')->where('status', 3);
    }

    public function approve($id)
    {
        $product_review = ProductReview::findOrFail($id);
        if ($product_review->type == 'product') {
            $seller_product = $product_review->product;
            $total_review = ($seller_product->reviews->count() > 0) ? $seller_product->reviews->count() : 1;

            $seller_product->update([
                'avg_rating' => number_format($seller_product->reviews->sum('rating') / $total_review, 2)
            ]);

            if ($seller_product->product->category_id != null || $seller_product->product->category_id != 0) {
                $category = $seller_product->product->category;
                $category->update([
                    'avg_rating' => number_format($category->sellerProductsAll()->sum('avg_rating') / count($category->sellerProductsAll()), 2)
                ]);
            }

            if ($seller_product->product->brand_id != null || $seller_product->product->brand_id != 0) {
                $brand = $seller_product->product->brand;
                $brand->update([
                    'avg_rating' => number_format($brand->sellerProductsAll()->sum('avg_rating') / count($brand->sellerProductsAll()), 2)
                ]);
            }
        } else {
            $giftcard = $product_review->giftcard;
            $total_review = ($giftcard->reviews->count() > 0) ? $giftcard->reviews->count() : 1;
            $giftcard->update([
                'avg_rating' => number_format($giftcard->reviews->sum('rating') / $total_review, 2)
            ]);
        }

        // Send Notification
        $this->sendNotificationToSeller($product_review->seller_id);

        return $product_review->update([
            'status' => 1
        ]);
    }
    public function approveAll()
    {
        $reviews = ProductReview::where('status', 0)->get();
        foreach ($reviews as $product_review) {

            if ($product_review->type == 'product') {
                $seller_product = $product_review->product;
                $total_review = ($seller_product->reviews->count() > 0) ? $seller_product->reviews->count() : 1;

                $seller_product->update([
                    'avg_rating' => number_format($seller_product->reviews->sum('rating') / $total_review, 2)
                ]);

                if ($seller_product->product->category_id != null || $seller_product->product->category_id != 0) {
                    $category = $seller_product->product->category;
                    $category->update([
                        'avg_rating' => number_format($category->sellerProductsAll()->sum('avg_rating') / count($category->sellerProductsAll()), 2)
                    ]);
                }

                if ($seller_product->product->brand_id != null || $seller_product->product->brand_id != 0) {
                    $brand = $seller_product->product->brand;
                    $brand->update([
                        'avg_rating' => number_format($brand->sellerProductsAll()->sum('avg_rating') / count($brand->sellerProductsAll()), 2)
                    ]);
                }
            } else {
                $giftcard = $product_review->giftcard;
                $total_review = ($giftcard->reviews->count() > 0) ? $giftcard->reviews->count() : 1;
                $giftcard->update([
                    'avg_rating' => number_format($giftcard->reviews->sum('rating') / $total_review, 2)
                ]);
            }

            $product_review->update([
                'status' => 1
            ]);

            // Send Notification
            $this->sendNotificationToSeller($product_review->seller_id);
        }
        return true;
    }
    public function deleteById($id)
    {
        $review = ProductReview::where('id', $id)->first();
        $review->update([
            'status' => 3
        ]);

        return true;
    }

    public function getReviewConfiguration()
    {
        return GeneralSetting::first();
    }

    public function reviewConfigurationUpdate($request)
    {
        $generatlSetting = GeneralSetting::first();
        $generatlSetting->auto_approve_product_review = $request->product_review_status;
        $generatlSetting->auto_approve_seller_review = $request->seller_review_status;
        $generatlSetting->save();
    }


    public function sendNotificationToSeller($sellerId)
    {
        if(isModuleActive('MultiVendor') && $sellerId != 1){
            $notificationUrl = route('seller.product-reviews.index');
            $notificationUrl = str_replace(url('/'),'',$notificationUrl);
            $this->notificationUrl = $notificationUrl;
            $this->adminNotificationUrl = '/review/product-list';
            $this->routeCheck = 'review.product.index';
            $this->typeId = EmailTemplateType::where('type', 'product_review_approve_email_template')->first()->id;
            $this->notificationSend("Product review", $sellerId);
        }
    }
}