Completed
Push — develop ( a060f1...c1e96d )
by Zack
24:07 queued 18:16
created

GravityView_Field_Sequence   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 44%

Importance

Changes 0
Metric Value
dl 0
loc 101
ccs 11
cts 25
cp 0.44
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A field_options() 0 20 1
B get_sequence() 0 48 9
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 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 );
0 ignored issues
show
Documentation introduced by
The property reverse does not exist on object<GV\Field>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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 ) {
0 ignored issues
show
Documentation introduced by
The property reverse does not exist on object<GV\Field>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
				$startlines[ $context_key ] = $context->view->get_entries()->total() - ( $pagenum * $pagesize );
104
				$startlines[ $context_key ] += $context->field->start - 1;
0 ignored issues
show
Documentation introduced by
The property start does not exist on object<GV\Field>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
105
			} else {
106
				$startlines[ $context_key ] = ( $pagenum * $pagesize ) + $context->field->start;
0 ignored issues
show
Documentation introduced by
The property start does not exist on object<GV\Field>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
107
			}
108
		}
109
110
		return $context->field->reverse ? $startlines[ $context_key ]-- : $startlines[ $context_key ]++;
0 ignored issues
show
Documentation introduced by
The property reverse does not exist on object<GV\Field>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
111
	}
112
}
113
114
new GravityView_Field_Sequence;
115