GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-workflow_step.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-workflow_step.php
4  * @since 1.17.2
5  * @package GravityView
6  * @subpackage includes\fields
7  */
8 
10 
11  public $name = 'workflow_step';
12 
13  public $group = 'add-ons';
14 
15  public $icon = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyMS43IDExLjIiPjxwYXRoIGQ9Ik0xNC43IDUuOWwtNy00Yy0xLjItLjctMi41LS44LTMuNy0uMy0xLjcuNy0yLjYgMS45LTIuNyAzLjYtLjEgMS41LjQgMi43IDEuNCAzLjcgMS4xIDEuMSAyLjYgMS40IDQuMy45LjIgMCAuNS0uMiAxLjEtLjQuMi0uMS4zLS4xLjQtLjEuMyAwIC41LjEuNi40LjEuMyAwIC41LS4zLjctMS4yLjctMi40LjktMy44LjgtMS4zLS4yLTIuNS0uNy0zLjQtMS42Qy41IDguNS0uMSA3LjEgMCA1LjVjLjEtMi40IDEuMi00IDMuMy01QzQuNS0uMSA1LjgtLjIgNy4yLjJjLjIuMS42LjIgMS4yLjZsNyAzLjkuNC0uNi44IDIuMS0yLjIuMy4zLS42em0tNy44LS41bDcgNGMxLjIuNyAyLjUuOCAzLjcuMyAxLjctLjcgMi42LTEuOSAyLjgtMy42LjEtMS40LS40LTIuNi0xLjUtMy43cy0yLjUtMS40LTQuMy0xYy0uNC4xLS44LjMtMS4xLjRsLS40LjFjLS4zIDAtLjUtLjEtLjYtLjQtLjEtLjMgMC0uNS4zLS43IDEuMS0uNyAyLjQtLjkgMy44LS44IDEuNC4yIDIuNS43IDMuNCAxLjcgMS4yIDEuMiAxLjcgMi41IDEuNiA0LjEtLjEgMi4zLTEuMiA0LTMuMyA1LTEuNC42LTIuNy42LTMuOS4yLS4zLS4xLS43LS4zLTEuMS0uNWwtNy0zLjktLjQuNUw1LjEgNWwyLjItLjMtLjQuN3oiLz48L3N2Zz4=';
16 
17  public function __construct() {
18  $this->label = esc_html__( 'Workflow Step', 'gk-gravityview' );
19  $this->default_search_label = $this->label;
20 
21  $this->add_hooks();
22 
23  parent::__construct();
24  }
25 
26  function add_hooks() {
27 
28  add_filter( 'gravityview_search_field_label', array( $this, 'modify_gravityview_search_field_step_label' ), 10, 3 );
29 
30  add_filter( 'gravityview_widget_search_filters', array( $this, 'modify_frontend_search_fields' ), 10, 3 );
31 
32  add_filter( 'gravityview_field_entry_value_workflow_step', array( $this, 'modify_entry_value_workflow_step' ), 10, 4 );
33  }
34 
35  /**
36  * Get the value of the Workflow Step based on the `workflow_step` entry meta int value
37  *
38  * @uses Gravity_Flow_API::get_current_step
39  *
40  * @param string $output HTML value output
41  * @param array $entry The GF entry array
42  * @param array $field_settings Settings for the particular GV field
43  * @param array $field Current field being displayed
44  *
45  * @since 1.17
46  *
47  * @return string If Gravity Flow not found, or entry not processed yet, returns initial value. Otherwise, returns name of workflow step.
48  */
50 
51  // If not set, the entry hasn't started a workflow
52  $has_workflow_step = isset( $entry['workflow_step'] );
53 
54  if( $has_workflow_step ) {
55 
56  $GFlow = new Gravity_Flow_API( $entry['form_id'] );
57 
58  if ( $current_step = $GFlow->get_current_step( $entry ) ) {
59  $output = esc_html( $current_step->get_name() );
60  } else {
61  $output = esc_html__( 'Workflow Complete', 'gk-gravityview' );
62  }
63 
64  unset( $GFlow );
65  }
66 
67  return $output;
68  }
69 
70  /**
71  * Get the Workflow Step ID from a search field key
72  *
73  * @param string $key Search field key, in the following format: `workflow_step_status_[number]`
74  *
75  * @return bool|int The ID of the workflow step. False if not a workflow step field key.
76  */
77  private function get_step_id_from_key( $key ) {
78 
79  $workflow_step_id = false;
80 
81  preg_match( '/workflow_step_status_(\d+)/', $key, $matches );
82 
83  if ( ! empty( $matches ) ) {
84  $workflow_step_id = intval( $matches[1] );
85  }
86 
87  return $workflow_step_id;
88  }
89 
90  /**
91  * @since 1.17.3
92  *
93  * @param string $label Existing label text, sanitized.
94  * @param null|GF_Field $gf_field If search field is connected to a Gravity Forms field, the field object.
95  * @param array $field Array with the following keys: `field` ID of the meta key or field ID to be searched, `input` the type of search input to be shown, `label` the existing label. Same as $label parameter.
96  *
97  * @return string If showing a search field for a Step, show the step label.
98  */
99  function modify_gravityview_search_field_step_label( $label = '', $gf_field = null, $field = array() ) {
100 
101  $return = $label;
102 
103  if ( '' === $label && $workflow_step_id = $this->get_step_id_from_key( $field['field'] ) ) {
104 
105  $step = $this->get_workflow_step( $workflow_step_id );
106 
107  $return = esc_html( $step->get_label() );
108  }
109 
110  return $return;
111  }
112 
113  /**
114  * Get a Gravity_Flow_Step object from the step ID
115  *
116  * @since 1.17.3
117  *
118  * @uses GravityView_View::getFormId() to get the current form being searched
119  * @uses Gravity_Flow_API::get_step()
120  *
121  * @param int $workflow_step_id ID of the step
122  *
123  * @return bool|Gravity_Flow_Step
124  */
125  function get_workflow_step( $workflow_step_id = 0 ) {
126 
127  $form_id = GravityView_View::getInstance()->getFormId();
128 
129  $GFlow = new Gravity_Flow_API( $form_id );
130 
131  $workflow_step = $GFlow->get_step( $workflow_step_id );
132 
133  if( ! $GFlow || ! $workflow_step ) {
134  return false;
135  }
136 
137  return $workflow_step;
138  }
139 
140  /**
141  * Set the search field choices to the Steps available for the current form
142  *
143  * @since 1.17.3
144  *
145  * @param array $search_fields
146  * @param GravityView_Widget_Search $widget
147  * @param array $widget_args
148  *
149  * @return array
150  */
151  function modify_frontend_search_fields( $search_fields = array(), GravityView_Widget_Search $widget = null, $widget_args = array() ) {
152 
153  foreach ( $search_fields as & $search_field ) {
154 
155  if ( $this->name === $search_field['key'] ) {
156 
157  $form_id = GravityView_View::getInstance()->getFormId();
158 
159  $workflow_steps = gravity_flow()->get_steps( $form_id );
160 
161  $choices = array();
162 
163  foreach ( $workflow_steps as $step ) {
164  $choices[] = array(
165  'text' => $step->get_name(),
166  'value' => $step->get_id(),
167  );
168  }
169 
170  $search_field['choices'] = $choices;
171  }
172 
173  // Workflow Step Statuses
174  else if ( $workflow_step_id = $this->get_step_id_from_key( $search_field['key'] ) ) {
175 
176  $status_key = sprintf( 'workflow_step_status_%d', $workflow_step_id );
177 
178  $search_field['choices'] = GravityView_Plugin_Hooks_Gravity_Flow::get_status_options( null, $status_key );
179  }
180  }
181 
182  return $search_fields;
183  }
184 
185 }
186 
Modify field settings by extending this class.
modify_gravityview_search_field_step_label( $label='', $gf_field=null, $field=array())
static getInstance( $passed_post=NULL)
modify_frontend_search_fields( $search_fields=array(), GravityView_Widget_Search $widget=null, $widget_args=array())
Set the search field choices to the Steps available for the current form.
$field_settings['content']
Definition: custom.php:27
static get_status_options( $form_id=0, $status_key='workflow_final_status')
Get the available status choices from Gravity Flow.
modify_entry_value_workflow_step( $output, $entry, $field_settings, $field)
Get the value of the Workflow Step based on the workflow_step entry meta int value.
get_step_id_from_key( $key)
Get the Workflow Step ID from a search field key.
if(empty( $created_by)) $form_id
get_workflow_step( $workflow_step_id=0)
Get a Gravity_Flow_Step object from the step ID.
$entry
Definition: notes.php:27