Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
added singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloamgomes committed Sep 15, 2018
1 parent 0d2a3dd commit 606d888
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
50 changes: 48 additions & 2 deletions Controller/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Admin extends AuthController {
protected $definitions = [
'collections' => 'collections',
'regions' => 'regions',
'singletons' => 'singletons',
'forms' => 'forms',
'config' => 'config',
'accounts' => 'accounts',
Expand Down Expand Up @@ -155,7 +156,7 @@ public function view($backup) {

$regions = [];
// Regions are deprecated and supported only with legacy module.
if ($info['backup']['regions'] && $this->app->module('regions') instanceof Lime\Module) {
if (isset($info['backup']['regions']) && $info['backup']['regions'] && $this->app->module('regions') instanceof Lime\Module) {
$data = Spyc::YAMLLoad($zip->getFromName('regions.yaml', 0, ZipArchive::FL_NODIR));
if ($data && isset($data['regions'])) {
foreach ($data['regions'] as $region) {
Expand All @@ -164,6 +165,17 @@ public function view($backup) {
}
}

$singletons = [];
// Singletons are a replace to regions and therefore not present in old backups.
if (isset($info['backup']['singletons']) && $info['backup']['singletons']) {
$data = Spyc::YAMLLoad($zip->getFromName('singletons.yaml', 0, ZipArchive::FL_NODIR));
if ($data && isset($data['singletons'])) {
foreach ($data['singletons'] as $singleton) {
$singletons[] = !empty($singleton['label']) ? $singleton['label'] : $singleton['name'];
}
}
}

$forms = [];
if ($info['backup']['forms']) {
$data = Spyc::YAMLLoad($zip->getFromName('forms.yaml', 0, ZipArchive::FL_NODIR));
Expand Down Expand Up @@ -239,6 +251,7 @@ public function view($backup) {
'config' => $config,
'collections' => array_values($collections),
'regions' => $regions,
'singletons' => $singletons,
'forms' => $forms,
'accounts' => $accounts,
'webhooks' => $webhooks,
Expand Down Expand Up @@ -463,13 +476,46 @@ protected function restoreRegions($zip, $zipHandle, $fullRestore) {
if ($fullRestore || !isset($regions[$name])) {
$this->module("regions")->createRegion($name, $region);
}
elseif (isset($collections[$name])) {
elseif (isset($regions[$name])) {
$this->module("regions")->updateRegion($name, $region);
}
}
}
}

/**
* Restore a set of singletons from a backup zip file.
*
* @param object $zip
* The ZipArchive object that contains the backup.
* @param resource $zipHandle
* A Zip Handler.
* @param bool $fullRestore
* Flag to define if its a full or partial restore.
*/
protected function restoreSingletons($zip, $zipHandle, $fullRestore) {
$data = Spyc::YAMLLoad($zip->getFromName('singletons.yaml', 0, ZipArchive::FL_NODIR));
$singletons = $this->module('singletons')->singletons();

if ($data && isset($data['singletons'])) {
if ($fullRestore && !empty($singletons)) {
foreach ($singletons as $singleton) {
$this->module('singletons')->removeSingleton($singleton['name']);
}
}

foreach ($data['singletons'] as $singleton) {
$name = $singleton['name'];
if ($fullRestore || !isset($singletons[$name])) {
$this->module("singletons")->createSingleton($name, $singleton);
}
elseif (isset($singletons[$name])) {
$this->module("singletons")->updateSingleton($name, $singleton);
}
}
}
}

/**
* Restore a set of forms from a backup zip file.
*
Expand Down
6 changes: 6 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
$zip->addFromString('/data/regions.yaml', \Spyc::YAMLDump(['regions' => $regions], TRUE));
}

// Save singletons.
if ($settings['singletons']) {
$singletons = $this->app->module('singletons')->singletons();
$zip->addFromString('/data/singletons.yaml', \Spyc::YAMLDump(['singletons' => $singletons], TRUE));
}

// Save forms.
if ($settings['forms']) {
$forms = $this->app->module('forms')->forms();
Expand Down
1 change: 1 addition & 0 deletions views/backups/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
this.options = [
{'key': 'config', 'label': 'Global cockpit configuration', 'value': true, 'disabled': false},
{'key': 'collections', 'label': 'Collections definitions', 'value': true, 'disabled': false},
{'key': 'singletons', 'label': 'Singletons definitions', 'value': true, 'disabled': false},
{'key': 'forms', 'label': 'Forms definitions', 'value': true, 'disabled': false},
{'key': 'accounts', 'label': 'User accounts', 'value': true, 'disabled': false},
{'key': 'webhooks', 'label': 'Webhooks definitions', 'value': true, 'disabled': false},
Expand Down
10 changes: 10 additions & 0 deletions views/backups/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
</span>
</td>
</tr>
<tr>
<td>@lang('Singletons')</td>
<td>
<div if="{ !info.singletons }">@lang('No singletons saved')</div>
<span class="uk-badge uk-margin-small-left" each="{ singleton in singletons }" if="{ info.singletons && singletons.length }">
{ singleton }
</span>
</td>
</tr>
<tr if="{info.regions}">
<td>@lang('Regions')</td>
<td>
Expand Down Expand Up @@ -148,6 +157,7 @@
this.info = {{ json_encode($info) }};
this.config = {{ json_encode($config) }};
this.collections = {{ json_encode($collections) }};
this.singletons = {{ json_encode($singletons) }};
this.regions = {{ json_encode($regions) }};
this.forms = {{ json_encode($forms) }};
this.accounts = {{ json_encode($accounts) }};
Expand Down

0 comments on commit 606d888

Please sign in to comment.