-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJspreadsheetdb.php
157 lines (136 loc) · 4.57 KB
/
Jspreadsheetdb.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
use bossanova\Database\Database;
class Jspreadsheetdb
{
/** Tablename **/
public $table = null;
/** Database instance **/
public $database = null;
/** Primary key **/
public $primaryKey = null;
/**
* Create the jexcel to postgresql instance
* @param Database $instance
* @param string $table
*/
public function __construct(Database $instance = null, $table)
{
if (isset($instance)) {
$this->database = $instance;
} else {
$this->database = Database::getInstance();
}
$this->table = $table;
}
/**
* Set the primary key of table
* @param string $primaryKey
*/
public function setPrimaryKey($primaryKey)
{
$this->primaryKey = $primaryKey;
}
/**
* Save the information back to the table
* @param array $post
* @return string[]
*/
public function save($post)
{
// Parse the posted json
$request = json_decode($post['data'], true);
// Process the updateData request
if (isset($request['updateData']) && $request['updateData']) {
// Process all records
foreach ($request['updateData'] as $v) {
// Verify if the record exists in the table
$result = $this->database->table($this->table)
->argument(1, $this->primaryKey, $v['id'])
->execute();
// Exists, so update the table with the correct columns
if ($this->database->fetch_assoc($result)) {
$this->database->column($v['data'], true)
->update()
->execute();
} else {
// No, create a new record
$this->database->column($v['data'], true)
->insert()
->execute();
}
}
return [
'success' => 1,
'message' => 'Updated',
];
} else {
return [
'success' => 1,
'message' => 'This action is not yet being persisted',
];
}
}
public function create($options)
{
// Basic configurations, can be replaced
$config = array_merge([
'columns' => [],
'data' => [],
'persistence' => '/',
'defaultColWidth' => '200px',
'minDimensions' => [3, 10]
], $options);
// Get all column information
if ($rows = $this->database->getColumns($this->table)) {
// Get the data in the table
$result = $this->database->table($this->table)
->order($this->primaryKey)
->execute();
// Load all records
while ($row = $this->database->fetch_assoc($result)) {
$config['data'][] = $row;
}
foreach ($rows as $v) {
// Crate the columns
$column = [
'type' => $this->getType($v['data_type']),
'name' => $v['column_name'],
'title' => $v['column_name'],
];
// This columns is the primary key
if ($this->primaryKey && $this->primaryKey == $v['column_name']) {
$column['primaryKey'] = true;
$column['readOnly'] = true;
$column['type'] = 'number';
$column['width'] = '80px';
}
$config['columns'][] = $column;
}
// Stringfy the configuration
$config = json_encode($config);
// Randon ID just in case
if (! isset($options['id'])) {
$options['id'] = 'j' . rand(1000,9000);
}
// Create the Jspreadsheet configuration
return "<div id='{$options['id']}'></div><script>jspreadsheet(document.getElementById('{$options['id']}'), { worksheets: [$config] });</script>";
} else {
// Table not found
return 'Table not found';
}
}
/**
* Treat more cases
* @param string $type
* @return string
*/
public function getType($type) {
if (strpos($type, 'int') !== false || strpos($type, 'numeric') !== false) {
return 'number';
} else if (strpos($type, 'timestamp') !== false || strpos($type, 'calendar') !== false) {
return 'calendar';
} else {
return 'text';
}
}
}