-
Notifications
You must be signed in to change notification settings - Fork 16
/
DBTableEditor.class.php
127 lines (120 loc) · 3.93 KB
/
DBTableEditor.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Classes to enable wp-db-table-editor to work
*
* @package wp-db-table-editor
*/
use PHPSQLParser\PHPSQLParser;
use PHPSQLParser\PHPSQLCreator;
/*
* The primary entrypoint to configuring wp-db-table-editor's
* creates a DBTableEditor instance and puts it in the global
* configuration array
*/
function add_db_table_editor($args=null){
global $DBTE_INSTANCES;
$o = new DBTableEditor($args);
$DBTE_INSTANCES[] = $o;
return $o;
}
/**
* A data table containing column objects and row arrays
* for convenience also contains an array of columnNames
*
* Can be initialized by passing sql, where, or rows & columns
* as arguments associative array
*/
function insert_where($sql, $where){
if(is_array($where)) $where = implode(' AND ', $where);
$sqlparser = new PHPSQLParser();
$sqlcreator = new PHPSQLCreator();
$parsed = $sqlparser->parse($sql);
// a bit of a hack because we are abusing the constant node, but whatever
$whereExp = Array("expr_type"=>"const", "base_expr"=>$where, "sub_tree"=>null);
if(!@$parsed['WHERE']) $parsed['WHERE'] = Array();
else $whereExp['base_expr']=' AND ('.$whereExp['base_expr'].')';
$parsed['WHERE'][]=$whereExp;
$sql = $sqlcreator->create($parsed);
return $sql;
}
class DBTE_DataTable {
var $rows,$columns, $columnNames;
function __construct($args=null){
global $wpdb;
$args = wp_parse_args($args);
$sql = @$args['sql'];
$where = @$args['where'];
if($sql){
if($where){
// this has sometimes gone wrong
$sql = insert_where ($sql, $where);
}
$haslimit = preg_match('/limit\s+\d+/i', $sql);
if(!$haslimit){
$sql .= ' LIMIT '.$limit.' OFFSET '.$offset;
}
// error_log("Got sql:\n".$sql);
$this->rows = $wpdb->get_results($sql, ARRAY_N);
if($wpdb->last_error){
error_log("Failed to execute query:\n$sql\nERR:".$wpdb->last_error."\n");
return null;
}
$this->offset = $offset;
if(!@$args['columns']){
$this->columnNames = $cnames = $wpdb->get_col_info('name');
$ctypes = $wpdb->get_col_info('type');
$this->columns = Array();
for($i=0; $i < count($cnames) ; $i++){
$this->columns[]=Array('name'=>$cnames[$i], 'type'=>$ctypes[$i]);
}
}
}else if(@$args['rows']){
$this->rows = $args['rows'];
}
if(@$args['columns']){
$this->columns = $args['columns'];
}
else{ // handle building columns from wpdb
$this->columnNames = $cnames = $wpdb->get_col_info('name');
$ctypes = $wpdb->get_col_info('type');
$this->columns = Array();
for($i=0; $i < count($cnames) ; $i++){
$this->columns[]=Array('name'=>$cnames[$i], 'type'=>$ctypes[$i]);
}
}
}
}
/**
* A class to contain the configuration state for each DBTableEditor
* that is available
* @access public
*/
class DBTableEditor {
var $table, $title, $sql, $dataFn, $id, $data, $cap, $jsFile,
$noedit, $editcap, $noedit_columns, $hide_columns, $default_values,
$columnFilters, $columnNameMap, $save_cb, $insert_cb, $update_cb, $delete_cb,
$export_id_field,
$id_column, $auto_date, $async_data;
function __construct($args=null){
$args = wp_parse_args($args, array('cap'=>'edit_others_posts'));
foreach($args as $k => $v) $this->{$k} = $v;
if(!$this->id) $this->id = $this->table;
if(!$this->title) $this->title = $this->table;
if(!$this->id_column) $this->id_column = 'id';
if(!isset($args['auto_date'])) $this->auto_date=true;
}
/*
* Gets data from the data source (either sql, or dataFn (prefering sql)
* default is to SELECT * FROM {table}
*/
function getData($args=null){
if(!$args)$args=Array();
$fn = $this->dataFn;
$sql = $this->sql;
if($sql) $args['sql'] = $sql;
else if($fn) $args['rows'] = $fn($args);
else $args["sql"] ="SELECT * FROM $this->table";
$this->data = new DBTE_DataTable($args);
return $this->data;
}
}