HEX
Server: LiteSpeed
System: Linux sv4.hami.host 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64
User: armgroup (1008)
PHP: 8.2.32
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open, mail, socket_create, socket_create_listen, socket_create_pair, link, dl, openlog, syslog, stream_socket_server, curl_multi_init
Upload Files
File: /home/armgroup/libs/app/Http/Livewire/Admin/AttributeGroup/Edit.php
<?php

namespace App\Http\Livewire\Admin\AttributeGroup;

use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Http\Livewire\General;
use App\Models\Attribute;
use App\Models\AttributeGroup;
use App\Models\Category;
use App\Models\Product;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Livewire\Component;

class Edit extends Component
{
    use ConvertEmptyStringsToNull, General;

    public AttributeGroup $attributeGroup;

    public $attributes = [];

    public $category_id = [];

    public $categories;

    public $deleteIds = [];

    public function mount()
    {
        $this->categories = Category::doesntHave('children')
            ->where('type', Product::class)
            ->select('id', 'name')
            ->get();
        $this->attributes = $this->attributeGroup
            ->attributes()
            ->select('id', 'group_id', 'name', 'sort_order', 'highlight')
            ->get()
            ->toArray();
        $this->category_id = $this->attributeGroup
            ->categories()
            ->pluck('id')
            ->toArray();
    }

    public function render()
    {
        return view('livewire.admin.attribute-group.edit');
    }

    protected function rules()
    {
        return [
            'attributeGroup.name' => 'required|max:250|string',
            'attributeGroup.label' => 'nullable|max:250|string',
            'attributeGroup.sort_order' => 'nullable|integer|min:0|max:10000',
            'attributes.*.name' => 'required|max:250|string|distinct',
            'attributes.*.highlight' => 'required|boolean',
            'attributes.*.sort_order' => 'nullable|integer|min:0|max:10000',
            'category_id.*' => ['required', Rule::exists('categories', 'id')->where(function ($query) {
                return $query->where('type', Product::class);
            })],
        ];
    }

    protected $validationAttributes = [
        'attributes.*.name' => 'ویژگی',
        'attributes.*.highlight' => 'برجسته',
        'attributes.*.sort_order' => 'ترتیپ',
    ];

    public function addItem()
    {
        array_push($this->attributes, [
            'id' => null,
            'group_id' => $this->attributeGroup->id,
            'name' => null,
            'sort_order' => null,
            'highlight' => false,
        ]);
    }

    public function removeItem($index)
    {
        if ($this->attributes[$index]['id']) {
            $this->deleteIds[] = $this->attributes[$index]['id'];
        }

        unset($this->attributes[$index]);
    }

    public function update()
    {
        $this->validate();
        DB::transaction(function () {
            $this->attributeGroup->update([
                'name' => $this->attributeGroup->name,
                'label' => $this->attributeGroup->label,
                'sort_order' => $this->attributeGroup->sort_order,
            ]);
            if (! count($this->attributes)) {
                $this->attributeGroup->attributes()->delete();
            } else {
                if (count($this->deleteIds)) {
                    Attribute::whereIn('id', $this->deleteIds)->delete();
                }

                Attribute::upsert(
                    $this->attributes,
                    ['id', 'group_id'],
                    ['name', 'sort_order', 'highlight']
                );
            }
            if (count($this->category_id)) {
                $this->attributeGroup->categories()->sync($this->category_id);
            } else {
                $this->attributeGroup->categories()->detach();
            }
            saveActivity("گروه ویژگی {$this->attributeGroup->name} ویرایش شد", $this->attributeGroup);
            $this->doneMessage("گروه ویژگی {$this->attributeGroup->name} با موفقیت ویرایش شد");
        });

        return redirect()->route('admin.attribute-groups.index');
    }
}