-
Notifications
You must be signed in to change notification settings - Fork 2
/
custom-shortcodes-cb-bookings-overviews.php
225 lines (194 loc) · 8.46 KB
/
custom-shortcodes-cb-bookings-overviews.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
<?php
/*
Plugin Name: CB Shortcodes: Booking overviews
Plugin URI: https://github.com/flotte-berlin/cb-shortcodes
Description: Shortcodes for displaying cb-bookings overviews on a page. Place in a non-public page!
Version: 1.2.1
Author: gundelfisch
Author URI: https://flotte-berlin.de
License: GPLv2 or later
*/
/***
* Shortcodes for displaying cb-bookings overviews on a page
* Attention: the results contain personal user-data and should be displayed only on private pages for selected users!
* Author: gundelfisch
* Version: 1.0.0.
*
* *
* [cb_bookings_preview] coming bookings of all locations with booker's names (abbreviated)
* [cb_bookings_overview] bookings overview of 1 location with booker's contact data, parameter
* 'locid' (id of location)
* 'days' (max. days +/- today, default 15)
* [cb_bookings_location] bookings overview for location manager, parameter
* 'days' (max. days +/- today, default 15)
* 'acf' (ACF fieldname, default 'user_locations')
*
* sortable table works with Plugin 'Table Sorter'
* ACF = Plugin 'Advanced Custom Fields' (select 1-n locations in user profile)
*/
/****************** coming bookings of all locations: ****************/
function cb_bookings_preview_shortcode( $atts ) {
$today = date("Y-m-d");
$heute = date("j. n. Y");
$print = '';
global $wpdb;
$cbTable = $wpdb->prefix . "cb_bookings";
$query = $wpdb->prepare("SELECT * FROM $cbTable WHERE status = 'confirmed' AND date_end >= '%s' ORDER by location_id ASC, item_id ASC, date_start ASC",
$today
);
$bookings = $wpdb->get_results($query);
$location = '';
$item = '';
$countLoc = 0;
if ( $bookings ) {
$print = "<table class='user-bookings tablesorter'><thead><tr class='bg'><th>Standort</th><th>Lastenrad</th><th>Buchungen</th><th>Anzahl</th></tr></thead><tbody>";
foreach ( $bookings as $booking )
{
if ($booking->location_id != $location or $booking->item_id != $item) {
if ($countLoc > 0) {
$print .= "</ul></td><td>".$countLoc."</td></tr>";
}
$countLoc = 0;
$location = $booking->location_id;
$item = $booking->item_id;
$loc_name = get_the_title ($location);
$item_name = get_the_title ($item);
$print .= "<tr><td>".$loc_name. "</td>";
$print .= "<td><b><a href='".get_permalink($item)."'><span class='green'>". $item_name . "</span></a></b></td>";
$anchor = $item_name.":S".$location;
$show = 'document.getElementById("'.$anchor.'").style.display="block"';
$hide = 'document.getElementById("'.$anchor.'").style.display="none"';
$print .= "<td><a class='anker' name='a:".$anchor."'></a><a href='#a:".$anchor."' onclick='".$show."' ondblclick='".$hide."'>Details</a>";
$print .= "<ul id='".$anchor."' style='display:none;'>";
}
$countLoc++;
$user = get_userdata ( $booking->user_id );
$date_start = DateTime::createFromFormat('Y-m-d', $booking->date_start);
$date_end = DateTime::createFromFormat('Y-m-d', $booking->date_end);
$print .= "<li>".$date_start->format('d.m.') . " - " . $date_end->format('d.m.') ." ".$user->first_name." ". substr($user->last_name,0,1).".</li>";
}
if ($countLoc > 0) {
$print .= "</ul></td><td>".$countLoc."</td></tr>";
}
$print .= "</tbody></table>";
}
return $print;
}
add_shortcode( 'cb_bookings_preview', 'cb_bookings_preview_shortcode' );
/****************** bookings overview of 1 location: ****************/
function cb_bookings_overview_shortcode( $atts ) {
if ( isset( $atts['locid'] ) && ! ( empty ($atts['locid'] ) ) ) { // make sure the location id is set
$date = new dateTime(current_time('mysql'));
$heute = $date->format("j. n. Y");
$today = $date->format("Y-m-d");
$location = $atts['locid'];
$max_days = isset ( $atts['days'] ) ? $atts['days'] : 15;
if (!$max_days > 0) {$max_days = 15;}
$start_date = new dateTime();
$start_date = $start_date->modify('-'.$max_days.' days');
$date_begin = $start_date->format('Y-m-d');
$end_date = new dateTime();
$end_date = $end_date->modify('+'.$max_days.' days');
$date_last = $end_date->format('Y-m-d');
$loc_name = get_the_title ($location);
$print = "<p><b>Buchungsübersicht für Standort: ". $loc_name. "</b><br>(Buchungsstand vom ". $heute . " für +/-".$max_days." Tage)</p>";
$trenner = "</th><th>";
$print .= "<table><tr><th>von".$trenner."bis".$trenner."gebucht von".$trenner."Mail".$trenner."Telefon</th></tr>";
$trenner = "</td><td>";
global $wpdb;
$cbTable = $wpdb->prefix . "cb_bookings";
$query = $wpdb->prepare("SELECT * FROM $cbTable WHERE status = 'confirmed' AND location_id = %s AND date_end >= '%s' AND date_start <= '%s' ORDER by item_id ASC, date_start ASC",
$location,
$date_begin,
$date_last
);
$bookings = $wpdb->get_results($query);
$item = '';
$next_booking = 0;
foreach ( $bookings as $booking )
{
if ($booking->item_id != $item) {
$next_booking = 0;
$item = $booking->item_id;
$item_name = get_the_title ($item);
$print .= "<tr><td colspan='5'><b>Lastenrad: <a class='green' href='".get_permalink($item)."'>". $item_name . "</a></b></td></tr>";
}
$user = get_user_by ( 'id', $booking->user_id );
$date_start = DateTime::createFromFormat('Y-m-d', $booking->date_start);
$date_end = DateTime::createFromFormat('Y-m-d', $booking->date_end);
$style='';
if ($booking->date_end < $today) {$style = "class='bg'"; }
else if ($booking->date_start >= $today and $next_booking != 1 ){
$style = "class='next'";
$next_booking = 1;
}
$print .= "<tr ".$style."><td>".$date_start->format('j. n.').$trenner.$date_end->format('j. n.').$trenner. $user->first_name . ' ' .$user->last_name.$trenner.$user->user_email.$trenner.$user->phone."</td></tr>";
}
$print .= "</table>";
return $print;
} else { // if ( isset( $atts['locid'] ) && ! ( empty ($atts['locid'] ) ) )
echo ("You must provide a Location id!");
} // end if locid
}
add_shortcode( 'cb_bookings_overview', 'cb_bookings_overview_shortcode' );
/****************** bookings overview for location manager: ****************/
function cb_bookings_location_shortcode( $atts ) {
$fieldname = isset ( $atts['acf'] ) ? $atts['acf'] : 'user_locations';
$max_days = isset ( $atts['days'] ) ? $atts['days'] : 15;
$current_user = wp_get_current_user();
if ( 0 != $current_user->ID ) {
$user_info = get_userdata($current_user->ID);
$print = "<p>Hallo ". $user_info->first_name .",";
if (in_array( 'administrator', (array) $user_info->roles )
or in_array( 'editor', (array) $user_info->roles )
or in_array( 'contributor', (array) $user_info->roles )
){
$print .= "<br>als Administrator siehst du hier immer die Buchungsübersicht zu allen Standorten:</p>";
} else {
$print .= "<br>für Partner werden hier Buchungsinfos nur zu den Standorten angezeigt, die im Benutzerprofil eingetragen sind:</p>";
}
if (in_array( 'administrator', (array) $user_info->roles )
or in_array( 'editor', (array) $user_info->roles )
or in_array( 'contributor', (array) $user_info->roles )
) {
$print .= "<p>als Teammitglied siehst du hier zuerst noch alle Standort-Ansprechpartner:</p>";
$args = array(
'role__not_in' => array('subscriber'),
'meta_key' => $fieldname,
'meta_value' => '',
'meta_compare' => '!='
);
$users = get_users( $args );
$trenner = "</th><th>";
$trenner1 = "</th><th class = 'sortless'>";
$print .= '<table class = "tablesorter"><thead><tr><th>Wer'.$trenner1.'Mail'.$trenner1.'Telefon'.$trenner.' Standorte</th></thead><tbody>';
$trenner = "</td><td>";
foreach ( $users as $user ) {
$user_locations = get_field($fieldname, 'user_'.$user->ID);
if ( $user_locations) {
$print .= "<tr><td>".$user->first_name." ".$user->last_name.$trenner.$user->user_email.$trenner.$user->phone.$trenner."<ul><li>".implode('</li><li>', array_column($user_locations, 'post_title'))."</li></ul></td></tr>";
}
}
$print .= "</tbody></table>";
}
if ( in_array( 'administrator', (array) $user_info->roles ) ) {
$locations = get_posts( array(
'post_type' => 'cb_locations',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => -1
) );
} else {
$locations = get_field('user_locations', 'user_'.$current_user->ID);
}
if ( $locations ) {
foreach ( $locations as $location ) {
$atts = array ('locid' => $location->ID, 'days' => $max_days);
$print .= cb_bookings_overview_shortcode( $atts );
}
}
}
return $print;
}
add_shortcode( 'cb_bookings_location', 'cb_bookings_location_shortcode' );
?>