| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 5 |
* |
| 6 |
* @package WPDataProjects\Project |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPDataProjects\Project { |
| 10 |
|
| 11 |
use WPDataAccess\WPDA; |
| 12 |
use WPDataAccess\Data_Dictionary\WPDA_List_Columns_Cache; |
| 13 |
use WPDataAccess\Plugin_Table_Models\WPDP_Project_Design_Table_Model; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class WPDP_Project |
| 17 |
* |
| 18 |
* @author Peter Schulz |
| 19 |
* @since 2.0.0 |
| 20 |
*/ |
| 21 |
class WPDP_Project { |
| 22 |
|
| 23 |
/** |
| 24 |
* Project structure |
| 25 |
* |
| 26 |
* @var array|null |
| 27 |
*/ |
| 28 |
protected $project = null; |
| 29 |
|
| 30 |
/** |
| 31 |
* Project ID |
| 32 |
* |
| 33 |
* @var string|null |
| 34 |
*/ |
| 35 |
protected $project_id = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* Page ID |
| 39 |
* |
| 40 |
* @var string|null |
| 41 |
*/ |
| 42 |
protected $page_id = null; |
| 43 |
|
| 44 |
/** |
| 45 |
* WPDP_Project constructor |
| 46 |
* |
| 47 |
* Set project and page ID. Sets the structure for the Data Projects tool main page if no project ID is |
| 48 |
* provided. Sets the structure for the Data Projects tool table options page if project ID is |
| 49 |
* 'wpda_sys_tables'. |
| 50 |
* |
| 51 |
* @param string $project_id Project ID |
| 52 |
* @param string $page_id Page ID |
| 53 |
*/ |
| 54 |
public function __construct( $project_id = null, $page_id = null ) { |
| 55 |
$this->project_id = $project_id; |
| 56 |
$this->page_id = $page_id; |
| 57 |
|
| 58 |
if ( null === $this->project_id ) { |
| 59 |
$this->init_self(); |
| 60 |
} elseif ( 'wpda_sys_tables' === $this->project_id ) { |
| 61 |
$this->init_self_tables(); |
| 62 |
} else { |
| 63 |
$this->init_project_page( $this->project_id, $this->page_id ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Sets the structure for the Data Projects tool main page |
| 69 |
*/ |
| 70 |
protected function init_self() { |
| 71 |
global $wpdb; |
| 72 |
$this->project = |
| 73 |
array( |
| 74 |
'mode' => 'edit', |
| 75 |
'setname' => '', |
| 76 |
'title' => '', |
| 77 |
'subtitle' => '', |
| 78 |
'parent' => |
| 79 |
array( |
| 80 |
'key' => array( 'project_id' ), |
| 81 |
'data_type' => array( 'number' ), |
| 82 |
), |
| 83 |
'children' => |
| 84 |
array( |
| 85 |
array( |
| 86 |
'table_name' => $wpdb->prefix . 'wpda_project_page', |
| 87 |
'setname' => '', |
| 88 |
'tab_label' => 'Pages', |
| 89 |
'default_where' => '', |
| 90 |
'default_orderby' => '', |
| 91 |
'relation_1n' => |
| 92 |
array( |
| 93 |
'parent_key' => array( 'project_id' ), |
| 94 |
'child_key' => array( 'project_id' ), |
| 95 |
'data_type' => array( 'number' ), |
| 96 |
), |
| 97 |
), |
| 98 |
), |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Sets the structure for the Data Projects tool table options page |
| 104 |
*/ |
| 105 |
protected function init_self_tables() { |
| 106 |
$this->project = |
| 107 |
array( |
| 108 |
'mode' => 'edit', |
| 109 |
'setname' => '', |
| 110 |
'title' => '', |
| 111 |
'subtitle' => '', |
| 112 |
'parent' => |
| 113 |
array( |
| 114 |
'key' => array( 'wpda_table_name' ), |
| 115 |
'data_type' => array( 'varchar' ), |
| 116 |
), |
| 117 |
'children.' => |
| 118 |
array(), |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Creates the structure for the given project page |
| 124 |
* |
| 125 |
* @param string $project_id Project ID |
| 126 |
* @param string $page_id Page ID |
| 127 |
*/ |
| 128 |
protected function init_project_page( $project_id, $page_id ) { |
| 129 |
global $wpdb; |
| 130 |
$query = $wpdb->prepare( |
| 131 |
" |
| 132 |
select * from {$wpdb->prefix}wpda_project_page |
| 133 |
where project_id = %d |
| 134 |
and page_id = %d |
| 135 |
", |
| 136 |
array( |
| 137 |
$project_id, |
| 138 |
$page_id, |
| 139 |
) |
| 140 |
); |
| 141 |
|
| 142 |
$project_page = $wpdb->get_results( $query, 'ARRAY_A' ); // phpcs:ignore WordPress.DB.PreparedSQL |
| 143 |
if ( 0 === $wpdb->num_rows ) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
$schema_name = $project_page[0]['page_schema_name']; |
| 148 |
$table_name = $project_page[0]['page_table_name']; |
| 149 |
$page_setname = $project_page[0]['page_setname']; |
| 150 |
|
| 151 |
$relationships = WPDP_Project_Design_Table_Model::get_column_options( $table_name, 'relationships', $page_setname, $schema_name ); |
| 152 |
if ( ! isset( $relationships['relationships'] ) || null === $relationships['relationships'] ) { |
| 153 |
$this->project = |
| 154 |
array( |
| 155 |
'mode' => $project_page[0]['page_mode'], |
| 156 |
'setname' => $page_setname, |
| 157 |
'title' => $project_page[0]['page_title'], |
| 158 |
'subtitle' => $project_page[0]['page_subtitle'], |
| 159 |
'parent' => array(), |
| 160 |
'children' => array(), |
| 161 |
'page_allow_full_export' => $project_page[0]['page_allow_full_export'], |
| 162 |
); |
| 163 |
|
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
// Add parent column and data type |
| 168 |
$wpda_list_columns = WPDA_List_Columns_Cache::get_list_columns( $schema_name, $table_name ); |
| 169 |
$parent_primary_key = $wpda_list_columns->get_table_primary_key(); |
| 170 |
$parent_primary_key_data_type = array(); |
| 171 |
foreach ( $parent_primary_key as $pk ) { |
| 172 |
if ( isset( $relationships['table'] ) && null !== $relationships['table'] ) { |
| 173 |
foreach ( $relationships['table'] as $column ) { |
| 174 |
if ( $column->column_name === $pk ) { |
| 175 |
array_push( $parent_primary_key_data_type, WPDA::get_type( $column->data_type ) );//phpcs:ignore - 8.1 proof |
| 176 |
break; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// Add children |
| 183 |
$children = array(); |
| 184 |
foreach ( $relationships['relationships'] as $relationship ) { |
| 185 |
$child_key_data_type = array(); |
| 186 |
if ( '1n' === $relationship->relation_type ) { |
| 187 |
$n_relationship = WPDP_Project_Design_Table_Model::get_column_options( $relationship->target_table_name, 'tableinfo', $page_setname, $schema_name ); |
| 188 |
|
| 189 |
if ( isset( $n_relationship->tab_label ) ) { |
| 190 |
$tab_label = $n_relationship->tab_label; |
| 191 |
} else { |
| 192 |
$tab_label = ''; |
| 193 |
} |
| 194 |
|
| 195 |
foreach ( $relationship->source_column_name as $source_column_name ) { |
| 196 |
if ( ! in_array( $source_column_name, $parent_primary_key ) ) { |
| 197 |
array_push( $parent_primary_key, $source_column_name );//phpcs:ignore - 8.1 proof |
| 198 |
foreach ( $relationships['table'] as $column ) { |
| 199 |
if ( $column->column_name === $source_column_name ) { |
| 200 |
array_push( $parent_primary_key_data_type, WPDA::get_type( $column->data_type ) );//phpcs:ignore - 8.1 proof |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
foreach ( $relationship->target_column_name as $target_column_name ) { |
| 208 |
foreach ( $relationships['table'] as $column ) { |
| 209 |
if ( $column->column_name === $target_column_name ) { |
| 210 |
array_push( $child_key_data_type, WPDA::get_type( $column->data_type ) );//phpcs:ignore - 8.1 proof |
| 211 |
break; |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
$child = array( |
| 217 |
'table_name' => $relationship->target_table_name, |
| 218 |
'tab_label' => $tab_label === '' ? $relationship->target_table_name : $tab_label, |
| 219 |
'default_where' => isset( $n_relationship->default_where ) ? $n_relationship->default_where : '', |
| 220 |
'default_orderby' => isset( $n_relationship->default_orderby ) ? $n_relationship->default_orderby : '', |
| 221 |
'relation_' . $relationship->relation_type => array( |
| 222 |
'parent_key' => $relationship->source_column_name, |
| 223 |
'child_key' => $relationship->target_column_name, |
| 224 |
'data_type' => $child_key_data_type, |
| 225 |
), |
| 226 |
); |
| 227 |
array_push( $children, $child );//phpcs:ignore - 8.1 proof |
| 228 |
} elseif ( 'nm' === $relationship->relation_type ) { |
| 229 |
$nm_relationships = WPDP_Project_Design_Table_Model::get_column_options( $relationship->relation_table_name, 'relationships', $page_setname, $schema_name ); |
| 230 |
|
| 231 |
if ( isset( $nm_relationships['tableinfo'] ) && isset( $nm_relationships['tableinfo']->tab_label ) ) { |
| 232 |
$tab_label = $nm_relationships['tableinfo']->tab_label; |
| 233 |
} else { |
| 234 |
$tab_label = ''; |
| 235 |
} |
| 236 |
|
| 237 |
if ( isset( $nm_relationships['relationships'] ) && null !== $nm_relationships['relationships'] ) { |
| 238 |
$nm_relationship_found = null; |
| 239 |
foreach ( $nm_relationships['relationships'] as $nm_relationship ) { |
| 240 |
if ( $nm_relationship->target_table_name === $relationship->target_table_name ) { |
| 241 |
$nm_relationship_found = $nm_relationship; |
| 242 |
break; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
if ( null !== $nm_relationship_found ) { |
| 247 |
foreach ( $nm_relationship_found->source_column_name as $source_column_name ) { |
| 248 |
foreach ( $nm_relationships['table'] as $column ) { |
| 249 |
if ( $column->column_name === $source_column_name ) { |
| 250 |
array_push( $child_key_data_type, WPDA::get_type( $column->data_type ) );//phpcs:ignore - 8.1 proof |
| 251 |
if ( ! in_array( $source_column_name, $parent_primary_key ) ) {//phpcs:ignore - 8.1 proof |
| 252 |
array_push( $parent_primary_key, $source_column_name );//phpcs:ignore - 8.1 proof |
| 253 |
array_push( $parent_primary_key_data_type, WPDA::get_type( $column->data_type ) );//phpcs:ignore - 8.1 proof |
| 254 |
} |
| 255 |
break; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
$child = array( |
| 261 |
'table_name' => $relationship->relation_table_name, |
| 262 |
'tab_label' => $tab_label === '' ? $relationship->relation_table_name : $tab_label, |
| 263 |
'default_where' => isset( $nm_relationships['tableinfo']->default_where ) ? $nm_relationships['tableinfo']->default_where : '', |
| 264 |
'default_orderby' => isset( $nm_relationships['tableinfo']->default_orderby ) ? $nm_relationships['tableinfo']->default_orderby : '', |
| 265 |
'relation_nm' => array( |
| 266 |
'child_table' => $relationship->target_table_name, |
| 267 |
'parent_key' => $nm_relationship->source_column_name, |
| 268 |
'child_table_select' => $nm_relationship->target_column_name, |
| 269 |
'child_table_where' => $relationship->target_column_name, |
| 270 |
'data_type' => $child_key_data_type, |
| 271 |
), |
| 272 |
); |
| 273 |
array_push( $children, $child );//phpcs:ignore - 8.1 proof |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
// Prepare parent key |
| 280 |
$parent = array( |
| 281 |
'key' => $parent_primary_key, |
| 282 |
'data_type' => $parent_primary_key_data_type, |
| 283 |
); |
| 284 |
|
| 285 |
$this->project = |
| 286 |
array( |
| 287 |
'mode' => $project_page[0]['page_mode'], |
| 288 |
'setname' => $page_setname, |
| 289 |
'title' => $project_page[0]['page_title'], |
| 290 |
'subtitle' => $project_page[0]['page_subtitle'], |
| 291 |
'parent' => $parent, |
| 292 |
'children' => $children, |
| 293 |
'page_allow_full_export' => $project_page[0]['page_allow_full_export'], |
| 294 |
); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
/** |
| 299 |
* Returns the project structure |
| 300 |
* |
| 301 |
* @return array |
| 302 |
*/ |
| 303 |
public function get_project() { |
| 304 |
return $this->project; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Returns the project mode or null if not available |
| 309 |
* |
| 310 |
* @return string|null |
| 311 |
*/ |
| 312 |
public function get_mode() { |
| 313 |
if ( isset( $this->project['mode'] ) ) { |
| 314 |
return $this->project['mode']; |
| 315 |
} else { |
| 316 |
return null; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Returns the project setname or null if not available |
| 322 |
* |
| 323 |
* @return string|null |
| 324 |
*/ |
| 325 |
public function get_setname() { |
| 326 |
if ( isset( $this->project['setname'] ) ) { |
| 327 |
return $this->project['setname']; |
| 328 |
} else { |
| 329 |
return null; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Returns the project title or null if not available |
| 335 |
* |
| 336 |
* @return string|null |
| 337 |
*/ |
| 338 |
public function get_title() { |
| 339 |
if ( isset( $this->project['title'] ) ) { |
| 340 |
return $this->project['title']; |
| 341 |
} else { |
| 342 |
return null; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Returns the project sub title or null if not available |
| 348 |
* |
| 349 |
* @return string|null |
| 350 |
*/ |
| 351 |
public function get_subtitle() { |
| 352 |
if ( isset( $this->project['subtitle'] ) ) { |
| 353 |
return $this->project['subtitle']; |
| 354 |
} else { |
| 355 |
return null; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Returns the project parent info or null if not available |
| 361 |
* |
| 362 |
* @return array|null |
| 363 |
*/ |
| 364 |
public function get_parent() { |
| 365 |
if ( isset( $this->project['parent'] ) ) { |
| 366 |
return $this->project['parent']; |
| 367 |
} else { |
| 368 |
return null; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Returns the project children or null if not available |
| 374 |
* |
| 375 |
* @return array|null |
| 376 |
*/ |
| 377 |
public function get_children() { |
| 378 |
if ( isset( $this->project['children'] ) ) { |
| 379 |
return $this->project['children']; |
| 380 |
} else { |
| 381 |
return null; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Is the user allowed to start a full export to CSV or JSON |
| 387 |
* |
| 388 |
* @return string yes or no |
| 389 |
*/ |
| 390 |
public function get_page_allow_full_export() { |
| 391 |
if ( isset( $this->project['page_allow_full_export'] ) ) { |
| 392 |
return $this->project['page_allow_full_export']; |
| 393 |
} else { |
| 394 |
return 'no'; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
} |
| 399 |
|
| 400 |
} |
| 401 |
|