-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFullCalendar.php
136 lines (118 loc) · 3.65 KB
/
FullCalendar.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
<?php
/**
* @link https://github.com/LAV45/yii2-fullcalendar
* @copyright Copyright (c) 2015 LAV45!
* @license http://opensource.org/licenses/BSD-3-Clause
* @package yii2-fullcalendar
*/
namespace lav45\widget;
use Yii;
use Locale;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* FullCalendar widget is a Yii2 wrapper for the FullCalendar.
*
* @author Alexey Loban <[email protected]>
* @see http://fullcalendar.io
*/
class FullCalendar extends Widget
{
/**
* @var boolean If the plugin displays a Google Calendar.
*/
public $googleCalendar = false;
/**
* @see https://fullcalendar.io/docs
* @var array the options for the underlying JS plugin.
*/
public $clientOptions;
/**
* @var array the HTML attributes for the widget container tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options;
/**
* Initializes the widget.
* This method will register the bootstrap asset bundle. If you override this method,
* make sure you call the parent implementation first.
*/
public function init()
{
parent::init();
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
}
/**
* Runs the widget.
*/
public function run()
{
$this->registerAsset();
$this->registerScript();
Html::addCssClass($this->options, 'fullcalendar');
return Html::tag('div', '', $this->options);
}
/**
* Registers a FullCalendar plugin
*/
protected function registerScript()
{
$options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions);
$this->getView()->registerJs("jQuery('#{$this->options['id']}').fullCalendar({$options});");
}
protected function registerAsset()
{
$view = $this->getView();
$asset = FullCalendarAsset::register($view);
$this->registerLocale($asset);
PrintAsset::register($view);
if ($this->googleCalendar) {
GoogleCalendarAsset::register($view);
}
}
protected function registerLocale($asset)
{
$language = $this->getLanguage();
$basePath = "{$asset->basePath}/locale";
if (!file_exists("{$basePath}/{$language}.js")) {
$language = $this->getPrimaryLanguage($language);
}
if (file_exists("{$basePath}/{$language}.js")) {
$this->clientOptions['locale'] = $language;
$this->getView()->registerJsFile("{$asset->baseUrl}/locale/{$language}.js", [
'depends' => ['lav45\widget\FullCalendarAsset']
]);
} elseif(isset($this->clientOptions['locale'])) {
unset($this->clientOptions['locale']);
}
}
protected function getLanguage()
{
/**
* locale changed in 3.0 from lang
* @see https://fullcalendar.io/docs/text/locale/
*/
if (isset($this->clientOptions['lang'])) {
$language = $this->clientOptions['lang'];
unset($this->clientOptions['lang']);
} elseif(isset($this->clientOptions['locale'])) {
$language = $this->clientOptions['locale'];
} else {
$language = Yii::$app->language;
}
$language = strtolower($language);
return $language;
}
/**
* @param string $locale `en-EN`, `ru-RU`
* @return string en or ru
*/
protected function getPrimaryLanguage($locale)
{
return extension_loaded('intl') ?
Locale::getPrimaryLanguage($locale) : substr($locale, 0, 2);
}
}