2016-04-18 17:33:36 +00:00
|
|
|
<?php
|
|
|
|
|
2016-10-18 13:11:37 +00:00
|
|
|
namespace api\module;
|
2016-04-18 17:33:36 +00:00
|
|
|
use \manager\sessionManager;
|
2016-11-05 10:56:03 +00:00
|
|
|
use \database\core\DatabaseDriver;
|
2016-04-18 17:33:36 +00:00
|
|
|
use \manager\ManagerError;
|
2016-10-18 13:11:37 +00:00
|
|
|
use \database\core\Repo;
|
|
|
|
use \lightdb\core\lightdb;
|
2016-04-18 17:33:36 +00:00
|
|
|
|
|
|
|
class subject{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE LA LISTE DE TOUS LES SUJETS
|
|
|
|
*
|
|
|
|
* @return subjects<Array> Tableau contenant les informations de tous les utilisateurs
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getAll(){
|
2016-05-27 10:13:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Contiendra les sujets
|
2016-09-12 16:04:53 +00:00
|
|
|
$subjects = [];
|
2016-05-27 10:13:19 +00:00
|
|
|
|
|
|
|
/* [1] On récupére la liste des sujets
|
2016-04-18 17:33:36 +00:00
|
|
|
=========================================================*/
|
2016-05-27 10:13:19 +00:00
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-10-18 13:11:37 +00:00
|
|
|
$db = new lightdb('survey_db');
|
2016-05-27 10:13:19 +00:00
|
|
|
$ids = array_keys( $db->index() );
|
2016-04-18 17:33:36 +00:00
|
|
|
|
2016-05-27 10:13:19 +00:00
|
|
|
/* (2) On récupère tous les sujets */
|
|
|
|
foreach($ids as $id){
|
|
|
|
$sub = $db->fetch($id)['subject'];
|
2016-05-27 08:39:39 +00:00
|
|
|
|
2016-05-27 10:50:32 +00:00
|
|
|
$sub['creation'] = date('d/m/Y H:i:s', $sub['creation']);
|
2016-05-27 10:13:19 +00:00
|
|
|
$subjects[$id] = $sub;
|
|
|
|
}
|
2016-05-27 10:50:32 +00:00
|
|
|
$db->close();
|
2016-04-18 17:33:36 +00:00
|
|
|
|
2016-05-27 10:13:19 +00:00
|
|
|
|
|
|
|
/* [2] On récupére la liste des sujets pour PHONE
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-10-18 13:11:37 +00:00
|
|
|
$db = new lightdb('phone_db');
|
2016-05-27 10:13:19 +00:00
|
|
|
$ids = array_keys( $db->index() );
|
|
|
|
$db->close();
|
|
|
|
|
2016-05-27 10:50:32 +00:00
|
|
|
/* (2) Si un des sujets de 'survey' est dans PHONE, on ajoute la mention */
|
2016-05-27 10:13:19 +00:00
|
|
|
foreach($subjects as $id=>$data)
|
|
|
|
if( in_array($id, $ids) ) // Si dans phone
|
|
|
|
$subjects[$id]['phone'] = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On récupére la liste des sujets pour FACEBOOK
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-10-18 13:11:37 +00:00
|
|
|
$db = new lightdb('facebook_db');
|
2016-05-27 10:13:19 +00:00
|
|
|
$ids = array_keys( $db->index() );
|
|
|
|
$db->close();
|
|
|
|
|
2016-05-27 10:50:32 +00:00
|
|
|
/* (2) Si un des sujets de 'survey' est dans FACBEOOK, on ajoute la mention */
|
2016-05-27 10:13:19 +00:00
|
|
|
foreach($subjects as $id=>$data)
|
|
|
|
if( in_array($id, $ids) ) // Si dans facebook
|
|
|
|
$subjects[$id]['facebook'] = true;
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] Gestion du retour
|
2016-04-18 17:33:36 +00:00
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-04-18 17:33:36 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
2016-05-27 10:13:19 +00:00
|
|
|
'subjects' => $subjects
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-04-18 17:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-27 10:50:32 +00:00
|
|
|
/* RETOURNE LA LISTE DE TOUS LES AMIS D'UN SUJET
|
|
|
|
*
|
|
|
|
* @subject_id<int> Identifiant du sujet d'enquête
|
|
|
|
*
|
|
|
|
* @return subjects<Array> Tableau contenant les informations de tous les utilisateurs
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getFriends($params){
|
|
|
|
extract($params);
|
|
|
|
|
|
|
|
|
|
|
|
// Contiendra les sujets
|
2016-09-12 16:04:53 +00:00
|
|
|
$subjects = [];
|
2016-05-27 10:50:32 +00:00
|
|
|
|
|
|
|
/* [1] On récupére la liste des sujets
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-10-18 13:11:37 +00:00
|
|
|
$db = new lightdb('survey_db');
|
2016-05-27 10:50:32 +00:00
|
|
|
$fetch = $db->fetch($subject_id);
|
|
|
|
$db->close();
|
|
|
|
|
|
|
|
/* (2) Si on trouve personne, on renvoie une erreur */
|
|
|
|
if( $fetch === false )
|
2016-09-12 16:04:53 +00:00
|
|
|
return [ 'ModuleError' => ManagerError::ModuleError ];
|
2016-05-27 10:50:32 +00:00
|
|
|
|
|
|
|
/* (3) On enregistre ses contacts s'il en a */
|
|
|
|
if( isset($fetch['contacts']) )
|
2016-05-27 14:32:26 +00:00
|
|
|
foreach($fetch['contacts'] as $contact)
|
|
|
|
$subjects[$contact['id']] = $contact;
|
2016-05-27 10:50:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] On récupére la liste des contacts saisis dans PHONE
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-10-18 13:11:37 +00:00
|
|
|
$db = new lightdb('phone_db');
|
2016-05-27 10:50:32 +00:00
|
|
|
$fetch = $db->fetch($subject_id);
|
|
|
|
$db->close();
|
|
|
|
|
|
|
|
/* (2) Si on trouve des contacts, on les ajoute */
|
|
|
|
if( $fetch !== false && isset($fetch['contacts']) )
|
2016-05-27 14:32:26 +00:00
|
|
|
foreach($fetch['contacts'] as $contact)
|
|
|
|
$subjects[$contact['id']] = $contact;
|
2016-05-27 10:50:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On récupére la liste des sujets pour FACEBOOK
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-10-18 13:11:37 +00:00
|
|
|
$db = new lightdb('facebook_db');
|
2016-05-27 10:50:32 +00:00
|
|
|
$fetch = $db->fetch($subject_id);
|
|
|
|
$db->close();
|
|
|
|
|
|
|
|
/* (2) Si on trouve des contacts, on les ajoute */
|
|
|
|
if( $fetch !== false && isset($fetch['contacts']) )
|
2016-05-27 14:32:26 +00:00
|
|
|
foreach($fetch['contacts'] as $contact)
|
|
|
|
$subjects[$contact['id']] = $contact;
|
2016-05-27 10:50:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [4] Gestion du retour
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-05-27 10:50:32 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
|
|
|
'subjects' => $subjects
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-05-27 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-18 17:33:36 +00:00
|
|
|
|
|
|
|
/* CREATION D'UN SUJET
|
|
|
|
*
|
2016-05-27 08:39:39 +00:00
|
|
|
* @name<String> Pseudo du sujet
|
2016-04-18 17:33:36 +00:00
|
|
|
*
|
|
|
|
* @return id_subject<int> Renvoie l'id du sujet cree
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function create($params){
|
|
|
|
extract($params);
|
|
|
|
|
2016-05-27 08:39:39 +00:00
|
|
|
|
|
|
|
/* [1] On récupère l'id unique actuel
|
2016-04-18 17:33:36 +00:00
|
|
|
=========================================================*/
|
2016-10-18 13:11:37 +00:00
|
|
|
$funiq = fopen( __BUILD__.'/src/dynamic/uniqid', 'r+' );
|
2016-05-27 08:39:39 +00:00
|
|
|
flock($funiq, LOCK_EX); // On verrouille le fichier
|
2016-06-01 15:06:18 +00:00
|
|
|
$uniqid = trim( fgets( $funiq ) );
|
|
|
|
|
2016-05-27 08:39:39 +00:00
|
|
|
|
|
|
|
if( !is_numeric($uniqid) )
|
|
|
|
$uniqid = 0;
|
2016-04-18 17:33:36 +00:00
|
|
|
|
2016-05-27 08:39:39 +00:00
|
|
|
// Décalage à appliquer à tous les ids
|
|
|
|
$newId = intval($uniqid) + 1;
|
|
|
|
|
2016-05-27 10:50:32 +00:00
|
|
|
// On crée notre sujet
|
2016-09-12 16:04:53 +00:00
|
|
|
$data = [ 'subject' => [
|
2016-05-27 08:39:39 +00:00
|
|
|
'id' => $newId,
|
|
|
|
'name' => $name,
|
|
|
|
'creation' => time()
|
2016-09-12 16:04:53 +00:00
|
|
|
]];
|
2016-05-27 08:39:39 +00:00
|
|
|
|
2016-05-27 10:50:32 +00:00
|
|
|
/* [2] On crée le sujet dans SURVEYS
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-10-18 13:11:37 +00:00
|
|
|
$db = new lightdb('survey_db');
|
2016-05-27 08:39:39 +00:00
|
|
|
$db->insert( $newId, $data );
|
|
|
|
$db->close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On met à jour le nouvel ID unique
|
|
|
|
=========================================================*/
|
|
|
|
rewind($funiq); // On revient au début du fichier
|
|
|
|
fwrite($funiq, $newId); // On écrit la nouvelle valeur (forcément plus grande)
|
|
|
|
flock($funiq, LOCK_UN); // On débloque le verrou
|
|
|
|
fclose($funiq);
|
|
|
|
|
2016-04-18 17:33:36 +00:00
|
|
|
|
|
|
|
/* [2] Gestion du retour
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-04-18 17:33:36 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
2016-05-27 08:39:39 +00:00
|
|
|
'id_subject' => $newId
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-04-18 17:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-26 14:33:00 +00:00
|
|
|
/* RECHERCHE DE SUJETS
|
|
|
|
*
|
|
|
|
* @name<String> Nom du sujet recherché
|
|
|
|
*
|
|
|
|
* @return results<Array> Tableau contenant les résultats
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function search($params){
|
|
|
|
extract($params);
|
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
|
2016-05-26 14:33:00 +00:00
|
|
|
// Contiendra les sujets
|
2016-09-12 16:04:53 +00:00
|
|
|
$subjects = [];
|
2016-05-26 14:33:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [0] Notre fonction de recherche (comparaison)
|
|
|
|
=========================================================*/
|
|
|
|
function compareSearch($A, $B){
|
2016-11-05 10:59:42 +00:00
|
|
|
// Returns all if no search keyword
|
|
|
|
if( $A == '' )
|
|
|
|
return true;
|
|
|
|
|
2016-05-26 14:33:00 +00:00
|
|
|
// {1} On supprime les espaces et tout en minuscule //
|
|
|
|
$A = str_replace(' ', '', strtolower($A));
|
|
|
|
$B = str_replace(' ', '', strtolower($B));
|
|
|
|
|
|
|
|
// {2} On vérifie si A est dans B et inversement //
|
|
|
|
if( strpos($A, $B) !== false ) return true;
|
|
|
|
if( strpos($B, $A) !== false ) return true;
|
|
|
|
|
|
|
|
return $A == $B;
|
|
|
|
}
|
|
|
|
|
2016-05-27 08:51:05 +00:00
|
|
|
/* [1] On récupére la liste des sujets
|
2016-05-26 14:33:00 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-10-18 13:11:37 +00:00
|
|
|
$db = new lightdb('survey_db');
|
2016-05-26 14:33:00 +00:00
|
|
|
$ids = array_keys( $db->index() );
|
|
|
|
|
|
|
|
/* (2) On récupère tous les sujets */
|
|
|
|
foreach($ids as $id){
|
|
|
|
$sub = $db->fetch($id)['subject'];
|
2016-05-26 17:43:50 +00:00
|
|
|
if( compareSearch($name, $sub['name']) ){
|
|
|
|
|
2016-05-27 08:51:05 +00:00
|
|
|
$sub['creation'] = date('d/m/Y H:i:s', $sub['creation']);
|
|
|
|
$subjects[$id] = $sub;
|
2016-05-26 17:43:50 +00:00
|
|
|
}
|
2016-05-26 14:33:00 +00:00
|
|
|
}
|
2016-05-27 10:50:32 +00:00
|
|
|
$db->close();
|
2016-05-26 14:33:00 +00:00
|
|
|
|
|
|
|
|
2016-05-27 10:13:19 +00:00
|
|
|
/* [2] On récupére la liste des sujets pour PHONE
|
2016-05-26 14:33:00 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-10-18 13:11:37 +00:00
|
|
|
$db = new lightdb('phone_db');
|
2016-05-26 14:33:00 +00:00
|
|
|
$ids = array_keys( $db->index() );
|
2016-05-27 08:51:05 +00:00
|
|
|
$db->close();
|
2016-05-26 17:43:50 +00:00
|
|
|
|
2016-05-27 10:50:32 +00:00
|
|
|
/* (2) Si un des sujets de 'survey' est dans PHONE, on ajoute la mention */
|
2016-05-27 08:51:05 +00:00
|
|
|
foreach($subjects as $id=>$data)
|
|
|
|
if( in_array($id, $ids) ) // Si dans phone
|
|
|
|
$subjects[$id]['phone'] = true;
|
2016-05-26 17:43:50 +00:00
|
|
|
|
2016-05-26 14:33:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-27 10:13:19 +00:00
|
|
|
/* [3] On récupére la liste des sujets pour FACEBOOK
|
2016-05-26 14:33:00 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (1) On initialise et ouvre la bd */
|
2016-10-18 13:11:37 +00:00
|
|
|
$db = new lightdb('facebook_db');
|
2016-05-26 14:33:00 +00:00
|
|
|
$ids = array_keys( $db->index() );
|
2016-05-27 08:51:05 +00:00
|
|
|
$db->close();
|
2016-05-26 17:43:50 +00:00
|
|
|
|
2016-05-27 10:50:32 +00:00
|
|
|
/* (2) Si un des sujets de 'survey' est dans FACBEOOK, on ajoute la mention */
|
2016-05-27 08:51:05 +00:00
|
|
|
foreach($subjects as $id=>$data)
|
|
|
|
if( in_array($id, $ids) ) // Si dans facebook
|
|
|
|
$subjects[$id]['facebook'] = true;
|
2016-05-26 14:33:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] Retour des données
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-05-26 14:33:00 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
|
|
|
'results' => $subjects
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-05-26 14:33:00 +00:00
|
|
|
|
|
|
|
}
|
2016-04-18 17:33:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|