Skip to content
Snippets Groups Projects
CompareProduct.php 5.68 KiB
Newer Older
Shizuco's avatar
Shizuco committed
<?php

declare (strict_types = 1);

Shizuco's avatar
Shizuco committed
namespace App\Nova\Lenses;

Shizuco's avatar
Shizuco committed
use App\Models\Product;
use App\Models\Retailer;
Shizuco's avatar
Shizuco committed
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\LensRequest;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Lenses\Lens;

class CompareProduct extends Lens
{
Shizuco's avatar
Shizuco committed
    public static function query(LensRequest $request, $query)
    {
        $data = self::getDataFromUrl();
        if ($data) {
            $existFields = Product::where('retailer_id', $data['retailer'])->pluck('sku')->all();
Shizuco's avatar
Shizuco committed
            $returnQuery = $query
            ->where('retailer_id', '!=', $data['retailer'])
            ->whereIn('sku', $existFields)
            ->join('retailers as r', 'products.retailer_id', '=', 'r.id');
            if($data['field'] === 'name')
            {
                $returnQuery->select(
                    'sku',
                    DB::raw('group_concat(products.id) as id'),
                    DB::raw('group_concat(products.name) as name'),
                    DB::raw('group_concat(r.id) as retailer_id'));
            }
            else{
                $returnQuery->select(
Shizuco's avatar
Shizuco committed
                    DB::raw('group_concat(products.id) as id'),
                    DB::raw('group_concat(' . $data['field'] . ') as ' . $data['field']),
                    DB::raw('group_concat(products.name) as name'),
Shizuco's avatar
Shizuco committed
                    DB::raw('group_concat(r.id) as retailer_id'));
            }
            $returnQuery->groupBy('sku');
            return $returnQuery;
Shizuco's avatar
Shizuco committed
        }
    }

    /**
     * Get the fields available to the lens.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
Shizuco's avatar
Shizuco committed
    public function fields(NovaRequest $request)
    {
        if (self::getDataFromUrl()) {
            $data = self::getDataFromUrl();
            $fields = [
                Text::make('Name', function () use ($data) {
                    $sku = Product::where('retailer_id', $this['retailer_id'])->where('sku', $this['sku'])->pluck('sku')->first();
                    return Product::where('sku', $sku)->where('retailer_id', $data['retailer'])->pluck('name')->first();
                })
                    ->sortable()
                    ->rules('required', 'max:255', 'min:2'),

                Text::make('SKU')
                    ->sortable()
                    ->rules('numeric'),
            ];
            $existFields = Product::where('retailer_id', $data['retailer'])->pluck('sku')->all();
            $retailers = Retailer::where('id', '!=', $data['retailer'])->pluck('id', 'name')->all();
            $count = 0;
            foreach ($retailers as $retailer => $key) {
                $availeble = Product::where('retailer_id', $key)->whereIn('sku', $existFields)->get();
                if (count($availeble) !== 0) {
                    array_push($fields,
Shizuco's avatar
Shizuco committed
                        Text::make($retailer . ' ' . $data['field'], function () use ($count, $data) {
                            $field = $data['field'];
Shizuco's avatar
Shizuco committed
                            $fields = explode(',', $this[$field]);
Shizuco's avatar
Shizuco committed
                            if (count($fields) < $count + 1) {
                                return null;
                            }
Shizuco's avatar
Shizuco committed
                            return $fields[$count];
                        }));
                }
                $count++;
            }
            return $fields;
Shizuco's avatar
Shizuco committed
        return [];
Shizuco's avatar
Shizuco committed
    }

    /**
     * Get the cards available on the lens.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function cards(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the filters available for the lens.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function filters(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the actions available on the lens.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function actions(NovaRequest $request)
    {
        if (self::getDataFromUrl()) {
            $data = self::getDataFromUrl();
Shizuco's avatar
Shizuco committed
            $retailers = self::getRetailers();
Shizuco's avatar
Shizuco committed
                \App\Nova\Actions\ChangeValue::make($data['field'], $data['retailer'], $retailers)
                ->confirmText('This action is used to change the value of the previously selected field in the previously selected retailer to the value of the field in the newly selected retailer')
                ->confirmButtonText('Change')
                ->cancelButtonText("Don't change"),
Shizuco's avatar
Shizuco committed
    }

    /**
     * Get the URI key for the lens.
     *
     * @return string
     */
    public function uriKey()
    {
        return 'compare-product';
    }
Shizuco's avatar
Shizuco committed
    public static function getDataFromUrl()
Shizuco's avatar
Shizuco committed
        if (!array_key_exists('query', parse_url(url()->previous()))) {
            return false;
        }
        parse_str(parse_url(url()->previous())['query'], $data);
        return $data;
    }
Shizuco's avatar
Shizuco committed

    public static function getRetailers()
    {
        $data = self::getDataFromUrl();
        $trueRetailers = [];
        $existFields = Product::where('retailer_id', $data['retailer'])->pluck('sku')->all();
        $retailers = Retailer::where('id', '!=', $data['retailer'])->pluck('id', 'name')->all();
        $count = 0;
        foreach ($retailers as $retailer => $key) {
            $availeble = Product::where('retailer_id', $key)->whereIn('sku', $existFields)->get();
            if (count($availeble) !== 0) {
                array_push($trueRetailers, $key);
            }
            $count++;
        }
        return $trueRetailers;
    }
Shizuco's avatar
Shizuco committed
}