forked from AccelerationNet/wp-db-table-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBTableEditor.class.php
91 lines (87 loc) · 2.71 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
<?php
/**
* Classes to enable wp-db-table-editor to work
*
* @package wp-db-table-editor
*/
/*
* 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
*/
class DBTE_DataTable {
var $rows,$columns, $columnNames;
function DBTE_DataTable($args=null){
global $wpdb;
$args = wp_parse_args($args);
$sql = @$args['sql'];
$where = @$args['where'];
if($sql){
if($where){
if(is_array($where)) $where = implode(' AND ', $where);
if(strrpos(strtolower($sql) ,'where') > 0) $sql .= " AND ";
else $sql .= " WHERE ";
$sql .= ' ('.$where.') ';
}
$this->rows = $wpdb->get_results($sql, ARRAY_N);
}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, $insert_cb, $update_cb, $delete_cb,
$id_column, $auto_date;
function DBTableEditor($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){
$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;
}
}