| 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\Connection\WPDADB; |
| 12 |
use WPDataProjects\Simple_Form\WPDP_Simple_Form; |
| 13 |
use WPDataAccess\Utilities\WPDA_Message_Box; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class WPDP_Child_Form extends WPDP_Simple_Form |
| 17 |
* |
| 18 |
* @see WPDP_Simple_Form |
| 19 |
* |
| 20 |
* @author Peter Schulz |
| 21 |
* @since 2.0.0 |
| 22 |
*/ |
| 23 |
class WPDP_Child_Form extends WPDP_Simple_Form { |
| 24 |
|
| 25 |
/** |
| 26 |
* Possible values: edit and view |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $mode; |
| 31 |
|
| 32 |
/** |
| 33 |
* Saved initially requested action |
| 34 |
* |
| 35 |
* Action is changed during processing |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected $initial_action; |
| 40 |
|
| 41 |
/** |
| 42 |
* Parent key values |
| 43 |
* |
| 44 |
* @var array |
| 45 |
*/ |
| 46 |
protected $parent; |
| 47 |
|
| 48 |
/** |
| 49 |
* Child relationships (for actual parent) |
| 50 |
* |
| 51 |
* @var array |
| 52 |
*/ |
| 53 |
protected $child; |
| 54 |
|
| 55 |
/** |
| 56 |
* WPDP_Child_Form constructor |
| 57 |
* |
| 58 |
* @param $schema_name |
| 59 |
* @param $table_name |
| 60 |
* @param $wpda_list_columns |
| 61 |
* @param array $args |
| 62 |
*/ |
| 63 |
public function __construct( $schema_name, $table_name, $wpda_list_columns, $args = array() ) { |
| 64 |
if ( isset( $args['mode'] ) ) { |
| 65 |
$this->mode = $args['mode']; |
| 66 |
} else { |
| 67 |
wp_die( __( 'ERROR: Wrong arguments [missing mode]', 'wp-data-access' ) ); |
| 68 |
} |
| 69 |
|
| 70 |
if ( isset( $args['parent'] ) ) { |
| 71 |
$this->parent = $args['parent']; |
| 72 |
} else { |
| 73 |
wp_die( __( 'ERROR: Wrong arguments [missing parent]', 'wp-data-access' ) ); |
| 74 |
} |
| 75 |
|
| 76 |
if ( isset( $args['child'] ) ) { |
| 77 |
$this->child = $args['child']; |
| 78 |
} else { |
| 79 |
wp_die( __( 'ERROR: Wrong arguments [missing child]', 'wp-data-access' ) ); |
| 80 |
} |
| 81 |
|
| 82 |
$this->page_number_item_name = 'child_page_number'; |
| 83 |
|
| 84 |
parent::__construct( $schema_name, $table_name, $wpda_list_columns, $args ); |
| 85 |
|
| 86 |
$this->initial_action = $this->action; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Overwrite method to add parent argument to get back to parent child relationship |
| 91 |
*/ |
| 92 |
protected function add_parent_args() { |
| 93 |
foreach ( $this->parent['parent_key'] as $parent_key ) { |
| 94 |
?> |
| 95 |
<input type='hidden' |
| 96 |
name='WPDA_PARENT_KEY*<?php echo( esc_attr( $parent_key ) ); ?>' |
| 97 |
value='<?php echo( esc_attr( $this->parent['parent_key_value'][ $parent_key ] ) ); ?>' |
| 98 |
/> |
| 99 |
<?php |
| 100 |
} |
| 101 |
$child_tab = $this->get_child_tab(); |
| 102 |
?> |
| 103 |
<input type='hidden' name='mode' value='<?php echo esc_attr( $this->mode ); ?>'> |
| 104 |
<input type='hidden' name='child_request' value='TRUE'/> |
| 105 |
<input type='hidden' name='child_tab' value='<?php echo esc_attr( $child_tab ); ?>'/> |
| 106 |
<?php |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get actual child tab |
| 111 |
* |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
protected function get_child_tab() { |
| 115 |
if ( isset( $_REQUEST['child_tab'] ) ) { |
| 116 |
return sanitize_text_field( wp_unslash( $_REQUEST['child_tab'] ) ); // input var okay. |
| 117 |
} else { |
| 118 |
return ''; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Overwrites method add_parent_args_to_back_button to add parent arguments |
| 124 |
* |
| 125 |
* @return string|void |
| 126 |
*/ |
| 127 |
protected function add_parent_args_to_back_button() { |
| 128 |
$child_tab = $this->get_child_tab(); |
| 129 |
$args = "&action=list&mode={$this->mode}&child_request=TRUE&child_tab=$child_tab"; |
| 130 |
foreach ( $this->parent['parent_key'] as $parent_key ) { |
| 131 |
$args .= '&' . esc_attr( $parent_key ) . '=' . esc_attr( $this->parent['parent_key_value'][ $parent_key ] ); |
| 132 |
} |
| 133 |
|
| 134 |
return $args; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Overwrites method prepare_row to prepare child insert |
| 139 |
*/ |
| 140 |
protected function prepare_row() { |
| 141 |
parent::prepare_row(); |
| 142 |
|
| 143 |
if ( 'new' === $this->initial_action && 'edit' === $this->action && 'save' === $this->action2 ) { |
| 144 |
if ( isset( $this->child['relation_nm'] ) ) { |
| 145 |
// Create relationship (n:m). |
| 146 |
// Prepare values. |
| 147 |
if ( - 1 !== $this->auto_increment_value ) { |
| 148 |
$child_columns = array( |
| 149 |
$this->child['relation_nm']['child_table_select'][0] => |
| 150 |
$this->auto_increment_value, |
| 151 |
$this->child['relation_nm']['child_table_where'][0] => |
| 152 |
$this->parent['parent_key_value'][ $this->parent['parent_key'][0] ], |
| 153 |
); |
| 154 |
} else { |
| 155 |
$index = 0; |
| 156 |
foreach ( $this->child['relation_nm']['parent_key'] as $parent_key ) { |
| 157 |
$child_columns[ $this->child['relation_nm']['child_table_select'][ $index ] ] = |
| 158 |
$this->form_items_new_values[ $this->child['relation_nm']['parent_key'][ $parent_key ] ]; |
| 159 |
$child_columns[ $this->child['relation_nm']['child_table_where'][ $index ] ] = |
| 160 |
$this->parent['parent_key_value'][ $this->parent['parent_key'][ $index ] ]; |
| 161 |
$index ++; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// Perform insert. |
| 166 |
$wpdadb = WPDADB::get_db_connection( $this->schema_name ); |
| 167 |
if ( null === $wpdadb ) { |
| 168 |
wp_die( sprintf( __( 'ERROR - Remote database %s not available', 'wp-data-access' ), esc_attr( $this->schema_name ) ) ); |
| 169 |
} |
| 170 |
$result = $wpdadb->insert( $this->child['relation_nm']['child_table'], $child_columns ); |
| 171 |
|
| 172 |
// Error handling. |
| 173 |
if ( false === $result ) { |
| 174 |
$this->relationship_insert_failed(); |
| 175 |
} elseif ( 1 !== $result ) { |
| 176 |
$this->relationship_insert_failed(); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Message shown on insert error |
| 184 |
*/ |
| 185 |
protected function relationship_insert_failed() { |
| 186 |
$msg = new WPDA_Message_Box( |
| 187 |
array( |
| 188 |
'message_text' => __( 'Error create relationship (insert failed)', 'wp-data-access' ), |
| 189 |
'message_type' => 'error', |
| 190 |
'message_is_dismissible' => false, |
| 191 |
) |
| 192 |
); |
| 193 |
$msg->box(); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Overwrites method prepare_items to add parent key values |
| 198 |
* |
| 199 |
* @param bool $set_back_form_values |
| 200 |
*/ |
| 201 |
protected function prepare_items( $set_back_form_values = false ) { |
| 202 |
parent::prepare_items( $set_back_form_values ); |
| 203 |
|
| 204 |
if ( isset( $this->child['relation_1n'] ) ) { |
| 205 |
foreach ( $this->child['relation_1n']['child_key'] as $child_key ) { |
| 206 |
$child_index = $this->get_item_index( $child_key ); |
| 207 |
$child_item = $this->form_items[ $child_index ]; |
| 208 |
if ( $child_item->get_item_name() === $child_key ) { |
| 209 |
foreach ( $this->child['relation_1n']['parent_key'] as $parent_key ) { |
| 210 |
foreach ( $this->parent['parent_key'] as $pkey ) { |
| 211 |
if ( $parent_key === $pkey ) { |
| 212 |
$child_item->set_item_default_value( $this->parent['parent_key_value'][ $parent_key ] ); |
| 213 |
$child_item->set_hide_item( true ); |
| 214 |
$child_item->set_foreign_key_column( true ); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|