GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-address.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-address.php
4  * @package GravityView
5  * @subpackage includes\fields
6  */
7 
8 /**
9  * Add custom options for address fields
10  */
12 
13  /** @inheritDoc */
14  var $name = 'address';
15 
16  /** @inheritDoc */
17  var $group = 'advanced';
18 
19  /** @inheritDoc */
20  var $is_numeric = false;
21 
22  /** @inheritDoc */
23  var $is_searchable = true;
24 
25  /** @inheritDoc */
26  var $search_operators = array( 'is', 'isnot', 'contains' );
27 
28  /**
29  * @since 2.8.1
30  * @var string
31  */
32  var $icon = 'dashicons-location-alt';
33 
34  /** @inheritDoc */
35  var $_gf_field_class_name = 'GF_Field_Address';
36 
37  public function __construct() {
38  $this->label = esc_html__( 'Address', 'gk-gravityview' );
39 
40  $this->add_hooks();
41 
42  parent::__construct();
43  }
44 
45  /**
46  * Add filters for this field type
47  *
48  * @since 1.19.2
49  *
50  * @return void
51  */
52  private function add_hooks() {
53  add_filter( 'gravityview/extension/search/input_type', array( $this, 'search_bar_input_type' ), 10, 3 );
54  add_filter( 'gravityview/search/input_types', array( $this, 'input_types' ) );
55  add_filter( 'gravityview_widget_search_filters', array( $this, 'search_field_filter' ), 10, 3 );
56  }
57 
58  /**
59  * Dynamically add choices to the address field dropdowns, if any
60  *
61  * @since 1.19.2
62  *
63  * @param array $search_fields Array of search filters with `key`, `label`, `value`, `type` keys
64  * @param GravityView_Widget_Search $widget Current widget object
65  * @param array $widget_args Args passed to this method. {@since 1.8}
66  *
67  * @return array If the search field GF Field type is `address`, and there are choices to add, adds them and changes the input type. Otherwise, sets the input to text.
68  */
69  public function search_field_filter( $search_fields, $widget, $widget_args ) {
70 
71  foreach ( $search_fields as & $search_field ) {
72 
73  if ( 'address' !== \GV\Utils::get( $search_field, 'type' ) ) {
74  continue;
75  }
76 
77  $field_id = intval( floor( $search_field['key'] ) );
78  $input_id = gravityview_get_input_id_from_id( $search_field['key'] );
79  $form = GravityView_View::getInstance()->getForm();
80 
81  /** @type GF_Field_Address $address_field */
82  $address_field = GFFormsModel::get_field( $form, $field_id );
83 
84  $choices = array();
85 
86  $method_name = 'get_choices_' . self::get_input_type_from_input_id( $input_id );
87  if( method_exists( $this, $method_name ) ) {
88  /**
89  * @uses GravityView_Field_Address::get_choices_country()
90  * @uses GravityView_Field_Address::get_choices_state()
91  */
92  $choices = $this->{$method_name}( $address_field, $form );
93  }
94 
95  if( ! empty( $choices ) ) {
96  $search_field['choices'] = $choices;
97  $search_field['type'] = \GV\Utils::get( $search_field, 'input');
98  } else {
99  $search_field['type'] = 'text';
100  $search_field['input'] = 'input_text';
101  }
102  }
103 
104  return $search_fields;
105  }
106 
107  /**
108  * Get array of countries to use for the search choices
109  *
110  * @since 1.19.2
111  *
112  * @see GF_Field_Address::get_countries()
113  *
114  * @param GF_Field_Address $address_field
115  *
116  * @return array Array of countries with `value` and `text` keys as the name of the country
117  */
118  private function get_choices_country( $address_field ) {
119 
120  $countries = $address_field->get_countries();
121 
122  $country_choices = array();
123 
124  foreach ( $countries as $key => $country ) {
125  $country_choices[] = array(
126  'value' => $country,
127  'text' => $country,
128  );
129  }
130 
131  return $country_choices;
132  }
133 
134  /**
135  * Get array of states to use for the search choices
136  *
137  * @since 1.19.2
138  *
139  * @uses GF_Field_Address::get_us_states()
140  * @uses GF_Field_Address::get_us_state_code()
141  * @uses GF_Field_Address::get_canadian_provinces()
142  *
143  * @param GF_Field_Address $address_field
144  * @param array $form
145  *
146  * @return array Array of countries with `value` and `text` keys as the name of the country
147  */
148  private function get_choices_state( $address_field, $form ) {
149 
150  $address_type = empty( $address_field->addressType ) ? $address_field->get_default_address_type( $form['id'] ) : $address_field->addressType;
151 
152  $state_choices = array();
153 
154  switch ( $address_type ) {
155  case 'us':
156  $states = GFCommon::get_us_states();
157  break;
158  case 'canadian':
159  $states = GFCommon::get_canadian_provinces();
160  break;
161  default:
162  $address_types = $address_field->get_address_types( $form['id'] );
163  $states = empty( $address_types[ $address_type ]['states'] ) ? array() : $address_types[ $address_type ]['states'];
164  break;
165  }
166 
167  foreach ( $states as $key => $state ) {
168  if ( is_array( $state ) ) {
169  $state_subchoices = array();
170 
171  foreach ( $state as $key => $substate ) {
172  $state_subchoices[] = array(
173  'value' => is_numeric( $key ) ? $substate : $key,
174  'text' => $substate,
175  );
176  }
177 
178  $state_choices[] = array(
179  'text' => $key,
180  'value' => $state_subchoices,
181  );
182 
183  } else {
184  $state_choices[] = array(
185  'value' => is_numeric( $key ) ? $state : $key,
186  'text' => $state,
187  );
188  }
189  }
190 
191  return $state_choices;
192  }
193 
194  /**
195  * Add the input types available for each custom search field type
196  *
197  * @since 1.19.2
198  *
199  * @param array $input_types Array of input types as the keys (`select`, `radio`, `multiselect`, `input_text`) with a string or array of supported inputs as values
200  *
201  * @return array $input_types array, but
202  */
203  public function input_types( $input_types ) {
204 
205  // Use the same inputs as the "text" input type allows
206  $text_inputs = \GV\Utils::get( $input_types, 'text' );
207 
208  $input_types['street'] = $text_inputs;
209  $input_types['street2'] = $text_inputs;
210  $input_types['city'] = $text_inputs;
211 
212  $input_types['state'] = array( 'select', 'radio', 'link' ) + $text_inputs;
213  $input_types['zip'] = array( 'input_text' );
214  $input_types['country'] = array( 'select', 'radio', 'link' ) + $text_inputs;
215 
216  return $input_types;
217  }
218 
219  /**
220  * Converts the custom input type (address) into the selected type
221  *
222  * @since 1.19.2
223  *
224  * @param string $input_type Assign an input type according to the form field type. Defaults: `boolean`, `multi`, `select`, `date`, `text`
225  * @param string $field_type Gravity Forms field type (also the `name` parameter of GravityView_Field classes)
226  * @param string|int|float $field_id ID of the field being processed
227  *
228  * @return string If the field ID matches an address field input, return those options {@see GravityView_Field_Address::input_types() }. Otherwise, original value is used.
229  */
230  public function search_bar_input_type( $input_type, $field_type, $field_id ) {
231 
232  // Is this search field for an input (eg: 4.2) or the whole address field (eg: 4)?
234 
235  if( 'address' !== $field_type && $input_id ) {
236  return $input_type;
237  }
238 
239  // If the input ID matches an expected address input, set to that. Otherwise, keep existing input type.
240  if( $address_field_name = self::get_input_type_from_input_id( $input_id ) ) {
241  $input_type = $address_field_name;
242  }
243 
244  return $input_type;
245  }
246 
247  /**
248  * Get a name for the input based on the input ID
249  *
250  * @since 1.19.2
251  *
252  * @param int $input_id ID of the specific input for the address field
253  *
254  * @return false|string If the input ID matches a known address field input, returns a name for that input ("city", or "country"). Otherwise, returns false.
255  */
256  private static function get_input_type_from_input_id( $input_id ) {
257 
258  $input_type = false;
259 
260  switch ( $input_id ) {
261  case 1:
262  $input_type = 'street';
263  break;
264  case 2:
265  $input_type = 'street2';
266  break;
267  case 3:
268  $input_type = 'city';
269  break;
270  case 4:
271  $input_type = 'state';
272  break;
273  case 5:
274  $input_type = 'zip';
275  break;
276  case 6:
277  $input_type = 'country';
278  break;
279  }
280 
281  return $input_type;
282  }
283 
284  public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
285 
286  // If this is NOT the full address field, return default options.
287  if( floor( $field_id ) !== floatval( $field_id ) ) {
288  return $field_options;
289  }
290 
291  if( 'edit' === $context ) {
292  return $field_options;
293  }
294 
295  $add_options = array();
296 
297  $add_options['show_map_link'] = array(
298  'type' => 'checkbox',
299  'label' => __( 'Show Map Link:', 'gk-gravityview' ),
300  'desc' => __( 'Display a "Map It" link below the address', 'gk-gravityview' ),
301  'value' => true,
302  'merge_tags' => false,
303  'group' => 'display',
304  'priority' => 98,
305  );
306 
307  $add_options['show_map_link_new_window'] = array(
308  'type' => 'checkbox',
309  'label' => __( 'Open map link in a new tab or window?', 'gk-gravityview' ),
310  'value' => false,
311  'merge_tags' => false,
312  'group' => 'display',
313  'requires' => 'show_map_link',
314  'priority' => 99,
315  );
316 
317  return $add_options + $field_options;
318  }
319 
320 }
321 
Modify field settings by extending this class.
static getInstance( $passed_post=NULL)
static get_input_type_from_input_id( $input_id)
Get a name for the input based on the input ID.
get_choices_country( $address_field)
Get array of countries to use for the search choices.
get_choices_state( $address_field, $form)
Get array of states to use for the search choices.
field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id)
search_bar_input_type( $input_type, $field_type, $field_id)
Converts the custom input type (address) into the selected type.
if(gravityview() ->plugin->is_GF_25()) $form
gravityview_get_input_id_from_id( $field_id='')
Very commonly needed: get the # of the input based on a full field ID.
add_hooks()
Add filters for this field type.
input_types( $input_types)
Add the input types available for each custom search field type.
search_field_filter( $search_fields, $widget, $widget_args)
Dynamically add choices to the address field dropdowns, if any.
if(empty( $created_by)) $form_id
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
Add custom options for address fields.
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
Definition: time.php:22