GravityView  2.1.1
The best, easiest way to display Gravity Forms entries on your website.
class-gv-template-view-table.php
Go to the documentation of this file.
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 Table Template class .
11  *
12  * Renders a \GV\View and a \GV\Entry_Collection via a \GV\View_Renderer.
13  */
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  public function __construct( View $view, Entry_Collection $entries, Request $request ) {
31 
32  add_filter( 'gravityview/template/field/label', array( __CLASS__, 'add_columns_sort_links' ), 100, 2 );
33 
34  parent::__construct( $view, $entries, $request );
35  }
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  static public function add_columns_sort_links( $column_label, $context = null ) {
51 
52  $sort_columns = $context->view->settings->get( 'sort_columns' );
53 
54  if ( empty( $sort_columns ) ) {
55  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') ) );
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  public function the_columns() {
97  $fields = $this->view->fields->by_position( 'directory_table-columns' );
98 
99  foreach ( $fields->by_visible()->all() as $field ) {
100  $context = Template_Context::from_template( $this, compact( 'field' ) );
101 
102  $args = array(
103  'field' => is_numeric( $field->ID ) ? $field->as_configuration() : null,
104  'hide_empty' => false,
105  'zone_id' => 'directory_table-columns',
106  'markup' => '<th id="{{ field_id }}" class="{{ class }}" style="{{width:style}}" data-label="{{label_value:data-label}}">{{label}}</th>',
107  'label_markup' => '<span class="gv-field-label">{{ label }}</span>',
108  'label' => self::get_field_column_label( $field, $context ),
109  );
110 
111  echo \gravityview_field_output( $args, $context );
112  }
113  }
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  protected static function get_field_column_label( $field, $context = null ) {
124 
125  $form = $field->form_id ? GF_Form::by_id( $field->form_id ) : $context->view->form;
126 
127  /**
128  * @deprecated Here for back-compatibility.
129  */
130  $column_label = apply_filters( 'gravityview_render_after_label', $field->get_label( $context->view, $form ), $field->as_configuration() );
131  $column_label = apply_filters( 'gravityview/template/field_label', $column_label, $field->as_configuration(), $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  $column_label = apply_filters( 'gravityview/template/field/label', $column_label, $context );
140 
141  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  public function the_entry( \GV\Entry $entry, $attributes ) {
153 
154  $fields = $this->view->fields->by_position( 'directory_table-columns' )->by_visible();
155 
156  $context = Template_Context::from_template( $this, compact( 'entry', 'fields' ) );
157 
158  /**
159  * Push legacy entry context.
160  */
162  '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  $fields = apply_filters( 'gravityview_table_cells', $fields->as_configuration(), \GravityView_View::getInstance() );
172  $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  $fields = apply_filters( 'gravityview/template/table/fields', $fields, $context );
181 
182  $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  $attributes = apply_filters( 'gravityview/template/table/entry/row/attributes', $attributes, $context );
193 
194  /** Glue the attributes together. */
195  foreach ( $attributes as $attribute => $value ) {
196  $attributes[ $attribute ] = sprintf( "$attribute=\"%s\"", esc_attr( $value ) );
197  }
198  $attributes = implode( ' ', $attributes );
199 
200  ?>
201  <tr<?php echo $attributes ? " $attributes" : ''; ?>>
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  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  do_action( 'gravityview_table_cells_before', \GravityView_View::getInstance() );
218 
219  foreach ( $fields->all() as $field ) {
220  $this->the_field( $field, $entry );
221  }
222 
223  /**
224  * @action `gravityview/template/table/cells/after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells.
225  * @since 2.0
226  * @param \GV\Template_Context The context.
227  */
228  do_action( 'gravityview/template/table/cells/after', $context );
229 
230  /**
231  * @action `gravityview_table_cells_after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells.
232  * @since 1.0.7
233  * @param \GravityView_View $this Current GravityView_View object
234  * @deprecated Use `gravityview/template/table/cells/after`
235  */
236  do_action( 'gravityview_table_cells_after', \GravityView_View::getInstance() );
237 
238  ?>
239  </tr>
240  <?php
241  }
242 
243  /**
244  * Output a field cell.
245  *
246  * @param \GV\Field $field The field to be ouput.
247  * @param \GV\Field $entry The entry this field is for.
248  *
249  * @return void
250  */
251  public function the_field( \GV\Field $field, \GV\Entry $entry ) {
252  $form = $this->view->form;
253 
254  if ( $entry instanceof Multi_Entry ) {
255  if ( ! $entry = Utils::get( $entry, $field->form_id ) ) {
256  return;
257  }
258  $form = GF_Form::by_id( $field->form_id );
259  }
260 
261  $context = Template_Context::from_template( $this, compact( 'field', 'entry' ) );
262 
263  $renderer = new Field_Renderer();
264  $source = is_numeric( $field->ID ) ? $this->view->form : new Internal_Source();
265 
266  $value = $renderer->render( $field, $this->view, $source, $entry, $this->request );
267 
268  $args = array(
269  'entry' => $entry->as_entry(),
270  'field' => is_numeric( $field->ID ) ? $field->as_configuration() : null,
271  'value' => $value,
272  'hide_empty' => false,
273  'zone_id' => 'directory_table-columns',
274  'label' => self::get_field_column_label( $field, $context ),
275  'markup' => '<td id="{{ field_id }}" class="{{ class }}" data-label="{{label_value:data-label}}">{{ value }}</td>',
276  'form' => $form,
277  );
278 
279  /** Output. */
280  echo \gravityview_field_output( $args, $context );
281  }
282 
283  /**
284  * `gravityview_table_body_before` and `gravityview/template/table/body/before` actions.
285  *
286  * Output inside the `tbody` of the table.
287  *
288  * @param $context \GV\Template_Context The 2.0 context.
289  *
290  * @return void
291  */
292  public static function body_before( $context ) {
293  /**
294  * @action `gravityview/template/table/body/before` Output inside the `tbody` of the table.
295  * @since 2.0
296  * @param \GV\Template_Context $context The template context.
297  */
298  do_action( 'gravityview/template/table/body/before', $context );
299 
300  /**
301  * @action `gravityview_table_body_before` Inside the `tbody`, before any rows are rendered. Can be used to insert additional rows.
302  * @deprecated Use `gravityview/template/table/body/before`
303  * @since 1.0.7
304  * @param \GravityView_View $gravityview_view Current GravityView_View object.
305  */
306  do_action( 'gravityview_table_body_before', \GravityView_View::getInstance() /** ugh! */ );
307  }
308 
309  /**
310  * `gravityview_table_body_after` and `gravityview/template/table/body/after` actions.
311  *
312  * Output inside the `tbody` of the table.
313  *
314  * @param $context \GV\Template_Context The 2.0 context.
315  *
316  * @return void
317  */
318  public static function body_after( $context ) {
319  /**
320  * @action `gravityview/template/table/body/after` Output inside the `tbody` of the table at the end.
321  * @since 2.0
322  * @param \GV\Template_Context $context The template context.
323  */
324  do_action( 'gravityview/template/table/body/after', $context );
325 
326  /**
327  * @action `gravityview_table_body_after` Inside the `tbody`, after any rows are rendered. Can be used to insert additional rows.
328  * @deprecated Use `gravityview/template/table/body/after`
329  * @since 1.0.7
330  * @param \GravityView_View $gravityview_view Current GravityView_View object.
331  */
332  do_action( 'gravityview_table_body_after', \GravityView_View::getInstance() /** ugh! */ );
333  }
334 
335  /**
336  * `gravityview_table_tr_before` and `gravityview/template/table/tr/after` actions.
337  *
338  * Output inside the `tr` of the table.
339  *
340  * @param $context \GV\Template_Context The 2.0 context.
341  *
342  * @return void
343  */
344  public static function tr_before( $context ) {
345  /**
346  * @action `gravityview/template/table/tr/before` Output inside the `tr` of the table when there are no results.
347  * @since 2.0
348  * @param \GV\Template_Context $context The template context.
349  */
350  do_action( 'gravityview/template/table/tr/before', $context );
351 
352  /**
353  * @action `gravityview_table_tr_before` Before the `tr` while rendering each entry in the loop. Can be used to insert additional table rows.
354  * @since 1.0.7
355  * @deprecated USe `gravityview/template/table/tr/before`
356  * @param \GravityView_View $gravityview_view Current GraivtyView_View object.
357  */
358  do_action( 'gravityview_table_tr_before', \GravityView_View::getInstance() /** ugh! */ );
359  }
360 
361  /**
362  * `gravityview_table_tr_after` and `gravityview/template/table/tr/after` actions.
363  *
364  * Output inside the `tr` of the table.
365  *
366  * @param $context \GV\Template_Context The 2.0 context.
367  *
368  * @return void
369  */
370  public static function tr_after( $context ) {
371  /**
372  * @action `gravityview/template/table/tr/after` Output inside the `tr` of the table when there are no results.
373  * @since 2.0
374  * @param \GV\Template_Context $context The template context.
375  */
376  do_action( 'gravityview/template/table/tr/after', $context );
377 
378  /**
379  * @action `gravityview_table_tr_after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells.
380  * @since 1.0.7
381  * @deprecated USe `gravityview/template/table/tr/after`
382  * @param \GravityView_View $gravityview_view Current GravityView_View object.
383  */
384  do_action( 'gravityview_table_tr_after', \GravityView_View::getInstance() /** ugh! */ );
385  }
386 
387  /**
388  * `gravityview_entry_class` and `gravityview/template/table/entry/class` filters.
389  *
390  * Modify of the class of a row.
391  *
392  * @param string $class The class.
393  * @param \GV\Entry $entry The entry.
394  * @param \GV\Template_Context The context.
395  *
396  * @return string The classes.
397  */
398  public static function entry_class( $class, $entry, $context ) {
399  /**
400  * @filter `gravityview_entry_class` Modify the class applied to the entry row.
401  * @param string $class Existing class.
402  * @param array $entry Current entry being displayed
403  * @param \GravityView_View $this Current GravityView_View object
404  * @deprecated Use `gravityview/template/table/entry/class`
405  * @return string The modified class.
406  */
407  $class = apply_filters( 'gravityview_entry_class', $class, $entry->as_entry(), \GravityView_View::getInstance() );
408 
409  /**
410  * @filter `gravityview/template/table/entry/class` Modify the class aplied to the entry row.
411  * @param string $class The existing class.
412  * @param \GV\Template_Context The context.
413  * @return string The modified class.
414  */
415  return apply_filters( 'gravityview/template/table/entry/class', $class, Template_Context::from_template( $context->template, compact( 'entry' ) ) );
416  }
417 }
If this file is called directly, abort.
$url
Definition: post_image.php:25
static body_before( $context)
gravityview_table_body_before and gravityview/template/table/body/before actions. ...
static getInstance( $passed_post=NULL)
static _override_sorting_id_by_field_type( $sort_field_id, $form_id)
Override sorting per field.
__construct(View $view, Entry_Collection $entries, Request $request)
Constructor.
static add_columns_sort_links( $column_label, $context=null)
Add sorting links to HTML columns that support sorting.
If this file is called directly, abort.
$class
the_entry(\GV\Entry $entry, $attributes)
Output the entry row.
$entries
If this file is called directly, abort.
the_field(\GV\Field $field, \GV\Entry $entry)
Output a field cell.
static entry_class( $class, $entry, $context)
gravityview_entry_class and gravityview/template/table/entry/class filters.
get( $key, $default=null)
Retrieve a setting.
static load( $configuration)
Hydrates the legacy context globals as needed.
Definition: _mocks.php:546
If this file is called directly, abort.
static get_field_column_label( $field, $context=null)
Returns the label for a column, with support for all deprecated filters.
static tr_before( $context)
gravityview_table_tr_before and gravityview/template/table/tr/after actions.
If this file is called directly, abort.
If this file is called directly, abort.
static tr_after( $context)
gravityview_table_tr_after and gravityview/template/table/tr/after actions.
static body_after( $context)
gravityview_table_body_after and gravityview/template/table/body/after actions.
If this file is called directly, abort.
If this file is called directly, abort.
If this file is called directly, abort.
If this file is called directly, abort.
the_columns()
Output the table column names.
gravityview_field_output( $passed_args, $context=null)
Output field based on a certain html markup.
Definition: class-api.php:1272
$entry
Definition: notes.php:27
$field
Definition: gquiz_grade.php:11
static getInstance()
Get the one true instantiated self.