Completed
Push — master ( feb49b...2e7061 )
by Stephanie
06:17 queued 03:03
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
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' );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
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 extra_tablenav( $which ) {
118
		$form_id = FrmAppHelper::simple_get( 'form', 'absint' );
119
		if ( $which == 'top' && empty( $form_id ) ) {
120
			echo '<div class="alignleft actions">';
121
122
			// Override the referrer to prevent it from being used for the screen options.
123
			echo '<input type="hidden" name="_wp_http_referer" value="" />';
124
125
			echo FrmFormsHelper::forms_dropdown( 'form', $form_id, array( 'blank' => __( 'View all forms', 'formidable' ) ) ); // WPCS: XSS ok.
126
			submit_button( __( 'Filter', 'formidable' ), 'filter_action action', '', false, array( 'id' => 'post-query-submit' ) );
127
			echo '</div>';
128
		}
129
	}
130
131
	/**
132
	 * Gets the name of the primary column in the Entries screen
133
	 *
134
	 * @since 2.0.14
135
	 *
136
	 * @return string $primary_column
137
	 */
138
	protected function get_primary_column_name() {
139
		$columns = get_column_headers( $this->screen );
140
		$hidden  = get_hidden_columns( $this->screen );
141
142
		$primary_column = '';
143
144
		foreach ( $columns as $column_key => $column_display_name ) {
145
			if ( 'cb' != $column_key && ! in_array( $column_key, $hidden ) ) {
146
				$primary_column = $column_key;
147
				break;
148
			}
149
		}
150
151
		return $primary_column;
152
	}
153
154
	public function single_row( $item, $style = '' ) {
155
		// Set up the hover actions for this user
156
		$actions   = array();
157
		$view_link = '?page=formidable-entries&frm_action=show&id=' . $item->id;
158
159
		$this->get_actions( $actions, $item, $view_link );
160
161
		$action_links = $this->row_actions( $actions );
162
163
		// Set up the checkbox ( because the user is editable, otherwise its empty )
164
		$checkbox = "<input type='checkbox' name='item-action[]' id='cb-item-action-{$item->id}' value='{$item->id}' />";
165
166
		$r = "<tr id='item-action-{$item->id}'$style>";
167
168
		list( $columns, $hidden, , $primary ) = $this->get_column_info();
169
		$action_col = false;
170
171
		foreach ( $columns as $column_name => $column_display_name ) {
172
			$class = $column_name . ' column-' . $column_name;
173
174
			if ( $column_name === $primary ) {
175
				$class .= ' column-primary';
176
			}
177
178
			if ( in_array( $column_name, $hidden ) ) {
179
				$class .= ' frm_hidden';
180
			} elseif ( ! $action_col && ! in_array( $column_name, array( 'cb', 'id', 'form_id', 'post_id' ) ) ) {
181
				$action_col = $column_name;
182
			}
183
184
			$attributes = 'class="' . esc_attr( $class ) . '"';
185
			unset( $class );
186
			$attributes .= ' data-colname="' . $column_display_name . '"';
187
188
			$form_id           = $this->params['form'] ? $this->params['form'] : 0;
189
			$this->column_name = preg_replace( '/^(' . $form_id . '_)/', '', $column_name );
190
191
			if ( $this->column_name == 'cb' ) {
192
				$r .= "<th scope='row' class='check-column'>$checkbox</th>";
193
			} else {
194
				if ( in_array( $column_name, $hidden ) ) {
195
					$val = '';
196
				} else {
197
					$val = $this->column_value( $item );
198
				}
199
200
				$r .= "<td $attributes>";
201
				if ( $column_name == $action_col ) {
202
					$edit_link = FrmAppHelper::maybe_full_screen_link( '?page=formidable-entries&frm_action=edit&id=' . $item->id );
203
					$r         .= '<a href="' . esc_url( isset( $actions['edit'] ) ? $edit_link : $view_link ) . '" class="row-title" >' . $val . '</a> ';
204
					$r         .= $action_links;
205
				} else {
206
					$r .= $val;
207
				}
208
				$r .= '</td>';
209
			}
210
			unset( $val );
211
		}
212
		$r .= '</tr>';
213
214
		return $r;
215
	}
