Completed
Pull Request — 2.x (#3555)
by
unknown
06:27
created

PodsField_Time::options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 69
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 56
nc 1
nop 0
dl 0
loc 69
rs 9.2083
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package Pods\Fields
4
 */
5
class PodsField_Time extends PodsField {
6
7
    /**
8
     * Field Type Group
9
     *
10
     * @var string
11
     * @since 2.0
12
     */
13
    public static $group = 'Date / Time';
14
15
    /**
16
     * Field Type Identifier
17
     *
18
     * @var string
19
     * @since 2.0
20
     */
21
    public static $type = 'time';
22
23
    /**
24
     * Field Type Label
25
     *
26
     * @var string
27
     * @since 2.0
28
     */
29
    public static $label = 'Time';
30
31
    /**
32
     * Field Type Preparation
33
     *
34
     * @var string
35
     * @since 2.0
36
     */
37
    public static $prepare = '%s';
38
39
    /**
40
     * Do things like register/enqueue scripts and stylesheets
41
     *
42
     * @since 2.0
43
     */
44
    public function __construct () {
45
46
    }
47
48
    /**
49
     * Add options and set defaults to
50
     *
51
     * @return array
52
     * @since 2.0
53
     */
54
    public function options () {
55
        $options = array(
56
            self::$type . '_repeatable' => array(
57
                'label' => __( 'Repeatable Field', 'pods' ),
58
                'default' => 0,
59
                'type' => 'boolean',
60
                'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ),
61
                'boolean_yes_label' => '',
62
                'dependency' => true,
63
                'developer_mode' => true
64
            ),
65
            self::$type . '_type' => array(
66
                'label' => __( 'Time Format Type', 'pods' ),
67
                'default' => '12',
68
                'type' => 'pick',
69
                'data' => array(
70
                    '12' => __( '12 hour', 'pods' ),
71
                    '24' => __( '24 hour', 'pods' )
72
                ),
73
                'dependency' => true
74
            ),
75
            self::$type . '_format' => array(
76
                'label' => __( 'Time Format', 'pods' ),
77
                'depends-on' => array( self::$type . '_type' => '12' ),
78
                'default' => 'h_mma',
79
                'type' => 'pick',
80
                'data' => array(
81
                    'h_mm_A' => date_i18n( 'g:i A' ),
82
                    'h_mm_ss_A' => date_i18n( 'g:i:s A' ),
83
                    'hh_mm_A' => date_i18n( 'h:i A' ),
84
                    'hh_mm_ss_A' => date_i18n( 'h:i:s A' ),
85
                    'h_mma' => date_i18n( 'g:ia' ),
86
                    'hh_mma' => date_i18n( 'h:ia' ),
87
                    'h_mm' => date_i18n( 'g:i' ),
88
                    'h_mm_ss' => date_i18n( 'g:i:s' ),
89
                    'hh_mm' => date_i18n( 'h:i' ),
90
                    'hh_mm_ss' => date_i18n( 'h:i:s' )
91
                )
92
            ),
93
            self::$type . '_format_24' => array(
94
                'label' => __( 'Time Format', 'pods' ),
95
                'depends-on' => array( self::$type . '_type' => '24' ),
96
                'default' => 'hh_mm',
97
                'type' => 'pick',
98
                'data' => array(
99
                    'hh_mm' => date_i18n( 'H:i' ),
100
                    'hh_mm_ss' => date_i18n( 'H:i:s' )
101
                )
102
            ),
103
            self::$type . '_allow_empty' => array(
104
                'label' => __( 'Allow empty value?', 'pods' ),
105
                'default' => 1,
106
                'type' => 'boolean'
107
            ),
108
            self::$type . '_html5' => array(
109
                'label' => __( 'Enable HTML5 Input Field?', 'pods' ),
110
                'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),
111
                'type' => 'boolean'
112
            )
113
        );
114
115
		$options[ self::$type . '_type' ][ 'default' ] = apply_filters( 'pods_form_ui_field_time_format_type_default', $options[ self::$type . '_type' ][ 'default' ] );
116
		$options[ self::$type . '_format' ][ 'data' ] = apply_filters( 'pods_form_ui_field_time_format_options', $options[ self::$type . '_format' ][ 'data' ] );
117
		$options[ self::$type . '_format' ][ 'default' ] = apply_filters( 'pods_form_ui_field_time_format_default', $options[ self::$type . '_format' ][ 'default' ] );
118
		$options[ self::$type . '_format_24' ][ 'data' ] = apply_filters( 'pods_form_ui_field_time_format_24_options', $options[ self::$type . '_format_24' ][ 'data' ] );
119
		$options[ self::$type . '_format_24' ][ 'default' ] = apply_filters( 'pods_form_ui_field_time_format_24_default', $options[ self::$type . '_format_24' ][ 'default' ] );
120
121
        return $options;
122
    }
123
124
    /**
125
     * Define the current field's schema for DB table storage
126
     *
127
     * @param array $options
128
     *
129
     * @return array
130
     * @since 2.0
131
     */
132
    public function schema ( $options = null ) {
133
        $schema = 'TIME NOT NULL default "00:00:00"';
134
135
        return $schema;
136
    }
137
138
    /**
139
     * Change the way the value of the field is displayed with Pods::get
140
     *
141
     * @param mixed $value
142
     * @param string $name
143
     * @param array $options
144
     * @param array $pod
145
     * @param int $id
146
     *
147
     * @return mixed|null|string
148
     * @since 2.0
149
     */
150
    public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
151
        $format = $this->format( $options );
152
153
        if ( !empty( $value ) ) {
154
            $date = $this->createFromFormat( 'H:i:s', (string) $value );
155
            $date_local = $this->createFromFormat( $format, (string) $value );
156
157
            if ( false !== $date )
158
                $value = $date->format( $format );
159
            elseif ( false !== $date_local )
160
                $value = $date_local->format( $format );
161
            else
162
                $value = date_i18n( $format, strtotime( (string) $value ) );
163
        }
164
        elseif ( 0 == pods_var( self::$type . '_allow_empty', $options, 1 ) )
165
            $value = date_i18n( $format );
166
        else
167
            $value = '';
168
169
        return $value;
170
    }
171
172
    /**
173
     * Customize output of the form field
174
     *
175
     * @param string $name
176
     * @param mixed $value
177
     * @param array $options
178
     * @param array $pod
179
     * @param int $id
180
     *
181
     * @since 2.0
182
     */
183 View Code Duplication
    public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
        $options = (array) $options;
185
        $form_field_type = PodsForm::$field_type;
0 ignored issues
show
Bug introduced by
The property field_type cannot be accessed from this context as it is declared private in class PodsForm.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
186
187
        if ( is_array( $value ) )
188
            $value = implode( ' ', $value );
189
190
        // Format Value
191
        $value = $this->display( $value, $name, $options, null, $pod, $id );
0 ignored issues
show
Unused Code introduced by
The call to PodsField_Time::display() has too many arguments starting with $id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
192
193
        $field_type = 'time';
194
195
        if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) {
196
            if ( pods_var( 'read_only', $options, false ) ) {
197
                $options[ 'readonly' ] = true;
198
199
                $field_type = 'text';
200
            }
201
            else
202
                return;
203
        }
