2020-06-09 15:57:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller\Component;
|
|
|
|
|
|
|
|
use Cake\Controller\Component;
|
|
|
|
use Cake\Error\Debugger;
|
2020-08-07 21:47:04 +02:00
|
|
|
use Cake\Utility\Hash;
|
2020-06-09 15:57:16 +02:00
|
|
|
use Cake\Utility\Inflector;
|
2021-01-14 11:33:51 +01:00
|
|
|
use Cake\View\ViewBuilder;
|
2020-06-09 15:57:16 +02:00
|
|
|
|
|
|
|
class CRUDComponent extends Component
|
|
|
|
{
|
|
|
|
|
|
|
|
public function initialize(array $config): void
|
|
|
|
{
|
|
|
|
$this->Controller = $this->getController();
|
|
|
|
$this->Table = $config['table'];
|
|
|
|
$this->request = $config['request'];
|
|
|
|
$this->TableAlias = $this->Table->getAlias();
|
2020-12-07 14:17:10 +01:00
|
|
|
$this->ObjectAlias = Inflector::singularize($this->TableAlias);
|
2020-09-28 01:25:07 +02:00
|
|
|
$this->MetaFields = $config['MetaFields'];
|
|
|
|
$this->MetaTemplates = $config['MetaTemplates'];
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 00:34:51 +02:00
|
|
|
public function index(array $options): void
|
2020-06-09 15:57:16 +02:00
|
|
|
{
|
2020-06-19 00:34:51 +02:00
|
|
|
if (!empty($options['quickFilters'])) {
|
|
|
|
if (empty($options['filters'])) {
|
|
|
|
$options['filters'] = [];
|
|
|
|
}
|
|
|
|
$options['filters'][] = 'quickFilter';
|
|
|
|
}
|
|
|
|
$params = $this->Controller->ParamHandler->harvestParams(empty($options['filters']) ? [] : $options['filters']);
|
2020-06-09 15:57:16 +02:00
|
|
|
$query = $this->Table->find();
|
|
|
|
$query = $this->setFilters($params, $query);
|
2020-06-19 00:34:51 +02:00
|
|
|
$query = $this->setQuickFilters($params, $query, empty($options['quickFilters']) ? [] : $options['quickFilters']);
|
|
|
|
if (!empty($options['contain'])) {
|
|
|
|
$query->contain($options['contain']);
|
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
|
|
|
$data = $query->all();
|
|
|
|
$this->Controller->restResponsePayload = $this->Controller->RestResponse->viewData($data, 'json');
|
|
|
|
} else {
|
|
|
|
$this->Controller->loadComponent('Paginator');
|
|
|
|
$data = $this->Controller->Paginator->paginate($query);
|
2020-12-08 15:08:12 +01:00
|
|
|
if (!empty($options['contextFilters'])) {
|
|
|
|
$this->setFilteringContext($options['contextFilters'], $params);
|
2020-12-07 16:06:01 +01:00
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
$this->Controller->set('data', $data);
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 12:48:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getResponsePayload Returns the adaquate response payload based on the request context
|
|
|
|
*
|
|
|
|
* @return false or Array
|
|
|
|
*/
|
|
|
|
public function getResponsePayload()
|
|
|
|
{
|
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
|
|
|
return $this->Controller->restResponsePayload;
|
|
|
|
} else if ($this->Controller->ParamHandler->isAjax() && $this->request->is(['post', 'put'])) {
|
2021-01-12 10:16:58 +01:00
|
|
|
return $this->Controller->ajaxResponsePayload;
|
2021-01-11 12:48:58 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
|
2020-09-28 01:25:07 +02:00
|
|
|
private function getMetaTemplates()
|
|
|
|
{
|
2020-12-07 09:52:35 +01:00
|
|
|
$metaTemplates = [];
|
2020-09-28 01:25:07 +02:00
|
|
|
if (!empty($this->Table->metaFields)) {
|
|
|
|
$metaQuery = $this->MetaTemplates->find();
|
2020-12-10 17:46:16 +01:00
|
|
|
$metaQuery
|
|
|
|
->order(['is_default' => 'DESC'])
|
|
|
|
->where([
|
|
|
|
'scope' => $this->Table->metaFields,
|
|
|
|
'enabled' => 1
|
|
|
|
]);
|
2020-09-28 01:25:07 +02:00
|
|
|
$metaQuery->contain(['MetaTemplateFields']);
|
|
|
|
$metaTemplates = $metaQuery->all();
|
|
|
|
}
|
2020-12-07 09:52:35 +01:00
|
|
|
$this->Controller->set('metaTemplates', $metaTemplates);
|
2020-09-28 01:25:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-25 01:45:28 +02:00
|
|
|
public function add(array $params = []): void
|
2020-06-09 15:57:16 +02:00
|
|
|
{
|
2020-09-28 01:25:07 +02:00
|
|
|
$this->getMetaTemplates();
|
2020-06-09 15:57:16 +02:00
|
|
|
$data = $this->Table->newEmptyEntity();
|
2020-11-05 10:17:42 +01:00
|
|
|
if (!empty($params['fields'])) {
|
|
|
|
$this->Controller->set('fields', $params['fields']);
|
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
if ($this->request->is('post')) {
|
2021-01-13 14:18:26 +01:00
|
|
|
$patchEntityParams = [
|
|
|
|
'associated' => []
|
|
|
|
];
|
|
|
|
if (!empty($params['id'])) {
|
|
|
|
unset($params['id']);
|
|
|
|
}
|
2020-11-05 10:17:42 +01:00
|
|
|
$input = $this->__massageInput($params);
|
|
|
|
if (!empty($params['fields'])) {
|
|
|
|
$patchEntityParams['fields'] = $params['fields'];
|
2020-06-25 01:45:28 +02:00
|
|
|
}
|
2020-11-05 10:17:42 +01:00
|
|
|
$data = $this->Table->patchEntity($data, $input, $patchEntityParams);
|
2020-12-15 10:40:49 +01:00
|
|
|
$savedData = $this->Table->save($data);
|
|
|
|
if ($savedData !== false) {
|
2020-06-19 00:34:51 +02:00
|
|
|
$message = __('{0} added.', $this->ObjectAlias);
|
2020-09-28 01:25:07 +02:00
|
|
|
if (!empty($input['metaFields'])) {
|
|
|
|
$this->saveMetaFields($data->id, $input);
|
|
|
|
}
|
2020-06-19 00:34:51 +02:00
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
2021-01-14 11:33:51 +01:00
|
|
|
$this->Controller->restResponsePayload = $this->RestResponse->viewData($savedData, 'json');
|
2020-12-15 10:40:49 +01:00
|
|
|
} else if ($this->Controller->ParamHandler->isAjax()) {
|
2020-08-07 21:47:04 +02:00
|
|
|
if (!empty($params['displayOnSuccess'])) {
|
2021-01-14 11:33:51 +01:00
|
|
|
$displayOnSuccess = $this->renderViewInVariable($params['displayOnSuccess'], ['entity' => $data]);
|
|
|
|
$this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxSuccessResponse($this->ObjectAlias, 'add', $savedData, $message, ['displayOnSuccess' => $displayOnSuccess]);
|
|
|
|
} else {
|
|
|
|
$this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxSuccessResponse($this->ObjectAlias, 'add', $savedData, $message);
|
2020-08-07 21:47:04 +02:00
|
|
|
}
|
2021-01-14 11:33:51 +01:00
|
|
|
} else {
|
|
|
|
$this->Controller->Flash->success($message);
|
2020-11-06 10:25:22 +01:00
|
|
|
if (empty($params['redirect'])) {
|
2020-11-06 13:21:45 +01:00
|
|
|
$this->Controller->redirect(['action' => 'view', $data->id]);
|
2020-11-06 10:25:22 +01:00
|
|
|
} else {
|
|
|
|
$this->Controller->redirect($params['redirect']);
|
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-15 10:40:49 +01:00
|
|
|
$this->Controller->isFailResponse = true;
|
2020-11-06 10:07:25 +01:00
|
|
|
$validationMessage = $this->prepareValidationError($data);
|
|
|
|
$message = __(
|
|
|
|
'{0} could not be added.{1}',
|
|
|
|
$this->ObjectAlias,
|
|
|
|
empty($validationMessage) ? '' : ' ' . __('Reason:{0}', $validationMessage)
|
|
|
|
);
|
2020-06-19 00:34:51 +02:00
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
2020-12-15 10:40:49 +01:00
|
|
|
} else if ($this->Controller->ParamHandler->isAjax()) {
|
|
|
|
$this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxFailResponse($this->ObjectAlias, 'add', $data, $message, $validationMessage);
|
2020-06-09 15:57:16 +02:00
|
|
|
} else {
|
|
|
|
$this->Controller->Flash->error($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 00:34:51 +02:00
|
|
|
$this->Controller->set('entity', $data);
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
|
2020-11-06 10:07:25 +01:00
|
|
|
private function prepareValidationError($data)
|
|
|
|
{
|
|
|
|
$validationMessage = '';
|
|
|
|
if (!empty($data->getErrors())) {
|
|
|
|
foreach ($data->getErrors() as $field => $errorData) {
|
|
|
|
$errorMessages = [];
|
|
|
|
foreach ($errorData as $key => $value) {
|
|
|
|
$errorMessages[] = $value;
|
|
|
|
}
|
|
|
|
$validationMessage .= __(' {1}', $field, implode(',', $errorMessages));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $validationMessage;
|
|
|
|
}
|
|
|
|
|
2020-09-28 01:25:07 +02:00
|
|
|
private function saveMetaFields($id, $input)
|
|
|
|
{
|
2021-01-15 14:40:03 +01:00
|
|
|
$this->Table->saveMetaFields($id, $input, $this->Table);
|
2020-09-28 01:25:07 +02:00
|
|
|
}
|
|
|
|
|
2020-11-05 10:17:42 +01:00
|
|
|
private function __massageInput($params)
|
|
|
|
{
|
|
|
|
$input = $this->request->getData();
|
|
|
|
if (!empty($params['override'])) {
|
|
|
|
foreach ($params['override'] as $field => $value) {
|
|
|
|
$input[$field] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($params['removeEmpty'])) {
|
2021-01-12 08:50:01 +01:00
|
|
|
foreach ($params['removeEmpty'] as $removeEmptyField) {
|
|
|
|
if (empty($input[$removeEmptyField])) {
|
|
|
|
unset($input[$removeEmptyField]);
|
|
|
|
}
|
2020-11-05 10:17:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
2020-06-21 21:33:38 +02:00
|
|
|
public function edit(int $id, array $params = []): void
|
2020-06-09 15:57:16 +02:00
|
|
|
{
|
|
|
|
if (empty($id)) {
|
2020-06-19 00:34:51 +02:00
|
|
|
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
2020-09-28 01:25:07 +02:00
|
|
|
$this->getMetaTemplates();
|
2020-06-21 21:33:38 +02:00
|
|
|
$data = $this->Table->get($id, isset($params['get']) ? $params['get'] : []);
|
2020-09-28 01:25:07 +02:00
|
|
|
$data = $this->getMetaFields($id, $data);
|
2020-11-05 10:17:42 +01:00
|
|
|
if (!empty($params['fields'])) {
|
|
|
|
$this->Controller->set('fields', $params['fields']);
|
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
if ($this->request->is(['post', 'put'])) {
|
2021-01-13 14:18:26 +01:00
|
|
|
$patchEntityParams = [
|
|
|
|
'associated' => []
|
|
|
|
];
|
2020-11-05 10:17:42 +01:00
|
|
|
$input = $this->__massageInput($params);
|
|
|
|
if (!empty($params['fields'])) {
|
|
|
|
$patchEntityParams['fields'] = $params['fields'];
|
2020-06-25 01:45:28 +02:00
|
|
|
}
|
2020-11-06 10:07:25 +01:00
|
|
|
$data = $this->Table->patchEntity($data, $input, $patchEntityParams);
|
2020-12-10 15:18:02 +01:00
|
|
|
$savedData = $this->Table->save($data);
|
|
|
|
if ($savedData !== false) {
|
2021-01-13 14:15:53 +01:00
|
|
|
$message = __('{0} `{1}` updated.', $this->ObjectAlias, $savedData->{$this->Table->getDisplayField()});
|
2020-09-28 01:25:07 +02:00
|
|
|
if (!empty($input['metaFields'])) {
|
2021-01-15 14:40:03 +01:00
|
|
|
$this->MetaFields->deleteAll(['scope' => $this->Table->metaFields, 'parent_id' => $savedData->id]);
|
|
|
|
$this->saveMetaFields($savedData->id, $input);
|
2020-09-28 01:25:07 +02:00
|
|
|
}
|
2020-06-19 00:34:51 +02:00
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
2021-01-15 14:40:03 +01:00
|
|
|
$this->Controller->restResponsePayload = $this->RestResponse->viewData($savedData, 'json');
|
2020-12-10 15:18:02 +01:00
|
|
|
} else if ($this->Controller->ParamHandler->isAjax()) {
|
|
|
|
$this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxSuccessResponse($this->ObjectAlias, 'edit', $savedData, $message);
|
2020-06-09 15:57:16 +02:00
|
|
|
} else {
|
|
|
|
$this->Controller->Flash->success($message);
|
2020-11-06 10:25:22 +01:00
|
|
|
if (empty($params['redirect'])) {
|
|
|
|
$this->Controller->redirect(['action' => 'view', $id]);
|
|
|
|
} else {
|
|
|
|
$this->Controller->redirect($params['redirect']);
|
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-06 10:07:25 +01:00
|
|
|
$validationMessage = $this->prepareValidationError($data);
|
|
|
|
$message = __(
|
2021-01-12 10:16:58 +01:00
|
|
|
__('{0} could not be modified.'),
|
2020-11-06 10:07:25 +01:00
|
|
|
$this->ObjectAlias,
|
|
|
|
);
|
2020-06-19 00:34:51 +02:00
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
2020-12-10 15:18:02 +01:00
|
|
|
} else if ($this->Controller->ParamHandler->isAjax()) {
|
2021-01-12 10:16:58 +01:00
|
|
|
$this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxFailResponse($this->ObjectAlias, 'edit', $data, $message, $data->getErrors());
|
2020-11-06 10:07:25 +01:00
|
|
|
} else {
|
|
|
|
$this->Controller->Flash->error($message);
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 00:34:51 +02:00
|
|
|
$this->Controller->set('entity', $data);
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 09:07:48 +01:00
|
|
|
public function attachMetaData($id, $data)
|
|
|
|
{
|
|
|
|
if (empty($this->Table->metaFields)) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
$query = $this->MetaFields->MetaTemplates->find();
|
2020-12-08 15:03:38 +01:00
|
|
|
$metaFields = $this->Table->metaFields;
|
|
|
|
$query->contain('MetaTemplateFields', function ($q) use ($id, $metaFields) {
|
|
|
|
return $q->innerJoinWith('MetaFields')
|
|
|
|
->where(['MetaFields.scope' => $metaFields, 'MetaFields.parent_id' => $id]);
|
2020-12-08 09:07:48 +01:00
|
|
|
});
|
|
|
|
$query->innerJoinWith('MetaTemplateFields', function ($q) {
|
|
|
|
return $q->contain('MetaFields')->innerJoinWith('MetaFields');
|
|
|
|
});
|
2020-12-10 17:46:16 +01:00
|
|
|
$query->group(['MetaTemplates.id'])->order(['MetaTemplates.is_default' => 'DESC']);
|
2020-12-08 09:07:48 +01:00
|
|
|
$metaTemplates = $query->all();
|
|
|
|
$data['metaTemplates'] = $metaTemplates;
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2020-09-28 01:25:07 +02:00
|
|
|
public function getMetaFields($id, $data)
|
|
|
|
{
|
2020-09-28 13:14:45 +02:00
|
|
|
if (empty($this->Table->metaFields)) {
|
|
|
|
return $data;
|
|
|
|
}
|
2020-09-28 01:25:07 +02:00
|
|
|
$query = $this->MetaFields->find();
|
2020-12-07 09:52:35 +01:00
|
|
|
$query->where(['MetaFields.scope' => $this->Table->metaFields, 'MetaFields.parent_id' => $id]);
|
2020-09-28 01:25:07 +02:00
|
|
|
$metaFields = $query->all();
|
|
|
|
$data['metaFields'] = [];
|
|
|
|
foreach($metaFields as $metaField) {
|
2020-12-08 10:31:17 +01:00
|
|
|
$data['metaFields'][$metaField->meta_template_id][$metaField->field] = $metaField->value;
|
2020-09-28 01:25:07 +02:00
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2020-06-19 00:34:51 +02:00
|
|
|
public function view(int $id, array $params = []): void
|
2020-06-09 15:57:16 +02:00
|
|
|
{
|
|
|
|
if (empty($id)) {
|
2020-06-19 00:34:51 +02:00
|
|
|
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->Table->get($id, $params);
|
2020-12-08 09:07:48 +01:00
|
|
|
$data = $this->attachMetaData($id, $data);
|
2020-06-09 15:57:16 +02:00
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
|
|
|
$this->Controller->restResponsePayload = $this->Controller->RestResponse->viewData($data, 'json');
|
|
|
|
}
|
2020-06-19 00:34:51 +02:00
|
|
|
$this->Controller->set('entity', $data);
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 00:34:51 +02:00
|
|
|
public function delete(int $id): void
|
2020-06-09 15:57:16 +02:00
|
|
|
{
|
|
|
|
if (empty($id)) {
|
2020-06-19 00:34:51 +02:00
|
|
|
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
$data = $this->Table->get($id);
|
|
|
|
if ($this->request->is('post') || $this->request->is('delete')) {
|
|
|
|
if ($this->Table->delete($data)) {
|
2020-06-19 00:34:51 +02:00
|
|
|
$message = __('{0} deleted.', $this->ObjectAlias);
|
2020-06-09 15:57:16 +02:00
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
2021-01-11 12:48:58 +01:00
|
|
|
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
|
|
|
|
} else if ($this->Controller->ParamHandler->isAjax()) {
|
|
|
|
$this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxSuccessResponse($this->ObjectAlias, 'delete', $data, $message);
|
2020-06-09 15:57:16 +02:00
|
|
|
} else {
|
|
|
|
$this->Controller->Flash->success($message);
|
2020-06-19 00:34:51 +02:00
|
|
|
$this->Controller->redirect($this->Controller->referer());
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->Controller->set('metaGroup', 'ContactDB');
|
|
|
|
$this->Controller->set('scope', 'users');
|
|
|
|
$this->Controller->set('id', $data['id']);
|
|
|
|
$this->Controller->set('data', $data);
|
|
|
|
$this->Controller->viewBuilder()->setLayout('ajax');
|
|
|
|
$this->Controller->render('/genericTemplates/delete');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function massageFilters(array $params): array
|
|
|
|
{
|
|
|
|
$massagedFilters = [
|
|
|
|
'simpleFilters' => [],
|
|
|
|
'relatedFilters' => []
|
|
|
|
];
|
|
|
|
if (!empty($params)) {
|
|
|
|
foreach ($params as $param => $paramValue) {
|
2020-06-21 21:33:38 +02:00
|
|
|
if (strpos($param, '.') !== false) {
|
2020-06-19 00:34:51 +02:00
|
|
|
$param = explode('.', $param);
|
|
|
|
if ($param[0] === $this->Table->getAlias()) {
|
2020-06-09 15:57:16 +02:00
|
|
|
$massagedFilters['simpleFilters'][implode('.', $param)] = $paramValue;
|
|
|
|
} else {
|
2020-06-19 00:34:51 +02:00
|
|
|
$massagedFilters['relatedFilters'][implode('.', $param)] = $paramValue;
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-21 21:33:38 +02:00
|
|
|
$massagedFilters['simpleFilters'][$param] = $paramValue;
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $massagedFilters;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setQuickFilters(array $params, \Cake\ORM\Query $query, array $quickFilterFields): \Cake\ORM\Query
|
|
|
|
{
|
|
|
|
$queryConditions = [];
|
2021-01-15 16:58:46 +01:00
|
|
|
$this->Controller->set('quickFilter', empty($quickFilterFields) ? [] : $quickFilterFields);
|
2020-06-09 15:57:16 +02:00
|
|
|
if (!empty($params['quickFilter']) && !empty($quickFilterFields)) {
|
2021-01-15 11:49:20 +01:00
|
|
|
$this->Controller->set('quickFilterValue', $params['quickFilter']);
|
2020-06-09 15:57:16 +02:00
|
|
|
foreach ($quickFilterFields as $filterField) {
|
2021-01-15 11:32:47 +01:00
|
|
|
$likeCondition = false;
|
|
|
|
if (is_array($filterField)) {
|
|
|
|
$likeCondition = reset($filterField);
|
|
|
|
$filterFieldName = array_key_first($filterField);
|
|
|
|
$queryConditions[$filterFieldName . ' LIKE'] = '%' . $params['quickFilter'] .'%';
|
|
|
|
} else {
|
|
|
|
$queryConditions[$filterField] = $params['quickFilter'];
|
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
$query->where(['OR' => $queryConditions]);
|
2021-01-15 11:49:20 +01:00
|
|
|
} else {
|
|
|
|
$this->Controller->set('quickFilterValue', '');
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setFilters($params, \Cake\ORM\Query $query): \Cake\ORM\Query
|
|
|
|
{
|
|
|
|
$params = $this->massageFilters($params);
|
|
|
|
$conditions = array();
|
|
|
|
if (!empty($params['simpleFilters'])) {
|
|
|
|
foreach ($params['simpleFilters'] as $filter => $filterValue) {
|
2020-06-21 21:33:38 +02:00
|
|
|
if ($filter === 'quickFilter') {
|
|
|
|
continue;
|
|
|
|
}
|
2020-12-08 15:07:17 +01:00
|
|
|
if (is_array($filterValue)) {
|
|
|
|
$query->where([($filter . ' IN') => $filterValue]);
|
2020-06-09 15:57:16 +02:00
|
|
|
} else {
|
2020-12-08 15:07:17 +01:00
|
|
|
if (strlen(trim($filterValue, '%')) === strlen($filterValue)) {
|
|
|
|
$query->where([$filter => $filterValue]);
|
|
|
|
} else {
|
|
|
|
$query->like([$filter => $filterValue]);
|
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($params['relatedFilters'])) {
|
|
|
|
foreach ($params['relatedFilters'] as $filter => $filterValue) {
|
|
|
|
$filterParts = explode('.', $filter);
|
2020-06-19 00:34:51 +02:00
|
|
|
$query->matching($filterParts[0], function(\Cake\ORM\Query $q) use ($filterValue, $filter) {
|
2020-06-09 15:57:16 +02:00
|
|
|
if (strlen(trim($filterValue, '%')) === strlen($filterValue)) {
|
|
|
|
return $q->where([$filter => $filterValue]);
|
|
|
|
} else {
|
|
|
|
return $q->like([$filter => $filterValue]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
2020-11-20 11:09:24 +01:00
|
|
|
|
2020-12-08 15:08:12 +01:00
|
|
|
protected function setFilteringContext($contextFilters, $params)
|
2020-12-07 16:06:01 +01:00
|
|
|
{
|
2020-12-08 15:08:12 +01:00
|
|
|
$filteringContexts = [];
|
2020-12-15 16:44:39 +01:00
|
|
|
if (!isset($contextFilters['allow_all']) || $contextFilters['allow_all']) {
|
2020-12-08 15:08:12 +01:00
|
|
|
$filteringContexts[] = ['label' => __('All')];
|
|
|
|
}
|
|
|
|
if (!empty($contextFilters['fields'])) {
|
|
|
|
foreach ($contextFilters['fields'] as $field) {
|
|
|
|
$contextsFromField = $this->getFilteringContextFromField($field);
|
|
|
|
foreach ($contextsFromField as $contextFromField) {
|
2021-01-14 15:30:16 +01:00
|
|
|
if (is_bool($contextFromField)) {
|
|
|
|
$contextFromFieldText = sprintf('%s: %s', $field, $contextFromField ? 'true' : 'false');
|
|
|
|
} else {
|
|
|
|
$contextFromFieldText = $contextFromField;
|
|
|
|
}
|
2020-12-08 15:08:12 +01:00
|
|
|
$filteringContexts[] = [
|
2021-01-14 15:30:16 +01:00
|
|
|
'label' => Inflector::humanize($contextFromFieldText),
|
2020-12-08 15:08:12 +01:00
|
|
|
'filterCondition' => [
|
|
|
|
$field => $contextFromField
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
2020-12-07 16:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-08 15:08:12 +01:00
|
|
|
if (!empty($contextFilters['custom'])) {
|
|
|
|
$filteringContexts = array_merge($filteringContexts, $contextFilters['custom']);
|
|
|
|
}
|
|
|
|
$this->Controller->set('filteringContexts', $filteringContexts);
|
2020-12-07 16:06:01 +01:00
|
|
|
}
|
|
|
|
|
2020-12-07 14:17:10 +01:00
|
|
|
public function toggle(int $id, string $fieldName = 'enabled', array $params = []): void
|
|
|
|
{
|
|
|
|
if (empty($id)) {
|
|
|
|
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->Table->get($id, $params);
|
|
|
|
if ($this->request->is(['post', 'put'])) {
|
2020-12-10 17:18:17 +01:00
|
|
|
if (isset($params['force_state'])) {
|
|
|
|
$data->{$fieldName} = $params['force_state'];
|
|
|
|
} else {
|
|
|
|
$data->{$fieldName} = !$data->{$fieldName};
|
|
|
|
}
|
2020-12-10 15:18:02 +01:00
|
|
|
$savedData = $this->Table->save($data);
|
|
|
|
if ($savedData !== false) {
|
|
|
|
$message = __('{0} field {1}. (ID: {2} {3})',
|
2020-12-07 14:17:10 +01:00
|
|
|
$fieldName,
|
|
|
|
$data->{$fieldName} ? __('enabled') : __('disabled'),
|
2020-12-10 15:18:02 +01:00
|
|
|
Inflector::humanize($this->ObjectAlias),
|
|
|
|
$data->id
|
2020-12-07 14:17:10 +01:00
|
|
|
);
|
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
|
|
|
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
|
|
|
|
} else if ($this->Controller->ParamHandler->isAjax()) {
|
2020-12-10 15:18:02 +01:00
|
|
|
$this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxSuccessResponse($this->ObjectAlias, 'toggle', $savedData, $message);
|
2020-12-07 14:17:10 +01:00
|
|
|
} else {
|
|
|
|
$this->Controller->Flash->success($message);
|
|
|
|
if (empty($params['redirect'])) {
|
|
|
|
$this->Controller->redirect(['action' => 'view', $id]);
|
|
|
|
} else {
|
|
|
|
$this->Controller->redirect($params['redirect']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$validationMessage = $this->prepareValidationError($data);
|
|
|
|
$message = __(
|
|
|
|
'{0} could not be modified.{1}',
|
|
|
|
$this->ObjectAlias,
|
|
|
|
empty($validationMessage) ? '' : ' ' . __('Reason:{0}', $validationMessage)
|
|
|
|
);
|
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
|
|
|
} else if ($this->Controller->ParamHandler->isAjax()) {
|
2020-12-10 15:18:02 +01:00
|
|
|
$this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxFailResponse($this->ObjectAlias, 'toggle', $message, $validationMessage);
|
2020-12-07 14:17:10 +01:00
|
|
|
} else {
|
|
|
|
$this->Controller->Flash->error($message);
|
|
|
|
if (empty($params['redirect'])) {
|
|
|
|
$this->Controller->redirect(['action' => 'view', $id]);
|
|
|
|
} else {
|
|
|
|
$this->Controller->redirect($params['redirect']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->Controller->set('entity', $data);
|
|
|
|
$this->Controller->set('fieldName', $fieldName);
|
|
|
|
$this->Controller->viewBuilder()->setLayout('ajax');
|
|
|
|
$this->Controller->render('/genericTemplates/toggle');
|
|
|
|
}
|
|
|
|
|
2020-11-20 11:09:24 +01:00
|
|
|
public function toggleEnabled(int $id, array $path, string $fieldName = 'enabled'): bool
|
|
|
|
{
|
|
|
|
if (empty($id)) {
|
|
|
|
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
|
|
|
|
}
|
|
|
|
$data = $this->Table->get($id);
|
|
|
|
if ($this->request->is('post')) {
|
|
|
|
$data[$fieldName] = $data[$fieldName] ? true : false;
|
|
|
|
$this->Table->save($data);
|
|
|
|
$this->Controller->restResponsePayload = $this->Controller->RestResponse->viewData(['value' => $data[$fieldName]], 'json');
|
|
|
|
} else {
|
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
|
|
|
$this->Controller->restResponsePayload = $this->Controller->RestResponse->viewData(['value' => $data[$fieldName]], 'json');
|
|
|
|
} else {
|
|
|
|
$this->Controller->set('fieldName', $fieldName);
|
|
|
|
$this->Controller->set('currentValue', $data[$fieldName]);
|
|
|
|
$this->Controller->set('path', $path);
|
|
|
|
$this->Controller->render('/genericTemplates/ajaxForm');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-07 16:06:01 +01:00
|
|
|
|
2021-01-11 12:48:58 +01:00
|
|
|
private function getFilteringContextFromField($field)
|
2020-12-07 16:06:01 +01:00
|
|
|
{
|
2021-01-11 12:48:58 +01:00
|
|
|
$exploded = explode('.', $field);
|
|
|
|
if (count($exploded) > 1) {
|
|
|
|
$model = $exploded[0];
|
|
|
|
$subField = $exploded[1];
|
2021-01-14 16:34:21 +01:00
|
|
|
$associationType = $this->Table->associations()->get($model)->type();
|
|
|
|
if ($associationType == 'oneToMany' || $associationType == 'manyToMany') {
|
|
|
|
$fieldToExtract = sprintf('%s.{*}.%s', strtolower($model), $subField);
|
|
|
|
} else {
|
|
|
|
$fieldToExtract = sprintf('%s.%s', Inflector::singularize(strtolower($model)), $subField);
|
|
|
|
}
|
|
|
|
$query = $this->Table->find()->contain($model);
|
2021-01-11 12:48:58 +01:00
|
|
|
} else {
|
2021-01-18 16:13:10 +01:00
|
|
|
$fieldToExtract = $field;
|
|
|
|
$query = $this->Table->find();
|
2021-01-11 12:48:58 +01:00
|
|
|
}
|
2021-01-18 16:13:10 +01:00
|
|
|
return $query->select([$field])
|
|
|
|
->distinct()
|
|
|
|
->extract($fieldToExtract)
|
|
|
|
->toList();
|
2020-12-07 16:06:01 +01:00
|
|
|
}
|
2021-01-14 11:33:51 +01:00
|
|
|
|
|
|
|
private function renderViewInVariable($templateRelativeName, $data)
|
|
|
|
{
|
|
|
|
$builder = new ViewBuilder();
|
|
|
|
$builder->disableAutoLayout()->setTemplate("{$this->TableAlias}/{$templateRelativeName}");
|
|
|
|
$view = $builder->build($data);
|
|
|
|
return $view->render();
|
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|