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

213 lines
5.6 KiB
PHP
Raw Normal View History

2019-02-18 07:30:40 +00:00
<?php
2018-07-17 13:28:34 +00:00
namespace Webkul\Attribute\Repositories;
2019-02-18 07:30:40 +00:00
2018-07-17 13:28:34 +00:00
use Webkul\Core\Eloquent\Repository;
use Illuminate\Support\Facades\Event;
2018-07-17 13:28:34 +00:00
use Webkul\Attribute\Repositories\AttributeOptionRepository;
use Illuminate\Container\Container as App;
/**
* Attribute Reposotory
*
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class AttributeRepository extends Repository
{
/**
* AttributeOptionRepository object
*
* @var array
*/
protected $attributeOption;
/**
* Create a new controller instance.
*
* @param Webkul\Attribute\Repositories\AttributeOptionRepository $attributeOption
* @return void
*/
public function __construct(AttributeOptionRepository $attributeOption, App $app)
{
$this->attributeOption = $attributeOption;
parent::__construct($app);
}
/**
* Specify Model class name
*
* @return mixed
*/
function model()
{
2019-02-18 07:30:40 +00:00
return 'Webkul\Attribute\Contracts\Attribute';
2018-07-17 13:28:34 +00:00
}
/**
* @param array $data
* @return mixed
*/
public function create(array $data)
{
Event::fire('catalog.attribute.create.before');
2018-08-17 05:48:21 +00:00
$data = $this->validateUserInput($data);
2018-07-27 06:22:12 +00:00
$options = isset($data['options']) ? $data['options'] : [];
unset($data['options']);
2018-07-17 13:28:34 +00:00
$attribute = $this->model->create($data);
2019-01-15 11:54:41 +00:00
if (in_array($attribute->type, ['select', 'multiselect', 'checkbox']) && count($options)) {
2019-02-26 06:25:00 +00:00
foreach ($options as $optionInputs) {
$this->attributeOption->create(array_merge([
'attribute_id' => $attribute->id
], $optionInputs));
2018-07-17 13:28:34 +00:00
}
}
Event::fire('catalog.attribute.create.after', $attribute);
2018-07-17 13:28:34 +00:00
return $attribute;
}
/**
* @param array $data
* @param $id
* @param string $attribute
* @return mixed
*/
public function update(array $data, $id, $attribute = "id")
{
2018-08-17 05:48:21 +00:00
$data = $this->validateUserInput($data);
2018-08-22 09:44:35 +00:00
$attribute = $this->find($id);
2018-07-17 13:28:34 +00:00
Event::fire('catalog.attribute.update.before', $id);
2018-07-17 13:28:34 +00:00
$attribute->update($data);
$previousOptionIds = $attribute->options()->pluck('id');
2019-01-15 11:54:41 +00:00
if (in_array($attribute->type, ['select', 'multiselect', 'checkbox'])) {
if (isset($data['options'])) {
2018-07-17 13:28:34 +00:00
foreach ($data['options'] as $optionId => $optionInputs) {
if (str_contains($optionId, 'option_')) {
2019-02-26 06:25:00 +00:00
$this->attributeOption->create(array_merge([
'attribute_id' => $attribute->id,
], $optionInputs));
2018-07-17 13:28:34 +00:00
} else {
2019-01-15 11:54:41 +00:00
if (is_numeric($index = $previousOptionIds->search($optionId))) {
2018-07-17 13:28:34 +00:00
$previousOptionIds->forget($index);
}
$this->attributeOption->update($optionInputs, $optionId);
}
}
}
}
foreach ($previousOptionIds as $optionId) {
$this->attributeOption->delete($optionId);
}
Event::fire('catalog.attribute.update.after', $attribute);
2018-07-17 13:28:34 +00:00
return $attribute;
}
2018-08-17 05:48:21 +00:00
/**
* @param $id
* @return void
*/
public function delete($id)
{
Event::fire('catalog.attribute.delete.before', $id);
parent::delete($id);
Event::fire('catalog.attribute.delete.after', $id);
}
2018-08-17 05:48:21 +00:00
/**
* @param array $data
* @return array
*/
public function validateUserInput($data)
{
2019-01-15 11:54:41 +00:00
if ($data['is_configurable']) {
2018-08-17 05:48:21 +00:00
$data['value_per_channel'] = $data['value_per_locale'] = 0;
}
2019-01-15 11:54:41 +00:00
if (! in_array($data['type'], ['select', 'multiselect', 'price'])) {
2018-08-17 05:48:21 +00:00
$data['is_filterable'] = 0;
}
2019-04-10 06:40:46 +00:00
if (in_array($data['type'], ['select', 'multiselect', 'boolean'])) {
unset($data['value_per_locale']);
}
2018-08-17 05:48:21 +00:00
return $data;
}
/**
* @return array
*/
public function getFilterAttributes()
{
2019-03-11 12:27:15 +00:00
return $this->model->where('is_filterable', 1)->get();
2018-08-17 05:48:21 +00:00
}
2018-10-12 08:22:06 +00:00
/**
* @return array
*/
public function getProductDefaultAttributes($codes = null)
{
$attributeColumns = ['id', 'code', 'value_per_channel', 'value_per_locale', 'type', 'is_filterable'];
2019-01-15 11:54:41 +00:00
if (! is_array($codes) && !$codes)
2018-10-12 08:22:06 +00:00
return $this->findWhereIn('code', [
'name',
'description',
'short_description',
'url_key',
'price',
'special_price',
'special_price_from',
'special_price_to',
'status'
], $attributeColumns);
2019-02-18 07:30:40 +00:00
2019-01-15 11:54:41 +00:00
if (in_array('*', $codes))
2018-10-12 08:22:06 +00:00
return $this->all($attributeColumns);
return $this->findWhereIn('code', $codes, $attributeColumns);
}
2019-04-18 07:22:51 +00:00
/**
* @return Object
*/
public function getAttributeByCode($code)
{
static $attributes = [];
if (array_key_exists($code, $attributes))
return $attributes[$code];
return $attributes[$code] = $this->findOneByField('code', $code);
}
/**
* @return Object
*/
public function getFamilyAttributes($attributeFamily)
{
static $attributes = [];
if (array_key_exists($attributeFamily->id, $attributes))
return $attributes[$attributeFamily->id];
return $attributes[$attributeFamily->id] = $attributeFamily->custom_attributes;
}
2018-07-17 13:28:34 +00:00
}