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