Completed
Push — master ( e127fe...023e44 )
by Stephanie
02:22
created

FrmEntriesListHelper::display_tablenav()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
class FrmEntriesListHelper extends FrmListHelper {
4
	protected $column_name;
5
	protected $item;
6
	protected $field;
7
8
	public function prepare_items() {
9
		global $per_page;
10
11
		$per_page = $this->get_items_per_page( 'formidable_page_formidable_entries_per_page' );
12
		$form_id  = $this->params['form'];
13
14
		$s_query = array();
15
16
		if ( $form_id ) {
17
			$s_query['it.form_id'] = $form_id;
18
			$join_form_in_query    = false;
19
		} else {
20
			$s_query[]          = array(
21
				'or'               => 1,
22
				'parent_form_id'   => null,
23
				'parent_form_id <' => 1,
24
			);
25
			$join_form_in_query = true;
26
		}
27
28
		$s = self::get_param(
29
			array(
30
				'param'    => 's',
31
				'sanitize' => 'sanitize_text_field',
32
			)
33
		);
34
35
		if ( $s != '' && FrmAppHelper::pro_is_installed() ) {
36
			$fid     = self::get_param( array( 'param' => 'fid' ) );
37
			$s_query = FrmProEntriesHelper::get_search_str( $s_query, $s, $form_id, $fid );
38
		}
39
40
		$s_query = apply_filters( 'frm_entries_list_query', $s_query, compact( 'form_id' ) );
41
42
		$orderby = self::get_param(
43
			array(
44
				'param'   => 'orderby',
45
				'default' => 'id',
46
			)
47
		);
48
49
		if ( strpos( $orderby, 'meta' ) !== false ) {
50
			$order_field_type = FrmField::get_type( str_replace( 'meta_', '', $orderby ) );
51
			$orderby          .= in_array( $order_field_type, array( 'number', 'scale', 'star' ) ) ? '+0' : '';
52
		}
53
54
		$order = self::get_param(
55
			array(
56
				'param'   => 'order',
57
				'default' => 'DESC',
58
			)
59
		);
60
		$order = FrmDb::esc_order( $orderby . ' ' . $order );
61
62
		$page  = $this->get_pagenum();
63
		$start = (int) self::get_param(
64
			array(
65
				'param'   => 'start',
66
				'default' => ( $page - 1 ) * $per_page,
67
			)
68
		);
69
70
		$limit       = FrmDb::esc_limit( $start . ',' . $per_page );
71
		$this->items = FrmEntry::getAll( $s_query, $order, $limit, true, $join_form_in_query );
72
		$total_items = FrmEntry::getRecordCount( $s_query );
73
74
		$this->set_pagination_args(
75
			array(
76
				'total_items' => $total_items,
77
				'per_page'    => $per_page,
78
			)
79
		);
80
	}
81
82
	public function no_items() {
83
		$s = self::get_param(
84
			array(
85
				'param'    => 's',
86
				'sanitize' => 'sanitize_text_field',
87
			)
88
		);
89
		if ( ! empty( $s ) ) {
90
			esc_html_e( 'No Entries Found', 'formidable' );
91
92
			return;
93
		}
94
95
		$form_id = $this->params['form'];
96
		$form    = $this->params['form'];
97
98
		if ( $form_id ) {
99
			$form = FrmForm::getOne( $form_id );
100
		}
101
		$has_form = ! empty( $form );
102
103
		if ( ! $has_form ) {
104
			$has_form = FrmForm::getAll( array(), '', 1 );
105
			$has_form = ! empty( $has_form );
106
		}
107
108
		$colspan = $this->get_column_count();
109
110
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/no_entries.php' );
111
	}
112
113
	public function search_box( $text, $input_id ) {
114
		// Searching is a pro feature
115
	}
116
117
	protected function display_tablenav( $which ) {
118
		$is_footer = ( $which !== 'top' );
119
		if ( $is_footer && ! empty( $this->items ) ) {
120
			?>
121
			<p>
122
				<?php esc_html_e( 'Getting spam form submissions?', 'formidable' ); ?>
123
				<a href="https://formidableforms.com/knowledgebase/add-spam-protection/" target="_blank">
124
					<?php esc_html_e( 'Learn how to prevent them.', 'formidable' ); ?>
125
				</a>
126
			</p>
127
			<?php
128
		}
129
		parent::display_tablenav( $which );
130
	}
131
132
	protected function extra_tablenav( $which ) {
133
		$form_id = FrmAppHelper::simple_get( 'form', 'absint' );
134
		if ( $which == 'top' && empty( $form_id ) ) {
135
			echo '<div class="alignleft actions">';
136
137
			// Override the referrer to prevent it from being used for the screen options.
138
			echo '<input type="hidden" name="_wp_http_referer" value="" />';
139
140
			echo FrmFormsHelper::forms_dropdown( 'form', $form_id, array( 'blank' => __( 'View all forms', 'formidable' ) ) ); // WPCS: XSS ok.
141
			submit_button( __( 'Filter', 'formidable' ), 'filter_action action', '', false, array( 'id' => 'post-query-submit' ) );
142
			echo '</div>';
143
		}
144
	}
145
146
	/**
147
	 * Gets the name of the primary column in the Entries screen
148
	 *
149
	 * @since 2.0.14
150
	 *
151
	 * @return string $primary_column
152
	 */
153
	protected function get_primary_column_name() {
154
		$columns = get_column_headers( $this->screen );
155
		$hidden  = get_hidden_columns( $this->screen );
156
157
		$primary_column = '';
158
159
		foreach ( $columns as $column_key => $column_display_name ) {
160
			if ( 'cb' != $column_key && ! in_array( $column_key, $hidden ) ) {
161
				$primary_column = $column_key;
162
				break;
163
			}
164
		}
165
166
		return $primary_column;
167
	}
168
169
	public function single_row( $item, $style = '' ) {
170
		// Set up the hover actions for this user
171
		$actions   = array();
172
		$view_link = '?page=formidable-entries&frm_action=show&id=' . $item->id;
173
174
		$this->get_actions( $actions, $item, $view_link );
175
176
		$action_links = $this->row_actions( $actions );
177
178
		// Set up the checkbox ( because the user is editable, otherwise its empty )
179
		$checkbox = "<input type='checkbox' name='item-action[]' id='cb-item-action-{$item->id}' value='{$item->id}' />";
180
181
		$r = "<tr id='item-action-{$item->id}'$style>";
182
183
		list( $columns, $hidden, , $primary ) = $this->get_column_info();
184
		$action_col = false;
185
186
		foreach ( $columns as $column_name => $column_display_name ) {
187
			$class = $column_name . ' column-' . $column_name;
188
189
			if ( $column_name === $primary ) {
190
				$class .= ' column-primary';
191
			}
192
193
			if ( in_array( $column_name, $hidden ) ) {
194
				$class .= ' frm_hidden';
195
			} elseif ( ! $action_col && ! in_array( $column_name, array( 'cb', 'id', 'form_id', 'post_id' ) ) ) {
196
				$action_col = $column_name;
197
			}
198
199
			$attributes = 'class="' . esc_attr( $class ) . '"';
200
			unset( $class );
201
			$attributes .= ' data-colname="' . $column_display_name . '"';
202
203
			$form_id           = $this->params['form'] ? $this->params['form'] : 0;
204
			$this->column_name = preg_replace( '/^(' . $form_id . '_)/', '', $column_name );
205
206
			if ( $this->column_name == 'cb' ) {
207
				$r .= "<th scope='row' class='check-column'>$checkbox</th>";
208
			} else {
209
				if ( in_array( $column_name, $hidden ) ) {
210
					$val = '';
211
				} else {
212
					$val = $this->column_value( $item );
213
				}
214
215
				$r .= "<td $attributes>";
216
				if ( $column_name == $action_col ) {
217
					$edit_link = FrmAppHelper::maybe_full_screen_link( '?page=formidable-entries&frm_action=edit&id=' . $item->id );
218
					$r         .= '<a href="' . esc_url( isset( $actions['edit'] ) ? $edit_link : $view_link ) . '" class="row-title" >' . $val . '</a> ';
219
					$r         .= $action_links;
220
				} else {
221
					$r .= $val;
222
				}
223
				$r .= '</td>';
224
			}
225
			unset( $val );
226
		}
227
		$r .= '</tr>';
228
229
		return $r;
230
	}
231
232
	private function column_value( $item ) {
233
		$col_name = $this->column_name;
234
235
		switch ( $col_name ) {
236
			case 'ip':
237
			case 'id':
238
			case 'item_key':
239
				$val = $item->{$col_name};
240
				break;
241
			case 'name':
242
			case 'description':
243
				$val = FrmAppHelper::truncate( strip_tags( $item->{$col_name} ), 100 );
244
				break;
245
			case 'created_at':
246
			case 'updated_at':
247
				$date = FrmAppHelper::get_formatted_time( $item->{$col_name} );
248
				$val  = '<abbr title="' . esc_attr( FrmAppHelper::get_formatted_time( $item->{$col_name}, '', 'g:i:s A' ) ) . '">' . $date . '</abbr>';
249
				break;
250
			case 'is_draft':
251
				$val = empty( $item->is_draft ) ? esc_html__( 'No', 'formidable' ) : esc_html__( 'Yes', 'formidable' );
252
				break;
253
			case 'form_id':
254
				$val = FrmFormsHelper::edit_form_link( $item->form_id );
255
				break;
256
			case 'post_id':
257
				$val = FrmAppHelper::post_edit_link( $item->post_id );
258
				break;
259
			case 'user_id':
260
				$user = get_userdata( $item->user_id );
261
				$val  = $user ? $user->user_login : '';
262
				break;
263
			case 'parent_item_id':
264
				$val = $item->parent_item_id;
265
				break;
266
			default:
267
				$val = apply_filters( 'frm_entries_' . $col_name . '_column', false, compact( 'item' ) );
268
				if ( $val === false ) {
269
					$this->get_column_value( $item, $val );
270
				}
271
		}
272
273
		return $val;
274
	}
275
276
	/**
277
	 * @param string $view_link
278
	 */
279
	private function get_actions( &$actions, $item, $view_link ) {
280
		$view_link = FrmAppHelper::maybe_full_screen_link( $view_link );
281
		$actions['view'] = '<a href="' . esc_url( $view_link ) . '">' . __( 'View', 'formidable' ) . '</a>';
282
283
		if ( current_user_can( 'frm_delete_entries' ) ) {
284
			$delete_link       = '?page=formidable-entries&frm_action=destroy&id=' . $item->id . '&form=' . $this->params['form'];
285
			$delete_link       = FrmAppHelper::maybe_full_screen_link( $delete_link );
286
			$actions['delete'] = '<a href="' . esc_url( wp_nonce_url( $delete_link ) ) . '" class="submitdelete" data-frmverify="' . esc_attr__( 'Permanently delete this entry?', 'formidable' ) . '">' . __( 'Delete', 'formidable' ) . '</a>';
287
		}
288
289
		$actions = apply_filters( 'frm_row_actions', $actions, $item );
290
	}
291
292
	private function get_column_value( $item, &$val ) {
293
		$col_name = $this->column_name;
294
295
		if ( strpos( $col_name, 'frmsep_' ) === 0 ) {
296
			$sep_val  = true;
297
			$col_name = str_replace( 'frmsep_', '', $col_name );
298
		} else {
299
			$sep_val = false;
300
		}
301
302
		if ( strpos( $col_name, '-_-' ) ) {
303
			list( $col_name, $embedded_field_id ) = explode( '-_-', $col_name );
304
		}
305
306
		$field = FrmField::getOne( $col_name );
307
		if ( ! $field ) {
308
			return;
309
		}
310
311
		$atts = array(
312
			'type'              => $field->type,
313
			'truncate'          => true,
314
			'post_id'           => $item->post_id,
315
			'entry_id'          => $item->id,
316
			'embedded_field_id' => 0,
317
		);
318
319
		if ( $sep_val ) {
320
			$atts['saved_value'] = true;
321
		}
322
323
		if ( isset( $embedded_field_id ) ) {
324
			$atts['embedded_field_id'] = $embedded_field_id;
325
			unset( $embedded_field_id );
326
		}
327
328
		$val = FrmEntriesHelper::prepare_display_value( $item, $field, $atts );
329
	}
330
}
331