Completed
Push — master ( cd320e...b9ab02 )
by Zack
15s
created

View_Table_Template::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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