-
Notifications
You must be signed in to change notification settings - Fork 3
/
GanttColumnHeader.php
140 lines (124 loc) · 3.18 KB
/
GanttColumnHeader.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
<?php
/**
* @package yii2-gantt
* @author Markus Rotter <[email protected]>
* @copyright Copyright © Markus Rotter, 2020
* @version 0.0.2
*/
namespace rottriges\ganttcolumn;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
class GanttColumnHeader extends \yii\base\Component
{
private $_headerRow;
public $unitSize = 14;
public $years = [];
public $months = [];
public $weeks = [];
/**
* @var array the english month settings.
*/
private static $_months = [
'monthsLong' => [
1 => 'January',
2 => 'February',
3 => 'March',
4 =>'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 =>'September',
10 => 'October',
11 =>'November',
12 =>'December',
],
'monthsShort' => [
1 => 'Jan',
2 => 'Feb',
3 => 'Mar',
4 =>'Apr',
5 => 'May',
6 => 'Jun',
7 => 'Jul',
8 => 'Aug',
9 =>'Sep',
10 => 'Oct',
11 =>'Nov',
12 =>'Dec',
],
];
public function getHeaderRow()
{
return $this->getYearRow() . $this->getMonthRow() . $this->getWeekRow();
}
protected function getYearRow()
{
$row = '';
$bgOdd = 'bg-info';
$bgEven = 'bg-danger';
$i = 0;
foreach ($this->years as $year => $units) {
$bgColor = $this->getColBgColor($i, $bgOdd, $bgEven);
$row .= $this->generateRow( $units, $year, $bgColor);
$i++;
}
$row .= Html::tag('div', '', ['class' => 'last-header-col' ]);
return $row;
}
protected function getMonthRow()
{
$row = '';
$bgOdd = 'bg-success';
$bgEven = 'bg-warning';
$i = 0;
// GanttColumnHeader::registerTranslations();
foreach ($this->months as $yearMonth => $units) {
$bgColor = $this->getColBgColor($i, $bgOdd, $bgEven);
// explode 'YYYY-mm' to 'mm'
$value = intval(explode('-', $yearMonth)[1]);
$value = self::$_months['monthsShort'][$value];
$value = GanttColumn::t('ganttColumn', $value);
$row .= $this->generateRow( $units, $value, $bgColor);
$i++;
}
$row .= Html::tag('div', '', ['class' => 'last-header-col' ]);
return $row;
}
protected function getWeekRow()
{
$row = '';
$bgOdd = 'week bg-week-od';
$bgEven = 'week bg-week-even';
$i = 0;
foreach ($this->weeks as $week) {
$bgColor = $this->getColBgColor($i, $bgOdd, $bgEven);
$row .= $this->generateRow( 1, $week, $bgColor);
$i++;
}
$row .= Html::tag('div', '', ['class' => 'last-header-col' ]);
return $row;
}
private function generateRow($widthVal, $value, $bgColor)
{
$width = $this->getColWidth($widthVal);
$options['class'] = 'header-col ' . $bgColor;
$options['style'] = 'width: ' . $width;
return Html::tag('div', $value, $options);
}
private function getColWidth($val)
{
$width = $val * $this->unitSize;
$colWidth = $width . 'px';
return $colWidth;
}
private function getColBgColor($val, $bgEven, $bgOdd)
{
// even $val
if ($val % 2) return $bgEven;
// odd $val
return $bgOdd;
}
}
?>