2016-04-30 10:19:51 +00:00
|
|
|
<?php
|
|
|
|
|
2016-10-18 13:11:37 +00:00
|
|
|
namespace api\module;
|
2016-04-30 10:19:51 +00:00
|
|
|
use \manager\sessionManager;
|
2016-11-05 10:56:03 +00:00
|
|
|
use \database\core\DatabaseDriver;
|
2016-04-30 10:19:51 +00:00
|
|
|
use \manager\ManagerError;
|
2016-10-18 13:11:37 +00:00
|
|
|
use \api\core\ModuleRequest;
|
|
|
|
use \database\core\Repo;
|
|
|
|
use \lightdb\core\lightdb;
|
2016-04-30 10:19:51 +00:00
|
|
|
|
2016-06-20 05:54:40 +00:00
|
|
|
|
2016-04-30 10:19:51 +00:00
|
|
|
class input{
|
|
|
|
|
|
|
|
/* TRAITE LES DONNÉES D'UN FORMULAIRE DE TYPE TÉLÉPHONIQUE
|
|
|
|
*
|
2016-04-30 14:13:37 +00:00
|
|
|
* @subject<Array> Tableau contenant les données du sujet
|
|
|
|
* @contacts<Array> Tableau contenant les données des contacts
|
|
|
|
* @mini<Array> Tableau contenant les données des mini fiches relation
|
|
|
|
* @fiches<Array> Tableau contenant les données des fiches relation
|
2016-04-30 10:19:51 +00:00
|
|
|
*
|
|
|
|
* @return subject_id<int> Retourne l'id sujet de l'enquête
|
|
|
|
*
|
|
|
|
*/
|
2016-11-22 13:30:34 +00:00
|
|
|
public static function phone($params){
|
2016-04-30 10:19:51 +00:00
|
|
|
extract($params);
|
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [1] On récupère l'id unique actuel
|
2016-04-30 10:19:51 +00:00
|
|
|
=========================================================*/
|
2016-11-06 13:22:15 +00:00
|
|
|
$funiq = fopen( __BUILD__.'/lightdb/storage/uniqid', 'r+' );
|
2016-05-17 15:05:32 +00:00
|
|
|
flock($funiq, LOCK_EX); // On verrouille le fichier
|
|
|
|
$uniqid = fgets( $funiq );
|
2016-05-17 14:51:24 +00:00
|
|
|
|
|
|
|
if( !is_numeric($uniqid) )
|
|
|
|
$uniqid = 0;
|
2016-05-09 09:01:43 +00:00
|
|
|
|
2016-05-17 14:51:24 +00:00
|
|
|
// Décalage à appliquer à tous les ids
|
2016-05-26 17:43:50 +00:00
|
|
|
$offset = intval($uniqid) + 1;
|
2016-05-09 09:01:43 +00:00
|
|
|
|
2016-05-18 08:08:34 +00:00
|
|
|
// on enregistre l'id du sujet
|
2016-11-22 13:30:34 +00:00
|
|
|
$subject_id = intval($subject['subject_id']);
|
2016-05-18 08:08:34 +00:00
|
|
|
|
2016-05-17 14:51:24 +00:00
|
|
|
// Contiendra la valeur de l'id maximum
|
2016-11-06 13:22:15 +00:00
|
|
|
$maxId = $offset;
|
2016-05-17 14:51:24 +00:00
|
|
|
|
2016-05-09 09:01:43 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [2] On initialise nos variables (lightdb + autres)
|
2016-11-22 10:06:03 +00:00
|
|
|
=========================================================*/
|
2016-11-22 13:30:34 +00:00
|
|
|
/* (1) Fichiers de sortie */
|
2016-11-06 13:22:15 +00:00
|
|
|
$file = [
|
|
|
|
"subject" => "",
|
|
|
|
"relations" => []
|
|
|
|
];
|
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* (2) Bases de données */
|
|
|
|
$subjectdb = new lightdb('subject');
|
|
|
|
$contactdb = new lightdb('contact');
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [3] Données du sujet
|
2016-05-13 16:26:30 +00:00
|
|
|
=========================================================*/
|
2016-11-22 13:30:34 +00:00
|
|
|
$subject_set = $subjectdb->fetch($subject_id);
|
2016-05-09 12:12:02 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* (1) Si le sujet n'existe pas -> ERROR */
|
|
|
|
if( $subject_set === false )
|
|
|
|
return ['ModuleError'=>ManagerError::UnreachableResource];
|
2016-05-09 12:12:02 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* (2) Initialisation des contacts si vide */
|
|
|
|
if( !isset($subject_set['contacts']) )
|
|
|
|
$subject_set['contacts'] = [];
|
2016-05-09 12:12:02 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* (3) Initialisation des relations si vide */
|
|
|
|
if( !isset($subject_set['relations']) )
|
|
|
|
$subject_set['relations'] = [];
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (4) Ajout de l'enquête */
|
|
|
|
if( !in_array('phone', $subject_set['subject']['surveys']) )
|
|
|
|
$subject_set['subject']['surveys'][] = 'phone';
|
|
|
|
|
|
|
|
/* (5) On récupère les noms des contacts */
|
2016-11-22 13:30:34 +00:00
|
|
|
$contactsById = []; // idContact -> nomContact
|
2016-05-09 09:01:43 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
foreach($subject_set['contacts'] as $contactId){
|
|
|
|
$contactId = intval($contactId);
|
|
|
|
$contactData = $contactdb->fetch( $contactId );
|
2016-05-09 09:01:43 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
$contactsById[$contactId] = $contactData['name'];
|
|
|
|
}
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [4] On saisit les fiches + les contacts des fiches
|
2016-05-09 17:02:57 +00:00
|
|
|
=========================================================*/
|
2016-11-06 13:22:15 +00:00
|
|
|
$contactsDone = [
|
|
|
|
"existing" => [], // Contacts exportés déja enregistrés
|
|
|
|
"username" => [] // Nouveaux contacts déja enregistrés
|
|
|
|
];
|
2016-11-22 13:30:34 +00:00
|
|
|
$finalid = []; // id_contact -> id_final_contact (existant ou nouveau)
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
foreach($fiches as $f=>$ficheData){
|
2016-11-22 16:17:04 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (1) On récupère les données du contact associé */
|
2016-11-22 13:30:34 +00:00
|
|
|
if( !isset($contacts[$ficheData['contact']]) )
|
2016-11-23 11:12:53 +00:00
|
|
|
continue;
|
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
$contact = $contacts[ $ficheData['contact'] ];
|
2016-05-09 17:02:57 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (2) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) ){
|
2016-11-22 13:30:34 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
$newId = (int) $contact['existing'];
|
2016-11-22 13:30:34 +00:00
|
|
|
$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']) )
|
2016-11-23 11:12:53 +00:00
|
|
|
continue;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* (3) Si nouveau contact */
|
|
|
|
}else{
|
2016-11-22 13:30:34 +00:00
|
|
|
|
|
|
|
$newId = (int) ($offset+$contact['uid']);
|
2016-11-06 13:22:15 +00:00
|
|
|
$uname = $contact['username'];
|
2016-11-22 13:30:34 +00:00
|
|
|
|
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
|
|
|
// S'il a déja été saisi, on quitte
|
2016-11-06 13:22:15 +00:00
|
|
|
if( in_array($contact['username'], $contactsDone['username']) )
|
2016-11-23 11:12:53 +00:00
|
|
|
continue;
|
2016-11-06 13:22:15 +00:00
|
|
|
}
|
2016-05-17 14:51:24 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (4) Update du @maxId */
|
|
|
|
if( $newId > $maxId )
|
|
|
|
$maxId = (int) $newId;
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* (5) On enregistre les données du contact */
|
|
|
|
$contactdb->insert($newId, [
|
2016-05-30 14:54:41 +00:00
|
|
|
'id' => $newId,
|
2016-11-06 13:22:15 +00:00
|
|
|
'name' => $uname,
|
2016-05-13 16:26:30 +00:00
|
|
|
'sexe' => $ficheData['sexe'],
|
|
|
|
'age' => $ficheData['age'],
|
|
|
|
'studies2' => $ficheData['studies'],
|
|
|
|
'reltype' => ($ficheData['reltype']==10) ? $ficheData['reltypeSpecial'] : $ficheData['reltype'], // si 'autre' -> valeur, sinon le code
|
|
|
|
'dist' => $ficheData['loc'],
|
|
|
|
'job' => $ficheData['job'],
|
|
|
|
'famsit' => $ficheData['famsit'],
|
|
|
|
'city' => $ficheData['city'],
|
|
|
|
'cp' => $ficheData['cp'],
|
2016-05-29 15:51:06 +00:00
|
|
|
'quartier' => $ficheData['quartier'],
|
2016-05-13 16:26:30 +00:00
|
|
|
'duration' => $ficheData['duration'],
|
|
|
|
'context' => $ficheData['context'],
|
|
|
|
'contextExtra' => $ficheData['contextSpecial'],
|
|
|
|
'freq' => $ficheData['freq'],
|
|
|
|
'connect' => $ficheData['connect'],
|
|
|
|
'connectExtra' => $ficheData['connectSpecial']
|
2016-11-22 13:30:34 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
// 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;
|
2016-05-09 17:02:57 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
// On enregistre la relation avec EGO
|
2016-11-22 13:30:34 +00:00
|
|
|
$subject_set['relations'][] = [
|
2016-05-30 14:54:41 +00:00
|
|
|
'idA' => $subject_id,
|
|
|
|
'idB' => $newId,
|
2016-11-06 13:22:15 +00:00
|
|
|
'type' => ($f<20) ? 4 : 5 // 4->appels 5->sms
|
|
|
|
];
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* (x) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) )
|
|
|
|
$contactsDone['existing'][] = $contact['existing'];
|
|
|
|
/* (x+1) Si nouveau contact */
|
|
|
|
else
|
|
|
|
$contactsDone['username'][] = $contact['username'];
|
2016-05-13 16:26:30 +00:00
|
|
|
}
|
2016-05-09 17:02:57 +00:00
|
|
|
|
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [5] On enregistre les mini + les contacts des mini
|
2016-11-06 13:22:15 +00:00
|
|
|
=========================================================*/
|
|
|
|
foreach($mini as $miniData){
|
2016-11-22 16:17:04 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (1) On récupère les données du contact associé */
|
2016-11-22 13:30:34 +00:00
|
|
|
if( !isset($contacts[$miniData['contact']]) )
|
2016-11-23 11:12:53 +00:00
|
|
|
continue;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
$contact = $contacts[ $miniData['contact'] ];
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* (2) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) ){
|
2016-11-22 13:30:34 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
$newId = (int) $contact['existing'];
|
2016-11-22 13:30:34 +00:00
|
|
|
$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']) )
|
2016-11-23 11:12:53 +00:00
|
|
|
continue;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* (3) Si nouveau contact */
|
|
|
|
}else{
|
2016-11-22 13:30:34 +00:00
|
|
|
|
2016-11-22 16:17:04 +00:00
|
|
|
$newId = (int) ($offset+$contact['uid']);
|
2016-11-06 13:22:15 +00:00
|
|
|
$uname = $contact['username'];
|
2016-11-22 13:30:34 +00:00
|
|
|
|
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
|
|
|
// S'il a déja été saisi, on quitte
|
2016-11-06 13:22:15 +00:00
|
|
|
if( in_array($contact['username'], $contactsDone['username']) )
|
2016-11-23 11:12:53 +00:00
|
|
|
continue;
|
2016-11-06 13:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Update du @maxId */
|
|
|
|
if( $newId > $maxId )
|
|
|
|
$maxId = (int) $newId;
|
|
|
|
|
|
|
|
|
|
|
|
// On remplit les données qui iront dans le fichier pour ce contact
|
2016-11-22 13:30:34 +00:00
|
|
|
$contactdb->insert($newId, [
|
2016-11-06 13:22:15 +00:00
|
|
|
'id' => $newId,
|
|
|
|
'name' => $uname,
|
|
|
|
'sexe' => $miniData['sexe'],
|
|
|
|
'age' => $miniData['age'],
|
|
|
|
'studies1' => $miniData['studies'],
|
|
|
|
'reltype' => ($miniData['reltype']==10) ? $miniData['reltypeSpecial'] : $miniData['reltype'], // si 'autre' -> valeur, sinon le code
|
|
|
|
'dist' => $miniData['loc']
|
2016-11-22 13:30:34 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
// 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;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
// On enregistre la relation avec EGO
|
2016-11-22 13:30:34 +00:00
|
|
|
$subject_set['relations'][] = [
|
2016-11-06 13:22:15 +00:00
|
|
|
'idA' => $subject_id,
|
|
|
|
'idB' => $newId,
|
|
|
|
'type' => 2 // relation cellulaire mineure
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
/* (x) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) )
|
|
|
|
$contactsDone['existing'][] = $contact['existing'];
|
|
|
|
/* (x+1) Si nouveau contact */
|
|
|
|
else
|
|
|
|
$contactsDone['username'][] = $contact['username'];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [6] On enregistre les relations de la MATRICE
|
2016-05-09 17:02:57 +00:00
|
|
|
=========================================================*/
|
2016-11-23 11:12:53 +00:00
|
|
|
var_dump('finalid\'s', $finalid);
|
2016-11-22 13:30:34 +00:00
|
|
|
$clen = count($finalid);
|
2016-05-30 14:54:41 +00:00
|
|
|
|
|
|
|
/* (1) On récupére les ids des contacts */
|
2016-09-12 16:04:53 +00:00
|
|
|
$cIdList = [];
|
2016-11-22 13:30:34 +00:00
|
|
|
foreach($finalid as $lastid=>$newid){
|
|
|
|
$id = (int) $lastid;
|
2016-11-06 15:50:29 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
if( !in_array($id, $cIdList) )
|
|
|
|
$cIdList[$id] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2016-05-12 19:47:52 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
// Si relation alter-alter
|
|
|
|
$relationXY = isset($matrice[$y]) && in_array($x, $matrice[$y])
|
|
|
|
|| ( isset($matrice[$x]) && in_array($y, $matrice[$x]) );
|
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
$subject_set['relations'][] = [
|
2016-11-22 13:34:05 +00:00
|
|
|
'idA' => $finalid[$x],
|
|
|
|
'idB' => $finalid[$y],
|
2016-05-30 14:54:41 +00:00
|
|
|
'type' => $relationXY ? 1 : 0 // 0->aucune relation 1->relation alter alter
|
2016-11-22 13:30:34 +00:00
|
|
|
];
|
2016-05-30 14:54:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2016-05-12 19:47:52 +00:00
|
|
|
|
|
|
|
|
2016-11-22 13:30:34 +00:00
|
|
|
/* [6] On les données du sujet
|
2016-05-17 14:51:24 +00:00
|
|
|
=========================================================*/
|
2016-11-22 14:05:28 +00:00
|
|
|
$subjectdb->delete($subject_id);
|
2016-11-22 13:30:34 +00:00
|
|
|
$subjectdb->insert($subject_id, $subject_set);
|
|
|
|
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [7] Ferme les bases de données
|
2016-11-22 13:30:34 +00:00
|
|
|
=========================================================*/
|
2016-11-22 14:05:28 +00:00
|
|
|
$subjectdb->close();
|
|
|
|
$contactdb->close();
|
2016-11-22 13:30:34 +00:00
|
|
|
|
2016-05-18 14:04:17 +00:00
|
|
|
|
|
|
|
/* [7] On met à jour le nouvel ID unique
|
|
|
|
=========================================================*/
|
|
|
|
rewind($funiq); // On revient au début du fichier
|
2016-11-06 13:22:15 +00:00
|
|
|
fwrite($funiq, $maxId); // On écrit la nouvelle valeur (forcément plus grande)
|
2016-05-18 14:04:17 +00:00
|
|
|
flock($funiq, LOCK_UN); // On débloque le verrou
|
|
|
|
fclose($funiq);
|
|
|
|
|
|
|
|
|
|
|
|
/* [9] Gestion du retour
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-05-18 14:04:17 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
|
|
|
'subject_id' => $subject_id
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-05-18 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* TRAITE LES DONNÉES D'UN FORMULAIRE DE TYPE FACEBOOK
|
|
|
|
*
|
|
|
|
* @subject<Array> Tableau contenant les données du sujet
|
|
|
|
* @contacts<Array> Tableau contenant les données des contacts
|
|
|
|
* @mini<Array> Tableau contenant les données des mini fiches relation
|
|
|
|
* @fiches<Array> Tableau contenant les données des fiches relation
|
|
|
|
*
|
|
|
|
* @return subject_id<int> Retourne l'id sujet de l'enquête
|
|
|
|
*
|
|
|
|
*/
|
2016-11-22 14:05:28 +00:00
|
|
|
public static function facebook($params){
|
2016-05-18 14:04:17 +00:00
|
|
|
extract($params);
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [1] On récupère l'id unique actuel
|
2016-05-18 14:04:17 +00:00
|
|
|
=========================================================*/
|
2016-11-06 13:22:15 +00:00
|
|
|
$funiq = fopen( __BUILD__.'/lightdb/storage/uniqid', 'r+' );
|
2016-05-18 14:04:17 +00:00
|
|
|
flock($funiq, LOCK_EX); // On verrouille le fichier
|
|
|
|
$uniqid = fgets( $funiq );
|
|
|
|
|
|
|
|
if( !is_numeric($uniqid) )
|
|
|
|
$uniqid = 0;
|
|
|
|
|
|
|
|
// Décalage à appliquer à tous les ids
|
2016-05-26 17:43:50 +00:00
|
|
|
$offset = intval($uniqid) + 1;
|
2016-05-18 14:04:17 +00:00
|
|
|
|
|
|
|
// on enregistre l'id du sujet
|
2016-11-22 14:05:28 +00:00
|
|
|
$subject_id = intval($subject['subject_id']);
|
2016-05-18 14:04:17 +00:00
|
|
|
|
|
|
|
// Contiendra la valeur de l'id maximum
|
2016-11-06 13:22:15 +00:00
|
|
|
$maxId = $offset;
|
|
|
|
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [2] On initialise nos variables (lightdb + autres)
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Fichiers de sortie */
|
2016-11-06 13:22:15 +00:00
|
|
|
$file = [
|
|
|
|
"subject" => "",
|
|
|
|
"relations" => []
|
|
|
|
];
|
2016-11-22 10:06:03 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (2) Bases de données */
|
|
|
|
$subjectdb = new lightdb('subject');
|
|
|
|
$contactdb = new lightdb('contact');
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [3] Données du sujet
|
2016-05-18 14:04:17 +00:00
|
|
|
=========================================================*/
|
2016-11-22 14:05:28 +00:00
|
|
|
$subject_set = $subjectdb->fetch($subject_id);
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (1) Si le sujet n'existe pas -> ERROR */
|
|
|
|
if( $subject_set === false )
|
|
|
|
return ['ModuleError'=>ManagerError::UnreachableResource];
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (2) Initialisation des contacts si vide */
|
|
|
|
if( !isset($subject_set['contacts']) )
|
|
|
|
$subject_set['contacts'] = [];
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (3) Initialisation des relations si vide */
|
|
|
|
if( !isset($subject_set['relations']) )
|
|
|
|
$subject_set['relations'] = [];
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (4) Ajout de l'enquête */
|
|
|
|
if( !in_array('facebook', $subject_set['surveys']) )
|
|
|
|
$subject_set['surveys'][] = 'facebook';
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (5) On récupère les noms des contacts */
|
|
|
|
$contactsById = []; // idContact -> nomContact
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
foreach($subject_set['contacts'] as $contactId){
|
|
|
|
$contactId = intval($contactId);
|
|
|
|
$contactData = $contactdb->fetch( $contactId );
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
$contactsById[$contactId] = $contactData['name'];
|
|
|
|
}
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [4] On saisit les fiches + les contacts des fiches
|
2016-05-18 14:04:17 +00:00
|
|
|
=========================================================*/
|
2016-11-06 13:22:15 +00:00
|
|
|
$contactsDone = [
|
|
|
|
"existing" => [], // Contacts exportés déja enregistrés
|
|
|
|
"username" => [] // Nouveaux contacts déja enregistrés
|
|
|
|
];
|
2016-11-22 14:05:28 +00:00
|
|
|
$finalid = []; // id_contact -> id_final_contact (existant ou nouveau)
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
foreach($fiches as $f=>$ficheData){
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (1) On récupère les données du contact associé */
|
2016-11-22 14:05:28 +00:00
|
|
|
if( !isset($contacts[$ficheData['contact']]) )
|
|
|
|
break;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
$contact = $contacts[ $ficheData['contact'] ];
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (2) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) ){
|
2016-11-22 14:05:28 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
$newId = (int) $contact['existing'];
|
2016-11-22 14:05:28 +00:00
|
|
|
$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;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* (3) Si nouveau contact */
|
|
|
|
}else{
|
2016-11-22 14:05:28 +00:00
|
|
|
|
|
|
|
$newId = (int) ($offset+$contact['uid']);
|
2016-11-06 13:22:15 +00:00
|
|
|
$uname = $contact['username'];
|
2016-11-22 14:05:28 +00:00
|
|
|
|
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
|
|
|
// S'il a déja été saisi, on quitte
|
2016-11-06 13:22:15 +00:00
|
|
|
if( in_array($contact['username'], $contactsDone['username']) )
|
2016-11-22 14:05:28 +00:00
|
|
|
break;
|
2016-11-06 13:22:15 +00:00
|
|
|
}
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* (4) Update du @maxId */
|
|
|
|
if( $newId > $maxId )
|
|
|
|
$maxId = (int) $newId;
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* (5) On enregistre les données du contact */
|
|
|
|
$contactdb->insert($newId, [
|
2016-05-30 14:54:41 +00:00
|
|
|
'id' => $newId,
|
2016-11-06 13:22:15 +00:00
|
|
|
'name' => $uname,
|
2016-05-18 14:04:17 +00:00
|
|
|
'sexe' => $ficheData['sexe'],
|
|
|
|
'age' => $ficheData['age'],
|
|
|
|
'studies2' => $ficheData['studies'],
|
|
|
|
'reltype' => ($ficheData['reltype']==10) ? $ficheData['reltypeSpecial'] : $ficheData['reltype'], // si 'autre' -> valeur, sinon le code
|
|
|
|
'dist' => $ficheData['loc'],
|
|
|
|
'job' => $ficheData['job'],
|
|
|
|
'famsit' => $ficheData['famsit'],
|
|
|
|
'city' => $ficheData['city'],
|
|
|
|
'cp' => $ficheData['cp'],
|
2016-05-30 14:54:41 +00:00
|
|
|
'quartier' => $ficheData['quartier'],
|
2016-05-18 14:04:17 +00:00
|
|
|
'duration' => $ficheData['duration'],
|
|
|
|
'context' => $ficheData['context'],
|
|
|
|
'contextExtra' => $ficheData['contextSpecial'],
|
|
|
|
'freq' => $ficheData['freq'],
|
|
|
|
'connect' => $ficheData['connect'],
|
|
|
|
'connectExtra' => $ficheData['connectSpecial']
|
2016-11-22 14:05:28 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
// 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;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
// On enregistre la relation avec EGO
|
2016-11-22 14:05:28 +00:00
|
|
|
$subject_set['relations'][] = [
|
2016-11-06 13:22:15 +00:00
|
|
|
'idA' => $subject_id,
|
|
|
|
'idB' => $newId,
|
2016-11-22 14:05:28 +00:00
|
|
|
'type' => ($f<20) ? 6 : 7 // 6->historique 7->messenger
|
2016-11-06 13:22:15 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
/* (x) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) )
|
|
|
|
$contactsDone['existing'][] = $contact['existing'];
|
|
|
|
/* (x+1) Si nouveau contact */
|
|
|
|
else
|
|
|
|
$contactsDone['username'][] = $contact['username'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [5] On enregistre les mini + les contacts des mini
|
2016-11-06 13:22:15 +00:00
|
|
|
=========================================================*/
|
|
|
|
foreach($mini as $miniData){
|
|
|
|
/* (1) On récupère les données du contact associé */
|
2016-11-22 14:05:28 +00:00
|
|
|
if( !isset($contacts[$miniData['contact']]) )
|
|
|
|
break;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
$contact = $contacts[ $miniData['contact'] ];
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* (2) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) ){
|
2016-11-22 14:05:28 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
$newId = (int) $contact['existing'];
|
2016-11-22 14:05:28 +00:00
|
|
|
$uname = $contactsById[ intval($contact['existing']) ];
|
|
|
|
|
|
|
|
$existingData = $dbcontact->fetch( intval($contact['existing']) );
|
2016-11-06 13:22:15 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
$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;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* (3) Si nouveau contact */
|
|
|
|
}else{
|
2016-11-22 14:05:28 +00:00
|
|
|
|
2016-11-22 16:17:04 +00:00
|
|
|
$newId = (int) ($offset+$contact['uid']);
|
2016-11-06 13:22:15 +00:00
|
|
|
$uname = $contact['username'];
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
$finalid[ intval($contact['uid']) ] = $newId;
|
|
|
|
|
|
|
|
// S'il a déja été saisi, on quitte
|
|
|
|
if( in_array($contact['username'], $contactsDone['username']) )
|
|
|
|
break;
|
2016-11-06 13:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Update du @maxId */
|
|
|
|
if( $newId > $maxId )
|
|
|
|
$maxId = (int) $newId;
|
|
|
|
|
|
|
|
|
|
|
|
// On remplit les données qui iront dans le fichier pour ce contact
|
2016-11-22 14:05:28 +00:00
|
|
|
$contactdb->insert($newId, [
|
2016-11-06 13:22:15 +00:00
|
|
|
'id' => $newId,
|
|
|
|
'name' => $uname,
|
|
|
|
'sexe' => $miniData['sexe'],
|
|
|
|
'age' => $miniData['age'],
|
|
|
|
'studies1' => $miniData['studies'],
|
|
|
|
'reltype' => ($miniData['reltype']==10) ? $miniData['reltypeSpecial'] : $miniData['reltype'], // si 'autre' -> valeur, sinon le code
|
|
|
|
'dist' => $miniData['loc']
|
2016-11-22 14:05:28 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
// 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;
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
// On enregistre la relation avec EGO
|
2016-11-22 14:05:28 +00:00
|
|
|
$subject_set['relations'][] = [
|
2016-05-30 14:54:41 +00:00
|
|
|
'idA' => $subject_id,
|
|
|
|
'idB' => $newId,
|
2016-11-22 14:05:28 +00:00
|
|
|
'type' => 3 // relation facebook mineure
|
2016-11-06 13:22:15 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
/* (x) Si le contact a été importé d'une autre enquête */
|
|
|
|
if( isset($contact['existing']) && is_numeric($contact['existing']) )
|
|
|
|
$contactsDone['existing'][] = $contact['existing'];
|
|
|
|
/* (x+1) Si nouveau contact */
|
|
|
|
else
|
|
|
|
$contactsDone['username'][] = $contact['username'];
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-05-18 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [6] On enregistre les relations de la MATRICE
|
2016-05-18 14:04:17 +00:00
|
|
|
=========================================================*/
|
2016-11-22 14:05:28 +00:00
|
|
|
$clen = count($finalid);
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* (1) On récupére les ids des contacts */
|
|
|
|
$cIdList = [];
|
2016-11-22 14:05:28 +00:00
|
|
|
foreach($finalid as $lastid=>$newid){
|
|
|
|
$id = (int) $lastid;
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
if( !in_array($id, $cIdList) )
|
|
|
|
$cIdList[$id] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
ksort($cIdList);
|
|
|
|
|
|
|
|
/* (2) On remplit les relations */
|
|
|
|
foreach($cIdList as $y=>$yNull){
|
|
|
|
foreach($cIdList as $x=>$xNull)
|
2016-05-30 14:54:41 +00:00
|
|
|
if( $x < $y ){ // On affiche que sous la diagonale
|
|
|
|
|
|
|
|
// Si relation alter-alter
|
2016-11-06 13:22:15 +00:00
|
|
|
$relationXY = isset($matrice[$y]) && in_array($x, $matrice[$y])
|
|
|
|
|| ( isset($matrice[$x]) && in_array($y, $matrice[$x]) );
|
2016-05-30 14:54:41 +00:00
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
$subject_set['relations'][] = [
|
|
|
|
'idA' => $finalid[$x],
|
|
|
|
'idB' => $finalid[$y],
|
2016-05-30 14:54:41 +00:00
|
|
|
'type' => $relationXY ? 1 : 0 // 0->aucune relation 1->relation alter alter
|
2016-11-06 13:22:15 +00:00
|
|
|
];
|
2016-05-18 14:04:17 +00:00
|
|
|
|
2016-05-30 14:54:41 +00:00
|
|
|
}
|
2016-11-06 13:22:15 +00:00
|
|
|
}
|
2016-05-18 14:04:17 +00:00
|
|
|
|
|
|
|
|
2016-11-22 14:05:28 +00:00
|
|
|
/* [6] On les données du sujet
|
2016-05-18 14:04:17 +00:00
|
|
|
=========================================================*/
|
2016-11-22 14:05:28 +00:00
|
|
|
$subjectdb->insert($subject_id, $subject_set);
|
|
|
|
|
|
|
|
|
|
|
|
/* [7] On enregistre les données des contacts
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On enregistre chaque contact */
|
|
|
|
foreach($file['contacts'] as $c=>$contactData)
|
|
|
|
$contactdb->insert($contactData['id'], $contactData);
|
|
|
|
|
2016-05-12 19:47:52 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
/* [7] On met à jour le nouvel ID unique
|
2016-05-17 14:18:45 +00:00
|
|
|
=========================================================*/
|
2016-05-17 15:05:32 +00:00
|
|
|
rewind($funiq); // On revient au début du fichier
|
2016-11-06 13:22:15 +00:00
|
|
|
fwrite($funiq, $maxId); // On écrit la nouvelle valeur (forcément plus grande)
|
2016-05-17 15:05:32 +00:00
|
|
|
flock($funiq, LOCK_UN); // On débloque le verrou
|
|
|
|
fclose($funiq);
|
2016-05-17 14:51:24 +00:00
|
|
|
|
2016-11-06 13:22:15 +00:00
|
|
|
|
|
|
|
/* [9] Gestion du retour
|
2016-04-30 10:19:51 +00:00
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-04-30 10:19:51 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
2016-05-13 16:26:30 +00:00
|
|
|
'subject_id' => $subject_id
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-04-30 10:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|