<?php

class Calendar extends CWidget
{

    public $libPathFullCalendar = 'ext.Calendar.assets'; //the path to the FullCalendar lib

    /**
     * @var string Google's calendar URL.
     */
    public $googleCalendarUrl;

    /**
     * @var string Theme's CSS file.
     */
    public $themeCssFile;

    public $events = array();

    /**
     * @var array FullCalendar's options.
     */
    public $options=array();

    /**
     * @var array HTML options.
     */
    public $htmlOptions=array();

    
    /**
     * @var string PHP file extension. Default is php.
     */
    public $ext='php';

    /**
     * Run the widget.
     */
    public function run()
    {

        $this->registerFiles();

        echo $this->showOutput();
    }


    /**
     * Register assets.
     */
    protected function registerFiles()
    {        
        $assetsDir=Yii::getPathOfAlias($this->libPathFullCalendar);
        $assets=Yii::app()->assetManager->publish($assetsDir);

        $cs=Yii::app()->clientScript;

        $ext=defined('YII_DEBUG') && YII_DEBUG ? 'min.js' : 'min.js';
        $cs->registerScriptFile($assets.'/lib/moment.'.$ext, CClientScript::POS_BEGIN);
        $cs->registerScriptFile($assets.'/fullcalendar.'.$ext, CClientScript::POS_BEGIN);
        $cs->registerScriptFile($assets.'/locale/pt-br.js', CClientScript::POS_BEGIN);

        $cs->registerCssFile($assets.'/fullcalendar.min.css');
        $cs->registerCssFile($assets.'/fullcalendar.print.min.css','print');

        if ($this->googleCalendarUrl) {
            $cs->registerScriptFile($assets.'/gcal.' . $ext);
            $this->options['events']=$this->googleCalendarUrl;
        }
        if ($this->themeCssFile) {
            $this->options['theme']=true;
            $cs->registerCssFile($assets.'/lib/'.$this->themeCssFile);
        }

        $this->options['header'] = array(
            'theme' => true,
            'left' => 'prev,next today',
            'center' => 'title',
            'right' => 'month,basicWeek,listDay,listMonth,listWeek'
        );

        $this->options['firstDay'] = 0;
        $this->options['defaultDate'] = date('Y-m-d');
        $this->options['navLinks'] = true;
        $this->options['editable'] = false;
        $this->options['eventLimit'] = true;
        $this->options['events'] = $this->events;
        $daysOfWeek = array();
        $hiddenDays = array();

        if(User::getUser()->FUN_DOMINGO == "7") {
            $daysOfWeek[] = 0;
        } else {
            $hiddenDays[] = 0;
        }
        if(User::getUser()->FUN_SEGUNDA == "1") {
            $daysOfWeek[] = 1; 
        } else {
            $hiddenDays[] = 1;
        }
        if(User::getUser()->FUN_TERCA == "2") {
            $daysOfWeek[] = 2; 
        } else {
            $hiddenDays[] = 2;
        }
        if(User::getUser()->FUN_QUARTA == "3") {
            $daysOfWeek[] = 3; 
        } else {
            $hiddenDays[] = 3;
        }
        if(User::getUser()->FUN_QUINTA == "4") {
            $daysOfWeek[] = 4; 
        } else {
            $hiddenDays[] = 4;
        }
        if(User::getUser()->FUN_SEXTA == "5") {
            $daysOfWeek[] = 5; 
        } else {
            $hiddenDays[] = 5;
        }
        if(User::getUser()->FUN_SABADO == "6") {
            $daysOfWeek[] = 6; 
        } else {
            $hiddenDays[] = 6;
        }

        $this->options['businessHours'] = array(
            'dow' => $daysOfWeek,
            'start' => User::horaInicioManha(true),
            'end' => User::horaFimTarde(true),
            );
        $this->options['hiddenDays'] = $hiddenDays;
        
        $this->options['eventRender'] = 'js:function(event, element, view) {$(element).qtip({content: event.description,position:{my: "middle left", at:"middle right",viewport:$(window)},style:{tip:{corner:true}}});}';

        $this->widget('ext.qTip2.qTip2');

        $js='$("#'.$this->id.'").fullCalendar('.CJavaScript::encode($this->options).');';
        $cs->registerScript(__CLASS__.'#'.$this->id, $js, CClientScript::POS_READY);
        
    }

    /**
     * Returns the html output.
     *
     * @return string Html output
     */
    protected function showOutput()
    {
        if (! isset($this->htmlOptions['id']))
            $this->htmlOptions['id']=$this->id;

        return CHtml::tag('div', $this->htmlOptions,'');
    }
}
