Completed
Pull Request — 2.x (#3388)
by Scott Kingsley
11:08 queued 03:21
created

PodsField_Website::display()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 18
rs 9.2
cc 4
eloc 9
nc 3
nop 5
1
<?php
2
/**
3
 * @package Pods\Fields
4
 */
5
class PodsField_Website extends PodsField {
6
7
    /**
8
     * Field Type Group
9
     *
10
     * @var string
11
     * @since 2.0
12
     */
13
    public static $group = 'Text';
14
15
    /**
16
     * Field Type Identifier
17
     *
18
     * @var string
19
     * @since 2.0
20
     */
21
    public static $type = 'website';
22
23
    /**
24
     * Field Type Label
25
     *
26
     * @var string
27
     * @since 2.0
28
     */
29
    public static $label = 'Website';
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
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
52
     *
53
     * @since 2.0
54
     */
55
    public function options () {
56
        $options = array(
57
            self::$type . '_repeatable' => array(
58
                'label' => __( 'Repeatable Field', 'pods' ),
59
                'default' => 0,
60
                'type' => 'boolean',
61
                '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' ),
62
                'boolean_yes_label' => '',
63
                'dependency' => true,
64
                'developer_mode' => true
65
            ),
66
            self::$type . '_format' => array(
67
                'label' => __( 'Format', 'pods' ),
68
                'default' => 'normal',
69
                'type' => 'pick',
70
                'data' => array(
71
                    'normal' => __( 'http://example.com/', 'pods' ),
72
                    'no-www' => __( 'http://example.com/ (remove www)', 'pods' ),
73
                    'force-www' => __( 'http://www.example.com/ (force www if no sub-domain provided)', 'pods' ),
74
                    'no-http' => __( 'example.com', 'pods' ),
75
                    'no-http-no-www' => __( 'example.com (force removal of www)', 'pods' ),
76
                    'no-http-force-www' => __( 'www.example.com (force www if no sub-domain provided)', 'pods' )
77
                )
78
            ),
79
            self::$type . '_clickable' => array(
80
                'label' => __( 'Output as a link?', 'pods' ),
81
                'default' => apply_filters( 'pods_form_ui_field_website_clickable', 0, self::$type ),
82
                'type' => 'boolean',
83
                'dependency' => true,
84
            ),
85
            self::$type . '_new_window' => array(
86
                'label' => __( 'Open link in new window?', 'pods' ),
87
                'default' => apply_filters( 'pods_form_ui_field_website_new_window', 0, self::$type ),
88
                'type' => 'boolean',
89
                'depends-on' => array( self::$type . '_clickable' => true ),
90
            ),
91
            self::$type . '_max_length' => array(
92
                'label' => __( 'Maximum Length', 'pods' ),
93
                'default' => 255,
94
                'type' => 'number',
95
                'help' => __( 'Set to -1 for no limit', 'pods' )
96
            ),
97
            self::$type . '_html5' => array(
98
                'label' => __( 'Enable HTML5 Input Field?', 'pods' ),
99
                'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),
100
                'type' => 'boolean'
101
            )/*,
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
            self::$type . '_size' => array(
103
                'label' => __( 'Field Size', 'pods' ),
104
                'default' => 'medium',
105
                'type' => 'pick',
106
                'data' => array(
107
                    'small' => __( 'Small', 'pods' ),
108
                    'medium' => __( 'Medium', 'pods' ),
109
                    'large' => __( 'Large', 'pods' )
110
                )
111
            )*/
112
        );
113
        return $options;
114
    }
115
116
    /**
117
     * Define the current field's schema for DB table storage
118
     *
119
     * @param array $options
120
     *
121
     * @return array
122
     * @since 2.0
123
     */
124 View Code Duplication
    public function schema ( $options = 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...
125
        $length = (int) pods_var( self::$type . '_max_length', $options, 255 );
126
127
        $schema = 'VARCHAR(' . $length . ')';
128
129
        if ( 255 < $length || $length < 1 )
130
            $schema = 'LONGTEXT';
131
132
        return $schema;
133
    }
134
135
    /**
136
     * Change the way the value of the field is displayed with Pods::get
137
     *
138
     * @param mixed $value
139
     * @param string $name
140
     * @param array $options
141
     * @param array $pod
142
     * @param int $id
143
     *
144
     * @return mixed|null
145
     * @since 2.0
146
     */
147
    public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
148
        // Ensure proper format
149
        $value = $this->pre_save( $value, $id, $name, $options, null, $pod );
150
151
        if ( 1 == pods_v( self::$type . '_clickable', $options ) && 0 < strlen( $value ) ) {
152
            $link = '<a href="%s"%s>%s</a>';
153
154
            $atts = '';
155
156
            if ( 1 == pods_v( self::$type . '_new_window', $options ) ) {
157
            	$atts .= ' target="_blank"';
158
            }
159
160
            $value = sprintf( $link, esc_url( $value ), $atts, esc_html( $value ) );
161
        }
162
163
        return $value;
164
    }
