sarga/packages/Webkul/Attribute/src/Repositories/AttributeRepository.php

268 lines
7.5 KiB
PHP
Raw Normal View History

2020-01-21 09:43:01 +00:00
<?php
namespace Webkul\Attribute\Repositories;
use Webkul\Core\Eloquent\Repository;
use Illuminate\Support\Facades\Event;
use Webkul\Attribute\Repositories\AttributeOptionRepository;
use Illuminate\Container\Container as App;
use Illuminate\Support\Str;
/**
* Attribute Reposotory
*
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class AttributeRepository extends Repository
{
/**
* AttributeOptionRepository object
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Attribute\Repositories\AttributeOptionRepository
2020-01-21 09:43:01 +00:00
*/
protected $attributeOptionRepository;
/**
* Create a new repository instance.
*
2020-03-05 05:34:57 +00:00
* @param \Webkul\Attribute\Repositories\AttributeOptionRepository $attributeOptionRepository
2020-01-21 09:43:01 +00:00
* @return void
*/
public function __construct(
AttributeOptionRepository $attributeOptionRepository,
App $app
)
{
$this->attributeOptionRepository = $attributeOptionRepository;
parent::__construct($app);
}
/**
* Specify Model class name
*
* @return mixed
*/
function model()
{
return 'Webkul\Attribute\Contracts\Attribute';
}
/**
2020-03-05 05:34:57 +00:00
* @param array $data
* @return \Webkul\Attribute\Contracts\Attribute
2020-01-21 09:43:01 +00:00
*/
public function create(array $data)
{
Event::dispatch('catalog.attribute.create.before');
$data = $this->validateUserInput($data);
$options = isset($data['options']) ? $data['options'] : [];
2020-02-19 11:39:17 +00:00
2020-01-21 09:43:01 +00:00
unset($data['options']);
2020-02-19 11:39:17 +00:00
2020-01-21 09:43:01 +00:00
$attribute = $this->model->create($data);
if (in_array($attribute->type, ['select', 'multiselect', 'checkbox']) && count($options)) {
foreach ($options as $optionInputs) {
$this->attributeOptionRepository->create(array_merge([
2020-03-04 06:32:53 +00:00
'attribute_id' => $attribute->id,
2020-02-27 08:03:03 +00:00
], $optionInputs));
2020-01-21 09:43:01 +00:00
}
}
Event::dispatch('catalog.attribute.create.after', $attribute);
return $attribute;
}
/**
2020-03-05 05:34:57 +00:00
* @param array $data
* @param int $id
* @param string $attribute
* @return \Webkul\Attribute\Contracts\Attribute
2020-01-21 09:43:01 +00:00
*/
public function update(array $data, $id, $attribute = "id")
{
$data = $this->validateUserInput($data);
$attribute = $this->find($id);
Event::dispatch('catalog.attribute.update.before', $id);
$attribute->update($data);
$previousOptionIds = $attribute->options()->pluck('id');
if (in_array($attribute->type, ['select', 'multiselect', 'checkbox'])) {
if (isset($data['options'])) {
foreach ($data['options'] as $optionId => $optionInputs) {
if (Str::contains($optionId, 'option_')) {
$this->attributeOptionRepository->create(array_merge([
2020-02-27 08:03:03 +00:00
'attribute_id' => $attribute->id,
], $optionInputs));
2020-01-21 09:43:01 +00:00
} else {
if (is_numeric($index = $previousOptionIds->search($optionId))) {
$previousOptionIds->forget($index);
}
$this->attributeOptionRepository->update($optionInputs, $optionId);
}
}
}
}
foreach ($previousOptionIds as $optionId) {
$this->attributeOptionRepository->delete($optionId);
}
Event::dispatch('catalog.attribute.update.after', $attribute);
return $attribute;
}
/**
2020-03-05 05:34:57 +00:00
* @param int $id
2020-01-21 09:43:01 +00:00
* @return void
*/
public function delete($id)
{
Event::dispatch('catalog.attribute.delete.before', $id);
parent::delete($id);
Event::dispatch('catalog.attribute.delete.after', $id);
}
/**
2020-03-05 05:34:57 +00:00
* @param array $data
2020-01-21 09:43:01 +00:00
* @return array
*/
public function validateUserInput($data)
{
if ($data['is_configurable']) {
$data['value_per_channel'] = $data['value_per_locale'] = 0;
}
2020-01-28 10:36:25 +00:00
if (! in_array($data['type'], ['select', 'multiselect', 'price', 'checkbox'])) {
2020-01-21 09:43:01 +00:00
$data['is_filterable'] = 0;
}
if (in_array($data['type'], ['select', 'multiselect', 'boolean'])) {
unset($data['value_per_locale']);
}
return $data;
}
/**
* @return array
*/
public function getFilterAttributes()
{
return $this->model->where('is_filterable', 1)->with('options')->get();
}
/**
2020-03-05 05:34:57 +00:00
*
* @param array $codes
2020-01-21 09:43:01 +00:00
* @return array
*/
public function getProductDefaultAttributes($codes = null)
{
$attributeColumns = ['id', 'code', 'value_per_channel', 'value_per_locale', 'type', 'is_filterable'];
if (! is_array($codes) && ! $codes)
return $this->findWhereIn('code', [
'name',
'description',
'short_description',
'url_key',
'price',
'special_price',
'special_price_from',
'special_price_to',
2020-03-04 06:32:53 +00:00
'status',
2020-01-21 09:43:01 +00:00
], $attributeColumns);
2020-02-19 11:39:17 +00:00
if (in_array('*', $codes)) {
2020-01-21 09:43:01 +00:00
return $this->all($attributeColumns);
2020-02-19 11:39:17 +00:00
}
2020-01-21 09:43:01 +00:00
return $this->findWhereIn('code', $codes, $attributeColumns);
}
/**
2020-03-05 05:34:57 +00:00
* @param string $code
* @return \Webkul\Attribute\Contracts\Attribute
2020-01-21 09:43:01 +00:00
*/
public function getAttributeByCode($code)
{
static $attributes = [];
2020-02-19 11:39:17 +00:00
if (array_key_exists($code, $attributes)) {
2020-01-21 09:43:01 +00:00
return $attributes[$code];
2020-02-19 11:39:17 +00:00
}
2020-01-21 09:43:01 +00:00
return $attributes[$code] = $this->findOneByField('code', $code);
}
/**
2020-03-05 05:34:57 +00:00
* @param \Webkul\Attribute\Contracts\AttributeFamily $attributeFamily
* @return \Webkul\Attribute\Contracts\Attribute
2020-01-21 09:43:01 +00:00
*/
public function getFamilyAttributes($attributeFamily)
{
static $attributes = [];
2020-02-19 11:39:17 +00:00
if (array_key_exists($attributeFamily->id, $attributes)) {
2020-01-21 09:43:01 +00:00
return $attributes[$attributeFamily->id];
2020-02-19 11:39:17 +00:00
}
2020-01-21 09:43:01 +00:00
return $attributes[$attributeFamily->id] = $attributeFamily->custom_attributes;
}
/**
2020-03-05 05:34:57 +00:00
* @return array
2020-01-21 09:43:01 +00:00
*/
public function getPartial()
{
$attributes = $this->model->all();
2020-03-05 05:34:57 +00:00
$trimmed = [];
2020-01-21 09:43:01 +00:00
foreach($attributes as $key => $attribute) {
if ($attribute->code != 'tax_category_id'
&& (
$attribute->type == 'select'
|| $attribute->type == 'multiselect'
|| $attribute->code == 'sku'
2020-02-28 10:25:53 +00:00
)) {
2020-01-21 09:43:01 +00:00
if ($attribute->options()->exists()) {
array_push($trimmed, [
2020-02-19 11:39:17 +00:00
'id' => $attribute->id,
'name' => $attribute->admin_name,
'type' => $attribute->type,
'code' => $attribute->code,
2020-01-21 09:43:01 +00:00
'has_options' => true,
2020-03-04 06:32:53 +00:00
'options' => $attribute->options,
2020-01-21 09:43:01 +00:00
]);
} else {
array_push($trimmed, [
2020-02-19 11:39:17 +00:00
'id' => $attribute->id,
'name' => $attribute->admin_name,
'type' => $attribute->type,
'code' => $attribute->code,
2020-01-21 09:43:01 +00:00
'has_options' => false,
2020-03-04 06:32:53 +00:00
'options' => null,
2020-01-21 09:43:01 +00:00
]);
}
}
}
return $trimmed;
}
}