-
Notifications
You must be signed in to change notification settings - Fork 0
/
sb-connect.php
226 lines (201 loc) · 10.8 KB
/
sb-connect.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/**
* Plugin Name: SB Connect
* Plugin URI: https://github.com/majsan/sb-connect
* Description: Show saldo in Systembolaget on your site
* Version: 1.0
* Author: Majsan
* Author URI:
**/
if (!defined('SBCONNECT_PLUGIN_URL')) {
// TODO fix this
//define('SBCONNECT_PLUGIN_URL', plugin_dir_url(__FILE__));
define('SBCONNECT_PLUGIN_URL', 'wp-content/plugins/sb-connect/');
}
// TODO: rename
function get_articles($table)
{
global $wpdb;
$sql = "SELECT * FROM {$wpdb->prefix}{$table}";
$result = $wpdb->get_results($sql, 'ARRAY_A');
return $result;
}
// TODO: rename
function remove_article($table, $id)
{
global $wpdb;
$sql = "DELETE FROM {$wpdb->prefix}{$table} WHERE id={$id}";
$result = $wpdb->get_results($sql, 'ARRAY_A');
return $result;
}
global $sbconnect_db_version;
$sbconnect_db_version = '1.0';
function sbconnect_install()
{
global $wpdb;
global $sbconnect_db_version;
$charset_collate = $wpdb->get_charset_collate();
$articles_table_name = $wpdb->prefix . "sbconnect_articles";
$sites_table_name = $wpdb->prefix . "sbconnect_sites";
$sql1 = "CREATE TABLE $articles_table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
#time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
article_name text NOT NULL,
article_id tinytext NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
$sql2 = "CREATE TABLE $sites_table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
#time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
site_name text NOT NULL,
site_id tinytext NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql1);
dbDelta($sql2);
}
add_action('admin_menu', 'plugin_setup_menu');
register_activation_hook(__FILE__, 'sbconnect_install');
#register_deactivation_hook(__FILE__, 'sb_something');
register_uninstall_hook(__FILE__, 'remove_db_tables');
function plugin_setup_menu()
{
add_menu_page(
'SBConnect',
'SBConnect',
'manage_options',
'sb-connect',
'sbconnect_init',
'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxOS4yLjEsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4KCjxzdmcKICAgeG1sbnM6ZGM9Imh0dHA6Ly9wdXJsLm9yZy9kYy9lbGVtZW50cy8xLjEvIgogICB4bWxuczpjYz0iaHR0cDovL2NyZWF0aXZlY29tbW9ucy5vcmcvbnMjIgogICB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiCiAgIHhtbG5zOnN2Zz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCiAgIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8y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iwtMTIuNiAxMS44LC04LjUgMTguMywtMTguMyAyMiwtMjUuOCAzLjIsLTYuNiAyLjYsLTE0LjQgLTEuNywtMjAuNCBtIC0yMjEsLTIwOC4xIDIuNSwwLjYgYyAwLjksMC4yIDEuNSwxLjEgMS41LDIgbCAtMC4yLDIuMiBjIC0wLjEsMC43IDAuMywxLjQgMC45LDEuOCBsIDIuNywxLjYgYyA0LjQsOC45IDguNiwxNy4xIDEyLjQsMjQuNiAwLjQsMC43IDEuMywwLjkgMS45LDAuMyA2LjMsLTYuNyAxMy44LC0xNC43IDIyLjcsLTIzLjkgMC43LC0wLjggMC44LC0yIDAsLTIuNyAtNC41LC00LjYgLTkuMiwtOS40IC0xNC40LC0xNC42IGwgLTAuNywtMyBjIC0wLjEsLTAuNyAtMC43LC0xLjIgLTEuNCwtMS40IGwgLTIuMSwtMC41IGMgLTAuOSwtMC4yIC0xLjUsLTEuMSAtMS40LC0yIGwgMC4yLC0yLjYgYyAwLjEsLTEuMSAtMC43LC0yIC0xLjgsLTIgLTIuNSwtMC4xIC03LjQsMC42IC0xNC40LDUuNiAtNi45LDUgLTkuMiw5LjQgLTkuOSwxMS44IC0wLjIsMC45IDAuNSwyIDEuNSwyLjIiCiAgIGNsYXNzPSJzdDAiIC8+Cgk8L2c+Cjwvc3ZnPg==',
0
);
add_submenu_page('sb-connect', 'SBConnect', 'Översikt', 'manage_options', 'sb-connect', 'sbconnect_init');
add_submenu_page('sb-connect', 'Lägg till artikel', 'Lägg till artikel', 'manage_options', 'sb-connect-new-article', 'sbconnect_add_new_article');
add_submenu_page('sb-connect', 'Lägg till butik', 'Lägg till butik', 'manage_options', 'sb-connect-new-site', 'sbconnect_add_new_site');
}
function display_rows($rows, $table, $col1, $col2)
{
?>
<table>
<thead>
<tr>
<th><?php echo "{$col1}"; ?></th>
<th><?php echo "{$col2}"; ?></th>
<th>Ta bort</th>
</tr>
</thead>
<tbody>
<?php foreach ($rows as $row) {
echo "<tr>";
echo "<td>{$row[$col1]}</td>";
echo "<td>{$row[$col2]}</td>"; ?>
<td>
<form method="post" class="delete-form">
<input type="hidden" name="page" value="sb-connect">
<input type="hidden" name="table" value="<?php echo $table ?>">
<input type="hidden" name="id" value="<?php echo $row["id"] ?>">
<button type="submit" class="delete-row">
<img src="/<?php echo constant("SBCONNECT_PLUGIN_URL") . "admin/images/icons8-trash-can.svg" ?>"/>
</button>
</form>
</td>
</tr>
<?php
} ?>
</tbody>
</table>
<?php
}
?>
<style>
.delete-row {
border: 0;
}
.delete-form {
margin-block-end: 0em;
}
</style>
<?php
function show_table($table, $col1, $col2)
{
$rows = get_articles($table);
display_rows($rows, $table, $col1, $col2);
}
function sbconnect_init()
{
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// TODO: security, only admin can remove
remove_article($_POST["table"], $_POST["id"]);
} ?>
<h1>SBConnect</h1>
<h2>Artiklar</h2>
<p>Pluginet kan visa lagersaldo för följande artiklar</p>
<div style="margin-bottom: 15px;"><a href="http://localhost/wp-admin/admin.php?page=sb-connect-new-article" class="page-title-action">Lägg till</a></div>
<?php show_table("sbconnect_articles", "article_name", "article_id"); ?>
<h2>Butiker</h2>
<p>Pluginet kommer för varje artikel att visa lagersaldo på följande butiker.</p>
<div style="margin-bottom: 15px;"><a href="http://localhost/wp-admin/admin.php?page=sb-connect-new-site" class="page-title-action">Lägg till</a></div>
<?php show_table("sbconnect_sites", "site_name", "site_id"); ?>
<?php
}
function sbconnect_add_new_article()
{
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
global $wpdb;
$wpdb->insert(
$wpdb->prefix . 'sbconnect_articles',
array(
'article_name' => htmlspecialchars($_POST["article_name"]),
'article_id' => htmlspecialchars($_POST["article_id"])
)
);
echo 'Added ' . htmlspecialchars($_POST["article_name"]) . '!';
// TODO redirect instead of echo
} else {
?>
<h1>Lägg till ny artikel</h1>
<form method="post">
<label style="display: block;">
Artikelnamn:
<input type="text" name="article_name" placeholder="Artikelnamn" />
</label>
<label style="display: block;">
Artikelnummer:
<input type="text" name="article_id" placeholder="Artikelnummer" />
</label>
<input type="submit" value="Lägg till" />
</form>
<?php
}
}
function sbconnect_add_new_site()
{
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
global $wpdb;
$wpdb->insert(
$wpdb->prefix . 'sbconnect_sites',
array(
'site_name' => htmlspecialchars($_POST["site_name"]),
'site_id' => htmlspecialchars($_POST["site_id"])
)
);
echo 'Added ' . htmlspecialchars($_POST["site_name"]) . '!';
// TODO redirect instead of echo
} else {
?>
<h1>Lägg till ny butik</h1>
<form method="post">
<label style="display: block;">
Butiksnamn:
<input type="text" name="site_name" placeholder="Butiksnamn" />
</label>
<label style="display: block;">
Butikens ID:
<input type="text" name="site_id" placeholder="Butikens ID" />
</label>
<input type="submit" value="Lägg till" />
</form>
<?php
}
}