Completed
Push — master ( ddcef8...c695e0 )
by Stephanie
43s queued 10s
created

FrmEntriesListHelper::no_items()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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