| 1 |
<?php |
| 2 |
/** |
| 3 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 4 |
* |
| 5 |
* @package WPDataProjects |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPDataProjects\List_Table { |
| 9 |
|
| 10 |
use WPDataAccess\List_Table\WPDA_List_View; |
| 11 |
use WPDataAccess\Utilities\WPDA_Repository; |
| 12 |
use WPDataProjects\Project\WPDP_Project; |
| 13 |
use WPDataProjects\Data_Dictionary\WPDP_List_Columns_Cache; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class WPDP_List_View |
| 17 |
* |
| 18 |
* @package WPDataProjects\List_Table |
| 19 |
* @author Peter Schulz |
| 20 |
* @since 2.0.0 |
| 21 |
*/ |
| 22 |
class WPDP_List_View extends WPDA_List_View { |
| 23 |
|
| 24 |
/** |
| 25 |
* @var null|string |
| 26 |
*/ |
| 27 |
protected $project_id = null; |
| 28 |
/** |
| 29 |
* @var null |
| 30 |
*/ |
| 31 |
protected $page_id = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var null |
| 35 |
*/ |
| 36 |
protected $title; |
| 37 |
/** |
| 38 |
* @var null |
| 39 |
*/ |
| 40 |
protected $subtitle; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var |
| 44 |
*/ |
| 45 |
protected $mode; |
| 46 |
|
| 47 |
/** |
| 48 |
* @var null |
| 49 |
*/ |
| 50 |
protected $where_clause = null; |
| 51 |
|
| 52 |
/** |
| 53 |
* @var string |
| 54 |
*/ |
| 55 |
protected $label_type = 'listtable'; |
| 56 |
|
| 57 |
/** |
| 58 |
* WPDP_List_View constructor. |
| 59 |
* |
| 60 |
* @param array $args |
| 61 |
*/ |
| 62 |
public function __construct( array $args = [] ) { |
| 63 |
if ( isset( $args['project_id'] ) ) { |
| 64 |
$this->project_id = sanitize_text_field( wp_unslash( $args['project_id'] ) ); |
| 65 |
} elseif ( isset( $_REQUEST['tab'] ) && 'tables' === $_REQUEST['tab'] ) { |
| 66 |
$this->project_id = 'wpda_sys_tables'; |
| 67 |
} |
| 68 |
if ( isset( $args['page_id'] ) ) { |
| 69 |
$this->page_id = sanitize_text_field( wp_unslash( $args['page_id'] ) ); |
| 70 |
} |
| 71 |
|
| 72 |
$this->project = new WPDP_Project( $this->project_id, $this->page_id ); |
| 73 |
$this->title = $this->project->get_title(); |
| 74 |
$this->subtitle = $this->project->get_subtitle(); |
| 75 |
$this->mode = $this->project->get_mode(); |
| 76 |
|
| 77 |
$args['title'] = ( null === $this->title || '' === $this->title ) ? null : $this->title; |
| 78 |
$args['subtitle'] = $this->subtitle; |
| 79 |
|
| 80 |
parent::__construct( $args ); |
| 81 |
|
| 82 |
if ( |
| 83 |
'edit' === $this->action || |
| 84 |
'new' === $this->action || |
| 85 |
'view' === $this->action |
| 86 |
) { |
| 87 |
$this->label_type = 'tableform'; |
| 88 |
} |
| 89 |
|
| 90 |
// Overwrite column header text. |
| 91 |
$this->column_headers = isset( $args['column_headers'] ) ? $args['column_headers'] : ''; |
| 92 |
|
| 93 |
if ( isset( $args['where_clause'] ) && '' !== $args['where_clause'] ) { |
| 94 |
$this->where_clause = $args['where_clause']; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Overwrite method. |
| 100 |
*/ |
| 101 |
public function show() { |
| 102 |
// Prepare columns for list table. Needed in get_column_headers() and handed over to list table to prevent |
| 103 |
// processing the same queries multiple times. |
| 104 |
if ( null === $this->wpda_list_columns ) { |
| 105 |
$this->wpda_list_columns = WPDP_List_Columns_Cache::get_list_columns( $this->schema_name, $this->table_name, $this->label_type ); |
| 106 |
} |
| 107 |
|
| 108 |
$wpda_repository = new WPDA_Repository(); |
| 109 |
$wpda_repository->inform_user(); |
| 110 |
|
| 111 |
switch ( $this->action ) { |
| 112 |
|
| 113 |
case 'new': // Show edit form in editing mode to create new records. |
| 114 |
case 'edit': // Show edit form in editing mode to update records. |
| 115 |
case 'view': // Show edit form in view mode to view records. |
| 116 |
$this->display_edit_form(); |
| 117 |
break; |
| 118 |
|
| 119 |
case 'create_table': // Show form to create new table. |
| 120 |
$this->display_design_menu(); |
| 121 |
break; |
| 122 |
|
| 123 |
default: // Show list (default). |
| 124 |
$this->display_list_table(); |
| 125 |
|
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Overwrite method. |
| 131 |
*/ |
| 132 |
protected function display_list_table() { |
| 133 |
$this->wpda_list_table = new WPDP_List_Table( |
| 134 |
[ |
| 135 |
'schema_name' => $this->schema_name, |
| 136 |
'table_name' => $this->table_name, |
| 137 |
'wpda_list_columns' => $this->wpda_list_columns, |
| 138 |
'column_headers' => $this->column_headers, |
| 139 |
'title' => $this->title, |
| 140 |
'subtitle' => $this->subtitle, |
| 141 |
'mode' => $this->mode, |
| 142 |
'where_clause' => $this->where_clause, |
| 143 |
] |
| 144 |
); |
| 145 |
|
| 146 |
$this->wpda_list_table->show(); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Overwrite method. |
| 151 |
*/ |
| 152 |
public function get_column_headers() { |
| 153 |
if ( null === $this->wpda_list_columns ) { |
| 154 |
$this->wpda_list_columns = WPDP_List_Columns_Cache::get_list_columns( $this->schema_name, $this->table_name, $this->label_type ); |
| 155 |
} |
| 156 |
return $this->wpda_list_columns->get_table_column_headers(); |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
} |