Completed
Branch uploads (08d8c9)
by Stephanie
04:20
created

FrmEntriesListHelper::search_box()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
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
		}
21
22
		$s = isset( $_REQUEST['s'] ) ? stripslashes($_REQUEST['s']) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
23
24
	    if ( $s != '' && FrmAppHelper::pro_is_installed() ) {
25
	        $fid = isset( $_REQUEST['fid'] ) ? sanitize_title( $_REQUEST['fid'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
26
	        $s_query = FrmProEntriesHelper::get_search_str( $s_query, $s, $form_id, $fid );
27
	    }
28
29
        $orderby = isset( $_REQUEST['orderby'] ) ? sanitize_title( $_REQUEST['orderby'] ) : $default_orderby;
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
30
        if ( strpos($orderby, 'meta') !== false ) {
31
            $order_field_type = FrmField::get_type( str_replace( 'meta_', '', $orderby ) );
32
			$orderby .= in_array( $order_field_type, array( 'number', 'scale' ) ) ? ' +0 ' : '';
33
        }
34
35
		$order = isset( $_REQUEST['order'] ) ? sanitize_title( $_REQUEST['order'] ) : $default_order;
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
36
		$order = ' ORDER BY ' . $orderby . ' ' . $order;
37
38
        $page = $this->get_pagenum();
39
		$start = (int) isset( $_REQUEST['start'] ) ? absint( $_REQUEST['start'] ) : ( ( $page - 1 ) * $per_page );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
40
41
		$this->items = FrmEntry::getAll( $s_query, $order, ' LIMIT ' . $start . ',' . $per_page, true, false );
42
        $total_items = FrmEntry::getRecordCount($s_query);
43
44
		$this->set_pagination_args( array(
45
			'total_items' => $total_items,
46
			'per_page' => $per_page,
47
		) );
48
	}
49
50
	public function no_items() {
51
        $s = isset( $_REQUEST['s'] ) ? $_REQUEST['s'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
52
	    if ( ! empty($s) ) {
53
            _e( 'No Entries Found', 'formidable' );
54
            return;
55
        }
56
57
        $form_id = $form = $this->params['form'];
58
        if ( $form_id ) {
59
            $form = FrmForm::getOne($form_id);
60
        }
61
        $colspan = $this->get_column_count();
62
63
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/no_entries.php' );
64
	}
65
66
	public function search_box( $text, $input_id ) {
67
		// Searching is a pro feature
68
	}
69
70
	/**
71
	* Gets the name of the primary column in the Entries screen
72
	*
73
	* @since 2.0.14
74
	*
75
	* @return string $primary_column
76
	*/
77
	protected function get_primary_column_name() {
78
		$columns = get_column_headers( $this->screen );
79
		$hidden = get_hidden_columns( $this->screen );
80
81
		$primary_column = '';
82
83
		foreach ( $columns as $column_key => $column_display_name ) {
84
			if ( 'cb' != $column_key && ! in_array( $column_key, $hidden ) ) {
85
				$primary_column = $column_key;
86
				break;
87
			}
88
		}
89
90
		return $primary_column;
91
	}
92
93
	public function single_row( $item, $style = '' ) {
94
		// Set up the hover actions for this user
95
		$actions = array();
96
		$view_link = '?page=formidable-entries&frm_action=show&id=' . $item->id;
97
98
		$this->get_actions( $actions, $item, $view_link );
99
100
        $action_links = $this->row_actions( $actions );
101
102
		// Set up the checkbox ( because the user is editable, otherwise its empty )
103
		$checkbox = "<input type='checkbox' name='item-action[]' id='cb-item-action-{$item->id}' value='{$item->id}' />";
104
105
		$r = "<tr id='item-action-{$item->id}'$style>";
106
107
		list( $columns, $hidden, , $primary ) = $this->get_column_info();
108
        $action_col = false;
109
110
		foreach ( $columns as $column_name => $column_display_name ) {
111
			$class = $column_name . ' column-' . $column_name;
112
113
			if ( $column_name === $primary ) {
114
				$class .= ' column-primary';
115
			}
116
117
			if ( in_array( $column_name, $hidden ) ) {
118
				$class .= ' frm_hidden';
119
			} else if ( ! $action_col && ! in_array( $column_name, array( 'cb', 'id', 'form_id', 'post_id' ) ) ) {
120
			    $action_col = $column_name;
121
            }
122
123
			$attributes = 'class="' . esc_attr( $class ) . '"';
124
			unset($class);
125
			$attributes .= ' data-colname="' . $column_display_name . '"';
126
127
			$col_name = preg_replace( '/^(' . $this->params['form'] . '_)/', '', $column_name );
128
			$this->column_name = $col_name;
129
130
			switch ( $col_name ) {
131
				case 'cb':
132
					$r .= "<th scope='row' class='check-column'>$checkbox</th>";
133
					break;
134
				case 'ip':
135
				case 'id':
136
				case 'item_key':
137
				    $val = $item->{$col_name};
138
				    break;
139
				case 'name':
140
				case 'description':
141
				    $val = FrmAppHelper::truncate(strip_tags($item->{$col_name}), 100);
142
				    break;
143
				case 'created_at':
144
				case 'updated_at':
145
				    $date = FrmAppHelper::get_formatted_time($item->{$col_name});
146
					$val = '<abbr title="' . esc_attr( FrmAppHelper::get_formatted_time( $item->{$col_name}, '', 'g:i:s A' ) ) . '">' . $date . '</abbr>';
147
					break;
148
				case 'is_draft':
149
				    $val = empty($item->is_draft) ? __( 'No') : __( 'Yes');
150
			        break;
151
				case 'form_id':
152
				    $val = FrmFormsHelper::edit_form_link($item->form_id);
153
    				break;
154
				case 'post_id':
155
				    $val = FrmAppHelper::post_edit_link($item->post_id);
156
				    break;
157
				case 'user_id':
158
				    $user = get_userdata($item->user_id);
159
				    $val = $user->user_login;
160
				    break;
161
				default:
162
					$val = apply_filters( 'frm_entries_' . $col_name . '_column', false, compact( 'item' ) );
163
					if ( $val === false ) {
164
						$this->get_column_value( $item, $val );
165
					}
166
				break;
167
			}
168
169
			if ( isset( $val ) ) {
170
			    $r .= "<td $attributes>";
171
				if ( $column_name == $action_col ) {
172
					$edit_link = '?page=formidable-entries&frm_action=edit&id=' . $item->id;
173
					$r .= '<a href="' . esc_url( isset( $actions['edit'] ) ? $edit_link : $view_link ) . '" class="row-title" >' . $val . '</a> ';
174
			        $r .= $action_links;
175
				} else {
176
			        $r .= $val;
177
			    }
178
			    $r .= '</td>';
179
			}
180
			unset($val);
181
		}
182
		$r .= '</tr>';
183
184
		return $r;
185
	}
186
187
    /**
188
     * @param string $view_link
189
     */
190
    private function get_actions( &$actions, $item, $view_link ) {
191
		$actions['view'] = '<a href="' . esc_url( $view_link ) . '">' . __( 'View', 'formidable' ) . '</a>';
192
193 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...
194
			$delete_link = '?page=formidable-entries&frm_action=destroy&id=' . $item->id . '&form=' . $this->params['form'];
195
			$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...
196
	    }
197
198
        $actions = apply_filters('frm_row_actions', $actions, $item);
199
    }
200
201
	private function get_column_value( $item, &$val ) {
202
		$col_name = $this->column_name;
203
204
		if ( strpos( $col_name, 'frmsep_' ) === 0 ) {
205
			$sep_val = true;
206
			$col_name = str_replace( 'frmsep_', '', $col_name );
207
		} else {
208
			$sep_val = false;
209
		}
210
211
		if ( strpos( $col_name, '-_-' ) ) {
212
			list( $col_name, $embedded_field_id ) = explode( '-_-', $col_name );
213
		}
214
215
		$field = FrmField::getOne( $col_name );
216
		if ( ! $field ) {
217
			return;
218
		}
219
220
		$atts = array(
221
			'type' => $field->type, 'truncate' => true,
222
			'post_id' => $item->post_id, 'entry_id' => $item->id,
223
			'embedded_field_id' => 0,
224
		);
225
226
		if ( $sep_val ) {
227
			$atts['saved_value'] = true;
228
		}
229
230
		if ( isset( $embedded_field_id ) ) {
231
			$atts['embedded_field_id'] = $embedded_field_id;
232
			unset( $embedded_field_id );
233
		}
234
235
		$val = FrmEntriesHelper::prepare_display_value( $item, $field, $atts );
236
	}
237
}
238