Completed
Push — develop ( 36bdeb...23e671 )
by Gennady
20:27
created

View_Table_Template::entry_class()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 19
ccs 3
cts 3
cp 1
crap 1
rs 9.6333
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 Table Template class .
11
 *
12
 * Renders a \GV\View and a \GV\Entry_Collection via a \GV\View_Renderer.
13
 */
14
class View_Table_Template extends View_Template {
15
	/**
16
	 * @var string The template slug to be loaded (like "table", "list")
17
	 */
18
	public static $slug = 'table';
19
20
21
	/**
22
     * Constructor. Add filters to modify output.
23
     *
24
	 * @since 2.0.4
25
	 *
26
	 * @param View $view
27
	 * @param Entry_Collection $entries
28
	 * @param Request $request
29
	 */
30 15
	public function __construct( View $view, Entry_Collection $entries, Request $request ) {
31
32 15
	    add_filter( 'gravityview/template/field/label', array( __CLASS__, 'add_columns_sort_links' ), 100, 2 );
33
34 15
		parent::__construct( $view, $entries, $request );
35 15
	}
36
37
	/**
38
     * Add sorting links to HTML columns that support sorting
39
     *
40
     * @since 2.0.4
41
     * @since 2.0.5 Made static
42
     *
43
     * @static
44
     *
45
	 * @param string $column_label Label for the table column
46
	 * @param \GV\Template_Context $context
47
	 *
48
	 * @return string
49
	 */
50 24
	static public function add_columns_sort_links( $column_label, $context = null ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
51
52 24
		$sort_columns = $context->view->settings->get( 'sort_columns' );
53
54 24
		if ( empty( $sort_columns ) ) {
55 24
            return $column_label;
56
		}
57
58
		if ( ! \GravityView_frontend::getInstance()->is_field_sortable( $context->field->ID, $context->view->form->form ) ) {
59
			return $column_label;
60
		}
61
62
		$sorting = \GravityView_View::getInstance()->getSorting();
63
64
		$class = 'gv-sort';
65
66
		$sort_field_id = \GravityView_frontend::_override_sorting_id_by_field_type( $context->field->ID, $context->view->form->ID );
67
68
		$sort_args = array(
69
			'sort' => $context->field->ID,
70
			'dir' => 'asc',
71
		);
72
73
		if ( ! empty( $sorting['key'] ) && (string) $sort_field_id === (string) $sorting['key'] ) {
74
			//toggle sorting direction.
75
			if ( 'asc' === $sorting['direction'] ) {
76
				$sort_args['dir'] = 'desc';
77
				$class .= ' gv-icon-sort-desc';
78
			} else {
79
				$sort_args['dir'] = 'asc';
80
				$class .= ' gv-icon-sort-asc';
81
			}
82
		} else {
83
			$class .= ' gv-icon-caret-up-down';
84
		}
85
86
		$url = add_query_arg( $sort_args, remove_query_arg( array('pagenum') ) );
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
87
88
		return '<a href="'. esc_url_raw( $url ) .'" class="'. $class .'" ></a>&nbsp;'. $column_label;
89
	}
90
91
	/**
92
	 * Output the table column names.
93
	 *
94
	 * @return void
95
	 */
96 16
	public function the_columns() {
97 16
		$fields = $this->view->fields->by_position( 'directory_table-columns' );
98
99 16
		foreach ( $fields->by_visible()->all() as $field ) {
100 16
			$context = Template_Context::from_template( $this, compact( 'field' ) );
101
102
			$args = array(
103 16
				'field' => is_numeric( $field->ID ) ? $field->as_configuration() : null,
104
				'hide_empty' => false,
105 16
				'zone_id' => 'directory_table-columns',
106 16
				'markup' => '<th id="{{ field_id }}" class="{{ class }}" style="{{width:style}}" data-label="{{label_value:data-label}}">{{label}}</th>',
107 16
				'label_markup' => '<span class="gv-field-label">{{ label }}</span>',
108 16
				'label' => self::get_field_column_label( $field, $context ),
109
			);
110
111 16
			echo \gravityview_field_output( $args, $context );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '\'
Loading history...
112
		}
113 16
	}
114
115
	/**
116
     * Returns the label for a column, with support for all deprecated filters
117
     *
118
     * @since 2.1
119
     *
120
	 * @param \GV\Field $field
121
	 * @param \GV\Template_Context $context
122
	 */
123 15
	protected static function get_field_column_label( $field, $context = null ) {
124
125 15
		$form = $field->form_id ? GF_Form::by_id( $field->form_id ) : $context->view->form;
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...
126
127
		/**
128
		 * @deprecated Here for back-compatibility.
129
		 */
130 15
		$column_label = apply_filters( 'gravityview_render_after_label', $field->get_label( $context->view, $form ), $field->as_configuration() );
131 15
		$column_label = apply_filters( 'gravityview/template/field_label', $column_label, $field->as_configuration(), ( $form && $form->form ) ? $form->form : null, null );
132
133
		/**
134
		 * @filter `gravityview/template/field/label` Override the field label.
135
		 * @since 2.0
136
		 * @param[in,out] string $column_label The label to override.
137
		 * @param \GV\Template_Context $context The context. Does not have entry set here.
138
		 */
139 15
		$column_label = apply_filters( 'gravityview/template/field/label', $column_label, $context );
140
141 15
		return $column_label;
142
    }
143
144
	/**
145
	 * Output the entry row.
146
	 *
147
	 * @param \GV\Entry $entry The entry to be rendered.
148
	 * @param array $attributes The attributes for the <tr> tag
149
	 *
150
	 * @return void
151
	 */
152 13
	public function the_entry( \GV\Entry $entry, $attributes ) {
153
154 13
		$fields = $this->view->fields->by_position( 'directory_table-columns' )->by_visible();
155
156 13
		$context = Template_Context::from_template( $this, compact( 'entry', 'fields' ) );
157
158
		/**
159
		 * Push legacy entry context.
160
		 */
161 13
		\GV\Mocks\Legacy_Context::load( array(
162 13
			'entry' => $entry,
163
		) );
164
165
		/**
166
		 * @filter `gravityview_table_cells` Modify the fields displayed in a table
167
		 * @param array $fields
168
		 * @param \GravityView_View $this
169
		 * @deprecated Use `gravityview/template/table/fields`
170
		 */
171 13
		$fields = apply_filters( 'gravityview_table_cells', $fields->as_configuration(), \GravityView_View::getInstance() );
172 13
		$fields = Field_Collection::from_configuration( $fields );
173
174
		/**
175
		 * @filter `gravityview/template/table/fields` Modify the fields displayed in this tables.
176
		 * @param \GV\Field_Collection $fields The fields.
177
		 * @param \GV\Template_Context $context The context.
178
		 * @since 2.0
179
		 */
180 13
		$fields = apply_filters( 'gravityview/template/table/fields', $fields, $context );
181
182 13
		$context = Template_Context::from_template( $this, compact( 'entry', 'fields' ) );
183
184
		/**
185
		 * @filter `gravityview/template/table/entry/row/attributes` Filter the row attributes for the row in table view.
186
		 *
187
		 * @param array $attributes The HTML attributes.
188
		 * @param \GV\Template_Context The context.
189
		 *
190
		 * @since 2.0
191
		 */
192 13
		$attributes = apply_filters( 'gravityview/template/table/entry/row/attributes', $attributes, $context );
193
194
		/** Glue the attributes together. */
195 13
		foreach ( $attributes as $attribute => $value ) {
196 13
			$attributes[ $attribute ] = sprintf( "$attribute=\"%s\"", esc_attr( $value ) );
197
		}
198 13
		$attributes = implode( ' ', $attributes );
199
200
		?>
201
			<tr<?php echo $attributes ? " $attributes" : ''; ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$attributes'
Loading history...
202
                <?php
203
204
				/**
205
				 * @action `gravityview/template/table/cells/before` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells.
206
				 * @since 2.0
207
				 * @param \GV\Template_Context The context.
208
				 */
209 13
				do_action( 'gravityview/template/table/cells/before', $context );
210
211
                /**
212
                 * @action `gravityview_table_cells_before` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells.
213
                 * @since 1.0.7
214
				 * @param \GravityView_View $this Current GravityView_View object
215
				 * @deprecated Use `gravityview/template/table/cells/before`
216
                 */
217 13
                do_action( 'gravityview_table_cells_before', \GravityView_View::getInstance() );
218
219 13
                foreach ( $fields->all() as $field ) {
220 13
					if ( isset( $this->view->unions[ $entry['form_id'] ] ) ) {
221
						if ( isset( $this->view->unions[ $entry['form_id'] ][ $field->ID ] ) ) {
222
							$field = $this->view->unions[ $entry['form_id'] ][ $field->ID ];
223
						} else {
224
							if ( ! $field instanceof Internal_Field ) {
225
								$field = Internal_Field::from_configuration( array( 'id' => 'custom' ) );
226
							}
227
						}
228
					}
229 13
					$this->the_field( $field, $entry );
230
				}
231
232
				/**
233
				 * @action `gravityview/template/table/cells/after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells.
234
				 * @since 2.0
235
				 * @param \GV\Template_Context The context.
236
				 */
237 13
				do_action( 'gravityview/template/table/cells/after', $context );
238
239
                /**
240
                 * @action `gravityview_table_cells_after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells.
241
                 * @since 1.0.7
242
				 * @param \GravityView_View $this Current GravityView_View object
243
				 * @deprecated Use `gravityview/template/table/cells/after`
244
                 */
245 13
                do_action( 'gravityview_table_cells_after', \GravityView_View::getInstance() );
246
247
				?>
248 13
			</tr>
249
		<?php
250 13
	}
251
252
	/**
253
	 * Output a field cell.
254
	 *
255
	 * @param \GV\Field $field The field to be ouput.
256
	 * @param \GV\Field $entry The entry this field is for.
257
	 *
258
	 * @return void
259
	 */
260 12
	public function the_field( \GV\Field $field, \GV\Entry $entry ) {
261 12
		$form = $this->view->form;
262 12
		$single_entry = $entry;
263
264 12
		if ( $entry->is_multi() ) {
265 1
			if ( ! $single_entry = $entry->from_field( $field ) ) {
266
				echo '<td></td>';
267
				return;
268
			}
269 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...
270
		}
271
272 12
		$renderer = new Field_Renderer();
273 12
		$source = is_numeric( $field->ID ) ? $form : new Internal_Source();
274
275 12
		$value = $renderer->render( $field, $this->view, $source, $entry, $this->request );
276
277 12
		$context = Template_Context::from_template( $this, compact( 'field' ) );
278 12
		$context->entry = $single_entry;
279
280
		$args = array(
281 12
			'entry' => $entry->as_entry(),
282 12
			'field' => is_numeric( $field->ID ) ? $field->as_configuration() : null,
283 12
			'value' => $value,
284
			'hide_empty' => false,
285 12
			'zone_id' => 'directory_table-columns',
286 12
            'label' => self::get_field_column_label( $field, $context ),
287 12
			'markup' => '<td id="{{ field_id }}" class="{{ class }}" data-label="{{label_value:data-label}}">{{ value }}</td>',
288 12
            'form' => $form,
289
		);
290
291
		/** Output. */
292 12
		echo \gravityview_field_output( $args, $context );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '\'
Loading history...
293 12
	}
294
295
	/**
296
	 * `gravityview_table_body_before` and `gravityview/template/table/body/before` actions.
297
	 *
298
	 * Output inside the `tbody` of the table.
299
	 *
300
	 * @param $context \GV\Template_Context The 2.0 context.
301
	 *
302
	 * @return void
303
	 */
304 15
	public static function body_before( $context ) {
305
		/**
306
		 * @action `gravityview/template/table/body/before` Output inside the `tbody` of the table.
307
		 * @since 2.0
308
		 * @param \GV\Template_Context $context The template context.
309
		 */
310 15
		do_action( 'gravityview/template/table/body/before', $context );
311
312
		/**
313
		* @action `gravityview_table_body_before` Inside the `tbody`, before any rows are rendered. Can be used to insert additional rows.
314
		* @deprecated Use `gravityview/template/table/body/before`
315
		* @since 1.0.7
316
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
317
		*/
318 15
		do_action( 'gravityview_table_body_before', \GravityView_View::getInstance() /** ugh! */ );
319 15
	}
320
321
	/**
322
	 * `gravityview_table_body_after` and `gravityview/template/table/body/after` actions.
323
	 *
324
	 * Output inside the `tbody` of the table.
325
	 *
326
	 * @param $context \GV\Template_Context The 2.0 context.
327
	 *
328
	 * @return void
329
	 */
330 15
	public static function body_after( $context ) {
331
		/**
332
		 * @action `gravityview/template/table/body/after` Output inside the `tbody` of the table at the end.
333
		 * @since 2.0
334
		 * @param \GV\Template_Context $context The template context.
335
		 */
336 15
		do_action( 'gravityview/template/table/body/after', $context );
337
338
		/**
339
		* @action `gravityview_table_body_after` Inside the `tbody`, after any rows are rendered. Can be used to insert additional rows.
340
		* @deprecated Use `gravityview/template/table/body/after`
341
		* @since 1.0.7
342
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
343
		*/
344 15
		do_action( 'gravityview_table_body_after', \GravityView_View::getInstance() /** ugh! */ );
345 15
	}
346
347
	/**
348
	 * `gravityview_table_tr_before` and `gravityview/template/table/tr/after` actions.
349
	 *
350
	 * Output inside the `tr` of the table.
351
	 *
352
	 * @param $context \GV\Template_Context The 2.0 context.
353
	 *
354
	 * @return void
355
	 */
356 6
	public static function tr_before( $context ) {
357
		/**
358
		 * @action `gravityview/template/table/tr/before` Output inside the `tr` of the table when there are no results.
359
		 * @since 2.0
360
		 * @param \GV\Template_Context $context The template context.
361
		 */
362 6
		do_action( 'gravityview/template/table/tr/before', $context );
363
364
		/**
365
		 * @action `gravityview_table_tr_before` Before the `tr` while rendering each entry in the loop. Can be used to insert additional table rows.
366
		 * @since 1.0.7
367
		 * @deprecated USe `gravityview/template/table/tr/before`
368
		 * @param \GravityView_View $gravityview_view Current GraivtyView_View object.
369
		 */
370 6
		do_action( 'gravityview_table_tr_before', \GravityView_View::getInstance() /** ugh! */ );
371 6
	}
372
373
	/**
374
	 * `gravityview_table_tr_after` and `gravityview/template/table/tr/after` actions.
375
	 *
376
	 * Output inside the `tr` of the table.
377
	 *
378
	 * @param $context \GV\Template_Context The 2.0 context.
379
	 *
380
	 * @return void
381
	 */
382 6
	public static function tr_after( $context ) {
383
		/**
384
		 * @action `gravityview/template/table/tr/after` Output inside the `tr` of the table when there are no results.
385
		 * @since 2.0
386
		 * @param \GV\Template_Context $context The template context.
387
		 */
388 6
		do_action( 'gravityview/template/table/tr/after', $context );
389
390
		/**
391
		 * @action `gravityview_table_tr_after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells.
392
		 * @since 1.0.7
393
		 * @deprecated USe `gravityview/template/table/tr/after`
394
		 * @param \GravityView_View $gravityview_view Current GravityView_View object.
395
		 */
396 6
		do_action( 'gravityview_table_tr_after', \GravityView_View::getInstance() /** ugh! */ );
397 6
	}
398
399
	/**
400
	 * `gravityview_entry_class` and `gravityview/template/table/entry/class` filters.
401
	 *
402
	 * Modify of the class of a row.
403
	 *
404
	 * @param string $class The class.
405
	 * @param \GV\Entry $entry The entry.
406
	 * @param \GV\Template_Context The context.
407
	 *
408
	 * @return string The classes.
409
	 */
410 12
	public static function entry_class( $class, $entry, $context ) {
411
		/**
412
		 * @filter `gravityview_entry_class` Modify the class applied to the entry row.
413
		 * @param string $class Existing class.
414
		 * @param array $entry Current entry being displayed
415
		 * @param \GravityView_View $this Current GravityView_View object
416
		 * @deprecated Use `gravityview/template/table/entry/class`
417
		 * @return string The modified class.
418
		 */
419 12
		$class = apply_filters( 'gravityview_entry_class', $class, $entry->as_entry(), \GravityView_View::getInstance() );
420
421
		/**
422
		 * @filter `gravityview/template/table/entry/class` Modify the class aplied to the entry row.
423
		 * @param string $class The existing class.
424
		 * @param \GV\Template_Context The context.
425
		 * @return string The modified class.
426
		 */
427 12
		return apply_filters( 'gravityview/template/table/entry/class', $class, Template_Context::from_template( $context->template, compact( 'entry' ) ) );
428
	}
429
}
430