2020-09-28 01:25:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use App\Controller\AppController;
|
|
|
|
use Cake\Utility\Hash;
|
|
|
|
use Cake\Utility\Text;
|
|
|
|
use \Cake\Database\Expression\QueryExpression;
|
|
|
|
|
|
|
|
class MetaTemplatesController extends AppController
|
|
|
|
{
|
2021-11-09 09:11:35 +01:00
|
|
|
public $quickFilterFields = [['name' => true], 'uuid', ['scope' => true]];
|
2021-09-10 11:55:54 +02:00
|
|
|
public $filterFields = ['name', 'uuid', 'scope', 'namespace'];
|
|
|
|
public $containFields = ['MetaTemplateFields'];
|
2020-12-08 15:08:12 +01:00
|
|
|
|
2021-11-24 09:14:09 +01:00
|
|
|
public function update($template_id=false)
|
2020-09-28 01:25:07 +02:00
|
|
|
{
|
2021-11-24 09:14:09 +01:00
|
|
|
if (!empty($template_id)) {
|
|
|
|
$metaTemplate = $this->MetaTemplates->get($template_id);
|
|
|
|
}
|
2020-11-20 11:09:24 +01:00
|
|
|
if ($this->request->is('post')) {
|
2021-11-24 09:14:09 +01:00
|
|
|
$result = $this->MetaTemplates->update($template_id);
|
2020-11-20 11:09:24 +01:00
|
|
|
if ($this->ParamHandler->isRest()) {
|
|
|
|
return $this->RestResponse->viewData($result, 'json');
|
|
|
|
} else {
|
2021-11-24 09:14:09 +01:00
|
|
|
if ($result['success']) {
|
|
|
|
$message = __n('{0} templates updated.', 'The template has been updated.', empty($template_id), $result['updated']);
|
|
|
|
} else {
|
|
|
|
$message = __n('{0} templates could not be updated.', 'The template could not be updated.',empty($template_id), $result['updated']);
|
|
|
|
}
|
|
|
|
$this->CRUD->setResponseForController('update', $result['success'], $message, $metaTemplate, $metaTemplate->getErrors(), ['redirect' => $this->referer()]);
|
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
|
|
|
}
|
2020-11-20 11:09:24 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!$this->ParamHandler->isRest()) {
|
2021-11-24 09:14:09 +01:00
|
|
|
if (!empty($template_id)) {
|
2021-12-01 11:01:31 +01:00
|
|
|
$this->set('metaTemplate', $metaTemplate);
|
|
|
|
$this->setUpdateStatus($metaTemplate->id);
|
2021-11-24 09:14:09 +01:00
|
|
|
} else {
|
|
|
|
$this->set('title', __('Update Meta Templates'));
|
|
|
|
$this->set('question', __('Are you sure you wish to update the Meta Template definitions'));
|
2021-12-01 11:01:31 +01:00
|
|
|
$updateableTemplates = $this->MetaTemplates->checkForUpdates();
|
|
|
|
$this->set('updateableTemplates', $updateableTemplates);
|
|
|
|
$this->render('updateAll');
|
2021-11-24 09:14:09 +01:00
|
|
|
}
|
2020-11-20 11:09:24 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 01:25:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2021-12-01 11:01:31 +01:00
|
|
|
$updateableTemplate = $this->MetaTemplates->checkForUpdates();
|
2020-09-28 01:25:07 +02:00
|
|
|
$this->CRUD->index([
|
2021-09-10 11:55:54 +02:00
|
|
|
'filters' => $this->filterFields,
|
|
|
|
'quickFilters' => $this->quickFilterFields,
|
2020-12-08 15:08:12 +01:00
|
|
|
'contextFilters' => [
|
|
|
|
'fields' => ['scope'],
|
|
|
|
'custom' => [
|
|
|
|
[
|
2021-11-09 09:11:35 +01:00
|
|
|
'label' => __('Default Templates'),
|
|
|
|
'filterCondition' => ['is_default' => true]
|
2020-12-08 15:08:12 +01:00
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
2021-12-01 11:01:31 +01:00
|
|
|
'contain' => $this->containFields,
|
|
|
|
'afterFind' => function($data) use ($updateableTemplate) {
|
|
|
|
foreach ($data as $i => $metaTemplate) {
|
|
|
|
if (!empty($updateableTemplate[$metaTemplate->uuid])) {
|
|
|
|
$metaTemplate->set('status', $this->MetaTemplates->getTemplateStatus($updateableTemplate[$metaTemplate->uuid]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2020-09-28 01:25:07 +02:00
|
|
|
]);
|
2021-06-29 16:15:05 +02:00
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
2020-09-28 01:25:07 +02:00
|
|
|
}
|
2021-12-01 11:01:31 +01:00
|
|
|
$updateableTemplate = [
|
|
|
|
'not_up_to_date' => $this->MetaTemplates->getNotUpToDateTemplates(),
|
|
|
|
'new' => $this->MetaTemplates->getNewTemplates(),
|
|
|
|
];
|
2020-12-10 16:50:46 +01:00
|
|
|
$this->set('defaultTemplatePerScope', $this->MetaTemplates->getDefaultTemplatePerScope());
|
2020-09-28 01:25:07 +02:00
|
|
|
$this->set('alignmentScope', 'individuals');
|
2021-12-01 11:01:31 +01:00
|
|
|
$this->set('updateableTemplate', $updateableTemplate);
|
2020-09-28 01:25:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function view($id)
|
|
|
|
{
|
|
|
|
$this->CRUD->view($id, [
|
|
|
|
'contain' => ['MetaTemplateFields']
|
|
|
|
]);
|
2021-06-29 16:15:05 +02:00
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
2020-09-28 01:25:07 +02:00
|
|
|
}
|
2021-12-01 11:01:31 +01:00
|
|
|
$this->setUpdateStatus($id);
|
2020-09-28 01:25:07 +02:00
|
|
|
}
|
2020-11-20 11:09:24 +01:00
|
|
|
|
2020-12-10 15:18:02 +01:00
|
|
|
public function toggle($id, $fieldName = 'enabled')
|
2020-11-20 11:09:24 +01:00
|
|
|
{
|
2020-12-10 17:18:17 +01:00
|
|
|
if ($this->request->is('POST') && $fieldName == 'is_default') {
|
|
|
|
$template = $this->MetaTemplates->get($id);
|
|
|
|
$this->MetaTemplates->removeDefaultFlag($template->scope);
|
|
|
|
$this->CRUD->toggle($id, $fieldName, ['force_state' => !$template->is_default]);
|
|
|
|
} else {
|
|
|
|
$this->CRUD->toggle($id, $fieldName);
|
|
|
|
}
|
2021-06-29 16:15:05 +02:00
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
2020-11-20 11:09:24 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-01 11:01:31 +01:00
|
|
|
|
|
|
|
public function setUpdateStatus($id)
|
|
|
|
{
|
|
|
|
$metaTemplate = $this->MetaTemplates->get($id);
|
|
|
|
$templateOnDisk = $this->MetaTemplates->readTemplateFromDisk($metaTemplate->uuid);
|
|
|
|
$updateableTemplate = $this->MetaTemplates->checkUpdatesForTemplate($templateOnDisk);
|
|
|
|
$this->set('updateableTemplate', $updateableTemplate);
|
|
|
|
$this->set('templateOnDisk', $templateOnDisk);
|
|
|
|
}
|
2020-09-28 01:25:07 +02:00
|
|
|
}
|