Commit ce08df09 authored by Denis S. Valdenaire's avatar Denis S. Valdenaire

lot of mofication/corrections - users edit begins

parent 1fc22d6e
<?php
class Role extends Record {
public $id;
public $name, $description;
public $table = "roles";
public function __construct($id = 0) {
if (!$this->id) {
$this->id = $id;
}
}
public static function fetch_user_roles($user_id) {
$roles = array();
// SQL SELECT roles user_roles
$sql = "SELECT r.id, r.name, r.description
FROM roles r, user_roles ur
WHERE r.id = ur.role_id
AND ur.user_id = ".$user_id;
$GLOBALS["data"]->select($sql, $roles, "Role");
return $roles;
}
}
......@@ -23,9 +23,23 @@ class User extends Record {
FROM users
WHERE id = ".$id;
$GLOBALS["data"]->select($sql, $user, "User");
if($user->id != 0) {
$user->roles = Role::fetch_user_roles($user->id);
}
return $user;
}
public static function fetch_all(&$users) {
$users = array();
// SQL SELECT users
$sql = "SELECT id, name, email, active,
created_at, updated_at
FROM users
ORDER BY name";
$GLOBALS["data"]->select($sql, $users, "User", true);
return sizeof($users);
}
public static function validate($name, $password) {
// SQL SELECT users
$sql = "SELECT id, name, email, active
......@@ -36,6 +50,8 @@ class User extends Record {
if($user->id == 0) {
$user = new User(0);
$user->alert_msg = "Echec de l'authentification";
} else {
$user->roles = Role::fetch($user->id);
}
return $user;
}
......@@ -48,6 +64,17 @@ class User extends Record {
$GLOBALS["data"]->select($sql, $users, "User");
return sizeof($users);
}
public function has_role($role_name) {
while(list($key, $val) = each($this->roles)) {
if($val->name == $role_name) {
reset($this->roles);
return true;
}
}
reset($this->roles);
return false;
}
}
?>
<?php
$render = "users/loginform";
switch($_REQUEST["a"]) {
case "login":
$render = "user/loginform";
break;
if($user->id == 0) {
$render = "users/loginform";
} else {
switch($_REQUEST["a"]) {
case "edit":
try {
if($user->id == $data->db_escape_string($_REQUEST["i"])) {
$luser = $user;
$render = "users/edit";
} else {
$luser = User::fetch($data->db_escape_string($_REQUEST["i"]));
if($luser->id != 0) {
$render = "users/edit";
} else {
$render = "users/not_found"; // TODO
}
}
} catch(data_exception $e) {
$render = "data_exception";
}
break;
case "login":
$render = "users/loginform";
break;
default:
try {
User::fetch_all($users);
$render = "users/list";
} catch(data_exception $e) {
$render = "data_exception";
}
break;
}
}
include("views/".$render.".php");
......@@ -74,31 +74,46 @@ if(!array_key_exists("user_id", $_SESSION)) {
<a class="navbar-brand" href="index.php">
<img id="logo" src="images/logo-texte.png" alt="phpLudoreve"></a>
</div>
<?php if($user->id != 0) { ?>
<?php if($user->id != 0) {
$menu_entries = array();
?>
<div id="navbar" class="collapse navbar-collapse navbar-right">
<ul class="nav navbar-nav">
<li><a href="index.php?o=members">Adhérents</a></li>
<?php if($user->has_role("games")) {
$menu_entries["esar_categories"] = "Catégories Esar";
?>
<li><a href="index.php?o=games">Jeux</a></li>
<?php } ?>
<?php if($user->has_role("members")) {
$menu_entries["membership_types"] = "Types d'adhésion";
$menu_entries["payment_methods"] = "Méthodes de paiement";
?>
<li><a href="index.php?o=members">Adhérents</a></li>
<?php } ?>
<?php if($user->has_role("admin")) { ?>
<li><a href="index.php?o=users&a=list">Comptes</a></li>
<?php } ?>
<?php if(sizeof($menu_entries)) { ?>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
aria-expanded="false">Options <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="index.php?o=membership_types&a=list">Types d'adhésion</a></li>
<li><a href="index.php?o=payment_methods&a=list">Méthodes de paiement</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
<?php while(list($key, $val) = each($menu_entries)) { ?>
<li><a href="index.php?o=<?=$key?>&a=list"><?=$val?></a></li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="index.php?o=users&a=edit&i=<?=$user->id?>"><span class="glyphicon glyphicon-user"></span> Mon compte</a></li>
<li><a href="index.php?a=logout"><span class="glyphicon glyphicon-log-out"></span> Se déconnecter</a></li>
</ul>
<form class="navbar-form navbar-right">
<form class="navbar-form navbar-right">
<div id="search-all" >
<input class="typeahead" type="text" placeholder="Recherche...">
</div>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="index.php?o=users&a=edit&i=<?=$user->id?>"><span class="glyphicon glyphicon-user"></span> Account</a></li>
<li><a href="index.php?a=logout"><span class="glyphicon glyphicon-log-out"></span> Logout</a></li>
</ul>
</form>
</div>
<?php } ?>
</div>
......@@ -132,12 +147,12 @@ if($user->id == 0) {
</form>
<footer>
<?php if($debug) { ?>
<pre>
REQUEST :
<?php print_r($_REQUEST) ?>
SESSION :
<?php print_r($_SESSION) ?>
</pre>
<pre>
REQUEST :
<?php print_r($_REQUEST) ?>
SESSION :
<?php print_r($_SESSION) ?>
</pre>
<?php } ?>
</footer>
<!-- Placed at the end of the document so the pages load faster -->
......@@ -149,96 +164,8 @@ SESSION :
<script src="js/moment-with-locales.min.js"></script>
<script src="js/bootstrap-datetimepicker.js"></script>
<script src="js/bootstrap-switch.min.js"></script>
<script src="js/functions.js"></script>
<?php if($user->id != 0) { ?>
<script type="application/javascript">
$(document).ready(function () {
var members = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('full_name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: { url : 'api.php?o=members&a=name_list',
cache: false }
});
var games = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: { url : 'api.php?o=games&a=name_list',
cache: false }
});
$('#search-members .typeahead').typeahead({
highlight: true
},
{
name: 'members',
display: 'full_name',
source: members,
templates: {
header: '<h3 class="category-name">Adhérents</h3>'
}
});
$('#search-games-for-loans .typeahead').typeahead({
highlight: true
},
{
name: 'games',
display: 'name',
source: new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: { url : 'api.php?o=games&a=name_list&filter=available',
cache: false }
})
}).bind('typeahead:selected', function(obj, datum, name) {
if(typeof datum.id !== 'undefined') {
console.log(datum);
$('#game_id').val(datum.id);
}
});
$('#search-all .typeahead').typeahead({
highlight: true
},
{
name: 'members',
display: 'full_name',
source: members,
templates: {
header: '<h3 class="category-name">Adhérents</h3>'
}
},
{
name: 'games',
display: 'name',
source: games,
templates: {
header: '<h3 class="category-name">Jeux</h3>'
}
});
// from https://github.com/twitter/typeahead.js/issues/300 suggestion
$('#search-all').bind('typeahead:selected', function(obj, datum, name) {
// alert(JSON.stringify(datum)); // contains datum value, tokens and custom fields
// outputs, e.g., {"redirect_url":"http://localhost/test/topic/test_topic","image_url":"http://localhost/test/upload/images/t_FWnYhhqd.jpg","description":"A test description","value":"A test value","tokens":["A","test","value"]}
// in this case I created custom fields called 'redirect_url', 'image_url', 'description'
if(typeof datum.full_name !== 'undefined') {
window.location.href = "index.php?o=members&a=edit&i=" + datum.id;
} else {
window.location.href = "index.php?o=games&a=edit&i=" + datum.id;
}
});
// every check box on site turned into a switch except with data-switch-with-ajax flag
$("input[type=\"checkbox\"]").not("[data-switch-with-ajax]").bootstrapSwitch({
onText: "Oui",
offText: "Non"
});
});
/*
TODO : Display calendar events via ajax
See documentation at https://github.com/zabuto/calendar
*/
</script>
<script src="js/functions.js"></script>
<?php } ?>
</body>
</html>
......@@ -56,8 +56,8 @@ function deleteFamilyMember(id) {
// refresh the family_member list with the response
$(document).ready(function () {
$('#add_family_member').click(function(){
set_value('o', 'family_members');
set_value('a', 'add');
$('#o').val('family_members');
$('#a').val('add');
var formData = new FormData($('#defaultform')[0]);
$.ajax({
url: 'api.php', // post on the API
......@@ -76,7 +76,7 @@ $(document).ready(function () {
processData: false
});
// restore values
set_value('o', 'members');
set_value('a', 'edit');
$('#o').val('members');
$('#a').val('edit');
});
});
function set_value(myField, myValue) {
document.getElementById(myField).value = myValue;
}
$(document).ready(function () {
var members = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('full_name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: { url : 'api.php?o=members&a=name_list',
cache: false }
});
function apply_value(myField, myValue) {
set_value(myField, myValue);
document.defaultform.submit();
}
var games = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: { url : 'api.php?o=games&a=name_list',
cache: false }
});
$('#search-members .typeahead').typeahead({
highlight: true
},
{
name: 'members',
display: 'full_name',
source: members,
templates: {
header: '<h3 class="category-name">Adhérents</h3>'
}
});
var xhr = null;
function getXhr() {
if (window.XMLHttpRequest) // Firefox et autres
xhr = new XMLHttpRequest();
else if (window.ActiveXObject){ // Internet Explorer
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
}
else { // XMLHttpRequest non support� par le navigateur
alert("XMLHTTPRequest objects not supported.");
}
return xhr;
}
function modif_date(currentDate, myDiv, myField){
getXhr();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
contenu = xhr.responseText;
document.getElementById(myDiv).innerHTML = contenu;
}
$('#search-games-for-loans .typeahead').typeahead({
highlight: true
},
{
name: 'games',
display: 'name',
source: new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: { url : 'api.php?o=games&a=name_list&filter=available',
cache: false }
})
}).bind('typeahead:selected', function(obj, datum, name) {
if(typeof datum.id !== 'undefined') {
console.log(datum);
$('#game_id').val(datum.id);
}
});
xhr.open("POST","async/date_async.php",true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send("date="+currentDate+"&field="+myField+"&div="+myDiv);
}
$('#search-all .typeahead').typeahead({
highlight: true
},
{
name: 'members',
display: 'full_name',
source: members,
templates: {
header: '<h3 class="category-name">Adhérents</h3>'
}
},
{
name: 'games',
display: 'name',
source: games,
templates: {
header: '<h3 class="category-name">Jeux</h3>'
}
});
// from https://github.com/twitter/typeahead.js/issues/300 suggestion
$('#search-all').bind('typeahead:selected', function(obj, datum, name) {
// alert(JSON.stringify(datum)); // contains datum value, tokens and custom fields
// outputs, e.g., {"redirect_url":"http://localhost/test/topic/test_topic","image_url":"http://localhost/test/upload/images/t_FWnYhhqd.jpg","description":"A test description","value":"A test value","tokens":["A","test","value"]}
// in this case I created custom fields called 'redirect_url', 'image_url', 'description'
if(typeof datum.full_name !== 'undefined') {
window.location.href = "index.php?o=members&a=edit&i=" + datum.id;
} else {
window.location.href = "index.php?o=games&a=edit&i=" + datum.id;
}
});
// every check box on site turned into a switch except with data-switch-with-ajax flag
$("input[type=\"checkbox\"]").not("[data-switch-with-ajax]").bootstrapSwitch({
onText: "Oui",
offText: "Non"
});
});
/*
TODO : Display calendar events via ajax
See documentation at https://github.com/zabuto/calendar
*/
......@@ -60,8 +60,8 @@ function deleteMedia(id) {
// send a new file via an AJAX call, in case of success
// refresh the media list with the response
$('#add_media').click(function(){
set_value('o', 'medias');
set_value('a', 'upload');
$('#o').val('medias');
$('#a').val('upload');
var formData = new FormData($('#defaultform')[0]);
$.ajax({
url: 'api.php', // post on the API
......@@ -80,10 +80,6 @@ $('#add_media').click(function(){
processData: false
});
// restore values
set_value('o', 'games');
set_value('a', 'edit');
$('#o').val('games');
$('#a').val('edit');
});
<div class="form-group">
<label class="control-label col-sm-2" for="name">Nom</label>
<div class="col-sm-4">
<input type="text" id="name" name="name" class="form-control" value="<?=$membership_type->name?>"/>
</div>
<label class="control-label col-sm-2" for="prix">Prix</label>
<div class="col-sm-4">
<input type="text" id="prix" name="prix" class="form-control" value="<?=$membership_type->price?>"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="description">Description</label>
<div class="col-sm-10">
<textarea id="description" name="description" class="form-control" rows="4"><?=$game->description?></textarea>
</div>
</div>
<div class="form-group">
<?php if ($game->id_jeu != 0) { ?>
<div class="col-sm-4 col-sm-offset-4">
<input type="submit" class="btn btn-primary" value="Enregistrer les changements" onClick="set_value('a', 'update');">
<input type="button" class="btn btn-danger" value="Supprimer" onClick="if(confirm('Really ?')) {set_value('a','delete'); defaultform.submit()}">
<?php } else { ?>
<div class="col-sm-2 col-sm-offset-6">
<input type="button" class="btn btn-primary" value="Créer" onClick="set_value('a', 'create');validate_and_submit()">
<?php } ?>
</div>
</div>
......@@ -2,7 +2,7 @@
<h2>Liste des jeux</h2>
</div>
<div class="col-sm-4" align="center">
<span class="btn btn-success" onClick="set_value('a', 'new'); defaultform.submit()">
<span class="btn btn-success" onClick="$('#a').val('new'); defaultform.submit()">
<i class="glyphicon glyphicon-plus"></i>
<span>Nouveau jeu ...</span>
</span>
......
<div class="row">
<div class="col-sm-4">
<!-- right hand side of the screen, w/ loans status & new buttons -->
<div class="thumbnail">
......
......@@ -2,7 +2,7 @@
<h2>Liste des adhérents</h2>
</div>
<div class="col-sm-4" align="center">
<span class="btn btn-success" onClick="set_value('a', 'new'); defaultform.submit()">
<span class="btn btn-success" onClick="$('#a').val('new'); defaultform.submit()">
<i class="glyphicon glyphicon-plus"></i>
<span>Nouvelle adhésion...</span>
</span>
......
......@@ -37,7 +37,7 @@
<button type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span></button>
</a>
<a onClick="if(confirm('Êtes vous sur ?')) { set_value('a', 'delete_loan'); set_value('i', '<?=$val->id?>'); defaultform.submit()}" href="#">
<a onClick="if(confirm('Êtes vous sur ?')) { $('a').val('delete_loan'); $('#i').val('<?=$val->id?>'); defaultform.submit()}" href="#">
<button type="button" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
</a>
......@@ -49,7 +49,7 @@
<div class="col-sm-12" align="center">
<input type="button" class="btn btn-primary" id="back_button" value="&lt;&lt; Retour à la fiche adhérent">
<?php if($member->has_valid_subscription()) { ?>
<span class="btn btn-success btn-md" onClick="set_value('a', 'new'); set_value('o', 'loans'); defaultform.submit()">
<span class="btn btn-success btn-md" onClick="$('#a').val('new'); $('#o').val('loans'); defaultform.submit()">
<i class="glyphicon glyphicon-plus"></i>
<span>Nouvel emprunt...</span>
</span>
......
......@@ -39,7 +39,7 @@
<button type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span></button>
</a>
<a onClick="if(confirm('Êtes vous sur ?')) { set_value('a', 'delete_subscription'); set_value('i', '<?=$val->id?>'); defaultform.submit()}" href="#">
<a onClick="if(confirm('Êtes vous sur ?')) { $('#a').val('delete_subscription'); $('i').val('<?=$val->id?>'); defaultform.submit()}" href="#">
<button type="button" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
</a>
......@@ -50,7 +50,7 @@
<div class="form-group">
<div class="col-sm-12" align="center">
<input type="button" class="btn btn-primary" id="back_button" value="&lt;&lt; Retour à la fiche adhérent">
<span class="btn btn-success btm-md" onClick="set_value('a', 'new'); set_value('o', 'subscriptions'); defaultform.submit()">
<span class="btn btn-success btm-md" onClick="$('a').val('new'); $('o').val('subscriptions'); defaultform.submit()">
<i class="glyphicon glyphicon-plus"></i>
<span>Nouvelle adhésion...</span>
</span>
......
......@@ -20,7 +20,7 @@
<button type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span></button>
</a>
<a onClick="if(confirm('Êtes vous sur ?')) { set_value('a', 'delete'); set_value('i', '<?=$val->id?>'); defaultform.submit()}" href="#">
<a onClick="if(confirm('Êtes vous sur ?')) { $('#a').val('delete'); $('#i').val('<?=$val->id?>'); defaultform.submit()}" href="#">
<button type="button" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
</a>
......@@ -28,7 +28,7 @@
<?php } ?>
</table>
<div class="col-sm-8 col-sm-offset-2" align="center">
<span class="btn btn-success btm-md" onClick="set_value('a', 'new'); defaultform.submit()">
<span class="btn btn-success btm-md" onClick="$('a').val('new'); defaultform.submit()">
<i class="glyphicon glyphicon-plus"></i>
<span>Nouveau type</span>
</span>
......
......@@ -18,7 +18,7 @@
<button type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span></button>
</a>
<a onClick="if(confirm('Êtes vous sur ?')) { set_value('a', 'delete'); set_value('i', '<?=$val->id?>'); defaultform.submit()}" href="#">
<a onClick="if(confirm('Êtes vous sur ?')) { $('#a').val('delete'); $('#i').val('<?=$val->id?>'); defaultform.submit()}" href="#">
<button type="button" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
</a>
......@@ -26,7 +26,7 @@
<?php } ?>
</table>
<div class="col-sm-8 col-sm-offset-2" align="center">
<span class="btn btn-success btm-md" onClick="set_value('a', 'new'); defaultform.submit()">
<span class="btn btn-success btm-md" onClick="$('#a').val('new'); defaultform.submit()">
<i class="glyphicon glyphicon-plus"></i>
<span>Nouvelle méthode...</span>
</span>
......
<div class="panel panel-default">
<div class="panel-heading">
<h4><!-- class="panel-title" -->
<span class="glyphicon glyphicon-th-list" style="margin-right: 10px" ></span>
<?=($luser->id != 0) ? $luser->name : "Nouvel utilisateur"?>
</h4>
</div>
<div class="panel-body">
<div class="form-group">
<label class="control-label col-sm-2" for="name">Nom</label>
<div class="col-sm-4">
<input type="text" id="name" name="name" class="form-control" value="<?=$luser->name?>"/>
</div>
<label class="control-label col-sm-2" for="email">Mail</label>
<div class="col-sm-4">
<input type="text" id="email" name="email" class="form-control" value="<?=$luser->email?>"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="membership_type_id">Type d'adhésion</label>
<div class="col-sm-4">
<select id="membership_type_id" name="membership_type_id" class="form-control">
</select>
<script>
$('#membership_type_id').html('<option value="">Loading...</option>');
$.ajax({url: 'api.php?o=membership_types&a=list',
success: function(output) {
var html = '';
$.each(output, function(key, val){
html = html + '<option value="' + val.id + '"'
+ (val.id == <?=(int)$subscription->membership_type_id?> ? ' selected ' : '' ) + '>'
+ val.name + '</option>';
});
$('#membership_type_id').html(html);
},
error: function (xhr, ajaxOptions, thrownError) {
// well, that's weird, ok :)
$('#membership_type_id').html('<option value="">' + xhr.status + ' ' + thrownError + '</option>');
// alert(xhr.status + " " + thrownError);
}});
</script>
</div>
<label class="control-label col-sm-2" for="active">Activé</label>
<div class="col-sm-1">
<input type="checkbox" id="active" name="active" class="form-control"
<?=($luser->active ? "checked" : "")?>/>