204
        elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) {
205
            $options[ 'readonly' ] = true;
206
207
            $field_type = 'text';
208
        }
209
210
        pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
211
    }
212
213
    /**
214
     * Change the value or perform actions after validation but before saving to the DB
215
     *
216
     * @param mixed $value
217
     * @param int $id
218
     * @param string $name
219
     * @param array $options
220
     * @param array $fields
221
     * @param array $pod
222
     * @param object $params
223
     *
224
     * @return mixed|string
225
     * @since 2.0
226
     */
227 View Code Duplication
    public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
228
        $format = $this->format( $options );
229
230
        if ( !empty( $value ) && ( 0 == pods_var( self::$type . '_allow_empty', $options, 1 ) || !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) )
231
            $value = $this->convert_date( $value, 'H:i:s', $format );
232
        elseif ( 1 == pods_var( self::$type . '_allow_empty', $options, 1 ) )
233
            $value = '00:00:00';
234
        else
235
            $value = date_i18n( 'H:i:s' );
236
237
        return $value;
238
    }
239
240
    /**
241
     * Customize the Pods UI manage table column output
242
     *
243
     * @param int $id
244
     * @param mixed $value
245
     * @param string $name
246
     * @param array $options
247
     * @param array $fields
248
     * @param array $pod
249
     *
250
     * @return mixed|null|string
251
     * @since 2.0
252
     */
