From 5a8fdf0dd140ea8e3b11645d0c8458f3f1f1357f Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 19 Feb 2017 15:46:52 +0100 Subject: [PATCH] [Fixed] machineDefault/init & machineDefault/sync ok --- build/api/module/machineDefault.php | 2 +- build/database/repo/machine.php | 14 ++++++++------ build/orm/core/Rows.php | 15 ++++++++++++--- build/orm/core/SQLBuilder.php | 15 +++++++++++++++ 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/build/api/module/machineDefault.php b/build/api/module/machineDefault.php index 33b0471..601b9b7 100755 --- a/build/api/module/machineDefault.php +++ b/build/api/module/machineDefault.php @@ -498,7 +498,7 @@ $checkToken = new Repo('machine/checkToken', [ $_SESSION['WAREHOUSE']['id'], $token, $renew ]); // Si token incorrect, on envoie une erreur - if( $checkToken->answer() !== true ) + if( !$checkToken->answer() ) return [ 'error' => new Error(Err::TokenError) ]; diff --git a/build/database/repo/machine.php b/build/database/repo/machine.php index 74765a1..c9f002c 100755 --- a/build/database/repo/machine.php +++ b/build/database/repo/machine.php @@ -27,8 +27,8 @@ 'id_machine' => Rows::INSERT_DEFAULT, 'id_warehouse' => $id_warehouse, 'name' => $name, - 'token' => Rows::INSERT_DEFAULT, - 'unlock_code' => Rows::INSERT_DEFAULT + 'token' => Rows::NULL, + 'unlock_code' => Rows::NULL ]); // Si erreur (car name doit être unique) @@ -296,13 +296,15 @@ =========================================================*/ /* (1) On effectue la requête */ $machine = Table::get('machine') + ->select('id_machine') + ->select('name') ->whereId($id_machine) ->whereIdWarehouse($id_warehouse) ->whereUnlockCode($unlock_code) ->fetch(); /* (2) On vérifie si on a bien le bon code */ - if( $machine === false ) + if( count($machine) < 1 ) return false; @@ -336,7 +338,7 @@ public static function checkToken($id_warehouse, $token, $newToken=null){ /* [1] On vérifie le token =========================================================*/ - $hash = secure_hash($token, ''); + $hash = hash('sha512', $token); $byToken = self::getByToken($id_warehouse, $hash); @@ -348,10 +350,10 @@ /* [2] On met à jour le token =========================================================*/ $updated = Table::get('machine') - ->whereId($id_machine) + ->whereId($byToken['id_machine']) ->edit([ 'token' => Checker::run('hash', $newToken) ? $newToken : $token, - 'id_machine' => $byToken[0]['id_machine'] + 'id_machine' => $byToken['id_machine'] ]); diff --git a/build/orm/core/Rows.php b/build/orm/core/Rows.php index f0de40a..75ec804 100755 --- a/build/orm/core/Rows.php +++ b/build/orm/core/Rows.php @@ -37,6 +37,7 @@ // {3} Constantes d'insertion // const INSERT_DEFAULT = '__DEFAULT__'; // Valeur DEFAULT (pour insertion) + const NULL = '__NULL__'; // Valeur DEFAULT (pour insertion) /* Attributs */ private $driver; // Database driver label @@ -266,9 +267,13 @@ /* (2) On vérifie le type de chaque valeur */ $type = $this->schema['columns'][$field]['type']; - if( $type == 'int' && !is_numeric($args[0][0]) ) return $this; - if( $type == 'float' && !is_numeric($args[0][0]) ) return $this; - if( in_array($type, ['text', 'varchar']) && !is_string($args[0][0]) ) return $this; + if( !is_null($args[0][0]) ){ + + if( $type == 'int' && !is_numeric($args[0][0]) ) return $this; + if( $type == 'float' && !is_numeric($args[0][0]) ) return $this; + if( in_array($type, ['text', 'varchar']) && !is_string($args[0][0]) ) return $this; + + } } @@ -511,6 +516,10 @@ /* (3) On vérifie les types des champs */ foreach($cleared as $field=>$value){ + // let null values + if( is_null($value) ) + continue; + $type = $this->schema['columns'][$field]['type']; // {1} Si de type INT/FLOAT et pas numérique, on retire le champ // diff --git a/build/orm/core/SQLBuilder.php b/build/orm/core/SQLBuilder.php index 99bc59f..24a907e 100755 --- a/build/orm/core/SQLBuilder.php +++ b/build/orm/core/SQLBuilder.php @@ -138,6 +138,13 @@ $sql .= substr($value[1], 2, -2).' '; /* (3) Variable */ + // {1} Si NULL // + if( is_null($value[0]) ){ + $sql .= 'NULL'; + return $sql; + } + + // {2} Si not NULL // $sql .= ':'.$field[0].'_x_'.$field[1].'_'.$offset; $bound[':'.$field[0].'_x_'.$field[1].'_'.$offset] = $value[0]; @@ -166,6 +173,14 @@ $sql[$c] = $field.' = '; /* (2) Variable */ + // {1} Si NULL // + if( is_null($value) ){ + $sql[$c] .= 'NULL'; + $c++; + continue; + } + + // {2} Si not NULL // $sql[$c] .= ':update_'.$field; $bound[':update_'.$field] = $value;