| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 5 |
* |
| 6 |
* @package WPDataProjects\Parent_Child |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPDataProjects\Parent_Child { |
| 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 |
use WPDataProjects\List_Table\WPDP_List_View; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class WPDP_Parent_List_View extends WPDA_List_View |
| 19 |
* |
| 20 |
* Adds parent-child functionality to WPDA_List_View |
| 21 |
* |
| 22 |
* @see WPDA_List_View |
| 23 |
* |
| 24 |
* @author Peter Schulz |
| 25 |
* @since 2.0.0 |
| 26 |
*/ |
| 27 |
class WPDP_Parent_List_View extends WPDP_List_View { |
| 28 |
|
| 29 |
/** |
| 30 |
* Project ID |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $project_id = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Page ID |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
protected $page_id = null; |
| 42 |
|
| 43 |
/** |
| 44 |
* Instance of class WPDP_Project for $this->project_id |
| 45 |
* |
| 46 |
* @var WPDP_Project |
| 47 |
*/ |
| 48 |
protected $project; |
| 49 |
|
| 50 |
/** |
| 51 |
* Parent info |
| 52 |
* |
| 53 |
* @var array |
| 54 |
*/ |
| 55 |
protected $parent; |
| 56 |
|
| 57 |
/** |
| 58 |
* Child relationships |
| 59 |
* |
| 60 |
* @var array |
| 61 |
*/ |
| 62 |
protected $children; |
| 63 |
|
| 64 |
/** |
| 65 |
* Page title |
| 66 |
* |
| 67 |
* @var string |
| 68 |
*/ |
| 69 |
protected $title; |
| 70 |
|
| 71 |
/** |
| 72 |
* Page sub title |
| 73 |
* |
| 74 |
* @var string |
| 75 |
*/ |
| 76 |
protected $subtitle; |
| 77 |
|
| 78 |
/** |
| 79 |
* Possible values: TRUE and null |
| 80 |
* |
| 81 |
* TRUE = request is a child request |
| 82 |
* All other values (including null) = request is a parent request |
| 83 |
* |
| 84 |
* @var mixed |
| 85 |
*/ |
| 86 |
protected $child_request; |
| 87 |
|
| 88 |
/** |
| 89 |
* Possible values: edit and view |
| 90 |
* |
| 91 |
* @var string |
| 92 |
*/ |
| 93 |
protected $mode = null; |
| 94 |
|
| 95 |
/** |
| 96 |
* Class to be instantiated for data entry form |
| 97 |
* |
| 98 |
* @var string|null |
| 99 |
*/ |
| 100 |
protected $parent_edit_form_class = null; |
| 101 |
|
| 102 |
/** |
| 103 |
* SQL where clause |
| 104 |
* |
| 105 |
* @var null |
| 106 |
*/ |
| 107 |
protected $where_clause = null; |
| 108 |
|
| 109 |
/** |
| 110 |
* Order by |
| 111 |
* |
| 112 |
* @var null |
| 113 |
*/ |
| 114 |
protected $orderby_clause = null; |
| 115 |
|
| 116 |
/** |
| 117 |
* WPDP_Parent_List_View constructor |
| 118 |
* |
| 119 |
* Adds parent-child functionality |
| 120 |
* |
| 121 |
* @param array $args |
| 122 |
*/ |
| 123 |
public function __construct( $args = array() ) { |
| 124 |
if ( isset( $args['project_id'] ) ) { |
| 125 |
$this->project_id = sanitize_text_field( wp_unslash( $args['project_id'] ) ); |
| 126 |
} elseif ( isset( $_REQUEST['tab'] ) && 'tables' === $_REQUEST['tab'] ) { |
| 127 |
$this->project_id = 'wpda_sys_tables'; |
| 128 |
} |
| 129 |
if ( isset( $args['page_id'] ) ) { |
| 130 |
$this->page_id = sanitize_text_field( wp_unslash( $args['page_id'] ) ); |
| 131 |
} |
| 132 |
|
| 133 |
if ( null !== $this->title && '' !== $this->title ) { |
| 134 |
$args['title'] = $this->title; |
| 135 |
} |
| 136 |
$args['subtitle'] = $this->subtitle; |
| 137 |
|
| 138 |
parent::__construct( $args ); |
| 139 |
|
| 140 |
$this->child_request = ( |
| 141 |
isset( $_REQUEST['child_request'] ) && |
| 142 |
'TRUE' === sanitize_text_field( wp_unslash( $_REQUEST['child_request'] ) ) |
| 143 |
); |
| 144 |
|
| 145 |
if ( isset( $_REQUEST['mode'] ) ) { |
| 146 |
$this->mode = sanitize_text_field( wp_unslash( $_REQUEST['mode'] ) ); // input var okay. |
| 147 |
} |
| 148 |
|
| 149 |
if ( isset( $args['parent_edit_form_class'] ) ) { |
| 150 |
$this->parent_edit_form_class = $args['parent_edit_form_class']; // input var okay. |
| 151 |
} |
| 152 |
|
| 153 |
if ( isset( $args['where_clause'] ) && '' !== $args['where_clause'] ) { |
| 154 |
$this->where_clause = $args['where_clause']; |
| 155 |
} |
| 156 |
|
| 157 |
if ( isset( $args['orderby_clause'] ) && '' !== $args['orderby_clause'] ) { |
| 158 |
$this->orderby_clause = $args['orderby_clause']; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Overwrites method show to add parent-child functionality |
| 164 |
*/ |
| 165 |
public function show() { |
| 166 |
// Add datetimepicker |
| 167 |
wp_enqueue_style( 'datetimepicker' ); |
| 168 |
wp_enqueue_script( 'datetimepicker' ); |
| 169 |
|
| 170 |
$this->project = new WPDP_Project( $this->project_id, $this->page_id ); |
| 171 |
if ( null === $this->project->get_project() ) { |
| 172 |
wp_die( __( 'Data Project page not found [need a valid project_id and page_id]', 'wp-data-access' ) ); |
| 173 |
} |
| 174 |
if ( null === $this->title ) { |
| 175 |
$this->title = $this->project->get_title(); |
| 176 |
} |
| 177 |
if ( null === $this->mode ) { |
| 178 |
$this->mode = $this->project->get_mode(); |
| 179 |
} |
| 180 |
$this->subtitle = $this->project->get_subtitle(); |
| 181 |
$this->parent = $this->project->get_parent(); |
| 182 |
$this->children = $this->project->get_children(); |
| 183 |
|
| 184 |
$wpda_repository = new WPDA_Repository(); |
| 185 |
$wpda_repository->inform_user(); |
| 186 |
|
| 187 |
// DO NOT REMOVE!!! |
| 188 |
// Yes, this is double code! But for some completely unclear reason this member seems to lose this value... |
| 189 |
$this->child_request = ( |
| 190 |
isset( $_REQUEST['child_request'] ) && |
| 191 |
'TRUE' === sanitize_text_field( wp_unslash( $_REQUEST['child_request'] ) ) |
| 192 |
); |
| 193 |
|
| 194 |
if ( $this->child_request ) { |
| 195 |
$this->display_edit_form(); |
| 196 |
} else { |
| 197 |
switch ( $this->action ) { |
| 198 |
case 'new': |
| 199 |
case 'view': |
| 200 |
case 'edit': |
| 201 |
$this->display_edit_form(); |
| 202 |
break; |
| 203 |
default: |
| 204 |
$this->display_list_table(); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Overwrites method display_edit_form to add parent-child functionality |
| 211 |
*/ |
| 212 |
protected function display_edit_form() { |
| 213 |
if ( |
| 214 |
'view' === $this->mode && |
| 215 |
'only' !== $this->allow_insert || |
| 216 |
( ! $this->child_request && 'view' === $this->action ) |
| 217 |
) { |
| 218 |
$edit_form_class = 'WPDataProjects\\Parent_Child\\WPDP_Parent_Form_View'; |
| 219 |
$mode = 'view'; |
| 220 |
} else { |
| 221 |
$edit_form_class = 'WPDataProjects\\Parent_Child\\WPDP_Parent_Form'; |
| 222 |
$mode = 'edit'; |
| 223 |
} |
| 224 |
|
| 225 |
if ( null !== $this->parent_edit_form_class ) { |
| 226 |
$edit_form_class = $this->parent_edit_form_class; |
| 227 |
} |
| 228 |
|
| 229 |
$args = array( |
| 230 |
'title' => null === $this->title ? __( 'Back', 'wp-data-access' ) : $this->title, |
| 231 |
'subtitle' => $this->subtitle, |
| 232 |
'add_action_to_title' => 'FALSE', |
| 233 |
'mode' => $mode, |
| 234 |
'child_request' => $this->child_request, |
| 235 |
); |
| 236 |
if ( 'off' === $this->allow_insert ) { |
| 237 |
$args['hide_add_new'] = true; |
| 238 |
} |
| 239 |
if ( 'only' === $this->allow_insert ) { |
| 240 |
$args['action'] = 'new'; |
| 241 |
} |
| 242 |
|
| 243 |
$this->wpda_list_columns = WPDP_List_Columns_Cache::get_list_columns( $this->schema_name, $this->table_name, 'tableform', $this->setname ); |
| 244 |
$form = new $edit_form_class( |
| 245 |
$this->schema_name, |
| 246 |
$this->table_name, |
| 247 |
$this->wpda_list_columns, |
| 248 |
$args, |
| 249 |
array( |
| 250 |
'parent' => $this->parent, |
| 251 |
'children' => $this->children, |
| 252 |
) |
| 253 |
); |
| 254 |
|
| 255 |
$form->prepare_form(); |
| 256 |
|
| 257 |
if ( isset( $_POST['postaction'] ) && 'list' === $_POST['postaction'] ) { |
| 258 |
$this->display_list_table(); |
| 259 |
} else { |
| 260 |
$form->show(); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Overwrites method display_list_table to add parent-child functionality |
| 266 |
*/ |
| 267 |
protected function display_list_table() { |
| 268 |
$this->wpda_list_columns = WPDP_List_Columns_Cache::get_list_columns( $this->schema_name, $this->table_name, 'listtable', $this->setname ); |
| 269 |
$this->wpda_list_table = new $this->list_table_class( |
| 270 |
array( |
| 271 |
'wpdaschema_name' => $this->schema_name, |
| 272 |
'table_name' => $this->table_name, |
| 273 |
'wpda_list_columns' => $this->wpda_list_columns, |
| 274 |
'column_headers' => $this->column_headers, |
| 275 |
'project' => $this->project, |
| 276 |
'where_clause' => $this->where_clause, |
| 277 |
'orderby_clause' => $this->orderby_clause, |
| 278 |
'default_where' => $this->default_where, |
| 279 |
'allow_delete' => $this->allow_delete, |
| 280 |
'allow_import' => $this->allow_import, |
| 281 |
'allow_update' => $this->allow_update, |
| 282 |
'allow_insert' => $this->allow_insert, |
| 283 |
'pid' => $this->page_id, |
| 284 |
) |
| 285 |
); |
| 286 |
|
| 287 |
$this->wpda_list_table->set_bulk_actions_enabled( $this->bulk_actions_enabled ); |
| 288 |
$this->wpda_list_table->set_search_box_enabled( $this->search_box_enabled ); |
| 289 |
|
| 290 |
if ( null !== $this->title ) { |
| 291 |
$this->wpda_list_table->set_title( $this->title ); |
| 292 |
} |
| 293 |
if ( null !== $this->subtitle ) { |
| 294 |
$this->wpda_list_table->set_subtitle( $this->subtitle ); |
| 295 |
} |
| 296 |
|
| 297 |
$this->wpda_list_table->show(); |
| 298 |
} |
| 299 |
|
| 300 |
} |
| 301 |
|
| 302 |
} |
| 303 |
|