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

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
{

    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
            return $request->withOrdering($request->withFilters(
                $query->where('retailer_id', '!=', $data['retailer'])->whereIn('sku', $existFields)->orderBy('retailer_id', 'DESC')
Shizuco's avatar
Shizuco committed
            ));
        }
        return $request->withOrdering($request->withFilters(
            $query->where('id', -1)
        ));
    }

    /**
     * Get the fields available to the lens.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function fields(NovaRequest $request)
    {
        if (self::getDataFromUrl()) {
Shizuco's avatar
Shizuco committed
            return [
                Text::make('Name')
                    ->sortable()
                    ->rules('required', 'max:255', 'min:2'),

                Select::make('Retailer', 'retailer_id')
                    ->options(
                        Retailer::pluck('name', 'id')->all()
                    )
                    ->rules('required')
                    ->placeholder('Choose retailer')
                    ->displayUsingLabels(),

                Text::make('SKU')
                    ->sortable()
                    ->rules('numeric'),

                Text::make(ucfirst(self::getDataFromUrl()['field']))
Shizuco's avatar
Shizuco committed
                    ->sortable()
                    ->rules('required'),
            ];
        }
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();
            return [
                new \App\Nova\Actions\ChangeValue($data['field'], $data['retailer']),
            ];
        } else {
Shizuco's avatar
Shizuco committed
            return [
                \App\Nova\Actions\CompareActionChangeValues::make()->standalone(),
Shizuco's avatar
Shizuco committed
    }

    /**
     * Get the URI key for the lens.
     *
     * @return string
     */
    public function uriKey()
    {
        return 'compare-product';
    }

    private static function getDataFromUrl()
    {
        if(count(parse_url(url()->previous())) < 5)
        {
            return false;
        }
        parse_str(parse_url(url()->previous())['query'], $data);
        return $data;
    }
Shizuco's avatar
Shizuco committed
}