GravityView  1.19.4
The best, easiest way to display Gravity Forms entries on your website.
class.field.type.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Modify option field type by extending this class
5  */
6 abstract class GravityView_FieldType {
7 
8  /**
9  * Field form html `name`
10  *
11  * @var string
12  */
13  protected $name;
14 
15  /**
16  * Field settings
17  *
18  * @var array
19  */
20  protected $field;
21 
22  /**
23  * Field current value
24  *
25  * @var mixed
26  */
27  protected $value;
28 
29  function __construct( $name = '', $field = array(), $curr_value = NULL ) {
30 
31  $this->name = $name;
32 
33  $defaults = self::get_field_defaults();
34 
35  // Backward compatibility
36  if( !empty( $field['choices'] ) ) {
37  $field['options'] = $field['choices'];
38  unset( $field['choices'] );
39  }
40 
41  $this->field = wp_parse_args( $field, $defaults );
42 
43  $this->value = is_null( $curr_value ) ? $this->field['value'] : $curr_value;
44 
45  }
46 
47  /**
48  * Returns the default details for a field option
49  *
50  * - default // default option value, in case nothing is defined (@deprecated)
51  * - desc // option description
52  * - value // the option default value
53  * - label // the option label
54  * - left_label // In case of checkboxes, left label will appear on the left of the checkbox
55  * - id // the field id
56  * - type // the option type ( text, checkbox, select, ... )
57  * - options // when type is select, define the select options ('choices' is @deprecated)
58  * - merge_tags // if the option supports merge tags feature
59  * - class // (new) define extra classes for the field
60  * - tooltip //
61  *
62  * @return array
63  */
64  public static function get_field_defaults() {
65  return array(
66  'desc' => '',
67  'value' => NULL,
68  'label' => '',
69  'left_label' => NULL,
70  'id' => NULL,
71  'type' => 'text',
72  'options' => NULL,
73  'merge_tags' => true,
74  'class' => '',
75  'tooltip' => NULL,
76  'requires' => NULL
77  );
78  }
79 
80 
81  function get_tooltip() {
82  if( !function_exists('gform_tooltip') ) {
83  return NULL;
84  }
85 
86  return !empty( $this->field['tooltip'] ) ? ' '.gform_tooltip( $this->field['tooltip'] , false, true ) : NULL;
87  }
88 
89  /**
90  * Build input id based on the name
91  * @return string
92  */
93  function get_field_id() {
94  if( isset( $this->field['id'] ) ) {
95  return esc_attr( $this->field['id'] );
96  }
97  return esc_attr( sanitize_html_class( $this->name ) );
98  }
99 
100  /**
101  * Retrieve field label
102  * @return string
103  */
104  function get_field_label() {
105  return esc_html( trim( $this->field['label'] ) );
106  }
107 
108  /**
109  * Retrieve field left label
110  *
111  * @since 1.7
112  *
113  * @return string
114  */
115  function get_field_left_label() {
116  return ! empty( $this->field['left_label'] ) ? esc_html( trim( $this->field['left_label'] ) ) : NULL;
117  }
118 
119  /**
120  * Retrieve field label class
121  * @return string
122  */
123  function get_label_class() {
124  return 'gv-label-'. sanitize_html_class( $this->field['type'] );
125  }
126 
127 
128  /**
129  * Retrieve field description
130  * @return string
131  */
132  function get_field_desc() {
133  return !empty( $this->field['desc'] ) ? '<span class="howto">'. $this->field['desc'] .'</span>' : '';
134  }
135 
136 
137  /**
138  * Verify if field should have merge tags
139  * @return boolean
140  */
141  function show_merge_tags() {
142  // Show the merge tags if the field is a list view
143  $is_list = preg_match( '/_list-/ism', $this->name );
144  // Or is a single entry view
145  $is_single = preg_match( '/single_/ism', $this->name );
146 
147  return ( $is_single || $is_list );
148  }
149 
150 
151 
152  /**
153  * important! Override this class
154  * outputs the field option html
155  */
156  function render_option() {
157  // to replace on each field
158  }
159 
160  /**
161  * important! Override this class if needed
162  * outputs the field setting html
163  */
164  function render_setting( $override_input = NULL ) {
165 
166  if( !empty( $this->field['full_width'] ) ) { ?>
167  <th scope="row" colspan="2">
168  <div>
169  <label for="<?php echo $this->get_field_id(); ?>">
170  <?php echo $this->get_field_label() . $this->get_tooltip(); ?>
171  </label>
172  </div>
173  <?php $this->render_input( $override_input ); ?>
174  </th>
175  <?php } else { ?>
176  <th scope="row">
177  <label for="<?php echo $this->get_field_id(); ?>">
178  <?php echo $this->get_field_label() . $this->get_tooltip(); ?>
179  </label>
180  </th>
181  <td>
182  <?php $this->render_input( $override_input ); ?>
183  </td>
184  <?php }
185 
186  }
187 
188  /**
189  * important! Override this class
190  * outputs the input html part
191  */
192  function render_input( $override_input ) {
193  echo '';
194  }
195 
196 }
static get_field_defaults()
Returns the default details for a field option.
render_option()
important! Override this class outputs the field option html
get_label_class()
Retrieve field label class.
show_merge_tags()
Verify if field should have merge tags.
get_field_id()
Build input id based on the name.
get_field_desc()
Retrieve field description.
render_input($override_input)
important! Override this class outputs the input html part
render_setting($override_input=NULL)
important! Override this class if needed outputs the field setting html
__construct($name= '', $field=array(), $curr_value=NULL)
Modify option field type by extending this class.
get_field_left_label()
Retrieve field left label.
get_field_label()
Retrieve field label.