Welcome Guest, Not a member yet? Register   Sign In
I Am Missing Something
#1

[eluser]wowdezign[/eluser]
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once(BASEPATH.'libraries/Calendar'.EXT);
class Availability_Calendar{
    private $ci;
    private $show_months;
    private $prefs;
    private    $first_year;
    private $first_month;
    private $start_date;
    private $all_dates = array();
    private $months = array();
    private $date_arrs = array();

    public function __construct(){
        $this->ci = get_instance();
        $this->set_firsts();
    }

    public function initialize($months=1){
        $this->show_months = $months;
        $this->set_prefs();
        for($i=0;$i<$this->show_months;$i++){
            $this->calendars[] = $this->ci->load->library('calendar',$this->prefs,'calendar'.$i);
        }
        $this->get_availability();
        $this->set_prev_next();
    }

    public function generate(){
        $i=0;
        foreach($this->date_arrs as $mnth){
            $c = 'calendar'.$i;
            echo $this->ci->$c->generate($mnth[0],$mnth[1],$mnth[2]);
            $i++;
        }
    }

    public function get_availability(){
/////////////////////////////////////////////////////////////////////////////////
// Database Result Set Simulation
/////////////////////////////////////////////////////////////////////////////////
        $obj1 = new stdClass;
        $obj1->property_id = 1;
        $obj1->reservation_start = '2010-01-29 10:00:00';
        $obj1->reservation_end = '2010-02-04 10:00:00';
        $obj1->number_of_days = 6;
        
        $obj2 = new stdClass;
        $obj2->property_id = 1;
        $obj2->reservation_start = '2010-03-12 17:32:12';
        $obj2->reservation_end = '2010-03-16 17:32:12';
        $obj2->number_of_days = 4;
        
        $obj3 = new stdClass;
        $obj3->property_id = 1;
        $obj3->reservation_start = '2010-02-10 09:52:33';
        $obj3->reservation_end = '2010-02-14 09:52:33';
        $obj3->number_of_days = 4;
        
        $sample = array($obj1,$obj2,$obj3);
////////////////////////////////////////////////////////////////////////////////
// End Database Simulation
////////////////////////////////////////////////////////////////////////////////
        $this->set_dates($sample);
        $this->set_cals();
    }

    public function get_prev_link(){
        return $this->prev_link;
    }

    public function get_next_link(){
        return $this->next_link;
    }

    private function set_prev_next(){
        $prev_year = date('Y',strtotime('-1 month',strtotime($this->start_date)));
        $prev_month = date('m',strtotime('-1 month',strtotime($this->start_date)));
        $next_year = date('Y',strtotime('+1 month',strtotime($this->start_date)));
        $next_month = date('m',strtotime('+1 month',strtotime($this->start_date)));
        $this->prev_link = site_url($this->ci->uri->segment(1).'/'.$this->ci->uri->segment(2).'/'.$prev_year.'/'.$prev_month);
        $this->next_link = site_url($this->ci->uri->segment(1).'/'.$this->ci->uri->segment(2).'/'.$next_year.'/'.$next_month);
    }

    private function set_firsts(){
        $this->first_year = ($this->ci->uri->segment(3)) ? $this->ci->uri->segment(3) : Date('Y') ;
        $this->first_month = ($this->ci->uri->segment(4) && !($this->ci->uri->segment(4) > 12)) ? $this->ci->uri->segment(4) : redirect(site_url($this->ci->uri->segment(1).'/'.$this->ci->uri->segment(2).'/'.$this->ci->uri->segment(3).'/'.date('m'))) ;
        $this->start_date = $this->first_year."-".$this->first_month."-01";
    }

    private function set_dates($arr){
        foreach($arr as $row){
            $this->add_dates($row);
        }
    }

