Completed
Push — master ( b62348...ed92ff )
by Stephanie
02:42
created

FrmEntriesListHelper::extra_tablenav()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 1
dl 0
loc 9
rs 9.6666
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
		$default_orderby = 'id';
15
		$default_order = 'DESC';
16
		$s_query = array();
17
18
		if ( $form_id ) {
19
			$s_query['it.form_id'] = $form_id;
20
			$join_form_in_query = false;
21
		} else {
22
			$s_query[] = array( 'or' => 1, 'parent_form_id' => null, 'parent_form_id <' => 1 );
23
			$join_form_in_query = true;
24
		}
25
26
		$s = isset( $_REQUEST['s'] ) ? stripslashes($_REQUEST['s']) : '';
27
28
	    if ( $s != '' && FrmAppHelper::pro_is_installed() ) {
29
	        $fid = isset( $_REQUEST['fid'] ) ? sanitize_title( $_REQUEST['fid'] ) : '';
30
	        $s_query = FrmProEntriesHelper::get_search_str( $s_query, $s, $form_id, $fid );
31
	    }
32
33
		$s_query = apply_filters( 'frm_entries_list_query', $s_query, compact( 'form_id' ) );
34
35
        $orderby = isset( $_REQUEST['orderby'] ) ? sanitize_title( $_REQUEST['orderby'] ) : $default_orderby;
36
        if ( strpos($orderby, 'meta') !== false ) {
37
            $order_field_type = FrmField::get_type( str_replace( 'meta_', '', $orderby ) );
38
			$orderby .= in_array( $order_field_type, array( 'number', 'scale' ) ) ? ' +0 ' : '';
39
        }
40
41
		$order = isset( $_REQUEST['order'] ) ? sanitize_title( $_REQUEST['order'] ) : $default_order;
42
		$order = ' ORDER BY ' . $orderby . ' ' . $order;
43
44
        $page = $this->get_pagenum();
45
		$start = (int) isset( $_REQUEST['start'] ) ? absint( $_REQUEST['start'] ) : ( ( $page - 1 ) * $per_page );
46
47
		$this->items = FrmEntry::getAll( $s_query, $order, ' LIMIT ' . $start . ',' . $per_page, 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 = isset( $_REQUEST['s'] ) ? $_REQUEST['s'] : '';
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( compact('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( $atts ) {
175
		$col_name = $this->column_name;
176
		$item = $atts['item'];
177
		$val = '';
0 ignored issues
show
Unused Code introduced by
$val is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
178
179
		switch ( $col_name ) {
180
			case 'ip':
181
			case 'id':
182
			case 'item_key':
183
				$val = $item->{$col_name};
184
				break;
185
			case 'name':
186
			case 'description':
187
				$val = FrmAppHelper::truncate( strip_tags( $item->{$col_name} ), 100 );
188
				break;
189
			case 'created_at':
190
			case 'updated_at':
191
				$date = FrmAppHelper::get_formatted_time( $item->{$col_name} );
192
				$val = '<abbr title="' . esc_attr( FrmAppHelper::get_formatted_time( $item->{$col_name}, '', 'g:i:s A' ) ) . '">' . $date . '</abbr>';
193
			break;
194
			case 'is_draft':
195
				$val = empty($item->is_draft) ? __( 'No' ) : __( 'Yes' );
196
			break;
197
			case 'form_id':
198
				$val = FrmFormsHelper::edit_form_link( $item->form_id );
199
			break;
200
			case 'post_id':
201
				$val = FrmAppHelper::post_edit_link( $item->post_id );
202
			break;
203
			case 'user_id':
204
				$user = get_userdata( $item->user_id );
205
				$val = $user ? $user->user_login : '';
206
			break;
207
			case 'parent_item_id':
208
				$val = $item->parent_item_id;
209
			break;
210
			default:
211
				$val = apply_filters( 'frm_entries_' . $col_name . '_column', false, compact( 'item' ) );
212
				if ( $val === false ) {
213
					$this->get_column_value( $item, $val );
214
				}
215
		}
216
217
		return $val;
218
	}
219
220
    /**
221
     * @param string $view_link
222
     */
223
    private function get_actions( &$actions, $item, $view_link ) {
224
		$actions['view'] = '<a href="' . esc_url( $view_link ) . '">' . __( 'View', 'formidable' ) . '</a>';
225
226 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...
227
			$delete_link = '?page=formidable-entries&frm_action=destroy&id=' . $item->id . '&form=' . $this->params['form'];
228
			$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...
229
	    }
230
231
        $actions = apply_filters('frm_row_actions', $actions, $item);
232
    }
233
234
	private function get_column_value( $item, &$val ) {
235
		$col_name = $this->column_name;
236
237
		if ( strpos( $col_name, 'frmsep_' ) === 0 ) {
238
			$sep_val = true;
239
			$col_name = str_replace( 'frmsep_', '', $col_name );
240
		} else {
241
			$sep_val = false;
242
		}
243
244
		if ( strpos( $col_name, '-_-' ) ) {
245
			list( $col_name, $embedded_field_id ) = explode( '-_-', $col_name );
246
		}
247
248
		$field = FrmField::getOne( $col_name );
249
		if ( ! $field ) {
250
			return;
251
		}
252
253
		$atts = array(
254
			'type' => $field->type, 'truncate' => true,
255
			'post_id' => $item->post_id, 'entry_id' => $item->id,
256
			'embedded_field_id' => 0,
257
		);
258
259
		if ( $sep_val ) {
260
			$atts['saved_value'] = true;
261
		}
262
263
		if ( isset( $embedded_field_id ) ) {
264
			$atts['embedded_field_id'] = $embedded_field_id;
265
			unset( $embedded_field_id );
266
		}
267
268
		$val = FrmEntriesHelper::prepare_display_value( $item, $field, $atts );
269
	}
270
}
271