diff --git a/build/api/core/AuthSystemDefault.php b/build/api/core/AuthSystemDefault.php
index 59d73a1..a7d563b 100644
--- a/build/api/core/AuthSystemDefault.php
+++ b/build/api/core/AuthSystemDefault.php
@@ -26,6 +26,7 @@
if( !isset($_SESSION['CAS']) || !is_array($_SESSION['CAS']) ) $_SESSION['CAS'] = [];
if( !isset($_SESSION['AUTH']) || !is_array($_SESSION['AUTH']) ) $_SESSION['AUTH'] = [];
if( !isset($_SESSION['AvailableDepartment']) || !is_array($_SESSION['AvailableDepartment']) ) $_SESSION['AvailableDepartment'] = [];
+ if( !isset($_SESSION['VERSION']) || !is_string($_SESSION['VERSION']) ) $_SESSION['VERSION'] = null;
/* (2) Check CAS
diff --git a/build/api/module/department/versionController.php b/build/api/module/department/versionController.php
index 0a2d063..4bea994 100644
--- a/build/api/module/department/versionController.php
+++ b/build/api/module/department/versionController.php
@@ -124,7 +124,7 @@ class versionController{
);
/* (3) Return versions */
- return ['versions' => $versions];
+ return ['versions' => $versions ];
}
@@ -218,7 +218,7 @@ class versionController{
/** @var department $dept_repo */
$dept_repo = Repo::getRepo('department');
- /* (2) Check whether we have to [apply OR preview] */
+ /* (3) Check whether we have to [apply OR preview] */
$apply = ( $apply === '1' );
@@ -230,6 +230,7 @@ class versionController{
/* (1) Reset database to 'prod' */
$_SESSION['CurrentDatabase'] = $this->origin_dbname;
+ $_SESSION['VERSION'] = null;
/* (2) Return success */
return [ 'updated' => true ];
@@ -261,6 +262,7 @@ class versionController{
/* (1) Restore from this version */
$restored = $dept_repo->restore($this->origin_dbname, $snapshot);
+ $_SESSION['VERSION'] = null;
/* (2) Dispatch repo execution status */
return [ 'updated' => $restored ];
@@ -286,6 +288,7 @@ class versionController{
/* (3) Store 'preview' database in session */
$_SESSION['CurrentDatabase'] = $preview_dbname;
+ $_SESSION['VERSION'] = $version;
/* (4) Return status */
diff --git a/build/api/module/departmentController.php b/build/api/module/departmentController.php
index 747f0a4..513aa95 100644
--- a/build/api/module/departmentController.php
+++ b/build/api/module/departmentController.php
@@ -33,12 +33,12 @@ class departmentController
$_SESSION['CurrentDatabase'] = $dep["dbName"];
$_SESSION['CurrentDepartementId'] = $dep["idDep"];
- return ["success" => true];
+ return ['switched' => true];
}
}
}
- return ["success" => false];
+ return ['switched' => false];
}
diff --git a/build/router/controller/js.php b/build/router/controller/js.php
index 71c3c4e..4dabea1 100644
--- a/build/router/controller/js.php
+++ b/build/router/controller/js.php
@@ -31,7 +31,8 @@
'name' => $_SESSION['CAS']['login'],
'connected' => isset($_SESSION['AUTH']) ? count($_SESSION['AUTH']) > 0 : false,
'departments' => array_map(function($d){ return [ 'id' => $d['idDep'], 'label' => $d['labelDep']]; }, $_SESSION['AvailableDepartment']),
- 'department_id' => $_SESSION['CurrentDepartementId']
+ 'department_id' => $_SESSION['CurrentDepartementId'],
+ 'version' => $_SESSION['VERSION']
]
])."\n";
diff --git a/config/modules.json b/config/modules.json
index 9247ab8..81ab578 100644
--- a/config/modules.json
+++ b/config/modules.json
@@ -80,7 +80,11 @@
"per": [],
"par": {
"URL0": {"des": "Department id", "typ": "id", "ren": "department" }
+ },
+ "output": {
+ "switched": { "des": "Whether the department has been switched", "typ": "bool" }
}
+
},
"errors":{
"GET": {
diff --git a/webpack/vue/header.vue b/webpack/vue/header.vue
index 5db7a07..5c37315 100644
--- a/webpack/vue/header.vue
+++ b/webpack/vue/header.vue
@@ -7,7 +7,7 @@
{{ get_dcurrent().label || 'département à jour' }}
- {{ d.label }}
+ {{ d.label }}
@@ -18,6 +18,7 @@
{{ get_vcurrent().date || 'version à jour' }}
{{ v.date || 'version à jour' }}
+ Nouvelle version
@@ -53,11 +54,14 @@ export default {
/* (1) Get current department data
---------------------------------------------------------*/
- get_dcurrent(){
+ get_dcurrent(id){
+
+ // use @dep_id, if invalid argument @id
+ ( isNaN(id) ) && ( id = this.dep_id );
// search in @dpts where id is @dep_id
for( var d in this.dpts )
- if( this.dpts[d].id == this.dep_id )
+ if( this.dpts[d].id == id )
return this.dpts[d];
return { id: null, label: null };
@@ -66,11 +70,14 @@ export default {
/* (2) Get current versoin data
---------------------------------------------------------*/
- get_vcurrent(){
+ get_vcurrent(id){
+
+ // use @dep_id, if invalid argument @id
+ ( isNaN(id) ) && ( id = this.ver_id );
// search in @vers where id is @ver_id
for( var v in this.vers )
- if( this.vers[v].id == this.ver_id )
+ if( this.vers[v].id == id )
return this.vers[v];
return { date: null };
@@ -85,17 +92,21 @@ export default {
this.d_dialog = false;
this.v_dialog = false;
- // 2. Ask for department change
+ // 2. Do nothing if no change
+ if( this.dep_id == id )
+ return;
+
+ // 3. Ask for department change
api.call(`PUT department/${id}`, {}, function(rs){
- // error -> do nothing
- if( rs.error !== 0 || rs.success !== true )
+ // 1. error -> do nothing
+ if( rs.error !== 0 || rs.switched !== true )
return;
- // 3. Update GUI
+ // 2. Update GUI
this.dep_id = id;
- // 4. Reload page if needed
+ // 3. Reload page if needed
setTimeout(() => { document.location = ''; }, 200);
}.bind(this));
@@ -110,16 +121,112 @@ export default {
this.d_dialog = false;
this.v_dialog = false;
- // 2. Ask for version change
- // TODO: api.call()
+ // 2. Do nothing if no change
+ if( this.ver_id == id )
+ return;
+
+ // 3. Get version date
+ var verdate = this.get_vcurrent(id).date;
+
+ // 4. If null date -> go to current version
+ ( verdate === null ) && ( verdate = '' );
+
+ // 5. Ask for department change
+ api.call(`PUT department/version/0/${verdate}`, {}, function(rs){
+
+ // 1. error -> do nothing
+ if( rs.error !== 0 || rs.updated !== true )
+ return;
+
+ // 2. Update GUI
+ this.ver_id = id;
+
+ // 3. Reload page if needed
+ setTimeout(() => { document.location = ''; }, 200);
+
+ }.bind(this));
+
+ },
+
+ /* (5) Create a new version from now
+ ---------------------------------------------------------*/
+ v_create(){
+
+ // 1. De-activate dialogs
+ this.d_dialog = false;
+ this.v_dialog = false;
+
+ // 2. Popup confirm
+ (new Promise( (resolve, reject) => {
+
+ popup.ask({
+ title: 'Confirmation de création de version',
+ content: `Une sauvegarde (ou version) va être crée à partir de l'état actuel des données de tout le département
Voulez-vous créer cette sauvegarde ?`,
+ action: 'Créer',
+ type: 'valid'
+ }, (popup_rs) => { popup_rs && resolve() });
+
+ // 3. On popup confirm
+ })).then( () => {
+
+ // Call API to create a new version
+ api.call(`POST department/version/`, {}, function(rs){
+
+ // 1. error -> popup
+ if( rs.error !== 0 || !rs.hasOwnProperty('created_id') ){
+
+ return popup.ask({
+ title: 'Error ('+err_code+')',
+ content: 'La création de sauvegarde à échoué.',
+ action: 'OK',
+ type: 'neutral'
+ }, () => {});
+
+ }
+
+ // 2. Get last version id
+ var last_id = this.vers[ this.vers.length-1 ].id;
+
+ // 3. Update GUI
+ this.vers.push( { id: last_id+1, date: rs.created_id } );
+
+ }.bind(this));
+
+ })
- // 3. Update GUI
- this.ver_id = id;
}
+
},
beforeMount(){
- // set onblur to hide department lists
+ /* (1) Try to fetch versions from API */
+ api.call('GET department/version', {}, function(rs){
+
+ // 1. Manage error
+ if( rs.error !== 0 || !rs.hasOwnProperty('versions') )
+ return;
+
+ // 2. Init version list
+ this.vers = [ { id: -1, date: null } ];
+ var idv = 0;
+
+ // 3. Store versions
+ for( var ver of rs.versions ){
+
+ // if current version -> set @ver_id
+ if( _SERVER.session.version === ver )
+ this.ver_id = idv
+
+ // add version to list
+ this.vers.push( { id: idv++, date: ver } );
+
+ }
+
+ }.bind(this) );
+
+
+
+ /* (2) Set onblur to hide department lists */
window.onblur.link('header.department', (e) => {
// only hide not [data-unblur-department] elements