    private function set_cals(){
        $this->months[0] = date('m',strtotime($this->start_date));
        $this->months[1] = date('m',strtotime('+1 month',strtotime($this->start_date)));
        $this->months[2] = date('m',strtotime('+2 month',strtotime($this->start_date)));        

        $tmp1 = array();
        $tmp2 = array();
        $tmp3 = array();

        foreach($this->all_dates as $k=>$dt){
            if(date('m',strtotime($dt))==$this->months[0]){
                $year1 = date('Y',strtotime($dt));
                $month1 = date('m',strtotime($dt));
                $tmp1[date('d',strtotime($dt))] = TRUE;
            }
            if(date('m',strtotime($dt))==$this->months[1]){
                $year2 = date('Y',strtotime($dt));
                $month2 = date('m',strtotime($dt));
                $tmp2[date('d',strtotime($dt))] = TRUE;
            }
            if(date('m',strtotime($dt))==$this->months[2]){
                $year3 = date('Y',strtotime($dt));
                $month3 = date('m',strtotime($dt));
                $tmp3[date('d',strtotime($dt))] = TRUE;
            }
        }

        $this->date_arrs[0][0] = ($year1)?($year1):date('Y',strtotime('+0 month',strtotime($this->start_date)));
        $this->date_arrs[1][0] = ($year2)?($year2):date('Y',strtotime('+1 month',strtotime($this->start_date)));
        $this->date_arrs[2][0] = ($year3)?($year3):date('Y',strtotime('+2 month',strtotime($this->start_date)));

        $this->date_arrs[0][1] = ($month1)?($month1):date('m',strtotime('+0 month',strtotime($this->start_date)));
        $this->date_arrs[1][1] = ($month2)?($month2):date('m',strtotime('+1 month',strtotime($this->start_date)));
        $this->date_arrs[2][1] = ($month3)?($month3):date('m',strtotime('+2 month',strtotime($this->start_date)));

        $this->date_arrs[0][2] = $tmp1;
        $this->date_arrs[1][2] = $tmp2;
        $this->date_arrs[2][2] = $tmp3;
    }
    
    private function add_dates($row){
        $begin = strtotime($row->reservation_start);
        $d = 60*60*24;
        for($i=0;$i<$row->number_of_days;$i++){
            $nd = $d*$i;
            $this->all_dates[] = date('Y-m-d',$begin+$nd);
        }
    }
}
#2

[eluser]wowdezign[/eluser]
Controller:
Code:
$this->load->library('Availability_Calendar');
$this->availability_calendar->initialize(3);



View:
Code:
<div class="vacancyCalendarContainer">
        <table id="calendarPager">
            <tr>
                <td class="alignLeft" style="padding-left:15px;">
                    <a >availability_calendar->get_prev_link(); ?&gt;">&lt;&lt; Previous</a>
                </td>
                <td>
                    <h3 style='padding:.2em;'>Available Dates</h3>
                </td>
                <td class="alignRight" style="padding-right:15px;">
                    <a >availability_calendar->get_next_link(); ?&gt;">Next &gt;&gt;</a>
                </td>
            </tr>
        </table>
        <table class='calendarKey'>
            <tr>
                <td>
                    <p>Available =&nbsp;&nbsp;<span class='availableKeyBlock'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p>
                </td>
                <td>
                    <p>Unavailable =&nbsp;&nbsp;<span class='unAvailableKeyBlock'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p>
                </td>
                <td>
                    <p>Today =&nbsp;&nbsp;<span class='todayKeyBlock'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p>
                </td>
            </tr>
        </table>
        &lt;?php $this->availability_calendar->generate(); ?&gt;
        </div>
        <div id="availabilityFooter">&nbsp;</div>

Everything works fine except:

In the data, one reservation spans across January and February, but when you look at the resulting calendars, the three days in February are not shown with content in them. I have done "print_r()" to try to trace the output, but I cannot find where this breaks.

Does anyone see what I am missing?
#3

[eluser]wowdezign[/eluser]
Wow! Nobody has any input on this?

I saw an older post where someone said that multiple instances of the calendar is not needed. Maybe I have wasted my time on this.

I really am hoping for an CI expert to shed some light on this issue I have. I just can't see why my dates array is getting broken.

I know that it's happening in the generate() method of the Calendar class but I can't see why.
#4

[eluser]wowdezign[/eluser]
OK. I finally found my problem!

If you pass data into the generate method, be sure that your array keys are not in 0? format for example,

Code:
array(01=>TRUE,02=>TRUE,03=>TRUE); // This code will not work.

However,

Code:
array(1=>TRUE,2=>TRUE,3=>TRUE); // This code will work.

Since the only time this happens is at the beginning of the month and I was testing for overlap, I mistakenly thought it was due to the dates going from one table to another.

So, there you have it. If you are having problems with dates showing up, make sure any keys under ten do not contain a 0. Now I am ecstatic :-) :lol: :cheese: :bug: :exclaim:




Theme © iAndrew 2016 - Forum software by © MyBB