| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 5 |
* |
| 6 |
* @package WPDataProjects\List_Table |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPDataProjects\List_Table { |
| 10 |
|
| 11 |
use WPDataAccess\List_Table\WPDA_List_View; |
| 12 |
use WPDataAccess\Utilities\WPDA_Repository; |
| 13 |
use WPDataProjects\Project\WPDP_Project; |
| 14 |
use WPDataProjects\Data_Dictionary\WPDP_List_Columns_Cache; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class WPDP_List_View extends WPDA_List_View |
| 18 |
* |
| 19 |
* Data Projects uses WPDP_List_View instead of WPDA_List_View to handle column labels correctly. |
| 20 |
* |
| 21 |
* @see WPDA_List_View |
| 22 |
* |
| 23 |
* @author Peter Schulz |
| 24 |
* @since 2.0.0 |
| 25 |
*/ |
| 26 |
class WPDP_List_View extends WPDA_List_View { |
| 27 |
|
| 28 |
/** |
| 29 |
* Project ID |
| 30 |
* |
| 31 |
* @var null|string |
| 32 |
*/ |
| 33 |
protected $project_id = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* Page ID |
| 37 |
* |
| 38 |
* @var null |
| 39 |
*/ |
| 40 |
protected $page_id = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* Page title |
| 44 |
* |
| 45 |
* @var null |
| 46 |
*/ |
| 47 |
protected $title; |
| 48 |
|
| 49 |
/** |
| 50 |
* Page subtitle |
| 51 |
* |
| 52 |
* @var null |
| 53 |
*/ |
| 54 |
protected $subtitle; |
| 55 |
|
| 56 |
/** |
| 57 |
* Possible values for mode are: view and edit |
| 58 |
* |
| 59 |
* @var |
| 60 |
*/ |
| 61 |
protected $mode; |
| 62 |
|
| 63 |
/** |
| 64 |
* SQL where clause |
| 65 |
* |
| 66 |
* @var null |
| 67 |
*/ |
| 68 |
protected $where_clause = null; |
| 69 |
|
| 70 |
/** |
| 71 |
* Order by |
| 72 |
* |
| 73 |
* @var null |
| 74 |
*/ |
| 75 |
protected $orderby_clause = null; |
| 76 |
|
| 77 |
/** |
| 78 |
* Possible values for label type are: listtable and tableform |
| 79 |
* |
| 80 |
* @var string |
| 81 |
*/ |
| 82 |
protected $label_type = 'listtable'; |
| 83 |
|
| 84 |
/** |
| 85 |
* Allow insert? |
| 86 |
* |
| 87 |
* @var string|null |
| 88 |
*/ |
| 89 |
protected $allow_insert = null; |
| 90 |
|
| 91 |
/** |
| 92 |
* Allow delete? |
| 93 |
* |
| 94 |
* @var string|null |
| 95 |
*/ |
| 96 |
protected $allow_delete = null; |
| 97 |
|
| 98 |
/** |
| 99 |
* Allow import? |
| 100 |
* |
| 101 |
* @var string|null |
| 102 |
*/ |
| 103 |
protected $allow_import = null; |
| 104 |
|
| 105 |
/** |
| 106 |
* Project info |
| 107 |
* |
| 108 |
* @var WPDP_Project |
| 109 |
*/ |
| 110 |
protected $project = null; |
| 111 |
|
| 112 |
/** |
| 113 |
* Options set name |
| 114 |
* |
| 115 |
* @var string |
| 116 |
*/ |
| 117 |
protected $setname = 'default'; |
| 118 |
|
| 119 |
/** |
| 120 |
* Overwrite constructor |
| 121 |
* |
| 122 |
* @param array $args |
| 123 |
*/ |
| 124 |
public function __construct( array $args = array() ) { |
| 125 |
if ( isset( $args['project_id'] ) ) { |
| 126 |
$this->project_id = sanitize_text_field( wp_unslash( $args['project_id'] ) ); |
| 127 |
} elseif ( isset( $_REQUEST['tab'] ) && 'tables' === $_REQUEST['tab'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- verified in parent class |
| 128 |
$this->project_id = 'wpda_sys_tables'; |
| 129 |
} |
| 130 |
if ( isset( $args['page_id'] ) ) { |
| 131 |
$this->page_id = sanitize_text_field( wp_unslash( $args['page_id'] ) ); |
| 132 |
} |
| 133 |
|
| 134 |
$this->project = new WPDP_Project( $this->project_id, $this->page_id ); |
| 135 |
if ( null === $this->project->get_project() ) { |
| 136 |
wp_die( esc_attr__( 'Data Project page not found [need a valid project_id and page_id]', 'wp-data-access' ) ); |
| 137 |
} |
| 138 |
$this->title = $this->project->get_title(); |
| 139 |
$this->subtitle = $this->project->get_subtitle(); |
| 140 |
$this->mode = $this->project->get_mode(); |
| 141 |
$this->setname = null === $this->project->get_setname() ? 'default' : $this->project->get_setname(); |
| 142 |
|
| 143 |
if ( null !== $this->title && '' !== $this->title ) { |
| 144 |
$args['title'] = $this->title; |
| 145 |
} |
| 146 |
$args['subtitle'] = $this->subtitle; |
| 147 |
|
| 148 |
parent::__construct( $args ); |
| 149 |
|
| 150 |
if ( |
| 151 |
'edit' === $this->action || |
| 152 |
'new' === $this->action || |
| 153 |
'view' === $this->action |
| 154 |
) { |
| 155 |
$this->label_type = 'tableform'; |
| 156 |
} |
| 157 |
|
| 158 |
// Overwrite column header text. |
| 159 |
$this->column_headers = isset( $args['column_headers'] ) ? $args['column_headers'] : ''; |
| 160 |
|
| 161 |
if ( isset( $args['where_clause'] ) && '' !== $args['where_clause'] ) { |
| 162 |
$this->where_clause = $args['where_clause']; |
| 163 |
} |
| 164 |
|
| 165 |
if ( isset( $args['orderby_clause'] ) && '' !== $args['orderby_clause'] ) { |
| 166 |
$this->orderby_clause = $args['orderby_clause']; |
| 167 |
} |
| 168 |
|
| 169 |
if ( isset( $args['allow_insert'] ) ) { |
| 170 |
$this->allow_insert = sanitize_text_field( wp_unslash( $args['allow_insert'] ) ); |
| 171 |
} |
| 172 |
if ( isset( $args['allow_delete'] ) ) { |
| 173 |
$this->allow_delete = sanitize_text_field( wp_unslash( $args['allow_delete'] ) ); |
| 174 |
} |
| 175 |
if ( isset( $args['allow_import'] ) ) { |
| 176 |
$this->allow_import = sanitize_text_field( wp_unslash( $args['allow_import'] ) ); |
| 177 |
} |
| 178 |
if ( isset( $args['bulk_actions_enabled'] ) ) { |
| 179 |
$this->bulk_actions_enabled = $args['bulk_actions_enabled']; |
| 180 |
} |
| 181 |
|
| 182 |
global $wpda_project_mode; |
| 183 |
$wpda_project_mode = array( |
| 184 |
'project_id' => $this->project_id, |
| 185 |
'page_id' => $this->page_id, |
| 186 |
'setname' => $this->setname, |
| 187 |
'mode' => $this->mode, |
| 188 |
'page_allow_full_export' => $this->project->get_page_allow_full_export(), |
| 189 |
'allow_insert' => $this->allow_insert, |
| 190 |
); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Overwrite show method |
| 195 |
* |
| 196 |
* @see WPDA_List_View::show() |
| 197 |
*/ |
| 198 |
public function show() { |
| 199 |
// Add datetimepicker |
| 200 |
wp_enqueue_style( 'datetimepicker' ); |
| 201 |
wp_enqueue_script( 'datetimepicker' ); |
| 202 |
|
| 203 |
// Prepare columns for list table. Needed in get_column_headers() and handed over to list table to prevent |
| 204 |
// processing the same queries multiple times. |
| 205 |
if ( null === $this->wpda_list_columns ) { |
| 206 |
$this->wpda_list_columns = |
| 207 |
WPDP_List_Columns_Cache::get_list_columns( $this->schema_name, $this->table_name, $this->label_type, $this->setname ); |
| 208 |
} |
| 209 |
|
| 210 |
$wpda_repository = new WPDA_Repository(); |
| 211 |
$wpda_repository->inform_user(); |
| 212 |
|
| 213 |
if ( 'only' === $this->allow_insert && 'new' !== $this->action ) { |
| 214 |
wp_die( esc_attr__( 'ERROR: Action not allowed', 'wp-data-access' ) ); |
| 215 |
} |
| 216 |
|
| 217 |
if ( |
| 218 |
'view' === $this->mode && |
| 219 |
( |
| 220 |
'new' === $this->action || 'edit' === $this->action |
| 221 |
) |
| 222 |
) { |
| 223 |
if ( ( 'only' === $this->allow_insert || null === $this->allow_insert ) && 'new' === $this->action ) { |
| 224 |
// Allow these actions (exceptions) |
| 225 |
} else { |
| 226 |
wp_die( esc_attr__( 'ERROR: Action not allowed', 'wp-data-access' ) ); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
switch ( $this->action ) { |
| 231 |
|
| 232 |
case 'new': // Show edit form in editing mode to create new records. |
| 233 |
case 'edit': // Show edit form in editing mode to update records. |
| 234 |
case 'view': // Show edit form in view mode to view records. |
| 235 |
$this->display_edit_form(); |
| 236 |
break; |
| 237 |
|
| 238 |
case 'create_table': // Show form to create new table. |
| 239 |
$this->display_design_menu(); |
| 240 |
break; |
| 241 |
|
| 242 |
default: // Show list (default). |
| 243 |
$this->display_list_table(); |
| 244 |
|
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Overwrite display_edit_form method |
| 250 |
* |
| 251 |
* @see WPDA_List_View::display_edit_form() |
| 252 |
*/ |
| 253 |
protected function display_edit_form() { |
| 254 |
$args = array( |
| 255 |
'title' => $this->title, |
| 256 |
'add_action_to_title' => 'FALSE', |
| 257 |
'hide_add_new' => 'off' === $this->allow_insert, |
| 258 |
); |
| 259 |
|
| 260 |
if ( 'only' === $this->allow_insert ) { |
| 261 |
$args['show_back_button'] = false; |
| 262 |
$args['show_back_icon'] = false; |
| 263 |
$args['action'] = 'new'; |
| 264 |
} |
| 265 |
|
| 266 |
$form = new $this->edit_form_class( |
| 267 |
$this->schema_name, |
| 268 |
$this->table_name, |
| 269 |
$this->wpda_list_columns, |
| 270 |
$args |
| 271 |
); |
| 272 |
|
| 273 |
$form->prepare_form(); |
| 274 |
|
| 275 |
if ( isset( $_POST['postaction'] ) && 'list' === $_POST['postaction'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified in parent class |
| 276 |
// Jump back to list after pressing SUBMIT > LIST |
| 277 |
// Change column list from edit to list mode |
| 278 |
$this->wpda_list_columns = |
| 279 |
WPDP_List_Columns_Cache::get_list_columns( $this->schema_name, $this->table_name, 'listtable', $this->setname ); |
| 280 |
$this->display_list_table(); |
| 281 |
} else { |
| 282 |
$form->show(); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Overwrite display_list_table method |
| 288 |
* |
| 289 |
* @see WPDA_List_View::display_list_table() |
| 290 |
*/ |
| 291 |
protected function display_list_table() { |
| 292 |
$args = array( |
| 293 |
'wpdaschema_name' => $this->schema_name, |
| 294 |
'table_name' => $this->table_name, |
| 295 |
'wpda_list_columns' => $this->wpda_list_columns, |
| 296 |
'column_headers' => $this->column_headers, |
| 297 |
'title' => $this->title, |
| 298 |
'subtitle' => $this->subtitle, |
| 299 |
'mode' => $this->mode, |
| 300 |
'where_clause' => $this->where_clause, |
| 301 |
'orderby_clause' => $this->orderby_clause, |
| 302 |
'pid' => $this->page_id, |
| 303 |
); |
| 304 |
|
| 305 |
if ( null !== $this->allow_insert ) { |
| 306 |
$args['allow_insert'] = $this->allow_insert; |
| 307 |
} |
| 308 |
if ( null !== $this->allow_update ) { |
| 309 |
$args['allow_update'] = $this->allow_update; |
| 310 |
} |
| 311 |
if ( null !== $this->allow_delete ) { |
| 312 |
$args['allow_delete'] = $this->allow_delete; |
| 313 |
} |
| 314 |
if ( null !== $this->allow_import ) { |
| 315 |
$args['allow_import'] = $this->allow_import; |
| 316 |
} |
| 317 |
if ( false === $this->bulk_actions_enabled ) { |
| 318 |
$args['bulk_actions_enabled'] = $this->bulk_actions_enabled; |
| 319 |
} |
| 320 |
|
| 321 |
$this->wpda_list_table = new WPDP_List_Table( $args ); |
| 322 |
$this->wpda_list_table->show(); |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Overwrite get_column_headers method |
| 327 |
* |
| 328 |
* @see WPDA_List_View::get_column_headers() |
| 329 |
*/ |
| 330 |
public function get_column_headers() { |
| 331 |
if ( null === $this->wpda_list_columns ) { |
| 332 |
$this->wpda_list_columns = WPDP_List_Columns_Cache::get_list_columns( $this->schema_name, $this->table_name, $this->label_type, $this->setname ); |
| 333 |
} |
| 334 |
|
| 335 |
return $this->wpda_list_columns->get_table_column_headers(); |
| 336 |
} |
| 337 |
|
| 338 |
} |
| 339 |
|
| 340 |
} |
| 341 |
|