Newer
Older
declare (strict_types = 1);
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();
->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'));
DB::raw('group_concat(' . $data['field'] . ') as ' . $data['field']),
DB::raw('group_concat(products.name) as name'),
DB::raw('group_concat(r.id) as retailer_id'));
}
$returnQuery->groupBy('sku');
return $returnQuery;
}
}
/**
* Get the fields available to the lens.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
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,
Text::make($retailer . ' ' . $data['field'], function () use ($count, $data, $key) {
$fields = explode(',', $this[$data['field']]);
if (!array_key_exists($count, $fields)) {
$count = 0;
}
$currentProduct = Product::where('retailer_id', $key)->where('sku', $this->sku)->get();
if (count($currentProduct) !== 0) {
return $fields[$count];
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
}
/**
* 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();
\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"),
}
/**
* Get the URI key for the lens.
*
* @return string
*/
public function uriKey()
{
return 'compare-product';
}
if (!array_key_exists('query', parse_url(url()->previous()))) {
return false;
}
parse_str(parse_url(url()->previous())['query'], $data);
return $data;
}
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;
}