Completed
Push — master ( 02cc92...6df54b )
by Stephanie
02:42
created

FrmEntriesListHelper::prepare_items()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 47
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 30
nc 12
nop 0
dl 0
loc 47
rs 8.5125
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( 'or' => 1, 'parent_form_id' => null, 'parent_form_id <' => 1 );
21
			$join_form_in_query = true;
22
		}
23
24
		$s = self::get_param( array( 'param' => 's', 'sanitize' => 'sanitize_text_field' ) );
25
26
		if ( $s != '' && FrmAppHelper::pro_is_installed() ) {
27
			$fid = self::get_param( array( 'param' => 'fid' ) );
28
			$s_query = FrmProEntriesHelper::get_search_str( $s_query, $s, $form_id, $fid );
29
		}
30
31
		$s_query = apply_filters( 'frm_entries_list_query', $s_query, compact( 'form_id' ) );
32
33
		$orderby = self::get_param( array( 'param' => 'orderby', 'default' => 'id' ) );
34
35
		if ( strpos( $orderby, 'meta' ) !== false ) {
36
			$order_field_type = FrmField::get_type( str_replace( 'meta_', '', $orderby ) );
37
			$orderby .= in_array( $order_field_type, array( 'number', 'scale' ) ) ? ' +0 ' : '';
38
		}
39
40
		$order = self::get_param( array( 'param' => 'order', 'default' => 'DESC' ) );
41
		$order = FrmDb::esc_order( $orderby . ' ' . $order );
42
43
		$page = $this->get_pagenum();
44
		$start = (int) self::get_param( array( 'param' => 'start', 'default' => ( ( $page - 1 ) * $per_page ) ) );
45
46
		$limit = FrmDb::esc_limit( $start . ',' . $per_page );
47
		$this->items = FrmEntry::getAll( $s_query, $order, $limit, true, $join_form_in_query );
48
        $total_items = FrmEntry::getRecordCount($s_query);
49
50
		$this->set_pagination_args( array(
51
			'total_items' => $total_items,
52
			'per_page' => $per_page,
53
		) );
54
	}
55
56
	public function no_items() {
57
		$s = self::get_param( array( 'param' => 's', 'sanitize' => 'sanitize_text_field' ) );
58
	    if ( ! empty($s) ) {
59
            _e( 'No Entries Found', 'formidable' );
60
            return;
61
        }
62
63
		$form_id = $this->params['form'];
64
		$form = $this->params['form'];
65
66
        if ( $form_id ) {
67
            $form = FrmForm::getOne($form_id);
68
        }
69
        $colspan = $this->get_column_count();
70
71
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/no_entries.php' );
72
	}
73
74
	public function search_box( $text, $input_id ) {
75
		// Searching is a pro feature
76
	}
77
78
	protected function extra_tablenav( $which ) {
79
		$form_id = FrmAppHelper::simple_get( 'form', 'absint' );
80
		if ( $which == 'top' && empty( $form_id ) ) {
81
			echo '<div class="alignleft actions">';
82
			echo FrmFormsHelper::forms_dropdown( 'form', $form_id, array( 'blank' => __( 'View all forms', 'formidable' ) ) );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'FrmFormsHelper'
Loading history...
83
			submit_button( __( 'Filter' ), 'filter_action', '', false, array( 'id' => 'post-query-submit' ) );
84
			echo '</div>';
85
		}
86
	}
87
88
	/**
89
	* Gets the name of the primary column in the Entries screen
90
	*
91
	* @since 2.0.14
92
	*
93
	* @return string $primary_column
94
	*/
95
	protected function get_primary_column_name() {
96
		$columns = get_column_headers( $this->screen );
97
		$hidden = get_hidden_columns( $this->screen );
98
99
		$primary_column = '';
100
101
		foreach ( $columns as $column_key => $column_display_name ) {
102
			if ( 'cb' != $column_key && ! in_array( $column_key, $hidden ) ) {
103
				$primary_column = $column_key;
104
				break;
105
			}
106
		}
107
108
		return $primary_column;
109
	}
