Completed
Pull Request — 2.x (#3388)
by Scott Kingsley
07:34
created

PodsField_Website::display()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

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