settings->get_current_form(); $form = gf_apply_filters( array( 'gform_form_notification_page', $form['id'] ), $form, $notification_id ); // Get routing fields. $routing_fields = self::get_routing_fields( $form, '0' ); // If form does not have routing fields, exit. if ( empty( $routing_fields ) ) { $html = sprintf( '

%s

', esc_html__( 'To use notification routing, your form must have a field supported by conditional logic.', 'gravityforms' ) ); return $html; } // Get routing. $routing = $this->get_value(); $routing = empty( $routing ) ? array( array() ) : array_values( $routing ); // Add hidden input with routing. $html = sprintf( '', esc_attr( $this->settings->get_input_name_prefix() ), esc_attr( $this->name ), esc_attr( json_encode( $routing ) ) ); // Open routing container. $html .= '
'; // Get routing operators. $operators = array( 'is' => esc_html__( 'is', 'gravityforms' ), 'isnot' => esc_html__( 'is not', 'gravityforms' ), '>' => esc_html__( 'greater than', 'gravityforms' ), '<' => esc_html__( 'less than', 'gravityforms' ), 'contains' => esc_html__( 'contains', 'gravityforms' ), 'starts_with' => esc_html__( 'starts with', 'gravityforms' ), 'ends_with' => esc_html__( 'ends with', 'gravityforms' ), ); // Set has invalid rule flag. $has_invalid_rule = false; // Loop through routing, display inputs. foreach ( $routing as $i => $route ) { // Determine if rule is valid. if ( ! GFNotification::is_valid_notification_email( rgar( $route, 'email' ) ) && $this->settings->is_dependency_met( rgobj( $this, 'dependency' ) ) ) { $valid_rule = false; $has_invalid_rule = true; } else { $valid_rule = true; } // Prepare email input. $email_input = sprintf( '', $i, esc_attr( rgar( $route, 'email' ) ) ); // Prepare routing field input. $field_input = sprintf( '', $i, self::get_routing_fields( $form, rgar( $route, 'fieldId' ) ) ); // Get operators for route. $operator_options = ''; foreach ( $operators as $key => $value ) { $operator_options .= sprintf( '', $key, $value, selected( rgar( $route, 'operator' ), $key, false ) ); } // Prepare operator input. $operator_input = sprintf( '', $i, $operator_options ); // Prepare add button. $add_button = sprintf( '', esc_attr__( 'Add Another Rule', 'gravityforms' ), $i, $i + 1 ); // Prepare delete button. if ( count( $routing ) > 1 ) { $delete_button = sprintf( '', $i, esc_attr__( 'Remove This Rule', 'gravityforms' ), GFCommon::get_base_url() ); $delete_button = sprintf( '', esc_attr__( 'Remove This Rule', 'gravityforms' ), $i ); } else { $delete_button = ''; } // Display input. $html .= sprintf( '%s%s%s%s%s%s%s%s
', $valid_rule ? '' : ' class="gform-settings-field__notification-routing-route--invalid"', esc_html__( 'Send to', 'gravityforms' ), $email_input, esc_html__( 'if', 'gravityforms' ), $field_input, $operator_input, self::get_field_values( $i, $form, rgar( $route, 'fieldId' ), rgar( $route, 'value' ) ), $add_button, $delete_button ); } // Close routing container. $html .= ''; // Display validation error. if ( $has_invalid_rule ) { $html .= sprintf( '%s', esc_html__( 'Please enter a valid email address for all highlighted routing rules above.', 'gravityforms' ) ); } ob_start(); ?> set_error( rgobj( $this, 'error_message' ) ); } // Validate routes. foreach ( $value as $route ) { if ( ! GFNotification::is_valid_notification_email( rgar( $route, 'email' ) ) ) { $this->set_error( rgobj( $this, 'error_message' ) ); } } } // # HELPER METHODS ------------------------------------------------------------------------------------------------ /** * Gets all fields that can be used for notification routing and builds dropdowns. * * @since 2.5 * * @param array $form The Form Object to search through. * @param int $selected_field_id The currently selected field ID. * * @return string */ private static function get_routing_fields( $form, $selected_field_id ) { $str = ''; foreach ( $form['fields'] as $field ) { $field_label = GFFormsModel::get_label( $field ); if ( in_array( $field->get_input_type(), GFNotification::get_routing_field_types() ) ) { $selected = $field->id == $selected_field_id ? "selected='selected'" : ''; $str .= "'; } } return $str; } /** * Gets field values to be used with routing * * @since Unknown * * @param int $i The routing rule ID. * @param array $form The Form Object. * @param int $field_id The field ID. * @param string $selected_value The field value of the selected item. * * @return string */ private static function get_field_values( $i, $form, $field_id, $selected_value ) { if ( empty( $field_id ) ) { $field_id = self::get_first_routing_field( $form ); } if ( empty( $field_id ) ) { return null; } $field = GFAPI::get_field( $form, $field_id ); if ( ! $field ) { return null; } if ( $field->type == 'post_category' && $field->displayAllCategories == true ) { return wp_dropdown_categories( array( 'class' => 'gfield_routing_select gfield_category_dropdown gfield_routing_value_dropdown', 'orderby' => 'name', 'id' => 'routing_value_' . $i, 'selected' => $selected_value, 'hierarchical' => true, 'hide_empty' => 0, 'echo' => false, ) ); } else if ( $field->choices ) { $is_any_selected = false; $html = sprintf( ''; return $html; } else { return sprintf( '', esc_html__( 'Enter value', 'gravityforms' ), esc_attr( $selected_value ), $i ); } } /** * Gets the first field that can be used for notification routing. * * @since Unknown * * @param array $form The Form Object to search through. * * @return int */ private static function get_first_routing_field( $form ) { foreach ( $form['fields'] as $field ) { if ( in_array( $field->get_input_type(), GFNotification::get_routing_field_types() ) ) { return $field->id; } } return 0; } } Fields::register( 'notification_routing', '\Gravity_Forms\Gravity_Forms\Settings\Fields\Notification_Routing' );