110
111
	public function single_row( $item, $style = '' ) {
112
		// Set up the hover actions for this user
113
		$actions = array();
114
		$view_link = '?page=formidable-entries&frm_action=show&id=' . $item->id;
115
116
		$this->get_actions( $actions, $item, $view_link );
117
118
        $action_links = $this->row_actions( $actions );
119
120
		// Set up the checkbox ( because the user is editable, otherwise its empty )
121
		$checkbox = "<input type='checkbox' name='item-action[]' id='cb-item-action-{$item->id}' value='{$item->id}' />";
122
123
		$r = "<tr id='item-action-{$item->id}'$style>";
124
125
		list( $columns, $hidden, , $primary ) = $this->get_column_info();
126
        $action_col = false;
127
128
		foreach ( $columns as $column_name => $column_display_name ) {
129
			$class = $column_name . ' column-' . $column_name;
130
131
			if ( $column_name === $primary ) {
132
				$class .= ' column-primary';
133
			}
134
135
			if ( in_array( $column_name, $hidden ) ) {
136
				$class .= ' frm_hidden';
137
			} else if ( ! $action_col && ! in_array( $column_name, array( 'cb', 'id', 'form_id', 'post_id' ) ) ) {
138
			    $action_col = $column_name;
139
            }
140
141
			$attributes = 'class="' . esc_attr( $class ) . '"';
142
			unset($class);
143
			$attributes .= ' data-colname="' . $column_display_name . '"';
144
145
			$form_id = $this->params['form'] ? $this->params['form'] : 0;
146
			$this->column_name = preg_replace( '/^(' . $form_id . '_)/', '', $column_name );
147
148
			if ( $this->column_name == 'cb' ) {
149
				$r .= "<th scope='row' class='check-column'>$checkbox</th>";
150
			} else {
151
				if ( in_array( $column_name, $hidden ) ) {
152
					$val = '';
153
				} else {
154
					$val = $this->column_value( $item );
155
				}
156
157
			    $r .= "<td $attributes>";
158
				if ( $column_name == $action_col ) {
159
					$edit_link = '?page=formidable-entries&frm_action=edit&id=' . $item->id;
160
					$r .= '<a href="' . esc_url( isset( $actions['edit'] ) ? $edit_link : $view_link ) . '" class="row-title" >' . $val . '</a> ';
161
			        $r .= $action_links;
162
				} else {
163
			        $r .= $val;
164
			    }
165
			    $r .= '</td>';
166
			}
167
			unset($val);
168
		}
169
		$r .= '</tr>';
170
171
		return $r;
172
	}
173
174
	private function column_value( $item ) {
175
		$col_name = $this->column_name;
176
177
		switch ( $col_name ) {
178
			case 'ip':
179
			case 'id':
180
			case 'item_key':
181
				$val = $item->{$col_name};
182
				break;
183
			case 'name':
184
			case 'description':
185
				$val = FrmAppHelper::truncate( strip_tags( $item->{$col_name} ), 100 );
186
				break;
187
			case 'created_at':
188
			case 'updated_at':
189
				$date = FrmAppHelper::get_formatted_time( $item->{$col_name} );
190
				$val = '<abbr title="' . esc_attr( FrmAppHelper::get_formatted_time( $item->{$col_name}, '', 'g:i:s A' ) ) . '">' . $date . '</abbr>';
191
			break;
192
			case 'is_draft':
193
				$val = empty( $item->is_draft ) ? __( 'No' ) : __( 'Yes' );
194
			break;
195
			case 'form_id':
196
				$val = FrmFormsHelper::edit_form_link( $item->form_id );
197
			break;
198
			case 'post_id':
199
				$val = FrmAppHelper::post_edit_link( $item->post_id );
200
			break;
201
			case 'user_id':
202
				$user = get_userdata( $item->user_id );
203
				$val = $user ? $user->user_login : '';
204
			break;
205
			case 'parent_item_id':
206
				$val = $item->parent_item_id;
207
			break;
208
			default:
209
				$val = apply_filters( 'frm_entries_' . $col_name . '_column', false, compact( 'item' ) );
210
				if ( $val === false ) {
211
					$this->get_column_value( $item, $val );
212
				}
213
		}
214
215
		return $val;
216
	}
217
218
    /**
219
     * @param string $view_link
220
     */
221
    private function get_actions( &$actions, $item, $view_link ) {
222
		$actions['view'] = '<a href="' . esc_url( $view_link ) . '">' . __( 'View', 'formidable' ) . '</a>';
223
224 View Code Duplication
        if ( current_user_can('frm_delete_entries') ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
225
			$delete_link = '?page=formidable-entries&frm_action=destroy&id=' . $item->id . '&form=' . $this->params['form'];
226
			$actions['delete'] = '<a href="' . esc_url( wp_nonce_url( $delete_link ) ) . '" class="submitdelete" onclick="return confirm(\'' . esc_attr( __( 'Are you sure you want to delete that?', 'formidable' ) ) . '\')">' . __( 'Delete' ) . '</a>';
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
227
	    }
228
229
        $actions = apply_filters('frm_row_actions', $actions, $item);
230
    }
231
232
	private function get_column_value( $item, &$val ) {
233
		$col_name = $this->column_name;
234
235
		if ( strpos( $col_name, 'frmsep_' ) === 0 ) {
236
			$sep_val = true;
237
			$col_name = str_replace( 'frmsep_', '', $col_name );
238
		} else {
239
			$sep_val = false;
240
		}
241
242
		if ( strpos( $col_name, '-_-' ) ) {
243
			list( $col_name, $embedded_field_id ) = explode( '-_-', $col_name );
244
		}
245
246
		$field = FrmField::getOne( $col_name );
247
		if ( ! $field ) {
248
			return;
249
		}
250
251
		$atts = array(
252
			'type' => $field->type, 'truncate' => true,
253
			'post_id' => $item->post_id, 'entry_id' => $item->id,
254
			'embedded_field_id' => 0,
255
		);
256
257
		if ( $sep_val ) {
258
			$atts['saved_value'] = true;
259
		}
260
261
		if ( isset( $embedded_field_id ) ) {
262
			$atts['embedded_field_id'] = $embedded_field_id;
263
			unset( $embedded_field_id );
264
		}
265
266
		$val = FrmEntriesHelper::prepare_display_value( $item, $field, $atts );
267
	}
268
}
269