253 View Code Duplication
    public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
254
        $value = $this->display( $value, $name, $options, $pod, $id );
255
256
        if ( 1 == pods_var( self::$type . '_allow_empty', $options, 1 ) && ( empty( $value ) || in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) )
257
            $value = false;
258
259
        return $value;
260
    }
261
262
    /**
263
     * Build date/time format string based on options
264
     *
265
     * @param $options
266
     *
267
     * @return string
268
     * @since 2.0
269
     */
270
    public function format ( $options ) {
271
        $time_format = array(
272
            'h_mm_A' => 'g:i A',
273
            'h_mm_ss_A' => 'g:i:s A',
274
            'hh_mm_A' => 'h:i A',
275
            'hh_mm_ss_A' => 'h:i:s A',
276
            'h_mma' => 'g:ia',
277
            'hh_mma' => 'h:ia',
278
            'h_mm' => 'g:i',
279
            'h_mm_ss' => 'g:i:s',
280
            'hh_mm' => 'h:i',
281
            'hh_mm_ss' => 'h:i:s'
282
        );
283
284
        $time_format_24 = array(
285
			'hh_mm' => 'H:i',
286
			'hh_mm_ss' => 'H:i:s'
287
        );
288
289
		$time_format = apply_filters( 'pods_form_ui_field_time_formats', $time_format );
290
		$time_format_24 = apply_filters( 'pods_form_ui_field_time_formats_24', $time_format_24 );
291
292
        if ( 12 == pods_var( self::$type . '_type', $options ) )
293
            $format = $time_format[ pods_var( self::$type . '_format', $options, 'hh_mm', null, true ) ];
294 View Code Duplication
        else
295
            $format = $time_format_24[ pods_var( self::$type . '_format_24', $options, 'hh_mm', null, true ) ];
296
297
        return $format;
298
    }
299
300
    /**
301
     * @param $format
302
     * @param $date
303
     *
304
     * @return DateTime
305
     */
306 View Code Duplication
    public function createFromFormat ( $format, $date ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
307
        $datetime = false;
308
309
        if ( method_exists( 'DateTime', 'createFromFormat' ) ) {
310
            $timezone = get_option( 'timezone_string' );
311
312
            if ( empty( $timezone ) )
313
                $timezone = timezone_name_from_abbr( '', get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, 0 );
314
315
            if ( !empty( $timezone ) ) {
316
                $datetimezone = new DateTimeZone( $timezone );
317
318
                $datetime = DateTime::createFromFormat( $format, (string) $date, $datetimezone );
0 ignored issues
show
Bug introduced by
The method createFromFormat() does not exist on DateTime. Did you maybe mean format()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
319
            }
320
        }
321
322
        if ( false === $datetime )
323
            $datetime = new DateTime( date_i18n( 'H:i:s', strtotime( (string) $date ) ) );
324
325
        return apply_filters( 'pods_form_ui_field_datetime_formatter', $datetime, $format, $date );
326
    }
327
328
    /**
329
     * Convert a date from one format to another
330
     *
331
     * @param $date
332
     * @param $new_format
333
     * @param $original_format
334
     */
335 View Code Duplication
    public function convert_date ( $value, $new_format, $original_format = 'H:i:s' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
336
        if ( !empty( $value ) && !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) {
337
            $date = $this->createFromFormat( $original_format, (string) $value );
338
339
            if ( false !== $date )
340
                $value = $date->format( $new_format );
341
            else
342
                $value = date_i18n( $new_format, strtotime( (string) $value ) );
343
        }
344
        else
345
            $value = date_i18n( $new_format );
346
347
        return $value;
348
    }
349
}
350