-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom-availability.php
200 lines (172 loc) · 5.9 KB
/
room-availability.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
<?php
/*
Plugin Name: Room Availability
Plugin URI:
Description:
Version: 1.0.0
Author: Thorsten Gawantka
Author URI:
License: GPLv2
*/
/*
Copyright (C) 2014 Thorsten Gawantka
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
defined('ABSPATH') or die("No script kiddies please!");
require_once 'inc/RoomAvailability.php';
class Roomavailability_Base {
const YEARS_TO_RENDER = 2;
}
class Roomavailability_Front extends Roomavailability_Base {
const bootstrap_script = <<< 'EOT'
<script type="text/javascript">
var availabilities = %s;
window.onload = function() {
render_roomavailability()
};
</script>
EOT;
const container = '<div id="roomavailability">%s</div>';
const optionTemplate = '<option value="%s" %s>%s</option>';
const controlForm = <<< 'EOT'
<form>
<fieldset>
<label>%s</label>
<select id="month">
%s
</select>
<label>%s</label>
<select id="year">
%s
</select>
</fieldset>
</form>
EOT;
private $thisMonth;
private $thisYear;
function __construct() {
$this->thisMonth = date('n');
$this->thisYear = date('Y');
}
function render_controls() {
$monthNames = array(
"Januar",
"Februar",
"März",
"April",
"Mai",
"Juni",
"Juli",
"August",
"September",
"Oktober",
"November",
"Dezember");
$monthOptions = array();
for ($i = 0; $i < count($monthNames); $i++) {
$atts = '';
if ($i + 1 === $this->thisMonth) {
$atts = 'selected';
}
array_push($monthOptions, sprintf(self::optionTemplate, $i + 1, $atts, $monthNames[$i]));
}
$yearOptions = array();
for ($i = 0; $i < Roomavailability_Base::YEARS_TO_RENDER; $i++) {
$year = $this->thisYear + $i;
$atts = '';
if ($year == $this->thisYear) {
$atts = 'selected';
}
array_push($yearOptions, sprintf(self::optionTemplate, $year, $atts, $year));
}
return sprintf(self::controlForm, 'Monat', implode($monthOptions), 'Jahr', implode($yearOptions));
}
/**
* Query for all rooms. Rooms are usually pages with custom field
* 'availability'.
*
* @return array An array of room objects.
*/
function query_rooms() {
$query = array(
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'hierarchical' => 0,
'meta_key' => 'availability',
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($query);
return $pages;
}
function query_availabilities() {
$rooms = $this->query_rooms();
$availabilities = array();
foreach ($rooms as $room) {
$value = get_post_meta($room->ID, 'availability', true);
$availability = new RoomAvailability($room->post_title, $value);
array_push($availabilities, $availability);
}
return $availabilities;
}
}
/**
* Root function of room-availability plugin. This function replaces the
* shortcode [roomavailability] or variations of
* [roomavailability room="Room 1"] with an overview of all rooms or the
* specified room.
*
* @param type $atts The shortcodes provided attributes.
* @return string The replacement of the shortcode.
*/
function render_roomavailability($atts) {
$a = shortcode_atts(array(
'room' => NULL
), $atts);
$roomav = new Roomavailability_Front();
$availabilities = array();
foreach ($roomav->query_availabilities() as $value) {
array_push($availabilities, $value->toArray());
}
$availabilitiesJson = json_encode($availabilities);
$room = $a['room'];
if ($room === NULL) {
// TODO Render overview
} else {
// TODO Render room
}
$script = sprintf(Roomavailability_Front::bootstrap_script, $availabilitiesJson);
$container = sprintf(Roomavailability_Front::container, $roomav->render_controls());
return $script . $container;
}
function register_scripts() {
wp_enqueue_script('room-availability', plugins_url('js/room-availability.js', __FILE__), array('jquery'));
wp_enqueue_style('room-availability', plugins_url('css/site.css', __FILE__));
}
add_action('wp_enqueue_scripts', 'register_scripts');
add_shortcode('roomavailability', 'render_roomavailability');
add_action('rest_api_init', function () {
register_rest_field('page', 'availability', array(
'get_callback' => function ($object) {
return get_post_meta($object['id'], 'availability', true);
},
'update_callback' => function ($value, $object) {
return update_post_meta($object->ID, 'availability', $value);
},
'schema' => array(
'description' => __('Availability of the room', 'roomavailability'),
'type' => 'string',
'context' => array('view', 'edit')
),
));
});