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-11-04 08:10:32 +01:00
|
|
|
use Cake\Utility\Text;
|
2021-01-14 11:33:51 +01:00
|
|
|
use Cake\View\ViewBuilder;
|
2021-08-26 16:12:55 +02:00
|
|
|
use Cake\ORM\TableRegistry;
|
2021-06-23 11:12:14 +02:00
|
|
|
use Cake\Http\Exception\MethodNotAllowedException;
|
|
|
|
use Cake\Http\Exception\NotFoundException;
|
2020-06-09 15:57:16 +02:00
|
|
|
|
|
|
|
class CRUDComponent extends Component
|
|
|
|
{
|
2021-06-28 23:27:21 +02:00
|
|
|
public $components = ['RestResponse'];
|
2020-06-09 15:57:16 +02:00
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
2021-02-26 10:36:06 +01:00
|
|
|
$options['filters'][] = 'filteringLabel';
|
2021-08-30 15:11:21 +02:00
|
|
|
if ($this->taggingSupported()) {
|
|
|
|
$options['filters'][] = 'filteringTags';
|
|
|
|
}
|
2021-03-10 09:35:26 +01:00
|
|
|
|
|
|
|
$optionFilters = empty($options['filters']) ? [] : $options['filters'];
|
|
|
|
foreach ($optionFilters as $i => $filter) {
|
|
|
|
$optionFilters[] = "{$filter} !=";
|
|
|
|
}
|
|
|
|
$params = $this->Controller->ParamHandler->harvestParams($optionFilters);
|
2020-06-09 15:57:16 +02:00
|
|
|
$query = $this->Table->find();
|
2021-06-01 07:47:22 +02:00
|
|
|
if (!empty($options['filterFunction'])) {
|
|
|
|
$query = $options['filterFunction']($query);
|
|
|
|
}
|
2021-02-26 10:36:06 +01:00
|
|
|
$query = $this->setFilters($params, $query, $options);
|
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']);
|
|
|
|
}
|
2021-08-30 15:11:21 +02:00
|
|
|
if ($this->taggingSupported()) {
|
|
|
|
$query->contain('Tags');
|
|
|
|
}
|
2021-06-01 07:47:22 +02:00
|
|
|
if (!empty($options['fields'])) {
|
|
|
|
$query->select($options['fields']);
|
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
|
|
|
$data = $query->all();
|
2021-04-30 23:59:53 +02:00
|
|
|
if (isset($options['afterFind'])) {
|
|
|
|
if (is_callable($options['afterFind'])) {
|
|
|
|
$data = $options['afterFind']($data);
|
|
|
|
} else {
|
|
|
|
$data = $this->Table->{$options['afterFind']}($data);
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 08:54:09 +02:00
|
|
|
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
|
2020-06-09 15:57:16 +02:00
|
|
|
} else {
|
|
|
|
$this->Controller->loadComponent('Paginator');
|
|
|
|
$data = $this->Controller->Paginator->paginate($query);
|
2021-04-30 23:59:53 +02:00
|
|
|
if (isset($options['afterFind'])) {
|
|
|
|
if (is_callable($options['afterFind'])) {
|
|
|
|
$data = $options['afterFind']($data);
|
|
|
|
} else {
|
|
|
|
$data = $this->Table->{$options['afterFind']}($data);
|
|
|
|
}
|
|
|
|
}
|
2021-08-30 15:11:21 +02:00
|
|
|
$this->setFilteringContext($options['contextFilters'] ?? [], $params);
|
2020-06-09 15:57:16 +02:00
|
|
|
$this->Controller->set('data', $data);
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 09:43:36 +01:00
|
|
|
|
|
|
|
public function filtering(): void
|
|
|
|
{
|
2021-08-30 15:11:21 +02:00
|
|
|
if ($this->taggingSupported()) {
|
|
|
|
$this->Controller->set('taggingEnabled', true);
|
|
|
|
$this->setAllTags();
|
|
|
|
}
|
2021-10-21 10:20:07 +02:00
|
|
|
$filters = !empty($this->Controller->filterFields) ? $this->Controller->filterFields : [];
|
2021-03-10 09:43:36 +01:00
|
|
|
$this->Controller->set('filters', $filters);
|
|
|
|
$this->Controller->viewBuilder()->setLayout('ajax');
|
|
|
|
$this->Controller->render('/genericTemplates/filters');
|
|
|
|
}
|
2021-04-30 23:59:53 +02:00
|
|
|
|
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
|
2021-11-03 11:47:10 +01:00
|
|
|
])
|
|
|
|
->contain('MetaTemplateFields')
|
|
|
|
->formatResults(function (\Cake\Collection\CollectionInterface $metaTemplates) { // Set meta-template && meta-template-fields indexed by their ID
|
|
|
|
return $metaTemplates
|
2021-11-04 08:10:32 +01:00
|
|
|
->map(function ($metaTemplate) {
|
2021-11-03 11:47:10 +01:00
|
|
|
$metaTemplate->meta_template_fields = Hash::combine($metaTemplate->meta_template_fields, '{n}.id', '{n}');
|
|
|
|
return $metaTemplate;
|
|
|
|
})
|
|
|
|
->indexBy('id');
|
|
|
|
});
|
2020-09-28 01:25:07 +02:00
|
|
|
$metaTemplates = $metaQuery->all();
|
|
|
|
}
|
2021-11-03 11:47:10 +01:00
|
|
|
return $metaTemplates;
|
2020-09-28 01:25:07 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 01:45:28 +02:00
|
|
|
public function add(array $params = []): void
|
2020-06-09 15:57:16 +02:00
|
|
|
{
|
2021-11-04 08:10:32 +01:00
|
|
|
$metaTemplates = $this->getMetaTemplates();
|
2020-06-09 15:57:16 +02:00
|
|
|
$data = $this->Table->newEmptyEntity();
|
2021-11-04 08:10:32 +01:00
|
|
|
// $data = $this->attachMetaTemplates($data, $metaTemplates->toArray());
|
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 = [
|
2021-06-28 14:02:52 +02:00
|
|
|
'associated' => [],
|
2021-06-28 14:49:38 +02:00
|
|
|
'accessibleFields' => $data->getAccessibleFieldForNew(),
|
2021-01-13 14:18:26 +01:00
|
|
|
];
|
|
|
|
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);
|
2021-10-01 13:19:26 +02:00
|
|
|
if (isset($params['beforeSave'])) {
|
|
|
|
$data = $params['beforeSave']($data);
|
|
|
|
}
|
2020-12-15 10:40:49 +01:00
|
|
|
$savedData = $this->Table->save($data);
|
|
|
|
if ($savedData !== false) {
|
2021-10-01 13:19:26 +02:00
|
|
|
if (isset($params['afterSave'])) {
|
|
|
|
$params['afterSave']($data);
|
|
|
|
}
|
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]);
|
2021-06-17 08:54:09 +02:00
|
|
|
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxSuccessResponse($this->ObjectAlias, 'add', $savedData, $message, ['displayOnSuccess' => $displayOnSuccess]);
|
2021-01-14 11:33:51 +01:00
|
|
|
} else {
|
2021-06-17 08:54:09 +02:00
|
|
|
$this->Controller->ajaxResponsePayload = $this->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;
|
2021-06-30 12:18:58 +02:00
|
|
|
$validationErrors = $data->getErrors();
|
|
|
|
$validationMessage = $this->prepareValidationMessage($validationErrors);
|
2020-11-06 10:07:25 +01:00
|
|
|
$message = __(
|
|
|
|
'{0} could not be added.{1}',
|
|
|
|
$this->ObjectAlias,
|
2021-03-15 22:47:13 +01:00
|
|
|
empty($validationMessage) ? '' : PHP_EOL . __('Reason:{0}', $validationMessage)
|
2020-11-06 10:07:25 +01:00
|
|
|
);
|
2020-06-19 00:34:51 +02:00
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
2021-06-17 08:54:09 +02:00
|
|
|
$this->Controller->restResponsePayload = $this->RestResponse->viewData($message, 'json');
|
2020-12-15 10:40:49 +01:00
|
|
|
} else if ($this->Controller->ParamHandler->isAjax()) {
|
2021-06-30 12:18:58 +02:00
|
|
|
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, 'add', $data, $message, $validationErrors);
|
2020-06-09 15:57:16 +02:00
|
|
|
} else {
|
|
|
|
$this->Controller->Flash->error($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 10:27:40 +02:00
|
|
|
$this->Controller->entity = $data;
|
2020-06-19 00:34:51 +02:00
|
|
|
$this->Controller->set('entity', $data);
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
|
2021-07-05 17:15:21 +02:00
|
|
|
public function prepareValidationMessage($errors)
|
2021-06-30 12:18:58 +02:00
|
|
|
{
|
|
|
|
$validationMessage = '';
|
|
|
|
if (!empty($errors)) {
|
|
|
|
if (count($errors) == 1) {
|
|
|
|
$field = array_keys($errors)[0];
|
|
|
|
$fieldError = implode(', ', array_values($errors[$field]));
|
|
|
|
$validationMessage = __('{0}: {1}', $field, $fieldError);
|
|
|
|
} else {
|
|
|
|
$validationMessage = __('There has been validation issues with multiple fields');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $validationMessage;
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-11-08 14:08:47 +01:00
|
|
|
if (is_array($value)) {
|
|
|
|
$errorMessages[] = implode('& ', Hash::extract($value, "{s}.{s}"));
|
|
|
|
} else {
|
|
|
|
$errorMessages[] = $value;
|
|
|
|
}
|
2020-11-06 10:07:25 +01:00
|
|
|
}
|
2021-06-30 12:18:58 +02:00
|
|
|
$validationMessage .= __('{0}: {1}', $field, implode(',', $errorMessages));
|
2020-11-06 10:07:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-04 08:10:32 +01:00
|
|
|
// prune empty values and marshall fields
|
|
|
|
private function massageMetaFields($entity, $input, $allMetaTemplates=[])
|
|
|
|
{
|
|
|
|
if (empty($input['MetaTemplates'])) {
|
2021-11-05 17:44:37 +01:00
|
|
|
return ['entity' => $entity, 'metafields_to_delete' => []];
|
2021-11-04 08:10:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$metaFieldsTable = TableRegistry::getTableLocator()->get('MetaFields');
|
|
|
|
$metaFieldsIndex = [];
|
|
|
|
if (empty($metaTemplates)) {
|
|
|
|
$allMetaTemplates = $this->getMetaTemplates()->toArray();
|
|
|
|
}
|
|
|
|
foreach ($entity->meta_fields as $i => $metaField) {
|
|
|
|
$metaFieldsIndex[$metaField->id] = $i;
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:44:37 +01:00
|
|
|
$metaFieldsToDelete = [];
|
2021-11-04 08:10:32 +01:00
|
|
|
foreach ($input['MetaTemplates'] as $template_id => $template) {
|
|
|
|
foreach ($template['meta_template_fields'] as $meta_template_field_id => $meta_template_field) {
|
|
|
|
$rawMetaTemplateField = $allMetaTemplates[$template_id]['meta_template_fields'][$meta_template_field_id];
|
|
|
|
foreach ($meta_template_field['metaFields'] as $meta_field_id => $meta_field) {
|
|
|
|
if ($meta_field_id == 'new') { // create new meta_field
|
|
|
|
$new_meta_fields = $meta_field;
|
|
|
|
foreach ($new_meta_fields as $new_value) {
|
|
|
|
if (!empty($new_value)) {
|
|
|
|
$metaField = $metaFieldsTable->newEmptyEntity();
|
|
|
|
$metaFieldsTable->patchEntity($metaField, [
|
|
|
|
'value' => $new_value,
|
|
|
|
'scope' => $this->Table->metaFields, // get scope from behavior
|
|
|
|
'field' => $rawMetaTemplateField->field,
|
|
|
|
'meta_template_id' => $rawMetaTemplateField->meta_template_id,
|
|
|
|
'meta_template_field_id' => $rawMetaTemplateField->id,
|
|
|
|
'parent_id' => $entity->id,
|
|
|
|
'uuid' => Text::uuid(),
|
|
|
|
]);
|
|
|
|
$entity->meta_fields[] = $metaField;
|
2021-11-08 14:08:47 +01:00
|
|
|
$entity->MetaTemplates[$template_id]->meta_template_fields[$meta_template_field_id]->metaFields[] = $metaField;
|
|
|
|
// $entity->MetaTemplates[$template_id]->meta_template_fields[$meta_template_field_id]->metaFields['new'][] = $metaField;
|
2021-11-04 08:10:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$new_value = $meta_field['value'];
|
|
|
|
if (!empty($new_value)) {
|
2021-11-08 14:08:47 +01:00
|
|
|
// update meta_field and attach validation errors
|
|
|
|
if (!empty($metaFieldsIndex[$meta_field_id])) {
|
|
|
|
$index = $metaFieldsIndex[$meta_field_id];
|
|
|
|
$metaFieldsTable->patchEntity($entity->meta_fields[$index], [
|
|
|
|
'value' => $new_value, 'meta_template_field_id' => $rawMetaTemplateField->id
|
|
|
|
], ['value']);
|
|
|
|
$metaFieldsTable->patchEntity(
|
|
|
|
$entity->MetaTemplates[$template_id]->meta_template_fields[$meta_template_field_id]->metaFields[$meta_field_id],
|
|
|
|
['value' => $new_value, 'meta_template_field_id' => $rawMetaTemplateField->id],
|
|
|
|
['value']
|
|
|
|
);
|
|
|
|
} else { // metafield comes from a second post where the temporary entity has already been created
|
|
|
|
$metaField = $metaFieldsTable->newEmptyEntity();
|
|
|
|
$metaFieldsTable->patchEntity($metaField, [
|
|
|
|
'value' => $new_value,
|
|
|
|
'scope' => $this->Table->metaFields, // get scope from behavior
|
|
|
|
'field' => $rawMetaTemplateField->field,
|
|
|
|
'meta_template_id' => $rawMetaTemplateField->meta_template_id,
|
|
|
|
'meta_template_field_id' => $rawMetaTemplateField->id,
|
|
|
|
'parent_id' => $entity->id,
|
|
|
|
'uuid' => Text::uuid(),
|
|
|
|
]);
|
|
|
|
$entity->meta_fields[] = $metaField;
|
|
|
|
$entity->MetaTemplates[$template_id]->meta_template_fields[$meta_template_field_id]->metaFields[] = $metaField;
|
|
|
|
}
|
2021-11-04 08:10:32 +01:00
|
|
|
} else {
|
2021-11-08 14:08:47 +01:00
|
|
|
// Metafield value is empty, indicating the field should be removed
|
|
|
|
$index = $metaFieldsIndex[$meta_field_id];
|
2021-11-05 17:44:37 +01:00
|
|
|
$metaFieldsToDelete[] = $entity->meta_fields[$index];
|
2021-11-04 08:10:32 +01:00
|
|
|
unset($entity->meta_fields[$index]);
|
2021-11-08 14:08:47 +01:00
|
|
|
unset($entity->MetaTemplates[$template_id]->meta_template_fields[$meta_template_field_id]->metaFields[$meta_field_id]);
|
2021-11-04 08:10:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$entity->setDirty('meta_fields', true);
|
2021-11-05 17:44:37 +01:00
|
|
|
return ['entity' => $entity, 'metafields_to_delete' => $metaFieldsToDelete];
|
2021-11-04 08:10:32 +01: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
|
|
|
}
|
2021-11-03 11:47:10 +01:00
|
|
|
$metaTemplates = $this->getMetaTemplates();
|
2021-08-26 12:06:12 +02:00
|
|
|
if ($this->taggingSupported()) {
|
|
|
|
$params['contain'][] = 'Tags';
|
2021-08-30 15:11:21 +02:00
|
|
|
$this->setAllTags();
|
2021-08-26 12:06:12 +02:00
|
|
|
}
|
2021-11-04 08:10:32 +01:00
|
|
|
$getParam = isset($params['get']) ? $params['get'] : $params;
|
2021-11-05 17:03:11 +01:00
|
|
|
if ($this->Table->hasBehavior('MetaFields')) {
|
2021-11-04 08:10:32 +01:00
|
|
|
if (empty($getParam['contain'])) {
|
|
|
|
$getParam['contain'] = [];
|
|
|
|
}
|
|
|
|
if (is_array($getParam['contain'])) {
|
|
|
|
$getParam['contain'][] = 'MetaFields';
|
|
|
|
} else {
|
|
|
|
$getParam['contain'] = [$getParam['contain'], 'MetaFields'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$data = $this->Table->get($id, $getParam);
|
2021-11-03 11:47:10 +01:00
|
|
|
$data = $this->attachMetaTemplates($data, $metaTemplates->toArray());
|
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
|
|
|
}
|
2021-11-08 14:08:47 +01:00
|
|
|
$massagedData = $this->massageMetaFields($data, $input, $metaTemplates);
|
|
|
|
unset($input['MetaTemplates']); // Avoid MetaTemplates to be overriden when patching entity
|
|
|
|
$data = $massagedData['entity'];
|
|
|
|
$metaFieldsToDelete = $massagedData['metafields_to_delete'];
|
2020-11-06 10:07:25 +01:00
|
|
|
$data = $this->Table->patchEntity($data, $input, $patchEntityParams);
|
2021-10-01 13:19:26 +02:00
|
|
|
if (isset($params['beforeSave'])) {
|
|
|
|
$data = $params['beforeSave']($data);
|
|
|
|
}
|
2020-12-10 15:18:02 +01:00
|
|
|
$savedData = $this->Table->save($data);
|
|
|
|
if ($savedData !== false) {
|
2021-11-05 17:44:37 +01:00
|
|
|
if ($this->Table->hasBehavior('MetaFields') && !empty($metaFieldsToDelete)) {
|
|
|
|
$this->Table->MetaFields->unlink($savedData, $metaFieldsToDelete);
|
|
|
|
}
|
2021-10-01 13:19:26 +02:00
|
|
|
if (isset($params['afterSave'])) {
|
|
|
|
$params['afterSave']($data);
|
|
|
|
}
|
2021-01-13 14:15:53 +01:00
|
|
|
$message = __('{0} `{1}` updated.', $this->ObjectAlias, $savedData->{$this->Table->getDisplayField()});
|
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()) {
|
2021-06-17 08:54:09 +02:00
|
|
|
$this->Controller->ajaxResponsePayload = $this->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 {
|
2021-06-30 12:18:58 +02:00
|
|
|
$validationErrors = $data->getErrors();
|
2021-11-08 14:08:47 +01:00
|
|
|
$validationMessage = $this->prepareValidationError($data);
|
2020-11-06 10:07:25 +01:00
|
|
|
$message = __(
|
2021-07-05 17:15:21 +02:00
|
|
|
'{0} could not be modified.{1}',
|
|
|
|
$this->ObjectAlias,
|
|
|
|
empty($validationMessage) ? '' : PHP_EOL . __('Reason:{0}', $validationMessage)
|
2020-11-06 10:07:25 +01:00
|
|
|
);
|
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-06-30 12:18:58 +02:00
|
|
|
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, 'edit', $data, $message, $validationErrors);
|
2020-11-06 10:07:25 +01:00
|
|
|
} else {
|
|
|
|
$this->Controller->Flash->error($message);
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 10:27:40 +02:00
|
|
|
$this->Controller->entity = $data;
|
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;
|
|
|
|
}
|
2021-02-26 10:36:06 +01:00
|
|
|
$metaFieldScope = $this->Table->metaFields;
|
|
|
|
$query = $this->MetaTemplates->find()->where(['MetaTemplates.scope' => $metaFieldScope]);
|
|
|
|
$query->contain(['MetaTemplateFields.MetaFields' => function ($q) use ($id, $metaFieldScope) {
|
|
|
|
return $q->where(['MetaFields.scope' => $metaFieldScope, 'MetaFields.parent_id' => $id]);
|
|
|
|
}]);
|
|
|
|
$query
|
|
|
|
->order(['MetaTemplates.is_default' => 'DESC'])
|
|
|
|
->order(['MetaTemplates.name' => 'ASC']);
|
|
|
|
$metaTemplates = $query->all()->toArray();
|
|
|
|
$metaTemplates = $this->pruneEmptyMetaTemplates($metaTemplates);
|
2020-12-08 09:07:48 +01:00
|
|
|
$data['metaTemplates'] = $metaTemplates;
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2021-02-26 10:36:06 +01:00
|
|
|
public function pruneEmptyMetaTemplates($metaTemplates)
|
|
|
|
{
|
|
|
|
foreach ($metaTemplates as $i => $metaTemplate) {
|
|
|
|
foreach ($metaTemplate['meta_template_fields'] as $j => $metaTemplateField) {
|
|
|
|
if (empty($metaTemplateField['meta_fields'])) {
|
|
|
|
unset($metaTemplates[$i]['meta_template_fields'][$j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($metaTemplates[$i]['meta_template_fields'])) {
|
|
|
|
unset($metaTemplates[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $metaTemplates;
|
|
|
|
}
|
|
|
|
|
2021-11-03 11:47:10 +01:00
|
|
|
// public function getMetaFields($id, $data)
|
|
|
|
// {
|
|
|
|
// if (empty($this->Table->metaFields)) {
|
|
|
|
// return $data;
|
|
|
|
// }
|
|
|
|
// $query = $this->MetaFields->find();
|
|
|
|
// $query->where(['MetaFields.scope' => $this->Table->metaFields, 'MetaFields.parent_id' => $id]);
|
|
|
|
// $metaFields = $query->all();
|
|
|
|
// $data['metaFields'] = [];
|
|
|
|
// foreach($metaFields as $metaField) {
|
|
|
|
// // $data['metaFields'][$metaField->meta_template_id][$metaField->field] = $metaField->value;
|
|
|
|
// $data['metaFields'][$metaField->meta_template_id][$metaField->meta_template_field_id] = $metaField->value;
|
|
|
|
// }
|
|
|
|
// return $data;
|
|
|
|
// }
|
|
|
|
public function getMetaFields($id)
|
2020-09-28 01:25:07 +02:00
|
|
|
{
|
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();
|
2021-11-03 11:47:10 +01:00
|
|
|
$data = [];
|
2021-11-04 08:10:32 +01:00
|
|
|
foreach ($metaFields as $metaField) {
|
2021-11-03 11:47:10 +01:00
|
|
|
if (empty($data[$metaField->meta_template_id][$metaField->meta_template_field_id])) {
|
|
|
|
$data[$metaField->meta_template_id][$metaField->meta_template_field_id] = [];
|
|
|
|
}
|
|
|
|
$data[$metaField->meta_template_id][$metaField->meta_template_field_id][$metaField->id] = $metaField;
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function attachMetaTemplates($data, $metaTemplates)
|
|
|
|
{
|
|
|
|
$metaFields = $this->getMetaFields($data->id, $data);
|
|
|
|
foreach ($metaTemplates as $i => $metaTemplate) {
|
|
|
|
if (isset($metaFields[$metaTemplate->id])) {
|
|
|
|
foreach ($metaTemplate->meta_template_fields as $j => $meta_template_field) {
|
|
|
|
if (isset($metaFields[$metaTemplate->id][$meta_template_field->id])) {
|
|
|
|
$metaTemplates[$metaTemplate->id]->meta_template_fields[$j]['metaFields'] = $metaFields[$metaTemplate->id][$meta_template_field->id];
|
|
|
|
} else {
|
|
|
|
$metaTemplates[$metaTemplate->id]->meta_template_fields[$j]['metaFields'] = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 01:25:07 +02:00
|
|
|
}
|
2021-11-03 11:47:10 +01:00
|
|
|
$data['MetaTemplates'] = $metaTemplates;
|
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
|
|
|
}
|
|
|
|
|
2021-08-26 12:06:12 +02:00
|
|
|
if ($this->taggingSupported()) {
|
|
|
|
$params['contain'][] = 'Tags';
|
2021-08-26 16:12:55 +02:00
|
|
|
$this->setAllTags();
|
2021-08-26 12:06:12 +02:00
|
|
|
}
|
2021-11-05 17:03:11 +01:00
|
|
|
if ($this->Table->hasBehavior('MetaFields')) {
|
2021-11-04 15:21:03 +01:00
|
|
|
if (!empty($this->request->getQuery('full'))) {
|
|
|
|
$params['contain']['MetaFields'] = ['MetaTemplateFields' => 'MetaTemplates'];
|
|
|
|
} else {
|
|
|
|
$params['contain'][] = 'MetaFields';
|
|
|
|
}
|
|
|
|
}
|
2021-08-26 12:06:12 +02:00
|
|
|
|
2020-06-09 15:57:16 +02:00
|
|
|
$data = $this->Table->get($id, $params);
|
2021-11-05 17:03:11 +01:00
|
|
|
$metaTemplates = $this->getMetaTemplates();
|
|
|
|
$data = $this->attachMetaTemplates($data, $metaTemplates->toArray());
|
|
|
|
// $data = $this->attachMetaData($id, $data);
|
2021-06-01 07:47:22 +02:00
|
|
|
if (isset($params['afterFind'])) {
|
|
|
|
$data = $params['afterFind']($data);
|
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
2021-06-17 08:54:09 +02:00
|
|
|
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
|
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
|
|
|
}
|
|
|
|
|
2021-11-04 08:10:32 +01:00
|
|
|
public function delete($id = false): void
|
2020-06-09 15:57:16 +02:00
|
|
|
{
|
2021-06-23 11:12:14 +02:00
|
|
|
if ($this->request->is('get')) {
|
2021-11-04 08:10:32 +01:00
|
|
|
if (!empty($id)) {
|
2021-06-23 11:12:14 +02:00
|
|
|
$data = $this->Table->get($id);
|
|
|
|
$this->Controller->set('id', $data['id']);
|
|
|
|
$this->Controller->set('data', $data);
|
|
|
|
$this->Controller->set('bulkEnabled', false);
|
|
|
|
} else {
|
|
|
|
$this->Controller->set('bulkEnabled', true);
|
|
|
|
}
|
|
|
|
} else if ($this->request->is('post') || $this->request->is('delete')) {
|
|
|
|
$ids = $this->getIdsOrFail($id);
|
|
|
|
$isBulk = count($ids) > 1;
|
|
|
|
$bulkSuccesses = 0;
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$data = $this->Table->get($id);
|
|
|
|
$success = $this->Table->delete($data);
|
|
|
|
$success = true;
|
|
|
|
if ($success) {
|
|
|
|
$bulkSuccesses++;
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-23 11:12:14 +02:00
|
|
|
$message = $this->getMessageBasedOnResult(
|
|
|
|
$bulkSuccesses == count($ids),
|
|
|
|
$isBulk,
|
|
|
|
__('{0} deleted.', $this->ObjectAlias),
|
|
|
|
__('All {0} have been deleted.', Inflector::pluralize($this->ObjectAlias)),
|
|
|
|
__('Could not delete {0}.', $this->ObjectAlias),
|
2021-11-04 08:10:32 +01:00
|
|
|
__(
|
|
|
|
'{0} / {1} {2} have been deleted.',
|
2021-06-23 11:12:14 +02:00
|
|
|
$bulkSuccesses,
|
|
|
|
count($ids),
|
|
|
|
Inflector::pluralize($this->ObjectAlias)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->setResponseForController('delete', $bulkSuccesses, $message, $data);
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
$this->Controller->set('metaGroup', 'ContactDB');
|
|
|
|
$this->Controller->set('scope', 'users');
|
|
|
|
$this->Controller->viewBuilder()->setLayout('ajax');
|
|
|
|
$this->Controller->render('/genericTemplates/delete');
|
|
|
|
}
|
|
|
|
|
2021-11-04 08:10:32 +01:00
|
|
|
public function tag($id = false): void
|
2021-08-26 12:06:12 +02:00
|
|
|
{
|
|
|
|
if (!$this->taggingSupported()) {
|
|
|
|
throw new Exception("Table {$this->TableAlias} does not support tagging");
|
|
|
|
}
|
|
|
|
if ($this->request->is('get')) {
|
2021-08-26 16:12:55 +02:00
|
|
|
$this->setAllTags();
|
2021-11-04 08:10:32 +01:00
|
|
|
if (!empty($id)) {
|
2021-08-26 12:06:12 +02:00
|
|
|
$params = [
|
|
|
|
'contain' => 'Tags',
|
|
|
|
];
|
|
|
|
$entity = $this->Table->get($id, $params);
|
|
|
|
$this->Controller->set('id', $entity->id);
|
|
|
|
$this->Controller->set('data', $entity);
|
|
|
|
$this->Controller->set('bulkEnabled', false);
|
|
|
|
} else {
|
|
|
|
$this->Controller->set('bulkEnabled', true);
|
|
|
|
}
|
|
|
|
} else if ($this->request->is('post') || $this->request->is('delete')) {
|
|
|
|
$ids = $this->getIdsOrFail($id);
|
|
|
|
$isBulk = count($ids) > 1;
|
|
|
|
$bulkSuccesses = 0;
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$params = [
|
|
|
|
'contain' => 'Tags',
|
|
|
|
];
|
|
|
|
$entity = $this->Table->get($id, $params);
|
|
|
|
$input = $this->request->getData();
|
2021-08-31 15:21:28 +02:00
|
|
|
$tagsToAdd = json_decode($input['tag_list']);
|
|
|
|
// patching will mirror tag in the DB, however, we only want to add tags
|
|
|
|
$input['tags'] = array_merge($tagsToAdd, $entity->tags);
|
2021-08-26 12:06:12 +02:00
|
|
|
$patchEntityParams = [
|
|
|
|
'fields' => ['tags'],
|
|
|
|
];
|
|
|
|
$entity = $this->Table->patchEntity($entity, $input, $patchEntityParams);
|
|
|
|
$savedData = $this->Table->save($entity);
|
|
|
|
$success = true;
|
|
|
|
if ($success) {
|
|
|
|
$bulkSuccesses++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$message = $this->getMessageBasedOnResult(
|
|
|
|
$bulkSuccesses == count($ids),
|
|
|
|
$isBulk,
|
2021-08-26 16:12:55 +02:00
|
|
|
__('{0} tagged with `{1}`.', $this->ObjectAlias, $input['tag_list']),
|
2021-08-26 12:06:12 +02:00
|
|
|
__('All {0} have been tagged.', Inflector::pluralize($this->ObjectAlias)),
|
2021-08-26 16:12:55 +02:00
|
|
|
__('Could not tag {0} with `{1}`.', $this->ObjectAlias, $input['tag_list']),
|
2021-11-04 08:10:32 +01:00
|
|
|
__(
|
|
|
|
'{0} / {1} {2} have been tagged.',
|
2021-08-26 12:06:12 +02:00
|
|
|
$bulkSuccesses,
|
|
|
|
count($ids),
|
|
|
|
Inflector::pluralize($this->ObjectAlias)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->setResponseForController('tag', $bulkSuccesses, $message, $savedData);
|
|
|
|
}
|
|
|
|
$this->Controller->viewBuilder()->setLayout('ajax');
|
|
|
|
$this->Controller->render('/genericTemplates/tagForm');
|
|
|
|
}
|
|
|
|
|
2021-11-04 08:10:32 +01:00
|
|
|
public function untag($id = false): void
|
2021-08-26 12:06:12 +02:00
|
|
|
{
|
|
|
|
if (!$this->taggingSupported()) {
|
|
|
|
throw new Exception("Table {$this->TableAlias} does not support tagging");
|
|
|
|
}
|
|
|
|
if ($this->request->is('get')) {
|
2021-08-26 16:12:55 +02:00
|
|
|
$this->setAllTags();
|
2021-11-04 08:10:32 +01:00
|
|
|
if (!empty($id)) {
|
2021-08-26 12:06:12 +02:00
|
|
|
$params = [
|
|
|
|
'contain' => 'Tags',
|
|
|
|
];
|
|
|
|
$entity = $this->Table->get($id, $params);
|
|
|
|
$this->Controller->set('id', $entity->id);
|
|
|
|
$this->Controller->set('data', $entity);
|
|
|
|
$this->Controller->set('bulkEnabled', false);
|
|
|
|
} else {
|
|
|
|
$this->Controller->set('bulkEnabled', true);
|
|
|
|
}
|
|
|
|
} else if ($this->request->is('post') || $this->request->is('delete')) {
|
|
|
|
$ids = $this->getIdsOrFail($id);
|
|
|
|
$isBulk = count($ids) > 1;
|
|
|
|
$bulkSuccesses = 0;
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$params = [
|
|
|
|
'contain' => 'Tags',
|
|
|
|
];
|
|
|
|
$entity = $this->Table->get($id, $params);
|
|
|
|
$input = $this->request->getData();
|
2021-08-31 15:21:28 +02:00
|
|
|
$tagsToRemove = json_decode($input['tag_list']);
|
|
|
|
// patching will mirror tag in the DB, however, we only want to remove tags
|
|
|
|
$input['tags'] = array_filter($entity->tags, function ($existingTag) use ($tagsToRemove) {
|
2021-10-01 15:13:18 +02:00
|
|
|
return !in_array($existingTag->name, $tagsToRemove);
|
2021-08-31 15:21:28 +02:00
|
|
|
});
|
2021-08-26 12:06:12 +02:00
|
|
|
$patchEntityParams = [
|
|
|
|
'fields' => ['tags'],
|
|
|
|
];
|
|
|
|
$entity = $this->Table->patchEntity($entity, $input, $patchEntityParams);
|
|
|
|
$savedData = $this->Table->save($entity);
|
|
|
|
$success = true;
|
|
|
|
if ($success) {
|
|
|
|
$bulkSuccesses++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$message = $this->getMessageBasedOnResult(
|
|
|
|
$bulkSuccesses == count($ids),
|
|
|
|
$isBulk,
|
2021-08-26 16:12:55 +02:00
|
|
|
__('{0} untagged with `{1}`.', $this->ObjectAlias, implode(', ', $tagsToRemove)),
|
2021-08-26 12:06:12 +02:00
|
|
|
__('All {0} have been untagged.', Inflector::pluralize($this->ObjectAlias)),
|
2021-08-26 16:12:55 +02:00
|
|
|
__('Could not untag {0} with `{1}`.', $this->ObjectAlias, $input['tag_list']),
|
2021-11-04 08:10:32 +01:00
|
|
|
__(
|
|
|
|
'{0} / {1} {2} have been untagged.',
|
2021-08-26 12:06:12 +02:00
|
|
|
$bulkSuccesses,
|
|
|
|
count($ids),
|
|
|
|
Inflector::pluralize($this->ObjectAlias)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->setResponseForController('tag', $bulkSuccesses, $message, $entity);
|
|
|
|
}
|
|
|
|
$this->Controller->viewBuilder()->setLayout('ajax');
|
|
|
|
$this->Controller->render('/genericTemplates/tagForm');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function viewTags(int $id, array $params = []): void
|
|
|
|
{
|
|
|
|
if (!$this->taggingSupported()) {
|
|
|
|
throw new Exception("Table {$this->TableAlias} does not support tagging");
|
|
|
|
}
|
|
|
|
if (empty($id)) {
|
|
|
|
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
|
|
|
|
}
|
|
|
|
|
|
|
|
$params['contain'][] = 'Tags';
|
|
|
|
$data = $this->Table->get($id, $params);
|
|
|
|
if (isset($params['afterFind'])) {
|
|
|
|
$data = $params['afterFind']($data);
|
|
|
|
}
|
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
|
|
|
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
|
|
|
|
}
|
|
|
|
$this->Controller->set('entity', $data);
|
2021-08-26 16:12:55 +02:00
|
|
|
$this->setAllTags();
|
2021-08-26 12:06:12 +02:00
|
|
|
$this->Controller->viewBuilder()->setLayout('ajax');
|
|
|
|
$this->Controller->render('/genericTemplates/tag');
|
|
|
|
}
|
|
|
|
|
2021-11-04 08:10:32 +01:00
|
|
|
public function setResponseForController($action, $success, $message, $data = [], $errors = null)
|
2021-06-23 11:12:14 +02:00
|
|
|
{
|
|
|
|
if ($success) {
|
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
|
|
|
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
|
|
|
|
} elseif ($this->Controller->ParamHandler->isAjax()) {
|
|
|
|
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxSuccessResponse($this->ObjectAlias, $action, $data, $message);
|
|
|
|
} else {
|
|
|
|
$this->Controller->Flash->success($message);
|
|
|
|
$this->Controller->redirect($this->Controller->referer());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
|
|
|
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
|
|
|
|
} elseif ($this->Controller->ParamHandler->isAjax()) {
|
|
|
|
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, $action, $data, $message, !is_null($errors) ? $errors : $data->getErrors());
|
|
|
|
} else {
|
|
|
|
$this->Controller->Flash->error($message);
|
|
|
|
$this->Controller->redirect($this->Controller->referer());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getMessageBasedOnResult($isSuccess, $isBulk, $messageSingleSuccess, $messageBulkSuccess, $messageSingleFailure, $messageBulkFailure)
|
|
|
|
{
|
|
|
|
if ($isSuccess) {
|
|
|
|
$message = $isBulk ? $messageBulkSuccess : $messageSingleSuccess;
|
|
|
|
} else {
|
|
|
|
$message = $isBulk ? $messageBulkFailure : $messageSingleFailure;
|
|
|
|
}
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getIdsOrFail
|
|
|
|
*
|
|
|
|
* @param mixed $id
|
|
|
|
* @return Array The ID converted to a list or the list of provided IDs from the request
|
|
|
|
* @throws NotFoundException when no ID could be found
|
|
|
|
*/
|
2021-11-04 08:10:32 +01:00
|
|
|
public function getIdsOrFail($id = false): array
|
2021-06-23 11:12:14 +02:00
|
|
|
{
|
|
|
|
$params = $this->Controller->ParamHandler->harvestParams(['ids']);
|
|
|
|
if (!empty($params['ids'])) {
|
|
|
|
$params['ids'] = json_decode($params['ids']);
|
|
|
|
}
|
|
|
|
$ids = [];
|
|
|
|
if (empty($id)) {
|
|
|
|
if (empty($params['ids'])) {
|
|
|
|
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
|
|
|
|
}
|
|
|
|
$ids = $params['ids'];
|
|
|
|
} else {
|
|
|
|
$id = $this->getInteger($id);
|
|
|
|
if (!is_null($id)) {
|
|
|
|
$ids = [$id];
|
|
|
|
} else {
|
|
|
|
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getInteger($value)
|
|
|
|
{
|
|
|
|
return is_numeric($value) ? intval($value) : null;
|
|
|
|
}
|
|
|
|
|
2020-06-09 15:57:16 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-10 11:55:54 +02:00
|
|
|
public function setQuickFilters(array $params, \Cake\ORM\Query $query, array $quickFilterFields): \Cake\ORM\Query
|
2020-06-09 15:57:16 +02:00
|
|
|
{
|
|
|
|
$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']);
|
2021-11-04 15:21:03 +01:00
|
|
|
$queryConditions = $this->genQuickFilterConditions($params, $quickFilterFields);
|
2021-10-28 09:00:20 +02:00
|
|
|
$query->where(['OR' => $queryConditions]);
|
|
|
|
} else {
|
|
|
|
$this->Controller->set('quickFilterValue', '');
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:27:30 +02:00
|
|
|
public function genQuickFilterConditions(array $params, array $quickFilterFields): array
|
2021-10-28 09:00:20 +02:00
|
|
|
{
|
|
|
|
$queryConditions = [];
|
|
|
|
foreach ($quickFilterFields as $filterField) {
|
|
|
|
if (is_array($filterField)) {
|
|
|
|
reset($filterField);
|
|
|
|
$filterFieldName = array_key_first($filterField);
|
|
|
|
if (!empty($filterField[$filterFieldName])) {
|
|
|
|
$queryConditions[$filterFieldName . ' LIKE'] = '%' . $params['quickFilter'] . '%';
|
2021-01-15 11:32:47 +01:00
|
|
|
} else {
|
|
|
|
$queryConditions[$filterField] = $params['quickFilter'];
|
|
|
|
}
|
2021-10-28 09:00:20 +02:00
|
|
|
} else {
|
|
|
|
$queryConditions[$filterField] = $params['quickFilter'];
|
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
2021-10-28 09:00:20 +02:00
|
|
|
return $queryConditions;
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
|
2021-02-26 10:36:06 +01:00
|
|
|
protected function setFilters($params, \Cake\ORM\Query $query, array $options): \Cake\ORM\Query
|
2020-06-09 15:57:16 +02:00
|
|
|
{
|
2021-02-26 10:36:06 +01:00
|
|
|
$filteringLabel = !empty($params['filteringLabel']) ? $params['filteringLabel'] : '';
|
|
|
|
unset($params['filteringLabel']);
|
2021-08-30 15:11:21 +02:00
|
|
|
$filteringTags = !empty($params['filteringTags']) && $this->taggingSupported() ? $params['filteringTags'] : '';
|
|
|
|
unset($params['filteringTags']);
|
2021-02-26 10:36:06 +01:00
|
|
|
$customFilteringFunction = '';
|
|
|
|
$chosenFilter = '';
|
|
|
|
if (!empty($options['contextFilters']['custom'])) {
|
|
|
|
foreach ($options['contextFilters']['custom'] as $filter) {
|
|
|
|
if ($filter['label'] == $filteringLabel) {
|
|
|
|
$customFilteringFunction = $filter;
|
|
|
|
$chosenFilter = $filter;
|
|
|
|
break;
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 10:36:06 +01:00
|
|
|
|
2021-03-10 09:36:45 +01:00
|
|
|
$activeFilters = [];
|
2021-02-26 10:36:06 +01:00
|
|
|
if (!empty($customFilteringFunction['filterConditionFunction'])) {
|
|
|
|
$query = $customFilteringFunction['filterConditionFunction']($query);
|
2021-03-10 09:36:45 +01:00
|
|
|
$activeFilters['filteringLabel'] = $filteringLabel;
|
2021-02-26 10:36:06 +01:00
|
|
|
} else {
|
|
|
|
if (!empty($chosenFilter)) {
|
|
|
|
$params = $this->massageFilters($chosenFilter['filterCondition']);
|
|
|
|
} else {
|
|
|
|
$params = $this->massageFilters($params);
|
|
|
|
}
|
|
|
|
if (!empty($params['simpleFilters'])) {
|
|
|
|
foreach ($params['simpleFilters'] as $filter => $filterValue) {
|
2021-03-10 09:36:45 +01:00
|
|
|
$activeFilters[$filter] = $filterValue;
|
2021-02-26 10:36:06 +01:00
|
|
|
if ($filter === 'quickFilter') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (is_array($filterValue)) {
|
|
|
|
$query->where([($filter . ' IN') => $filterValue]);
|
|
|
|
} else {
|
|
|
|
$query = $this->setValueCondition($query, $filter, $filterValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($params['relatedFilters'])) {
|
|
|
|
foreach ($params['relatedFilters'] as $filter => $filterValue) {
|
2021-03-10 09:36:45 +01:00
|
|
|
$activeFilters[$filter] = $filterValue;
|
2021-02-26 10:36:06 +01:00
|
|
|
$filterParts = explode('.', $filter);
|
|
|
|
$query = $this->setNestedRelatedCondition($query, $filterParts, $filterValue);
|
|
|
|
}
|
2020-06-09 15:57:16 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-30 15:11:21 +02:00
|
|
|
|
|
|
|
if ($this->taggingSupported() && !empty($filteringTags)) {
|
|
|
|
$activeFilters['filteringTags'] = $filteringTags;
|
|
|
|
$query = $this->setTagFilters($query, $filteringTags);
|
|
|
|
}
|
|
|
|
|
2021-03-10 09:36:45 +01:00
|
|
|
$this->Controller->set('activeFilters', $activeFilters);
|
2020-06-09 15:57:16 +02:00
|
|
|
return $query;
|
|
|
|
}
|
2020-11-20 11:09:24 +01:00
|
|
|
|
2021-08-30 15:11:21 +02:00
|
|
|
protected function setTagFilters($query, $tags)
|
|
|
|
{
|
|
|
|
$modelAlias = $this->Table->getAlias();
|
|
|
|
$subQuery = $this->Table->find('tagged', [
|
2021-10-01 15:13:18 +02:00
|
|
|
'name' => $tags,
|
2021-11-03 11:47:10 +01:00
|
|
|
'OperatorAND' => true
|
2021-08-30 15:11:21 +02:00
|
|
|
])->select($modelAlias . '.id');
|
2021-09-01 16:12:56 +02:00
|
|
|
return $query->where([$modelAlias . '.id IN' => $subQuery]);
|
2021-08-30 15:11:21 +02:00
|
|
|
}
|
|
|
|
|
2021-02-24 11:11:29 +01:00
|
|
|
protected function setNestedRelatedCondition($query, $filterParts, $filterValue)
|
|
|
|
{
|
|
|
|
$modelName = $filterParts[0];
|
|
|
|
if (count($filterParts) == 2) {
|
|
|
|
$fieldName = implode('.', $filterParts);
|
|
|
|
$query = $this->setRelatedCondition($query, $modelName, $fieldName, $filterValue);
|
|
|
|
} else {
|
|
|
|
$filterParts = array_slice($filterParts, 1);
|
2021-11-04 08:10:32 +01:00
|
|
|
$query = $query->matching($modelName, function (\Cake\ORM\Query $q) use ($filterParts, $filterValue) {
|
2021-02-24 11:11:29 +01:00
|
|
|
return $this->setNestedRelatedCondition($q, $filterParts, $filterValue);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setRelatedCondition($query, $modelName, $fieldName, $filterValue)
|
|
|
|
{
|
2021-11-04 08:10:32 +01:00
|
|
|
return $query->matching($modelName, function (\Cake\ORM\Query $q) use ($fieldName, $filterValue) {
|
2021-02-24 11:11:29 +01:00
|
|
|
return $this->setValueCondition($q, $fieldName, $filterValue);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setValueCondition($query, $fieldName, $value)
|
|
|
|
{
|
|
|
|
if (strlen(trim($value, '%')) === strlen($value)) {
|
|
|
|
return $query->where([$fieldName => $value]);
|
|
|
|
} else {
|
2021-03-10 09:37:22 +01:00
|
|
|
return $query->where(function ($exp, \Cake\ORM\Query $q) use ($fieldName, $value) {
|
|
|
|
return $exp->like($fieldName, $value);
|
|
|
|
});
|
2021-02-24 11:11:29 +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 {
|
2021-03-15 22:47:13 +01:00
|
|
|
$contextFromFieldText = sprintf('%s: %s', $field, $contextFromField);
|
2021-01-14 15:30:16 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-03-10 09:38:41 +01:00
|
|
|
public function setParentConditionsForMetaFields($query, array $metaConditions)
|
2021-02-26 10:36:06 +01:00
|
|
|
{
|
|
|
|
$metaTemplates = $this->MetaFields->MetaTemplates->find('list', [
|
|
|
|
'keyField' => 'name',
|
|
|
|
'valueField' => 'id'
|
|
|
|
])->where(['name IN' => array_keys($metaConditions)])->all()->toArray();
|
|
|
|
$fieldsConditions = [];
|
|
|
|
foreach ($metaConditions as $templateName => $templateConditions) {
|
|
|
|
$metaTemplateID = isset($metaTemplates[$templateName]) ? $metaTemplates[$templateName] : -1;
|
|
|
|
foreach ($templateConditions as $conditions) {
|
|
|
|
$conditions['meta_template_id'] = $metaTemplateID;
|
|
|
|
$fieldsConditions[] = $conditions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$matchingMetaQuery = $this->getParentIDQueryForMetaANDConditions($fieldsConditions);
|
|
|
|
return $query->where(['id IN' => $matchingMetaQuery]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getParentIDQueryForMetaANDConditions(array $metaANDConditions)
|
|
|
|
{
|
|
|
|
if (empty($metaANDConditions)) {
|
|
|
|
throw new Exception('Invalid passed conditions');
|
|
|
|
}
|
|
|
|
foreach ($metaANDConditions as $i => $conditions) {
|
|
|
|
$metaANDConditions[$i]['scope'] = $this->Table->metaFields;
|
|
|
|
}
|
|
|
|
$firstCondition = $this->prefixConditions('MetaFields', $metaANDConditions[0]);
|
|
|
|
$conditionsToJoin = array_slice($metaANDConditions, 1);
|
|
|
|
$query = $this->MetaFields->find()
|
|
|
|
->select('parent_id')
|
|
|
|
->where($firstCondition);
|
|
|
|
foreach ($conditionsToJoin as $i => $conditions) {
|
|
|
|
$joinedConditions = $this->prefixConditions("m{$i}", $conditions);
|
|
|
|
$joinedConditions[] = "m{$i}.parent_id = MetaFields.parent_id";
|
|
|
|
$query->rightJoin(
|
|
|
|
["m{$i}" => 'meta_fields'],
|
|
|
|
$joinedConditions
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function prefixConditions(string $prefix, array $conditions)
|
|
|
|
{
|
|
|
|
$prefixedConditions = [];
|
|
|
|
foreach ($conditions as $condField => $condValue) {
|
|
|
|
$prefixedConditions["${prefix}.${condField}"] = $condValue;
|
|
|
|
}
|
|
|
|
return $prefixedConditions;
|
|
|
|
}
|
|
|
|
|
2021-08-26 12:06:12 +02:00
|
|
|
public function taggingSupported()
|
|
|
|
{
|
|
|
|
return $this->Table->behaviors()->has('Tag');
|
|
|
|
}
|
|
|
|
|
2021-08-26 16:12:55 +02:00
|
|
|
public function setAllTags()
|
|
|
|
{
|
|
|
|
$this->Tags = TableRegistry::getTableLocator()->get('Tags.Tags');
|
|
|
|
$allTags = $this->Tags->find()->all()->toList();
|
|
|
|
$this->Controller->set('allTags', $allTags);
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-11-04 08:10:32 +01:00
|
|
|
$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()) {
|
2021-06-17 08:54:09 +02:00
|
|
|
$this->Controller->ajaxResponsePayload = $this->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 {
|
2021-06-30 12:18:58 +02:00
|
|
|
$validationErrors = $data->getErrors();
|
|
|
|
$validationMessage = $this->prepareValidationMessage($validationErrors);
|
2020-12-07 14:17:10 +01:00
|
|
|
$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()) {
|
2021-06-30 12:18:58 +02:00
|
|
|
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, 'toggle', $message, $validationErrors);
|
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);
|
2021-06-17 08:54:09 +02:00
|
|
|
$this->Controller->restResponsePayload = $this->RestResponse->viewData(['value' => $data[$fieldName]], 'json');
|
2020-11-20 11:09:24 +01:00
|
|
|
} else {
|
|
|
|
if ($this->Controller->ParamHandler->isRest()) {
|
2021-06-17 08:54:09 +02:00
|
|
|
$this->Controller->restResponsePayload = $this->RestResponse->viewData(['value' => $data[$fieldName]], 'json');
|
2020-11-20 11:09:24 +01:00
|
|
|
} 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-19 12:28:46 +01:00
|
|
|
$association = $this->Table->associations()->get($model);
|
|
|
|
$associationType = $association->type();
|
|
|
|
if ($associationType == 'oneToMany') {
|
|
|
|
$fieldToExtract = $subField;
|
|
|
|
$associatedTable = $association->getTarget();
|
|
|
|
$query = $associatedTable->find()->rightJoin(
|
|
|
|
[$this->Table->getAlias() => $this->Table->getTable()],
|
|
|
|
[sprintf('%s.id = %s.%s', $this->Table->getAlias(), $associatedTable->getAlias(), $association->getForeignKey())]
|
|
|
|
)
|
2021-11-04 08:10:32 +01:00
|
|
|
->where([
|
|
|
|
["${field} IS NOT" => NULL]
|
|
|
|
]);
|
2021-01-19 12:28:46 +01:00
|
|
|
} else if ($associationType == 'manyToOne') {
|
2021-01-14 16:34:21 +01:00
|
|
|
$fieldToExtract = sprintf('%s.%s', Inflector::singularize(strtolower($model)), $subField);
|
2021-01-19 12:28:46 +01:00
|
|
|
$query = $this->Table->find()->contain($model);
|
|
|
|
} else {
|
|
|
|
throw new Exception("Association ${associationType} not supported in CRUD Component");
|
2021-01-14 16:34:21 +01:00
|
|
|
}
|
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
|
|
|
}
|