Completed
Push — master ( 67078f...11dea5 )
by Sam
02:17
created

src/Controllers/SchemaController.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file contains only one class.
4
 *
5
 * @package Tabulate
6
 * @file
7
 */
8
9
namespace WordPress\Tabulate\Controllers;
10
11
use \WordPress\Tabulate\DB\Database;
12
use \WordPress\Tabulate\DB\Table;
13
use \WordPress\Tabulate\DB\Column;
14
use \WordPress\Tabulate\Template;
15
16
/**
17
 * This controller handles creating, modifying, and deleting tables in the database.
18
 */
19
class SchemaController extends ControllerBase {
20
21
	/**
22
	 * View and edit the structure of the given table.
23
	 *
24
	 * @param string[] $args The request arguments.
25
	 * @return string
26
	 */
27
	public function index( $args ) {
28
		$template = new Template( 'table/schema.html' );
29
		if ( ! current_user_can( 'promote_users' ) ) {
30
			$template->add_notice( 'error', 'Only administrators are allowed to edit table structure.' );
31
		}
32
33
		$db = new Database( $this->wpdb );
34
		$template->action = 'structure';
35
		$template->tables = $db->get_tables();
36
		if ( isset( $args['table'] ) ) {
37
			$template->table = $db->get_table( $args['table'] );
38
			$template->record = $template->table->get_default_record();
39
		}
40
		$template->xtypes = Column::get_xtypes();
41
		return $template->render();
42
	}
43
44
	/**
45
	 * Add a new table and redirect to its schema-editing page.
46
	 *
47
	 * @param string[] $args The request arguments.
48
	 */
49
	public function newtable( $args ) {
50
		// Create table.
51
		$db = new Database( $this->wpdb );
52
		$table = $db->create_table( $args['new_table_name'] );
53
54
		// Redirect user with message.
55
		$template = new Template( 'table/schema.html' );
56
		$template->add_notice( 'updated', 'New table created' );
57
		$url = 'admin.php?page=tabulate&controller=schema&table=' . $table->get_name();
58
		wp_safe_redirect( admin_url( $url ) );
59
		exit;
60
	}
61
62
	/**
63
	 * Save modifications to a table's schema.
64
	 *
65
	 * @param string[] $args The request arguments.
66
	 */
67
	public function save( $args ) {
68
		if ( ! isset( $args['table'] ) || ! current_user_can( 'promote_users' ) ) {
69
			$url = admin_url( 'admin.php?page=tabulate' );
70
			wp_safe_redirect( $url );
71
			exit;
72
		}
73
		$db = new Database( $this->wpdb );
74
		$table = $db->get_table( $args['table'] );
75
		if ( isset( $args['delete'] ) ) {
76
			wp_safe_redirect( $table->get_url( 'delete', null, 'schema' ) );
0 ignored issues
show
null is of type null, but the function expects a false|array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
			exit;
78
		}
79
80
		// Rename.
81
		$new_name = $args['table'];
82
		if ( $table instanceof Table && ! empty( $args['new_name'] ) ) {
83
			$table->rename( $args['new_name'] );
84
			$new_name = $table->get_name();
85
		}
86
87
		// Set comment.
88
		if ( isset( $args['new_comment'] ) ) {
89
			$table->set_comment( $args['new_comment'] );
90
		}
91
92
		// Update columns.
93
		foreach ( $args['columns'] as $col_info ) {
94
			// Validate inputs.
95
			$old_col_name = isset( $col_info['old_name'] ) ? $col_info['old_name'] : null;
96
			$new_col_name = isset( $col_info['new_name'] ) ? $col_info['new_name'] : null;
97
			$xtype = isset( $col_info['xtype'] ) ? $col_info['xtype'] : null;
98
			$size = isset( $col_info['size'] ) ? $col_info['size'] : null;
99
			$nullable = isset( $col_info['nullable'] );
100
			$default = isset( $col_info['default'] ) ? $col_info['default'] : null;
101
			$auto_increment = isset( $args['auto_increment'] ) && $args['auto_increment'] === $old_col_name;
102
			$unique = isset( $col_info['unique'] );
103
			$comment = isset( $col_info['comment'] ) ? $col_info['comment'] : null;
104
			$target_table = isset( $col_info['target_table'] ) ? $db->get_table( $col_info['target_table'] ) : null;
105
			$after = isset( $col_info['after'] ) ? $col_info['after'] : null;
106
107
			// Change existing or insert new column.
108
			$altered = false;
109
			if ( $old_col_name ) {
110
				$col = $table->get_column( $col_info['old_name'] );
111
				if ( $col instanceof Column ) {
112
					$col->alter( $new_col_name, $xtype, $size, $nullable, $default, $auto_increment, $unique, $comment, $target_table, $after );
113
					$altered = true;
114
				}
115
			}
116
			if ( ! $altered && ! empty( $new_col_name ) ) {
117
				$table->add_column( $new_col_name, $xtype, $size, $nullable, $default, $auto_increment, $unique, $comment, $target_table, $after );
118
			}
119
		}
120
121
		// Finish up.
122
		$template = new Template( 'table/schema.html' );
123
		$template->add_notice( 'updated', 'Schema updated.' );
124
		$url = admin_url( 'admin.php?page=tabulate&controller=schema&table=' . $new_name );
125
		wp_safe_redirect( $url );
126
		exit;
127
	}
128
129
	/**
130
	 * Delete (drop) a table.
131
	 *
132
	 * @param string[] $args The request arguments.
133
	 */
134
	public function delete( $args ) {
135
		$template = new Template( 'table/delete.html' );
136
		$db = new Database( $this->wpdb );
137
		$table = $db->get_table( $args['table'] );
138
139
		// Ask for confirmation.
140
		if ( ! isset( $args['confirm_deletion'] ) ) {
141
			$template->table = $table;
142
			return $template->render();
143
		}
144
145
		// Carry out deletion.
146
		$table->drop();
147
		$template->add_notice( 'updated', 'Table dropped.' );
148
		wp_safe_redirect( admin_url( 'admin.php?page=tabulate' ) );
149
		exit;
150
	}
151
}
152