| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface SH4_Schedule_Html_Widget_IDayGrid |
| 3 |
{ |
| 4 |
public function reset(); |
| 5 |
public function setRange( $timeMin, $timeMax ); |
| 6 |
public function add( $start, $end, $content ); |
| 7 |
public function render(); |
| 8 |
} |
| 9 |
|
| 10 |
class SH4_Schedule_Html_Widget_DayGrid implements SH4_Schedule_Html_Widget_IDayGrid |
| 11 |
{ |
| 12 |
protected $timeMin = 0; |
| 13 |
protected $timeMax = 86400; |
| 14 |
protected $slots = array(); |
| 15 |
public $self, $ui, $t; |
| 16 |
|
| 17 |
public function __construct( |
| 18 |
HC3_Hooks $hooks, |
| 19 |
HC3_Time $t, |
| 20 |
HC3_Ui $ui |
| 21 |
) |
| 22 |
{ |
| 23 |
$this->self = $hooks->wrap( $this ); |
| 24 |
$this->ui = $ui; |
| 25 |
$this->t = $t; |
| 26 |
} |
| 27 |
|
| 28 |
public function reset() |
| 29 |
{ |
| 30 |
$this->slots = array(); |
| 31 |
return $this; |
| 32 |
} |
| 33 |
|
| 34 |
public function setRange( $timeMin, $timeMax ) |
| 35 |
{ |
| 36 |
$this->timeMin = $this->t->setDateTimeDb( $timeMin )->getTimestamp(); |
| 37 |
$this->timeMax = $this->t->setDateTimeDb( $timeMax )->getTimestamp(); |
| 38 |
} |
| 39 |
|
| 40 |
public function add( $start, $end, $content ) |
| 41 |
{ |
| 42 |
$slot = new stdClass(); |
| 43 |
|
| 44 |
$start = $this->t->setDateTimeDb( $start )->getTimestamp(); |
| 45 |
$end = $this->t->setDateTimeDb( $end )->getTimestamp(); |
| 46 |
|
| 47 |
if( ($end <= $this->timeMin) OR ($start >= $this->timeMax) ){ |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
if( $start < $this->timeMin ){ |
| 52 |
$start = $this->timeMin; |
| 53 |
} |
| 54 |
|
| 55 |
if( $end > $this->timeMax ){ |
| 56 |
$end = $this->timeMax; |
| 57 |
} |
| 58 |
|
| 59 |
$slot->start = $start; |
| 60 |
$slot->end = $end; |
| 61 |
$slot->content = $content; |
| 62 |
$slot->offset = 0; |
| 63 |
|
| 64 |
$this->slots[] = $slot; |
| 65 |
} |
| 66 |
|
| 67 |
public function render() |
| 68 |
{ |
| 69 |
/* slots */ |
| 70 |
$thisLength = $this->timeMax - $this->timeMin; |
| 71 |
$top = 0; |
| 72 |
|
| 73 |
$slots = $this->slots; |
| 74 |
|
| 75 |
/* split by rows */ |
| 76 |
$rows = array(); |
| 77 |
foreach( $slots as $slot ){ |
| 78 |
/* find suitable row */ |
| 79 |
$myRow = count($rows); |
| 80 |
for( $ri = 0; $ri < count($rows); $ri++ ){ |
| 81 |
$failedRow = FALSE; |
| 82 |
foreach( $rows[$ri] as $checkSlot ){ |
| 83 |
if( |
| 84 |
( $checkSlot->start < $slot->end ) && |
| 85 |
( $checkSlot->end > $slot->start ) |
| 86 |
){ |
| 87 |
$failedRow = TRUE; |
| 88 |
} |
| 89 |
if( $failedRow ){ |
| 90 |
break; |
| 91 |
} |
| 92 |
} |
| 93 |
if( ! $failedRow ){ |
| 94 |
$myRow = $ri; |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
if( ! isset($rows[$myRow]) ){ |
| 99 |
$rows[$myRow] = array(); |
| 100 |
} |
| 101 |
$rows[$myRow][] = $slot; |
| 102 |
} |
| 103 |
|
| 104 |
/* add offset */ |
| 105 |
for( $ri = 0; $ri < count($rows); $ri++ ){ |
| 106 |
for( $si = 0; $si < count($rows[$ri]); $si++ ){ |
| 107 |
$checkWith = $si ? $rows[$ri][$si-1]->end : $this->timeMin; |
| 108 |
$offset = $rows[$ri][$si]->start - $checkWith; |
| 109 |
$rows[$ri][$si]->offset = $offset; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
$out = array(); |
| 114 |
foreach( $rows as $row ){ |
| 115 |
$rowView = $this->ui->makeGrid() |
| 116 |
->gutter(0) |
| 117 |
; |
| 118 |
|
| 119 |
foreach( $row as $slot ){ |
| 120 |
$left = floor( 100 * 100 * (($slot->start - $this->timeMin) / $thisLength ) ) / 100; |
| 121 |
|
| 122 |
$slotDuration = $slot->end - $slot->start; |
| 123 |
if( $slotDuration + $slot->start > $this->timeMax ){ |
| 124 |
$slotDuration = $this->timeMax - $slot->start; |
| 125 |
} |
| 126 |
|
| 127 |
$width = floor( 100 * 100 * ($slotDuration / $thisLength ) ) / 100; |
| 128 |
$offset = floor( 100 * 100 * ($slot->offset / $thisLength ) ) / 100; |
| 129 |
|
| 130 |
// echo "left = $left, width = $width, offset = $offset<br>"; |
| 131 |
|
| 132 |
$rowView |
| 133 |
->add( $slot->content, $width . '%', $width . '%', $offset . '%' ) |
| 134 |
; |
| 135 |
} |
| 136 |
|
| 137 |
$out[] = $rowView; |
| 138 |
} |
| 139 |
|
| 140 |
$out = $this->ui->makeList( $out ) |
| 141 |
->gutter(1) |
| 142 |
; |
| 143 |
return $out; |
| 144 |
} |
| 145 |
} |