parent
8ea8bf6418
commit
1f32072d69
|
@ -16,7 +16,7 @@ class CRUDComponent extends Component
|
|||
$this->Table = $config['table'];
|
||||
$this->request = $config['request'];
|
||||
$this->TableAlias = $this->Table->getAlias();
|
||||
$this->ObjectAlias = \Cake\Utility\Inflector::singularize($this->TableAlias);
|
||||
$this->ObjectAlias = Inflector::singularize($this->TableAlias);
|
||||
$this->MetaFields = $config['MetaFields'];
|
||||
$this->MetaTemplates = $config['MetaTemplates'];
|
||||
}
|
||||
|
@ -349,6 +349,61 @@ class CRUDComponent extends Component
|
|||
return $query;
|
||||
}
|
||||
|
||||
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'])) {
|
||||
$data->{$fieldName} = !$data->{$fieldName};
|
||||
$data = $this->Table->save($data);
|
||||
if ($data !== false) {
|
||||
$message = __('{0}\'s `{1}` field: {2}. (ID: {3})',
|
||||
$this->ObjectAlias,
|
||||
$fieldName,
|
||||
$data->{$fieldName} ? __('enabled') : __('disabled'),
|
||||
$data->id,
|
||||
);
|
||||
if ($this->Controller->ParamHandler->isRest()) {
|
||||
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
|
||||
} else if ($this->Controller->ParamHandler->isAjax()) {
|
||||
$this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxSuccessResponse($this->ObjectAlias, 'toggle', $data, $message);
|
||||
} 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()) {
|
||||
$this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxFailResponse($this->ObjectAlias, 'toggle', $data, $message);
|
||||
} 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');
|
||||
}
|
||||
|
||||
public function toggleEnabled(int $id, array $path, string $fieldName = 'enabled'): bool
|
||||
{
|
||||
if (empty($id)) {
|
||||
|
|
|
@ -61,4 +61,13 @@ class ParamHandlerComponent extends Component
|
|||
{
|
||||
return (json_decode($data) != null) ? true : false;
|
||||
}
|
||||
|
||||
public function isAjax()
|
||||
{
|
||||
if ($this->isAjax !== null) {
|
||||
return $this->isAjax;
|
||||
}
|
||||
$this->isAjax = $this->request->is('ajax');
|
||||
return $this->isAjax;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace App\Controller\Component;
|
|||
|
||||
use Cake\Controller\Component;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Utility\Inflector;
|
||||
|
||||
class RestResponseComponent extends Component
|
||||
{
|
||||
|
@ -419,6 +420,28 @@ class RestResponseComponent extends Component
|
|||
return $this->__sendResponse($response, 200, $format);
|
||||
}
|
||||
|
||||
public function ajaxSuccessResponse($ObjectAlias, $action, $entity, $message)
|
||||
{
|
||||
$action = $this->__dissectAdminRouting($action);
|
||||
$response = [
|
||||
'success' => true,
|
||||
'message' => $message,
|
||||
'url' => $this->__generateURL($action, $ObjectAlias, $entity->id)
|
||||
];
|
||||
return $this->viewData($response);
|
||||
}
|
||||
|
||||
public function ajaxFailResponse($ObjectAlias, $action, $entity, $message)
|
||||
{
|
||||
$action = $this->__dissectAdminRouting($action);
|
||||
$response = [
|
||||
'success' => false,
|
||||
'message' => $message,
|
||||
'url' => $this->__generateURL($action, $ObjectAlias, $entity->id)
|
||||
];
|
||||
return $this->viewData($response);
|
||||
}
|
||||
|
||||
private function __sendResponse($response, $code, $format = false, $raw = false, $download = false, $headers = array())
|
||||
{
|
||||
if (strtolower($format) === 'application/xml' || strtolower($format) === 'xml') {
|
||||
|
|
|
@ -57,23 +57,11 @@ class MetaTemplatesController extends AppController
|
|||
|
||||
public function toggle($id)
|
||||
{
|
||||
$template = $this->MetaTemplates->getTemplate($id);
|
||||
$template['enabled'] = $template['enabled'] ? 0 : 1;
|
||||
$result = $this->MetaTemplates->save($template);
|
||||
if ($template['enabled']) {
|
||||
$message = $result ? __('Template enabled.') : __('Could not enable template');
|
||||
} else {
|
||||
$message = $result ? __('Template disabled.') : __('Could not disable template');
|
||||
}
|
||||
$this->CRUD->toggle($id);
|
||||
if ($this->ParamHandler->isRest()) {
|
||||
if ($result) {
|
||||
return $this->RestResponse->saveSuccessResponse('MetaTemplates', 'toggle', $id, 'json', $message);
|
||||
} else {
|
||||
return $this->RestResponse->saveFailResponse('MetaTemplates', 'toggle', $id, 'json', $message);
|
||||
}
|
||||
} else {
|
||||
if ($this->Flash->{$result ? 'success' : 'error'}($message));
|
||||
$this->redirect($this->referer());
|
||||
return $this->restResponsePayload;
|
||||
} else if($this->ParamHandler->isAjax() && $this->request->is(['post', 'put'])) {
|
||||
return $this->ajaxResponsePayload;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,9 @@ echo $this->element('genericElements/IndexTable/index_table', [
|
|||
'name' => 'Enabled',
|
||||
'sort' => 'enabled',
|
||||
'data_path' => 'enabled',
|
||||
'element' => 'boolean'
|
||||
'element' => 'toggle',
|
||||
'url' => '/metaTemplates/toggle',
|
||||
'url_params_data_paths' => ['id'],
|
||||
],
|
||||
[
|
||||
'name' => __('Scope'),
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
$checkboxId = 'GenericToggle-' . $seed;
|
||||
$tempboxId = 'TempBox-' . $seed;
|
||||
echo sprintf(
|
||||
'<input type="checkbox" id="%s" %s><span id="%s" class="hidden">',
|
||||
'<input type="checkbox" id="%s" %s><span id="%s" class="d-none">',
|
||||
$checkboxId,
|
||||
empty($data[0]) ? '' : 'checked',
|
||||
$tempboxId
|
||||
|
@ -19,23 +19,26 @@
|
|||
?>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function() {
|
||||
var url = baseurl + "<?= h($field['url']) ?>";
|
||||
var url = "<?= h($field['url']) ?>";
|
||||
<?php
|
||||
if (!empty($field['url_params_data_paths'][0])) {
|
||||
$id = $this->Hash->extract($row, $field['url_params_data_paths'][0]);
|
||||
echo 'url = url + "/' . h($id[0]) . '";';
|
||||
}
|
||||
?>
|
||||
$('#<?= $checkboxId ?>').on('click', function() {
|
||||
$('#<?= $checkboxId ?>').click(function(evt) {
|
||||
evt.preventDefault();
|
||||
$.ajax({
|
||||
type:"get",
|
||||
url: url,
|
||||
error:function() {
|
||||
showMessage('fail', '<?= __('Could not retrieve current state.') ?>.');
|
||||
showToast({
|
||||
variant: 'danger',
|
||||
title: '<?= __('Could not retrieve current state.') ?>.'
|
||||
})
|
||||
},
|
||||
success: function (data, textStatus) {
|
||||
$('#<?= $tempboxId ?>').html(data);
|
||||
// Make @mokaddem aka Graphman happy
|
||||
var $form = $('#<?= $tempboxId ?>').find('form');
|
||||
$.ajax({
|
||||
data: $form.serialize(),
|
||||
|
@ -43,10 +46,19 @@ $(document).ready(function() {
|
|||
type:"post",
|
||||
url: $form.attr('action'),
|
||||
success:function(data, textStatus) {
|
||||
showMessage('success', '<?= __('Field updated.') ?>.');
|
||||
showToast({
|
||||
variant: 'success',
|
||||
title: data.message
|
||||
})
|
||||
if (data.success) {
|
||||
$('#<?= $checkboxId ?>').prop('checked', !$('#<?= $checkboxId ?>').prop('checked'));
|
||||
}
|
||||
},
|
||||
error:function() {
|
||||
showMessage('fail', '<?= __('Could not update field.') ?>.');
|
||||
showToast({
|
||||
variant: 'danger',
|
||||
title: data.message
|
||||
})
|
||||
},
|
||||
complete:function() {
|
||||
$('#<?= $tempboxId ?>').empty();
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
<?= $this->Form->postLink(__('Toggle'), ['action' => 'toggle', $entity->id], ['confirm' => __('Are you sure you want to toggle {0} of {1}?', $fieldName. $entity->id)]) ?>
|
Loading…
Reference in New Issue