first commit
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Clients\Controllers;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use App\Modules\Clients\Models\ClientModel;
|
||||
|
||||
class Clients extends BaseController
|
||||
{
|
||||
protected $clientModel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->clientModel = new ClientModel();
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$config = $this->getTableConfig();
|
||||
$data = $this->prepareTableData($config);
|
||||
|
||||
$data['title'] = 'Клиенты';
|
||||
|
||||
return $this->renderTwig($config['viewPath'], $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Конфигурация таблицы клиентов
|
||||
*/
|
||||
protected function getTableConfig(): array
|
||||
{
|
||||
$organizationId = session()->get('active_org_id');
|
||||
|
||||
return [
|
||||
'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',
|
||||
'viewPath' => '@Clients/index',
|
||||
'partialPath' => '@Clients/_table',
|
||||
'itemsKey' => 'clients',
|
||||
'scope' => function ($builder) use ($organizationId) {
|
||||
$builder->where('organization_id', $organizationId);
|
||||
},
|
||||
];
|
||||
}
|
||||
public function table()
|
||||
{
|
||||
return parent::table();
|
||||
}
|
||||
|
||||
public function new()
|
||||
{
|
||||
$data = [
|
||||
'title' => 'Добавить клиента',
|
||||
'client' => null,
|
||||
];
|
||||
|
||||
return $this->renderTwig('@Clients/form', $data);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$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)
|
||||
{
|
||||
$organizationId = session()->get('active_org_id');
|
||||
|
||||
$client = $this->clientModel
|
||||
->where('id', $id)
|
||||
->where('organization_id', $organizationId)
|
||||
->first();
|
||||
|
||||
if (!$client) {
|
||||
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('Клиент не найден');
|
||||
}
|
||||
|
||||
$data = [
|
||||
'title' => 'Редактировать клиента',
|
||||
'client' => $client,
|
||||
];
|
||||
|
||||
return $this->renderTwig('@Clients/form', $data);
|
||||
}
|
||||
|
||||
public function update($id)
|
||||
{
|
||||
$organizationId = session()->get('active_org_id');
|
||||
|
||||
// Проверяем что клиент принадлежит организации
|
||||
$client = $this->clientModel
|
||||
->where('id', $id)
|
||||
->where('organization_id', $organizationId)
|
||||
->first();
|
||||
|
||||
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)
|
||||
{
|
||||
$organizationId = session()->get('active_org_id');
|
||||
|
||||
// Проверяем что клиент принадлежит организации
|
||||
$client = $this->clientModel
|
||||
->where('id', $id)
|
||||
->where('organization_id', $organizationId)
|
||||
->first();
|
||||
|
||||
if (!$client) {
|
||||
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('Клиент не найден');
|
||||
}
|
||||
|
||||
$this->clientModel->delete($id);
|
||||
|
||||
session()->setFlashdata('success', 'Клиент удалён');
|
||||
return redirect()->to('/clients');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user