Skip to content

Commit 9fbc382

Browse files
author
Paul
committed
updates
1 parent 34c4dfb commit 9fbc382

10 files changed

+312
-131
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
composer.lock

composer.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"name": "Jspreadsheet", "email": "[email protected]" }
1010
],
1111
"require": {
12-
"bossanova/bossanova" : "dev-master",
13-
"jspreadsheet/db": "dev-main"
12+
"bossanova/bossanova" : "dev-master"
1413
}
1514
}

config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@
8484
define('DB_CONFIG_HOST', 'postgresql');
8585
define('DB_CONFIG_USER', 'postgres');
8686
define('DB_CONFIG_PASS', 'postgres');
87-
define('DB_CONFIG_NAME', 'your-database');
87+
define('DB_CONFIG_NAME', 'test');
8888
}

docker-compose.yml

+65-33
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,68 @@
1-
postgresql:
2-
image: postgres:latest
3-
restart: always
4-
environment:
5-
POSTGRES_DB: postgres
6-
POSTGRES_USER: postgres
7-
POSTGRES_PASSWORD: postgres
8-
ports:
9-
- 54320:5432
10-
volumes:
11-
- .:/var/www/html
12-
- postgres_data:/var/lib/postgresql/data
1+
version: "3.7"
2+
services:
3+
postgresql:
4+
image: postgres:latest
5+
restart: always
6+
environment:
7+
POSTGRES_DB: postgres
8+
POSTGRES_USER: postgres
9+
POSTGRES_PASSWORD: postgres
10+
ports:
11+
- 54320:5432
12+
volumes:
13+
- .:/var/www/html
14+
- postgres_data:/var/lib/postgresql/data
15+
working_dir: /var/www/html
1316

14-
web:
15-
build: ./resources/docker/aws
16-
ports:
17-
- 8081:80
18-
volumes:
19-
- .:/var/www/html
20-
links:
21-
- php
17+
pgadmin:
18+
image: dpage/pgadmin4
19+
restart: always
20+
environment:
21+
PGADMIN_DEFAULT_EMAIL: postgres
22+
PGADMIN_DEFAULT_PASSWORD: postgres
23+
PGADMIN_LISTEN_PORT: 80
24+
ports:
25+
- "8035:80"
26+
volumes:
27+
- postgres_data:/var/lib/postgresql/data
28+
links:
29+
- postgresql
2230

23-
php:
24-
build: ./resources/docker/php
25-
ports:
26-
- 9000
27-
volumes:
28-
- .:/var/www/html
29-
links:
30-
- redis
31-
- postgresql
31+
redis:
32+
image: redis:alpine
33+
expose:
34+
- 6379
3235

33-
redis:
34-
image: redis:alpine
35-
expose:
36-
- 6379
36+
mailhog:
37+
image: mailhog/mailhog
38+
ports:
39+
- 1021:1025
40+
- 8021:8025
41+
42+
web:
43+
build: ./resources/docker/aws
44+
ports:
45+
- 8009:80
46+
- 8010:443
47+
volumes:
48+
- .:/var/www/html
49+
links:
50+
- php
51+
52+
php:
53+
build: ./resources/docker/php
54+
ports:
55+
- 9000
56+
volumes:
57+
- .:/var/www/html
58+
links:
59+
- redis
60+
- postgresql
61+
- mailhog
62+
depends_on:
63+
- redis
64+
- postgresql
65+
- mailhog
66+
67+
volumes:
68+
postgres_data:

