| 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\Simple_Form { |
| 9 |
|
| 10 |
use WPDataAccess\Simple_Form\WPDA_Simple_Form; |
| 11 |
use WPDataProjects\Parent_Child\WPDP_Child_Form; |
| 12 |
use WPDataProjects\Project\WPDP_Project_Design_Table_Model; |
| 13 |
|
| 14 |
class WPDP_Simple_Form extends WPDA_Simple_Form { |
| 15 |
|
| 16 |
protected function prepare_items( $set_back_form_values = false ) { |
| 17 |
parent::prepare_items( $set_back_form_values ); |
| 18 |
|
| 19 |
// Hide columns which have show attribute disabled. |
| 20 |
foreach ( $this->wpda_list_columns->get_table_columns() as $columns ) { |
| 21 |
if ( isset ( $columns['show'] ) && ! $columns['show'] ) { |
| 22 |
foreach ( $this->form_items as $item ) { |
| 23 |
if ( $item->get_item_name()===$columns['column_name'] ) { |
| 24 |
$item->set_hide_item( true ); |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
// Check if there are any lookup items defined for this table. |
| 31 |
$lookup_column_name = []; |
| 32 |
$tableform = WPDP_Project_Design_Table_Model::get_column_options( $this->table_name, 'tableform' ); |
| 33 |
|
| 34 |
if ( null !== $tableform ) { |
| 35 |
foreach ( $tableform as $tableform_item ) { |
| 36 |
if ( isset( $tableform_item->lookup ) && false !== $tableform_item->lookup ) { |
| 37 |
$lookup_column_name[ $tableform_item->column_name ] = $tableform_item->lookup; |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
if ( sizeof( $lookup_column_name ) > 0 ) { |
| 42 |
// Process lookup items and create listboxes. |
| 43 |
$lookups = []; |
| 44 |
$relationships = WPDP_Project_Design_Table_Model::get_column_options( $this->table_name, 'relationships' ); |
| 45 |
|
| 46 |
if ( null !== $relationships ) { |
| 47 |
if ( isset( $relationships['relationships'] ) ) { |
| 48 |
foreach ( $relationships['relationships'] as $relationship ) { |
| 49 |
if ( isset( $relationship->relation_type ) && 'lookup' === $relationship->relation_type ) { |
| 50 |
array_push( $lookups, $relationship ); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
foreach ( $this->form_items as $item ) { |
| 57 |
if ( isset( $lookup_column_name[ $item->get_item_name() ] ) ) { |
| 58 |
foreach ( $lookups as $lookup ) { |
| 59 |
// Lookups are always based on a single column. Use first element of array. |
| 60 |
$source_column_name = $lookup->source_column_name[0]; |
| 61 |
if ( $source_column_name === $item->get_item_name() ) { |
| 62 |
$target_column_name = $lookup->target_column_name[0]; |
| 63 |
$target_table_name = $lookup->target_table_name; |
| 64 |
|
| 65 |
if ( isset( $lookup_column_name[ $source_column_name ] ) ) { |
| 66 |
$lookup_sql = |
| 67 |
"select `{$lookup_column_name[ $source_column_name ]}`, `$target_column_name` " . |
| 68 |
"from `$target_table_name` " . |
| 69 |
"order by `{$lookup_column_name[ $source_column_name ]}`, `$target_column_name`"; |
| 70 |
|
| 71 |
global $wpdb; |
| 72 |
$rows = $wpdb->get_results( $lookup_sql, 'ARRAY_A' ); |
| 73 |
|
| 74 |
$lov_values = []; |
| 75 |
$lov_options = []; |
| 76 |
|
| 77 |
if ( isset( $relationships['table'] ) ) { |
| 78 |
foreach ( $relationships['table'] as $table_column ) { |
| 79 |
if ( isset( $table_column->column_name ) && isset( $table_column->mandatory ) ) { |
| 80 |
if ( $table_column->column_name === $source_column_name && 'No' === $table_column->mandatory ) { |
| 81 |
array_push( $lov_values , '' ); |
| 82 |
array_push( $lov_options, '' ); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
foreach ( $rows as $row ) { |
| 89 |
$lov_value = $row[$lookup_column_name[ $source_column_name ]] . ' (' . $row[$target_column_name] . ')'; |
| 90 |
array_push( $lov_values , $lov_value ); |
| 91 |
array_push( $lov_options, $row[$target_column_name] ); |
| 92 |
} |
| 93 |
|
| 94 |
$item->set_data_type( 'enum' ); |
| 95 |
$item->set_enum( $lov_values ); |
| 96 |
$item->set_enum_options( $lov_options ); |
| 97 |
$item->set_item_hide_icon( true ); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
} |