Completed
Push — develop ( e9f2c4...db0f69 )
by Zack
17:54
created

View_Table_Template::tr_before()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 16
ccs 4
cts 4
cp 1
crap 1
rs 9.7333
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 14
	public function __construct( View $view, Entry_Collection $entries, Request $request ) {
31
32 14
	    add_filter( 'gravityview/template/field/label', array( __CLASS__, 'add_columns_sort_links' ), 100, 2 );
33
34 14
		parent::__construct( $view, $entries, $request );
35 14
	}
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 22
	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 22
		$sort_columns = $context->view->settings->get( 'sort_columns' );
53
54 22
		if ( empty( $sort_columns ) ) {
55 22
            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 15
	public function the_columns() {
97 15
		$fields = $this->view->fields->by_position( 'directory_table-columns' );
98
99 15
		foreach ( $fields->by_visible()->all() as $field ) {
100 15
			$context = Template_Context::from_template( $this, compact( 'field' ) );
101 15
			$form = $field->form_id ? GF_Form::by_id( $field->form_id ) : $this->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...
102
103
			/**
104
			 * @deprecated Here for back-compatibility.
105
			 */
106 15
			$column_label = apply_filters( 'gravityview_render_after_label', $field->get_label( $this->view, $form ), $field->as_configuration() );
107 15
			$column_label = apply_filters( 'gravityview/template/field_label', $column_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...
108
109
			/**
110
			 * @filter `gravityview/template/field/label` Override the field label.
111
			 * @since 2.0
112
			 * @param[in,out] string $column_label The label to override.
113
			 * @param \GV\Template_Context $context The context. Does not have entry set here.
114
			 */
115 15
			$column_label = apply_filters( 'gravityview/template/field/label', $column_label, $context );
116
117
			$args = array(
118 15
				'field' => is_numeric( $field->ID ) ? $field->as_configuration() : null,
119
				'hide_empty' => false,
120 15
				'zone_id' => 'directory_table-columns',
121 15
				'markup' => '<th id="{{ field_id }}" class="{{ class }}" style="{{width:style}}">{{label}}</th>',
122 15
				'label_markup' => '<span class="gv-field-label">{{ label }}</span>',
123 15
				'label' => $column_label,
124
			);
125
126 15
			echo \gravityview_field_output( $args, $context );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '\'
Loading history...
127
		}
128 15
	}
129
130
	/**
131
	 * Output the entry row.
132
	 *
133
	 * @param \GV\Entry $entry The entry to be rendered.
134
	 * @param array $attributes The attributes for the <tr> tag
135
	 *
136
	 * @return void
137
	 */
138 12
	public function the_entry( \GV\Entry $entry, $attributes ) {
139
140 12
		$fields = $this->view->fields->by_position( 'directory_table-columns' )->by_visible();
141
142 12
		$context = Template_Context::from_template( $this, compact( 'entry', 'fields' ) );
143
144
		/**
145
		 * Push legacy entry context.
146
		 */
147 12
		\GV\Mocks\Legacy_Context::load( array(
148 12
			'entry' => $entry,
149
		) );
150
151
		/**
152
		 * @filter `gravityview_table_cells` Modify the fields displayed in a table
153
		 * @param array $fields
154
		 * @param \GravityView_View $this
155
		 * @deprecated Use `gravityview/template/table/fields`
156
		 */
157 12
		$fields = apply_filters( 'gravityview_table_cells', $fields->as_configuration(), \GravityView_View::getInstance() );
158 12
		$fields = Field_Collection::from_configuration( $fields );
159
160
		/**
161
		 * @filter `gravityview/template/table/fields` Modify the fields displayed in this tables.
162
		 * @param \GV\Field_Collection $fields The fields.
163
		 * @param \GV\Template_Context $context The context.
164
		 * @since 2.0
165
		 */
166 12
		$fields = apply_filters( 'gravityview/template/table/fields', $fields, $context );
167
168 12
		$context = Template_Context::from_template( $this, compact( 'entry', 'fields' ) );
169
170
		/**
171
		 * @filter `gravityview/template/table/entry/row/attributes` Filter the row attributes for the row in table view.
172
		 *
173
		 * @param array $attributes The HTML attributes.
174
		 * @param \GV\Template_Context The context.
175
		 *
176
		 * @since 2.0
177
		 */
178 12
		$attributes = apply_filters( 'gravityview/template/table/entry/row/attributes', $attributes, $context );
179
180
		/** Glue the attributes together. */
181 12
		foreach ( $attributes as $attribute => $value ) {
182 12
			$attributes[ $attribute ] = sprintf( "$attribute=\"%s\"", esc_attr( $value ) );
183
		}
184 12
		$attributes = implode( ' ', $attributes );
185
186
		?>
187
			<tr<?php echo $attributes ? " $attributes" : ''; ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$attributes'
Loading history...
188
                <?php
189
190
				/**
191
				 * @action `gravityview/template/table/cells/before` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells.
192
				 * @since 2.0
193
				 * @param \GV\Template_Context The context.
194
				 */
195 12
				do_action( 'gravityview/template/table/cells/before', $context );
196
197
                /**
198
                 * @action `gravityview_table_cells_before` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells.
199
                 * @since 1.0.7
200
				 * @param \GravityView_View $this Current GravityView_View object
201
				 * @deprecated Use `gravityview/template/table/cells/before`
202
                 */
203 12
                do_action( 'gravityview_table_cells_before', \GravityView_View::getInstance() );
204
205 12
                foreach ( $fields->all() as $field ) {
206 12
					$this->the_field( $field, $entry );
207
				}
208
209
				/**
210
				 * @action `gravityview/template/table/cells/after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells.
211
				 * @since 2.0
212
				 * @param \GV\Template_Context The context.
213
				 */
214 12
				do_action( 'gravityview/template/table/cells/after', $context );
215
216
                /**
217
                 * @action `gravityview_table_cells_after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells.
218
                 * @since 1.0.7
219
				 * @param \GravityView_View $this Current GravityView_View object
220
				 * @deprecated Use `gravityview/template/table/cells/after`
221
                 */
222 12
                do_action( 'gravityview_table_cells_after', \GravityView_View::getInstance() );
223
224
				?>
225 12
			</tr>
226
		<?php
227 12
	}
228
229
	/**
230
	 * Output a field cell.
231
	 *
232
	 * @param \GV\Field $field The field to be ouput.
233
	 * @param \GV\Field $entry The entry this field is for.
234
	 *
235
	 * @return void
236
	 */
237 11
	public function the_field( \GV\Field $field, \GV\Entry $entry ) {
238 11
		$form = $this->view->form;
239
240 11
		if ( $entry instanceof Multi_Entry ) {
241 1
			if ( ! $entry = Utils::get( $entry, $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...
242
				return;
243
			}
244 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...
245
		}
246
247 11
		$context = Template_Context::from_template( $this, compact( 'field', 'entry' ) );
248
249 11
		$renderer = new Field_Renderer();
250 11
		$source = is_numeric( $field->ID ) ? $this->view->form : new Internal_Source();
251
252 11
		$value = $renderer->render( $field, $this->view, $source, $entry, $this->request );
253
254
		$args = array(
255 11
			'entry' => $entry->as_entry(),
256 11
			'field' => is_numeric( $field->ID ) ? $field->as_configuration() : null,
257 11
			'value' => $value,
258
			'hide_empty' => false,
259 11
			'zone_id' => 'directory_table-columns',
260 11
			'markup' => '<td id="{{ field_id }}" class="{{ class }}">{{ value }}</td>',
261 11
            'form' => $form,
262
		);
263
264
		/** Output. */
265 11
		echo \gravityview_field_output( $args, $context );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '\'
Loading history...
266 11
	}
267
268
	/**
269
	 * `gravityview_table_body_before` and `gravityview/template/table/body/before` actions.
270
	 *
271
	 * Output inside the `tbody` of the table.
272
	 *
273
	 * @param $context \GV\Template_Context The 2.0 context.
274
	 *
275
	 * @return void
276
	 */
277 14
	public static function body_before( $context ) {
278
		/**
279
		 * @action `gravityview/template/table/body/before` Output inside the `tbody` of the table.
280
		 * @since 2.0
281
		 * @param \GV\Template_Context $context The template context.
282
		 */
283 14
		do_action( 'gravityview/template/table/body/before', $context );
284
285
		/**
286
		* @action `gravityview_table_body_before` Inside the `tbody`, before any rows are rendered. Can be used to insert additional rows.
287
		* @deprecated Use `gravityview/template/table/body/before`
288
		* @since 1.0.7
289
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
290
		*/
291 14
		do_action( 'gravityview_table_body_before', \GravityView_View::getInstance() /** ugh! */ );
292 14
	}
293
294
	/**
295
	 * `gravityview_table_body_after` and `gravityview/template/table/body/after` actions.
296
	 *
297
	 * Output inside the `tbody` of the table.
298
	 *
299
	 * @param $context \GV\Template_Context The 2.0 context.
300
	 *
301
	 * @return void
302
	 */
303 14
	public static function body_after( $context ) {
304
		/**
305
		 * @action `gravityview/template/table/body/after` Output inside the `tbody` of the table at the end.
306
		 * @since 2.0
307
		 * @param \GV\Template_Context $context The template context.
308
		 */
309 14
		do_action( 'gravityview/template/table/body/after', $context );
310
311
		/**
312
		* @action `gravityview_table_body_after` Inside the `tbody`, after any rows are rendered. Can be used to insert additional rows.
313
		* @deprecated Use `gravityview/template/table/body/after`
314
		* @since 1.0.7
315
		* @param \GravityView_View $gravityview_view Current GravityView_View object.
316
		*/
317 14
		do_action( 'gravityview_table_body_after', \GravityView_View::getInstance() /** ugh! */ );
318 14
	}
319
320
	/**
321
	 * `gravityview_table_tr_before` and `gravityview/template/table/tr/after` actions.
322
	 *
323
	 * Output inside the `tr` of the table.
324
	 *
325
	 * @param $context \GV\Template_Context The 2.0 context.
326
	 *
327
	 * @return void
328
	 */
329 6
	public static function tr_before( $context ) {
330
		/**
331
		 * @action `gravityview/template/table/tr/before` Output inside the `tr` of the table when there are no results.
332
		 * @since 2.0
333
		 * @param \GV\Template_Context $context The template context.
334
		 */
335 6
		do_action( 'gravityview/template/table/tr/before', $context );
336
337
		/**
338
		 * @action `gravityview_table_tr_before` Before the `tr` while rendering each entry in the loop. Can be used to insert additional table rows.
339
		 * @since 1.0.7
340
		 * @deprecated USe `gravityview/template/table/tr/before`
341
		 * @param \GravityView_View $gravityview_view Current GraivtyView_View object.
342
		 */
343 6
		do_action( 'gravityview_table_tr_before', \GravityView_View::getInstance() /** ugh! */ );
344 6
	}
345
346
	/**
347
	 * `gravityview_table_tr_after` and `gravityview/template/table/tr/after` actions.
348
	 *
349
	 * Output inside the `tr` of the table.
350
	 *
351
	 * @param $context \GV\Template_Context The 2.0 context.
352
	 *
353
	 * @return void
354
	 */
355 6
	public static function tr_after( $context ) {
356
		/**
357
		 * @action `gravityview/template/table/tr/after` Output inside the `tr` of the table when there are no results.
358
		 * @since 2.0
359
		 * @param \GV\Template_Context $context The template context.
360
		 */
361 6
		do_action( 'gravityview/template/table/tr/after', $context );
362
363
		/**
364
		 * @action `gravityview_table_tr_after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells.
365
		 * @since 1.0.7
366
		 * @deprecated USe `gravityview/template/table/tr/after`
367
		 * @param \GravityView_View $gravityview_view Current GravityView_View object.
368
		 */
369 6
		do_action( 'gravityview_table_tr_after', \GravityView_View::getInstance() /** ugh! */ );
370 6
	}
371
372
	/**
373
	 * `gravityview_entry_class` and `gravityview/template/table/entry/class` filters.
374
	 *
375
	 * Modify of the class of a row.
376
	 *
377
	 * @param string $class The class.
378
	 * @param \GV\Entry $entry The entry.
379
	 * @param \GV\Template_Context The context.
380
	 *
381
	 * @return string The classes.
382
	 */
383 11
	public static function entry_class( $class, $entry, $context ) {
384
		/**
385
		 * @filter `gravityview_entry_class` Modify the class applied to the entry row.
386
		 * @param string $class Existing class.
387
		 * @param array $entry Current entry being displayed
388
		 * @param \GravityView_View $this Current GravityView_View object
389
		 * @deprecated Use `gravityview/template/table/entry/class`
390
		 * @return string The modified class.
391
		 */
392 11
		$class = apply_filters( 'gravityview_entry_class', $class, $entry->as_entry(), \GravityView_View::getInstance() );
393
394
		/**
395
		 * @filter `gravityview/template/table/entry/class` Modify the class aplied to the entry row.
396
		 * @param string $class The existing class.
397
		 * @param \GV\Template_Context The context.
398
		 * @return string The modified class.
399
		 */
400 11
		return apply_filters( 'gravityview/template/table/entry/class', $class, Template_Context::from_template( $context->template, compact( 'entry' ) ) );
401
	}
402
}
403