-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings_page.php
339 lines (302 loc) · 11.6 KB
/
settings_page.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php namespace wp_tableizer;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
include_once(realpath(__DIR__.'/variables.php'));
global $wpdb;
global $tableizer_tab;
global $tableizer_tab_row_option;
global $tableizer_tab_order;
// Save
// echo '<pre>';print_r($_POST);echo '</pre>';
if (isset($_POST['action']) && $_POST['action'] === 'add_row') {
if (array_key_exists('categories', $_POST)) {
if (is_array($_POST['categories'])) {
$categories = $_POST['categories'];
} else {
$categories = empty($_POST['categories']) ? array() : array($_POST['categories']);
}
} else {
$categories = array();
}
if (array_key_exists('new_category', $_POST) && $_POST['new_category'] != '') {
array_push($categories, $_POST['new_category']);
}
foreach ($_POST['table'] as $row_number => $row) {
$init = $wpdb->get_row("SELECT IFNULL(max(row_id),-1)+1 AS next_row_number FROM {$tableizer_tab}");
$next_row_number = $init->next_row_number;
foreach ($_POST['table'][$row_number] as $col_number => $column) {
// $column = esc_sql( $column );
$wpdb->insert(
$tableizer_tab,
[
'row_id' => $next_row_number,
'value' => $column,
'type' => $_POST['types'][$col_number],
'column' => $col_number,
]
);
}
if (sizeof($categories)>0) {
$wpdb->query("INSERT INTO {$tableizer_tab_row_option} (row_id, option_name, option_value)
VALUES
".(array_key_exists('is_header', $_POST) && $_POST['is_header'] == 'on' ? "('{$next_row_number}','header',1)," : '')."
('{$next_row_number}','category','".implode("'),('{$next_row_number}','category','", $categories)."')
");
}
$wpdb->insert(
$tableizer_tab_row_option,
[
'row_id' => $next_row_number,
'option_name' => 'create_date',
'option_value' => current_time( 'mysql', 0 )
]
);
}
} elseif (isset($_POST['action']) && $_POST['action'] === 'update') {
foreach ($_POST['values'] as $cell_id => $value) {
$wpdb->update(
$tableizer_tab,
[
'value' => $value,
'type' => $_POST['types'][$cell_id]
],
[
'cel_id' => $cell_id
]
);
}
foreach ($_POST['categories'] as $row_id => $categories) {
$wpdb->delete(
$tableizer_tab_row_option,
[
'row_id' => $row_id,
'option_name' => 'category'
]
);
foreach ($categories as $category) {
$wpdb->insert(
$tableizer_tab_row_option,
[
'row_id' => $row_id,
'option_name' => 'category',
'option_value' => $category
]
);
}
}
if (array_key_exists('remove', $_POST) && is_array($_POST['remove'])) {
foreach ($_POST['remove'] as $row_id => $state) {
if ($state != 'on') {
continue;
}
$wpdb->delete(
$tableizer_tab_row_option,
[
'row_id' => $row_id
]
);
$wpdb->delete(
$tableizer_tab,
[
'row_id' => $row_id
]
);
}
}
} elseif (isset($_POST['action']) && $_POST['action'] === 'sort') {
$category = array_key_exists('category', $_POST) && $_POST['category'] !== '' ? $_POST['category'] : null;
$wpdb->delete(
$tableizer_tab_order,
[
'category_name' => $category
]
);
foreach ($_POST['order'] as $index => $row_id) {
$wpdb->insert(
$tableizer_tab_order,
[
'category_name' => $category,
'row_id' => $row_id,
'order_value' => $index
]
);
}
}
// Collect data
$categories = $wpdb->get_col("SELECT DISTINCT `option_value` FROM {$tableizer_tab_row_option} WHERE `option_name` = 'category';");
// View
?>
<h1>Tableizer</h1>
<section>
<h2>Usage</h2>
<p><code>[tableizer category="category name"]</code></p>
<h3>Attributes</h3>
<table>
<thead><tr><th>Attribute</th><th>Description</th></thead>
<tbody>
<tr><td><code>category</code></td><td>category to display</td></tr>
<tr><td><code>category-exclude</code></td><td>category to be excluded, comma separated</td></tr>
<tr><td><code>link_target</code></td><td>target for all displayed link cells</td></tr>
<tr><td><code>only_rows</code></td><td>outputs only content of tbody</td></tr>
<tr><td><code>per_page</code></td><td>number of rows displayed per page</td></tr>
<tr><td><code>top</code></td><td>number of the first N rows to display</td></tr>
</tbody>
</table>
<h3>Examples</h3>
<p><code>[tableizer category="category name"]</code></p>
<p><code>[tableizer category="category name" top="10"]</code></p>
<p><code>[tableizer category="category name" per_page="20"]</code></p>
<p><code>[tableizer category="category name" only_rows="on"]</code></p>
<p><code>[tableizer category="category name" link_target="_blank"]</code></p>
<p><code>[tableizer category="category name" category-exclude="category1,category2"]</code></p>
</section>
<section>
<h2>Add content</h2>
<section>
<h3>Cell content examples</h3>
<table>
<thead><tr><th>Type</th><th>Input</th><th>Output HTML</th></thead>
<tbody>
<tr>
<td>text</td>
<td><code>just plain text</code></td>
<td><samp>just plain text</samp></td>
</tr>
<tr>
<td>text</td>
<td><code><button>Click me</button></code></td>
<td><samp><button>Click me</button></samp></td>
</tr>
<tr>
<td>image</td>
<td><code>[Example image]http://example.com/example.png</code></td>
<td><samp><img src="http://example.com/example.png" alt="Example image"></samp></td>
</tr>
<tr>
<td>link</td>
<td><code>[Read more]http://example.com/full_article</code></td>
<td><samp><a href="http://example.com/full_article">Read more</a></samp></td>
</tr>
</tbody>
</table>
</section>
<h3>Add rows</h3>
<form method="get" action="#">
<input type="hidden" name="page" value="<?php echo $_GET['page']; ?>">
<p>
<label>Number of new columns:
<input type="number" name="cols_count" value="<?php echo isset($_GET['cols_count']) && is_numeric($_GET['cols_count']) ? $_GET['cols_count'] : 1; ?>" min="1" step="1">
</label>
<input type="submit" class="button">
</p>
</form>
<p><button id="btn-add-row" class="button">Add row</button></p>
<form action="<?php echo add_query_arg([]); ?>" method="post">
<input type="hidden" name="action" value="add_row">
<table id="table-input"><thead></thead><tbody>
<tr>
<?php for ($i=0; $i<(isset($_GET['cols_count']) && is_numeric($_GET['cols_count']) ? $_GET['cols_count'] : 1); $i++) { ?>
<td><select name="types[<?php echo $i; ?>]" style="width:100%;">
<option value="text" selected>text</option>
<option value="image">image</option>
<option value="link">link</option>
</select></td>
<?php } ?>
</tr>
</tbody></table>
<p><b>Select category:</b></p>
<p><input type="text" name="new_category" placeholder="New category name"></p>
<p><select name="categories" multiple><option></option><?php foreach ($categories as $category) {
print("<option value=\"{$category}\">{$category}</option>");
}?></select></p>
<p><b>Add as a header:</b> <input type="checkbox" name="is_header"></p>
<p><input type="submit" class="button"></p>
</form>
</section>
<section>
<h2>Manage data</h2>
<form method="get" action="<?php echo add_query_arg([]); ?>"><input type="hidden" name="page" value="tableizer_settings"><input type="hidden" name="editor_state" value="on"><p><b>Filter by category:</b>
<select name="filter_by_category"><option value="">Show all</option><?php foreach ($categories as $category) {
print("<option value=\"{$category}\"".(array_key_exists('filter_by_category', $_GET)&&$_GET['filter_by_category']==$category?' selected':'').">{$category}</option>");
}?></select>
<?php
$row_limit = array_key_exists('row_limit', $_GET) ? $_GET['row_limit'] : 20;
$row_offset = array_key_exists('row_offset', $_GET) ? $_GET['row_offset'] : 0;
$row_offset = array_key_exists('navigate', $_GET) ? ( $_GET['navigate'] == 'Previous' ? $row_offset - $row_limit : $row_offset + $row_limit ) : $row_offset;
$row_offset = max( [ $row_offset, 0 ] );
?>
<input type="number" name="row_limit" value="<?php echo $row_limit; ?>">
<input type="number" name="row_offset" value="<?php echo $row_offset; ?>">
<input type="submit" value="Filter" class="button">
<input type="submit" name="navigate" value="Previous" class="button">
<input type="submit" name="navigate" value="Next" class="button">
</p></form>
<?php if (array_key_exists('editor_state', $_GET ) && $_GET['editor_state'] == 'on') : ?>
<p><a href="<?php echo add_query_arg(['editor_state'=>'off']) ?>" class="button">Disable edition</a></p>
<form action="<?php echo add_query_arg([]); ?>" method="post">
<input type="hidden" name="action" value="update">
<?php print(make_table_editor(array_key_exists('filter_by_category', $_GET) && !empty($_GET['filter_by_category']) ? $_GET['filter_by_category'] : null, $row_limit, $row_offset)); ?>
<p><input type="submit" class="button" value="Update"></p>
</form>
<?php endif; ?>
</section>
<section>
<h2>Manage rows order</h2>
<form method="get" action="<?php echo add_query_arg([]); ?>">
<input type="hidden" name="page" value="tableizer_settings"><input type="hidden" name="editor_order" value="on">
<p><b>Select category:</b>
<select name="filter_by_category"><option value="">Global (no category)</option><?php foreach ($categories as $category) {
print("<option value=\"{$category}\"".(array_key_exists('filter_by_category', $_GET)&&$_GET['filter_by_category']==$category?' selected':'').">{$category}</option>");
}?></select>
<input type="submit" value="Filter" class="button">
</p>
</form>
<?php if (array_key_exists('editor_order', $_GET ) && $_GET['editor_order'] == 'on') : ?>
<form action="<?php echo add_query_arg([]); ?>" method="post">
<input type="hidden" name="action" value="sort">
<input type="hidden" name="category" value="<?php echo array_key_exists('filter_by_category', $_GET) ? $_GET['filter_by_category'] : ''; ?>">
<?php print(make_order_editor(array_key_exists('filter_by_category', $_GET) && !empty($_GET['filter_by_category']) ? $_GET['filter_by_category'] : null)); ?>
<input type="submit" class="button">
</form>
<?php endif; ?>
<style>
table.editor-order > tbody > tr {
cursor: move;
border: solid 1px #000;
}
</style>
</section>
<?php
// Elements
?>
<div style="display: none;">
<table id="new-rows">
<thead>
<tr>
<?php for ($i=0; $i<(isset($_GET['cols_count']) && is_numeric($_GET['cols_count']) ? $_GET['cols_count'] : 1); $i++) { ?>
<th>Col <?php echo $i; ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<tr>
<?php for ($i=0; $i<(isset($_GET['cols_count']) && is_numeric($_GET['cols_count']) ? $_GET['cols_count'] : 1); $i++) { ?>
<td><input type="text" name="table[$row_number][<?php echo $i; ?>]"></td>
<?php } ?>
</tr>
</tbody>
</table>
</div>
<?php
// Styles
?>
<style>
.tableizer-table-editor{
border-spacing: 0;
}
.tableizer-table-editor tr:not(:first-child) td{
border-top: solid 2px;
}
.tableizer-table-editor input[type="text"]{
width: 100%;
}
</style>