-
Notifications
You must be signed in to change notification settings - Fork 0
/
food-menu-upload.php
424 lines (385 loc) · 15 KB
/
food-menu-upload.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
/*
Plugin Name: Food Menu Upload
Description: Плагин для управления файлами меню школы через административную панель.
Version: 1.1
Author: webcreate21.ru
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
define('FOOD_MENU_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('FOOD_MENU_INCLUDES_DIR', FOOD_MENU_PLUGIN_DIR . 'includes/');
require_once FOOD_MENU_INCLUDES_DIR . 'MenuUploader.php';
require_once FOOD_MENU_INCLUDES_DIR . 'BaseMenu.php';
require_once FOOD_MENU_INCLUDES_DIR . 'CalendarMenu.php';
require_once FOOD_MENU_INCLUDES_DIR . 'ResourcesFile.php';
require_once FOOD_MENU_INCLUDES_DIR . 'TypicalMenu.php';
global $food_menu_db_version;
$food_menu_db_version = '1.0';
register_activation_hook(__FILE__, 'food_menu_upload_install');
function food_menu_upload_install()
{
global $wpdb;
global $food_menu_db_version;
$table_name = $wpdb->prefix . 'food_menu_files';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
date date DEFAULT NULL,
year varchar(4) DEFAULT NULL,
subtype varchar(2) DEFAULT NULL,
type varchar(50) NOT NULL,
filename varchar(255) NOT NULL,
url varchar(255) NOT NULL,
created datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option('food_menu_db_version', $food_menu_db_version);
}
register_deactivation_hook(__FILE__, 'food_menu_upload_uninstall');
function food_menu_upload_uninstall()
{
global $wpdb;
$table_name = $wpdb->prefix . 'food_menu_files';
$sql = "DROP TABLE IF EXISTS $table_name;";
$wpdb->query($sql);
}
add_action('admin_menu', 'food_menu_upload_create_menu');
function food_menu_upload_create_menu()
{
add_menu_page(
'Food Menu Upload',
'Школьное меню',
'manage_options',
'food-menu-upload',
'food_menu_upload_admin_page', // Основная страница плагина
'dashicons-media-spreadsheet',
20
);
add_submenu_page(
'food-menu-upload',
'Календарь питания',
'Календарь питания',
'manage_options',
'food-menu-upload-calendar',
'food_menu_upload_calendar_page'
);
add_submenu_page(
'food-menu-upload',
'Типовое меню',
'Типовое меню',
'manage_options',
'food-menu-upload-sample',
'food_menu_upload_sample_page'
);
add_submenu_page(
'food-menu-upload',
'Перечень ресурсов',
'Перечень ресурсов',
'manage_options',
'food-menu-upload-resources',
'food_menu_upload_resources_page'
);
}
function food_menu_upload_admin_page()
{
global $wpdb;
$table_name = $wpdb->prefix . 'food_menu_files';
$way = "food";
$root_directory = ABSPATH . $way;
$baseMenu = new \includes\BaseMenu($table_name, $way, $root_directory);
if (isset($_GET['success'])) {
echo '<div class="updated notice"><p>' . $_GET['success'] . '</p></div>';
}
?>
<div class="wrap">
<h1 class="text-primary">Загрузка ежедневного меню</h1>
<?= $baseMenu->getUploadForm(); ?>
<h1 class="text-primary">Мониторинг меню на сегодня <?= wp_date("d-m-Y")?></h1>
<?= $baseMenu->getTodayMenuTable() ?>
<h1>Последние 30 меню</h1>
<table class="shortcodes wp-list-table widefat fixed striped">
<thead>
<tr>
<th>Начальная школа <span>[menu_list subtype="sm" limit="30"]</span></th>
<th>Средняя школа <span> [menu_list subtype="ss" limit="30"]</span></th>
</tr>
</thead>
<tbody>
<tr>
<td><?= getLimitedMenuList('sm', 30,true) ?></td>
<td><?= getLimitedMenuList('ss', 30,true) ?></td>
</tr>
</tbody>
</table>
<h1 class="text-primary">Мониторинг загруженных файлов</h1>
<?= $baseMenu->getUploadedFiles() ?>
</div>
<?php
if (isset($_POST['upload'])) {
$baseMenu->upload($_POST, $_FILES);
}
if (isset($_POST['delete'])) {
$baseMenu->deleteFile($_POST['name']);
}
}
function food_menu_upload_calendar_page()
{
global $wpdb;
$table_name = $wpdb->prefix . 'food_menu_files';
$way = "food";
$root_directory = ABSPATH . $way;
$calendarMenu = new \includes\CalendarMenu($table_name, $way, $root_directory);
if (isset($_GET['success'])) {
echo '<div class="updated notice"><p>' . $_GET['success'] . '</p></div>';
}
?>
<div class="wrap">
<h1 class="text-primary">Загрузка Календаря питания</h1>
<?= $calendarMenu->getUploadForm(); ?>
<h1 class="text-primary">Загруженные календари</h1>
<?= $calendarMenu->getUploadedFiles() ?>
<h1>Шорткоды</h1>
<table class="shortcodes wp-list-table widefat fixed striped">
<tbody>
<tr>
<td><span>Отображения актуального календаря питания</span></td>
<td><span>[last_calendar_menu]</span></td>
</tr>
</tbody>
</table>
</div>
<?php
if (isset($_POST['upload'])) {
$calendarMenu->upload($_POST, $_FILES);
}
if (isset($_POST['delete'])) {
$calendarMenu->deleteFile($_POST['name']);
}
}
function food_menu_upload_resources_page()
{
global $wpdb;
$table_name = $wpdb->prefix . 'food_menu_files';
$way = "food";
$root_directory = ABSPATH . $way;
$resourcesFile = new \includes\ResourcesFile($table_name, $way, $root_directory);
if (isset($_GET['success'])) {
echo '<div class="updated notice"><p>' . $_GET['success'] . '</p></div>';
}
?>
<div class="wrap">
<h1 class="text-primary">Загрузка перечня ресурсов</h1>
<?= $resourcesFile->getUploadForm(); ?>
<h1 class="text-primary">Загруженные файлы</h1>
<?= $resourcesFile->getUploadedFiles() ?>
<h1>Шорткоды</h1>
<table class="shortcodes wp-list-table widefat fixed striped">
<tbody>
<tr>
<td><span> Получить ресурсный файл</span></td>
<td><span>[findex_file]</span></td>
</tr>
</tbody>
</table>
</div>
<?php
if (isset($_POST['upload'])) {
$resourcesFile->upload($_POST, $_FILES);
}
if (isset($_POST['delete'])) {
$resourcesFile->deleteFile($_POST['name']);
}
}
function food_menu_upload_sample_page()
{
global $wpdb;
$table_name = $wpdb->prefix . 'food_menu_files';
$way = "food";
$root_directory = ABSPATH . $way;
$typicalMenu = new \includes\TypicalMenu($table_name, $way, $root_directory);
if (isset($_GET['success'])) {
echo '<div class="updated notice"><p>' . $_GET['success'] . '</p></div>';
}
?>
<div class="wrap">
<h1 class="text-primary">Загрузка типового меню по годам</h1>
<?= $typicalMenu->getUploadForm(); ?>
<h1 class="text-primary">Загруженные файлы</h1>
<?= $typicalMenu->getTypicalMenuTable() ?>
<h1>Шорткоды</h1>
<table class="shortcodes wp-list-table widefat fixed striped">
<thead>
<tr>
<th>Последнее загруженное типовое меню для начальной школы</th>
<th>Последнее загруженное типовое меню для средней школы</th>
</tr>
</thead>
<tbody>
<tr>
<td><span> [last_sample_menu_by_type subtype="sm"]</span></td>
<td><span> [last_sample_menu_by_type subtype="ss"]</span></td>
</tr>
</tbody>
</table>
</div>
<?php
if (isset($_POST['upload'])) {
$typicalMenu->upload($_POST, $_FILES);
}
if (isset($_POST['delete'])) {
$typicalMenu->deleteFile($_POST['name']);
}
}
add_action('admin_enqueue_scripts', 'food_menu_upload_admin_assets');
function food_menu_upload_admin_assets($hook_suffix)
{
if (strpos($hook_suffix, 'food-menu-upload') !== false) {
wp_enqueue_style('food-menu-custom', plugins_url('style.css', __FILE__));
wp_enqueue_script('food-menu-jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js', array(), null, true);
wp_enqueue_script('food-menu-custom', plugins_url('script.js', __FILE__), array('jquery'), null, true);
}
}
add_shortcode('today_menu_sm', 'render_today_menu_sm');
function render_today_menu_sm()
{
// [today_menu_sm]
global $wpdb;
$table_name = $wpdb->prefix . 'food_menu_files';
$way = "food";
$root_directory = ABSPATH . $way;
$baseMenu = new \includes\BaseMenu($table_name, $way, $root_directory);
$todayMenu = $baseMenu->getTodayMenu('sm');
if ($todayMenu) {
return '<a href="' . $todayMenu->url . '">Меню на сегодня(Начальная школа)' . $todayMenu->filename . '</a>';
}
return '<span>На сегодня меню не загружено(Начальная школа)</span>';
}
add_shortcode('today_menu_ss', 'render_today_menu_ss');
function render_today_menu_ss()
{
// [today_menu_ss]
global $wpdb;
$table_name = $wpdb->prefix . 'food_menu_files';
$way = "food";
$root_directory = ABSPATH . $way;
$baseMenu = new \includes\BaseMenu($table_name, $way, $root_directory);
$todayMenu = $baseMenu->getTodayMenu('ss');
if ($todayMenu) {
return '<p><a href="' . $todayMenu->url . '">Меню на сегодня(Средняя школа) ' . $todayMenu->filename . '</a></p>';
}
return '<span>На сегодня меню не загружено(Средняя школа)</span>';
}
add_shortcode('menu_list', 'render_menu_list');
function render_menu_list($atts)
{
// [menu_list subtype="sm" limit="30"]
$atts = shortcode_atts(
array(
'limit' => '30', // Значение по умолчанию для type
'subtype' => 'sm', // Значение по умолчанию для subtype
),
$atts,
'menu_list'
);
$limit = sanitize_text_field($atts['limit']);
$subtype = sanitize_text_field($atts['subtype']);
return getLimitedMenuList($subtype, $limit);
// global $wpdb;
// $table_name = $wpdb->prefix . 'food_menu_files';
// $way = "food";
// $root_directory = ABSPATH . $way;
// $baseMenu = new \includes\BaseMenu($table_name, $way, $root_directory);
// $menuList = $baseMenu->getLimitedMenuList($subtype, $limit);
// if ($menuList) {
// $html = '<ul>';
// $description = $subtype == 'sm' ? ' (Начальная школа)' : ' (Средняя школа)';
// foreach ($menuList as $menu) {
// $html .= '<li><a href="' . $menu->url . '">' . $menu->filename . $description . '</a></li>';
// }
// $html .= '</ul>';
// return $html;
// }
// return '<div>Меню нет</div>';
}
add_shortcode('last_calendar_menu', 'render_last_calendar_menu');
function render_last_calendar_menu()
{
global $wpdb;
$table_name = $wpdb->prefix . 'food_menu_files';
$way = "food";
$root_directory = ABSPATH . $way;
$calendarMenu = new \includes\CalendarMenu($table_name, $way, $root_directory);
$lastMenu = $calendarMenu->getLastMenu();
if ($lastMenu) {
return '<p><a href="' . $lastMenu->url . '">' . $lastMenu->filename . ' (Календарь питания)</a></p>';
}
return '<span>Календарь питания не загружен</span>';
}
add_shortcode('last_sample_menu_by_type', 'render_sample_menu_by_type');
function render_sample_menu_by_type($atts)
{
// [last_sample_menu_by_type subtype="sm"]
$atts = shortcode_atts(
array(
'subtype' => 'sm', // Значение по умолчанию для subtype
),
$atts,
'last_sample_menu_by_type'
);
$subtype = sanitize_text_field($atts['subtype']);
global $wpdb;
$table_name = $wpdb->prefix . 'food_menu_files';
$way = "food";
$root_directory = ABSPATH . $way;
$typicalMenu = new \includes\TypicalMenu($table_name, $way, $root_directory);
$lastTypicalMenu = $typicalMenu->getLastTypicalMenu($subtype);
$description = $subtype == 'sm' ? ' (Начальная школа)' : ' (Средняя школа)';
if ($lastTypicalMenu) {
return '<p><a href="' . $lastTypicalMenu->url . '">' . $lastTypicalMenu->filename . ' Типовое примерное меню приготавливаемых блюд на ' . $lastTypicalMenu->year . ' год' . $description . '</a></p>';
}
return '<div>Типовое примерное меню приготавливаемых блюд' . $description . ' не загружено</div>';
}
add_shortcode('findex_file', 'render_findex_file');
function render_findex_file()
{
global $wpdb;
$table_name = $wpdb->prefix . 'food_menu_files';
$way = "food";
$root_directory = ABSPATH . $way;
$calendarMenu = new \includes\ResourcesFile($table_name, $way, $root_directory);
$recurseFile = $calendarMenu->getRecurseFile();
if ($recurseFile) {
return '<p><a href="' . $recurseFile->url . '">' . $recurseFile->filename . ' (Перечень ресурсов)</a></p>';
}
return '<span>Перечень ресурсов не загружен</span>';
}
function getLimitedMenuList($subtype, $limit, $makeRemove = false)
{
global $wpdb;
$table_name = $wpdb->prefix . 'food_menu_files';
$way = "food";
$root_directory = ABSPATH . $way;
$baseMenu = new \includes\BaseMenu($table_name, $way, $root_directory);
$menuList = $baseMenu->getLimitedMenuList($subtype, $limit);
if ($menuList) {
$html = '<ul>';
$description = $subtype == 'sm' ? ' (Начальная школа)' : ' (Средняя школа)';
foreach ($menuList as $menu) {
$html .= '<li><a href="' . $menu->url . '">' . $menu->filename . $description . '</a>';
if ($makeRemove) {
$html .= '<form method="POST" style="display:inline-block; margin-left:20px;">
<input type="hidden" name="name" value="' . $menu->filename . '">
<button type="submit" name="delete" class="button button-secondary">Удалить</button>
</form>';
}
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
return '<div>Меню нет</div>';
}