| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 5 |
* |
| 6 |
* @package WPDataAccess\Simple_Form |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPDataAccess\Simple_Form { |
| 10 |
|
| 11 |
use WPDataAccess\WPDA; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class WPDA_Simple_Form_Item_DateTime |
| 15 |
* |
| 16 |
* Adds a date/time picker to fields of type: date, datetime, timestamp and time |
| 17 |
* |
| 18 |
* @author Peter Schulz |
| 19 |
* @since 2.5.1 |
| 20 |
*/ |
| 21 |
class WPDA_Simple_Form_Item_DateTime extends WPDA_Simple_Form_Item { |
| 22 |
|
| 23 |
protected $date_format = 'Y-m-d'; |
| 24 |
protected $time_format = 'H:i'; |
| 25 |
|
| 26 |
protected $date_placeholder = 'yyyy-mm-dd'; |
| 27 |
protected $time_placeholder = 'hh:mi'; |
| 28 |
|
| 29 |
protected $date_picker = 'true'; |
| 30 |
protected $time_picker = 'false'; |
| 31 |
|
| 32 |
/** |
| 33 |
* WPDA_Simple_Form_Item_DateTime constructor. |
| 34 |
* |
| 35 |
* @param array $args |
| 36 |
*/ |
| 37 |
public function __construct( $args = array() ) { |
| 38 |
parent::__construct( $args ); |
| 39 |
|
| 40 |
// Get date and time formats |
| 41 |
$this->date_format = WPDA::get_option( WPDA::OPTION_PLUGIN_DATE_FORMAT ); |
| 42 |
$this->time_format = WPDA::get_option( WPDA::OPTION_PLUGIN_TIME_FORMAT ); |
| 43 |
|
| 44 |
// Get date and time placeholders |
| 45 |
$this->date_placeholder = WPDA::get_option( WPDA::OPTION_PLUGIN_DATE_PLACEHOLDER ); |
| 46 |
$this->time_placeholder = WPDA::get_option( WPDA::OPTION_PLUGIN_TIME_PLACEHOLDER ); |
| 47 |
|
| 48 |
switch ( $this->column_type ) { |
| 49 |
case 'time': |
| 50 |
$this->date_format = $this->time_format; |
| 51 |
$this->date_picker = 'false'; |
| 52 |
$this->time_picker = 'true'; |
| 53 |
$this->item_placeholder = $this->time_placeholder; |
| 54 |
$db_format = WPDA::DB_TIME_FORMAT; |
| 55 |
break; |
| 56 |
case 'date': |
| 57 |
$this->item_placeholder = $this->date_placeholder; |
| 58 |
$db_format = WPDA::DB_DATE_FORMAT; |
| 59 |
break; |
| 60 |
default: |
| 61 |
$this->date_format = $this->date_format . ' ' . $this->time_format; |
| 62 |
$this->time_picker = 'true'; |
| 63 |
$this->item_placeholder = $this->date_placeholder . ' ' . $this->time_placeholder; |
| 64 |
$db_format = WPDA::DB_DATETIME_FORMAT; |
| 65 |
} |
| 66 |
|
| 67 |
if ( null !== $this->item_value && '' !== $this->item_value ) { |
| 68 |
$convert_date = \DateTime::createFromFormat( $db_format, $this->item_value ); |
| 69 |
$this->item_value = $convert_date->format( $this->date_format ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Overwrite method show_item: add date/time picker |
| 75 |
*/ |
| 76 |
public function show_item() { |
| 77 |
parent::show_item(); |
| 78 |
?> |
| 79 |
<script type='text/javascript'> |
| 80 |
jQuery(function () { |
| 81 |
jQuery.datetimepicker.setLocale('<?php echo esc_attr( substr( get_locale(), 0, 2 ) ); ?>'); |
| 82 |
jQuery('#<?php echo esc_attr( $this->item_name ); ?>').datetimepicker({ |
| 83 |
format: '<?php echo $this->date_format; // phpcs:ignore WordPress.Security.EscapeOutput ?>', |
| 84 |
datepicker: <?php echo $this->date_picker; // phpcs:ignore WordPress.Security.EscapeOutput ?>, |
| 85 |
timepicker: <?php echo $this->time_picker; // phpcs:ignore WordPress.Security.EscapeOutput ?>, |
| 86 |
scrollMonth: false, |
| 87 |
scrollInput: false |
| 88 |
}); |
| 89 |
jQuery('#<?php echo esc_attr( $this->item_name ); ?>').attr('autocomplete', 'off'); |
| 90 |
}); |
| 91 |
</script> |
| 92 |
<?php |
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
|