| 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\List_Table\WPDP_List_Table_Lookup; |
| 13 |
use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Exist; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class WPDP_Parent_List_Table extends WPDP_List_Table_Lookup |
| 17 |
* |
| 18 |
* @see WPDP_List_Table_Lookup |
| 19 |
* |
| 20 |
* @author Peter Schulz |
| 21 |
* @since 2.0.0 |
| 22 |
*/ |
| 23 |
class WPDP_Parent_List_Table extends WPDP_List_Table_Lookup { |
| 24 |
|
| 25 |
/** |
| 26 |
* Project info |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
protected $project; |
| 31 |
|
| 32 |
/** |
| 33 |
* User message |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
protected $message_confirm_delete; |
| 38 |
|
| 39 |
/** |
| 40 |
* WPDP_Parent_List_Table constructor |
| 41 |
* |
| 42 |
* @param array $args |
| 43 |
*/ |
| 44 |
public function __construct( $args = array() ) { |
| 45 |
if ( isset( $args['project'] ) ) { |
| 46 |
$this->project = $args['project']; |
| 47 |
} else { |
| 48 |
wp_die( __( 'ERROR: Wrong arguments', 'wp-data-access' ) ); |
| 49 |
} |
| 50 |
|
| 51 |
$args['allow_import'] = 'off'; |
| 52 |
|
| 53 |
parent::__construct( $args ); |
| 54 |
|
| 55 |
$this->message_confirm_delete = |
| 56 |
__( "Delete current item and all its relationships?\\nThis action cannot be undone.\\n\'Cancel\' to stop, \'OK\' to delete.", 'wp-data-access' ); |
| 57 |
|
| 58 |
if ( isset( $args['where_clause'] ) && '' !== $args['where_clause'] ) { |
| 59 |
$this->where = $args['where_clause']; |
| 60 |
} |
| 61 |
|
| 62 |
if ( isset( $args['orderby_clause'] ) && '' !== $args['orderby_clause'] ) { |
| 63 |
$this->orderby = $args['orderby_clause']; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Overwrites method bind_action_buttons to set message_confirm_delete |
| 69 |
*/ |
| 70 |
protected function bind_action_buttons() { |
| 71 |
?> |
| 72 |
<script type='text/javascript'> |
| 73 |
jQuery(function () { |
| 74 |
jQuery("#doaction").click(function () { |
| 75 |
return wpdp_action_button("<?php echo $this->message_confirm_delete; // phpcs:ignore WordPress.Security.EscapeOutput ?>"); |
| 76 |
}); |
| 77 |
jQuery("#doaction2").click(function () { |
| 78 |
return wpdp_action_button("<?php echo $this->message_confirm_delete; // phpcs:ignore WordPress.Security.EscapeOutput ?>"); |
| 79 |
}); |
| 80 |
}); |
| 81 |
</script> |
| 82 |
<?php |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Overwrites method column_default_add_action to set mode |
| 87 |
* |
| 88 |
* @param array $item |
| 89 |
* @param string $column_name |
| 90 |
* @param array $actions |
| 91 |
*/ |
| 92 |
protected function column_default_add_action( $item, $column_name, &$actions ) { |
| 93 |
if ( 'view' === $this->project->get_mode() ) { |
| 94 |
unset( $actions['edit'] ); |
| 95 |
unset( $actions['delete'] ); |
| 96 |
?> |
| 97 |
<script type='text/javascript'> |
| 98 |
jQuery("#view_form_" + <?php echo esc_attr( self::$list_number - 1 ); ?>).append('<input type="hidden" name="mode" value="view">'); |
| 99 |
jQuery("#edit_form_" + <?php echo esc_attr( self::$list_number - 1 ); ?>).remove(); |
| 100 |
jQuery("#delete_form_" + <?php echo esc_attr( self::$list_number - 1 ); ?>).remove(); |
| 101 |
</script> |
| 102 |
<?php |
| 103 |
} else { |
| 104 |
?> |
| 105 |
<script type='text/javascript'> |
| 106 |
jQuery("#" + <?php echo esc_attr( self::$list_number - 1 ); ?>).append('<input type="hidden" name="mode" value="view">'); |
| 107 |
jQuery("#edit_form_" + <?php echo esc_attr( self::$list_number - 1 ); ?>).append('<input type="hidden" name="mode" value="edit">'); |
| 108 |
jQuery("#delete_form_" + <?php echo esc_attr( self::$list_number - 1 ); ?>).append('<input type="hidden" name="mode" value="edit">'); |
| 109 |
</script> |
| 110 |
<?php |
| 111 |
if ( 'off' !== $this->allow_delete ) { |
| 112 |
$title = __( 'Delete parent row and its child rows', 'wp-data-access' ); |
| 113 |
$actions['delete'] = sprintf( |
| 114 |
' |
| 115 |
<a href="javascript:void(0)" class="wpda_tooltip" |
| 116 |
title="' . $title . '" |
| 117 |
onclick="if (confirm(\'%s\')) jQuery(\'%s\').submit()" |
| 118 |
> |
| 119 |
<span style="white-space:nowrap"> |
| 120 |
<i class="fas fa-trash wpda_icon_on_button"></i> |
| 121 |
%s |
| 122 |
</span> |
| 123 |
</a> |
| 124 |
', |
| 125 |
$this->message_confirm_delete, |
| 126 |
'#delete_form_' . strval( self::$list_number - 1 ), |
| 127 |
__( 'Delete', 'wp-data-access' ) |
| 128 |
); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Overwrite method |
| 135 |
* |
| 136 |
* @param array $item |
| 137 |
* |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
protected function add_parent_args_as_string( $item ) { |
| 141 |
$parent_args = ''; |
| 142 |
if ( isset( $this->project ) ) { |
| 143 |
$parents = $this->project->get_parent(); |
| 144 |
if ( isset( $parents['key'] ) && is_array( $parents['key'] ) ) { |
| 145 |
foreach ( $parents['key'] as $key ) { |
| 146 |
$value = null; |
| 147 |
if ( isset( $item[ $key ] ) ) { |
| 148 |
$value = $item[ $key ]; |
| 149 |
} |
| 150 |
$parent_args .= "<input type='hidden' name='WPDA_PARENT_KEY*" . esc_attr( $key ) . "' value='" . esc_attr( $value ) . "' >"; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
return $parent_args; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Overwrite method get_bulk_actions to add action 'Delete Permanently' |
| 159 |
* |
| 160 |
* @return array |
| 161 |
*/ |
| 162 |
public function get_bulk_actions() { |
| 163 |
if ( 'view' === $this->project->get_mode() ) { |
| 164 |
return ''; |
| 165 |
} |
| 166 |
|
| 167 |
if ( ! $this->bulk_actions_enabled ) { |
| 168 |
// Bulk actions disabled. |
| 169 |
return ''; |
| 170 |
} |
| 171 |
|
| 172 |
if ( empty( $this->wpda_list_columns->get_table_primary_key() ) ) { |
| 173 |
// Tables has no primary key: no bulk actions allowed! |
| 174 |
// Primary key is neccesary to ensure uniqueness. |
| 175 |
return ''; |
| 176 |
} |
| 177 |
|
| 178 |
if ( 'off' !== $this->allow_delete ) { |
| 179 |
$actions = parent::get_bulk_actions(); |
| 180 |
} else { |
| 181 |
$actions = array(); |
| 182 |
} |
| 183 |
|
| 184 |
return $actions; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Overwrites method delete_row to delete child rows on parent delete |
| 189 |
* |
| 190 |
* @param string $where where clause |
| 191 |
* @param string $engine connect or empty |
| 192 |
* |
| 193 |
* @return mixed |
| 194 |
*/ |
| 195 |
public function delete_row( $where, $engine = '' ) { |
| 196 |
foreach ( $this->project->get_children() as $child ) { |
| 197 |
if ( isset( $child['relation_nm'] ) ) { |
| 198 |
$table_name = $child['relation_nm']['child_table']; |
| 199 |
$row_to_be_deleted = array(); |
| 200 |
$i = 0; |
| 201 |
foreach ( $where as $key ) { |
| 202 |
$row_to_be_deleted[ $child['relation_nm']['child_table_where'][ $i ] ] = $key; |
| 203 |
$i ++; |
| 204 |
} |
| 205 |
$this->delete_row_relationship( $table_name, $row_to_be_deleted ); |
| 206 |
} elseif ( isset( $child['relation_1n'] ) ) { |
| 207 |
$table_name = $child['table_name']; |
| 208 |
$row_to_be_deleted = array(); |
| 209 |
$i = 0; |
| 210 |
foreach ( $where as $key ) { |
| 211 |
$row_to_be_deleted[ $child['relation_1n']['child_key'][ $i ] ] = $key; |
| 212 |
$i ++; |
| 213 |
} |
| 214 |
$this->delete_row_relationship( $table_name, $row_to_be_deleted ); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
return parent::delete_row( $where, $engine ); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Delete relationship |
| 223 |
* |
| 224 |
* @param string $table_name Database table name |
| 225 |
* @param string $where SQL where clause |
| 226 |
* |
| 227 |
* @return mixed Return value of SQL delete |
| 228 |
*/ |
| 229 |
public function delete_row_relationship( $table_name, $where ) { |
| 230 |
$wpdadb = WPDADB::get_db_connection( $this->schema_name ); |
| 231 |
if ( null === $wpdadb ) { |
| 232 |
wp_die( sprintf( __( 'ERROR - Remote database %s not available', 'wp-data-access' ), esc_attr( $this->schema_name ) ) ); |
| 233 |
} |
| 234 |
|
| 235 |
$cannot_delete_from_view = new WPDA_Dictionary_Exist( $this->schema_name, $table_name ); |
| 236 |
if ( $cannot_delete_from_view->is_view() ) { |
| 237 |
// Cannot delete rows from view |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
return $wpdadb->delete( $table_name, $where ); // db call ok; no-cache ok. |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Overwrites method add_header_button to add arguments |
| 246 |
*/ |
| 247 |
protected function add_header_button() { |
| 248 |
if ( 'edit' === $this->project->get_mode() && 'off' !== $this->allow_insert ) { |
| 249 |
if ( is_admin() ) { |
| 250 |
$url = "?page={$this->page}"; |
| 251 |
} else { |
| 252 |
$url = ''; |
| 253 |
} |
| 254 |
?> |
| 255 |
<form |
| 256 |
method="post" |
| 257 |
action="<?php echo esc_attr( $url ); ?>" |
| 258 |
style="display: inline-block; vertical-align: unset;" |
| 259 |
> |
| 260 |
<div> |
| 261 |
<input type="hidden" name="action" value="new"> |
| 262 |
<input type="hidden" name="mode" value="edit"> |
| 263 |
<input type="hidden" name="table_name" value="<?php echo esc_attr( $this->table_name ); ?>"> |
| 264 |
<button type="submit" class="page-title-action"> |
| 265 |
<i class="fas fa-plus-circle wpda_icon_on_button"></i> |
| 266 |
<?php echo __( 'Add New', 'wp-data-access' ); ?> |
| 267 |
</button> |
| 268 |
</div> |
| 269 |
</form> |
| 270 |
<?php |
| 271 |
if ( null !== $this->wpda_import ) { |
| 272 |
$this->wpda_import->add_button( __( 'Import', 'wp-data-access' ) ); |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
} |
| 280 |
|