-
Notifications
You must be signed in to change notification settings - Fork 0
/
roster_admin.php
713 lines (627 loc) · 20.4 KB
/
roster_admin.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
<?php
require_once('roster_audittrail.php');
global $week_mapping;
$week_mapping = array(
'MONDAY' => 0,
'TUESDAY' => 1,
'WEDNESDAY' => 2,
'THURSDAY' => 3,
'FRIDAY' => 4,
'SATURDAY' => 5,
'SUNDAY' => 6
);
function __LOG ($message, $cease=false, $dump=false) {
if (!WP_DEBUG) {
return;
}
echo "<pre>";
if ($dump) {
var_dump($message);
} else {
print_r($message);
}
echo "</pre>";
if ($cease) {
wp_die();
}
}
class Person {
/**
* @var WP_Post
*/
protected $reference;
/**
* @var array
*/
protected $categories;
public function __construct($ref) {
$this->reference = $ref;
$this->categories = wp_get_post_categories($this->reference->ID);
}
public function html ($weekdays, $index=0) {
?>
<tr>
<?php $this->entity_cell_html(); ?>
<?php $this->roster_html($weekdays); ?>
</tr>
<?php
}
private function roster_html ($weekdays) {
foreach ($weekdays as $weekday) {
$checked = '';
if (in_array($weekday->term_id, $this->categories)) {
$checked = 'checked="checked"';
}
?>
<td style="text-align: center;">
<input type="checkbox"
name="weekday_<?= $weekday->term_id ?>"
value="<?= $weekday->term_id ?>"
<?= $checked ?> />
</td>
<?php
}
}
private function entity_cell_html () {
?>
<td class="column-description desc">
<strong><?= $this->reference->post_title; ?></strong>
<input type="hidden" name="pid" value="<?= $this->reference->ID ?>" />
</td>
<?php
}
public function get_id () {
return $this->reference->ID;
}
public function save_roster ($weekdays) {
$categories = array_keys(array_filter($weekdays, function ($w) {
return $w;
}));
foreach ($this->categories as $cat) {
if (!array_key_exists($cat, $weekdays)) {
array_push($categories, $cat);
}
}
wp_set_post_categories($this->reference->ID, $categories, false);
}
}
class Roster {
/**
* @var Person[]
*/
protected $people; // all the people posts
/**
* @var array
*/
protected $weekdays; // weekday categories
public function __construct ($weekdays, $people) {
$this->weekdays = $weekdays;
$this->people = $people;
}
public function html () {
?>
<table id="roster_table" class="wp-list-table widefat striped">
<?php $this->table_header(); ?>
<?php $this->table_body(); ?>
</table>
<?php
}
private function table_header () {
?>
<thead>
<tr>
<th></th>
<?php foreach ($this->weekdays as $w) { ?>
<th scope="col" class="manage-column column-description" style="text-align: center;">
<?= $w->name ?>
</th>
<?php } ?>
</tr>
</thead>
<?php
}
private function table_body () {
?>
<tbody>
<?php foreach ($this->people as $p) {
$p->html($this->weekdays);
} ?>
</tbody>
<?php
}
/**
* @return array(int => Person)
*/
private function get_people_in_assoc_array () {
$result = array();
foreach ($this->people as $person) {
$result[$person->get_id()] = $person;
}
return $result;
}
public function save ($roster) {
$people_dict = $this->get_people_in_assoc_array();
foreach ($roster as $pid => $weekdays) {
/**
* @var $person Person
*/
$person = $people_dict[$pid];
$person->save_roster($weekdays);
}
}
}
class RosterAdminController {
/**
* @var Roster
*/
protected $roster;
/**
* @var array
*/
protected $weekdays;
/**
* @var Person[]
*/
protected $people;
/**
* @var array( string => string )
*/
protected $options;
/**
* @var RosterAuditTrail
*/
protected $audit;
public function __construct () {
add_action('admin_menu', array($this, 'add_page'));
add_action('admin_init', array($this, 'page_init'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
$this->add_ajax_action();
}
protected function add_ajax_action () {
add_action('wp_ajax_save_roster', array($this, 'save_roster'));
}
public function init () {
$this->options = get_option('roster_options');
if (!$this->options['weekday_category']) {
$this->options['weekday_category'] = 'timetable';
}
$this->weekdays = $this->get_weekdays();
$this->people_init();
$this->roster_init();
$this->audit = new RosterAuditTrail();
}
protected function roster_init () {
$this->roster = new Roster($this->weekdays, $this->people);
}
public function page_init () {
wp_register_script('floatthead_script', plugins_url('assets/js/jquery.floatThead.min.js', __FILE__), array('jquery'));
wp_register_script('roster_script', plugins_url('assets/js/roster.js', __FILE__), array('jquery', 'floatthead_script'));
$this->init();
}
public function enqueue_scripts () {
wp_enqueue_script('floatthead_script');
wp_enqueue_script('roster_script');
wp_localize_script('roster_script', 'RosterAdmin', array(
'ajax_url' => admin_url('admin-ajax.php')
));
}
public function add_page () {
add_menu_page(
'Timetable', 'Timetable', 'manage_options', 'timetable');
add_submenu_page('timetable', 'By People', 'By People', 'manage_options', 'timetable', array($this, 'execute'));
}
public function execute () {
if (!$this->options['weekday_category']) {
?>
<div class="wrap"><h2>Please set weekday category</h2></div>
<?php
return;
}
$this->output();
}
protected function header () {
?>
<h2>Timetable Edit</h2>
<?php
}
protected function notification () {
?>
<div id="message" class="updated notice" style="display: none; position: fixed; top: 100px; z-index: 99999;">
<p>Roster <strong>updated</strong>.</p>
</div>
<?php
}
protected function output () {
?>
<div class="wrap">
<?php $this->header(); ?>
<?php $this->notification(); ?>
<?php $this->roster->html(); ?>
<?php $this->submit_html(); ?>
</div>
<?php
}
protected function submit_html () {
$audit = $this->audit->get_latest_audit();
?>
<div class="tablenav bottom">
<div class="alignleft actions">
<input type="button" id="roster_save" class="button button-primary action" value="Save timetable">
<span style="line-height: 35px;">
<em>Last modified: <?= date_i18n('Y/m/d g:i:s', strtotime($audit->modified_at), 0) ?></em>
</span>
</div>
<div class="tablenav-pages one-page">
<span class="displaying-num"><?= count($this->people) ?> roster(s)</span>
</div>
</div>
<?php
}
protected function people_init () {
$args = [
'numberposts' => -1,
'orderby' => 'title',
'order' => 'ASC'
];
if ($this->options['people_category']) {
$people_cat = get_category_by_path($this->options['people_category']);
$args['category'] = $people_cat->cat_ID;
}
$people = get_posts($args);
$this->people = $this->get_people($people);
}
/**
* @param WP_Post[] $people
* @return Person[]
*/
protected function get_people ($people) {
return array_map(function ($p) {
return new Person($p);
}, $people);
}
protected function get_weekdays () {
global $week_mapping;
$weekdays_cat = get_category_by_path($this->options['weekday_category']);
$weekdays = array_map(
function ($t) {
return get_category($t);
},
get_term_children($weekdays_cat->term_id, $weekdays_cat->taxonomy));
$result = [];
foreach ($weekdays as $weekday) {
$index = $week_mapping[strtoupper($weekday->cat_name)];
$result[$index] = $weekday;
}
ksort($result);
return $result;
}
public function save_roster () {
$roster = str_replace('\"', '"', $_POST['roster']);
$roster = json_decode($roster, true);
$this->roster->save($roster);
var_dump($this->roster);
$this->audit->audit();
echo "success";
ob_flush();
wp_die();
}
}
class NGWeekday { // NGWeekday
/**
* @var NGPerson[]
*/
protected $people;
public function __construct ($people) {
$this->people = $people;
}
public function html () {
}
}
class NGPerson extends Person {
public function is_checked($cat) {
global $week_mapping;
$today = new DateTime();
$date_str = $today->format('Y-m-d');
global $wpdb;
$table = $wpdb->prefix . 'ngroster';
$pid = $this->reference->ID;
$results = $wpdb->get_results(
"
SELECT roster_date, working
FROM $table
WHERE pid=$pid
AND roster_date >= '$date_str'
"
);
foreach ($results as $r) {
$w = DateTime::createFromFormat('Y-m-d', $r->roster_date)->format('N') - 1;
if ($w == $week_mapping[strtoupper($cat->cat_name)]) {
return boolval($r->working);
}
}
return in_array($cat->term_id, $this->categories);
}
public function html($cat, $index=0) {
$is_checked = $this->is_checked($cat);
$thumbnail_id = get_post_thumbnail_id($this->reference->ID);
$thumbnail_url = wp_get_attachment_image_src($thumbnail_id, 'thumbnail');
$id = $this->reference->ID;
$title = strtoupper($this->reference->post_title);
if (!$thumbnail_url) {
$thumbnail_url = plugins_url('assets/images/no_photo.jpg', __FILE__);
} else {
$thumbnail_url = $thumbnail_url[0];
}
?>
<div class="person-grid" data-edik-elem="<?= $id . ':' . esc_attr($title) ?>" data-edik-elem-type="people">
<input type="checkbox" data-edik-elem="<?= $title ?>"
data-edik-elem-title="<?= $id ?>"
data-edik-elem-type="person" class="on"
<?= $is_checked ? 'checked="checked"' : '' ?>/>
<img data-edik-elem="overlay" class="overlay"
src="<?= plugins_url('assets/images/on.png', __FILE__) ?>" />
<img data-edik-elem="gthumb" class="gthumb"
src="<?= $thumbnail_url ?>">
<div data-edik-elem="name" class="name" >
<h4><?= $this->reference->post_title ?></h4>
</div>
</div>
<?php
}
}
class NGRoster extends Roster { // NGRoster
const NUMBER_OF_DAYS = 7;
public function html() {
?>
<div id="side-sortables" class="accordion-container">
<ul class="outer-border">
<?php
$today = new DateTime();
for ($i = 0; $i < self::NUMBER_OF_DAYS; $i += 1) {
if ($i != 0) {
$today->add(new DateInterval('P1D'));
}
$this->weekday_html($today, $i==0);
}
?>
</ul>
</div>
<?php
}
/**
* @param DateTime $today
* @param Boolean $open
* @return string
*/
protected function weekday_html ($today, $open) {
$title = $today->format("l (j/M/Y)");
$day_of_week = $today->format("N");
$cat = $this->weekdays[$day_of_week - 1];
?>
<li class="control-section accordion-section <?= $open ? 'open' : '' ?>"
id="<?= $today->getTimestamp() ?>">
<h3 class="accordion-section-title hndle" tabindex="0" title="<?= $title ?>">
<?= $title ?>
</h3>
<div class="accordion-section-content">
<div class="inside">
<article>
<div class="filter">
<input type="text" data-edik-elem="search" class="search" />
</div>
<div data-edik-elem="people-grid">
<?php $this->people_html($cat); ?>
</div>
</article>
</div>
</div>
</li>
<?php
}
protected function people_html($cat) {
foreach ($this->people as $p) {
/**
* @var NGPerson $p
*/
$p->html($cat);
}
}
public function save($roster) {
global $wpdb;
$prefix = $wpdb->prefix;
$table = $prefix . 'ngroster';
$wpdb->query('START TRANSACTION');
foreach ($roster as $p => $weekdays) {
foreach ($weekdays as $w => $working) {
$pid = intval($p);
$date = new DateTime();
$date->setTimestamp($w);
$date_str = $date->format('Y-m-d');
if (!$wpdb->get_row("SELECT 1 FROM $table WHERE pid=$pid AND roster_date='$date_str'")) {
$wpdb->insert(
$table,
array(
'pid' => intval($p),
'roster_date' => $date->format('Y-m-d'),
'working' => $working
));
} else {
$wpdb->update(
$table,
array('working' => $working),
array('pid' => $pid, 'roster_date' => $date_str));
}
}
}
$wpdb->query('COMMIT');
}
}
class WidgetPerson extends NGPerson {
public function html($cat, $index=0) {
$is_checked = $this->is_checked($cat);
if (!$is_checked) {
return;
}
$thumbnail_id = get_post_thumbnail_id($this->reference->ID);
$thumbnail_url = wp_get_attachment_image_src($thumbnail_id, 'thumbnail');
$id = $this->reference->ID;
$title = strtoupper($this->reference->post_title);
if (!$thumbnail_url) {
$thumbnail_url = plugins_url('assets/images/no_photo.jpg', __FILE__);
} else {
$thumbnail_url = $thumbnail_url[0];
}
$post_url = get_permalink($id);
?>
<article class="slide-entry flex_column post-entry post-entry-<?= $id ?> slide-entry-overview
slide-loop-<?= $index ?>
slide-parity-<?= ($index + 1) % 2 == 0 ? 'even' : 'odd' ?>
av_one_fifth <?= $index % 5 == 0 ? 'first' : '' ?> fake-thumbnail"
itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost">
<a href="<?= $post_url ?>" data-rel="slide-1" class="slide-image" title="">
<img width="300" height="420" src="<?= $thumbnail_url ?>"
class="attachment-portfolio wp-post-image" alt="image">
<span class="image-overlay overlay-type-extern">
<span class="image-overlay-inside"></span>
</span>
</a>
<div class="slide-content">
<header class="entry-content-header">
<h3 class="slide-entry-title entry-title" itemprop="headline">
<a href="<?= $post_url ?>" title="<?= $title ?>"><?= $title?></a>
</h3>
</header>
</div>
<footer class="entry-footer"></footer>
</article>
<?php
}
}
class WidgetRoster extends NGRoster {
public function html() {
?>
<div id="side-sortables" class="accordion-container">
<ul class="outer-border">
<?php
$today = new DateTime();
for ($i = 0; $i < self::NUMBER_OF_DAYS; $i += 1) {
if ($i != 0) {
$today->add(new DateInterval('P1D'));
}
$this->weekday_html($today, $i==0);
}
?>
</ul>
</div>
<?php
}
protected function people_html($cat) {
$index = 0;
$need_to_close = false;
foreach ($this->people as $p) {
/**
* @var WidgetPerson $p
*/
if (!$p->is_checked($cat)) {
continue;
}
if ($index % 5 == 0) {
?>
<div class="slide-entry-wrap">
<?php
$need_to_close = true;
}
$p->html($cat, $index++);
if ($index % 5 == 0 && $index != 0) {
?>
</div>
<?php
$need_to_close = false;
}
}
if ($need_to_close) {
?>
</div>
<?php
}
}
/**
* @param DateTime $today
* @param Boolean $open
* @return string
*/
protected function weekday_html ($today, $open) {
$title = $today->format("l (j/M/Y)");
$day_of_week = $today->format("N");
$cat = $this->weekdays[$day_of_week - 1];
?>
<div class="flex_column av_one_full first avia-builder-el-3 el_after_av_one_full
el_before_av_one_full column-top-margin">
<section class="av_textblock_section" itemscope="itemscope"
itemtype="https://schema.org/CreativeWork">
<div class="avia_textblock " itemprop="text">
<h3 style="text-align: left; color: #fff; background: rgba(0,0,0,0.7); padding: 8px 0 8px 15px;border-radius: 8px;">
<?= $title ?>
</h3>
</div>
</section>
<div data-autoplay="" data-interval="5" data-animation="fade" data-show_slide_delay="90"
class="avia-content-slider avia-content-grid-active avia-content-slider1 avia-content-slider-odd
avia-builder-el-5 el_after_av_textblock avia-builder-el-last " itemscope="itemscope"
itemtype="https://schema.org/Blog">
<div class="avia-content-slider-inner">
<?php $this->people_html($cat); ?>
</div>
</div>
</div>
<?php
}
}
class NGRosterAdminController extends RosterAdminController {
protected function roster_init () {
$this->roster = new NGRoster($this->weekdays, $this->people);
}
public function page_init()
{
wp_register_script('ngroster_script', plugins_url('assets/js/ngroster.js', __FILE__), array('jquery'));
wp_register_style('ngroster_style', plugins_url('assets/css/ngroster.css', __FILE__));
$this->init();
}
public function enqueue_scripts () {
wp_enqueue_style('ngroster_style');
wp_enqueue_script('ngroster_script');
wp_enqueue_script( 'accordion' );
wp_localize_script('ngroster_script', 'NGRosterAdmin', array(
'ajax_url' => admin_url('admin-ajax.php')
));
}
/**
* @param WP_Post[] $people
* @return NGPerson[]
*/
protected function get_people($people) {
return array_map(function ($p) {
return new NGPerson($p);
}, $people);
}
public function add_page()
{
add_submenu_page('timetable', 'By Date', 'By Date', 'read', 'by-date', array($this, 'execute'));
}
public function save_ngroster () {
$roster = str_replace('\"', '"', $_POST['roster']);
$roster = json_decode($roster, true);
$this->roster->save($roster);
var_dump($this->roster);
$this->audit->audit();
echo "success";
ob_flush();
wp_die();
}
protected function add_ajax_action()
{
add_action('wp_ajax_save_ngroster', array($this, 'save_ngroster'));
}
}
?>