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

143 lines
4.7 KiB
PHP
Raw Normal View History

2018-07-17 13:28:34 +00:00
<?php
namespace Webkul\Attribute\Repositories;
use Webkul\Core\Eloquent\Repository;
2018-07-27 06:22:12 +00:00
use Webkul\Attribute\Repositories\AttributeRepository;
2018-07-17 13:28:34 +00:00
use Webkul\Attribute\Repositories\AttributeGroupRepository;
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 AttributeFamilyRepository extends Repository
{
2018-07-27 06:22:12 +00:00
/**
* AttributeRepository object
*
* @var array
*/
protected $attribute;
2018-07-17 13:28:34 +00:00
/**
* AttributeGroupRepository object
*
* @var array
*/
protected $attributeGroup;
/**
* Create a new controller instance.
*
2018-07-27 06:22:12 +00:00
* @param Webkul\Attribute\Repositories\AttributeRepository $attribute
* @param Webkul\Attribute\Repositories\AttributeGroupRepository $attributeGroup
2018-07-17 13:28:34 +00:00
* @return void
*/
2018-07-27 06:22:12 +00:00
public function __construct(AttributeRepository $attribute, AttributeGroupRepository $attributeGroup, App $app)
2018-07-17 13:28:34 +00:00
{
2018-07-27 06:22:12 +00:00
$this->attribute = $attribute;
2018-07-17 13:28:34 +00:00
$this->attributeGroup = $attributeGroup;
parent::__construct($app);
}
/**
* Specify Model class name
*
* @return mixed
*/
function model()
{
return 'Webkul\Attribute\Models\AttributeFamily';
}
/**
* @param array $data
* @return mixed
*/
public function create(array $data)
{
2018-07-27 06:22:12 +00:00
$attributeGroups = isset($data['attribute_groups']) ? $data['attribute_groups'] : [];
unset($data['attribute_groups']);
2018-07-17 13:28:34 +00:00
$family = $this->model->create($data);
2018-07-27 06:22:12 +00:00
foreach ($attributeGroups as $group) {
2018-08-09 04:35:13 +00:00
$custom_attributes = isset($group['custom_attributes']) ? $group['custom_attributes'] : [];
unset($group['custom_attributes']);
2018-07-27 06:22:12 +00:00
$attributeGroup = $family->attribute_groups()->create($group);
2018-07-24 11:11:32 +00:00
2018-08-09 04:35:13 +00:00
foreach ($custom_attributes as $attribute) {
2018-07-27 06:22:12 +00:00
if(isset($attribute['id'])) {
2018-08-09 04:35:13 +00:00
$attributeGroup->custom_attributes()->attach($attribute['id']);
2018-07-27 06:22:12 +00:00
} else {
$attributeModel = $this->attribute->findBy('code', $attribute['code']);
2018-08-09 04:35:13 +00:00
$attributeGroup->custom_attributes()->save($attributeModel, ['position' => $attribute['position']]);
2018-07-24 11:11:32 +00:00
}
}
}
return $family;
2018-07-17 13:28:34 +00:00
}
/**
* @param array $data
* @param $id
* @param string $attribute
* @return mixed
*/
public function update(array $data, $id, $attribute = "id")
{
$family = $this->findOrFail($id);
$family->update($data);
2018-07-24 11:11:32 +00:00
$previousAttributeGroupIds = $family->attribute_groups()->pluck('id');
if(isset($data['attribute_groups'])) {
foreach ($data['attribute_groups'] as $attributeGroupId => $attributeGroupInputs) {
if (str_contains($attributeGroupId, 'group_')) {
$attributeGroup = $family->attribute_groups()->create($attributeGroupInputs);
2018-08-09 04:35:13 +00:00
if(isset($attributeGroupInputs['custom_attributes'])) {
foreach ($attributeGroupInputs['custom_attributes'] as $attribute) {
$attributeGroup->custom_attributes()->attach($attribute['id']);
2018-07-24 11:11:32 +00:00
}
}
} else {
2018-07-27 06:22:12 +00:00
if(is_numeric($index = $previousAttributeGroupIds->search($attributeGroupId))) {
2018-07-24 11:11:32 +00:00
$previousAttributeGroupIds->forget($index);
}
$attributeGroup = $this->attributeGroup->findOrFail($attributeGroupId);
$attributeGroup->update($attributeGroupInputs);
2018-07-27 06:22:12 +00:00
2018-08-09 04:35:13 +00:00
$attributeIds = $attributeGroup->custom_attributes()->get()->pluck('id');
2018-07-24 11:11:32 +00:00
2018-08-09 04:35:13 +00:00
if(isset($attributeGroupInputs['custom_attributes'])) {
foreach ($attributeGroupInputs['custom_attributes'] as $attribute) {
2018-07-27 06:22:12 +00:00
if(is_numeric($index = $attributeIds->search($attribute['id']))) {
$attributeIds->forget($index);
} else {
2018-08-09 04:35:13 +00:00
$attributeGroup->custom_attributes()->attach($attribute['id']);
2018-07-27 06:22:12 +00:00
}
2018-07-24 11:11:32 +00:00
}
}
2018-07-27 06:22:12 +00:00
if($attributeIds->count()) {
2018-08-09 04:35:13 +00:00
$attributeGroup->custom_attributes()->detach($attributeIds);
2018-07-24 11:11:32 +00:00
}
}
}
}
foreach ($previousAttributeGroupIds as $attributeGroupId) {
$this->attributeGroup->delete($attributeGroupId);
}
2018-07-17 13:28:34 +00:00
return $family;
}
}