PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.9
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.9
3.5.4 3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / admin / agent-settings / class-wpsc-agent-leaves.php

class-wpsc-agent-leaves.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.4.9, at includes/admin/agent-settings/class-wpsc-agent-leaves.php

407 lines 12.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly!
4 }
5
6 if ( ! class_exists( 'WPSC_Agent_Leaves' ) ) :
7
8 final class WPSC_Agent_Leaves {
9
10 /**
11 * Initialize this class
12 *
13 * @return void
14 */
15 public static function init() {
16
17 // listing.
18 add_action( 'wp_ajax_wpsc_get_agent_leaves', array( __CLASS__, 'get_agent_leaves' ) );
19 add_action( 'wp_ajax_wpsc_get_agent_leave_events', array( __CLASS__, 'get_agent_leave_events' ) );
20
21 // add new.
22 add_action( 'wp_ajax_wpsc_get_add_agent_leaves', array( __CLASS__, 'get_add_agent_leaves' ) );
23 add_action( 'wp_ajax_wpsc_set_add_agent_leaves', array( __CLASS__, 'set_add_agent_leaves' ) );
24
25 // delete.
26 add_action( 'wp_ajax_wpsc_delete_agent_leave', array( __CLASS__, 'delete_agent_leave' ) );
27 }
28
29 /**
30 * Get agent leaves list
31 *
32 * @return void
33 */
34 public static function get_agent_leaves() {
35
36 if ( ! WPSC_Functions::is_site_admin() ) {
37 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
38 }
39
40 $locale = explode( '_', get_locale() );?>
41
42 <div class="wpsc-setting-header"><h2><?php esc_attr_e( 'Leaves', 'supportcandy' ); ?></h2></div>
43 <div class="wpsc-setting-section-body">
44 <div class="wpsc-dock-container">
45 <?php
46 printf(
47 /* translators: Click here to see the documentation */
48 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
49 '<a href="https://supportcandy.net/docs/working-hours/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
50 );
51 ?>
52 </div>
53 <div id="wpsc-calendar"></div>
54 <script>
55 var calendarEl = document.getElementById('wpsc-calendar');
56 var calendar = new FullCalendar.Calendar(calendarEl, {
57 headerToolbar: {
58 left: 'prev,next today',
59 center: 'title',
60 right: 'dayGridMonth,timeGridWeek,timeGridDay'
61 },
62 selectable: true,
63 locale: '<?php echo esc_attr( $locale[0] ); ?>',
64 select: function(info) {
65
66 var start = info.start;
67 var end = info.end;
68 end.setDate(end.getDate()-1);
69
70 var dateSelected = [];
71 do {
72 var d = start.toLocaleDateString('en-CA');
73 dateSelected.push(d);
74 start.setDate(parseInt(start.getDate())+1);
75 } while (start <= end);
76
77 wpsc_get_add_agent_leaves(dateSelected, '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_add_agent_leaves' ) ); ?>');
78 },
79 events: function(info, successCallback, failureCallback) {
80
81 var data = {
82 action: 'wpsc_get_agent_leave_events',
83 start: info.start.toLocaleDateString('en-CA'),
84 end: info.end.toLocaleDateString('en-CA'),
85 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_agent_leave_events' ) ); ?>'
86 };
87 jQuery.post(supportcandy.ajax_url, data, function (response) {
88 successCallback(response);
89 });
90
91 },
92 eventClick: function(info) {
93 wpsc_delete_agent_leave(info.event.id, '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_agent_leave' ) ); ?>' );
94 }
95 });
96 calendar.render();
97
98 // ADD translation for confirmation window of delete event
99 supportcandy.translations.deleteLeaveConfirmation = '<?php esc_attr_e( 'Are you sure to delete this leave?', 'supportcandy' ); ?>';
100
101 </script>
102 </div>
103 <?php
104 wp_die();
105 }
106
107 /**
108 * Load agent leave events
109 *
110 * @return void
111 */
112 public static function get_agent_leave_events() {
113
114 if ( check_ajax_referer( 'wpsc_get_agent_leave_events', '_ajax_nonce', false ) != 1 ) {
115 wp_send_json_error( 'Unauthorized request!', 401 );
116 }
117
118 global $wpdb;
119
120 if ( ! WPSC_Functions::is_site_admin() ) {
121 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
122 }
123
124 $start_date = isset( $_POST['start'] ) ? sanitize_text_field( wp_unslash( $_POST['start'] ) ) : '';
125 if ( ! $start_date ) {
126 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
127 }
128 $start_date = new DateTime( $start_date );
129
130 $end_date = isset( $_POST['end'] ) ? sanitize_text_field( wp_unslash( $_POST['end'] ) ) : '';
131 if ( ! $end_date ) {
132 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
133 }
134 $end_date = new DateTime( $end_date );
135
136 $response = array();
137
138 // non-recurring.
139 $holidays = WPSC_Holiday::find(
140 array(
141 'meta_query' => array(
142 'relation' => 'AND',
143 array(
144 'slug' => 'agent',
145 'compare' => '>',
146 'val' => 0,
147 ),
148 array(
149 'slug' => 'is_recurring',
150 'compare' => '=',
151 'val' => 0,
152 ),
153 array(
154 'slug' => 'holiday',
155 'compare' => 'BETWEEN',
156 'val' => array(
157 $start_date->format( 'Y-m-d H:i:s' ),
158 $end_date->format( 'Y-m-d H:i:s' ),
159 ),
160 ),
161 ),
162 )
163 )['results'];
164 foreach ( $holidays as $holiday ) {
165 $response[] = array(
166 'id' => $holiday->id,
167 'title' => $holiday->agent->name,
168 'start' => $holiday->holiday->format( 'Y-m-d' ),
169 'color' => '#f0932b',
170 'textColor' => '#ffffff',
171 );
172 }
173
174 // recurring.
175 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'psmsc_holidays WHERE ';
176 $sql .= 'is_recurring=1 AND agent > 0 AND ';
177 $sql .= "DATE_FORMAT(holiday, '%m-%d') BETWEEN '" . $start_date->format( 'm-d' ) . "' and '" . $end_date->format( 'm-d' ) . "'";
178 $results = $wpdb->get_results( $sql );
179 foreach ( $results as $holiday ) {
180
181 $holiday = new WPSC_Holiday( $holiday->id );
182
183 $start = '';
184 if ( intval( $start_date->format( 'Y' ) ) == intval( $end_date->format( 'Y' ) ) ) {
185 $start .= $start_date->format( 'Y' ) . '-' . $holiday->holiday->format( 'm-d' );
186 } elseif ( $holiday->holiday->format( 'm' ) == '12' ) {
187 $start .= $start_date->format( 'Y' ) . '-' . $holiday->holiday->format( 'm-d' );
188 } else {
189 $start .= $end_date->format( 'Y' ) . '-' . $holiday->holiday->format( 'm-d' );
190 }
191
192 $response[] = array(
193 'id' => $holiday->id,
194 'title' => $holiday->agent->name,
195 'start' => $start,
196 'color' => '#eb4d4b',
197 'textColor' => '#ffffff',
198 );
199 }
200
201 wp_send_json( $response, 200 );
202 }
203
204 /**
205 * Add agent leaves modal
206 *
207 * @return void
208 */
209 public static function get_add_agent_leaves() {
210
211 if ( check_ajax_referer( 'wpsc_get_add_agent_leaves', '_ajax_nonce', false ) != 1 ) {
212 wp_send_json_error( 'Unauthorized request!', 401 );
213 }
214
215 if ( ! WPSC_Functions::is_site_admin() ) {
216 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
217 }
218
219 $date_selected = isset( $_POST['dateSelected'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['dateSelected'] ) ) ) : array();
220 if ( ! $date_selected ) {
221 wp_send_json_error( 'Bad Request', 401 );
222 }
223
224 $title = esc_attr( wpsc__( 'Add new', 'supportcandy' ) );
225 $unique_id = uniqid( 'wpsc_' );
226
227 ob_start();
228 ?>
229 <form action="#" onsubmit="return false;" class="wpsc-frm-agent-holiday-actions">
230
231 <div class="wpsc-input-group">
232 <div class="label-container">
233 <label for=""><?php esc_attr_e( 'Agents', 'supportcandy' ); ?></label>
234 </div>
235 <select class="<?php echo esc_attr( $unique_id ); ?>" name="agents[]" multiple></select>
236 </div>
237
238 <div class="wpsc-input-group is-recurring">
239 <div class="label-container">
240 <label for=""><?php esc_attr_e( 'Repeate every year', 'supportcandy' ); ?></label>
241 </div>
242 <select name="is-recurring">
243 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
244 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
245 </select>
246 </div>
247
248 <input type="hidden" name="action" value="wpsc_set_add_agent_leaves">
249 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_add_agent_leaves' ) ); ?>">
250
251 </form>
252 <script>
253 jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
254 ajax: {
255 url: supportcandy.ajax_url,
256 dataType: 'json',
257 delay: 250,
258 data: function (params) {
259 return {
260 q: params.term, // search term
261 page: params.page,
262 action: 'wpsc_agent_autocomplete_admin_access',
263 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_agent_autocomplete_admin_access' ) ); ?>',
264 isMultiple: 1,
265 isAgentgroup: 0
266 };
267 },
268 processResults: function (data, params) {
269 var terms = [];
270 if ( data ) {
271 jQuery.each( data, function( id, text ) {
272 terms.push( { id: text.id, text: text.title } );
273 });
274 }
275 return {
276 results: terms
277 };
278 },
279 cache: true
280 },
281 escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
282 minimumInputLength: 1,
283 allowClear: false,
284 });
285 </script>
286 <?php
287 $body = ob_get_clean();
288
289 ob_start();
290 ?>
291 <button class="wpsc-button small primary" onclick="wpsc_set_add_agent_leaves(this);">
292 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
293 </button>
294 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
295 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
296 </button>
297 <?php
298 $footer = ob_get_clean();
299
300 $response = array(
301 'title' => $title,
302 'body' => $body,
303 'footer' => $footer,
304 );
305
306 wp_send_json( $response, 200 );
307 }
308
309 /**
310 * Set add agent leaves
311 *
312 * @return void
313 */
314 public static function set_add_agent_leaves() {
315
316 if ( check_ajax_referer( 'wpsc_set_add_agent_leaves', '_ajax_nonce', false ) != 1 ) {
317 wp_send_json_error( 'Unauthorized request!', 401 );
318 }
319
320 global $wpdb;
321
322 if ( ! WPSC_Functions::is_site_admin() ) {
323 wp_send_json_error( 'Unauthorized', 400 );
324 }
325
326 $date_selected = isset( $_POST['dateSelected'] ) ? sanitize_text_field( wp_unslash( $_POST['dateSelected'] ) ) : '';
327 $date_selected = $date_selected ? array_filter( array_map( 'sanitize_text_field', explode( ',', $date_selected ) ) ) : array();
328 if ( ! $date_selected ) {
329 wp_send_json_error( 'Bad Request', 401 );
330 }
331
332 $agents = isset( $_POST['agents'] ) ? array_filter( array_map( 'intval', wp_unslash( $_POST['agents'] ) ) ) : array();
333 if ( ! $agents ) {
334 wp_send_json_error( 'Bad Request', 401 );
335 }
336
337 $is_recurring = isset( $_POST['is-recurring'] ) ? intval( $_POST['is-recurring'] ) : '';
338 if ( ! is_numeric( $is_recurring ) ) {
339 wp_send_json_error( 'Bad Request', 401 );
340 }
341
342 foreach ( $date_selected as $date ) {
343
344 $date = new DateTime( $date . ' 00:00:00' );
345
346 // delete non-recurring record if exists.
347 $sql = "DELETE FROM {$wpdb->prefix}psmsc_holidays WHERE ";
348 $sql .= 'agent IN(' . implode( ',', $agents ) . ') AND ';
349 $sql .= 'is_recurring = 0 AND ';
350 $sql .= "holiday = '" . $date->format( 'Y-m-d H:i:s' ) . "'";
351 $wpdb->query( $sql );
352
353 // delete recurring record if exists.
354 $sql = "DELETE FROM {$wpdb->prefix}psmsc_holidays WHERE ";
355 $sql .= 'agent IN(' . implode( ',', $agents ) . ') AND ';
356 $sql .= 'is_recurring = 1 AND ';
357 $sql .= "DATE_FORMAT(holiday, '%m-%d') = '" . $date->format( 'm-d' ) . "'";
358 $wpdb->query( $sql );
359
360 // add record.
361 foreach ( $agents as $agent ) {
362 WPSC_Holiday::insert(
363 array(
364 'agent' => $agent,
365 'holiday' => $date->format( 'Y-m-d H:i:s' ),
366 'is_recurring' => $is_recurring,
367 )
368 );
369 }
370 }
371
372 wp_die();
373 }
374
375 /**
376 * Delete agent leave
377 *
378 * @return void
379 */
380 public static function delete_agent_leave() {
381
382 if ( check_ajax_referer( 'wpsc_delete_agent_leave', '_ajax_nonce', false ) != 1 ) {
383 wp_send_json_error( 'Unauthorized request!', 401 );
384 }
385
386 if ( ! WPSC_Functions::is_site_admin() ) {
387 wp_send_json_error( 'Unauthorized', 400 );
388 }
389
390 $holiday_id = isset( $_POST['holidayId'] ) ? intval( $_POST['holidayId'] ) : 0;
391 if ( ! $holiday_id ) {
392 wp_send_json_error( 'Bad Request', 401 );
393 }
394
395 $holiday = new WPSC_Holiday( $holiday_id );
396 if ( ! $holiday->id ) {
397 wp_send_json_error( 'Bad Request', 401 );
398 }
399
400 WPSC_Holiday::destroy( $holiday );
401 wp_die();
402 }
403 }
404 endif;
405
406 WPSC_Agent_Leaves::init();
407