GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-plugin-hooks-gravity-forms-survey.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Customization for the Gravity Forms Survey Addon
4  *
5  * @file class-gravityview-plugin-hooks-gravity-forms-survey.php
6  * @package GravityView
7  * @license GPL2
8  * @author GravityView <[email protected]>
9  * @link http://gravityview.co
10  * @copyright Copyright 2016, Katz Web Services, Inc.
11  *
12  * @since 1.17
13  */
14 
15 /**
16  * @inheritDoc
17  * @since 1.17
18  */
20 
21  /**
22  * @type string Optional. Constant that should be defined by plugin or theme. Used to check whether plugin is active.
23  * @since 1.17
24  */
25  protected $constant_name = 'GF_SURVEY_VERSION';
26 
27  protected function add_hooks() {
28  add_filter( 'gravityview/edit_entry/form_fields', array( $this, 'fix_survey_fields' ), 10 );
29  add_action( 'gravityview/edit-entry/render/before', array( $this, 'add_render_hooks' ) );
30  add_action( 'gravityview/edit-entry/render/after', array( $this, 'remove_render_hooks' ) );
31 
32  add_filter( 'gravityview/extension/search/input_type', array( $this, 'modify_search_bar_input_type' ), 10, 2 );
33 
34  }
35 
36  /**
37  * Modify the search form input type for survey fields
38  *
39  * @since 1.17.3
40  *
41  * @param string $input_type Assign an input type according to the form field type. Defaults: `boolean`, `multi`, `select`, `date`, `text`
42  * @param string $field_type Gravity Forms field type (also the `name` parameter of GravityView_Field classes)
43  */
44  function modify_search_bar_input_type( $input_type = 'text', $field_type = '' ) {
45 
46  $return = $input_type;
47 
48  if( 'survey' === $field_type ) {
49  $return = 'select';
50  }
51 
52  return $return;
53  }
54 
55  /**
56  * Make sure Survey fields accept pre-populating values; otherwise existing values won't be filled-in
57  *
58  * @since 1.16.4
59  * @since 1.17 Moved to GravityView_Plugin_Hooks_Gravity_Forms_Survey class
60  *
61  * @param array $form
62  *
63  * @return array Form, with all fields set to `allowsPrepopulate => true`
64  */
65  public function fix_survey_fields( $fields ) {
66 
67  /** @var GF_Field $field */
68  foreach( $fields as &$field ) {
69  if( 'survey' === $field->type ) {
70  $field->allowsPrepopulate = true;
71  }
72  }
73 
74  return $fields;
75  }
76 
77  /**
78  * Add filters before rendering the Edit Entry form
79  *
80  * @since 1.17
81  *
82  * @return void
83  */
84  function add_render_hooks() {
85  add_filter( 'gform_field_value', array( $this, 'fix_survey_field_value'), 10, 3 );
86  }
87 
88  /**
89  * Remove the hooks added before rendering Edit Entry form
90  *
91  * @see add_render_hooks
92  *
93  * @since 1.17
94  *
95  * @return void
96  */
97  function remove_render_hooks() {
98  remove_filter( 'gform_field_value', array( $this, 'fix_survey_field_value'), 10 );
99  }
100 
101  /**
102  * Survey fields inject their output using `gform_field_input` filter, but in Edit Entry, the values were empty.
103  * We filter the values here because it was the easiest access point: tell the survey field the correct value, GF outputs it.
104  *
105  * @since 1.16.4
106  * @since 1.17 Moved to GravityView_Plugin_Hooks_Gravity_Forms_Survey class
107  *
108  * @param string $value Existing value
109  * @param GF_Field $field
110  * @param string $name Field custom parameter name, normally blank.
111  *
112  * @return mixed
113  */
114  public function fix_survey_field_value( $value, $field, $name ) {
115 
116  if( 'survey' === $field->type ) {
117 
118  $entry = GravityView_Edit_Entry::getInstance()->instances['render']->get_entry();
119 
120  // We need to run through each survey row until we find a match for expected values
121  foreach ( $entry as $field_id => $field_value ) {
122 
123  if ( (int) $field_id !== (int) $field->id ) {
124  continue;
125  }
126 
127  if ( \GV\Utils::get( $field, 'gsurveyLikertEnableMultipleRows' ) ) {
128  list( $row_val, $col_val ) = explode( ':', $field_value, 2 );
129 
130  // If the $name matches the $row_val, we are processing the correct row
131  if( $row_val === $name ) {
133  break;
134  }
135  } else {
136  // When not processing multiple rows, the value is the $entry[ $field_id ] value.
138  break;
139  }
140  }
141  }
142 
143  return $value;
144  }
145 
146 }
147 
add_render_hooks()
Add filters before rendering the Edit Entry form.
modify_search_bar_input_type( $input_type='text', $field_type='')
Modify the search form input type for survey fields.
remove_render_hooks()
Remove the hooks added before rendering Edit Entry form.
fix_survey_field_value( $value, $field, $name)
Survey fields inject their output using gform_field_input filter, but in Edit Entry, the values were empty.
Abstract class that makes it easy for plugins and themes to register no-conflict scripts and styles...
$entry
Definition: notes.php:27
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
Definition: time.php:22
$field_value
Definition: checkbox.php:24
static getInstance()