<?php

class Ecalendar extends CFormModel{

        private $table = array();
        private $count = 100;
        public  $month = array('0'=>'January','1'=>'February','2'=>'March','3'=>'April','4'=>'May','5'=>'June','6'=>'July','7'=>'August','8'=>'September','9'=>'October','10'=>'November','11'=>'December');
        public  $itemsOnPage = 25;
        public  $page = 1;
        public  $startDate = 0;
        public  $endDate =  0;
        public  $sort	= '';
        public  $desc	= 0;
        

        
        public  $filter = 25;
        public  $importance = 0;
        public  $country = 0;
        public  $timeZone = 0;
        
        public  $listEcalendar = array();
        public  $listCountry = array();


        public function init() {
            $monday = date("w")-1;
            $friday = 7-date("w");
            $this->startDate = date('m/d/Y',mktime(0, 0, 0, date("m"),   date("d")-$monday,   date("Y")));
            $this->endDate   = date('m/d/Y',mktime(0, 0, 0, date("m"),   date("d")+$friday,   date("Y")));
                    
            parent::init();
        } 
        
        public function rules() {
            return array(
                array('country', 'numerical'),
                array('importance', 'numerical'),
                array('itemsOnPage', 'numerical'),
                array('startDate'   	, 'length', 'min'=>3),
                array('endDate'    	, 'length', 'min'=>3),
                array('filter', 'length', 'min' => 3),
                array('sort', 'length', 'min' => 3),
                array('desc', 'boolean'),
                array('page', 'numerical'),
            );
        }
    
         public function GetCalendar(){
             
            $page = $this->page > 0?$this->page - 1:0;
            
            $params = array(
                'getCalendar'=>true,
                'timeZone' =>$this->timeZone,
                'itemsOnPage'=>$this->itemsOnPage,
                'page'=>$page,
                'startDate'=>$this->startDate,
                'endDate'=>$this->endDate,
                'filter'=>$this->filter ,
                'importance'=>$this->importance,
                'country'=>$this->country,
                'sort'=> $this->sort,
                'desc'=> $this->desc
             );
            
             $Response = (array) Yii::app()->curl->post('http://bmfn.com/ecalendar-json.html', $params ,80);// /assets/site/snipet/ecalendar-json.php
//             echo '<pre>';
//             print_r($params);
//             print_r($Response);
//             exit();      
             $this->count = $Response['count'];
             $tab = html_entity_decode($Response['table']);
             $this->table = json_decode($tab,true);
     

             
            $table = '';
            if($this->table != null){
                foreach ($this->table as $value){
                    $importance = $value['importance']==4?'Holiday':$value['importance'];
                    $table .= '<tr id="'.$value['indicator_id'].'">';
                       $table .= '<td class="znak__ plus__" style="display: table-cell;">+</td>';
                       $table .= '<td>'.date('H:i',$value['newtime']).'</td>';
                       $table .= '<td>'.date('d F',$value['newtime']).'</td>';
                       $table .= '<td>'.$value['country_en'].'</td>';
                       $table .= '<td>'.$importance.'</td>';
                       $table .= '<td>'.$value['name_en'].'</td>';
                       if($value['period_date'] == 0){
                         $d = array(0=>'January',1=>'February',2=>'March',3=>'April',4=>'May',5=>'June',6=>'July',7=>'August',8=>'September',9=>'October',10=>'November',11=>'December',12=>'1st quater',12=>'2st quater',14=>'3st quater',15=>'4st quater');
                         $table .= '<td>'.$d[$value['period']].'</td>';
                       }else{
                         $table .= '<td>'.date('F',$value['period_date']).'</td>';  
                       }
                       $table .= '<td>'.$value['value_prev'].'<br>'.$value['value_prev2'].'</td>';
                       $table .= '<td>'.$value['value_fact'].'<br>'.$value['value_fact2'].'</td>';
                       $table .= '<td>'.$value['value_will'].'<br>'.$value['value_will2'].'</td>';                                             
                       $table .= '<td>'.$value['advanced_en'].'</td>';
                       $table .= '<td><a target="_blank" href="'.$value['url'].'">'.$value['url'].'</a></td>';
                       $table .= '<td>'.$value['source'].'</td>';
                    $table .= '</tr>';
                }
            }else{
                  $table.= '<tr><td style="display:none;"></td><td style="text-align: center; display:table-cell;" colspan="15"><br>'.Yii::t("main","107_no_reports").'<br>&nbsp;</td></tr>';
            }
             return $table;
        }
        
        public function GetPagination(){
              //$Response = (array) Yii::app()->curl->post('http://bmfn.com/ecalendar-json.html', array('getCalendar'=>true),80);
              return array(
                  'sort'=> $this->sort,
                  'desc'=> $this->desc,
                  'timeZone'=>  $this->timeZone,
                  'count'=>$this->count,
                  'itemsOnPage'=>$this->itemsOnPage,
                  'page'=>$this->page,
                  'startDate'=>$this->startDate,
                  'endDate'=>$this->endDate,
                  'filter'=>$this->filter ,
                  'importance'=>$this->importance,
                  'country'=>$this->country);
        }
        
        
        public function GetCountries(){
          $tmp = (array)Yii::app()->curl->post('http://bmfn.com/ecalendar-json.html', array('getCountries'=>true) ,80);
          $this->listCountry = json_decode($tmp['country'],true);

       /* $ipx = isset($_SERVER['HTTP_CF_CONNECTING_IP']) ? $_SERVER['HTTP_CF_CONNECTING_IP'] : $_SERVER['REMOTE_ADDR'];
        if ($ipx == '77.222.143.18' || $ipx == '46.98.252.74') {
            var_dump($this->listCountry);die();
        }*/

          return $this->listCountry;
        }
}
