179 lines
6.6 KiB
PHP
179 lines
6.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Class TTCrud
|
|
* @property string $headerTitle
|
|
* @property string $createText
|
|
* @property array $columns
|
|
* @property array $additionalActions
|
|
* @property array $infoMessages
|
|
* @property bool $onlyView
|
|
*/
|
|
class TTCrud extends mfBaseController {
|
|
public User $user;
|
|
private array $checkArray;
|
|
public ?array $postData;
|
|
/** @noinspection PhpMissingFieldTypeInspection */
|
|
public $model;
|
|
|
|
protected function init() {
|
|
$this->needlogin = true;
|
|
$me = new User();
|
|
$me->loadMe();
|
|
$this->user = $me;
|
|
$this->layout()->set('me', $me);
|
|
|
|
if (!$me->is(["Admin"])) {
|
|
$this->redirect("Dashboard");
|
|
}
|
|
|
|
$modelName = $this->mod . 'Model';
|
|
$this->model = new $modelName();
|
|
$this->postData = json_decode(file_get_contents('php://input'), true);
|
|
$this->checkArray = $this->getCheckArray();
|
|
}
|
|
|
|
/**
|
|
* Returns the checkArray for the CRUD component.
|
|
* @return array
|
|
*/
|
|
protected function getCheckArray(): array {
|
|
$checkArray = [];
|
|
|
|
foreach ($this->columns as $column) {
|
|
$checkArray[$column['key']] = ['required' => $column['required'] ?? false,
|
|
'required_length' => $column['required_length'] ?? 0,
|
|
'title' => $column['text'] ?? $column['key'],
|
|
'regex' => $column['regex'] ?? false];
|
|
}
|
|
|
|
return $checkArray;
|
|
}
|
|
|
|
protected function indexAction() {
|
|
$this->layout()->set('additionalJS', ['js/pages/WarehouseHistory/WarehouseHistoryModal.js']);
|
|
$customJsFile = defined('BASEDIR') ? BASEDIR . "/public/js/pages/{$this->mod}/{$this->mod}.js" : null;
|
|
|
|
if ($customJsFile && file_exists($customJsFile)) {
|
|
$pageName = $this->mod;
|
|
} else {
|
|
$pageName = "DefaultCrudView";
|
|
}
|
|
|
|
Helper::renderVue($this, $pageName, $this->headerTitle, ["CRUD_CONFIG" => $this->getCrudConfig(),
|
|
"CREATE_URL" => $this::getUrl($this->mod . "/create"),
|
|
"TABLE_URL" => $this::getUrl($this->mod . "/get"),
|
|
"UPDATE_URL" => $this::getUrl($this->mod . "/update"),
|
|
"DELETE_URL" => $this::getUrl($this->mod . "/delete"),]);
|
|
}
|
|
|
|
/**
|
|
* Returns the configuration for the CRUD component for the Vue component.
|
|
* @return array
|
|
*/
|
|
protected function getCrudConfig(): array {
|
|
if (method_exists($this, 'prepareCrudConfig')) {
|
|
$this->prepareCrudConfig();
|
|
}
|
|
|
|
|
|
$columns = array_map(function ($column) {
|
|
$newColumn = $column;
|
|
unset($newColumn['required'], $newColumn['required_length'], $newColumn['regex']);
|
|
return $newColumn;
|
|
}, $this->columns);
|
|
|
|
return ['key' => $this->mod,
|
|
'tableHeader' => $this->headerTitle,
|
|
'createText' => $this->createText,
|
|
'columns' => $columns,
|
|
'additionalActions' => $this->additionalActions];
|
|
}
|
|
|
|
protected function getAction() {
|
|
$filter = $this->postData['filters'] ?? [];
|
|
$order = $this->postData['order'] ?? ['key' => null, 'order' => 'ASC'];
|
|
$page = $this->postData['pagination']['page'] ?? 1;
|
|
$perPage = $this->postData['pagination']['per_page'] ?? 10;
|
|
|
|
$rows = $this->model::getAll($filter, $perPage, ($page - 1) * $perPage, $order);
|
|
$filteredAvailable = $this->model::count($filter);
|
|
$totalRows = $this->model::count();
|
|
|
|
self::returnJson(["rows" => $rows,
|
|
"pagination" => ["page" => $page,
|
|
"total_pages" => ceil($filteredAvailable / $perPage),
|
|
"per_page" => $perPage,
|
|
"filtered_available" => intval($filteredAvailable),
|
|
"total_rows" => intval($totalRows)]]);
|
|
}
|
|
|
|
protected function createAction() {
|
|
Helper::validateArray($this->postData, $this->checkArray);
|
|
|
|
if (method_exists($this, 'beforeCreate') && !$this->beforeCreate($this->postData)) {
|
|
self::returnJson(['success' => false, 'message' => 'Ein Fehler ist aufgetreten.']);
|
|
}
|
|
|
|
$id = $this->model::create($this->postData);
|
|
|
|
if (method_exists($this, 'afterCreate')) {
|
|
$this->afterCreate($this->postData);
|
|
}
|
|
|
|
self::returnJson(['success' => true,
|
|
'message' => $this->infoMessages['create'],
|
|
'id' => $id]);
|
|
}
|
|
|
|
protected function updateAction() {
|
|
Helper::validateArray($this->postData, array_merge($this->checkArray, ['id' => ['required' => true]]));
|
|
|
|
if (method_exists($this, 'beforeUpdate') && !$this->beforeUpdate($this->postData)) {
|
|
self::returnJson(['success' => false, 'message' => 'Ein Fehler ist aufgetreten.']);
|
|
}
|
|
|
|
$affectedRows = $this->model::update($this->postData);
|
|
|
|
if (method_exists($this, 'afterUpdate')) {
|
|
$this->afterUpdate($this->postData);
|
|
}
|
|
|
|
self::returnJson(['success' => $affectedRows > 0,
|
|
'message' => $affectedRows > 0 ? $this->infoMessages['update'] : $this->infoMessages['noChanges']]);
|
|
}
|
|
|
|
protected function deleteAction() {
|
|
Helper::validateArray($this->postData, ['id' => ['required' => true]]);
|
|
|
|
if (method_exists($this, 'beforeDelete') && !$this->beforeDelete($this->postData)) {
|
|
self::returnJson(['success' => false, 'message' => 'Ein Fehler ist aufgetreten.']);
|
|
}
|
|
|
|
$affectedRows = $this->model::delete($this->postData['id']);
|
|
|
|
self::returnJson(['success' => $affectedRows > 0,
|
|
'message' => $affectedRows > 0 ? $this->infoMessages['delete'] : $this->infoMessages['noChanges']]);
|
|
}
|
|
|
|
protected function autocompleteAction() {
|
|
$searchedID = $this->request->searchedID;
|
|
|
|
$textKey = property_exists($this->model, 'name') ? 'name' : 'title';
|
|
|
|
if (strlen($searchedID) > 0) {
|
|
$filter = ['id' => $searchedID];
|
|
} else {
|
|
$filter = [$textKey => $this->request->q];
|
|
}
|
|
|
|
$data = $this->model::getAll($filter, 10);
|
|
|
|
self::returnJson(array_map(function ($item) use ($textKey) {
|
|
return ['value' => $item->id, 'text' => $item->$textKey];
|
|
}, $data));
|
|
}
|
|
}
|
|
|
|
?>
|