Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions includes/fields/class-gravityview-field-created-by.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class GravityView_Field_Created_By extends GravityView_Field {

public function __construct() {
$this->label = esc_html__( 'Created By', 'gravityview' );
$this->default_search_label = __( 'Submitted by:', 'gravityview' );
parent::__construct();
}

Expand Down
1 change: 1 addition & 0 deletions includes/fields/class-gravityview-field-date-created.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class GravityView_Field_Date_Created extends GravityView_Field {
public function __construct() {

$this->label = esc_html__( 'Date Created', 'gravityview' );
$this->default_search_label = $this->label;
$this->description = esc_html__( 'The date the entry was created.', 'gravityview' );

add_filter( 'gravityview_field_entry_value_' . $this->name . '_pre_link', array( $this, 'get_content' ), 10, 4 );
Expand Down
1 change: 1 addition & 0 deletions includes/fields/class-gravityview-field-is-fulfilled.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class GravityView_Field_Is_Fulfilled extends GravityView_Field {
*/
public function __construct() {
$this->label = esc_html__( 'Is Fulfilled', 'gravityview' );
$this->default_search_label = $this->label;

add_filter( 'gravityview_field_entry_value_' . $this->name . '_pre_link', array( $this, 'get_content' ), 10, 4 );

Expand Down
53 changes: 51 additions & 2 deletions includes/fields/class-gravityview-field-workflow_final_status.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,61 @@ class GravityView_Field_Workflow_Final_Status extends GravityView_Field {

public function __construct() {
$this->label = esc_html__( 'Workflow Status', 'gravityview' );
$this->default_search_label = $this->label;
$this->add_hooks();
parent::__construct();
}

function field_options( $field_options, $template_id, $field_id, $context, $input_type ) {
function add_hooks() {
add_filter( 'gravityview_widget_search_filters', array( $this, 'modify_search_filters' ), 10, 3 );

return $field_options;
add_filter( 'gravityview_field_entry_value_workflow_final_status', array( $this, 'modify_entry_value_workflow_final_status' ), 10, 4 );
}

/**
* Convert the status key with the full status label. Uses custom labels, if set.
*
* @uses Gravity_Flow::translate_status_label()
*
* @param string $output HTML value output
* @param array $entry The GF entry array
* @param array $field_settings Settings for the particular GV field
* @param array $field Current field being displayed
*
* @since 1.17
*
* @return string If Gravity Flow not found, or entry not processed yet, returns initial value. Otherwise, returns name of workflow step.
*/
function modify_entry_value_workflow_final_status( $output, $entry, $field_settings, $field ) {

if( ! empty( $output ) ) {
$output = gravity_flow()->translate_status_label( $output );
}

return $output;
}


/**
* Populate the Final Status Search Bar field dropdown with all the statuses in Gravity Flow
*
* @since 1.17.3
*
* @param array $search_fields
* @param GravityView_Widget_Search $widget
* @param array $widget_args
*
* @return array
*/
function modify_search_filters( $search_fields = array(), GravityView_Widget_Search $widget, $widget_args = array() ) {

foreach ( $search_fields as & $search_field ) {
if ( $this->name === $search_field['key'] ) {
$search_field['choices'] = GravityView_Plugin_Hooks_Gravity_Flow::get_status_options();
}
}

return $search_fields;
}

}
Expand Down
162 changes: 160 additions & 2 deletions includes/fields/class-gravityview-field-workflow_step.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,170 @@ class GravityView_Field_Workflow_Step extends GravityView_Field {

public function __construct() {
$this->label = esc_html__( 'Workflow Step', 'gravityview' );
$this->default_search_label = $this->label;

$this->add_hooks();

parent::__construct();
}

function add_hooks() {

function field_options( $field_options, $template_id, $field_id, $context, $input_type ) {
add_filter( 'gravityview_search_field_label', array( $this, 'modify_gravityview_search_field_step_label' ), 10, 3 );

return $field_options;
add_filter( 'gravityview_widget_search_filters', array( $this, 'modify_frontend_search_fields' ), 10, 3 );

add_filter( 'gravityview_field_entry_value_workflow_step', array( $this, 'modify_entry_value_workflow_step' ), 10, 4 );
}

/**
* Get the value of the Workflow Step based on the `workflow_step` entry meta int value
*
* @uses Gravity_Flow_API::get_current_step
*
* @param string $output HTML value output
* @param array $entry The GF entry array
* @param array $field_settings Settings for the particular GV field
* @param array $field Current field being displayed
*
* @since 1.17
*
* @return string If Gravity Flow not found, or entry not processed yet, returns initial value. Otherwise, returns name of workflow step.
*/
function modify_entry_value_workflow_step( $output, $entry, $field_settings, $field ) {

// If not set, the entry hasn't started a workflow
$has_workflow_step = isset( $entry['workflow_step'] );

if( $has_workflow_step ) {

$GFlow = new Gravity_Flow_API( $entry['form_id'] );

if ( $current_step = $GFlow->get_current_step( $entry ) ) {
$output = esc_html( $current_step->get_name() );
} else {
$output = esc_html__( 'Workflow Complete', 'gravityview' );
}

unset( $GFlow );
}

return $output;
}

/**
* Get the Workflow Step ID from a search field key
*
* @param string $key Search field key, in the following format: `workflow_step_status_[number]`
*
* @return bool|int The ID of the workflow step. False if not a workflow step field key.
*/
private function get_step_id_from_key( $key ) {

$workflow_step_id = false;

preg_match( '/workflow_step_status_(\d+)/', $key, $matches );

if ( ! empty( $matches ) ) {
$workflow_step_id = intval( $matches[1] );
}

return $workflow_step_id;
}

/**
* @since 1.17.3
*
* @param string $label Existing label text, sanitized.
* @param null|GF_Field $gf_field If search field is connected to a Gravity Forms field, the field object.
* @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.
*
* @return string If showing a search field for a Step, show the step label.
*/
function modify_gravityview_search_field_step_label( $label = '', $gf_field = null, $field = array() ) {

$return = $label;

if ( '' === $label && $workflow_step_id = $this->get_step_id_from_key( $field['field'] ) ) {

$step = $this->get_workflow_step( $workflow_step_id );

$return = esc_html( $step->get_label() );
}

return $return;
}

/**
* Get a Gravity_Flow_Step object from the step ID
*
* @since 1.17.3
*
* @uses GravityView_View::getFormId() to get the current form being searched
* @uses Gravity_Flow_API::get_step()
*
* @param int $workflow_step_id ID of the step
*
* @return bool|Gravity_Flow_Step
*/
function get_workflow_step( $workflow_step_id = 0 ) {

$form_id = GravityView_View::getInstance()->getFormId();

$GFlow = new Gravity_Flow_API( $form_id );

$workflow_step = $GFlow->get_step( $workflow_step_id );

if( ! $GFlow || ! $workflow_step ) {
return false;
}

return $workflow_step;
}

/**
* Set the search field choices to the Steps available for the current form
*
* @since 1.17.3
*
* @param array $search_fields
* @param GravityView_Widget_Search $widget
* @param array $widget_args
*
* @return array
*/
function modify_frontend_search_fields( $search_fields = array(), GravityView_Widget_Search $widget, $widget_args = array() ) {

foreach ( $search_fields as & $search_field ) {

if ( $this->name === $search_field['key'] ) {

$form_id = GravityView_View::getInstance()->getFormId();

$workflow_steps = gravity_flow()->get_steps( $form_id );

$choices = array();

foreach ( $workflow_steps as $step ) {
$choices[] = array(
'text' => $step->get_name(),
'value' => $step->get_id(),
);
}

$search_field['choices'] = $choices;
}

// Workflow Step Statuses
else if ( $workflow_step_id = $this->get_step_id_from_key( $search_field['key'] ) ) {

$status_key = sprintf( 'workflow_step_status_%d', $workflow_step_id );

$search_field['choices'] = GravityView_Plugin_Hooks_Gravity_Flow::get_status_options( null, $status_key );
}
}

return $search_fields;
}

}
Expand Down
31 changes: 31 additions & 0 deletions includes/fields/class-gravityview-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ abstract class GravityView_Field {
*/
public $label;

/**
* @var string The default search label used by the search widget, if not set
*/
public $default_search_label;

/**
* `standard`, `advanced`, `post`, `pricing`, `meta`, `gravityview`
* @since 1.15.2
Expand Down Expand Up @@ -114,9 +119,35 @@ public function __construct() {
add_filter( 'gform_replace_merge_tags', array( $this, '_filter_gform_replace_merge_tags' ), 10, 7 );
}

if( 'meta' === $this->group || '' !== $this->default_search_label ) {
add_filter( 'gravityview_search_field_label', array( $this, 'set_default_search_label' ), 10, 3 );
}

GravityView_Fields::register( $this );
}

/**
* Allow setting a default search label for search fields based on the field type
*
* Useful for entry meta "fields" that don't have Gravity Forms labels, like `created_by`
*
* @since 1.17.3
*
* @param string $label Existing label text, sanitized.
* @param array $gf_field Gravity Forms field array, as returned by `GFFormsModel::get_field()`
* @param array $field Field setting as sent by the GV configuration - has `field`, `input` (input type), and `label` keys
*
* @return string
*/
function set_default_search_label( $label = '', $gf_field = null, $field = array() ) {

if( $this->name === $field['field'] && '' === $label ) {
$label = esc_html( $this->default_search_label );
}

return $label;
}

/**
* Match the merge tag in replacement text for the field. DO NOT OVERRIDE.
*
Expand Down
Loading