date_format = get_option('date_format') . ' ' . get_option('time_format');
parent::__construct( [
'singular' => fqp__( 'Submission' ),
'plural' => fqp__( 'Submissions' ),
'ajax' => false,
] );
}
protected function column_cb( $item ) {
return sprintf(
'',
/*$1%s*/ esc_attr( 'ID' ),
/*$2%s*/ esc_attr( $item['ID'] )
);
}
protected function column_name( $item ) {
$confirm = esc_attr( fqp__( 'Please confirm you would like to delete this Submission.' ) );
$actions = [
'view' => sprintf(
'' . fqp__( 'View' ) . '',
/*$1%s*/ admin_url( 'admin.php' ),
/*$2%s*/ $_REQUEST['page'],
/*$3%s*/ 'view',
/*$4%s*/ $item['ID']
),
'delete' => sprintf(
'' . fqp__( 'Delete' ) . '',
/*$1%s*/ plugins_url( $_REQUEST['page'], FQP_PLUGIN_DIR_PATH ),
/*$2%s*/ $_REQUEST['page'],
/*$3%s*/ 'delete',
/*$4%s*/ $item['ID']
),
];
return sprintf( '%1$s %2$s',
/*$1%s*/ esc_html( "{$item['prefix']} {$item['first_name']} {$item['last_name']}" ),
/*$2%s*/ $this->row_actions( $actions )
);
}
protected function column_partners ( $item ) {
if ( ! empty( $item['partners'] ) ) {
$html = "
";
foreach ( $item['partners'] as $partner ) {
$title = esc_html( $partner['title'] );
$html .= "- {$title}
";
}
$html .= "
";
} else {
$html = "-";
}
return $html;
}
protected function column_address ( $item ) {
return esc_html( "{$item['street']}, {$item['zip']}, {$item['city']}" );
}
protected function column_contact ( $item ) {
return sprintf(
'%2$s
%4$s',
/*$1%s*/ esc_attr( $item['phone'] ),
/*$2%s*/ esc_html( $item['phone'] ),
/*$3%s*/ esc_attr( $item['email'] ),
/*$4%s*/ esc_html( $item['email'] )
);
}
protected function column_email_subscription ( $item ) {
return ( 'y' == $item['email_subscription'] ) ? 'Yes' : 'No';
}
protected function column_added ( $item ) {
return esc_html( date( $this->date_format, strtotime( $item['added'] ) ) );
}
protected function column_default( $item, $column_name ) {
switch( $column_name ) {
case 'entry_type':
case 'backing_type':
case 'email':
case 'phone':
return esc_html( $item[$column_name] );
default:
return print_r( $item, true ); //Show the whole array for troubleshooting purposes
}
}
public function get_columns() {
return [
'cb' => '',
'name' => fqp__( 'Name' ),
'entry_type' => fqp__( 'Entry Type' ),
'backing_type' => fqp__( 'Backing Type' ),
'partners' => fqp__( 'Partners' ),
'address' => fqp__( 'Address' ),
'contact' => fqp__( 'Contact' ),
'email_subscription' => fqp__( 'Subscribed' ),
'added' => fqp__( 'Date' ),
];
}
protected function get_sortable_columns() {
return [
'added' => [ 'added', true ],
'entry_type' => [ 'entry_type', false ],
'backing_type' => [ 'backing_type', false ],
'name' => [ 'last_name', false ],
'email' => [ 'email', false ],
'phone' => [ 'phone', false ],
'email_subscription' => [ 'email_subscription', false ],
];
}
protected function get_bulk_actions() {
return [
'delete' => fqp__( 'Delete' ),
];
}
function process_bulk_action() {
if ( 'delete' === $this->current_action() ) {
wp_die('Items deleted (or they would be if we had items to delete)!');
}
}
public function prepare_items() {
$per_page = $this->get_items_per_page( 'posts_per_page' );
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = [ $columns, $hidden, $sortable ];
$this->process_bulk_action();
$this->items = $this->get_items( $per_page );
$total_items = $this->get_items_count();
$this->set_pagination_args( [
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil( $total_items / $per_page ),
] );
}
protected function get_items( $per_page ) {
global $wpdb;
$current_page = $this->get_pagenum();
$offset = ( $current_page - 1 ) * $per_page;
$orderby = $this->get_orderby();
$table = DB::get_table_name( 'submission' );
$query = "SELECT * FROM `{$table}` WHERE 1 ORDER BY {$orderby} LIMIT {$per_page} OFFSET {$offset}";
$list = $wpdb->get_results( $query, ARRAY_A );
if ( ! $list ) {
$list = [];
} else {
$ids = array_column( $list, 'ID' );
$list = array_combine( $ids, $list );
$ids = implode( ', ', $ids );
$query = "SELECT * FROM `{$table}_partner` WHERE `submission_ID` IN ({$ids}) ORDER BY `ID`";
$partners = $wpdb->get_results( $query, ARRAY_A );
foreach ( $partners as $partner ) {
$list[$partner['submission_ID']]['partners'][] = $partner;
}
}
return $list;
}
protected function get_items_count() {
global $wpdb;
$table = DB::get_table_name( 'submission' );
$query = "SELECT COUNT(*) FROM `{$table}` WHERE 1";
return $wpdb->get_var( $query );
}
protected function get_orderby() {
$orderby = @$_REQUEST['orderby'];
$order = strtolower( @$_REQUEST['order'] );
$allowed_cols = array_keys( $this->get_sortable_columns() );
if ( ! in_array( $orderby, $allowed_cols ) ) {
$orderby = reset( $allowed_cols );
}
if ( ! in_array( $order, [ 'asc', 'desc' ] ) ) {
$order = 'desc';
}
return "`{$orderby}` {$order}";
}
}