chg: [metaTemplate] Started implementing new update system - WiP

pull/93/head
Sami Mokaddem 2021-11-24 09:14:09 +01:00
parent 94c0b171a1
commit aa42e6763a
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
4 changed files with 86 additions and 42 deletions

View File

@ -23,7 +23,7 @@ class MetaTemplatesNavigation extends BaseNavigation
'url_vars' => ['id' => 'id'],
]);
$this->bcf->addRoute('MetaTemplates', 'update', [
'label' => __('Update'),
'label' => __('Update all templates'),
'icon' => 'download',
'url' => '/metaTemplates/update',
]);
@ -42,6 +42,11 @@ class MetaTemplatesNavigation extends BaseNavigation
public function addActions()
{
$this->bcf->addAction('MetaTemplates', 'index', 'MetaTemplates', 'update');
$this->bcf->addAction('MetaTemplates', 'view', 'MetaTemplates', 'update', [
'label' => __('Update template'),
'url' => '/metaTemplates/update/{{id}}',
'url_vars' => ['id' => 'id'],
]);
$this->bcf->addAction('MetaTemplates', 'view', 'MetaTemplates', 'enable');
$this->bcf->addAction('MetaTemplates', 'view', 'MetaTemplates', 'set_default');
}

View File

@ -13,20 +13,36 @@ class MetaTemplatesController extends AppController
public $filterFields = ['name', 'uuid', 'scope', 'namespace'];
public $containFields = ['MetaTemplateFields'];
public function update()
public function update($template_id=false)
{
if (!empty($template_id)) {
$metaTemplate = $this->MetaTemplates->get($template_id);
}
if ($this->request->is('post')) {
$result = $this->MetaTemplates->update();
$result = $this->MetaTemplates->update($template_id);
if ($this->ParamHandler->isRest()) {
return $this->RestResponse->viewData($result, 'json');
} else {
$this->Flash->success(__('{0} templates updated.', count($result)));
$this->redirect($this->referer());
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;
}
}
} else {
if (!$this->ParamHandler->isRest()) {
if (!empty($template_id)) {
$this->set('title', __('Update Meta Templates #{0}', h($template_id)));
$this->set('question', __('Are you sure you wish to update the Meta Template definitions of the template `{0}`?', h($metaTemplate->name)));
} else {
$this->set('title', __('Update Meta Templates'));
$this->set('question', __('Are you sure you wish to update the Meta Template definitions?'));
$this->set('question', __('Are you sure you wish to update the Meta Template definitions'));
}
$this->set('actionName', __('Update'));
$this->set('path', ['controller' => 'metaTemplates', 'action' => 'update']);
$this->render('/genericTemplates/confirm');

View File

@ -46,7 +46,7 @@ class MetaTemplatesTable extends AppTable
$files = scandir($path);
foreach ($files as $k => $file) {
if (substr($file, -5) === '.json') {
if ($this->loadMetaFile($path . $file) === true) {
if ($this->loadAndSaveMetaFile($path . $file) === true) {
$files_processed[] = $file;
}
}
@ -89,46 +89,62 @@ class MetaTemplatesTable extends AppTable
);
}
public function loadMetaFile(String $filePath)
public function loadAndSaveMetaFile(String $filePath)
{
if (file_exists($filePath)) {
$contents = file_get_contents($filePath);
$metaTemplate = json_decode($contents, true);
if (!empty($metaTemplate) && !empty($metaTemplate['uuid']) && !empty($metaTemplate['version'])) {
if (empty($metaTemplate)) {
return __('Could not load template file. Error while decoding the template\'s JSON');
}
if (empty($metaTemplate['uuid']) || empty($metaTemplate['version'])) {
return __('Could not load template file. Invalid template file. Missing template UUID or version');
}
return $this->saveMetaFile($metaTemplate);
}
return __('Could not load template file. File does not exists');
}
public function saveMetaFile(array $newMetaTemplate)
{
$query = $this->find();
$query->where(['uuid' => $metaTemplate['uuid']]);
$template = $query->first();
if (empty($template)) {
$template = $this->newEntity($metaTemplate);
$result = $this->save($template);
$query->contain('MetaTemaplteFields')->where(['uuid' => $newMetaTemplate['uuid']]);
$metaTemplate = $query->first();
if (empty($metaTemplate)) {
$metaTemplate = $this->newEntity($newMetaTemplate);
$result = $this->save($metaTemplate);
if (!$result) {
return __('Something went wrong, could not create the template.');
}
} else {
if ($template->version >= $metaTemplate['version']) {
return false;
if ($metaTemplate->version >= $newMetaTemplate['version']) {
return __('Could not update the template. Local version is newer.');
}
foreach (['version', 'source', 'name', 'namespace', 'scope', 'description'] as $field) {
$template->{$field} = $metaTemplate[$field];
}
$result = $this->save($template);
if (!$result) {
// Take care of meta template fields
$metaTemplate = $this->patchEntity($metaTemplate, $newMetaTemplate);
$metaTemplate = $this->save($metaTemplate);
if (!$metaTemplate) {
return __('Something went wrong, could not update the template.');
return false;
}
}
if ($result) {
$this->MetaTemplateFields->deleteAll(['meta_template_id' => $template->id]);
foreach ($metaTemplate['metaFields'] as $metaField) {
foreach ($newMetaTemplate['metaFields'] as $metaField) {
$metaField['meta_template_id'] = $template->id;
$metaField = $this->MetaTemplateFields->newEntity($metaField);
$this->MetaTemplateFields->save($metaField);
}
}
}
public function handleMetaTemplateFieldUpdateEdgeCase($metaTemplateField, $newMetaTemplateField)
{
if (false) { // Field has been removed
}
if (false) { // Field no longer multiple
}
if (false) { // Field no longer pass validation
}
return true;
}
}
}

View File

@ -149,6 +149,13 @@ echo $this->element('genericElements/IndexTable/index_table', [
'url_params_data_paths' => ['id'],
'icon' => 'eye'
],
[
'url' => '/metaTemplates/update',
'url_params_data_paths' => ['id'],
'title' => __('Update'),
'icon' => 'download',
'requirement' => true // FIXME: Check if template can be updated
]
]
]
]);