forked from philippfrenzel/yii2fullcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yii2fullcalendar.php
352 lines (291 loc) · 10.7 KB
/
yii2fullcalendar.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
<?php
/**
* This class is used to embed FullCalendar JQuery Plugin to my Yii2 Projects
* @copyright Frenzel GmbH - www.frenzel.net
* @link http://www.frenzel.net
* @author Philipp Frenzel <[email protected]>
*
*/
namespace yii2fullcalendar;
use Yii;
use yii\web\View;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\JsExpression;
use yii\base\Widget as elWidget;
class yii2fullcalendar extends elWidget
{
/**
* @var array options the HTML attributes (name-value pairs) for the field container tag.
* The values will be HTML-encoded using [[Html::encode()]].
* If a value is null, the corresponding attribute will not be rendered.
*/
public $options = [
'class' => 'fullcalendar'
];
/**
* @var bool $theme default is true and will render the jui theme for the calendar
*/
public $theme = true;
/**
* @var the name of the theme how the calendar should be displayed. default bootstrap 3
* Available Options
*
*/
public $themeSystem = 'bootstrap3';
/**
* @var array clientOptions the HTML attributes for the widget container tag.
*/
public $clientOptions = [
'weekends' => true,
'editable' => false,
'aspectRatio' => 1.35
];
/**
* @var string defaultView will define which view renderer will initially be used for displaying calendar events
*/
public $defaultView = 'month';
/**
* Holds an array of Event Objects
* @var array events of yii2fullcalendar\models\Event
* @todo add the event class and write docs
**/
public $events = [];
/**
* Add custom buttons to the calendar header
* @var array customButtons
*/
public $customButtons = [];
/**
* Define the look n feel for the calendar header, known placeholders are left, center, right
* @var array header format
*/
public $header = [
'center'=>'title',
'left'=>'prev,next today',
'right'=>'month,agendaWeek'
];
/**
* Will hold an url to json formatted events!
* replaced by $events pls refer to fullcalendar.io documentation
* @var url to json service
*/
public $ajaxEvents = NULL;
/**
* wheather the events will be "sticky" on pagination or not. Uncomment if you are loading events
* separately from the initial options.
* @var boolean
*/
//public $stickyEvents = true;
/**
* public string/integer $contentHeight
*/
public $contentHeight = NULL;
/**
* tell the calendar, if you like to render google calendar events within the view
* @var boolean
*/
public $googleCalendar = false;
/**
* the text that will be displayed on changing the pages
* @var string
*/
public $loading = 'Loading ...';
/**
* internal marker for the name of the plugin
* @var string
*/
private $_pluginName = 'fullCalendar';
/**
* The javascript function to us as en onLoading callback
* @var string the javascript code that implements the onLoading function
*/
public $onLoading = "";
/**
* The javascript function to us as en eventRender callback
* @var string the javascript code that implements the eventRender function
*/
public $eventRender = "";
/**
* The javascript function to us as en eventAfterRender callback
* @var string the javascript code that implements the eventAfterRender function
*/
public $eventAfterRender = "";
/**
* The javascript function to us as en eventAfterAllRender callback
* @var string the javascript code that implements the eventAfterAllRender function
*/
public $eventAfterAllRender = "";
/**
* The javascript function to us as en eventDrop callback
* @var string the javascript code that implements the eventDrop function
*/
public $eventDrop = "";
/**
* The javascript function to us as en eventResize callback
* @var string the javascript code that implements the eventResize function
*/
public $eventResize = "";
/**
* A js callback that triggered when the user clicks an event.
* @var string the javascript code that implements the eventClick function
*/
public $eventClick = "";
/**
* A js callback that triggered when the user clicks an day.
* @var string the javascript code that implements the dayClick function
*/
public $dayClick = "";
/**
* A js callback that will fire after a selection is made.
* @var string the javascript code that implements the select function
*/
public $select = "";
/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
*/
public function init()
{
//checks for the element id
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
//checks for the class
if (!isset($this->options['class'])) {
$this->options['class'] = 'fullcalendar';
}
parent::init();
}
/**
* Renders the widget.
*/
public function run()
{
$this->options['data-plugin-name'] = $this->_pluginName;
if (!isset($this->options['class'])) {
$this->options['class'] = 'fullcalendar';
}
echo Html::beginTag('div', $this->options) . "\n";
echo Html::beginTag('div',['class'=>'fc-loading','style' => 'display:none;']);
echo Html::encode($this->loading);
echo Html::endTag('div')."\n";
echo Html::endTag('div')."\n";
$this->registerPlugin();
}
/**
* Registers the FullCalendar javascript assets and builds the requiered js for the widget and the related events
*/
protected function registerPlugin()
{
$id = $this->options['id'];
$view = $this->getView();
/** @var \yii\web\AssetBundle $assetClass */
$assets = CoreAsset::register($view);
//by default we load the jui theme, but if you like you can set the theme to false and nothing gets loaded....
if($this->theme == true)
{
ThemeAsset::register($view);
}
if (array_key_exists('defaultView',$this->clientOptions) && ($this->clientOptions['defaultView'] == 'timelineDay' || $this->clientOptions['defaultView'] == 'timelineWeek' || $this->clientOptions['defaultView'] == 'timelineMonth' || $this->clientOptions['defaultView'] == 'agendaDay'))
{
SchedulerAsset::register($view);
}
if (isset($this->options['lang']))
{
$assets->language = $this->options['lang'];
}
if ($this->googleCalendar)
{
$assets->googleCalendar = $this->googleCalendar;
}
$js = array();
if($this->ajaxEvents != NULL){
$this->clientOptions['events'] = $this->ajaxEvents;
}
if(!is_null($this->contentHeight) && !isset($this->clientOptions['contentHeight']))
{
$this->clientOptions['contentHeight'] = $this->contentHeight;
}
if(isset($this->customButtons) && !isset($this->clientOptions['customButtons']))
{
$this->clientOptions['customButtons'] = $this->customButtons;
}
if(is_array($this->header) && isset($this->clientOptions['header']))
{
$this->clientOptions['header'] = array_merge($this->header,$this->clientOptions['header']);
} else {
$this->clientOptions['header'] = $this->header;
}
if(isset($this->defaultView) && !isset($this->clientOptions['defaultView']))
{
$this->clientOptions['defaultView'] = $this->defaultView;
}
// clear existing calendar display before rendering new fullcalendar instance
// this step is important when using the fullcalendar widget with pjax
$js[] = "var loading_container = jQuery('#$id .fc-loading');"; // take backup of loading container
$js[] = "jQuery('#$id').empty().append(loading_container);"; // remove/empty the calendar container and append loading container bakup
$cleanOptions = $this->getClientOptions();
$js[] = "jQuery('#$id').fullCalendar($cleanOptions);";
/**
* Loads events separately from the calendar creation. Uncomment if you need this functionality.
*
* lets check if we have an event for the calendar...
* if(is_array($this->events))
* {
* foreach($this->events AS $event)
* {
* $jsonEvent = Json::encode($event);
* $isSticky = $this->stickyEvents;
* $js[] = "jQuery('#$id').fullCalendar('renderEvent',$jsonEvent,$isSticky);";
* }
* }
*/
$view->registerJs(implode("\n", $js),View::POS_READY);
}
/**
* @return array the options for the text field
*/
protected function getClientOptions()
{
$id = $this->options['id'];
if ($this->onLoading)
$options['loading'] = new JsExpression($this->onLoading);
else {
$options['loading'] = new JsExpression("function(isLoading, view ) {
jQuery('#{$id}').find('.fc-loading').toggle(isLoading);
}");
}
//add new theme information for the calendar
$options['themeSystem'] = $this->themeSystem;
if ($this->eventRender){
$options['eventRender'] = new JsExpression($this->eventRender);
}
if ($this->eventAfterRender){
$options['eventAfterRender'] = new JsExpression($this->eventAfterRender);
}
if ($this->eventAfterAllRender){
$options['eventAfterAllRender'] = new JsExpression($this->eventAfterAllRender);
}
if ($this->eventDrop){
$options['eventDrop'] = new JsExpression($this->eventDrop);
}
if ($this->eventResize){
$options['eventResize'] = new JsExpression($this->eventResize);
}
if ($this->select){
$options['select'] = new JsExpression($this->select);
}
if ($this->eventClick){
$options['eventClick'] = new JsExpression($this->eventClick);
}
if ($this->dayClick){
$options['dayClick'] = new JsExpression($this->dayClick);
}
if (is_array($this->events) || is_string($this->events)){
$options['events'] = $this->events;
}
$options = array_merge($options, $this->clientOptions);
return Json::encode($options);
}
}