223 lines
8.2 KiB
PHP
223 lines
8.2 KiB
PHP
<?php
|
||
|
||
namespace App\Modules\Clients\Controllers;
|
||
|
||
use App\Controllers\BaseController;
|
||
use App\Modules\Clients\Models\ClientModel;
|
||
use App\Services\AccessService;
|
||
|
||
class Clients extends BaseController
|
||
{
|
||
protected ClientModel $clientModel;
|
||
|
||
public function __construct()
|
||
{
|
||
$this->clientModel = new ClientModel();
|
||
}
|
||
|
||
public function index()
|
||
{
|
||
// Проверка права на просмотр
|
||
if (!$this->access->canView('clients')) {
|
||
return $this->forbiddenResponse('У вас нет прав для просмотра клиентов');
|
||
}
|
||
|
||
$config = $this->getTableConfig();
|
||
|
||
return $this->renderTwig('@Clients/index', [
|
||
'title' => 'Клиенты',
|
||
'tableHtml' => $this->renderTable($config),
|
||
'can_create' => $this->access->canCreate('clients'),
|
||
'can_edit' => $this->access->canEdit('clients'),
|
||
'can_delete' => $this->access->canDelete('clients'),
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* Конфигурация таблицы клиентов
|
||
*/
|
||
protected function getTableConfig(): array
|
||
{
|
||
return [
|
||
'id' => 'clients-table',
|
||
'url' => '/clients/table',
|
||
'model' => $this->clientModel,
|
||
'columns' => [
|
||
'name' => ['label' => 'Имя / Название', 'width' => '40%'],
|
||
'email' => ['label' => 'Email', 'width' => '25%'],
|
||
'phone' => ['label' => 'Телефон', 'width' => '20%'],
|
||
],
|
||
'searchable' => ['name', 'email', 'phone'],
|
||
'sortable' => ['name', 'email', 'phone', 'created_at'],
|
||
'defaultSort' => 'name',
|
||
'order' => 'asc',
|
||
'actions' => ['label' => 'Действия', 'width' => '15%'],
|
||
'actionsConfig' => [
|
||
[
|
||
'label' => '',
|
||
'url' => '/clients/edit/{id}',
|
||
'icon' => 'fa-solid fa-pen',
|
||
'class' => 'btn-outline-primary',
|
||
'title' => 'Редактировать',
|
||
'type' => 'edit',
|
||
],
|
||
[
|
||
'label' => '',
|
||
'url' => '/clients/delete/{id}',
|
||
'icon' => 'fa-solid fa-trash',
|
||
'class' => 'btn-outline-danger',
|
||
'title' => 'Удалить',
|
||
'type' => 'delete',
|
||
]
|
||
],
|
||
'emptyMessage' => 'Клиентов пока нет',
|
||
'emptyIcon' => 'fa-solid fa-users',
|
||
'emptyActionUrl' => base_url('/clients/new'),
|
||
'emptyActionLabel'=> 'Добавить клиента',
|
||
'emptyActionIcon' => 'fa-solid fa-plus',
|
||
'can_edit' => $this->access->canEdit('clients'),
|
||
'can_delete' => $this->access->canDelete('clients'),
|
||
];
|
||
}
|
||
|
||
public function table(?array $config = null, ?string $pageUrl = null)
|
||
{
|
||
// Проверка права на просмотр
|
||
if (!$this->access->canView('clients')) {
|
||
return $this->forbiddenResponse('У вас нет прав для просмотра клиентов');
|
||
}
|
||
|
||
return parent::table($config, '/clients');
|
||
}
|
||
|
||
public function new()
|
||
{
|
||
// Проверка права на создание
|
||
if (!$this->access->canCreate('clients')) {
|
||
return $this->forbiddenResponse('У вас нет прав для создания клиентов');
|
||
}
|
||
|
||
$data = [
|
||
'title' => 'Добавить клиента',
|
||
'client' => null,
|
||
];
|
||
|
||
return $this->renderTwig('@Clients/form', $data);
|
||
}
|
||
|
||
public function create()
|
||
{
|
||
// Проверка права на создание
|
||
if (!$this->access->canCreate('clients')) {
|
||
return $this->forbiddenResponse('У вас нет прав для создания клиентов');
|
||
}
|
||
|
||
$organizationId = session()->get('active_org_id');
|
||
|
||
$rules = [
|
||
'name' => 'required|min_length[2]|max_length[255]',
|
||
'email' => 'permit_empty|valid_email',
|
||
'phone' => 'permit_empty|max_length[50]',
|
||
];
|
||
|
||
if (!$this->validate($rules)) {
|
||
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
|
||
}
|
||
|
||
$this->clientModel->insert([
|
||
'organization_id' => $organizationId,
|
||
'name' => $this->request->getPost('name'),
|
||
'email' => $this->request->getPost('email') ?? null,
|
||
'phone' => $this->request->getPost('phone') ?? null,
|
||
'notes' => $this->request->getPost('notes') ?? null,
|
||
]);
|
||
|
||
if ($this->clientModel->errors()) {
|
||
return redirect()->back()->withInput()->with('error', 'Ошибка при создании клиента');
|
||
}
|
||
|
||
session()->setFlashdata('success', 'Клиент успешно добавлен');
|
||
return redirect()->to('/clients');
|
||
}
|
||
|
||
public function edit($id)
|
||
{
|
||
// Проверка права на редактирование
|
||
if (!$this->access->canEdit('clients')) {
|
||
return $this->forbiddenResponse('У вас нет прав для редактирования клиентов');
|
||
}
|
||
|
||
$client = $this->clientModel->forCurrentOrg()->find($id);
|
||
|
||
if (!$client) {
|
||
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('Клиент не найден');
|
||
}
|
||
|
||
$data = [
|
||
'title' => 'Редактировать клиента',
|
||
'client' => $client,
|
||
];
|
||
|
||
return $this->renderTwig('@Clients/form', $data);
|
||
}
|
||
|
||
public function update($id)
|
||
{
|
||
// Проверка права на редактирование
|
||
if (!$this->access->canEdit('clients')) {
|
||
return $this->forbiddenResponse('У вас нет прав для редактирования клиентов');
|
||
}
|
||
|
||
// Проверяем что клиент принадлежит организации через forCurrentOrg()
|
||
$client = $this->clientModel->forCurrentOrg()->find($id);
|
||
|
||
if (!$client) {
|
||
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('Клиент не найден');
|
||
}
|
||
|
||
$rules = [
|
||
'name' => 'required|min_length[2]|max_length[255]',
|
||
'email' => 'permit_empty|valid_email',
|
||
'phone' => 'permit_empty|max_length[50]',
|
||
];
|
||
|
||
if (!$this->validate($rules)) {
|
||
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
|
||
}
|
||
|
||
$this->clientModel->update($id, [
|
||
'name' => $this->request->getPost('name'),
|
||
'email' => $this->request->getPost('email') ?? null,
|
||
'phone' => $this->request->getPost('phone') ?? null,
|
||
'notes' => $this->request->getPost('notes') ?? null,
|
||
]);
|
||
|
||
if ($this->clientModel->errors()) {
|
||
return redirect()->back()->withInput()->with('error', 'Ошибка при обновлении клиента');
|
||
}
|
||
|
||
session()->setFlashdata('success', 'Клиент успешно обновлён');
|
||
return redirect()->to('/clients');
|
||
}
|
||
|
||
public function delete($id)
|
||
{
|
||
// Проверка права на удаление
|
||
if (!$this->access->canDelete('clients')) {
|
||
return $this->forbiddenResponse('У вас нет прав для удаления клиентов');
|
||
}
|
||
|
||
// Проверяем что клиент принадлежит организации через forCurrentOrg()
|
||
$client = $this->clientModel->forCurrentOrg()->find($id);
|
||
|
||
if (!$client) {
|
||
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('Клиент не найден');
|
||
}
|
||
|
||
$this->clientModel->delete($id);
|
||
|
||
session()->setFlashdata('success', 'Клиент удалён');
|
||
return redirect()->to('/clients');
|
||
}
|
||
}
|