Repository 'machine' fonctionne avec ORM

This commit is contained in:
xdrm-brackets 2016-07-23 16:36:58 +02:00
parent 55535a2299
commit 0dc67f1fc7
11 changed files with 111 additions and 167 deletions

View File

@ -691,7 +691,7 @@ article.check-table{
justify-content: center;
/* (1) Gestion du checkbox HACK */
/* (1) Gestion du checkbox hack */
input[type='checkbox']{ display: none; }
input[type='checkbox'] + label[for]{

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,3 +1,4 @@
// TODO: Attendre que API soit chargée, sinon erreur, le CONTAINER prend comme contenu la dépendance JS
DOM = {
WRAPPER: $('WRAPPER'),
HEADER: $('HEADER'),

View File

@ -5,10 +5,6 @@
use \manager\Database;
// TODO: Suppression (avec prise en compte des jointures (sur le modèle de 'edit')
// TODO: Insertion
class Rows{

View File

@ -3,6 +3,8 @@
namespace manager\repo;
use \manager\Database;
use \manager\sessionManager;
use \manager\ORM\Table;
use \manager\ORM\Rows;
use \manager\Checker;
use \manager\repo\cluster as clusterRepo;
@ -20,27 +22,20 @@
*
*/
public static function create($id_warehouse, $name){
/* [1] Verification de l'unicite de name
/* [1] Creation de la machine
=========================================================*/
$check_unique = self::getByName($id_warehouse, $name) === false;
// Si une machine a deja ce nom, on renvoie une erreur
if( !$check_unique ){
return false;
}
/* [2] Creation de la machine
=========================================================*/
$insert_machine = Database::getPDO()->prepare("INSERT INTO machine(id_machine, id_warehouse, name, token)
VALUES(DEFAULT, :id_warehouse, :name, :token)");
$insert_machine->execute([
':id_warehouse' => $id_warehouse,
':name' => $name,
':token' => sessionManager::secure_hash( uniqid() )
$inserted = Table::get('machine')->insert([
'id_machine' => Rows::DEFAULT,
'id_warehouse' => $id_warehouse,
'name' => $name,
'token' => sessionManager::secure_hash( uniqid() )
]);
/* [3] On retourne l'id_machine ou FALSE si erreur
// Si erreur (car name doit être unique)
if( !$inserted )
return false;
/* [2] On retourne l'id_machine ou FALSE si erreur
=========================================================*/
$check_machine = self::getByName($id_warehouse, $name);
@ -67,15 +62,13 @@
*/
public static function search($id_warehouse, $keyword){
// On recupere les donnees
$searchmachines = Database::getPDO()->prepare("SELECT id_machine, name FROM machine
WHERE id_warehouse = :id_warehouse
AND name LIKE '%".$keyword."%'");
$search = Table::get('machine')
->whereIdWarehouse($id_warehouse)
->whereName(["%$keyword%", Rows::COND_LIKE])
->select('id_machine')
->select('name');
$searchmachines->execute([
':id_warehouse' => $id_warehouse
]);
return Database::delNumeric( $searchmachines->fetchAll() );
return $search->fetch();
}
@ -94,22 +87,16 @@
public static function getClusters($id_warehouse, $id_machine){
/* [1] On redige/execute la requete
=========================================================*/
$get_clusters = Database::getPDO()->prepare("SELECT c.*
FROM machine_cluster as c, machine_cluster_merge as cm, machine as m
WHERE cm.id_machine_cluster = c.id_machine_cluster
AND cm.id_machine = m.id_machine
AND cm.id_machine = :id_machine
AND c.id_warehouse = m.id_warehouse
AND m.id_warehouse = :id_warehouse
ORDER BY c.name");
$get_clusters->execute([
':id_warehouse' => $id_warehouse,
':id_machine' => $id_machine
]);
$cluster = Table::get('machine_cluster')
->whereIdWarehouse($id_warehouse)
->select('*');
$cluster_merge = Table::get('machine_cluster_merge')
->whereIdMachine($id_machine)
->join('id_machine_cluster', $cluster);
/* [2] On retourne la liste des groupes
=========================================================*/
return Database::delNumeric( $get_clusters->fetchAll() );
return $cluster_merge->fetch();
}
@ -129,40 +116,16 @@
*
*/
public static function edit($id_warehouse, $id_machine=null, $name=null){
/* [1] Verification de l'unicite de name (si differents)
/* [1] Modification de la machine
=========================================================*/
// On recupere les machine ayant le meme name (si existent)
$getbyname = self::getByName($id_warehouse, $name);
$check_unique = is_array($getbyname) && $getbyname['id_machine'] == $id_machine || !is_array($getbyname);
// Si un utilisateur a deja ce name (sauf lui), on renvoie une erreur
if( !$check_unique )
return false;
/* [2] Modification de la machine
=========================================================*/
$edit_machine = Database::getPDO()->prepare("UPDATE machine
SET name = :name
WHERE id_machine = :id_machine
AND id_warehouse = :id_warehouse");
$edit_machine->execute([
':name' => $name,
':id_machine' => $id_machine,
':id_warehouse' => $id_warehouse
]);
/* [3] On retourne si tout a ete modifie, si erreur
=========================================================*/
$check_machine = self::getById($id_warehouse, $id_machine);
$checker = $check_machine['id_machine'] == $id_machine;
$checker = $checker && $check_machine['name'] == $name;
$edited = Table::get('machine')
->whereId($id_machine)
->whereIdWarehouse($id_warehouse)
->edit([ 'name' => $name ]);
// On retourne l'etat de la modification
return $checker;
return $edited;
}
@ -183,18 +146,14 @@
public static function delete($id_warehouse, $id_machine){
/* [1] On redige/execute la requete
=========================================================*/
$delete_machine = Database::getPDO()->prepare("DELETE FROM machine
WHERE id_machine = :id_machine
AND id_warehouse = :id_warehouse");
$delete_machine->execute([
':id_machine' => $id_machine,
':id_warehouse' => $id_warehouse
]);
$deleted = Table::get('machine')
->whereId($id_machine)
->whereIdWarehouse($id_warehouse);
/* [2] On verifie que la machine n'existe plus
/* [2] On verifie que la machine est bien supprimée
=========================================================*/
return ( self::getById($id_warehouse, $id_machine) === false );
return $delete->delete();
}
@ -217,24 +176,13 @@
public static function getById($id_warehouse, $id_machine){
/* [1] On rédige/execute la requête
=========================================================*/
$get = Database::getPDO()->prepare("SELECT m.id_machine, m.name
FROM machine as m
WHERE m.id_warehouse = :id_warehouse
AND m.id_machine = :id_machine");
$get->execute([
':id_warehouse' => $id_warehouse,
':id_machine' => $id_machine
]);
$machine = Table::get('machine')
->whereId($id_machine)
->whereIdWarehouse($id_warehouse)
->select(['id_machine', 'name'])
->unique();
/* [2] Gestion des données
=========================================================*/
$found = $get->fetch();
// Si aucun résultat
if( $found === false )
return false;
return Database::delNumeric( $found, true );
return $machine->fetch();
}
@ -257,24 +205,13 @@
public static function getByName($id_warehouse, $name){
/* [1] On rédige/execute la requête
=========================================================*/
$get = Database::getPDO()->prepare("SELECT m.id_machine, m.name
FROM machine as m
WHERE m.id_warehouse = :id_warehouse
AND m.name = :name");
$get->execute([
':id_warehouse' => $id_warehouse,
':name' => $name
]);
$machine = Table::get('machine')
->whereName($name)
->whereIdWarehouse($id_warehouse)
->select(['id_machine', 'name'])
->unique();
/* [2] Gestion des données
=========================================================*/
$found = $get->fetch();
// Si aucun résultat
if( $found === false )
return false;
return Database::delNumeric( $found, true );
return $machine->fetch();
}
@ -297,24 +234,13 @@
public static function getByToken($id_warehouse, $token){
/* [1] On rédige/execute la requête
=========================================================*/
$get = Database::getPDO()->prepare("SELECT m.id_machine, m.name
FROM machine as m
WHERE m.id_warehouse = :id_warehouse
AND m.token = :token");
$get->execute([
':id_warehouse' => $id_warehouse,
':token' => $token
]);
$machine = Table::get('machine')
->whereToken($token)
->whereIdWarehouse($id_warehouse)
->select(['id_machine', 'name'])
->unique();
/* [2] Gestion des données
=========================================================*/
$found = $get->fetch();
// Si aucun résultat
if( $found === false )
return false;
return Database::delNumeric( $found, true );
return $machine->fetch();
}
@ -335,14 +261,12 @@
public static function getAll($id_warehouse){
/* [1] On rédige/execute la requête
=========================================================*/
$get = Database::getPDO()->prepare("SELECT m.id_machine, m.name
FROM machine as m
WHERE m.id_warehouse = :id_warehouse");
$get->execute([
':id_warehouse' => $id_warehouse
]);
$machine = Table::get('machine')
->whereIdWarehouse($id_warehouse)
->select(['id_machine', 'name'])
->unique();
return Database::delNumeric( $get->fetchAll() );
return $machine->fetch();
}
@ -373,18 +297,17 @@
/* [2] On met à jour le token
=========================================================*/
$updateToken = Database::getPDO()->prepare("UPDATE machine
SET token = :token
WHERE id_machine = :id_machine");
$updateToken->execute([
':token' => Checker::run('hash', $newToken) ? $newToken : $token,
':id_machine' => $byToken[0]['id_machine']
$updated = Table::get('machine')
->whereId($id_machine)
->edit([
'token' => Checker::run('hash', $newToken) ? $newToken : $token,
'id_machine' => $byToken[0]['id_machine']
]);
/* [3] On retourne que tout s'est bien déroulé
=========================================================*/
return true;
return $updated;
}

View File

@ -410,22 +410,40 @@
/* [4] Suppression
=========================================================*/
$warehouse =
Table::get('warehouse') // Access to table 'warehouse'
->whereName('stef-montauban'); // condition : name = 'my-warehouse'
// $warehouse =
// Table::get('warehouse') // Access to table 'warehouse'
// ->whereName('stef-montauban'); // condition : name = 'my-warehouse'
//
//
// $myUser =
// Table::get('user') // Access to table 'user'
// ->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field)
// ->whereUsername(['no%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column'
// ->select(['mail', 'username', 'firstname']) // Select clause
// ->select('id_user') // Select clause (added)
// ->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse'
// // ->unique() // unique result
//
// ->delete();
$myUser =
Table::get('user') // Access to table 'user'
->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field)
->whereUsername(['no%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column'
->select(['mail', 'username', 'firstname']) // Select clause
->select('id_user') // Select clause (added)
->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse'
// ->unique() // unique result
$keyword = 'h%s';
->fetch();
$searchmachines = Database::getPDO()->prepare("SELECT id_machine, name FROM machine
WHERE id_warehouse = :id_warehouse
AND name LIKE '%".$keyword."%'");
var_dump($myUser);
$searchmachines->execute([
':id_warehouse' => 7
]);
var_dump($searchmachines->fetchAll());
var_dump( Table::get('machine')
->whereIdWarehouse(7)
->whereName(["%$keyword%", Rows::COND_LIKE])
->select('id_machine')
->select('name')->fetch() );
?>

View File

@ -1,3 +1,5 @@
// TODO: Supprimer les retours vers la cible lors de 'création'/'modification'
// On referencie toutes les sections
var section = {
view: {

View File

@ -1,7 +1,7 @@
var section={view:{text:'#CONTAINER > section[data-sublink="view"] ',element:document.querySelector('#CONTAINER > section[data-sublink="view"]'),searchbar:document.querySelector('#CONTAINER > section[data-sublink="view"] > .searchbar')},create:{text:'#CONTAINER > section[data-sublink="create"] ',element:document.querySelector('#CONTAINER > section[data-sublink="create"]')},remove:{text:'#CONTAINER > section[data-sublink="remove"] ',element:document.querySelector('#CONTAINER > section[data-sublink="remove"]')},
edit:{text:'#CONTAINER > section[data-sublink="edit"] ',element:document.querySelector('#CONTAINER > section[data-sublink="edit"]')},groups:{text:'#CONTAINER > section[data-sublink="groups"] ',element:document.querySelector('#CONTAINER > section[data-sublink="groups"]'),searchbar:document.querySelector('#CONTAINER > section[data-sublink="groups"] > .searchbar')}};
if(null!=section.view.element){1<pageManager.vars.length&&!isNaN(pageManager.vars[1])&&(document.location="#"+pageManager.vars[1],document.getElementById(pageManager.vars[1]).addClass("selected"));section.view.link={edit:document.querySelectorAll(section.view.text+".link_edit[data-machine]"),remove:document.querySelectorAll(section.view.text+".link_remove[data-machine]")};section.view.group={rem:document.querySelectorAll(section.view.text+".rem-group[data-machine][data-group]"),add:document.querySelectorAll(section.view.text+
".add-group[data-machine]")};section.view.searchbar.addEventListener("keyup",function(a){api.send({path:"machineDefault/search",keywords:section.view.searchbar.value},function(a){if(0==a.ModuleError){for(var d=[],c=0;c<a.machines.length;c++)d.push(a.machines[c].id_machine);a=document.querySelectorAll(section.view.text+"> article.inline-box[id]");for(c=0;c<a.length;c++)-1<d.indexOf(a[c].id)?a[c].remClass("hidden"):a[c].addClass("hidden")}})},!1);for(var i=0;i<section.view.link.edit.length;i++)section.view.link.edit[i].addEventListener("click",
".add-group[data-machine]")};section.view.searchbar.addEventListener("keyup",function(a){api.send({path:"machineDefault/search",keywords:section.view.searchbar.value},function(a){if(0==a.ModuleError){for(var d=[],c=0;c<a.machines.length;c++)d.push(a.machines[c].id_machine);a=document.querySelectorAll(section.view.text+"> article.inline-box[id]");for(c=0;c<a.length;c++)-1<d.indexOf(parseInt(a[c].id))?a[c].remClass("hidden"):a[c].addClass("hidden")}})},!1);for(var i=0;i<section.view.link.edit.length;i++)section.view.link.edit[i].addEventListener("click",
function(a){pageManager.vars[1]=a.target.getData("machine");navSubMenu("edit");document.location=""},!1);for(i=0;i<section.view.link.remove.length;i++)section.view.link.remove[i].addEventListener("click",function(a){pageManager.vars[1]=a.target.getData("machine");navSubMenu("remove");document.location=""},!1);for(i=0;i<section.view.group.rem.length;i++)section.view.group.rem[i].addEventListener("click",function(a){var b={path:"machineDefault/unlink",id_machine:a.target.getData("machine"),id_cluster:a.target.getData("group")};
api.send(b,function(b){if(0!=b.ModuleError)return!1;a.target.parentNode.parentNode.removeChild(a.target.parentNode)})},!1)}
null!=section.create.element&&(section.create.input={name:document.querySelector(section.create.text+"#create_name"),submit:document.querySelector(section.create.text+"#create_submit")},section.create.input.submit.addEventListener("click",function(a){a.preventDefault();api.send({path:"machineDefault/create",name:section.create.input.name.value},function(a){0==a.ModuleError&&(console.log("Machine cree!"),pageManager.vars=["view",a.id_machine],pageManager.refresh(),document.location="",section.create.input.submit.anim("active",

View File

@ -1,3 +1,5 @@
// TODO: Supprimer les retours vers la cible lors de 'création'/'modification'
// On referencie toutes les sections
var section = {
view: {
@ -77,7 +79,7 @@ if( section.view.element != null ){
// Pour chaque machine
for( var i = 0 ; i < machine_list.length ; i++ ){
// Si doit etre visible
if( uid_list.indexOf(machine_list[i].id) > -1 )
if( uid_list.indexOf(parseInt(machine_list[i].id)) > -1 )
machine_list[i].remClass('hidden');
// Si ne doit pas etre visible
else

View File

@ -1,3 +1,5 @@
// TODO: Supprimer les retours vers la cible lors de 'création'/'modification'
// On referencie toutes les sections
var section = {
view: {