Skip to content

Commit

Permalink
fix: specific attribute updates added
Browse files Browse the repository at this point in the history
  • Loading branch information
devansh-webkul committed Aug 21, 2024
1 parent c357c9e commit a7a5767
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Container\Container;
use Illuminate\Support\Facades\Storage;
use Webkul\Attribute\Contracts\AttributeValue;
use Webkul\Core\Eloquent\Repository;

class AttributeValueRepository extends Repository
Expand All @@ -21,29 +22,20 @@ public function __construct(
}

/**
* Specify Model class name
* Specify model class name.
*
* @return mixed
*/
public function model()
{
return 'Webkul\Attribute\Contracts\AttributeValue';
return AttributeValue::class;
}

/**
* @param int $entityId
* @return void
* Save attribute value.
*/
public function save(array $data, $entityId)
public function save(array $data, $attributes = []): void
{
$conditions = ['entity_type' => $data['entity_type']];

if (isset($data['quick_add'])) {
$conditions['quick_add'] = 1;
}

$attributes = $this->attributeRepository->findWhere($conditions);

foreach ($attributes as $attribute) {
$typeColumn = $this->model::$attributeTypeFields[$attribute->type];

Expand Down Expand Up @@ -76,20 +68,20 @@ public function save(array $data, $entityId)

if ($attribute->type === 'image' || $attribute->type === 'file') {
$data[$attribute->code] = gettype($data[$attribute->code]) === 'object'
? request()->file($attribute->code)->store($data['entity_type'].'/'.$entityId)
? request()->file($attribute->code)->store($data['entity_type'].'/'.$data['entity_id'])
: null;
}

$attributeValue = $this->findOneWhere([
'entity_id' => $entityId,
'entity_type' => $data['entity_type'],
'entity_id' => $data['entity_id'],
'attribute_id' => $attribute->id,
]);

if (! $attributeValue) {
$this->create([
'entity_id' => $entityId,
'entity_type' => $data['entity_type'],
'entity_id' => $data['entity_id'],
'attribute_id' => $attribute->id,
$typeColumn => $data[$attribute->code],
]);
Expand All @@ -106,6 +98,8 @@ public function save(array $data, $entityId)
}

/**
* Is value unique.
*
* @param int $entityId
* @param string $entityType
* @param \Webkul\Attribute\Contracts\Attribute $attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Illuminate\Container\Container;
use Illuminate\Support\Facades\DB;
use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Attribute\Repositories\AttributeValueRepository;
use Webkul\Contact\Contracts\Organization;
use Webkul\Core\Eloquent\Repository;

class OrganizationRepository extends Repository
Expand All @@ -15,23 +17,26 @@ class OrganizationRepository extends Repository
* @return void
*/
public function __construct(
protected AttributeRepository $attributeRepository,
protected AttributeValueRepository $attributeValueRepository,
Container $container
) {
parent::__construct($container);
}

/**
* Specify Model class name
* Specify model class name.
*
* @return mixed
*/
public function model()
{
return 'Webkul\Contact\Contracts\Organization';
return Organization::class;
}

/**
* Create.
*
* @return \Webkul\Contact\Contracts\Organization
*/
public function create(array $data)
Expand All @@ -42,25 +47,62 @@ public function create(array $data)

$organization = parent::create($data);

$this->attributeValueRepository->save($data, $organization->id);
$conditions = ['entity_type' => $data['entity_type']];

if (isset($data['quick_add'])) {
$conditions['quick_add'] = 1;
}

$attributes = $this->attributeRepository->where($conditions)->get();

$this->attributeValueRepository->save(array_merge($data, [
'entity_id' => $organization->id,
]), $attributes);

return $organization;
}

/**
* Update.
*
* @param int $id
* @param string $attribute
* @param array $attribute
* @return \Webkul\Contact\Contracts\Organization
*/
public function update(array $data, $id, $attribute = 'id')
public function update(array $data, $id, $attributes = [])
{
if (isset($data['user_id'])) {
$data['user_id'] = $data['user_id'] ?: null;
}

$organization = parent::update($data, $id);

$this->attributeValueRepository->save($data, $id);
$conditions = ['entity_type' => $data['entity_type']];

if (isset($data['quick_add'])) {
$conditions['quick_add'] = 1;
}

/**
* If attributes are provided then only save the provided attributes and return.
*/
if (! empty($attributes)) {
$attributes = $this->attributeRepository->where($conditions)
->whereIn('code', $attributes)
->get();

$this->attributeValueRepository->save(array_merge($data, [
'entity_id' => $organization->id,
]), $attributes);

return $organization;
}

$attributes = $this->attributeRepository->where($conditions)->get();

$this->attributeValueRepository->save(array_merge($data, [
'entity_id' => $organization->id,
]), $attributes);

return $organization;
}
Expand Down
54 changes: 47 additions & 7 deletions packages/Webkul/Contact/src/Repositories/PersonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Webkul\Contact\Repositories;

use Illuminate\Container\Container;
use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Attribute\Repositories\AttributeValueRepository;
use Webkul\Contact\Contracts\Person;
use Webkul\Core\Eloquent\Repository;

class PersonRepository extends Repository
Expand All @@ -25,23 +27,26 @@ class PersonRepository extends Repository
* @return void
*/
public function __construct(
protected AttributeRepository $attributeRepository,
protected AttributeValueRepository $attributeValueRepository,
Container $container
) {
parent::__construct($container);
}

/**
* Specify Model class name
* Specify model class name.
*
* @return mixed
*/
public function model()
{
return 'Webkul\Contact\Contracts\Person';
return Person::class;
}

/**
* Create.
*
* @return \Webkul\Contact\Contracts\Person
*/
public function create(array $data)
Expand All @@ -52,31 +57,66 @@ public function create(array $data)

$person = parent::create($data);

$this->attributeValueRepository->save($data, $person->id);
$conditions = ['entity_type' => $data['entity_type']];

if (isset($data['quick_add'])) {
$conditions['quick_add'] = 1;
}

$attributes = $this->attributeRepository->where($conditions)->get();

$this->attributeValueRepository->save(array_merge($data, [
'entity_id' => $person->id,
]), $attributes);

return $person;
}

/**
* @param int $id
* @param string $attribute
* @param array $attribute
* @return \Webkul\Contact\Contracts\Person
*/
public function update(array $data, $id, $attribute = 'id')
public function update(array $data, $id, $attributes = [])
{
if (isset($data['user_id'])) {
$data['user_id'] = $data['user_id'] ?: null;
}

$person = parent::update($data, $id);

$this->attributeValueRepository->save($data, $id);
$conditions = ['entity_type' => $data['entity_type']];

if (isset($data['quick_add'])) {
$conditions['quick_add'] = 1;
}

/**
* If attributes are provided then only save the provided attributes and return.
*/
if (! empty($attributes)) {
$attributes = $this->attributeRepository->where($conditions)
->whereIn('code', $attributes)
->get();

$this->attributeValueRepository->save(array_merge($data, [
'entity_id' => $person->id,
]), $attributes);

return $person;
}

$attributes = $this->attributeRepository->where($conditions)->get();

$this->attributeValueRepository->save(array_merge($data, [
'entity_id' => $person->id,
]), $attributes);

return $person;
}

/**
* Retrieves customers count based on date
* Retrieves customers count based on date.
*
* @return number
*/
Expand Down
Loading

0 comments on commit a7a5767

Please sign in to comment.