58 lines
1.5 KiB
PHP
58 lines
1.5 KiB
PHP
|
<?php
|
||
|
|
||
|
/**
|
||
|
* Class MemberController
|
||
|
*
|
||
|
* Controller de l'action /member
|
||
|
*/
|
||
|
class MemberController {
|
||
|
|
||
|
/**
|
||
|
* @param Base $f3
|
||
|
* @param array $params
|
||
|
*
|
||
|
* Gestion de l'URL /api/member
|
||
|
*/
|
||
|
public function member(Base $f3, array $params) {
|
||
|
try {
|
||
|
$db = Csvdb::getInstance();
|
||
|
} catch (Exception $e) {
|
||
|
$f3->error($e->getCode(), $this->render(array(), $e->getMessage()));
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Fusion du paramètre /member/identifiant avec /member?identifiant=
|
||
|
$params = array_merge($params, $f3->get('GET'));
|
||
|
if (!empty($params['identifiant'])) {
|
||
|
$adherant = $db->get($params['identifiant']);
|
||
|
if (!empty($adherant)) {
|
||
|
echo $this->render(array($adherant));
|
||
|
} else {
|
||
|
echo $this->render(array(), 'Aucun adhérent ne correspond à votre demande');
|
||
|
}
|
||
|
} else {
|
||
|
echo $this->render($db->getAll());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param array $datas
|
||
|
* @param string $error
|
||
|
* @return string
|
||
|
*
|
||
|
* Fonction de rendu JSON pour le WS
|
||
|
*/
|
||
|
private function render(array $datas, $error = '') {
|
||
|
usort($datas, function ($a, $b) {
|
||
|
return strcmp($a['nom'], $b['nom']) + strcmp($a['prenom'], $b['prenom']);
|
||
|
});
|
||
|
|
||
|
return json_encode(array(
|
||
|
'count' => count($datas),
|
||
|
'datas' => $datas,
|
||
|
'error' => $error
|
||
|
));
|
||
|
}
|
||
|
|
||
|
}
|