forked from staypress/staypressbooking
/
sp_booking_widgets.php
231 lines (194 loc) · 8.23 KB
/
sp_booking_widgets.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
<?php
/*
Plugin Name: StayPress Booking Plugin Widgets
Version: 1.3
Plugin URI: http://staypress.org
Description: StayPress Booking Widgets
Author: StayPress team
Author URI: http://staypress.org
Copyright 2012 (email: support@staypress.com)
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
*/
require_once('includes/config.php');
require_once('classes/common.php');
// Set up my location
SPBCommon::set_booking_url(__FILE__);
SPBCommon::set_booking_dir(__FILE__);
class sp_availabiltycalendar extends WP_Widget {
function sp_availabiltycalendar() {
// Load the text-domain
$locale = apply_filters( 'staypress_locale', get_locale() );
$mofile = SPBCommon::booking_dir( "includes/lang/booking-$locale.mo" );
if ( file_exists( $mofile ) )
load_plugin_textdomain( 'booking', $mofile );
$widget_ops = array( 'classname' => 'sp_availabiltycalendar', 'description' => __('Availability calendar widget', 'booking') );
$control_ops = array('width' => 250, 'height' => 350, 'id_base' => 'sp_availabiltycalendar');
$this->WP_Widget( 'sp_availabiltycalendar', __('Availability Calendar', 'booking'), $widget_ops, $control_ops );
add_action('init', array(&$this, 'enqueue_page_headings'));
}
function enqueue_page_headings() {
// Only need these on the public side of the site
if(is_admin()) {
return;
}
// Check if we are actually being used anywhere
if(is_active_widget(false, false, 'sp_availabiltycalendar')) {
// we are an active widget so add in our styles and js
wp_enqueue_style('bookingwidgetcss', SPBCommon::booking_url('css/booking.calendarwidget.css'), array());
}
}
function widget( $args, $instance ) {
extract( $args );
// build the check array
$defaults = array(
'title' => __('Availability', 'booking'),
'parsetype' => 'url',
'property_id' => false,
'months' => 12,
'bookedclass' => 'booked',
'weekstarts' => 'sunday',
'excludestatus' => 'draft'
);
foreach($defaults as $key => $value) {
if(isset($instance[$key])) {
$defaults[$key] = $instance[$key];
}
}
//sp_booking_show_calendar($startyear, $startmonth, $property_id, $excludestatus = array('draft'), $holdingdiv = true)
echo $before_widget;
$title = apply_filters('widget_title', $instance['title'] );
if ( $title ) {
echo $before_title . $title . $after_title;
}
// Show the calendar li list.
// Start this month
$start = date("Y-m-01");
// get the exclude list
$excludestatus = explode(",", $instance['excludestatus']);
foreach( (array) $excludestatus as $key => $value) {
$excludestatus[$key] = trim($value);
}
// get the property id
switch( $instance['parsetype'] ) {
case 'define': if(defined('STAYPRESS_ON_PROPERTY_PAGE')) $property_id = (int) STAYPRESS_ON_PROPERTY_PAGE;
break;
case 'below': $property_id = (int) $instance['property_id'];
break;
case 'url': $uri = $_SERVER['REQUEST_URI'];
$number = (int) $instance['property_id'];
//
$urisplit = explode("/",$uri);
if( is_numeric($number) && count($urisplit) > (int) $number ) {
$property_id = $urisplit[$number];
} else {
$property_id = false;
}
break;
case 'querystring':
$property_id = (int) $_GET[$instance['property_id']];
break;
}
for($n = 0; $n < (int) $instance['months']; $n++) {
$showing = strtotime('+' . $n . ' months', strtotime($start));
if(function_exists('sp_booking_show_calendar')) {
sp_booking_show_calendar(date("Y", $showing), date("n", $showing), $property_id, $excludestatus, $instance['bookedclass'], true);
}
}
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$defaults = array(
'title' => __('Availability', 'booking'),
'parsetype' => 'url',
'property_id' => false,
'months' => 12,
'bookedclass' => 'booked',
'weekstarts' => 'sunday',
'excludestatus' => 'draft'
);
foreach ( $defaults as $key => $val ) {
$instance[$key] = $new_instance[$key];
}
return $instance;
}
function form( $instance ) {
$defaults = array(
'title' => __('Availability', 'booking'),
'parsetype' => 'url',
'property_id' => false,
'months' => 12,
'bookedclass' => 'booked',
'weekstarts' => 'sunday',
'excludestatus' => 'draft'
);
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<p>
<?php _e('Set your calendar display options with the settings below','booking'); ?>
</p>
<p>
<?php _e('Title:','booking'); ?><br/>
<input type='text' class='widefat' name='<?php echo $this->get_field_name( 'title' ); ?>' id='<?php echo $this->get_field_id( 'title' ); ?>' value='<?php echo esc_attr(stripslashes($instance['title'])); ?>' />
</p>
<p>
<?php _e('Number of months:','booking'); ?><br/>
<select name='<?php echo $this->get_field_name( 'months' ); ?>' id='<?php echo $this->get_field_id( 'months' ); ?>'>
<?php
for($n = 1; $n <= 24; $n++) {
?>
<option value='<?php echo $n; ?>' <?php if($instance['months'] == $n) echo "selected='selected'"; ?>><?php echo $n; ?></option>
<?php
}
?>
</select>
</p>
<p>
<?php _e('Get property details from:','booking'); ?><br/>
<select name='<?php echo $this->get_field_name( 'parsetype' ); ?>' id='<?php echo $this->get_field_id( 'parsetype' ); ?>'>
<option value='define' <?php if($instance['parsetype'] == 'define') echo "selected='selected'"; ?>><?php echo __('Automatic','booking'); ?></option>
<option value='below' <?php if($instance['parsetype'] == 'below') echo "selected='selected'"; ?>><?php echo __('Setting below','booking'); ?></option>
<option value='url' <?php if($instance['parsetype'] == 'url') echo "selected='selected'"; ?>><?php echo __('Parse URL','booking'); ?></option>
<option value='querystring' <?php if($instance['parsetype'] == 'querystring') echo "selected='selected'"; ?>><?php echo __('Querystring','booking'); ?></option>
</select>
</p>
<p>
<?php _e('Property ID:','booking'); ?><br/><small><?php _e('or url segment / querystring','booking'); ?></small><br/>
<input type='text' class='widefat' name='<?php echo $this->get_field_name( 'property_id' ); ?>' id='<?php echo $this->get_field_id( 'property_id' ); ?>' value='<?php echo esc_attr(stripslashes($instance['property_id'])); ?>' />
</p>
<!--
<p>
<?php _e('Week starts:','booking'); ?><br/>
<select name='<?php echo $this->get_field_name( 'weekstarts' ); ?>' id='<?php echo $this->get_field_id( 'weekstarts' ); ?>'>
<option value='sunday' <?php if($instance['weekstarts'] == 'sunday') echo "selected='selected'"; ?>><?php echo __('Sunday','booking'); ?></option>
<option value='monday' <?php if($instance['weekstarts'] == 'monday') echo "selected='selected'"; ?>><?php echo __('Monday','booking'); ?></option>
</select>
</p>
-->
<p>
<?php _e('Exclude booking status:','booking'); ?><br/><small><?php _e('comma separated','booking'); ?></small><br/>
<input type='text' class='widefat' name='<?php echo $this->get_field_name( 'excludestatus' ); ?>' id='<?php echo $this->get_field_id( 'excludestatus' ); ?>' value='<?php echo esc_attr(stripslashes($instance['excludestatus'])); ?>' />
</p>
<p>
<?php _e('Booked class:','booking'); ?><br/>
<input type='text' class='widefat' name='<?php echo $this->get_field_name( 'bookedclass' ); ?>' id='<?php echo $this->get_field_id( 'bookedclass' ); ?>' value='<?php echo esc_attr(stripslashes($instance['bookedclass'])); ?>' />
</p>
<p> </p>
<?php
}
}
function sp_bookingwidgets_register() {
register_widget( 'sp_availabiltycalendar' );
}
add_action( 'widgets_init', 'sp_bookingwidgets_register' );
?>