37 var
$icon =
'dashicons-editor-ol';
41 $this->label = esc_html__(
'Number Sequence',
'gk-gravityview' );
42 $this->
description = esc_html__(
'Display a sequential result number for each entry.',
'gk-gravityview' );
44 add_filter(
'gravityview/metaboxes/tooltips', array( $this,
'field_tooltips') );
46 add_filter(
'gravityview_entry_default_fields', array( $this,
'add_default_field' ), 10, 3 );
48 parent::__construct();
64 if (
'edit' === $zone ) {
65 return $entry_default_fields;
68 $entry_default_fields[
'sequence'] = array(
69 'label' => __(
'Result Number',
'gk-gravityview' ),
70 'type' => $this->name,
72 'icon' => $this->icon,
73 'group' =>
'gravityview',
76 return $entry_default_fields;
88 $return[
'reverse_sequence'] = array(
89 'title' => __(
'Reverse the order of the result numbers',
'gk-gravityview'),
90 'value' => __(
'Output the number sequence in descending order. If enabled, numbers will count down from high to low.',
'gk-gravityview'),
98 unset ( $field_options[
'search_filter'] );
103 'label' => __(
'First Number in the Sequence',
'gk-gravityview' ),
104 'desc' => __(
'For each entry, the displayed number will increase by one. When displaying ten entries, the first entry will display "1", and the last entry will show "10".',
'gk-gravityview'),
106 'merge_tags' =>
false,
109 'type' =>
'checkbox',
110 'label' => __(
'Reverse the order of the number sequence (high to low)',
'gk-gravityview' ),
111 'tooltip' =>
'reverse_sequence',
116 return $new_fields + $field_options;
139 static $merge_tag_sequences = array();
144 if ( empty( $view_data ) ) {
145 gravityview()->log->error(
'{sequence} Merge Tag used outside of a GravityView View.', array(
'data' => $matches ) );
152 if ( ! $legacy_field ) {
153 gravityview()->log->error(
'{sequence} Merge Tag was used without outside of the GravityView entry loop.', array(
'data' => $matches ) );
159 $context = new \GV\Template_Context();
165 $merge_tag_context =
$entry[
'id'] .
"/{$merge_tag_context}";
167 foreach ( $matches as $match ) {
169 $full_tag = $match[0];
170 $property = $match[1];
175 $modifiers = explode(
',', trim( $property ) );
177 foreach ( $modifiers as $modifier ) {
179 $modifier = trim( $modifier );
181 if (
'reverse' === $modifier ) {
185 $maybe_start = explode(
':', $modifier );
189 if( 1 ===
sizeof( $maybe_start ) ) {
190 $maybe_start = explode(
'=', $modifier );
193 if (
'start' === rgar( $maybe_start, 0 ) && is_numeric( rgar( $maybe_start, 1 ) ) ) {
194 $gv_field->start = (int) rgar( $maybe_start, 1 );
202 $merge_tag_context_modifiers = $merge_tag_context .
'/' . var_export(
$gv_field->reverse,
true ) .
'/' .
$gv_field->start;
204 if ( ! isset( $merge_tag_sequences[ $merge_tag_context_modifiers ] ) ) {
207 $sequence = $merge_tag_sequences[ $merge_tag_context_modifiers ] = $this->
get_sequence( $context );
209 $sequence = $merge_tag_sequences[ $merge_tag_context_modifiers ];
212 $return = str_replace( $full_tag, $sequence, $return );
226 static $startlines = array();
228 $context_key = md5( json_encode(
231 \
GV\Utils::get( $context,
'field/UID' ),
238 if ( $context->request &&
$entry = $context->request->is_entry() ) {
240 $sql_query = array();
242 add_filter(
'gform_gf_query_sql', $callback =
function( $sql ) use ( &$sql_query ) {
247 $total = $context->view->get_entries()->total();
248 remove_filter(
'gform_gf_query_sql', $callback );
250 unset( $sql_query[
'paginate'] );
254 foreach ( $wpdb->get_results( implode(
' ', $sql_query ), ARRAY_A ) as $n => $result ) {
255 if ( in_array(
$entry->ID, $result ) ) {
256 return $context->field->reverse ? ( $total - $n ) : ( $n + 1 );
261 } elseif ( ! isset( $startlines[ $context_key ] ) ) {
262 $pagenum = max( 0, \
GV\Utils::_GET(
'pagenum', 1 ) - 1 );
263 $pagesize = $context->view->settings->get(
'page_size', 25 );
265 if ( $context->field->reverse ) {
266 $startlines[ $context_key ] = $context->view->get_entries()->total() - ( $pagenum * $pagesize );
267 $startlines[ $context_key ] += $context->field->start - 1;
269 $startlines[ $context_key ] = ( $pagenum * $pagesize ) + $context->field->start;
273 return $context->field->reverse ? $startlines[ $context_key ]-- : $startlines[ $context_key ]++;
add_default_field( $entry_default_fields, $form=array(), $zone='')
Add as a default field, outside those set in the Gravity Form form.
Modify field settings by extending this class.
static getInstance( $passed_post=NULL)
field_tooltips( $tooltips)
Add tooltips.
replace_merge_tag( $matches=array(), $text='', $form=array(), $entry=array(), $url_encode=false, $esc_html=false)
Replace {sequence} Merge Tags inside Custom Content fields.
gravityview_get_current_view_data( $view_id=0)
Get data for a specific view.
static from_entry( $entry)
Construct a instance from a Gravity Forms entry array.
if(gravityview() ->plugin->is_GF_25()) $form
scale description p description
static by_id( $post_id)
Construct a instance from a post ID.
new GravityView_Field_Sequence
field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id)
static by_id( $field_id)
Get a from an internal Gravity Forms field ID.
if(empty( $created_by)) $form_id
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
gravityview()
The main GravityView wrapper function.
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
get_sequence( $context)
Calculate the current sequence number for the context.