-
Notifications
You must be signed in to change notification settings - Fork 0
/
editPlaces.php
154 lines (154 loc) · 7.98 KB
/
editPlaces.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
session_start();
require_once('config.php');
require_once('autoload.php');
Helper_Access::rejectIfLogout();
if(isset($_POST['action'])):
switch($_POST['action']) {
case 'add':
$place = new Model_Place();
$place->setName($_POST['name']);
$place->save();
echo 'ok/%/'.$place->getId().'/%/'.$place->getName();
break;
case 'edit':
$place = new Model_Place($_POST['id']);
$place->setName($_POST['name']);
$place->save();
echo 'ok/%/'.$place->getName();
break;
case 'delete':
$place = new Model_Place($_POST['id']);
$place->remove();
echo 'ok';
break;
}
else:
$places = Model_Place::getAllPlaces();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<style type="text/css">
.pointer {
cursor: pointer;
}
.hidden {
display: none;
}
</style>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">
var action_create = "<img src=\"media/add.png\" alt=\"Ajouter\" class=\"place_action_add\" />";
var action_edit = "<img src=\"media/edit.png\" alt=\"Éditer\" class=\"place_action_edit\" /> <img src=\"media/delete.png\" alt=\"Supprimer\" class=\"place_action_delete\" />";
var action_confirm = "<img src=\"media/confirm_edit.png\" alt=\"Confirmer\" class=\"place_action_confirm_edit\" /> <img src=\"media/cancel_edit.png\" alt=\"Annuler\" class=\"place_action_cancel_edit\" />";
var action_loading = "<img src=\"media/loading-mini.gif\" alt=\"Chargement\" />";
var action_alert = "<img src=\"media/alert.png\" alt=\"Erreur\" />";
$(document).ready(function() {
$("#lastline").live('focus',function() {
$(this).removeAttr("id");
$(this).append("<td class=\"place_action pointer\">"+action_create+"</td>");
$("#place_container").append("<tr class=\"place_div\" id=\"lastline\"><td class=\"place_name\"><input type=\"text\" class=\"place_input\" /></td></tr>")
});
$(".place_action_add").live('click', function() {
var id = $(".place_action").index($(this).parent());
$($(".place_name")[id]).children(".place_input").attr("disabled","disabled");
$($(".place_action")[id]).removeClass("pointer");
$($(".place_action")[id]).html(action_loading);
$.ajax({
type: "POST",
url: "editPlaces.php",
data: "action=add&name="+$.trim($($(".place_name")[id]).children(".place_input").val()),
success: function(msg) {
msg = msg.split("/%/");
if($.trim(msg[0]) == "ok") {
$($(".place_div")[id]).html("<td class=\"place_name\">"+$.trim(msg[2])+"</td><td class=\"place_action\">"+action_edit+"</td><td class=\"place_id hidden\">"+msg[1]+"</td>");
$($(".place_action")[id]).addClass("pointer");
} else {
$($(".place_action")[id]).html(action_alert);
}
}
})
});
$(".place_action_edit").live('click', function() {
var id = $(".place_action").index($(this).parent());
var name = $($(".place_name")[id]).text();
$($(".place_name")[id]).html("<input type=\"text\" class=\"place_input\" value=\""+name+"\" /><span class=\"place_original hidden\">"+name+"</span>");
$($(".place_action")[id]).html(action_confirm);
});
$(".place_action_confirm_edit").live('click', function() {
var id = $(".place_action").index($(this).parent());
var name = $($(".place_name")[id]).children(".place_input").val();
var id_db = $($(".place_id")[id]).text();
$($(".place_action")[id]).children(".place_input").attr("disabled","disabled");
$($(".place_action")[id]).removeClass("pointer");
$($(".place_action")[id]).html(action_loading);
$.ajax({
type: "POST",
url: "editPlaces.php",
data: "action=edit&id="+id_db+"&name="+name,
success: function(msg) {
msg = msg.split("/%/");
if($.trim(msg[0]) == "ok") {
$($(".place_name")[id]).text($.trim(msg[1]));
$($(".place_action")[id]).html(action_edit);
$($(".place_action")[id]).addClass("pointer");
} else {
$($(".place_action")[id]).html(action_alert);
}
}
})
});
$(".place_action_cancel_edit").live('click', function() {
var id = $(".place_action").index($(this).parent());
var name = $($(".place_name")[id]).children(".place_original").text();
$($(".place_name")[id]).text(name);
$($(".place_action")[id]).html(action_edit);
});
$(".place_action_delete").live('click', function() {
var id = $(".place_action").index($(this).parent());
var name = $($(".place_name")[id]).text();
var id_db = $($(".place_id")[id]).text();
$($(".place_action")[id]).removeClass("pointer");
$($(".place_action")[id]).html(action_loading);
if(confirm("Êtes-vous sûr de vouloir supprimer la place "+name+" ?")) {
$.ajax({
type: "POST",
url: "editPlaces.php",
data: "action=delete&id="+id_db,
success: function(msg) {
if($.trim(msg) == "ok") {
$($(".place_div")[id]).fadeOut(1000, function() {
$(this).remove();
})
} else {
$($(".place_action")[id]).html(action_alert);
}
}
})
} else {
$($(".place_action")[id]).html(action_edit);
$($(".place_action")[id]).addClass("pointer");
}
});
$("#refresh").click(function() {
window.location = "editplaces.php";
});
});
</script>
</head>
<body>
<?php include_once 'search.php';?>
<h1>Gestion des lieux</h1>
<table id="place_container">
<?php foreach($places as $place): ?>
<tr class="place_div"><td class="place_name"><?php echo $place->getName() ?></td><td class="place_action pointer"><img src="media/edit.png" alt="Éditer" class="place_action_edit" /> <img src="media/delete.png" alt="Supprimer" class="place_action_delete" /></td><td class="place_id hidden"><?php echo $place->getId() ?></td></tr>
<?php endforeach ?>
<tr class="place_div" id="lastline"><td class="place_name"><input type="text" class="place_input" /></td></tr>
</table>
<p><button id="refresh">Rafraîchir</button></p>
</body>
</html>
<?php endif ?>