2018-03-17 13:34:16 +00:00
|
|
|
<?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;
|
2018-03-17 13:34:16 +00:00
|
|
|
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-03-17 13:34:16 +00:00
|
|
|
|
2018-05-07 16:14:12 +00:00
|
|
|
//if no department found, return an empty array
|
|
|
|
return ['versions' => $depRepo->getAllVersions($_SESSION['CurrentDepartmentId']) ];
|
2018-03-17 13:34:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (5) Remove an existing version for this department
|
|
|
|
*
|
2018-05-07 16:14:12 +00:00
|
|
|
* @version<int> Version name (typically snapshot date)
|
2018-03-17 13:34:16 +00:00
|
|
|
*
|
|
|
|
* @return deleted<bool> Whether the version has been deleted
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
public function delete($args){
|
|
|
|
$version = null;
|
|
|
|
extract($args);
|
|
|
|
|
2018-05-07 16:14:12 +00:00
|
|
|
/* (1) Dispatch 'deleteVersion' result */
|
|
|
|
return [ 'deleted' => Repo::getRepo("meta")->deleteVersion($version) ];
|
2018-03-17 13:34:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (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);
|
2018-03-17 13:34:16 +00:00
|
|
|
|
|
|
|
/* (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*/
|
2018-03-17 13:34:16 +00:00
|
|
|
$dump = new Mysqldump(
|
2018-05-07 16:14:12 +00:00
|
|
|
'mysql:host='.$conf['host'].';dbname='.$_SESSION['CurrentDatabase'],
|
2018-03-17 13:34:16 +00:00
|
|
|
$conf['username'],
|
2018-05-07 16:14:12 +00:00
|
|
|
$conf['password']
|
2018-03-17 13:34:16 +00:00
|
|
|
);
|
|
|
|
|
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-03-17 13:34:16 +00:00
|
|
|
|
2018-05-07 16:14:12 +00:00
|
|
|
/** @var meta $metaRep */
|
|
|
|
$metaRep = Repo::getRepo("meta");
|
|
|
|
$versionId = $metaRep->createVersion($label, $dbName,$_SESSION['CurrentDepartmentId'] ,false);
|
2018-03-17 13:34:16 +00:00
|
|
|
|
|
|
|
/* (2.5) Return status */
|
2018-05-07 16:14:12 +00:00
|
|
|
return ['created_id' => $versionId ];
|
2018-03-17 13:34:16 +00:00
|
|
|
|
|
|
|
/* (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-03-17 13:34:16 +00:00
|
|
|
*
|
|
|
|
*
|
2018-05-07 16:14:12 +00:00
|
|
|
* @return bool success
|
2018-03-17 13:34:16 +00:00
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
public function put($args){
|
2018-05-07 16:14:12 +00:00
|
|
|
$label = null;
|
2018-03-17 13:34:16 +00:00
|
|
|
$version = null;
|
2018-05-07 16:14:12 +00:00
|
|
|
$default = null;
|
2018-03-17 13:34:16 +00:00
|
|
|
extract($args);
|
|
|
|
|
2018-05-07 16:14:12 +00:00
|
|
|
/** @var meta $metaRepo */
|
|
|
|
$metaRepo = Repo::getRepo("meta");
|
2018-03-17 13:34:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (4) Return status */
|
2018-05-07 16:14:12 +00:00
|
|
|
return [ 'updated' => $metaRepo->updateVersion($version,$label,$default) ];
|
2018-03-17 13:34:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|