[api.cas] on login select version with 'default=1' first then if nothing, take the first element. Same for [api.version.DELETE] to update the list

This commit is contained in:
xdrm-brackets 2018-05-09 17:43:20 +02:00
parent 058eb5c275
commit 1225f69d9d
3 changed files with 42 additions and 8 deletions

View File

@ -146,22 +146,28 @@ class casController{
$_SESSION['AvailableDepartments'] = $departments;
/* (4) Choose first department by default */
$_SESSION['CurrentDatabase'] = $departments[0]['versions'][0]['dbName'];
$_SESSION['CurrentDepartmentId'] = $departments[0]['idDep'];
$_SESSION['VERSION'] = [
'list' => $departments[0]['versions'],
'current' => null
];
/* (5) Select actual version */
/* (5) select version with default = 1 */
foreach($_SESSION['VERSION']['list'] as $v){
if( $v['dbName'] == $_SESSION['CurrentDatabase'] ){
if( $v['default'] == 1 ){
$_SESSION['VERSION']['current'] = intval($v['iddatabase']);
$_SESSION['CurrentDatabase'] = $v['dbName'];
break;
}
}
/* (6) Use this department's database */
/* (6) if no default -> select first */
if( !is_int($_SESSION['VERSION']) ){
$_SESSION['VERSION']['current'] = intval($_SESSION['VERSION']['list'][0]['iddatabase']);
$_SESSION['CurrentDatabase'] = $_SESSION['VERSION']['list'][0]['dbName'];
}
/* (7) Use this department's database */
Repo::switchDatabase($_SESSION['CurrentDatabase']);

View File

@ -48,8 +48,35 @@ class versionController{
$version = null;
extract($args);
/* (1) Dispatch 'deleteVersion' result */
return [ 'deleted' => Repo::getRepo("meta")->deleteVersion($version) ];
/* (1) Try to delete */
$deleted = Repo::getRepo("meta")->deleteVersion($version);
if( !$deleted )
return ['error' => new Error(Err::ModuleError)];
/* (2) Update version list */
$_SESSION['VERSION']['list'] = Repo::getRepo("meta")->getAllVersions($_SESSION['CurrentDepartmentId']);
/* (3) Update current */
$_SESSION['VERSION']['current'] = null;
// select version with default = 1
foreach($_SESSION['VERSION']['list'] as $v){
if( $v['default'] == 1 ){
$_SESSION['VERSION']['current'] = intval($v['iddatabase']);
$_SESSION['CurrentDatabase'] = $v['dbName'];
break;
}
}
// if no default -> select first
if( !is_int($_SESSION['VERSION']) ){
$_SESSION['VERSION']['current'] = intval($_SESSION['VERSION']['list'][0]['iddatabase']);
$_SESSION['CurrentDatabase'] = $_SESSION['VERSION']['list'][0]['dbName'];
}
return ['deleted' => true];
}

View File

@ -262,12 +262,13 @@ class meta extends Repo_i {
$versionData = $st->fetch();
if($versionData == false){
if( !is_array($versionData) ){
return false;
}
//delete database
$this->pdo->exec("DROP DATABASE {$versionData["dbName"]}");
$versionDatabase = $versionData['dbName'];
$this->pdo->exec("DROP DATABASE $versionDatabase;");
//remove from meta
$st = $this->pdo->prepare("DELETE FROM meta_vhost.`databases` WHERE iddatabase = :id");