165
166
    /**
167
     * Customize output of the form field
168
     *
169
     * @param string $name
170
     * @param mixed $value
171
     * @param array $options
172
     * @param array $pod
173
     * @param int $id
174
     *
175
     * @since 2.0
176
     */
177 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...
178
        $options = (array) $options;
179
        $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...
180
181
        if ( is_array( $value ) )
182
            $value = implode( ' ', $value );
183
184
        $field_type = 'website';
185
186
        if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) {
187
            if ( pods_var( 'read_only', $options, false ) ) {
188
                $options[ 'readonly' ] = true;
189
190
                $field_type = 'text';
191
            }
192
            else
193
                return;
194
        }
195
        elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) {
196
            $options[ 'readonly' ] = true;
197
198
            $field_type = 'text';
199
        }
200
201
        pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
202
    }
203
204
    /**
205
     * Validate a value before it's saved
206
     *
207
     * @param mixed $value
208
     * @param string $name
209
     * @param array $options
210
     * @param array $fields
211
     * @param array $pod
212
     * @param int $id
213
     *
214
     * @since 2.0
215
     */
216 View Code Duplication
    public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = 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...
217
        $errors = array();
218
219
        $label = strip_tags( pods_var_raw( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
220
221
        $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
222
223
        if ( is_array( $check ) )
224
            $errors = $check;
225
        else {
226
            if ( 0 < strlen( $value ) && strlen( $check ) < 1 ) {
227
                if ( 1 == pods_var( 'required', $options ) )
228
                    $errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
229
                else
230
                    $errors[] = sprintf( __( 'Invalid website provided for the field %s.', 'pods' ), $label );
231
            }
232
        }
233
234
        if ( !empty( $errors ) )
235
            return $errors;
236
237
        return true;
238
    }
239
240
    /**
241
     * Change the value or perform actions after validation but before saving to the DB
242
     *
243
     * @param mixed $value
244
     * @param int $id
245
     * @param string $name
246
     * @param array $options
247
     * @param array $fields
248
     * @param array $pod
249
     * @param object $params
250
     *
251
     * @since 2.0
252
     */
253
    public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
254
        $options = (array) $options;
255
256
        if ( is_array( $value ) ) {
257
            if ( isset( $value[ 'scheme' ] ) )
258
                $value = $this->build_url( $value );
259
            else
260
                $value = implode( '', $value );
261
        }
262
263
        if ( false === strpos( $value, '://' ) && 0 !== strpos( $value, '//' ) )
264
            $value = 'http://' . $value;
265
266
        $url = @parse_url( $value );
267
268
        if ( empty( $url ) || count( $url ) < 2 )
269
            $value = '';
270
        else {
271
            $defaults = array(
272
                'scheme' => 'http',
273
                'host' => '',
274
                'path' => '/',
275
                'query' => '',
276
                'fragment' => ''
277
            );
278
279
            $url = array_merge( $defaults, $url );
280
281
            if ( 'normal' == pods_var( self::$type . '_format', $options ) )
282
                $value = $this->build_url( $url );
283
            elseif ( 'no-www' == pods_var( self::$type . '_format', $options ) ) {
284 View Code Duplication
                if ( 0 === strpos( $url[ 'host' ], 'www.' ) )
285
                    $url[ 'host' ] = substr( $url[ 'host' ], 4 );
286
287
                $value = $this->build_url( $url );
288
            }
289
            elseif ( 'force-www' == pods_var( self::$type . '_format', $options ) ) {
290 View Code Duplication
                if ( false !== strpos( $url[ 'host' ], '.' ) && false === strpos( $url[ 'host' ], '.', 1 ) )
291
                    $url[ 'host' ] = 'www.' . $url[ 'host' ];
292
293
                $value = $this->build_url( $url );
294
            }
295
            elseif ( 'no-http' == pods_var( self::$type . '_format', $options ) ) {
296
                $value = $this->build_url( $url );
297
                $value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value );
298
299
                if ( '/' == $url[ 'path' ] )
300
                    $value = trim( $value, '/' );
301
            }
302
            elseif ( 'no-http-no-www' == pods_var( self::$type . '_format', $options ) ) {
303 View Code Duplication
                if ( 0 === strpos( $url[ 'host' ], 'www.' ) )
304
                    $url[ 'host' ] = substr( $url[ 'host' ], 4 );
305
306
                $value = $this->build_url( $url );
307
                $value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value );
308
309
                if ( '/' == $url[ 'path' ] )
310
                    $value = trim( $value, '/' );
311
            }
