78 lines
2.1 KiB
PHP
Raw Normal View History

2022-06-01 10:15:55 +03:00
<?php
namespace App\View\Components\Form\Group;
use App\Abstracts\View\Components\Form;
use App\Models\Common\Contact as Model;
2022-07-25 18:40:05 +03:00
use Illuminate\Support\Str;
2022-06-01 10:15:55 +03:00
class Contact extends Form
{
public $type = 'contact';
public $label;
public $view = 'components.form.group.contact';
public $path;
public $remoteAction;
public $contacts;
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
if (empty($this->name)) {
$this->name = 'contact_id';
}
2022-07-25 18:40:05 +03:00
$this->setRoutes();
$this->label = trans_choice('general.' . Str::plural($this->type), 1);
$this->contacts = Model::type($this->type)->enabled()->orderBy('name')->take(setting('default.select_limit'))->pluck('name', 'id');
2022-06-01 10:15:55 +03:00
2022-07-25 18:40:05 +03:00
$model = $this->getParentData('model');
2022-06-01 10:15:55 +03:00
$contact_id = old('contact.id', old('contact_id', null));
if (! empty($contact_id)) {
$this->selected = $contact_id;
if (! $this->contacts->has($contact_id)) {
$contact = Model::find($contact_id);
$this->contacts->put($contact->id, $contact->name);
}
}
if (! empty($model) && ! empty($model->contact_id)) {
2022-07-25 18:40:05 +03:00
$this->selected = $model->contact_id;
$selected_contact = $model->contact;
}
if (! empty($selected_contact) && ! $this->contacts->has($selected_contact->id)) {
$this->contacts->put($selected_contact->id, $selected_contact->name);
2022-06-01 10:15:55 +03:00
}
2022-07-25 18:40:05 +03:00
return view($this->view);
2022-06-01 10:15:55 +03:00
}
2022-07-25 18:40:05 +03:00
public function setRoutes(): void
2022-06-01 10:15:55 +03:00
{
2022-07-25 18:40:05 +03:00
$alias = config('type.contact.' . $this->type . '.alias');
$prefix = config('type.contact.' . $this->type . '.route.prefix');
2022-06-01 10:15:55 +03:00
2022-07-25 18:40:05 +03:00
$parameters = ['search' => 'enabled:1'];
2022-06-01 10:15:55 +03:00
2022-07-25 18:40:05 +03:00
$this->path = ! empty($alias) ? route("{$alias}.modals.{$prefix}.create") : route("modals.{$prefix}.create");
$this->remoteAction = ! empty($alias) ? route("{$alias}.{$prefix}.index", $parameters) : route("{$prefix}.index", $parameters);
2022-06-01 10:15:55 +03:00
}
}