PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.1
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.1
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 / custom-field-types / class-wpsc-cf-single-select.php

class-wpsc-cf-single-select.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.4.1, at includes/custom-field-types/class-wpsc-cf-single-select.php

1,498 lines 46.9 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_CF_Single_Select' ) ) :
7
8 final class WPSC_CF_Single_Select {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'cf_single_select';
16
17 /**
18 * Set whether this custom field type is of type date
19 *
20 * @var boolean
21 */
22 public static $is_date = false;
23
24 /**
25 * Set whether this custom field type has applicable to date range
26 *
27 * @var boolean
28 */
29 public static $has_date_range = false;
30
31 /**
32 * Set whether this custom field type has multiple values
33 *
34 * @var boolean
35 */
36 public static $has_multiple_val = false;
37
38 /**
39 * Data type for column created in tickets table
40 *
41 * @var string
42 */
43 public static $data_type = 'TINYTEXT NULL DEFAULT NULL';
44
45 /**
46 * Set whether this custom field type has reference to other class
47 *
48 * @var boolean
49 */
50 public static $has_ref = true;
51
52 /**
53 * Reference class for this custom field type so that its value(s) return with object or array of objects automatically. Empty string indicate no reference.
54 *
55 * @var string
56 */
57 public static $ref_class = 'wpsc_option';
58
59 /**
60 * Set whether this custom field field type is system default (no fields can be created from it).
61 *
62 * @var boolean
63 */
64 public static $is_default = false;
65
66 /**
67 * Set whether this field type has extra information that can be used in ticket form, edit custom fields, etc.
68 *
69 * @var boolean
70 */
71 public static $has_extra_info = true;
72
73 /**
74 * Set whether this custom field type can accept personal info.
75 *
76 * @var boolean
77 */
78 public static $has_personal_info = true;
79
80 /**
81 * Set whether fields created from this custom field type is allowed in create ticket form
82 *
83 * @var boolean
84 */
85 public static $is_ctf = true;
86
87 /**
88 * Set whether fields created from this custom field type is allowed in ticket list
89 *
90 * @var boolean
91 */
92 public static $is_list = true;
93
94 /**
95 * Set whether fields created from this custom field type is allowed in ticket filter
96 *
97 * @var boolean
98 */
99 public static $is_filter = true;
100
101 /**
102 * Set whether fields created from this custom field type can be given character limits
103 *
104 * @var boolean
105 */
106 public static $has_char_limit = false;
107
108 /**
109 * Set whether fields created from this custom field type has custom options set in options table
110 *
111 * @var boolean
112 */
113 public static $has_options = true;
114
115 /**
116 * Set whether fields created from this custom field type can be available for ticket list sorting
117 *
118 * @var boolean
119 */
120 public static $is_sort = true;
121
122 /**
123 * Set whether fields created from this custom field type can be auto-filled
124 *
125 * @var boolean
126 */
127 public static $is_auto_fill = true;
128
129 /**
130 * Set whether fields created from this custom field type can have placeholder
131 *
132 * @var boolean
133 */
134 public static $is_placeholder = true;
135
136 /**
137 * Set whether fields created from this custom field type is applicable for visibility conditions in create ticket form
138 *
139 * @var boolean
140 */
141 public static $is_visibility_conditions = true;
142
143 /**
144 * Set whether fields created from this custom field type is applicable for macros
145 *
146 * @var boolean
147 */
148 public static $has_macro = true;
149
150 /**
151 * Set whether fields of this custom field type is applicalbe for search on ticket list page.
152 *
153 * @var boolean
154 */
155 public static $is_search = true;
156
157 /**
158 * Initialize the class
159 *
160 * @return void
161 */
162 public static function init() {
163
164 // Get object of this class.
165 add_filter( 'wpsc_load_ref_classes', array( __CLASS__, 'load_ref_class' ) );
166
167 // Set custom field type.
168 add_filter( 'wpsc_cf_types', array( __CLASS__, 'add_cf_type' ), 2 );
169
170 // ticket form.
171 add_action( 'wpsc_js_validate_ticket_form', array( __CLASS__, 'js_validate_ticket_form' ) );
172 add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 );
173 add_action( 'wpsc_js_clear_value_hidden_fields', array( __CLASS__, 'js_clear_value_hidden_fields' ) );
174
175 // create ticket data for rest api.
176 add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 10, 3 );
177
178 // Ticket model.
179 add_filter( 'wpsc_ticket_joins', array( __CLASS__, 'ticket_join' ), 9, 2 );
180
181 // ticket search query.
182 add_filter( 'wpsc_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 );
183 }
184
185 /**
186 * Load current class to reference classes
187 *
188 * @param array $classes - Associative array of class names indexed by its slug.
189 * @return array
190 */
191 public static function load_ref_class( $classes ) {
192
193 $classes[ self::$slug ] = array(
194 'class' => __CLASS__,
195 'save-key' => 'id',
196 );
197 return $classes;
198 }
199
200 /**
201 * Add custom field type to list
202 *
203 * @param array $cf_types - custom field types array.
204 * @return array
205 */
206 public static function add_cf_type( $cf_types ) {
207
208 $cf_types[ self::$slug ] = array(
209 'label' => esc_attr__( 'Dropdown (single-select)', 'supportcandy' ),
210 'class' => __CLASS__,
211 );
212 return $cf_types;
213 }
214
215 /**
216 * Print operators for ticket form filter
217 *
218 * @param WPSC_Custom_Field $cf - custom field object.
219 * @param array $filter - Existing filters (if any).
220 * @return void
221 */
222 public static function get_operators( $cf, $filter = array() ) {
223 ?>
224
225 <div class="item conditional">
226 <select class="operator" onchange="wpsc_tc_get_operand(this, '<?php echo esc_attr( $cf->slug ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_tc_get_operand' ) ); ?>');">
227 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
228 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option>
229 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option>
230 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option>
231 </select>
232 </div>
233 <?php
234 }
235
236 /**
237 * Print operators for ticket form filter
238 *
239 * @param string $operator - condition operator on which operands should be returned.
240 * @param WPSC_Custom_Field $cf - custom field object.
241 * @param array $filter - Exising functions (if any).
242 * @return void
243 */
244 public static function get_operands( $operator, $cf, $filter = array() ) {
245
246 $is_multiple = $operator !== '=' ? true : false;
247 $options = $cf->get_options();
248 $unique_id = uniqid( 'wpsc_' );
249 ?>
250 <div class="item conditional operand single">
251 <select class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" <?php echo $is_multiple ? 'multiple' : ''; ?>>
252 <?php
253 foreach ( $options as $option ) {
254 $selected = '';
255 if ( isset( $filter['operand_val_1'] ) && ( ( $is_multiple && in_array( $option->id, $filter['operand_val_1'] ) ) || ( ! $is_multiple && $option->id == $filter['operand_val_1'] ) ) ) {
256 $selected = 'selected="selected"';
257 }
258 ?>
259 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option>
260 <?php
261 }
262 ?>
263 </select>
264 </div>
265 <script>jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').selectWoo();</script>
266 <?php
267 }
268
269 /**
270 * Check condition for this type
271 *
272 * @param array $condition - condition data.
273 * @param WPSC_Custom_Field $cf - custom field object.
274 * @param mixed $option - value to compare.
275 * @return boolean
276 */
277 public static function is_valid( $condition, $cf, $option ) {
278
279 $response = false;
280 switch ( $condition['operator'] ) {
281
282 case '=':
283 $response = $condition['operand_val_1'] == $option ? true : false;
284 break;
285
286 case 'IN':
287 $response = in_array( $option, $condition['operand_val_1'] ) ? true : false;
288 break;
289
290 case 'NOT IN':
291 $response = ! in_array( $option, $condition['operand_val_1'] ) ? true : false;
292 break;
293 }
294 return $response;
295 }
296
297 /**
298 * Parse filter and return sql query to be merged in ticket model query builder
299 *
300 * @param WPSC_Custom_Field $cf - custom field of this type.
301 * @param mixed $compare - comparison operator.
302 * @param mixed $val - value to compare.
303 * @return string
304 */
305 public static function parse_filter( $cf, $compare, $val ) {
306
307 $str = '';
308
309 switch ( $compare ) {
310
311 case '=':
312 $str = self::get_sql_slug( $cf ) . '=\'' . esc_sql( $val ) . '\'';
313 break;
314
315 case 'IN':
316 $str = 'CONVERT(' . self::get_sql_slug( $cf ) . ' USING utf8) IN(\'' . implode( '\', \'', esc_sql( $val ) ) . '\')';
317 break;
318
319 case 'NOT IN':
320 $str = 'CONVERT(' . self::get_sql_slug( $cf ) . ' USING utf8) NOT IN(\'' . implode( '\', \'', esc_sql( $val ) ) . '\')';
321 break;
322
323 default:
324 $str = '1=1';
325 }
326
327 return $str;
328 }
329
330 /**
331 * Return orderby string
332 *
333 * @param WPSC_Custom_Field $cf - custom field object.
334 * @return string
335 */
336 public static function get_orderby_string( $cf ) {
337
338 return 'op.name';
339 }
340
341 /**
342 * Check ticket condition
343 *
344 * @param array $condition - array with condition data.
345 * @param WPSC_Custom_Field $cf - custom field object.
346 * @param WPSC_Ticket $ticket - ticket object.
347 * @return boolean
348 */
349 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
350
351 if ( ! is_object( $ticket->{$cf->slug} ) ) {
352 return false;
353 }
354
355 $flag = true;
356
357 switch ( $condition['operator'] ) {
358
359 case '=':
360 $flag = $ticket->{$cf->slug}->id == $condition['operand_val_1'] ? true : false;
361 break;
362
363 case 'IN':
364 $flag = in_array( $ticket->{$cf->slug}->id, $condition['operand_val_1'] );
365 break;
366
367 case 'NOT IN':
368 $flag = ! in_array( $ticket->{$cf->slug}->id, $condition['operand_val_1'] );
369 break;
370
371 default:
372 $flag = true;
373 }
374
375 return $flag;
376 }
377
378 /**
379 * Check customer condition
380 *
381 * @param array $condition - array with condition data.
382 * @param WPSC_Custom_Field $cf - custom field object.
383 * @param WPSC_Customer $customer - customer object.
384 * @return boolean
385 */
386 public static function is_valid_customer_condition( $condition, $cf, $customer ) {
387
388 if ( ! is_object( $customer->{$cf->slug} ) ) {
389 return false;
390 }
391
392 $flag = true;
393
394 switch ( $condition['operator'] ) {
395
396 case '=':
397 $flag = $customer->{$cf->slug}->id == $condition['operand_val_1'] ? true : false;
398 break;
399
400 case 'IN':
401 $flag = in_array( $customer->{$cf->slug}->id, $condition['operand_val_1'] );
402 break;
403
404 case 'NOT IN':
405 $flag = ! in_array( $customer->{$cf->slug}->id, $condition['operand_val_1'] );
406 break;
407
408 default:
409 $flag = true;
410 }
411
412 return $flag;
413 }
414
415 /**
416 * Print ticket form field
417 *
418 * @param WPSC_Custom_Field $cf - Custom field object.
419 * @param array $tff - Array of ticket form field settings for this field.
420 * @return string
421 */
422 public static function print_tff( $cf, $tff ) {
423
424 $options = $cf->get_options();
425 $val = '';
426 if ( $cf->field == 'ticket' ) {
427 $val = $cf->is_auto_fill && $cf->default_value ? stripslashes( htmlspecialchars( $cf->default_value[0] ) ) : '';
428 } else {
429 $current_user = WPSC_Current_User::$current_user;
430 $val = $current_user->is_customer && $current_user->customer->{$cf->slug} ? intval( $current_user->customer->{$cf->slug}->id ) : 0;
431 }
432 $unique_id = uniqid( 'wpsc_' );
433
434 ob_start();
435 ?>
436 <div class="<?php echo esc_attr( WPSC_Functions::get_tff_classes( $cf, $tff ) ); ?>" data-cft="<?php echo esc_attr( self::$slug ); ?>" data-slug="<?php echo esc_attr( $cf->slug ); ?>">
437 <div class="wpsc-tff-label">
438 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
439 <?php
440 if ( $tff['is-required'] ) {
441 ?>
442 <span class="required-indicator">*</span>
443 <?php
444 }
445 ?>
446 </div>
447 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
448 <select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>" onchange="wpsc_check_tff_visibility()">
449 <option value=""></option>
450 <?php
451 foreach ( $options as $option ) :
452 ?>
453 <option <?php selected( $option->id, $val ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option>
454 <?php
455 endforeach;
456 ?>
457 </select>
458 <script>
459 jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
460 allowClear: true,
461 placeholder: "<?php echo esc_attr( $cf->placeholder_text ); ?>"
462 });
463 </script>
464 </div>
465 <?php
466 return ob_get_clean();
467 }
468
469 /**
470 * Validate this type field in create ticket
471 *
472 * @return void
473 */
474 public static function js_validate_ticket_form() {
475 ?>
476
477 case '<?php echo esc_attr( self::$slug ); ?>':
478 var val = customField.find('select').first().val();
479 if (customField.hasClass('required') && !val) {
480 isValid = false;
481 alert(supportcandy.translations.req_fields_missing);
482 }
483 break;
484 <?php
485 echo PHP_EOL;
486 }
487
488 /**
489 * Return custom field value in $_POST
490 * Ignore phpcs nonce issue as we already checked where it is called from.
491 *
492 * @param string $slug - Custom field slug.
493 * @param mixed $cf - Custom field object or false.
494 * @return mixed
495 */
496 public static function get_tff_value( $slug, $cf = false ) {
497
498 return isset( $_POST[ $slug ] ) ? intval( $_POST[ $slug ] ) : 0; // phpcs:ignore
499 }
500
501 /**
502 * Return default value for custom field of this type
503 *
504 * @param WPSC_Custom_Field $cf - custom field object.
505 * @return mixed
506 */
507 public static function get_default_value( $cf ) {
508
509 return $cf->default_value ? $cf->default_value[0] : '';
510 }
511
512 /**
513 * Check and return custom field value for new ticket to be created.
514 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
515 * Ignore phpcs nonce issue as we already checked where it is called from.
516 *
517 * @param array $data - Array of values to to stored in ticket in an insert function.
518 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
519 * @param boolean $is_my_profile - Whether it or not it is created from my-profile. This function is used by create ticket as well as my-profile. Due to customer fields handling is done same way, this flag gives apportunity to identify where it being called.
520 * @return array
521 */
522 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
523
524 if ( isset( $custom_fields[ self::$slug ] ) ) {
525 foreach ( $custom_fields[ self::$slug ] as $cf ) {
526 $value = self::get_tff_value( $cf->slug );
527 if ( $cf->field == 'ticket' ) {
528
529 $default_val = self::get_default_value( $cf );
530 $data[ $cf->slug ] = $value ? $value : $default_val;
531
532 } elseif ( $cf->field == 'agentonly' ) {
533
534 $data[ $cf->slug ] = self::get_default_value( $cf );
535
536 } elseif ( $cf->field == 'customer' && $data['customer'] != 0 ) {
537
538 $tff = get_option( 'wpsc-tff' );
539 if ( ! $is_my_profile && ! isset( $tff[ $cf->slug ] ) ) {
540 continue;
541 }
542 $customer = new WPSC_Customer( $data['customer'] );
543 $existing_val = $customer->{$cf->slug} ? $customer->{$cf->slug}->id : '';
544 $value = $value === 0 ? '' : $value;
545 if ( $existing_val != $value ) {
546 $customer->{$cf->slug} = $value;
547 $customer->save();
548
549 // Set log for this change.
550 WPSC_Log::insert(
551 array(
552 'type' => 'customer',
553 'ref_id' => $customer->id,
554 'modified_by' => WPSC_Current_User::$current_user->customer->id,
555 'body' => wp_json_encode(
556 array(
557 'slug' => $cf->slug,
558 'prev' => $existing_val,
559 'new' => $value,
560 )
561 ),
562 'date_created' => ( new DateTime() )->format( 'Y-m-d H:i:s' ),
563 )
564 );
565 }
566 }
567 }
568 }
569 return $data;
570 }
571
572 /**
573 * Set create ticket data for rest api request
574 *
575 * @param array $data - create ticket data array.
576 * @param WP_REST_Request $request - rest request object.
577 * @param array $custom_fields - custom field objects indexed by unique custom field types.
578 * @return array
579 */
580 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
581
582 $current_user = WPSC_Current_User::$current_user;
583 $tff = get_option( 'wpsc-tff' );
584
585 if ( isset( $custom_fields[ self::$slug ] ) ) {
586 foreach ( $custom_fields[ self::$slug ] as $cf ) {
587
588 if (
589 ! in_array( $cf->field, array( 'ticket', 'agentonly', 'customer' ) ) ||
590 ( $cf->field == 'customer' && ! isset( $tff[ $cf->slug ] ) )
591 ) {
592 continue;
593 }
594
595 $option = new WPSC_Option( intval( $request->get_param( $cf->slug ) ) );
596
597 if ( in_array( $cf->field, array( 'ticket', 'agentonly' ) ) ) {
598
599 $data[ $cf->slug ] = $cf->field == 'ticket' && $option->id ? $option->id : self::get_default_value( $cf );
600
601 } else {
602
603 $customer = new WPSC_Customer( $data['customer'] );
604 $existing_val = $customer->{$cf->slug};
605
606 if ( $option->id && $option->id != $existing_val->id ) {
607
608 $customer->{$cf->slug} = $option;
609 $customer->save();
610
611 // Set log for this change..
612 WPSC_Log::insert(
613 array(
614 'type' => 'customer',
615 'ref_id' => $customer->id,
616 'modified_by' => $current_user->customer->id,
617 'body' => wp_json_encode(
618 array(
619 'slug' => $cf->slug,
620 'prev' => $existing_val->id,
621 'new' => $option->id,
622 )
623 ),
624 'date_created' => ( new DateTime() )->format( 'Y-m-d H:i:s' ),
625 )
626 );
627 }
628 }
629 }
630 }
631
632 return $data;
633 }
634
635 /**
636 * Return slug string to be used in where condition of ticket model for this type of field
637 *
638 * @param WPSC_Custom_Field $cf - custom field object.
639 * @return string
640 */
641 public static function get_sql_slug( $cf ) {
642
643 $join_char = in_array( $cf->field, array( 'ticket', 'agentonly' ) ) ? 't.' : 'c.';
644 return $join_char . $cf->slug;
645 }
646
647 /**
648 * Return val field for meta query of this type of custom field
649 *
650 * @param array $condition - condition data.
651 * @return mixed
652 */
653 public static function get_meta_value( $condition ) {
654
655 $operator = $condition['operator'];
656 switch ( $operator ) {
657
658 case '=':
659 case 'IN':
660 case 'NOT IN':
661 return $condition['operand_val_1'];
662 }
663 return false;
664 }
665
666 /**
667 * Print generic input field for this type.
668 *
669 * @param WPSC_Custom_Field $cf - Custom field object.
670 * @param mixed $value - optional. Input field will be printed with given value if given.
671 * @return void
672 */
673 public static function print_cf_input( $cf, $value = '' ) {
674
675 if ( $value && ! is_object( $value ) ) {
676
677 $option = new WPSC_Option( $value );
678 if ( $option && $option->id ) {
679 $value = $option;
680 }
681 }
682
683 $options = $cf->get_options();
684 $unique_id = uniqid( 'wpsc_' );
685 ?>
686 <div class="wpsc-tff wpsc-sm-12 wpsc-md-12 wpsc-lg-12 wpsc-visible wpsc-xs-12" data-cft="<?php echo esc_attr( self::$slug ); ?>">
687 <div class="wpsc-tff-label">
688 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
689 </div>
690 <?php
691 $extra_info = stripslashes( $cf->extra_info );
692 if ( $extra_info ) :
693 ?>
694 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
695 <?php
696 endif
697 ?>
698 <select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>">
699 <option value=""></option>
700 <?php
701 foreach ( $options as $option ) {
702 ?>
703 <option <?php $value && selected( $option->id, $value->id ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option>
704 <?php
705 }
706 ?>
707 </select>
708 <script>
709 jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
710 allowClear: true,
711 placeholder: ""
712 });
713 </script>
714 </div>
715 <?php
716 }
717
718 /**
719 * Get value for generic field from post.
720 * Ignore phpcs nonce issue as we already checked where it is called from.
721 *
722 * @param WPSC_Custom_Field $cf - custom field object.
723 * @return string
724 */
725 public static function get_cf_input_val( $cf ) {
726
727 return isset( $_POST[ $cf->slug ] ) ? intval( $_POST[ $cf->slug ] ) : ''; // phpcs:ignore
728 }
729
730 /**
731 * Print edit ticket custom field in individual ticket
732 *
733 * @param WPSC_Custom_Field $cf - custom field object.
734 * @param WPSC_Ticket $ticket - ticket object.
735 * @return void
736 */
737 public static function print_edit_ticket_cf( $cf, $ticket ) {
738
739 $options = $cf->get_options();
740 $unique_id = uniqid( 'wpsc_' );
741 ?>
742 <div class="wpsc-tff wpsc-sm-12 wpsc-md-12 wpsc-lg-12 wpsc-visible wpsc-xs-12" data-cft="<?php echo esc_attr( self::$slug ); ?>">
743 <div class="wpsc-tff-label">
744 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
745 </div>
746 <?php
747 $extra_info = stripslashes( $cf->extra_info );
748 if ( $extra_info ) {
749 ?>
750 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
751 <?php
752 }
753 ?>
754 <select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>">
755 <option value=""></option>
756 <?php
757 foreach ( $options as $option ) {
758 ?>
759 <option <?php $ticket->{$cf->slug} && selected( $option->id, $ticket->{$cf->slug}->id ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option>
760 <?php
761 }
762 ?>
763 </select>
764 <script>
765 jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
766 allowClear: true,
767 placeholder: "<?php echo esc_attr( $cf->placeholder_text ); ?>"
768 });
769 </script>
770 </div>
771 <?php
772 }
773
774 /**
775 * Set edit individual ticket for this custom field type.
776 * Ignore phpcs nonce issue as we already checked where it is called from.
777 *
778 * @param WPSC_Custom_Field $cf - custom field object.
779 * @param WPSC_Ticket $ticket - ticket object.
780 * @return WPSC_Ticket
781 */
782 public static function set_edit_ticket_cf( $cf, $ticket ) {
783
784 $prev = $ticket->{$cf->slug} ? $ticket->{$cf->slug}->id : '';
785 $new = isset( $_POST[ $cf->slug ] ) ? intval( $_POST[ $cf->slug ] ) : ''; // phpcs:ignore
786 $new = $new === 0 ? '' : $new;
787
788 // Exit if there is no change.
789 if ( $prev == $new ) {
790 return $ticket;
791 }
792
793 // Change value.
794 $ticket->{$cf->slug} = $new;
795 $ticket->save();
796
797 return $ticket;
798 }
799
800 /**
801 * Modify ticket field value of this custom field type using rest api
802 *
803 * @param WPSC_Ticket $ticket - ticket object.
804 * @param WPSC_Custom_Field $cf - custom field.
805 * @param mixed $value - value to be set.
806 * @return void
807 */
808 public static function set_rest_edit_ticket_cf( $ticket, $cf, $value ) {
809
810 $id = WPSC_Functions::sanitize_option( intval( $value ), $cf, $cf->get_options() );
811 $prev = is_object( $ticket->{$cf->slug} ) ? $ticket->{$cf->slug}->id : 0;
812 if ( $prev != $id ) {
813 $ticket->{$cf->slug} = $id ? $id : '';
814 }
815 }
816
817 /**
818 * Insert log thread for this custom field type change
819 *
820 * @param WPSC_Custom_Field $cf - current custom field of this type.
821 * @param WPSC_Ticket $prev - ticket object before making any changes.
822 * @param WPSC_Ticket $new - ticket object after making changes.
823 * @param string $current_date - date string to be stored as create time.
824 * @param int $customer_id - current user customer id for blame.
825 * @return void
826 */
827 public static function insert_ticket_log( $cf, $prev, $new, $current_date, $customer_id ) {
828
829 // Exit if there is no change.
830 if ( $prev->{$cf->slug} == $new->{$cf->slug} ) {
831 return;
832 }
833
834 $prev_val = $prev->{$cf->slug} ? $prev->{$cf->slug}->id : '';
835 $new_val = $new->{$cf->slug} ? $new->{$cf->slug}->id : '';
836
837 $thread = WPSC_Thread::insert(
838 array(
839 'ticket' => $prev->id,
840 'customer' => $customer_id,
841 'type' => 'log',
842 'body' => wp_json_encode(
843 array(
844 'slug' => $cf->slug,
845 'prev' => $prev_val,
846 'new' => $new_val,
847 )
848 ),
849 'date_created' => $current_date,
850 'date_updated' => $current_date,
851 )
852 );
853 }
854
855 /**
856 * Return data for this custom field while creating duplicate ticket
857 *
858 * @param WPSC_Custom_Field $cf - custom field object.
859 * @param WPSC_Ticket $ticket - ticket object.
860 * @return mixed
861 */
862 public static function get_duplicate_ticket_data( $cf, $ticket ) {
863
864 return $ticket->{$cf->slug} ? $ticket->{$cf->slug}->id : '';
865 }
866
867 /**
868 * Print edit field for this type in edit customer info
869 *
870 * @param WPSC_Custom_field $cf - custom field object.
871 * @param WPSC_Customer $customer - customer object.
872 * @param array $tff - ticket form field data.
873 * @return string
874 */
875 public static function print_edit_customer_info( $cf, $customer, $tff ) {
876
877 $options = $cf->get_options();
878 $val = $customer->id && $customer->{$cf->slug} ? intval( $customer->{$cf->slug}->id ) : 0;
879 $unique_id = uniqid( 'wpsc_' );
880
881 ob_start();
882 ?>
883 <div class="<?php echo esc_attr( WPSC_Functions::get_tff_classes( $cf, $tff ) ); ?>" data-cft="<?php echo esc_attr( self::$slug ); ?>">
884 <div class="wpsc-tff-label">
885 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
886 </div>
887 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
888 <select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>">
889 <option value=""></option>
890 <?php
891 foreach ( $options as $option ) :
892 ?>
893 <option <?php selected( $option->id, $val ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option>
894 <?php
895 endforeach;
896 ?>
897 </select>
898 <script>
899 jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
900 allowClear: false,
901 placeholder: "<?php echo esc_attr( $cf->placeholder_text ); ?>"
902 });
903 </script>
904 </div>
905 <?php
906 return ob_get_clean();
907 }
908
909 /**
910 * Print add new custom field setting properties
911 *
912 * @param string $field_class - Class name of the field.
913 * @return void
914 */
915 public static function get_add_new_custom_field_properties( $field_class ) {
916
917 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) :
918 ?>
919 <div data-type="textfield" data-required="false" class="wpsc-input-group extra-info">
920 <div class="label-container">
921 <label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label>
922 </div>
923 <input name="extra_info" type="text" autocomplete="off" />
924 </div>
925 <?php
926 endif;
927 ?>
928
929 <div data-type="select-options" data-required="true" class="wpsc-input-group options">
930 <div class="label-container">
931 <label for="">
932 <?php esc_attr_e( 'Field options', 'supportcandy' ); ?>
933 <span class="required-char">*</span>
934 </label>
935 </div>
936 <div class="wpsc-options-container ui-sortable"></div>
937 <script>jQuery(".wpsc-options-container").sortable({ handle: '.wpsc-sort-handle' });</script>
938 <button class="wpsc-button small secondary" onclick="wpsc_get_add_new_option();">
939 <?php esc_attr_e( 'Add new', 'supportcandy' ); ?>
940 </button>
941 </div>
942 <?php
943
944 if ( in_array( 'default_value', $field_class::$allowed_properties ) ) :
945 ?>
946 <div data-type="single-select" data-required="false" class="wpsc-input-group default-val-option-single">
947 <div class="label-container">
948 <label for="">
949 <?php esc_attr_e( 'Default value', 'supportcandy' ); ?>
950 </label>
951 </div>
952 <select name="default-val-option-single[]" id="wpsc-default-val-option-single">
953 <option value=""></option>
954 </select>
955 </div>
956 <?php
957 endif;
958
959 if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) :
960 ?>
961 <div data-type="single-select" data-required="false" class="wpsc-input-group is_auto_fill">
962 <div class="label-container">
963 <label for="">
964 <?php esc_attr_e( 'Auto-fill in ticket form', 'supportcandy' ); ?>
965 </label>
966 </div>
967 <select name="is_auto_fill">
968 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
969 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
970 </select>
971 </div>
972 <?php
973 endif;
974
975 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) :
976 ?>
977 <div data-type="textfield" data-required="false" class="wpsc-input-group placeholder_text">
978 <div class="label-container">
979 <label for="">
980 <?php esc_attr_e( 'Placeholder', 'supportcandy' ); ?>
981 </label>
982 </div>
983 <input type="text" name="placeholder_text" autocomplete="off">
984 </div>
985 <?php
986 endif;
987
988 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
989 ?>
990 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
991 <div class="label-container">
992 <label for="">
993 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
994 </label>
995 </div>
996 <input type="number" name="tl_width" autocomplete="off">
997 </div>
998 <?php
999 endif;
1000
1001 if ( in_array( 'is_personal_info', $field_class::$allowed_properties ) ) :
1002 ?>
1003 <div data-type="single-select" data-required="false" class="wpsc-input-group is_personal_info">
1004 <div class="label-container">
1005 <label for="">
1006 <?php esc_attr_e( 'Has personal info', 'supportcandy' ); ?>
1007 </label>
1008 </div>
1009 <select name="is_personal_info">
1010 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1011 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1012 </select>
1013 </div>
1014 <?php
1015 endif;
1016
1017 if ( in_array( 'allow_my_profile', $field_class::$allowed_properties ) ) :
1018 ?>
1019 <div data-type="single-select" data-required="false" class="wpsc-input-group allow_my_profile">
1020 <div class="label-container">
1021 <label for="">
1022 <?php esc_attr_e( 'Allow in my profile?', 'supportcandy' ); ?>
1023 </label>
1024 </div>
1025 <select name="allow_my_profile">
1026 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1027 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1028 </select>
1029 </div>
1030 <?php
1031 endif;
1032
1033 if ( in_array( 'allow_ticket_form', $field_class::$allowed_properties ) ) :
1034 ?>
1035 <div data-type="single-select" data-required="false" class="wpsc-input-group allow_ticket_form">
1036 <div class="label-container">
1037 <label for="">
1038 <?php esc_attr_e( 'Allow in ticket form?', 'supportcandy' ); ?>
1039 </label>
1040 </div>
1041 <select name="allow_ticket_form">
1042 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1043 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1044 </select>
1045 </div>
1046 <?php
1047 endif;
1048 }
1049
1050 /**
1051 * Print edit custom field properties
1052 *
1053 * @param WPSC_Custom_Fields $cf - custom field object.
1054 * @param string $field_class - class name of field category.
1055 * @return void
1056 */
1057 public static function get_edit_custom_field_properties( $cf, $field_class ) {
1058
1059 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) :
1060 ?>
1061 <div data-type="textfield" data-required="false" class="wpsc-input-group extra-info">
1062 <div class="label-container">
1063 <label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label>
1064 </div>
1065 <input name="extra_info" type="text" value="<?php echo esc_attr( $cf->extra_info ); ?>" autocomplete="off" />
1066 </div>
1067 <?php
1068 endif;
1069 ?>
1070
1071 <div data-type="select-options" data-required="true" class="wpsc-input-group options">
1072 <div class="label-container">
1073 <label for="">
1074 <?php esc_attr_e( 'Field options', 'supportcandy' ); ?>
1075 <span class="required-char">*</span>
1076 </label>
1077 </div>
1078 <div class="wpsc-options-container ui-sortable">
1079 <?php
1080 $options = $cf->get_options();
1081 foreach ( $options as $option ) :
1082 ?>
1083 <div class="wpsc-option-item">
1084 <div class="content">
1085 <div class="wpsc-edit-option-container" style="display: none;">
1086 <input class="edit-option-text" type="text" value="" autocomplete="off" />
1087 <button onclick="wpsc_set_edit_option(this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_option' ) ); ?>');">
1088 <?php WPSC_Icons::get( 'check' ); ?>
1089 </button>
1090 <button class="cancel" onclick="wpsc_edit_option_cancel(this);">
1091 <?php WPSC_Icons::get( 'times' ); ?>
1092 </button>
1093 </div>
1094 <div class="wpsc-option-listing-container">
1095 <span class="sort wpsc-sort-handle"><?php WPSC_Icons::get( 'sort' ); ?></span>
1096 <div class="text"><?php echo esc_attr( $option->name ); ?></div>
1097 <span class="edit" onclick="wpsc_edit_option(this);"><?php WPSC_Icons::get( 'edit' ); ?></span>
1098 </div>
1099 </div>
1100 <div class="remove-container">
1101 <span onclick="wpsc_remove_option_item(this)"><?php WPSC_Icons::get( 'times-circle' ); ?></span>
1102 </div>
1103 <input type="hidden" class="option_id" name="options[]" value="<?php echo esc_attr( $option->id ); ?>">
1104 </div>
1105 <?php
1106 endforeach;
1107 ?>
1108 </div>
1109 <script>jQuery(".wpsc-options-container").sortable({ handle: '.wpsc-sort-handle' });</script>
1110 <button class="wpsc-button small secondary" onclick="wpsc_get_add_new_option();">
1111 <?php esc_attr_e( 'Add new', 'supportcandy' ); ?>
1112 </button>
1113 </div>
1114 <?php
1115
1116 if ( in_array( 'default_value', $field_class::$allowed_properties ) ) :
1117 $default_val = $cf->default_value ? $cf->default_value[0] : '';
1118 ?>
1119 <div data-type="single-select" data-required="false" class="wpsc-input-group default_value">
1120 <div class="label-container">
1121 <label for="">
1122 <?php esc_attr_e( 'Default value', 'supportcandy' ); ?>
1123 </label>
1124 </div>
1125 <select name="default-val-option-single[]" id="wpsc-default-val-option-single">
1126 <option value=""></option>
1127 <?php
1128 $options = $cf->get_options();
1129 foreach ( $options as $option ) :
1130 echo '<option ' . selected( $default_val, $option->id ) . ' value=' . esc_attr( $option->id ) . '>' . esc_attr( $option->name ) . '</option>';
1131 endforeach;
1132 ?>
1133 </select>
1134 </div>
1135 <?php
1136 endif;
1137
1138 if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) :
1139 ?>
1140 <div data-type="single-select" data-required="false" class="wpsc-input-group is_auto_fill">
1141 <div class="label-container">
1142 <label for="">
1143 <?php esc_attr_e( 'Auto-fill in ticket form', 'supportcandy' ); ?>
1144 </label>
1145 </div>
1146 <select name="is_auto_fill">
1147 <option <?php selected( $cf->is_auto_fill, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1148 <option <?php selected( $cf->is_auto_fill, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1149 </select>
1150 </div>
1151 <?php
1152 endif;
1153
1154 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) :
1155 ?>
1156 <div data-type="textfield" data-required="false" class="wpsc-input-group placeholder_text">
1157 <div class="label-container">
1158 <label for="">
1159 <?php esc_attr_e( 'Placeholder', 'supportcandy' ); ?>
1160 </label>
1161 </div>
1162 <input type="text" name="placeholder_text" value="<?php echo esc_attr( $cf->placeholder_text ); ?>" autocomplete="off">
1163 </div>
1164 <?php
1165 endif;
1166
1167 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
1168 ?>
1169 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
1170 <div class="label-container">
1171 <label for="">
1172 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
1173 </label>
1174 </div>
1175 <input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off">
1176 </div>
1177 <?php
1178 endif;
1179
1180 if ( in_array( 'is_personal_info', $field_class::$allowed_properties ) ) :
1181 ?>
1182 <div data-type="single-select" data-required="false" class="wpsc-input-group is_personal_info">
1183 <div class="label-container">
1184 <label for="">
1185 <?php esc_attr_e( 'Has personal info', 'supportcandy' ); ?>
1186 </label>
1187 </div>
1188 <select name="is_personal_info">
1189 <option <?php selected( $cf->is_personal_info, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1190 <option <?php selected( $cf->is_personal_info, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1191 </select>
1192 </div>
1193 <?php
1194 endif;
1195
1196 if ( in_array( 'allow_my_profile', $field_class::$allowed_properties ) ) :
1197 ?>
1198 <div data-type="single-select" data-required="false" class="wpsc-input-group allow_my_profile">
1199 <div class="label-container">
1200 <label for="">
1201 <?php esc_attr_e( 'Allow in my profile?', 'supportcandy' ); ?>
1202 </label>
1203 </div>
1204 <select name="allow_my_profile">
1205 <option <?php selected( $cf->allow_my_profile, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1206 <option <?php selected( $cf->allow_my_profile, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1207 </select>
1208 </div>
1209 <?php
1210 endif;
1211
1212 if ( in_array( 'allow_ticket_form', $field_class::$allowed_properties ) ) :
1213 ?>
1214 <div data-type="single-select" data-required="false" class="wpsc-input-group allow_ticket_form">
1215 <div class="label-container">
1216 <label for="">
1217 <?php esc_attr_e( 'Allow in ticket form?', 'supportcandy' ); ?>
1218 </label>
1219 </div>
1220 <select name="allow_ticket_form">
1221 <option <?php selected( $cf->allow_ticket_form, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
1222 <option <?php selected( $cf->allow_ticket_form, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
1223 </select>
1224 </div>
1225 <?php
1226 endif;
1227 }
1228
1229 /**
1230 * Set custom field properties. Can be used by add/edit custom field.
1231 * Ignore phpcs nonce issue as we already checked where it is called from.
1232 *
1233 * @param WPSC_Custom_Field $cf - custom field object.
1234 * @param string $field_class - class of field category.
1235 * @return void
1236 */
1237 public static function set_cf_properties( $cf, $field_class ) {
1238
1239 // extra info.
1240 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) {
1241 $cf->extra_info = isset( $_POST['extra_info'] ) ? sanitize_text_field( wp_unslash( $_POST['extra_info'] ) ) : ''; // phpcs:ignore
1242 }
1243
1244 // options.
1245 $options = isset( $_POST['options'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash($_POST['options']) ) ) : array(); // phpcs:ignore
1246 if ( ! $options ) {
1247 wp_send_json_error( new WP_Error( '001', 'Bad request!' ), 400 );
1248 }
1249
1250 // default value.
1251 if ( in_array( 'default_value', $field_class::$allowed_properties ) ) {
1252 $cf->default_value = isset( $_POST['default-val-option-single'] ) ? array_filter( array_map( 'intval', $_POST['default-val-option-single'] ) ) : array(); // phpcs:ignore
1253 }
1254
1255 // auto fill.
1256 if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) {
1257 $cf->is_auto_fill = isset( $_POST['is_auto_fill'] ) ? sanitize_text_field( wp_unslash( $_POST['is_auto_fill'] ) ) : ''; // phpcs:ignore
1258 }
1259
1260 // placeholder!
1261 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) {
1262 $cf->placeholder_text = isset( $_POST['placeholder_text'] ) ? sanitize_text_field( wp_unslash( $_POST['placeholder_text'] ) ) : ''; // phpcs:ignore
1263 }
1264
1265 // personal info.
1266 if ( in_array( 'is_personal_info', $field_class::$allowed_properties ) ) {
1267 $cf->is_personal_info = isset( $_POST['is_personal_info'] ) ? intval( $_POST['is_personal_info'] ) : 0; // phpcs:ignore
1268 }
1269
1270 // my-profile.
1271 if ( in_array( 'allow_my_profile', $field_class::$allowed_properties ) ) {
1272 $cf->allow_my_profile = isset( $_POST['allow_my_profile'] ) ? intval( $_POST['allow_my_profile'] ) : 0; // phpcs:ignore
1273 }
1274
1275 // ticket form.
1276 if ( in_array( 'allow_ticket_form', $field_class::$allowed_properties ) ) {
1277 $cf->allow_ticket_form = isset( $_POST['allow_ticket_form'] ) ? intval( $_POST['allow_ticket_form'] ) : 0; // phpcs:ignore
1278 }
1279
1280 // tl_width!
1281 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
1282 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
1283 $cf->tl_width = $tl_width ? $tl_width : 100;
1284 }
1285
1286 // save!
1287 $cf->save();
1288 $cf->set_options( $options, true );
1289 }
1290
1291 /**
1292 * Add SQL joins to ticket model for this field type
1293 *
1294 * @param array $joins - array of join string that can be imploded later.
1295 * @param array $filter - user filter.
1296 * @return array
1297 */
1298 public static function ticket_join( $joins, $filter ) {
1299
1300 global $wpdb;
1301
1302 $cf = WPSC_Custom_Field::get_cf_by_slug( $filter['orderby_slug'] );
1303
1304 if ( $cf && $cf->type::$slug == 'cf_single_select' ) {
1305 $join_str = self::get_sql_slug( $cf );
1306 $joins[] = 'LEFT JOIN ' . $wpdb->prefix . 'psmsc_options op ON ' . $join_str . ' = op.id';
1307 }
1308
1309 return $joins;
1310 }
1311
1312 /**
1313 * Add ticket search compatibility for fields of this custom field type.
1314 *
1315 * @param array $sql - Array of sql peices that can be joined later.
1316 * @param array $filter - User filter.
1317 * @param array $custom_fields - Custom fields array applicable for search.
1318 * @param string $search - search string.
1319 * @param array $allowed_search_fields - Allowed search fields.
1320 * @return array
1321 */
1322 public static function ticket_search( $sql, $filter, $custom_fields, $search, $allowed_search_fields ) {
1323
1324 if ( isset( $custom_fields[ self::$slug ] ) ) {
1325
1326 $search_items = false;
1327 foreach ( $custom_fields[ self::$slug ] as $cf ) {
1328 if ( in_array( $cf->slug, $allowed_search_fields ) ) {
1329 if ( ! $search_items ) {
1330 $search_items = WPSC_Option::get_tl_search_string( $search );
1331 if ( ! $search_items ) {
1332 break;
1333 }
1334 }
1335 $join_char = in_array( $cf->field, array( 'ticket', 'agentonly' ) ) ? 't.' : 'c.';
1336 $sql[] = $join_char . $cf->slug . ' IN(' . implode( ', ', $search_items ) . ')';
1337 }
1338 }
1339 }
1340
1341 return $sql;
1342 }
1343
1344 /**
1345 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
1346 *
1347 * @param WPSC_Custom_Field $cf - custom field object.
1348 * @param WPSC_Ticket $ticket - ticket object.
1349 * @param string $module - module name.
1350 * @return string
1351 */
1352 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
1353
1354 $option = $ticket->{$cf->slug};
1355 $value = is_object( $option ) && $option->id ? $option->name : '';
1356 return apply_filters( 'wpsc_ticket_field_val_single_select', $value, $cf, $ticket, $module );
1357 }
1358
1359 /**
1360 * Print ticket value for given custom field on ticket list
1361 *
1362 * @param WPSC_Custom_Field $cf - custom field object.
1363 * @param WPSC_Ticket $ticket - ticket object.
1364 * @return void
1365 */
1366 public static function print_tl_ticket_field_val( $cf, $ticket ) {
1367
1368 echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) );
1369 }
1370
1371 /**
1372 * Print ticket value for given custom field on widget
1373 *
1374 * @param WPSC_Custom_Field $cf - custom field object.
1375 * @param WPSC_Ticket $ticket - ticket object.
1376 * @return void
1377 */
1378 public static function print_widget_ticket_field_val( $cf, $ticket ) {
1379
1380 echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) );
1381 }
1382
1383 /**
1384 * Returns printable customer value for custom field. Can be used in export tickets, replace macros etc.
1385 *
1386 * @param WPSC_Custom_Field $cf - custom field object.
1387 * @param WPSC_Customer $customer - customer object.
1388 * @return string
1389 */
1390 public static function get_customer_field_val( $cf, $customer ) {
1391
1392 $option = $customer->{$cf->slug};
1393 return is_object( $option ) && $option->id ? $option->name : '';
1394 }
1395
1396 /**
1397 * Print customer value for given custom field on ticket list
1398 *
1399 * @param WPSC_Custom_Field $cf - custom field object.
1400 * @param WPSC_Customer $customer - customer object.
1401 * @return void
1402 */
1403 public static function print_tl_customer_field_val( $cf, $customer ) {
1404
1405 echo esc_attr( self::get_customer_field_val( $cf, $customer ) );
1406 }
1407
1408 /**
1409 * Print customer value for given custom field on widget
1410 *
1411 * @param WPSC_Custom_Field $cf - custom field object.
1412 * @param WPSC_Customer $customer - customer object.
1413 * @return void
1414 */
1415 public static function print_widget_customer_field_val( $cf, $customer ) {
1416
1417 echo esc_attr( self::get_customer_field_val( $cf, $customer ) );
1418 }
1419
1420 /**
1421 * Print given value for custom field
1422 *
1423 * @param WPSC_Custom_Field $cf - custom field object.
1424 * @param mixed $val - value to convert and print.
1425 * @return void
1426 */
1427 public static function print_val( $cf, $val ) {
1428
1429 $option = is_object( $val ) ? $val : new WPSC_Option( $val );
1430 echo $option->id ? esc_attr( $option->name ) : esc_attr__( 'None', 'supportcandy' );
1431 }
1432
1433 /**
1434 * Return printable value for history log macro
1435 *
1436 * @param WPSC_Custom_Field $cf - custom field object.
1437 * @param mixed $val - value to convert and return.
1438 * @return string
1439 */
1440 public static function get_history_log_val( $cf, $val ) {
1441
1442 ob_start();
1443 self::print_val( $cf, $val );
1444 return ob_get_clean();
1445 }
1446
1447 /**
1448 * Print dashboard activity function
1449 *
1450 * @param WPSC_Custom_Field $cf - custom field object.
1451 * @param array $recent_logs - recent_logs object.
1452 * @param array $body - body object.
1453 * @param int $view - check if frontend.
1454 * @return string
1455 */
1456 public static function print_activity( $cf, $recent_logs, $body, $view ) {
1457
1458 $url = WPSC_Functions::get_ticket_url( $recent_logs->ticket->id, $view );
1459 if ( $body->new ) {
1460 $ss_value = new WPSC_Option( $body->new );
1461 $value = $ss_value->name;
1462 } else {
1463 $value = 'None';
1464 }
1465 return esc_attr( $recent_logs->customer->name ) . ' updated the' . esc_attr( $cf->name ) . ' value of <a href="' . esc_attr( $url ) . '" target="_blank">#' . esc_attr( $recent_logs->ticket->id ) . '</a> to ' . esc_attr( $value );
1466 }
1467
1468 /**
1469 * Print given value for custom field
1470 *
1471 * @param WPSC_Custom_Field $cf - custom field object.
1472 * @param mixed $val - value to convert and print.
1473 * @return string
1474 */
1475 public static function get_field_value( $cf, $val ) {
1476
1477 $option = is_object( $val ) ? $val : new WPSC_Option( $val );
1478 return $option->id ? esc_attr( $option->name ) : esc_attr__( 'None', 'supportcandy' );
1479 }
1480
1481 /**
1482 * Clear value of hidden fields
1483 *
1484 * @return void
1485 */
1486 public static function js_clear_value_hidden_fields() {
1487 ?>
1488 case '<?php echo esc_attr( self::$slug ); ?>':
1489 customField.find('select').first().val('');
1490 break;
1491 <?php
1492 echo PHP_EOL;
1493 }
1494 }
1495 endif;
1496
1497 WPSC_CF_Single_Select::init();
1498