chg: [metaTemplate] Started implementing new update system - WiP
parent
94c0b171a1
commit
aa42e6763a
|
@ -23,7 +23,7 @@ class MetaTemplatesNavigation extends BaseNavigation
|
||||||
'url_vars' => ['id' => 'id'],
|
'url_vars' => ['id' => 'id'],
|
||||||
]);
|
]);
|
||||||
$this->bcf->addRoute('MetaTemplates', 'update', [
|
$this->bcf->addRoute('MetaTemplates', 'update', [
|
||||||
'label' => __('Update'),
|
'label' => __('Update all templates'),
|
||||||
'icon' => 'download',
|
'icon' => 'download',
|
||||||
'url' => '/metaTemplates/update',
|
'url' => '/metaTemplates/update',
|
||||||
]);
|
]);
|
||||||
|
@ -42,6 +42,11 @@ class MetaTemplatesNavigation extends BaseNavigation
|
||||||
public function addActions()
|
public function addActions()
|
||||||
{
|
{
|
||||||
$this->bcf->addAction('MetaTemplates', 'index', 'MetaTemplates', 'update');
|
$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', 'enable');
|
||||||
$this->bcf->addAction('MetaTemplates', 'view', 'MetaTemplates', 'set_default');
|
$this->bcf->addAction('MetaTemplates', 'view', 'MetaTemplates', 'set_default');
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,20 +13,36 @@ class MetaTemplatesController extends AppController
|
||||||
public $filterFields = ['name', 'uuid', 'scope', 'namespace'];
|
public $filterFields = ['name', 'uuid', 'scope', 'namespace'];
|
||||||
public $containFields = ['MetaTemplateFields'];
|
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')) {
|
if ($this->request->is('post')) {
|
||||||
$result = $this->MetaTemplates->update();
|
$result = $this->MetaTemplates->update($template_id);
|
||||||
if ($this->ParamHandler->isRest()) {
|
if ($this->ParamHandler->isRest()) {
|
||||||
return $this->RestResponse->viewData($result, 'json');
|
return $this->RestResponse->viewData($result, 'json');
|
||||||
} else {
|
} else {
|
||||||
$this->Flash->success(__('{0} templates updated.', count($result)));
|
if ($result['success']) {
|
||||||
$this->redirect($this->referer());
|
$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 {
|
} else {
|
||||||
if (!$this->ParamHandler->isRest()) {
|
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('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('actionName', __('Update'));
|
||||||
$this->set('path', ['controller' => 'metaTemplates', 'action' => 'update']);
|
$this->set('path', ['controller' => 'metaTemplates', 'action' => 'update']);
|
||||||
$this->render('/genericTemplates/confirm');
|
$this->render('/genericTemplates/confirm');
|
||||||
|
|
|
@ -46,7 +46,7 @@ class MetaTemplatesTable extends AppTable
|
||||||
$files = scandir($path);
|
$files = scandir($path);
|
||||||
foreach ($files as $k => $file) {
|
foreach ($files as $k => $file) {
|
||||||
if (substr($file, -5) === '.json') {
|
if (substr($file, -5) === '.json') {
|
||||||
if ($this->loadMetaFile($path . $file) === true) {
|
if ($this->loadAndSaveMetaFile($path . $file) === true) {
|
||||||
$files_processed[] = $file;
|
$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)) {
|
if (file_exists($filePath)) {
|
||||||
$contents = file_get_contents($filePath);
|
$contents = file_get_contents($filePath);
|
||||||
$metaTemplate = json_decode($contents, true);
|
$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 = $this->find();
|
||||||
$query->where(['uuid' => $metaTemplate['uuid']]);
|
$query->contain('MetaTemaplteFields')->where(['uuid' => $newMetaTemplate['uuid']]);
|
||||||
$template = $query->first();
|
$metaTemplate = $query->first();
|
||||||
if (empty($template)) {
|
if (empty($metaTemplate)) {
|
||||||
$template = $this->newEntity($metaTemplate);
|
$metaTemplate = $this->newEntity($newMetaTemplate);
|
||||||
$result = $this->save($template);
|
$result = $this->save($metaTemplate);
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
return __('Something went wrong, could not create the template.');
|
return __('Something went wrong, could not create the template.');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ($template->version >= $metaTemplate['version']) {
|
if ($metaTemplate->version >= $newMetaTemplate['version']) {
|
||||||
return false;
|
return __('Could not update the template. Local version is newer.');
|
||||||
}
|
}
|
||||||
foreach (['version', 'source', 'name', 'namespace', 'scope', 'description'] as $field) {
|
// Take care of meta template fields
|
||||||
$template->{$field} = $metaTemplate[$field];
|
$metaTemplate = $this->patchEntity($metaTemplate, $newMetaTemplate);
|
||||||
}
|
$metaTemplate = $this->save($metaTemplate);
|
||||||
$result = $this->save($template);
|
if (!$metaTemplate) {
|
||||||
if (!$result) {
|
|
||||||
return __('Something went wrong, could not update the template.');
|
return __('Something went wrong, could not update the template.');
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($result) {
|
if ($result) {
|
||||||
$this->MetaTemplateFields->deleteAll(['meta_template_id' => $template->id]);
|
$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['meta_template_id'] = $template->id;
|
||||||
$metaField = $this->MetaTemplateFields->newEntity($metaField);
|
$metaField = $this->MetaTemplateFields->newEntity($metaField);
|
||||||
$this->MetaTemplateFields->save($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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,6 +149,13 @@ echo $this->element('genericElements/IndexTable/index_table', [
|
||||||
'url_params_data_paths' => ['id'],
|
'url_params_data_paths' => ['id'],
|
||||||
'icon' => 'eye'
|
'icon' => 'eye'
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
'url' => '/metaTemplates/update',
|
||||||
|
'url_params_data_paths' => ['id'],
|
||||||
|
'title' => __('Update'),
|
||||||
|
'icon' => 'download',
|
||||||
|
'requirement' => true // FIXME: Check if template can be updated
|
||||||
|
]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
Loading…
Reference in New Issue