From 35b0cb36facca5f88d614cebce337aa667b07a61 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 20 Mar 2018 10:49:12 +0100 Subject: [PATCH] [repo.professor] fix CREATE sync with meta database --- build/database/repo/professor.php | 85 +++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 22 deletions(-) diff --git a/build/database/repo/professor.php b/build/database/repo/professor.php index 7f2fa79..7bea981 100644 --- a/build/database/repo/professor.php +++ b/build/database/repo/professor.php @@ -29,12 +29,19 @@ class professor extends Repo_i { ---------------------------------------------------------*/ public function create(string $lastName, string $firstName, int $category, int $hoursToDo = 0, ?string $initials = null , bool $isAdmin = false , ?string $casLogin = null ) : ?int{ + + /* (1) Create professor into local database + ---------------------------------------------------------*/ /* (1) Prepare Statement */ $st = $this->pdo->prepare("INSERT INTO Professeur(`casLogin`, `lastName`, `firstName`, `abreviation`, `admin`, `hoursToDo`, `Categorie_idCategorie`) VALUE (:casLogin, :lastName, :firstName, :abrev, :is_admin, :hoursToDo, :cat);"); - /* (2) Bind params and execute */ + /* (2) Manage statement error */ + if( is_bool($st) ) + return NULL; + + /* (3) Bind params and execute */ $success = $st->execute([ ':casLogin' => $casLogin, ':lastName' => $lastName, @@ -45,30 +52,64 @@ class professor extends Repo_i { ':cat' => $category ]); - $profId = $this->pdo->lastInsertId(); - - /* (3) synchroize the meta database */ - if(!is_null($casLogin)){ - $st = $this->pdo->prepare("INSERT IGNORE INTO meta_vhost.casUser(casLogin, firstName, lastName) VALUE (:casLogin,:firstName,:lastName)"); - $st->execute([ - "casLogin" => $casLogin, - "firstName" => $firstName, - "lastName" => $lastName - ]); - - $st = $this->pdo->prepare("INSERT INTO meta_vhost.linkedDep(departement_iddepartement, casUser_casLogin) VALUE (:idDep,:casLogin)"); - $st->execute([ - "idDep" => $_SESSION['CurrentDepartementId'], - "casLogin" => $casLogin - ]); - } - - /* (3) Manage error */ + /* (4) If execution error -> dispatch */ if( !$success ) return NULL; - /* (4) Return inserted ID */ - return $profId; + /* (5) Store id */ + $id_prof = $this->pdo->lastInsertId(); + + /* (6) Exit now if no meta database to udpate */ + if( is_null($casLogin) ) + return $id_prof; + + + + + /* (2) Create user in meta database + ---------------------------------------------------------*/ + /* (1) Try to insert user */ + $st = $this->pdo->prepare("INSERT IGNORE INTO meta_vhost.casUser(casLogin, firstName, lastName) VALUE (:casLogin, :firstName, :lastName)"); + + /* (2) Manage statement error */ + if( !is_bool($st) ){ + + /* (3) Try to execute */ + $success = $st->execute([ + ':casLogin' => $casLogin, + ':firstName' => $firstName, + ':lastName' => $lastName + ]); + + /* (4) Manage execution error */ + // if( !$success ) + + } + + + /* (3) Add user to department in meta database + ---------------------------------------------------------*/ + /* (1) Try to insert user */ + $st = $this->pdo->prepare("INSERT INTO meta_vhost.linkedDep(departement_iddepartement, casUser_casLogin) VALUE (:idDep, :casLogin)"); + + /* (2) Manage statement error */ + if( !is_bool($st) ){ + + /* (3) Try to execute */ + $success = $st->execute([ + ':idDep' => $_SESSION['CurrentDepartementId'], + ':casLogin' => $casLogin + ]); + + /* (4) Manage error */ + // if( !$success ) + + } + + + + /* (5) Return inserted ID */ + return $id_prof; }