| 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' ); |
| 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( array( |
| 29 |
'param' => 's', |
| 30 |
'sanitize' => 'sanitize_text_field', |
| 31 |
) ); |
| 32 |
|
| 33 |
if ( $s != '' && FrmAppHelper::pro_is_installed() ) { |
| 34 |
$fid = self::get_param( array( 'param' => 'fid' ) ); |
| 35 |
$s_query = FrmProEntriesHelper::get_search_str( $s_query, $s, $form_id, $fid ); |
| 36 |
} |
| 37 |
|
| 38 |
$s_query = apply_filters( 'frm_entries_list_query', $s_query, compact( 'form_id' ) ); |
| 39 |
|
| 40 |
$orderby = self::get_param( array( |
| 41 |
'param' => 'orderby', |
| 42 |
'default' => 'id', |
| 43 |
) ); |
| 44 |
|
| 45 |
if ( strpos( $orderby, 'meta' ) !== false ) { |
| 46 |
$order_field_type = FrmField::get_type( str_replace( 'meta_', '', $orderby ) ); |
| 47 |
$orderby .= in_array( $order_field_type, array( 'number', 'scale' ) ) ? ' +0 ' : ''; |
| 48 |
} |
| 49 |
|
| 50 |
$order = self::get_param( array( |
| 51 |
'param' => 'order', |
| 52 |
'default' => 'DESC', |
| 53 |
) ); |
| 54 |
$order = FrmDb::esc_order( $orderby . ' ' . $order ); |
| 55 |
|
| 56 |
$page = $this->get_pagenum(); |
| 57 |
$start = (int) self::get_param( array( |
| 58 |
'param' => 'start', |
| 59 |
'default' => ( $page - 1 ) * $per_page, |
| 60 |
) ); |
| 61 |
|
| 62 |
$limit = FrmDb::esc_limit( $start . ',' . $per_page ); |
| 63 |
$this->items = FrmEntry::getAll( $s_query, $order, $limit, true, $join_form_in_query ); |
| 64 |
$total_items = FrmEntry::getRecordCount($s_query); |
| 65 |
|
| 66 |
$this->set_pagination_args( array( |
| 67 |
'total_items' => $total_items, |
| 68 |
'per_page' => $per_page, |
| 69 |
) ); |
| 70 |
} |
| 71 |
|
| 72 |
public function no_items() { |
| 73 |
$s = self::get_param( array( |
| 74 |
'param' => 's', |
| 75 |
'sanitize' => 'sanitize_text_field', |
| 76 |
) ); |
| 77 |
if ( ! empty($s) ) { |
| 78 |
esc_html_e( 'No Entries Found', 'formidable' ); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$form_id = $this->params['form']; |
| 83 |
$form = $this->params['form']; |
| 84 |
|
| 85 |
if ( $form_id ) { |
| 86 |
$form = FrmForm::getOne($form_id); |
| 87 |
} |
| 88 |
$colspan = $this->get_column_count(); |
| 89 |
|
| 90 |
include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/no_entries.php' ); |
| 91 |
} |
| 92 |
|
| 93 |
public function search_box( $text, $input_id ) { |
| 94 |
// Searching is a pro feature |
| 95 |
} |
| 96 |
|
| 97 |
protected function extra_tablenav( $which ) { |
| 98 |
$form_id = FrmAppHelper::simple_get( 'form', 'absint' ); |
| 99 |
if ( $which == 'top' && empty( $form_id ) ) { |
| 100 |
echo '<div class="alignleft actions">'; |
| 101 |
echo FrmFormsHelper::forms_dropdown( 'form', $form_id, array( 'blank' => __( 'View all forms', 'formidable' ) ) ); |
| 102 |
submit_button( __( 'Filter' ), 'filter_action', '', false, array( 'id' => 'post-query-submit' ) ); |
| 103 |
echo '</div>'; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Gets the name of the primary column in the Entries screen |
| 109 |
* |
| 110 |
* @since 2.0.14 |
| 111 |
* |
| 112 |
* @return string $primary_column |
| 113 |
*/ |
| 114 |
protected function get_primary_column_name() { |
| 115 |
$columns = get_column_headers( $this->screen ); |
| 116 |
$hidden = get_hidden_columns( $this->screen ); |
| 117 |
|
| 118 |
$primary_column = ''; |
| 119 |
|
| 120 |
foreach ( $columns as $column_key => $column_display_name ) { |
| 121 |
if ( 'cb' != $column_key && ! in_array( $column_key, $hidden ) ) { |
| 122 |
$primary_column = $column_key; |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return $primary_column; |
| 128 |
} |
| 129 |
|
| 130 |
public function single_row( $item, $style = '' ) { |
| 131 |
// Set up the hover actions for this user |
| 132 |
$actions = array(); |
| 133 |
$view_link = '?page=formidable-entries&frm_action=show&id=' . $item->id; |
| 134 |
|
| 135 |
$this->get_actions( $actions, $item, $view_link ); |
| 136 |
|
| 137 |
$action_links = $this->row_actions( $actions ); |
| 138 |
|
| 139 |
// Set up the checkbox ( because the user is editable, otherwise its empty ) |
| 140 |
$checkbox = "<input type='checkbox' name='item-action[]' id='cb-item-action-{$item->id}' value='{$item->id}' />"; |
| 141 |
|
| 142 |
$r = "<tr id='item-action-{$item->id}'$style>"; |
| 143 |
|
| 144 |
list( $columns, $hidden, , $primary ) = $this->get_column_info(); |
| 145 |
$action_col = false; |
| 146 |
|
| 147 |
foreach ( $columns as $column_name => $column_display_name ) { |
| 148 |
$class = $column_name . ' column-' . $column_name; |
| 149 |
|
| 150 |
if ( $column_name === $primary ) { |
| 151 |
$class .= ' column-primary'; |
| 152 |
} |
| 153 |
|
| 154 |
if ( in_array( $column_name, $hidden ) ) { |
| 155 |
$class .= ' frm_hidden'; |
| 156 |
} else if ( ! $action_col && ! in_array( $column_name, array( 'cb', 'id', 'form_id', 'post_id' ) ) ) { |
| 157 |
$action_col = $column_name; |
| 158 |
} |
| 159 |
|
| 160 |
$attributes = 'class="' . esc_attr( $class ) . '"'; |
| 161 |
unset($class); |
| 162 |
$attributes .= ' data-colname="' . $column_display_name . '"'; |
| 163 |
|
| 164 |
$form_id = $this->params['form'] ? $this->params['form'] : 0; |
| 165 |
$this->column_name = preg_replace( '/^(' . $form_id . '_)/', '', $column_name ); |
| 166 |
|
| 167 |
if ( $this->column_name == 'cb' ) { |
| 168 |
$r .= "<th scope='row' class='check-column'>$checkbox</th>"; |
| 169 |
} else { |
| 170 |
if ( in_array( $column_name, $hidden ) ) { |
| 171 |
$val = ''; |
| 172 |
} else { |
| 173 |
$val = $this->column_value( $item ); |
| 174 |
} |
| 175 |
|
| 176 |
$r .= "<td $attributes>"; |
| 177 |
if ( $column_name == $action_col ) { |
| 178 |
$edit_link = '?page=formidable-entries&frm_action=edit&id=' . $item->id; |
| 179 |
$r .= '<a href="' . esc_url( isset( $actions['edit'] ) ? $edit_link : $view_link ) . '" class="row-title" >' . $val . '</a> '; |
| 180 |
$r .= $action_links; |
| 181 |
} else { |
| 182 |
$r .= $val; |
| 183 |
} |
| 184 |
$r .= '</td>'; |
| 185 |
} |
| 186 |
unset($val); |
| 187 |
} |
| 188 |
$r .= '</tr>'; |
| 189 |
|
| 190 |
return $r; |
| 191 |
} |
| 192 |
|
| 193 |
private function column_value( $item ) { |
| 194 |
$col_name = $this->column_name; |
| 195 |
|
| 196 |
switch ( $col_name ) { |
| 197 |
case 'ip': |
| 198 |
case 'id': |
| 199 |
case 'item_key': |
| 200 |
$val = $item->{$col_name}; |
| 201 |
break; |
| 202 |
case 'name': |
| 203 |
case 'description': |
| 204 |
$val = FrmAppHelper::truncate( strip_tags( $item->{$col_name} ), 100 ); |
| 205 |
break; |
| 206 |
case 'created_at': |
| 207 |
case 'updated_at': |
| 208 |
$date = FrmAppHelper::get_formatted_time( $item->{$col_name} ); |
| 209 |
$val = '<abbr title="' . esc_attr( FrmAppHelper::get_formatted_time( $item->{$col_name}, '', 'g:i:s A' ) ) . '">' . $date . '</abbr>'; |
| 210 |
break; |
| 211 |
case 'is_draft': |
| 212 |
$val = empty( $item->is_draft ) ? __( 'No' ) : __( 'Yes' ); |
| 213 |
break; |
| 214 |
case 'form_id': |
| 215 |
$val = FrmFormsHelper::edit_form_link( $item->form_id ); |
| 216 |
break; |
| 217 |
case 'post_id': |
| 218 |
$val = FrmAppHelper::post_edit_link( $item->post_id ); |
| 219 |
break; |
| 220 |
case 'user_id': |
| 221 |
$user = get_userdata( $item->user_id ); |
| 222 |
$val = $user ? $user->user_login : ''; |
| 223 |
break; |
| 224 |
case 'parent_item_id': |
| 225 |
$val = $item->parent_item_id; |
| 226 |
break; |
| 227 |
default: |
| 228 |
$val = apply_filters( 'frm_entries_' . $col_name . '_column', false, compact( 'item' ) ); |
| 229 |
if ( $val === false ) { |
| 230 |
$this->get_column_value( $item, $val ); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return $val; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* @param string $view_link |
| 239 |
*/ |
| 240 |
private function get_actions( &$actions, $item, $view_link ) { |
| 241 |
$actions['view'] = '<a href="' . esc_url( $view_link ) . '">' . __( 'View', 'formidable' ) . '</a>'; |
| 242 |
|
| 243 |
if ( current_user_can('frm_delete_entries') ) { |
| 244 |
$delete_link = '?page=formidable-entries&frm_action=destroy&id=' . $item->id . '&form=' . $this->params['form']; |
| 245 |
$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>'; |
| 246 |
} |
| 247 |
|
| 248 |
$actions = apply_filters('frm_row_actions', $actions, $item); |
| 249 |
} |
| 250 |
|
| 251 |
private function get_column_value( $item, &$val ) { |
| 252 |
$col_name = $this->column_name; |
| 253 |
|
| 254 |
if ( strpos( $col_name, 'frmsep_' ) === 0 ) { |
| 255 |
$sep_val = true; |
| 256 |
$col_name = str_replace( 'frmsep_', '', $col_name ); |
| 257 |
} else { |
| 258 |
$sep_val = false; |
| 259 |
} |
| 260 |
|
| 261 |
if ( strpos( $col_name, '-_-' ) ) { |
| 262 |
list( $col_name, $embedded_field_id ) = explode( '-_-', $col_name ); |
| 263 |
} |
| 264 |
|
| 265 |
$field = FrmField::getOne( $col_name ); |
| 266 |
if ( ! $field ) { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
$atts = array( |
| 271 |
'type' => $field->type, |
| 272 |
'truncate' => true, |
| 273 |
'post_id' => $item->post_id, |
| 274 |
'entry_id' => $item->id, |
| 275 |
'embedded_field_id' => 0, |
| 276 |
); |
| 277 |
|
| 278 |
if ( $sep_val ) { |
| 279 |
$atts['saved_value'] = true; |
| 280 |
} |
| 281 |
|
| 282 |
if ( isset( $embedded_field_id ) ) { |
| 283 |
$atts['embedded_field_id'] = $embedded_field_id; |
| 284 |
unset( $embedded_field_id ); |
| 285 |
} |
| 286 |
|
| 287 |
$val = FrmEntriesHelper::prepare_display_value( $item, $field, $atts ); |
| 288 |
} |
| 289 |
} |
| 290 |
|