|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @file class-gravityview-field-sequence.php |
|
4
|
|
|
* @package GravityView |
|
5
|
|
|
* @subpackage includes\fields |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Add a sequence field. |
|
10
|
|
|
* @since develop |
|
11
|
|
|
*/ |
|
12
|
|
|
class GravityView_Field_Sequence extends GravityView_Field { |
|
13
|
|
|
|
|
14
|
|
|
var $name = 'sequence'; |
|
15
|
|
|
|
|
16
|
|
|
var $contexts = array( 'single', 'multiple' ); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var bool |
|
20
|
|
|
* @since 1.15.3 |
|
21
|
|
|
*/ |
|
22
|
|
|
var $is_sortable = false; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool |
|
26
|
|
|
* @since 1.15.3 |
|
27
|
|
|
*/ var $is_searchable = false; |
|
28
|
|
|
|
|
29
|
|
|
var $group = 'gravityview'; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct() { |
|
32
|
|
|
|
|
33
|
|
|
$this->label = esc_html__( 'Result Number', 'gravityview' ); |
|
34
|
|
|
|
|
35
|
|
|
parent::__construct(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
function field_options( $field_options, $template_id, $field_id, $context, $input_type ) { |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
unset ( $field_options['search_filter'] ); |
|
41
|
|
|
|
|
42
|
|
|
$new_fields = array( |
|
43
|
|
|
'start' => array( |
|
44
|
|
|
'type' => 'number', |
|
45
|
|
|
'label' => __( 'First row number', 'gravityview' ), |
|
46
|
|
|
'value' => '1', |
|
47
|
|
|
), |
|
48
|
|
|
'reverse' => array( |
|
49
|
|
|
'type' => 'checkbox', |
|
50
|
|
|
'label' => __( 'Reverse the sequence', 'gravityview' ), |
|
51
|
|
|
'tooltip' => __( 'Output row numbers in reverse order.', 'gravityview' ), |
|
52
|
|
|
'value' => '', |
|
53
|
|
|
), |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
return $new_fields + $field_options; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Calculate the current sequence number for the context. |
|
61
|
|
|
* @param \GV\Template_Context $context The context. |
|
62
|
|
|
* @return int The sequence number for the field/entry within the view results. |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function get_sequence( $context ) { |
|
65
|
2 |
|
static $startlines = array(); |
|
66
|
|
|
|
|
67
|
2 |
|
$context_key = md5( json_encode( |
|
68
|
|
|
array( |
|
69
|
2 |
|
$context->view->ID, |
|
70
|
2 |
|
$context->field->UID, |
|
71
|
|
|
) |
|
72
|
|
|
) ); |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Figure out the starting number. |
|
76
|
|
|
*/ |
|
77
|
2 |
|
if ( $context->request && $entry = $context->request->is_entry() ) { |
|
78
|
1 |
|
$sql_query = ''; |
|
79
|
1 |
|
add_filter( 'gform_gf_query_sql', $callback = function( $sql ) use ( &$sql_query ) { |
|
80
|
1 |
|
$sql_query = $sql; |
|
81
|
1 |
|
return $sql; |
|
82
|
1 |
|
} ); |
|
83
|
|
|
|
|
84
|
|
|
$total = $context->view->get_entries()->total(); |
|
85
|
|
|
remove_filter( 'gform_gf_query_sql', $callback ); |
|
86
|
|
|
|
|
87
|
|
|
unset( $sql_query['paginate'] ); |
|
88
|
|
|
|
|
89
|
|
|
global $wpdb; |
|
90
|
|
|
|
|
91
|
|
|
foreach ( $wpdb->get_results( implode( ' ', $sql_query ), ARRAY_A ) as $n => $result ) { |
|
92
|
|
|
if ( in_array( $entry->ID, $result ) ) { |
|
93
|
|
|
return $context->field->reverse ? ( $total - $n ) : ( $n + 1 ); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return 0; |
|
98
|
|
|
} elseif ( ! isset( $startlines[ $context_key ] ) ) { |
|
99
|
|
|
$pagenum = max( 0, \GV\Utils::_GET( 'pagenum', 1 ) - 1 ); |
|
100
|
|
|
$pagesize = $context->view->settings->get( 'page_size', 25 ); |
|
101
|
|
|
|
|
102
|
|
|
if ( $context->field->reverse ) { |
|
|
|
|
|
|
103
|
|
|
$startlines[ $context_key ] = $context->view->get_entries()->total() - ( $pagenum * $pagesize ); |
|
104
|
|
|
$startlines[ $context_key ] += $context->field->start - 1; |
|
|
|
|
|
|
105
|
|
|
} else { |
|
106
|
|
|
$startlines[ $context_key ] = ( $pagenum * $pagesize ) + $context->field->start; |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $context->field->reverse ? $startlines[ $context_key ]-- : $startlines[ $context_key ]++; |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
new GravityView_Field_Sequence; |
|
115
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.