| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 5 |
* |
| 6 |
* @package WPDataProjects\Data_Dictionary |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPDataProjects\Data_Dictionary { |
| 10 |
|
| 11 |
use Cassandra\Varint; |
| 12 |
use \WPDataAccess\Data_Dictionary\WPDA_List_Columns; |
| 13 |
use \WPDataAccess\Plugin_Table_Models\WPDP_Project_Design_Table_Model; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class WPDP_List_Columns |
| 17 |
* |
| 18 |
* Taken from WPDA_List_Columns. This class adds extra functionality for Data Projects. Column headers |
| 19 |
* defined in 'Manage Table Options' are taken into account in this class. |
| 20 |
* |
| 21 |
* @author Peter Schulz |
| 22 |
* @since 2.0.0 |
| 23 |
*/ |
| 24 |
class WPDP_List_Columns extends WPDA_List_Columns { |
| 25 |
|
| 26 |
/** |
| 27 |
* Options set name |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $setname = 'default'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Possible values: listtable and tableform |
| 35 |
* |
| 36 |
* @var label_type |
| 37 |
*/ |
| 38 |
protected $label_type; |
| 39 |
|
| 40 |
/** |
| 41 |
* Column options as define in Data Projects |
| 42 |
* |
| 43 |
* @var null|array |
| 44 |
*/ |
| 45 |
protected $column_options = null; |
| 46 |
|
| 47 |
protected $parent_columns = null; |
| 48 |
|
| 49 |
/** |
| 50 |
* WPDP_List_Columns constructor. |
| 51 |
* |
| 52 |
* @param string $schema_name Database schema name |
| 53 |
* @param string $table_name Database table name |
| 54 |
* @param string $label_type Label type |
| 55 |
* @param string $setname Options set name |
| 56 |
*/ |
| 57 |
public function __construct( $schema_name, $table_name, $label_type, $setname = 'default' ) { |
| 58 |
$this->label_type = $label_type; |
| 59 |
$this->setname = $setname; |
| 60 |
|
| 61 |
$column_options = WPDP_Project_Design_Table_Model::get_column_options( $table_name, $this->label_type, $this->setname, $schema_name ); |
| 62 |
if ( null !== $column_options ) { |
| 63 |
foreach ( $column_options as $column_option ) { |
| 64 |
if ( isset( $column_option->column_name ) ) { |
| 65 |
$option = array( |
| 66 |
'label' => isset( $column_option->label ) ? $column_option->label : null, |
| 67 |
'show' => isset( $column_option->show ) ? $column_option->show : null, |
| 68 |
'readonly' => isset( $column_option->readonly ) ? $column_option->readonly : null, |
| 69 |
'less' => isset( $column_option->less ) ? $column_option->less : null, |
| 70 |
'default' => isset( $column_option->default ) ? $column_option->default : null, |
| 71 |
); |
| 72 |
$this->column_options[ $column_option->column_name ] = $option; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
parent::__construct( $schema_name, $table_name ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get column label (overwrites default method) |
| 82 |
* |
| 83 |
* Take column label from structure or default if not found (call parent method) |
| 84 |
* |
| 85 |
* @param string $column_name Database column name |
| 86 |
* |
| 87 |
* @return string Column label |
| 88 |
*/ |
| 89 |
public function get_column_label( $column_name ) { |
| 90 |
if ( null !== $this->column_options && isset( $this->table_column_headers[ $column_name ] ) ) { |
| 91 |
return $this->table_column_headers[ $column_name ]; |
| 92 |
} else { |
| 93 |
return parent::get_column_label( $column_name ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Return options set name |
| 99 |
* |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
public function get_setname() { |
| 103 |
return $this->setname; |
| 104 |
} |
| 105 |
|
| 106 |
public function get_all_columns() { |
| 107 |
return $this->parent_columns; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Set table columns (overwrites default method) |
| 112 |
* |
| 113 |
* Calls parent method to perform query and then sorts the result |
| 114 |
*/ |
| 115 |
protected function set_table_columns() { |
| 116 |
parent::set_table_columns(); |
| 117 |
|
| 118 |
// Save (all) columns |
| 119 |
$this->parent_columns = $this->get_table_columns(); |
| 120 |
|
| 121 |
// Reorder table columns according to sequence defined by user. |
| 122 |
$table_columns_sorted = array(); |
| 123 |
if ( ! isset( $this->table_columns ) ) { |
| 124 |
wp_die( __( 'ERROR: Wrong arguments [no table columns]', 'wp-data-access' ) ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( null !== $this->column_options ) { |
| 128 |
foreach ( $this->table_columns as $table_column ) { |
| 129 |
if ( isset( $this->column_options[ $table_column['column_name'] ] ) ) { |
| 130 |
$column_option = $this->column_options[ $table_column['column_name'] ]; |
| 131 |
|
| 132 |
if ( isset( $column_option['show'] ) && 'on' === $column_option['show'] && 'listtable' === $this->label_type ) { |
| 133 |
$table_columns_sorted[ $table_column['column_name'] ] = $table_column; |
| 134 |
} elseif ( 'tableform' === $this->label_type ) { |
| 135 |
if ( isset( $column_option['show'] ) && 'off' === $column_option['show'] ) { |
| 136 |
$table_column['show'] = false; |
| 137 |
} |
| 138 |
|
| 139 |
if ( isset( $column_option['readonly'] ) && 'on' === $column_option['readonly'] ) { |
| 140 |
$table_column['readonly'] = true; |
| 141 |
} |
| 142 |
|
| 143 |
if ( isset( $column_option['less'] ) && 'off' === $column_option['less'] ) { |
| 144 |
$table_column['less'] = false; |
| 145 |
} |
| 146 |
|
| 147 |
if ( isset( $column_option['default'] ) && '' !== $column_option['default'] ) { |
| 148 |
$table_column['default'] = $column_option['default']; |
| 149 |
} |
| 150 |
|
| 151 |
// $table_columns_sorted[] = $table_column; |
| 152 |
$table_columns_sorted[ $table_column['column_name'] ] = $table_column; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
// Re-order columns |
| 158 |
$table_columns_resorted = array(); |
| 159 |
foreach ( $this->column_options as $column_name => $column_options ) { |
| 160 |
if ( isset( $table_columns_sorted[ $column_name ] ) ) { |
| 161 |
$table_columns_resorted[] = $table_columns_sorted[ $column_name ]; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
$this->table_columns = $table_columns_resorted; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Set table column headers |
| 171 |
* |
| 172 |
* Use headers if a structure is found for the given table. Otherwise call parent to use the default. |
| 173 |
*/ |
| 174 |
protected function set_table_column_headers() { |
| 175 |
if ( ! isset( $this->table_columns ) ) { |
| 176 |
wp_die( __( 'ERROR: Wrong arguments [no table columns]', 'wp-data-access' ) ); |
| 177 |
} |
| 178 |
|
| 179 |
if ( null === $this->column_options ) { |
| 180 |
parent::set_table_column_headers(); |
| 181 |
} else { |
| 182 |
$this->table_column_headers = array(); |
| 183 |
|
| 184 |
foreach ( $this->table_columns as $key => $value ) { |
| 185 |
$column_option = $this->column_options[ $value['column_name'] ]; |
| 186 |
if ( isset( $column_option['label'] ) ) { |
| 187 |
$this->table_column_headers[ $value['column_name'] ] = $column_option['label']; |
| 188 |
} else { |
| 189 |
$this->table_column_headers[ $value['column_name'] ] = $this->get_column_label( $value['column_name'] ); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Gets the index of the column |
| 197 |
* |
| 198 |
* @param string $column_options Array containing columns and their options |
| 199 |
* @param string $column_name Database column name |
| 200 |
* |
| 201 |
* @return int Column index |
| 202 |
*/ |
| 203 |
private function get_array_index( $column_options, $column_name ) { |
| 204 |
$index = 0; |
| 205 |
foreach ( $column_options as $column_option ) { |
| 206 |
if ( isset( $column_option->column_name ) && $column_option->column_name === $column_name ) { |
| 207 |
return $index; |
| 208 |
} |
| 209 |
$index ++; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
} |
| 216 |
|