ptut-vhost/build/api/module/department/versionController.php

158 lines
3.8 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Created by PhpStorm.
* User: lucas
* Date: 15/03/18
* Time: 15:50
*/
namespace api\module\department;
use database\core\Repo;
2018-05-07 16:14:12 +00:00
use database\repo\meta;
use error\core\Error;
use error\core\Err;
use database\repo\department;
use Ifsnop\Mysqldump\Mysqldump;
class versionController{
/* (4) List available versions for this department
*
* @return versions<array> Version list
*
---------------------------------------------------------*/
public function get($args){
2018-05-07 16:14:12 +00:00
//search for the current department in the session and return its versions
/** @var meta $depRepo */
$depRepo = Repo::getRepo("meta");
2018-05-07 16:14:12 +00:00
//if no department found, return an empty array
return ['versions' => $depRepo->getAllVersions($_SESSION['CurrentDepartmentId']) ];
}
/* (5) Remove an existing version for this department
*
2018-05-07 16:14:12 +00:00
* @version<int> Version name (typically snapshot date)
*
* @return deleted<bool> Whether the version has been deleted
*
---------------------------------------------------------*/
public function delete($args){
$version = null;
extract($args);
/* (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];
}
/* (6) Creates a new version (snapshot of database) from now
*
* @return created_id<String> The created version id (date)
*
---------------------------------------------------------*/
public function post($args){
2018-05-07 16:14:12 +00:00
$label = null;
extract($args);
/* (2) Try to create the snapshot */
try{
/* (2.1) Get database configuration */
$conf = Repo::getDBConfig();
/* (2.2) Try to dump the database */
2018-05-07 16:14:12 +00:00
/** @var Mysqldump*/
$dump = new Mysqldump(
2018-05-07 16:14:12 +00:00
'mysql:host='.$conf['host'].';dbname='.$_SESSION['CurrentDatabase'],
$conf['username'],
2018-05-07 16:14:12 +00:00
$conf['password']
);
2018-05-07 16:14:12 +00:00
//create temporary file;
$file = tmpfile();
//get URI
$metaDatas = stream_get_meta_data($file);
$tmpFilename = $metaDatas['uri'];
//close file pointer
fclose($file);
//fill the file with sql dump
$dump->start($tmpFilename);
/** @var department $depRepo */
$depRepo = Repo::getRepo("department");
$dbName = $depRepo->createVersionDatabase($_SESSION['CurrentDepartmentId'],file_get_contents($tmpFilename));
2018-05-07 16:14:12 +00:00
/** @var meta $metaRep */
$metaRep = Repo::getRepo("meta");
$versionId = $metaRep->createVersion($label, $dbName,$_SESSION['CurrentDepartmentId'] ,false);
/* (2.5) Return status */
2018-05-07 16:14:12 +00:00
return ['created_id' => $versionId ];
/* (3) On error -> dispatch error */
}catch(\Exception $e){
return ['error' => new Error(Err::RepoError)];
}
}
2018-05-07 16:14:12 +00:00
/* (6) Modify a version and use it
*
*
2018-05-07 16:14:12 +00:00
* @return bool success
*
---------------------------------------------------------*/
public function put($args){
2018-05-07 16:14:12 +00:00
$label = null;
$version = null;
2018-05-07 16:14:12 +00:00
$default = null;
extract($args);
2018-05-07 16:14:12 +00:00
/** @var meta $metaRepo */
$metaRepo = Repo::getRepo("meta");
/* (4) Return status */
2018-05-07 16:14:12 +00:00
return [ 'updated' => $metaRepo->updateVersion($version,$label,$default) ];
}
}