public/Jspreadsheetdb.php

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
use bossanova\Database\Database;
4+
5+
class Jspreadsheetdb
6+
{
7+
/** Tablename **/
8+
public $table = null;
9+
10+
/** Database instance **/
11+
public $database = null;
12+
13+
/** Primary key **/
14+
public $primaryKey = null;
15+
16+
/**
17+
* Create the jexcel to postgresql instance
18+
* @param Database $instance
19+
* @param string $table
20+
*/
21+
public function __construct(Database $instance = null, $table)
22+
{
23+
if (isset($instance)) {
24+
$this->database = $instance;
25+
} else {
26+
$this->database = Database::getInstance();
27+
}
28+
29+
$this->table = $table;
30+
}
31+
32+
/**
33+
* Set the primary key of table
34+
* @param string $primaryKey
35+
*/
36+
public function setPrimaryKey($primaryKey)
37+
{
38+
$this->primaryKey = $primaryKey;
39+
}
40+
41+
/**
42+
* Save the information back to the table
43+
* @param array $post
44+
* @return string[]
45+
*/
46+
public function save($post)
47+
{
48+
// Parse the posted json
49+
$request = json_decode($post['data'], true);
50+
51+
// Process the setValue request
52+
if (isset($request['setValue']) && $request['setValue']) {
53+
// Process all records
54+
foreach ($request['setValue'] as $v) {
55+
// Verify if the record exists in the table
56+
$result = $this->database->table($this->table)
57+
->argument(1, $this->primaryKey, $v['id'])
58+
->execute();
59+
60+
// Exists, so update the table with the correct columns
61+
if ($this->database->fetch_assoc($result)) {
62+
$this->database->column($v['data'], true)
63+
->update()
64+
->execute();
65+
} else {
66+
// No, create a new record
67+
$this->database->column($v['data'], true)
68+
->insert()
69+
->execute();
70+
}
71+
}
72+
73+
return [
74+
'success' => 1,
75+
'message' => 'Updated',
76+
];
77+
} else if (isset($request['insertRow']) && $request['insertRow']) {
78+
// No, create a new record
79+
$this->database->column($v['data'], true)
80+
->insert()
81+
->execute();
82+
}
83+
}
84+
85+
public function create($options)
86+
{
87+
// Basic configurations, can be replaced
88+
$config = array_merge([
89+
'columns' => [],
90+
'data' => [],
91+
'persistence' => '/',
92+
'defaultColWidth' => '100px',
93+
'tableOverflow' => true,
94+
'tableWidth' => '1200px'
95+
], $options);
96+
97+
// Get all column information
98+
if ($rows = $this->database->getColumns($this->table)) {
99+
// Get the data in the table
100+
$result = $this->database->table($this->table)
101+
->order($this->primaryKey)
102+
->execute();
103+
104+
// Load all records
105+
while ($row = $this->database->fetch_assoc($result)) {
106+
$config['data'][] = $row;
107+
}
108+
109+
foreach ($rows as $v) {
110+
// Crate the columns
111+
$column = [
112+
'type' => $this->getType($v['data_type']),
113+
'name' => $v['column_name'],
114+
'title' => $v['column_name'],
115+
];
116+
117+
// This columns is the primary key
118+
if ($this->primaryKey && $this->primaryKey == $v['column_name']) {
119+
$column['primaryKey'] = true;
120+
$column['readOnly'] = true;
121+
$column['type'] = 'number';
122+
}
123+
124+
$config['columns'][] = $column;
125+
}
126+
127+
// Stringfy the configuration
128+
$config = json_encode($config);
129+
130+
// Randon ID just in case
131+
if (! isset($options['id'])) {
132+
$options['id'] = 'j' . rand(1000,9000);
133+
}
134+
135+
// Create the Jexcel configuration
136+
return "<div id='{$options['id']}'></div><script>jspreadsheet(document.getElementById('{$options['id']}'), { worksheets: [$config] });</script>";
137+
} else {
138+
// Table not found
139+
return 'Not found';
140+
}
141+
}
142+
143+
/**
144+
* Treat more cases
145+
* @param string $type
146+
* @return string
147+
*/
148+
public function getType($type) {
149+
if (strpos($type, 'int') !== false || strpos($type, 'numeric') !== false) {
150+
return 'number';
151+
} else if (strpos($type, 'timestamp') !== false || strpos($type, 'calendar') !== false) {
152+
return 'calendar';
153+
} else {
154+
return 'text';
155+
}
156+
}
157+
}

public/index.php

+13-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
include 'config.php';
66
include 'vendor/autoload.php';
7+
include 'public/Jspreadsheetdb.php';
78

89
use \bossanova\Database\Database;
910
use \bossanova\Render\Render;
@@ -22,21 +23,21 @@
2223
$license = 'YjQzMzdlOTRiOGY3ZTQ0ZDQ4ZTI1YWU3MDFjMDI0ZWJmOTNjODA1NWFiZTRiNDJhNmRiYTJlZjkwODQ3N2IwMWRmNWRjYWUwZDViM2VhMmI3NzVjOTcwMzVlN2ZhODI1Y2EyMmE3NDI0ZmE0ZjVmNGQ2MWEzN2M3MTA4MThhMDUsZXlKdVlXMWxJam9pY0dGMWJDNW9iMlJsYkNJc0ltUmhkR1VpT2pFMk16SXdPVEkwTURBc0ltUnZiV0ZwYmlJNld5SnFjMlpwWkdSc1pTNXVaWFFpTENKcVpYaGpaV3d1Ym1WMElpd2lZMjlrWlhOaGJtUmliM2d1YVc4aUxDSnFjMmhsYkd3dWJtVjBJaXdpTVROemQyMHVZM05pTG1Gd2NDSXNJbXh2WTJGc2FHOXpkQ0pkTENKd2JHRnVJam9pTXlKOQ==';
2324

2425
// Jspreasdheet Db class - Updates the table users to whatever table name
25-
$jexcel = new \jexcel\Jexceldb($database, 'users');
26+
$jss = new \Jspreadsheetdb($database, 'users');
2627
// Define the primary key
27-
$jexcel->setPrimaryKey('user_id');
28+
$jss->setPrimaryKey('user_id');
2829

