| 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\Project { |
| 9 |
|
| 10 |
use WPDataAccess\WPDA; |
| 11 |
use WPDataAccess\Data_Dictionary\WPDA_List_Columns_Cache; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class WPDP_Project |
| 15 |
* |
| 16 |
* @package WPDataProjects\Project |
| 17 |
* @author Peter Schulz |
| 18 |
* @since 2.0.0 |
| 19 |
*/ |
| 20 |
class WPDP_Project { |
| 21 |
|
| 22 |
/** |
| 23 |
* @var |
| 24 |
*/ |
| 25 |
protected $project; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var null |
| 29 |
*/ |
| 30 |
protected $project_id = null; |
| 31 |
/** |
| 32 |
* @var null |
| 33 |
*/ |
| 34 |
protected $page_id = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* WPDP_Project constructor. |
| 38 |
* |
| 39 |
* @param null $project_id |
| 40 |
* @param null $page_id |
| 41 |
*/ |
| 42 |
public function __construct( $project_id = null, $page_id = null ) { |
| 43 |
$this->project_id = $project_id; |
| 44 |
$this->page_id = $page_id; |
| 45 |
|
| 46 |
if ( null === $this->project_id ) { |
| 47 |
$this->init_self(); |
| 48 |
} elseif ( 'wpda_sys_tables' === $this->project_id ) { |
| 49 |
$this->init_self_tables(); |
| 50 |
} else { |
| 51 |
$this->init_project_page( $this->project_id, $this->page_id ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* |
| 57 |
*/ |
| 58 |
protected function init_self() { |
| 59 |
global $wpdb; |
| 60 |
$this->project = |
| 61 |
[ |
| 62 |
'mode' => 'edit', |
| 63 |
'title' => '', |
| 64 |
'subtitle' => '', |
| 65 |
'parent' => |
| 66 |
[ |
| 67 |
'key' => [ 'project_id' ], |
| 68 |
'data_type' => [ 'number' ], |
| 69 |
], |
| 70 |
'children' => |
| 71 |
[ |
| 72 |
[ |
| 73 |
'table_name' => $wpdb->prefix . 'wpdp_page', |
| 74 |
'tab_label' => 'Pages', |
| 75 |
'relation_1n' => |
| 76 |
[ |
| 77 |
'child_key' => [ 'project_id' ], |
| 78 |
'data_type' => [ 'number' ], |
| 79 |
], |
| 80 |
], |
| 81 |
], |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* |
| 87 |
*/ |
| 88 |
protected function init_self_tables() { |
| 89 |
$this->project = |
| 90 |
[ |
| 91 |
'mode' => 'edit', |
| 92 |
'title' => '', |
| 93 |
'subtitle' => '', |
| 94 |
'parent' => |
| 95 |
[ |
| 96 |
'key' => [ 'wpda_table_name' ], |
| 97 |
'data_type' => [ 'varchar' ], |
| 98 |
], |
| 99 |
'children.' => |
| 100 |
[], |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* @param $project_id |
| 106 |
* @param $page_id |
| 107 |
*/ |
| 108 |
protected function init_project_page( $project_id, $page_id ) { |
| 109 |
global $wpdb; |
| 110 |
$query = $wpdb->prepare( |
| 111 |
" |
| 112 |
select * from {$wpdb->prefix}wpdp_page |
| 113 |
where project_id = %d |
| 114 |
and page_id = %d |
| 115 |
", |
| 116 |
[ |
| 117 |
$project_id, |
| 118 |
$page_id, |
| 119 |
] |
| 120 |
); |
| 121 |
$project_page = $wpdb->get_results( $query, 'ARRAY_A' ); // WPCS: unprepared SQL OK; db call ok; no-cache ok. |
| 122 |
|
| 123 |
$table_name = $project_page[0]['page_table_name']; |
| 124 |
$relationships = WPDP_Project_Design_Table_Model::get_column_options( $table_name, 'relationships' ); |
| 125 |
|
| 126 |
$wpda_list_columns = WPDA_List_Columns_Cache::get_list_columns( '', $table_name ); |
| 127 |
$parent_primary_key = $wpda_list_columns->get_table_primary_key(); |
| 128 |
$parent_primary_key_data_type = []; |
| 129 |
foreach ( $parent_primary_key as $pk ) { |
| 130 |
if ( isset( $relationships['table'] ) && null !== $relationships['table'] ) { |
| 131 |
foreach ( $relationships['table'] as $column ) { |
| 132 |
if ( $column->column_name === $pk ) { |
| 133 |
array_push( $parent_primary_key_data_type, WPDA::get_type( $column->data_type ) ); |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
$parent = [ |
| 140 |
'key' => $parent_primary_key, |
| 141 |
'data_type' => $parent_primary_key_data_type, |
| 142 |
]; |
| 143 |
|
| 144 |
$children = []; |
| 145 |
if ( isset( $relationships['relationships'] ) && null !== $relationships['relationships'] ) { |
| 146 |
foreach ( $relationships['relationships'] as $relationship ) { |
| 147 |
$child_key_data_type = []; |
| 148 |
if ( '1n' === $relationship->relation_type || 'lookup' === $relationship->relation_type ) { |
| 149 |
$n_relationship = WPDP_Project_Design_Table_Model::get_column_options( $relationship->target_table_name, 'tableinfo' ); |
| 150 |
|
| 151 |
if ( isset( $n_relationship->tab_label ) ) { |
| 152 |
$tab_label = $n_relationship->tab_label; |
| 153 |
} else { |
| 154 |
$tab_label = ''; |
| 155 |
} |
| 156 |
|
| 157 |
foreach ( $relationships['table'] as $column ) { |
| 158 |
foreach ( $relationship->target_column_name as $target_column_name ) { |
| 159 |
if ( $column->column_name === $target_column_name ) { |
| 160 |
array_push( $child_key_data_type, WPDA::get_type( $column->data_type ) ); |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
$child = [ |
| 166 |
'table_name' => $relationship->target_table_name, |
| 167 |
'tab_label' => $tab_label === '' ? $relationship->target_table_name : $tab_label, |
| 168 |
'relation_' . $relationship->relation_type => [ |
| 169 |
'child_key' => $relationship->target_column_name, |
| 170 |
'data_type' => $child_key_data_type, |
| 171 |
] |
| 172 |
]; |
| 173 |
array_push( $children, $child ); |
| 174 |
} elseif ( 'nm' === $relationship->relation_type ) { |
| 175 |
$nm_relationships = WPDP_Project_Design_Table_Model::get_column_options( $relationship->relation_table_name, 'relationships' ); |
| 176 |
|
| 177 |
if ( isset( $nm_relationships['tableinfo'] ) && isset( $nm_relationships['tableinfo']->tab_label ) ) { |
| 178 |
$tab_label = $nm_relationships['tableinfo']->tab_label; |
| 179 |
} else { |
| 180 |
$tab_label = ''; |
| 181 |
} |
| 182 |
|
| 183 |
if ( null !== $nm_relationships['relationships'] ) { |
| 184 |
$nm_relationship_found = null; |
| 185 |
foreach ( $nm_relationships['relationships'] as $nm_relationship ) { |
| 186 |
if ( $nm_relationship->target_table_name === $relationship->target_table_name ) { |
| 187 |
$nm_relationship_found = $nm_relationship; |
| 188 |
break; |
| 189 |
} |
| 190 |
} |
| 191 |
foreach ( $nm_relationship_found->source_column_name as $source_column_name ) { |
| 192 |
foreach ( $nm_relationships['table'] as $column ) { |
| 193 |
if ( $column->column_name === $source_column_name ) { |
| 194 |
array_push( $child_key_data_type, WPDA::get_type( $column->data_type ) ); |
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
$child = [ |
| 200 |
'table_name' => $relationship->relation_table_name, |
| 201 |
'tab_label' => $tab_label === '' ? $relationship->relation_table_name : $tab_label, |
| 202 |
'relation_nm' => [ |
| 203 |
'child_table' => $relationship->target_table_name, |
| 204 |
'parent_key' => $nm_relationship->source_column_name, |
| 205 |
'child_table_select' => $nm_relationship->target_column_name, |
| 206 |
'child_table_where' => $relationship->target_column_name, |
| 207 |
'data_type' => $child_key_data_type, |
| 208 |
] |
| 209 |
]; |
| 210 |
array_push( $children, $child ); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
$this->project = |
| 217 |
[ |
| 218 |
'mode' => $project_page[0]['page_mode'], |
| 219 |
'title' => $project_page[0]['page_title'], |
| 220 |
'subtitle' => $project_page[0]['page_subtitle'], |
| 221 |
'parent' => $parent, |
| 222 |
'children' => $children, |
| 223 |
]; |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
/** |
| 229 |
* @return mixed |
| 230 |
*/ |
| 231 |
public function get_project() { |
| 232 |
return $this->project; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* @return null |
| 237 |
*/ |
| 238 |
public function get_mode() { |
| 239 |
if ( isset( $this->project['mode'] ) ) { |
| 240 |
return $this->project['mode']; |
| 241 |
} else { |
| 242 |
return null; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* @return null |
| 248 |
*/ |
| 249 |
public function get_title() { |
| 250 |
if ( isset( $this->project['title'] ) ) { |
| 251 |
return $this->project['title']; |
| 252 |
} else { |
| 253 |
return null; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* @return null |
| 259 |
*/ |
| 260 |
public function get_subtitle() { |
| 261 |
if ( isset( $this->project['subtitle'] ) ) { |
| 262 |
return $this->project['subtitle']; |
| 263 |
} else { |
| 264 |
return null; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* @return null |
| 270 |
*/ |
| 271 |
public function get_parent() { |
| 272 |
if ( isset( $this->project['parent'] ) ) { |
| 273 |
return $this->project['parent']; |
| 274 |
} else { |
| 275 |
return null; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* @return null |
| 281 |
*/ |
| 282 |
public function get_children() { |
| 283 |
if ( isset( $this->project['children'] ) ) { |
| 284 |
return $this->project['children']; |
| 285 |
} else { |
| 286 |
return null; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
} |
| 291 |
|
| 292 |
} |
| 293 |
|