-
Notifications
You must be signed in to change notification settings - Fork 1
/
equipment-rental.php
365 lines (325 loc) · 12.9 KB
/
equipment-rental.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
* Plugin Name: Equipment Rental
* Plugin URI: http://teslarent.eu/wp-plugins/equipment-rental/
* Description: The plugin helps manage a basic equipment rental service including calendar
* Version: 0.1
* Author: Mario Kadastik
* Author URI: http://teslarent.eu/Mario
* Network: false
* License: GPL2
*/
/* Copyright 2015 Mario Kadastik (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
defined('ABSPATH') or die('No direct access please');
global $er_db_version;
$er_db_version = '1.0';
// Function to create the initial database structure
function er_install() {
global $wpdb;
global $er_db_version;
// Do some preliminary work
$pre = $wpdb->prefix;
$charset = $wpdb->get_charset_collate();
require_once(ABSPATH . 'wp-admin/includes/upgrade.php' );
// Create equipment table
$tab = $pre . 'equipment_assets';
$sql = "CREATE TABLE $tab (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name text NOT NULL,
UNIQUE KEY id (id)
) $charset;";
dbDelta( $sql );
// Create clients table
$tab = $pre . 'equipment_clients';
$sql = "CREATE TABLE $tab (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name text NOT NULL,
email text,
phone text,
UNIQUE KEY id (id)
) $charset;";
dbDelta( $sql );
// Create rentals table
$tab = $pre . 'equipment_rentals';
$sql = "CREATE TABLE $tab (
id mediumint(9) NOT NULL AUTO_INCREMENT,
asset mediumint(9) NOT NULL,
client mediumint(9) NOT NULL,
start datetime NOT NULL,
end datetime NOT NULL,
confirmed tinyint(1) NOT NULL DEFAULT 0,
UNIQUE KEY id (id)
) $charset;";
dbDelta( $sql );
add_option( 'er_db_version', $er_db_version );
// Default options for colors in the calendar as well as the names of those
add_option( 'er_cal_busy_text', 'Hõivatud' );
add_option( 'er_cal_busy_col', '#FF0000' );
add_option( 'er_cal_reserved_text', 'Broneeritud' );
add_option( 'er_cal_reserved_col', '#FFFF00' );
add_option( 'er_cal_free_text', 'Vaba' );
add_option( 'er_cal_free_col', '#00FF00' );
}
// Register hook for initial database creation
register_activation_hook( __FILE__, 'er_install' );
// Let's create the options management menu and page
add_action( 'admin_menu', 'er_plugin_menu' );
function er_plugin_menu() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
add_menu_page( 'Equipment Rental', 'Equipment rental', 'manage_options', 'ER_main', 'er_menu_main' );
add_submenu_page( 'ER_main', 'Equipment list', 'Equipment list', 'manage_options', 'ER_equipment', 'er_menu_equipment' );
add_submenu_page( 'ER_main', 'Client list', 'Client list', 'manage_options', 'ER_clients', 'er_menu_clients' );
add_submenu_page( 'ER_main', 'Rentals', 'Rentals', 'manage_options', 'ER_rentals', 'er_menu_rentals' );
}
function er_menu_main() {
// Handle the updating if the form is posted
if ( isset( $_POST[ "busy_text" ] ) ) {
// Update the fields to the submitted ones
update_option( 'er_cal_busy_text', $_POST[ "busy_text" ] );
update_option( 'er_cal_busy_col', $_POST[ "busy_col" ] );
update_option( 'er_cal_reserved_text', $_POST[ "res_text" ] );
update_option( 'er_cal_reserved_col', $_POST[ "res_col" ] );
update_option( 'er_cal_free_text', $_POST[ "free_text" ] );
update_option( 'er_cal_free_col', $_POST[ "free_col" ] );
}
?>
<h2>Here you can tune generic options for the plugin</h2>
<br/>
<form name="settings_form" method="post" action="">
<table cellpadding=10>
<tbody>
<tr><td align="right">Busy text:</td><td><input type="text" name="busy_text" value="<?php echo get_option('er_cal_busy_text');?>" size=20></td></tr>
<tr><td align="right">Busy color:</td><td bgcolor="<?php echo get_option('er_cal_busy_col');?>"><input type="text" name="busy_col" value="<?php echo get_option('er_cal_busy_col');?>" size=20></td></tr>
<tr><td align="right">Reserved text:</td><td><input type="text" name="res_text" value="<?php echo get_option('er_cal_reserved_text');?>" size=20></td></tr>
<tr><td align="right">Reserved color:</td><td bgcolor="<?php echo get_option('er_cal_reserved_col');?>"><input type="text" name="res_col" value="<?php echo get_option('er_cal_reserved_col');?>" size=20></td></tr>
<tr><td align="right">Free text:</td><td><input type="text" name="free_text" value="<?php echo get_option('er_cal_free_text');?>" size=20></td></tr>
<tr><td align="right">Free color:</td><td bgcolor="<?php echo get_option('er_cal_free_col');?>"><input type="text" name="free_col" value="<?php echo get_option('er_cal_free_col');?>" size=20></td></tr>
<tr><td colspan=2 align="center"><input type=submit name="Update" class="button-primary"></td></tr>
</tbody>
</table>
</form>
<?php
}
function er_menu_equipment() {
global $wpdb;
// Define basics
$but_label = "Add new";
$eq_name = "";
$eq_id = "";
if ( isset( $_POST[ 'eq_id' ] ) && $_POST[ 'action' ] == 'Delete' ) {
// Means we're deleting this entry
$wpdb->delete( $wpdb->prefix . "equipment_assets", array( "id" => $_POST[ 'eq_id' ] ) );
}
if ( isset( $_POST[ 'eq_id' ] ) && $_POST[ 'action' ] == 'Edit' ) {
// Means we got a request to show it for updating
$eq_id = $_POST[ 'eq_id' ];
$eq_name = $wpdb->get_var( 'SELECT name FROM ' . $wpdb->prefix . 'equipment_assets WHERE id = ' . $eq_id . ';' );
$but_label = "Update";
}
if ( isset( $_POST[ 'eq_id' ] ) && $_POST[ 'action' ] == 'Update' ) {
// Means updating is actually happening
$wpdb->update( $wpdb->prefix . 'equipment_assets', array( 'name' => $_POST[ 'eq_name' ] ), array( 'id' => $_POST[ 'eq_id' ] ) );
}
if ( isset( $_POST[ 'eq_name' ] ) && $_POST[ 'action' ] == "Add new" ) {
// New addition
$wpdb->insert( $wpdb->prefix . 'equipment_assets', array( 'name' => $_POST[ 'eq_name' ] ) );
}
?>
<h2>Rental equipment management</h2>
<form name="equipment_form" method="post" action="">
Equipment name:
<input type=hidden name="eq_id" value="<?php echo $eq_id;?>">
<input type=text name="eq_name" value="<?php echo $eq_name;?>" size=40>
<input type="submit" name="action" class="button-primary" value="<?php echo $but_label;?>"></form>
<table cellpadding=5 style="border:1px">
<thead><th>ID</th><th>Asset name</th><th>Actions</th></thead>
<tbody>
<?php
$neq = $wpdb->get_var( 'SELECT COUNT(id) FROM ' . $wpdb->prefix . 'equipment_assets' );
if ($neq) {
$res = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'equipment_assets' );
foreach ($res as $r) {
echo "<tr>
<form method=post action=''>
<td><input type=hidden name=eq_id value=" . $r->id . ">" . $r->id . "</td>
<td>" . $r->name . "</td>
<td><input type=submit name=action value=Edit class=\"button-primary\">
<input type=submit name=action value=Delete class=\"button-primary\"></td>
</tr>";
}
}
?>
</tbody>
</table>
<?php
}
function er_menu_clients() {
global $wpdb;
// Define basics
$but_label = "Add new";
$cl_data = "";
$cl_id = "";
if ( isset( $_POST[ 'cl_id' ] ) && $_POST[ 'action' ] == 'Delete' ) {
// Means we're deleting this entry
$wpdb->delete( $wpdb->prefix . "equipment_clients", array( "id" => $_POST[ 'cl_id' ] ) );
}
if ( isset( $_POST[ 'cl_id' ] ) && $_POST[ 'action' ] == 'Edit' ) {
// Means we got a request to show it for updating
$cl_id = $_POST[ 'cl_id' ];
$cl_data = $wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . 'equipment_clients WHERE id = ' . $cl_id . ';' );
$but_label = "Update";
}
if ( isset( $_POST[ 'cl_id' ] ) && $_POST[ 'action' ] == 'Update' ) {
// Means updating is actually happening
$wpdb->update( $wpdb->prefix . 'equipment_clients',
array(
'name' => $_POST[ 'cl_name' ],
'email' => $_POST[ 'cl_email' ],
'phone' => $_POST[ 'cl_phone' ]
),
array( 'id' => $_POST[ 'cl_id' ] )
);
}
if ( isset( $_POST[ 'cl_name' ] ) && $_POST[ 'action' ] == "Add new" ) {
// New addition
$wpdb->insert( $wpdb->prefix . 'equipment_clients',
array(
'name' => $_POST[ 'cl_name' ],
'email' => $_POST[ 'cl_email' ],
'phone' => $_POST[ 'cl_phone' ]
) );
}
?>
<h2>Client list management</h2>
<form name="equipment_form" method="post" action="">
<input type=hidden name="cl_id" value="<?php echo $cl_id;?>">
<table>
<tr><td>Client name:</td><td><input type=text name="cl_name" value="<?php echo $cl_data->name;?>" size=40></td></tr>
<tr><td>Client e-mail:</td><td><input type=text name="cl_email" value="<?php echo $cl_data->email;?>" size=40></td></tr>
<tr><td>Client phone:</td><td><input type=text name="cl_phone" value="<?php echo $cl_data->phone;?>" size=40></td></tr>
<tr><td colspan=2 align=center><input type="submit" name="action" class="button-primary" value="<?php echo $but_label;?>"></td></tr>
</table>
</form>
<table cellpadding=5 style="border:1px">
<thead><th>ID</th><th>Client name</th><th>E-Mail</th><th>Phone</th><th>Actions</th></thead>
<tbody>
<?php
$neq = $wpdb->get_var( 'SELECT COUNT(id) FROM ' . $wpdb->prefix . 'equipment_clients' );
if ($neq) {
$res = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'equipment_clients' );
foreach ($res as $r) {
echo "<tr>
<form method=post action=''>
<td><input type=hidden name=cl_id value=" . $r->id . ">" . $r->id . "</td>
<td>" . $r->name . "</td>
<td>" . $r->email . "</td>
<td>" . $r->phone . "</td>
<td><input type=submit name=action value=Edit class=\"button-primary\">
<input type=submit name=action value=Delete class=\"button-primary\"></td>
</tr>";
}
}
?>
</tbody>
</table>
<?php
}
// Will get client list
function er_get_client_list() {
return 0;
}
// Will fetch equipment list
function er_get_equipment_list() {
return 0;
}
function er_menu_rentals() {
global $wpdb;
// Define basics
$but_label = "Add new";
$rent_id = "";
$cl_data = er_get_client_list();
$eq_data = er_get_equipment_list();
$rent_data = "";
if ( isset( $_POST[ 'rent_id' ] ) && $_POST[ 'action' ] == 'Delete' ) {
// Means we're deleting this entry
$wpdb->delete( $wpdb->prefix . "equipment_rentals", array( "id" => $_POST[ 'rent_id' ] ) );
}
if ( isset( $_POST[ 'rent_id' ] ) && $_POST[ 'action' ] == 'Edit' ) {
// Means we got a request to show it for updating
$rent_id = $_POST[ 'rent_id' ];
$rent_data = $wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . 'equipment_rentals WHERE id = ' . $rent_id . ';' );
$but_label = "Update";
}
if ( isset( $_POST[ 'cl_id' ] ) && $_POST[ 'action' ] == 'Update' ) {
// Means updating is actually happening
/*
$wpdb->update( $wpdb->prefix . 'equipment_clients',
array(
'name' => $_POST[ 'cl_name' ],
'email' => $_POST[ 'cl_email' ],
'phone' => $_POST[ 'cl_phone' ]
),
array( 'id' => $_POST[ 'cl_id' ] )
);
*/
}
if ( isset( $_POST[ 'cl_name' ] ) && $_POST[ 'action' ] == "Add new" ) {
// New addition
/*
$wpdb->insert( $wpdb->prefix . 'equipment_clients',
array(
'name' => $_POST[ 'cl_name' ],
'email' => $_POST[ 'cl_email' ],
'phone' => $_POST[ 'cl_phone' ]
) );
*/
}
?>
<h2>Rental periods management</h2>
<form name="equipment_form" method="post" action="">
<input type=hidden name="cl_id" value="<?php echo $cl_id;?>">
<table>
<tr><td>Client name:</td><td><input type=text name="cl_name" value="<?php echo $cl_data->name;?>" size=40></td></tr>
<tr><td>Client e-mail:</td><td><input type=text name="cl_email" value="<?php echo $cl_data->email;?>" size=40></td></tr>
<tr><td>Client phone:</td><td><input type=text name="cl_phone" value="<?php echo $cl_data->phone;?>" size=40></td></tr>
<tr><td colspan=2 align=center><input type="submit" name="action" class="button-primary" value="<?php echo $but_label;?>"></td></tr>
</table>
</form>
<table cellpadding=5 style="border:1px">
<thead><th>ID</th><th>Client name</th><th>E-Mail</th><th>Phone</th><th>Actions</th></thead>
<tbody>
<?php
$neq = $wpdb->get_var( 'SELECT COUNT(id) FROM ' . $wpdb->prefix . 'equipment_clients' );
if ($neq) {
$res = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'equipment_clients' );
foreach ($res as $r) {
echo "<tr>
<form method=post action=''>
<td><input type=hidden name=cl_id value=" . $r->id . ">" . $r->id . "</td>
<td>" . $r->name . "</td>
<td>" . $r->email . "</td>
<td>" . $r->phone . "</td>
<td><input type=submit name=action value=Edit class=\"button-primary\">
<input type=submit name=action value=Delete class=\"button-primary\"></td>
</tr>";
}
}
?>
</tbody>
</table>
<?php
}
?>