2930
if (Render::isAjax()) {
30-
echo $jexcel->save($_POST);
31+
echo $jss->save($_POST);
3132
} else {
3233
echo '<html>
33-
<script src="https://jspreadsheet.com/v7/jspreadsheet.js"></script>
34-
<link rel="stylesheet" href="https://jspreadsheet.com/v7/jspreadsheet.css" type="text/css" />';
35-
<script src="https://jsuites.net/v4/jsuites.js"></script>
36-
<link rel="stylesheet" href="https://jsuites.net/v4/jsuites.css" type="text/css" />
37-
38-
echo $jexcel->create([
39-
'license' => $license,
40-
'persistance' => 'jexcel',
41-
]);
34+
<script src="https://jspreadsheet.com/v10/jspreadsheet.js"></script>
35+
<link rel="stylesheet" href="https://jspreadsheet.com/v10/jspreadsheet.css" type="text/css" />
36+
<script src="https://jsuites.net/v5/jsuites.js"></script>
37+
<link rel="stylesheet" href="https://jsuites.net/v5/jsuites.css" type="text/css" />
38+
<script>
39+
jspreadsheet.setLicense("Y2EzNjI4ODU4Y2Q5MTZmMWQwNWMwMWUwM2VlMzVkMzYwYjcyMWFjNTE4YzExYjgzMjlhYTc3MzBlOGM5NDQ1N2NmZTk2N2JjYzYxZGVkNDM2NjQxY2M4YTA2YzhhMDgyMGRlNTA3OTViMmU4ZGRkODY0MDljZmI0ODBjOTQ2NmUsZXlKdVlXMWxJam9pU25Od2NtVmhaSE5vWldWMElpd2laR0YwWlNJNk1UY3dOekExTURRNE9Td2laRzl0WVdsdUlqcGJJbXB6Y0hKbFlXUnphR1ZsZEM1amIyMGlMQ0pqYjJSbGMyRnVaR0p2ZUM1cGJ5SXNJbXB6YUdWc2JDNXVaWFFpTENKamMySXVZWEJ3SWl3aWQyVmlJaXdpYkc5allXeG9iM04wSWwwc0luQnNZVzRpT2lJek5DSXNJbk5qYjNCbElqcGJJblkzSWl3aWRqZ2lMQ0oyT1NJc0luWXhNQ0lzSW1Ob1lYSjBjeUlzSW1admNtMXpJaXdpWm05eWJYVnNZU0lzSW5CaGNuTmxjaUlzSW5KbGJtUmxjaUlzSW1OdmJXMWxiblJ6SWl3aWFXMXdiM0owWlhJaUxDSmlZWElpTENKMllXeHBaR0YwYVc5dWN5SXNJbk5sWVhKamFDSXNJbkJ5YVc1MElpd2ljMmhsWlhSeklsMHNJbVJsYlc4aU9uUnlkV1Y5");
40+
</script>';
41+
42+
echo $jss->create([]);
4243
}

resources/docker/aws/Dockerfile

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
FROM amazonlinux
2-
3-
ENV container docker
4-
5-
RUN yum update -y \
6-
&& amazon-linux-extras install nginx1.12 -y
7-
8-
COPY localhost.conf /etc/nginx/conf.d
9-
10-
EXPOSE 8080
11-
12-
STOPSIGNAL SIGTERM
13-
14-
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
1+
FROM nginx:latest
2+
3+
ENV container docker
4+
5+
COPY default.conf /etc/nginx/conf.d
6+
7+
EXPOSE 80
8+
EXPOSE 443
9+
10+
STOPSIGNAL SIGTERM
11+
12+
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
server {
2-
listen 80;
3-
4-
client_max_body_size 100M;
5-
server_name localhost;
6-
access_log /var/log/nginx/access.log;
7-
error_log /var/log/nginx/error.log;
8-
9-
location / {
10-
root /var/www/html/public/;
11-
index index.php index.html index.htm;
12-
try_files $uri $uri/ /index.php?$args;
13-
}
14-
15-
location ~ \.php$ {
16-
root /var/www/html/public/;
17-
fastcgi_pass php:9000;
18-
fastcgi_index index.php;
19-
include fastcgi_params;
20-
fastcgi_param APPLICATION_ENV dev;
21-
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
22-
}
23-
}
1+
server {
2+
listen 80;
3+
client_max_body_size 100M;
4+
5+
server_name ~^(.+)$
6+
access_log /var/log/nginx/access.log;
7+
error_log /var/log/nginx/error.log;
8+
9+
location / {
10+
root /var/www/html/public/;
11+
index index.php index.html index.htm;
12+
try_files $uri $uri/ /index.php?$args;
13+
}
14+
15+
location ~ \.php$ {
16+
root /var/www/html/public/;
17+
fastcgi_pass php:9000;
18+
fastcgi_index index.php;
19+
include fastcgi_params;
20+
fastcgi_param APPLICATION_ENV dev;
21+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
22+
}
23+
}

0 commit comments

Comments
 (0)