Completed
Push — develop ( 87b48c...882b48 )
by Gennady
23:47 queued 03:49
created

View_List_Template   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 258
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 91.8%

Importance

Changes 0
Metric Value
dl 0
loc 258
ccs 56
cts 61
cp 0.918
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
B the_field() 0 65 9
A extract_zone_vars() 0 14 3
A entry_class() 0 19 1
A body_before() 0 16 1
A body_after() 0 16 1
A entry_before() 0 20 1
A entry_after() 0 20 1
1
<?php
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The View List Template class .
11
 *
12
 * Renders a \GV\View and a \GV\Entry_Collection via a \GV\View_Renderer.
13
 */
14
class View_List_Template extends View_Template {
15
	/**
16
	 * @var string The template slug to be loaded (like "table", "list")
17
	 */
18
	public static $slug = 'list';
19
20
	/**
21
	 * Output the field in the list view.
22
	 *
23
	 * @param \GV\Field $field The field to output.
24
	 * @param \GV\Entry $entry The entry.
25
	 * @param array $extras Extra stuff, like wpautop, etc.
26
	 *
27
	 * @return string
28
	 */
29 3
	public function the_field( \GV\Field $field, \GV\Entry $entry, $extras = null ) {
30 3
		$form = $this->view->form;
31
32 3
		if ( isset( $this->view->unions[ $entry['form_id'] ] ) ) {
33
			if ( isset( $this->view->unions[ $entry['form_id'] ][ $field->ID ] ) ) {
34
				$field = $this->view->unions[ $entry['form_id'] ][ $field->ID ];
35
			} elseif ( ! $field instanceof Internal_Field ) {
36
				$field = Internal_Field::from_configuration( array( 'id' => 'custom' ) );
37
			}
38
		}
39
40 3
		if ( $entry->is_multi() ) {
41 1
			if ( ! $single_entry = $entry->from_field( $field ) ) {
42 1
				return;
43
			}
44 1
			$form = GF_Form::by_id( $field->form_id );
0 ignored issues
show
Documentation introduced by
The property form_id 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...
45
		}
46
47
		/**
48
		 * Push legacy entry context.
49
		 */
50 3
		\GV\Mocks\Legacy_Context::load( array(
51 3
			'entry' => $entry,
52 3
			'form' => $form,
53
		) );
54
55 3
		$context = Template_Context::from_template( $this, compact( 'field', 'entry' ) );
56
57 3
		$renderer = new Field_Renderer();
58 3
		$source = is_numeric( $field->ID ) ? $form : new Internal_Source();
59
60 3
		$value = $renderer->render( $field, $this->view, $source, $entry, $this->request );
61
62
		/**
63
		 * @deprecated Here for back-compatibility.
64
		 */
65 3
		$label = apply_filters( 'gravityview_render_after_label', $field->get_label( $this->view, $form, $entry ), $field->as_configuration() );
66 3
		$label = apply_filters( 'gravityview/template/field_label', $label, $field->as_configuration(), $form->form ? $form->form : null, null );
0 ignored issues
show
Documentation introduced by
The property $form is declared private in GV\Form. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
67
68
		/**
69
		 * @filter `gravityview/template/field/label` Override the field label.
70
		 * @since 2.0
71
		 * @param[in,out] string $label The label to override.
72
		 * @param \GV\Template_Context $context The context.
73
		 */
74 3
		$label = apply_filters( 'gravityview/template/field/label', $label, $context );
75
76
		/**
77
		 * @filter `gravityview/template/table/entry/hide_empty`
78
		 * @param boolean Should the row be hidden if the value is empty? Default: don't hide.
79
		 * @param \GV\Template_Context $context The context ;) Love it, cherish it. And don't you dare modify it!
80
		 */
81 3
		$hide_empty = apply_filters( 'gravityview/render/hide-empty-zone', Utils::get( $extras, 'hide_empty', $this->view->settings->get( 'hide_empty', false ) ), $context );
82
83 3
		if ( is_numeric( $field->ID ) ) {
84 3
			$extras['field'] = $field->as_configuration();
85
		}
86
87 3
		$extras['entry'] = $entry->as_entry();
88 3
		$extras['hide_empty'] = $hide_empty;
89 3
		$extras['label'] = $label;
90 3
		$extras['value'] = $value;
91
92 3
		return \gravityview_field_output( $extras, $context );
93
	}
94
95
	/**
96
	 * Return an array of variables ready to be extracted.
97
	 *
98
	 * @param string|array $zones The field zones to grab.
99
	 *
100
	 * @return array An array ready to be extract()ed in the form of
101
	 *  $zone => \GV\Field_Collection
102
	 *  has_$zone => int
103
	 */
104 3
	public function extract_zone_vars( $zones ) {
105 3
		if ( ! is_array( $zones ) ) {
106
			$zones = array( $zones );
107
		}
108
109 3
		$vars = array();
110 3
		foreach ( $zones as $zone ) {
111 3
			$zone_var = str_replace( '-', '_', $zone );
112 3
			$vars[ $zone_var ] = $this->view->fields->by_position( 'directory_list-' . $zone )->by_visible( $this->view );
113 3
			$vars[ "has_$zone_var" ] = $vars[ $zone_var ]->count();
114
		}
115
116 3
		return $vars;
117
	}
118
119
	/**
120
	 * `gravityview_entry_class` and `gravityview/template/list/entry/class` filters.
121
	 *
122
	 * Modify of the class of a row.
123
	 *
124
	 * @param string $class The class.
125
	 * @param \GV\Entry $entry The entry.
126
	 * @param \GV\Template_Context The context.
127
	 *
128
	 * @return string The classes.
129
	 */
130 3
	public static function entry_class( $class, $entry, $context ) {
131
		/**
132
		 * @filter `gravityview_entry_class` Modify the class applied to the entry row.
133
		 * @param string $class Existing class.
134
		 * @param array $entry Current entry being displayed
135
		 * @param \GravityView_View $this Current GravityView_View object
136
		 * @deprecated Use `gravityview/template/list/entry/class`
137
		 * @return string The modified class.
138
		 */
139 3
		$class = apply_filters( 'gravityview_entry_class', $class, $entry->as_entry(), \GravityView_View::getInstance() );
140
141
		/**
142
		 * @filter `gravityview/template/list/entry/class` Modify the class aplied to the entry row.
143
		 * @param string $class The existing class.
144
		 * @param \GV\Template_Context The context.
145
		 * @return string The modified class.
146
		 */
147 3
		return apply_filters( 'gravityview/template/list/entry/class', $class, Template_Context::from_template( $context->template, compact( 'entry' ) ) );
148
	}
149
150
	/**
151
	 * `gravityview_list_body_before` and `gravityview/template/list/body/before` actions.
152
	 *
153
	 * Output inside the `tbody` of the list.
154
	 *
155
	 * @param $context \GV\Template_Context The 2.0 context.
156
	 *
157
	 * @return void
158
	 */
159 3
	public static function body_before( $context ) {
160
		/**
161
		 * @action `gravityview/template/list/body/before` Output inside the `tbody` of the list.
162
		 * @since 2.0
163
		 * @param \GV\Template_Context $context The template context.
164
		 */
165 3
		do_action( 'gravityview/template/list/body/before', $context );
166
167
		/**
168
		* @action `gravityview_list_body_before` Inside the `tbody`, before any rows are rendered. Can be used to insert additional rows.
169
		* @deprecated Use `gravityview/template/list/body/before`
170
		* @since 1.0.7
171
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
172
		*/
173 3
		do_action( 'gravityview_list_body_before', \GravityView_View::getInstance() /** ugh! */ );
174 3
	}
175
176
	/**
177
	 * `gravityview_list_body_after` and `gravityview/template/list/body/after` actions.
178
	 *
179
	 * Output inside the `tbody` of the list.
180
	 *
181
	 * @param $context \GV\Template_Context The 2.0 context.
182
	 *
183
	 * @return void
184
	 */
185 3
	public static function body_after( $context ) {
186
		/**
187
		 * @action `gravityview/template/list/body/after` Output inside the `tbody` of the list at the end.
188
		 * @since 2.0
189
		 * @param \GV\Template_Context $context The template context.
190
		 */
191 3
		do_action( 'gravityview/template/list/body/after', $context );
192
193
		/**
194
		* @action `gravityview_list_body_after` Inside the `tbody`, after any rows are rendered. Can be used to insert additional rows.
195
		* @deprecated Use `gravityview/template/list/body/after`
196
		* @since 1.0.7
197
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
198
		*/
199 3
		do_action( 'gravityview_list_body_after', \GravityView_View::getInstance() /** ugh! */ );
200 3
	}
201
202
	/**
203
	 * `gravityview_list_entry_before` and `gravityview/template/list/entry/before` actions.
204
	 * `gravityview_list_entry_title_before` and `gravityview/template/list/entry/title/before` actions.
205
	 * `gravityview_list_entry_content_before` and `gravityview/template/list/entry/content/before` actions.
206
	 * `gravityview_list_entry_footer_before` and `gravityview/template/list/entry/footer/before` actions.
207
	 *
208
	 * Output inside the `entry` of the list.
209
	 *
210
	 * @param \GV\Entry $entry The entry.
211
	 * @param \GV\Template_Context $context The 2.0 context.
212
	 * @param string $zone The list zone (footer, image, title, etc.).
213
	 *
214
	 * @return void
215
	 */
216 3
	public static function entry_before( $entry, $context, $zone = '' ) {
217 3
		$zone = str_replace( '//', '/', "/$zone/" );
218
219
		/**
220
		 * @action `gravityview/template/list/entry/$zone/before` Output inside the `entry` of the list at the end.
221
		 * @since 2.0
222
		 * @param \GV\Template_Context $context The template context.
223
		 */
224 3
		do_action( sprintf( 'gravityview/template/list/entry%sbefore', $zone ), Template_Context::from_template( $context->template, compact( 'entry' ) ) );
225
226 3
		$zone = str_replace( '/', '_', $zone );
227
228
		/**
229
		* @action `gravityview_list_entry_$zone_before` Inside the `entry`, before any rows are rendered. Can be used to insert additional rows.
230
		* @deprecated Use `gravityview/template/list/entry/$zone/before`
231
		* @since 1.0.7
232
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
233
		*/
234 3
		do_action( sprintf( 'gravityview_list_entry%sbefore', $zone ), $entry->as_entry(), \GravityView_View::getInstance() /** ugh! */ );
235 3
	}
236
237
	/**
238
	 * `gravityview_list_entry_after` and `gravityview/template/list/entry/after` actions.
239
	 * `gravityview_list_entry_title_after` and `gravityview/template/list/entry/title/after` actions.
240
	 * `gravityview_list_entry_content_after` and `gravityview/template/list/entry/content/after` actions.
241
	 * `gravityview_list_entry_footer_after` and `gravityview/template/list/entry/footer/after` actions.
242
	 *
243
	 * Output inside the `entry` of the list.
244
	 *
245
	 * @param \GV\Entry $entry The entry.
246
	 * @param \GV\Template_Context $context The 2.0 context.
247
	 * @param string $zone The list zone (footer, image, title, etc.).
248
	 *
249
	 * @return void
250
	 */
251 3
	public static function entry_after( $entry, $context, $zone = '' ) {
252 3
		$zone = str_replace( '//', '/', "/$zone/" );
253
254
		/**
255
		 * @action `gravityview/template/list/entry/$zone/after` Output inside the `entry` of the list at the end.
256
		 * @since 2.0
257
		 * @param \GV\Template_Context $context The template context.
258
		 */
259 3
		do_action( sprintf( 'gravityview/template/list/entry%safter', $zone ), Template_Context::from_template( $context->template, compact( 'entry' ) ) );
260
261 3
		$zone = str_replace( '/', '_', $zone );
262
263
		/**
264
		* @action `gravityview_list_entry_$zone_after` Inside the `entry`, after any rows are rendered. Can be used to insert additional rows.
265
		* @deprecated Use `gravityview/template/list/entry/after`
266
		* @since 1.0.7
267
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
268
		*/
269 3
		do_action( sprintf( 'gravityview_list_entry%safter', $zone ), $entry->as_entry(), \GravityView_View::getInstance() /** ugh! */ );
270 3
	}
271
}
272