From 52740e1ae49c0a648ae0554d3b0b9a47edcb4c83 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 22 Nov 2016 14:30:34 +0100 Subject: [PATCH] =?UTF-8?q?input/phone=20en=20refactor=20avec=20nouveau=20?= =?UTF-8?q?syst=C3=A8me=20de=20storage=20#lightdb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/api/module/input.php | 227 ++++++++++------------ build/lightdb/core/lightdb.php | 7 +- build/lightdb/storage/subject/description | 7 + doc/storage.md | 26 --- public_html/test.php | 3 +- 5 files changed, 113 insertions(+), 157 deletions(-) create mode 100644 build/lightdb/storage/subject/description delete mode 100644 doc/storage.md diff --git a/build/api/module/input.php b/build/api/module/input.php index 5114304..b8fe42a 100644 --- a/build/api/module/input.php +++ b/build/api/module/input.php @@ -21,10 +21,10 @@ * @return subject_id Retourne l'id sujet de l'enquête * */ - public static function xphone($params){ + public static function phone($params){ extract($params); - /* [0] On récupère l'id unique actuel + /* [1] On récupère l'id unique actuel =========================================================*/ $funiq = fopen( __BUILD__.'/lightdb/storage/uniqid', 'r+' ); flock($funiq, LOCK_EX); // On verrouille le fichier @@ -37,141 +37,90 @@ $offset = intval($uniqid) + 1; // on enregistre l'id du sujet - $subject_id = $subject['subject_id']; + $subject_id = intval($subject['subject_id']); // Contiendra la valeur de l'id maximum $maxId = $offset; - - - /* [1] On initialise nos storages + /* [2] On initialise nos variables (lightdb + autres) =========================================================*/ + /* (1) Fichiers de sortie */ $file = [ "subject" => "", - "contacts" => [], "relations" => [] ]; + /* (2) Bases de données */ + $subjectdb = new lightdb('subject'); + $contactdb = new lightdb('contact'); - - - - /* [2] On enregistre les données du sujet + /* [3] Données du sujet =========================================================*/ - /* (1) On crée le sujet */ - $file['subject'] = [ 'id' => $subject_id ]; + $subject_set = $subjectdb->fetch($subject_id); - /* (2) On récupère ses contacts facebook */ - // {2.1} On récupère les données du sujet // - $lfacebook = new lightdb('facebook_db'); - $lfacebook = $lfacebook->fetch($subject_id); + /* (1) Si le sujet n'existe pas -> ERROR */ + if( $subject_set === false ) + return ['ModuleError'=>ManagerError::UnreachableResource]; - // {2.2} si n'existe pas, on a un tableau vide // - if( $lfacebook === false ) - $lfacebook = [ 'contacts' => [] ]; + /* (2) Initialisation des contacts si vide */ + if( !isset($subject_set['contacts']) ) + $subject_set['contacts'] = []; - if( !isset($lfacebook['contacts']) ) - $lfacebook['contacts'] = []; + /* (3) Initialisation des relations si vide */ + if( !isset($subject_set['relations']) ) + $subject_set['relations'] = []; + + /* (4) On récupère les noms des contacts */ + $contactsById = []; // idContact -> nomContact + + foreach($subject_set['contacts'] as $contactId){ + $contactId = intval($contactId); + $contactData = $contactdb->fetch( $contactId ); + + $contactsById[$contactId] = $contactData['name']; + } - /* (3) On récupère ses contacts lab-surveys */ - // {3.1} On récupère les données du sujet // - $lsurvey = new lightdb('survey_db'); - $lsurvey = $lsurvey->fetch($subject_id); - - // {3.2} si n'existe pas, on a un tableau vide // - if( $lsurvey === false ) - $lsurvey = [ 'contacts' => [] ]; - - if( !isset($lsurvey['contacts']) ) - $lsurvey['contacts'] = []; - - - /* (4) On récupère uniquement la liste des contacts avec le type de fiche qu'ils ont */ - // {4.1} Contiendra les contacts exportés // - $exported = [ - 'fiche' => [], - 'mini' => [] - ]; - - // {4.2} Contiendra les username des contacts exportés // - $exportedU = []; - - // {4.3} On récupère les contacts facebook // - foreach($lfacebook['contacts'] as $c=>$data){ - - if( isset($data['studies2']) ){ - - $exported['fiche'][] = $data['id']; - $exportedU[$data['id']] = $data['name']; - - }else - - // S'il n'est pas déja dans les fiches - if( !in_array($data['id'], $exported['fiche']) ){ - - $exported['mini'][] = $data['id']; - $exportedU[$data['id']] = $data['name']; - } - - } - - // {4.4} On récupère les contacts lab-surveys // - foreach($lsurvey['contacts'] as $c=>$data){ - - if( isset($data['studies2']) ) - - // On ajoute le contact s'il n'y est pas déja - if( !in_array($data['id'], $exported['fiche']) ){ - - $exported['fiche'][] = $data['id']; - $exportedU['fiche'][] = $data['username']; - - } - - else - - // On ajoute le contact s'il n'y est pas déja (ni dans mini ni dans fiche) - if( !in_array($data['id'], $exported['mini']) && !in_array($data['id'], $exported['fiche']) ){ - $exported['mini'][] = $data['id']; - $exportedU['mini'][] = $data['username']; - } - } - - - - - - - /* [3] On enregistre les contacts des FICHES + /* [4] On saisit les fiches + les contacts des fiches =========================================================*/ $contactsDone = [ "existing" => [], // Contacts exportés déja enregistrés "username" => [] // Nouveaux contacts déja enregistrés ]; + $finalid = []; // id_contact -> id_final_contact (existant ou nouveau) foreach($fiches as $f=>$ficheData){ /* (1) On récupère les données du contact associé */ - if( !isset($contacts[$ficheData['uid']]) ) + if( !isset($contacts[$ficheData['contact']]) ) break; - $contact = $contacts[ $ficheData['uid'] ]; + $contact = $contacts[ $ficheData['contact'] ]; /* (2) Si le contact a été importé d'une autre enquête */ if( isset($contact['existing']) && is_numeric($contact['existing']) ){ + $newId = (int) $contact['existing']; - $uname = $exportedU[intval($contact['existing'])]; - // {2.1} S'il a déja une fiche ou qu'on a déja fait une fiche, on quitte // - if( in_array(intval($contact['existing']), $exported['fiche']) || in_array($contact['existing'], $contactsDone['existing']) ) + $uname = $contactsById[ intval($contact['existing']) ]; + + $existingData = $dbcontact->fetch( intval($contact['existing']) ); + + $finalid[ intval($contact['uid']) ] = $newId; + + // S'il a déja une fiche ou qu'on a déja fait une fiche, on quitte + if( is_array($existingData) && isset($existingData['studies2']) || in_array($contact['existing'], $contactsDone['existing']) ) break; /* (3) Si nouveau contact */ }else{ - $newId = (int) ($offset+$ficheData['uid']); + + $newId = (int) ($offset+$contact['uid']); $uname = $contact['username']; - // {3.1} S'il a déja été saisi, on quitte // + + $finalid[ intval($contact['uid']) ] = $newId; + + // S'il a déja été saisi, on quitte if( in_array($contact['username'], $contactsDone['username']) ) break; } @@ -180,8 +129,8 @@ if( $newId > $maxId ) $maxId = (int) $newId; - // On remplit les données qui iront dans le fichier pour ce contact - $file['contacts'][] = [ + /* (5) On enregistre les données du contact */ + $contactdb->insert($newId, [ 'id' => $newId, 'name' => $uname, 'sexe' => $ficheData['sexe'], @@ -200,10 +149,14 @@ 'freq' => $ficheData['freq'], 'connect' => $ficheData['connect'], 'connectExtra' => $ficheData['connectSpecial'] - ]; + ] ); + + // On ajoute le contact dans la liste des contacts du sujet (si pas déja) + if( !in_array($newId, $subject_set['contacts']) ) + $subject_set['contacts'][] = $newId; // On enregistre la relation avec EGO - $file['relations'][] = [ + $subject_set['relations'][] = [ 'idA' => $subject_id, 'idB' => $newId, 'type' => ($f<20) ? 4 : 5 // 4->appels 5->sms @@ -219,29 +172,38 @@ } - - /* [4] On enregistre les contacts des MINI + /* [5] On enregistre les mini + les contacts des mini =========================================================*/ foreach($mini as $miniData){ /* (1) On récupère les données du contact associé */ - if( !isset($contacts[$miniData['uid']]) ) + if( !isset($contacts[$miniData['contact']]) ) break; - $contact = $contacts[ $miniData['uid'] ]; + $contact = $contacts[ $miniData['contact'] ]; /* (2) Si le contact a été importé d'une autre enquête */ if( isset($contact['existing']) && is_numeric($contact['existing']) ){ + $newId = (int) $contact['existing']; - $uname = $exportedU[intval($contact['existing'])]; - // {2.1} S'il a déja une fiche ou qu'on a déja fait une fiche (ou mini), on quitte // - if( in_array(intval($contact['existing']), $exported['fiche']) || in_array($contact['existing'], $contactsDone['existing']) ) + $uname = $contactsById[ intval($contact['existing']) ]; + + $existingData = $dbcontact->fetch( intval($contact['existing']) ); + + $finalid[ intval($contact['uid']) ] = $newId; + + // S'il a déja une fiche ou qu'on a déja fait une fiche (ou mini), on quitte + if( is_array($existingData) && isset($existingData['studies2']) || in_array($contact['existing'], $contactsDone['existing']) ) break; /* (3) Si nouveau contact */ }else{ + $newId = (int) ($offset+$miniData['uid']); $uname = $contact['username']; - // {3.1} S'il a déja été saisi, on quitte // + + $finalid[ intval($contact['uid']) ] = $newId; + + // S'il a déja été saisi, on quitte if( in_array($contact['username'], $contactsDone['username']) ) break; } @@ -252,7 +214,7 @@ // On remplit les données qui iront dans le fichier pour ce contact - $file['contacts'][] = [ + $contactdb->insert($newId, [ 'id' => $newId, 'name' => $uname, 'sexe' => $miniData['sexe'], @@ -260,10 +222,14 @@ 'studies1' => $miniData['studies'], 'reltype' => ($miniData['reltype']==10) ? $miniData['reltypeSpecial'] : $miniData['reltype'], // si 'autre' -> valeur, sinon le code 'dist' => $miniData['loc'] - ]; + ] ); + + // On ajoute le contact dans la liste des contacts du sujet (si pas déja) + if( !in_array($newId, $subject_set['contacts']) ) + $subject_set['contacts'][] = $newId; // On enregistre la relation avec EGO - $file['relations'][] = [ + $subject_set['relations'][] = [ 'idA' => $subject_id, 'idB' => $newId, 'type' => 2 // relation cellulaire mineure @@ -280,15 +246,14 @@ } - - /* [5] On enregistre les relations de la MATRICE + /* [6] On enregistre les relations de la MATRICE =========================================================*/ - $clen = count($file['contacts']); + $clen = count($finalid); /* (1) On récupére les ids des contacts */ $cIdList = []; - foreach($file['contacts'] as $c=>$contact){ - $id = (int) $c; + foreach($finalid as $lastid=>$newid){ + $id = (int) $lastid; if( !in_array($id, $cIdList) ) $cIdList[$id] = null; @@ -296,31 +261,43 @@ ksort($cIdList); - /* (2) On remplit les relations */ foreach($cIdList as $y=>$yNull){ foreach($cIdList as $x=>$xNull) if( $x < $y ){ // On affiche que sous la diagonale - $idY = $file['contacts'][$y]['id'] - $offset; - $idX = $file['contacts'][$x]['id'] - $offset; + $idY = $finalid[$y]['id'] - $offset; + $idX = $finalid[$x]['id'] - $offset; // Si relation alter-alter $relationXY = isset($matrice[$y]) && in_array($x, $matrice[$y]) || ( isset($matrice[$x]) && in_array($y, $matrice[$x]) ); - array_push($file['relations'], [ + $subject_set['relations'][] = [ 'idA' => $offset + $x, 'idB' => $offset + $y, 'type' => $relationXY ? 1 : 0 // 0->aucune relation 1->relation alter alter - ]); + ]; } } - /* [6] On enregistre tout dans 'lightdb' + /* [6] On les données du sujet =========================================================*/ + $subjectdb->insert($subject_id, $subject_set); + + + /* [7] On enregistre les données des contacts + =========================================================*/ + /* (1) Pour chaque données de contact enregistré */ + foreach($file['contacts'] as $c=>$contactData){ + + + + } + + $db = new lightdb('phone_db'); $db->insert( $subject_id, $file ); $db->close(); diff --git a/build/lightdb/core/lightdb.php b/build/lightdb/core/lightdb.php index aa015d5..5b0af9a 100644 --- a/build/lightdb/core/lightdb.php +++ b/build/lightdb/core/lightdb.php @@ -77,17 +77,16 @@ public function close(){ $this->driver = null; } - /* RETOURNE LA LISTE DES INDEX - + /* RETOURNE UN INDEX * - * @i Index pour lequel on veut la ligne et le hash + * @i [OPT] Index pour lequel on veut la ligne et le hash * * @return Index Tableau associatif contenant le hash et la ligne * */ public function index($i=null){ - return is_numeric($i) ? $this->index : $this->index; + return is_numeric($i) ? $this->index[$i] : $this->index; } diff --git a/build/lightdb/storage/subject/description b/build/lightdb/storage/subject/description new file mode 100644 index 0000000..71144d4 --- /dev/null +++ b/build/lightdb/storage/subject/description @@ -0,0 +1,7 @@ +Contient les équivalences : + +id_subject => { + "subject": [données du sujet] + "contact": [liste des id des contacts], + "relations": [liste des relations] +} diff --git a/doc/storage.md b/doc/storage.md deleted file mode 100644 index d97f138..0000000 --- a/doc/storage.md +++ /dev/null @@ -1,26 +0,0 @@ -### Notation - -> lightdb `a` take the value `c` at key `b` -> "aL:b -> c" - - - - -### I. New subject survey - -INPUT: id_subject, contacts[], relations[] -LIGHTDB: subjectL, contactL, -BEGIN -``` - - // initialisation des données du sujet (contact par name) - subjectL:id_subject -> [ ] - - // On enregistre chaque contact - foreach contacts[] as i=>contact - contactL:uniqid -> contact[i] - subjectL:id_subject[contact[i].name] = uniqid++; - - -``` -END diff --git a/public_html/test.php b/public_html/test.php index 2923101..113290c 100644 --- a/public_html/test.php +++ b/public_html/test.php @@ -7,8 +7,7 @@ $sl = new lightdb('subject'); $cl = new lightdb('contact'); - echo "New subject survey\nT"; - + var_dump("New subject survey"); ?>