Completed
Push — develop ( 0f0835...c93cf3 )
by David
03:27
created

WL_Metabox_Field_date::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 14
rs 9.4286
cc 3
eloc 8
nc 2
nop 1
1
<?php
2
3
4
class WL_Metabox_Field_date extends WL_Metabox_Field {
5
	
6
	/*
7
	 * Attribute to distinguish between date formats, inferred from the schema property export type
8
	 * 
9
	 * @access private
10
	 * @var string $date_format The date format.
11
	 * @since 3.2.0
12
	 */
13
	private $date_format;
14
	
15
	/*
16
	 * Boolean flag to decide if the calendar should include time or not
17
	 * 
18
	 * @access private
19
	 * @var boolean $timepicker A boolean flag.
20
	 * @since 3.2.0
21
	 */
22
	private $timepicker;
23
	
24
	public function __construct( $args ) {
25
		
26
		// Call parent constructor
27
		parent::__construct( $args );
28
		
29
		// Distinguish between date and datetime	
30
		if( isset( $this->raw_custom_field['export_type'] ) && 'xsd:datetime' === $this->raw_custom_field['export_type'] ) {
31
			$this->date_format = 'Y/m/d H:i';
32
			$this->timepicker  = true;
33
		} else {
34
			$this->date_format = 'Y/m/d';
35
			$this->timepicker  = false;
36
		}
37
	}
38
    
39
    public function html_input( $date ) {
40
        
41
		$picker_date = ( empty( $date ) ? '' :  esc_attr( date( $this->date_format, strtotime( $date ) ) ) );
42
        
43
        $html = <<<EOF
44
			
45
			<div class="wl-input-wrapper">
46
				<input type="text" class="$this->meta_name" value="$picker_date" style="width:88%" />
47
				<input type="hidden" class="$this->meta_name" name="wl_metaboxes[$this->meta_name][]" value="$date" />      
48
				<button class="button wl-remove-input" type="button" style="width:10%">Remove</button>
49
			</div>
50
EOF;
51
		
52
        return $html;
53
54
    }
55
    
56
    public function html_wrapper_close() {
57
		
58
		// Should the widget include time picker?
59
		$timepicker = json_encode( $this->timepicker );
60
          
61
        $html = <<<EOF
62
			<script type='text/javascript'>
63
				( function( $ ) {
64
65
					$( function() {
66
67
						$( '.$this->meta_name[type=text]' ).datetimepicker( {
68
							format: '$this->date_format',
69
							timepicker:$timepicker,
70
							onChangeDateTime:function(dp, input){
71
72
								// format must be: 'YYYY-MM-DDTHH:MM:SSZ' from '2014/11/21 04:00'
73
								var currentDate = input.val()
74
									.replace(/(\d{4})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2})/,'$1-$2-$3T$4:$5:00Z');
75
76
								// store value to save in the hidden input field
77
								$( '.$this->meta_name[type=hidden]' ).val( currentDate );
78
							}
79
						});
80
81
					} );
82
				} ) ( jQuery );
83
			</script>
84
EOF;
85
        
86
        $html .= parent::html_wrapper_close();
87
        
88
        return $html;
89
    }
90
}
91
92