312
            elseif ( 'no-http-force-www' == pods_var( self::$type . '_format', $options ) ) {
313 View Code Duplication
                if ( false !== strpos( $url[ 'host' ], '.' ) && false === strpos( $url[ 'host' ], '.', 1 ) )
314
                    $url[ 'host' ] = 'www.' . $url[ 'host' ];
315
316
                $value = $this->build_url( $url );
317
                $value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value );
318
319
                if ( '/' == $url[ 'path' ] )
320
                    $value = trim( $value, '/' );
321
            }
322
        }
323
324
		$length = (int) pods_var( self::$type . '_max_length', $options, 255 );
325
326
		if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
327
			$value = pods_mb_substr( $value, 0, $length );
328
		}
329
330
        return $value;
331
    }
332
333
    /**
334
     * Customize the Pods UI manage table column output
335
     *
336
     * @param int $id
337
     * @param mixed $value
338
     * @param string $name
339
     * @param array $options
340
     * @param array $fields
341
     * @param array $pod
342
     *
343
     * @since 2.0
344
     */
345
    public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
346
        $value = $this->display( $value, $name, $options, $pod, $id );
347
348
        return $value;
349
    }
350
351
    /**
352
     * Build an url
353
     *
354
     * @param array|string $url
355
     *
356
     * @return string
357
     */
358
    public function build_url ( $url ) {
359
        if ( function_exists( 'http_build_url' ) )
360
            return http_build_url( $url );
361
362
        $defaults = array(
363
            'scheme' => 'http',
364
            'host' => '',
365
            'path' => '/',
366
            'query' => '',
367
            'fragment' => ''
368
        );
369
370
        $url = array_merge( $defaults, (array) $url );
371
372
        $new_url = trim( $url[ 'scheme' ] . '://', ':' ) . $url[ 'host' ] . '/' . ltrim( $url[ 'path' ], '/' );
373
374
        if ( !empty( $url[ 'query' ] ) )
375
            $new_url .= '?' . ltrim( $url[ 'query' ], '?' );
376
377
        if ( !empty( $url[ 'fragment' ] ) )
378
            $new_url .= '#' . ltrim( $url[ 'fragment' ], '#' );
379
380
        return $new_url;
381
    }
382
}
383