216
217
	private function column_value( $item ) {
218
		$col_name = $this->column_name;
219
220
		switch ( $col_name ) {
221
			case 'ip':
222
			case 'id':
223
			case 'item_key':
224
				$val = $item->{$col_name};
225
				break;
226
			case 'name':
227
			case 'description':
228
				$val = FrmAppHelper::truncate( strip_tags( $item->{$col_name} ), 100 );
229
				break;
230
			case 'created_at':
231
			case 'updated_at':
232
				$date = FrmAppHelper::get_formatted_time( $item->{$col_name} );
233
				$val  = '<abbr title="' . esc_attr( FrmAppHelper::get_formatted_time( $item->{$col_name}, '', 'g:i:s A' ) ) . '">' . $date . '</abbr>';
234
				break;
235
			case 'is_draft':
236
				$val = empty( $item->is_draft ) ? esc_html__( 'No', 'formidable' ) : esc_html__( 'Yes', 'formidable' );
237
				break;
238
			case 'form_id':
239
				$val = FrmFormsHelper::edit_form_link( $item->form_id );
240
				break;
241
			case 'post_id':
242
				$val = FrmAppHelper::post_edit_link( $item->post_id );
243
				break;
244
			case 'user_id':
245
				$user = get_userdata( $item->user_id );
246
				$val  = $user ? $user->user_login : '';
247
				break;
248
			case 'parent_item_id':
249
				$val = $item->parent_item_id;
250
				break;
251
			default:
252
				$val = apply_filters( 'frm_entries_' . $col_name . '_column', false, compact( 'item' ) );
253
				if ( $val === false ) {
254
					$this->get_column_value( $item, $val );
255
				}
256
		}
257
258
		return $val;
259
	}
260
261
	/**
262
	 * @param string $view_link
263
	 */
264
	private function get_actions( &$actions, $item, $view_link ) {
265
		$view_link = FrmAppHelper::maybe_full_screen_link( $view_link );
266
		$actions['view'] = '<a href="' . esc_url( $view_link ) . '">' . __( 'View', 'formidable' ) . '</a>';
267
268
		if ( current_user_can( 'frm_delete_entries' ) ) {
269
			$delete_link       = '?page=formidable-entries&frm_action=destroy&id=' . $item->id . '&form=' . $this->params['form'];
270
			$delete_link       = FrmAppHelper::maybe_full_screen_link( $delete_link );
271
			$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>';
272
		}
273
274
		$actions = apply_filters( 'frm_row_actions', $actions, $item );
275
	}
276
277
	private function get_column_value( $item, &$val ) {
278
		$col_name = $this->column_name;
279
280
		if ( strpos( $col_name, 'frmsep_' ) === 0 ) {
281
			$sep_val  = true;
282
			$col_name = str_replace( 'frmsep_', '', $col_name );
283
		} else {
284
			$sep_val = false;
285
		}
286
287
		if ( strpos( $col_name, '-_-' ) ) {
288
			list( $col_name, $embedded_field_id ) = explode( '-_-', $col_name );
289
		}
290
291
		$field = FrmField::getOne( $col_name );
292
		if ( ! $field ) {
293
			return;
294
		}
295
296
		$atts = array(
297
			'type'              => $field->type,
298
			'truncate'          => true,
299
			'post_id'           => $item->post_id,
300
			'entry_id'          => $item->id,
301
			'embedded_field_id' => 0,
302
		);
303
304
		if ( $sep_val ) {
305
			$atts['saved_value'] = true;
306
		}
307
308
		if ( isset( $embedded_field_id ) ) {
309
			$atts['embedded_field_id'] = $embedded_field_id;
310
			unset( $embedded_field_id );
311
		}
312
313
		$val = FrmEntriesHelper::prepare_display_value( $item, $field, $atts );
314
	}
315
}
316