Advertisement
kitt

calendar.php

Dec 22nd, 2011
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 29.53 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @author Marosdee Uma, website:: www.danya-reload.com @ danya0365[at]live.com, danya0365[at]gmail.com, danya-planemo[at]live.com
  5.  * @copyright 2009
  6.  * @version 1.5
  7.  */
  8.  
  9. class calendar extends base_object {
  10.     protected $table = 'sm_calendar';
  11.     protected $id_field = 'id';
  12.     var $id = null;
  13.     var $use_id;
  14.     var $web_id;
  15.  
  16.     /**
  17.     * 0-6 date('w')
  18.     *
  19.     * @var integer
  20.     */     
  21.     public $todayDayOfWeek;
  22.    
  23.     /**
  24.     * 1-31 date('j')
  25.     *
  26.     * @var integer
  27.     */ 
  28.     public $todayDay;
  29.    
  30.     /**
  31.     * 1-6 getWeek()
  32.     *
  33.     * @var integer
  34.     */ 
  35.     public $todayWeek;
  36.    
  37.     /**
  38.     * 1-12 date('n')
  39.     *
  40.     * @var integer
  41.     */ 
  42.     public $todayMonth;
  43.    
  44.     /**
  45.     * 1970 or 2009 date('Y')
  46.     *
  47.     * @var integer
  48.     */ 
  49.     public $todayYear;
  50.    
  51.     /**
  52.     * 0-6 date('w')
  53.     *
  54.     * @var integer
  55.     */ 
  56.     public $defaultDayofWeek;
  57.  
  58.     /**
  59.     * 1-31 date('j')
  60.     *
  61.     * @var integer
  62.     */ 
  63.     public $defaultDay;
  64.  
  65.     /**
  66.     * 1-6 getWeek()
  67.     *
  68.     * @var integer
  69.     */ 
  70.     public $defaultWeek;
  71.  
  72.     /**
  73.     * 1-12 date('n')
  74.     *
  75.     * @var integer
  76.     */ 
  77.     public $defaultMonth;
  78.  
  79.     /**
  80.     * 1970 or 2009 date('Y')
  81.     *
  82.     * @var integer
  83.     */ 
  84.     public $defaultYear;
  85.    
  86.     /**
  87.     * 1-31
  88.     *
  89.     * @var integer
  90.     */ 
  91.     public $totalDayInMonth;
  92.  
  93.     /**
  94.     * 0-6
  95.     *
  96.     * @var integer
  97.     */ 
  98.     public $firstDayOfWeek;
  99.  
  100.     /**
  101.     * 0-6
  102.     *
  103.     * @var integer
  104.     */ 
  105.     public $lastDayOfWeek;
  106.  
  107.     /**
  108.     * array of MonthTitle
  109.     *
  110.     * @var array
  111.     */ 
  112.     //private $monthTitle = array('january','February','March','April','May','June','July','August','September','October','November','December');
  113.     private $monthTitle = array('janvier','Février','Mars','Avril','Mai','Juin','Juillet','Aout','Septembre','Octobre','Novembre','Decembre');
  114.  
  115.     /**
  116.     * Show color in table row
  117.     *
  118.     * @var boolean
  119.     */ 
  120.     private $showColor = true;
  121.  
  122.     /**
  123.     * array of table color
  124.     *
  125.     * @var array
  126.     */ 
  127.     private $colorArray = array('#E1E1E1','#E1E1E1','#E1E1E1','#E1E1E1','#E1E1E1','#E1E1E1','#E1E1E1');
  128.  
  129.     /*
  130.     * width of table
  131.     */
  132.     private $defaultTableWidth = 174;
  133.  
  134.     /**
  135.     * column per row in year calendar
  136.     *
  137.     * @var integer
  138.     */ 
  139.     private $defaultPerRow = 4;
  140.  
  141.     /**
  142.     * class in table row
  143.     *
  144.     * @var array
  145.     */ 
  146.     private $defaultClassCol = array('colDay1', 'colDay2', 'colDay3', 'colDay4', 'colDay5', 'colDay6', 'colDay7');
  147.  
  148.     /**
  149.     * today class in calendar
  150.     *
  151.     * @var string
  152.     */ 
  153.     private $defaultTodayClass = 'today';
  154.    
  155.     /**
  156.     * blank class in calendar
  157.     *
  158.     * @var string
  159.     */ 
  160.     private $defaultBlankClass = 'colBlank';
  161.  
  162.     /**
  163.     * event class in calendar
  164.     *
  165.     * @var string
  166.     */ 
  167.     private $defaultEventDayClass = 'eventDay';
  168.  
  169.     /**
  170.     * array of Day of Week Title
  171.     *
  172.     * @var array
  173.     */ 
  174.     //private $dayOfWeekTitle = array('SUNDAY','MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY');
  175.     private $dayOfWeekTitle = array('L','M','M','J','V','S','D');
  176.  
  177.     /**
  178.     * array of event Day
  179.     *
  180.     * @var array
  181.     */ 
  182.     private $eventDay = array();
  183.  
  184.     /**
  185.     * title of week
  186.     *
  187.     * @var array
  188.     */ 
  189.     private $defaultWeekTitleFormat = " {week} - {month} - {year}";
  190.  
  191.     /**
  192.     * title of month
  193.     *
  194.     * @var string
  195.     */ 
  196.     private $defaultMonthTitleFormat = "{month} - {year}";
  197.  
  198.     /**
  199.     * title of year
  200.     *
  201.     * @var string
  202.     */ 
  203.     private $defaultYearTitleFormat = "{year}";
  204.    
  205.     /**
  206.     * Thai year will +543
  207.     *
  208.     * @var boolean
  209.     */ 
  210.     private $thaiYear = false;
  211.  
  212.     /**
  213.     * Show or hide Day of Back Month Or Week
  214.     *
  215.     * @var boolean
  216.     */ 
  217.     private $showBackDay = true;
  218.  
  219.     /**
  220.     * Show or hide Day of Next Month Or Week
  221.     *
  222.     * @var boolean
  223.     */ 
  224.     private $showNextDay = true;
  225.    
  226.     /**
  227.     * class constructor
  228.     *
  229.     * @return void
  230.     */
  231.     public function complete_construct()
  232.     {
  233.         $this->todayDayOfWeek = $this->defaultDayofWeek = date('w');
  234.         $this->todayDay = $this->defaultDay = date('j');
  235.         $this->todayWeek = $this->defaultWeek = $this->getWeek( date('j'), date('n'), date('Y'));
  236.         $this->todayMonth = $this->defaultMonth = date('n');
  237.         $this->todayYear = $this->defaultYear = date('Y');
  238.         if ( isset($_GET['y'])) $this->setYear($_GET['y']);
  239.     }
  240.  
  241.     public function get_fields(){
  242.         $array = array( 'id'     => $this->id,
  243.                         'fk_use_id'  => $this->use_id,
  244.                         'fk_web_id'  => $this->web_id,
  245.                         'title'  => $this->title,
  246.                         'start'  => $this->start,
  247.                         'end'    => $this->end,
  248.                         'remark' => $this->remark );
  249.         return $array;
  250.     }
  251.     /**
  252.      * @param $array
  253.      * @return unknown_type
  254.      */
  255.     public function set_fields( $array ) {
  256.         $this->id      = $array["id"];
  257.         $this->use_id   = (isset ($array["fk_use_id"]))? $array["fk_use_id"] : '';
  258.         $this->web_id   = (isset ($array["fk_web_id"])) ? $array["fk_web_id"] : '';
  259.         $this->title   = $array["title"];
  260.         $this->start   = $array["start"];
  261.         $this->end     = $array["end"];
  262.         $this->remark  = $array["remark"];
  263.         return 1;
  264.     }  
  265.     /**
  266.     * set column class
  267.     *  
  268.     * @param    array $class The name of column class 1-7
  269.     *               [0] class in SUNDAY
  270.     *               [1] class in MONDAY
  271.     *               [2] class in TUESDAY    
  272.     *               [3] class in WEDNESDAY
  273.     *               [4] class in THURSDAY
  274.     *               [5] class in FRIDAY
  275.     *               [6] class in SATURDAY
  276.     *
  277.     * @return   void
  278.     */
  279.     public function setColumnClass($class)
  280.     {
  281.         if ( count($class) >= 7 )
  282.         $this->defaultClassCol = $class;
  283.     }
  284.  
  285.     /**
  286.     * set column today class
  287.     *
  288.     * @param    string $today The name of today class
  289.     *
  290.     * @return   void
  291.     */
  292.     public function setTodayClass($today)
  293.     {
  294.         $this->defaultTodayClass = $today;
  295.     }
  296.    
  297.     /**
  298.     * set column blank class
  299.     *
  300.     * @param    string $blank The name of blank class
  301.     *
  302.     * @return   void
  303.     */
  304.     public function setBlankClass($blank)
  305.     {
  306.         $this->defaultBlankClass = $blank;
  307.     }
  308.     public function setWebId($webId) {
  309.         $this->web_id = $webId;
  310.     }
  311.     /**
  312.     * set column event class
  313.     *
  314.     * @param    string $event The name of event class
  315.     *
  316.     * @return   void
  317.     */
  318.     public function setEventClass($event)
  319.     {
  320.         $this->defaultEventDayClass = $event;
  321.     }
  322.  
  323.     /**
  324.     * set column per row in year calendar
  325.     *
  326.     * @param    integer $perRow (1-99)
  327.     *
  328.     * @return   void
  329.     */
  330.     public function setYearColumnPerRow($perRow)
  331.     {
  332.         if ( is_numeric($perRow) && $perRow > 0 ) $this->defaultPerRow = $perRow;
  333.     }
  334.  
  335.     /**
  336.     * set day of week title format
  337.     *
  338.     * @param    string $string
  339.     *               {week} will replace with number of current week
  340.     *               {month} will replace with the name of current month
  341.     *               {year} will replace with number of current year
  342.     *
  343.     * @return   void
  344.     */
  345.     public function setWeekTitleFormat($string)
  346.     {
  347.         $this->defaultWeekTitleFormat = $string;
  348.     }
  349.  
  350.     /**
  351.     * set month title format
  352.     *
  353.     * @param    string $string
  354.     *               {month} will replace with the name of current month
  355.     *               {year} will replace with number of current year
  356.     *
  357.     * @return   void
  358.     */
  359.     public function setMonthTitleFormat($string)
  360.     {
  361.         $this->defaultMonthTitleFormat = $string;
  362.     }
  363.  
  364.     /**
  365.     * set month title format
  366.     *
  367.     * @param    string $string
  368.     *               {year} will replace with number of current year
  369.     *
  370.     * @return   void
  371.     */
  372.     public function setYearTitleFormat($string)
  373.     {
  374.         $this->defaultYearTitleFormat = $string;
  375.     }
  376.  
  377.     /**
  378.     * set event day
  379.     *
  380.     * @param    array $eventDay
  381.     *               [0] will check in caledar format '31-1-2009'
  382.     *               [1] the name of event
  383.     *
  384.     * @return   void
  385.     */
  386.     public function setEventDay($eventDay)
  387.     {
  388.         if ( count($eventDay) > 0 )
  389.         $this->eventDay = $eventDay;
  390.     }
  391.  
  392.     /**
  393.     * set color in day
  394.     *
  395.     * @param    array $color
  396.     *               [0] color in SUNDAY
  397.     *               [1] color in MONDAY
  398.     *               [2] color in TUESDAY    
  399.     *               [3] color in WEDNESDAY
  400.     *               [4] color in THURSDAY
  401.     *               [5] color in FRIDAY
  402.     *               [6] color in SATURDAY
  403.     *
  404.     * @return   void
  405.     */
  406.     public function setColor($color)
  407.     {
  408.         if ( count($color) >= 7 )
  409.         $this->colorArray = $color;
  410.     }
  411.  
  412.  
  413.     /**
  414.     * set day of week title
  415.     *
  416.     * @param    array $day
  417.     *               [0] SUNDAY
  418.     *               [1] MONDAY
  419.     *               [2] TUESDAY    
  420.     *               [3] WEDNESDAY
  421.     *               [4] THURSDAY
  422.     *               [5] FRIDAY
  423.     *               [6] SATURDAY
  424.     *
  425.     * @return   void
  426.     */
  427.     public function setDayOfWeekTitle($day)
  428.     {
  429.         if ( count($day) == 7 )
  430.         $this->dayOfWeekTitle = $day;
  431.     }
  432.    
  433.     /**
  434.     * set month title name
  435.     *
  436.     * @param    array $month
  437.     *               [0] January
  438.     *               [1] February
  439.     *               [2] March    
  440.     *               [3] April
  441.     *               [4] May
  442.     *               [5] June
  443.     *               [6] July
  444.     *               [7] August
  445.     *               [8] September
  446.     *               [9] October
  447.     *               [10] Noverber
  448.     *               [11] December
  449.     *
  450.     * @return   void
  451.     */
  452.     public function setMonthTitle($month)
  453.     {
  454.         if ( count($month) == 12 )
  455.         $this->monthTitle = $month;
  456.     }
  457.  
  458.     /**
  459.     * set calendar table width
  460.     *
  461.     * @param    integer $width (1-2000)
  462.     *
  463.     * @return   void
  464.     */
  465.     public function setTableWidth($width)
  466.     {
  467.         $this->defaultTableWidth = $width;
  468.     }
  469.  
  470.     /**
  471.     * set current calendar week
  472.     *
  473.     * @param    integer $w (1-6)
  474.     *
  475.     * @return   void
  476.     */
  477.     public function setWeek($w)
  478.     {
  479.         if ( is_numeric($w) && $w > 0 && $w < 7 ) $this->defaultWeek = $w * 1;
  480.     }
  481.  
  482.     /**
  483.     * set current calendar month
  484.     *
  485.     * @param    integer $m (1-12)
  486.     *
  487.     * @return   void
  488.     */
  489.     public function setMonth($m)
  490.     {
  491.         if ( is_numeric($m) && $m > 0 && $m < 13) $this->defaultMonth = $m * 1;
  492.     }
  493.  
  494.     /**
  495.     * set current calendar year
  496.     *
  497.     * @param    integer $y (1970 or 2009)
  498.     *
  499.     * @return   void
  500.     */
  501.     public function setYear($y)
  502.     {
  503.         if ( is_numeric($y) && $y > 0 ) $this->defaultYear = $y * 1;
  504.     }
  505.  
  506.     /**
  507.     * set display color in day of week column
  508.     *
  509.     * @param    boolean $boolean (TRUE or FALSE)
  510.     *
  511.     * @return   void
  512.     */
  513.     public function setShowColor($boolean)
  514.     {
  515.         if ( is_bool($boolean) ) $this->showColor = $boolean;
  516.     }
  517.    
  518.     /**
  519.     * set use Thai year
  520.     *
  521.     * @param    boolean $boolean (TRUE or FALSE)
  522.     *
  523.     * @return   void
  524.     */
  525.     public function setThaiYear($boolean)
  526.     {
  527.         if ( is_bool($boolean) ) $this->thaiYear = $boolean;
  528.     }
  529.  
  530.     /**
  531.     * set show back day of back month
  532.     *
  533.     * @param    boolean $boolean (TRUE or FALSE)
  534.     *
  535.     * @return   void
  536.     */
  537.     public function setShowBackDay($boolean)
  538.     {
  539.         if ( is_bool($boolean) ) $this->showBackDay = $boolean;
  540.     }
  541.  
  542.     /**
  543.     * set show next day of next month
  544.     *
  545.     * @param    boolean $boolean (TRUE or FALSE)
  546.     *
  547.     * @return   void
  548.     */
  549.     public function setShowNextDay($boolean)
  550.     {
  551.         if ( is_bool($boolean) ) $this->showNextDay = $boolean;
  552.     }
  553.  
  554.     /**
  555.     * calculate number of week
  556.     *
  557.     * @param    integer $d (1-31)
  558.     * @param    integer $m (1-12)
  559.     * @param    integer $y (1970 or 2009)
  560.     *
  561.     * @return integer
  562.     */
  563.     public function getWeek($d = '', $m = '', $y = '')
  564.     {
  565.         $day    =   date('j');
  566.         $month  =   date('n');
  567.         $year   =   date('Y');
  568.        
  569.         if ( is_numeric($d) && $d > 0 ) $day    = $d * 1;
  570.         if ( is_numeric($m) && $m > 0 ) $month  = $m * 1;
  571.         if ( is_numeric($y) && $y > 0 ) $year   = $y * 1;
  572.        
  573.         $firstWeek      =   date('w', mktime( 0, 0, 0, $month, 1, $year));
  574.        
  575.         return ceil( ( $day + $firstWeek )  / 7 );
  576.     }
  577.    
  578.     /**
  579.     * get Calendar Week
  580.     *
  581.     * @param    integer $w (1-6)
  582.     * @param    integer $m (1-12)
  583.     * @param    integer $y (1970 or 2009)
  584.     *
  585.     * @return   output
  586.     */
  587.     public function calendarWeek($w = '', $m = '', $y = '')
  588.     {
  589.         $list = '';
  590.        
  591.         if ( is_numeric($w) && $w > 0 && $w < 7 ) $this->defaultWeek = $w * 1;
  592.         if ( is_numeric($m) && $m > 0 && $m < 13) $this->defaultMonth = $m * 1;
  593.         if ( is_numeric($y) && $y > 0 ) $this->defaultYear = $y * 1;
  594.        
  595.         $this->totalDayInMonth = date('t', mktime( 0, 0, 0, $this->defaultMonth, 1, $this->defaultYear) );
  596.         $this->firstDayOfWeek = $this->_getFirstDayOfWeek();
  597.         $this->lastDayOfWeek = $this->_getLastDayOfWeek();
  598.  
  599.         $backWeek   =   $this->defaultWeek - 1;
  600.         $backMonth  =   $this->defaultMonth;
  601.         $backYear   =   $this->defaultYear;
  602.  
  603.         $nextWeek   =   $this->defaultWeek + 1;
  604.         $nextMonth  =   $this->defaultMonth;
  605.         $nextYear   =   $this->defaultYear;
  606.        
  607.         if ( $this->defaultWeek == 1 && $this->firstDayOfWeek != 0 )
  608.         {
  609.             $backMonth  =   $this->defaultMonth -1;
  610.             $backYear   =   $this->defaultYear;
  611.            
  612.             if ( $backMonth < 1 )
  613.             {
  614.                 $backMonth  =   12;
  615.                 $backYear   =   $this->defaultYear -1;
  616.             }
  617.            
  618.             $backWeek = $this->getWeek( date('t', mktime( 0, 0, 0, $backMonth, 1, $backYear) ) , $backMonth, $backYear);
  619.            
  620.             $totalDay = 7 - $this->firstDayOfWeek;
  621.  
  622.             $totalDayInBackMonth = date('t', mktime( 0, 0, 0, $backMonth, 1, $backYear) );
  623.  
  624.             $startDayInBackMonth = ( $totalDayInBackMonth - $this->firstDayOfWeek ) + 1;
  625.            
  626.             $list .= "<tr>\n";
  627.            
  628.             for ( $i = $startDayInBackMonth; $i <= $totalDayInBackMonth; $i++ )
  629.             {
  630.                 $y = ( $this->showBackDay ) ? $i : 0;
  631.                 $list .= $this->_blankFormat($y);
  632.             }
  633.             for ( $i = 1; $i <= $totalDay; $i++ )
  634.             {
  635.                 $list .= $this->_dayFormat($i);
  636.             }
  637.             $list .= "</tr>\n";
  638.         }
  639.         else
  640.         {
  641.             $m = 1;
  642.  
  643.             $startDay = ( ( 7 * ( $this->defaultWeek - 1 ) ) - $this->firstDayOfWeek ) + 1;
  644.            
  645.             $i = $startDay;
  646.  
  647.             $list .= "<tr>\n";
  648.  
  649.             while ( $i < ( 7 + $startDay ) )
  650.             {
  651.                 if ( $i <= $this->totalDayInMonth )
  652.                 {
  653.                     $list .= $this->_dayFormat($i);
  654.                     $m++;
  655.                 }
  656.                 $i++;
  657.             }
  658.  
  659.             if ( $m < 8 )
  660.             {
  661.                 $nextWeek   =   1;
  662.                 $nextMonth  =   $this->defaultMonth +1;
  663.                 $nextYear   =   $this->defaultYear;
  664.  
  665.                 if ( $nextMonth > 12 )
  666.                 {
  667.                     $nextMonth  =   1;
  668.                     $nextYear   =   $this->defaultYear +1;
  669.                 }
  670.  
  671.                 $total = 8 - $m;
  672.                
  673.                 for ( $x = 1; $x <= $total; $x++ )
  674.                 {
  675.                     $z = ( $this->showNextDay ) ? $x : 0;
  676.                     $list .= $this->_blankFormat($z);
  677.                 }
  678.                 $list .= "</tr>\n";
  679.             }
  680.            
  681.             if ( $m == 8 )
  682.             {
  683.                 $list .= "</tr>\n";
  684.             }
  685.         }
  686.            
  687.         $weekTitle = $this->weekTitleFormat();
  688.        
  689.         $javaScript = $this->_showJavaScript();
  690.  
  691.         return <<<HTML
  692.             {$javaScript}
  693.             <table width="{$this->defaultTableWidth}" class="calendartable">
  694.                 <tr class="calendar-header">
  695.                     <td class="calendar-header"><a href='?w={$backWeek}&m={$backMonth}&y={$backYear}' onclick='getCalendarWeek($backWeek,$backMonth,$backYear); return false;'> &lt;&lt; </a></td>
  696.                     <td colspan='5' class="calendar-header">
  697.                         {$weekTitle}
  698.                     </td>
  699.                     <td class="calendar-header"><a href='?w={$nextWeek}&m={$nextMonth}&y={$nextYear}' onclick='getCalendarWeek($nextWeek,$nextMonth,$nextYear); return false;'> &gt;&gt; </a></td>
  700.                 </tr>
  701.                 <tr class="calendar-title">
  702.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[0]}</th>
  703.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[1]}</th>
  704.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[2]}</th>
  705.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[3]}</th>
  706.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[4]}</th>
  707.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[5]}</th>
  708.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[6]}</th>
  709.                 </tr>
  710.                 {$list}
  711.                 <tr>
  712.                     <td colspan='7' class="calendar-today">
  713.                         <a href='?w={$this->todayWeek}&m={$this->todayMonth}&y={$this->todayYear}'> Today </a>
  714.                     </td>
  715.                 </tr>
  716.             </table>
  717. HTML;
  718.     }
  719.    
  720.     /**
  721.     * calculate First Day Of Week
  722.     *
  723.     * @return   integer
  724.     */
  725.     private function _getFirstDayOfWeek()
  726.     {
  727.         return date('w', mktime( 0, 0, 0, $this->defaultMonth, 1, $this->defaultYear) );
  728.     }
  729.    
  730.     /**
  731.     * calculate Last Day Of Week
  732.     *
  733.     * @return   integer
  734.     */
  735.     private function _getLastDayOfWeek()
  736.     {
  737.         return date('w', mktime( 0, 0, 0, $this->defaultMonth, date('t', mktime( 0, 0, 0, $this->defaultMonth, 1, $this->defaultYear) ), $this->defaultYear) );
  738.     }
  739.    
  740.     /**
  741.     * get Calendar Month
  742.     *
  743.     * @param    integer $m (1-2)
  744.     * @param    integer $y (1970 or 2009)
  745.     *
  746.     * @return   output
  747.     */
  748.     public function calendarMonth( $m = '', $y = '', $inYearCalendar = false)
  749.     {
  750.         $list = '';
  751.         if ( is_numeric($m) && $m > 0 && $m < 13) $this->defaultMonth = $m;
  752.         if ( is_numeric($y) && $y > 0 ) $this->defaultYear = $y;
  753.        
  754.         $this->totalDayInMonth = date('t', mktime( 0, 0, 0, $this->defaultMonth, 1, $this->defaultYear) );
  755.         $this->firstDayOfWeek = $this->_getFirstDayOfWeek();
  756.         $this->lastDayOfWeek = $this->_getLastDayOfWeek();
  757.  
  758.         $backMonth  = $this->defaultMonth - 1;
  759.         $backYear   = $this->defaultYear;
  760.  
  761.         if ( $backMonth < 1 )
  762.         {
  763.             $backMonth = 12;
  764.             $backYear = $this->defaultYear - 1;
  765.         }
  766.  
  767.         $nextMonth  = $this->defaultMonth + 1;
  768.         $nextYear   = $this->defaultYear;
  769.  
  770.         if ( $nextMonth > 12 )
  771.         {
  772.             $nextMonth = 1;
  773.             $nextYear = $this->defaultYear + 1;
  774.         }
  775.  
  776.         if ( $this->firstDayOfWeek != 1)
  777.         {
  778.             $totalDayInBackMonth = date('t', mktime( 0, 0, 0, $backMonth, 1, $backYear) );
  779.             $this->firstDayOfWeek= ($this->firstDayOfWeek == 0)? 7: $this->firstDayOfWeek;
  780.             $startDayInBackMonth = ( $totalDayInBackMonth - $this->firstDayOfWeek+2 );
  781.  
  782.             $list .= "<tr>\n";
  783.  
  784.             for ( $i = $startDayInBackMonth; $i <= $totalDayInBackMonth; $i++ )
  785.             {
  786.                 $y = ( $this->showBackDay ) ? $i : 0;
  787.                 $list .= $this->_blankFormat($y);
  788.             }
  789.         }
  790.  
  791.         for ( $i = 1; $i <= $this->totalDayInMonth; $i++ )
  792.         {
  793.             $dayOfWeek = $this->_dayOfWeek($i);
  794.  
  795.             if ( $dayOfWeek == 1 ) $list .= "<tr>\n";
  796.  
  797.             $list .= $this->_dayFormat($i);
  798.  
  799.             if ( $dayOfWeek == 0 ) $list .= "</tr>\n";
  800.         }
  801.  
  802.         if ( $this->lastDayOfWeek != 0 )
  803.         {
  804.             $total = 7 - $this->lastDayOfWeek;
  805.  
  806.             for ( $i = 1; $i <= $total; $i++ )
  807.             {
  808.                 $z = ( $this->showNextDay ) ? $i : 0;
  809.                 $list .= $this->_blankFormat($z);
  810.             }
  811.             $list .= "</tr>\n";
  812.         }
  813.        
  814.         $monthTitleFormat = $this->monthTitleFormat();
  815.  
  816.         $colspan = ( ! $inYearCalendar ) ? 5 : 7;
  817.  
  818.         $backNavigation = ( ! $inYearCalendar ) ? "<td class='calendar-header'><a href='?m={$backMonth}&y={$backYear}' onclick='getCalendarMonth($backMonth,$backYear); return false;'> &lt;&lt; </a></td>" : '&nbsp;';
  819.  
  820.         $nextNavigation = ( ! $inYearCalendar ) ? "<td class='calendar-header'><a href='?m={$nextMonth}&y={$nextYear}' onclick='getCalendarMonth($nextMonth,$nextYear); return false;'> &gt;&gt; </a></td>" : '&nbsp;';
  821.        
  822.         $javaScript = $this->_showJavaScript();
  823.  
  824.         return <<<HTML
  825.             {$javaScript}
  826.             <table width="{$this->defaultTableWidth}" class="calendartable">
  827.                 <tr class="calendar-header">
  828.                     {$backNavigation}
  829.                     <td colspan='{$colspan}' class="calendar-header">
  830.                         {$monthTitleFormat}
  831.                     </td>
  832.                     {$nextNavigation}
  833.                 </tr>
  834.                 <tr class="calendar-title">
  835.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[0]}</th>
  836.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[1]}</th>
  837.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[2]}</th>
  838.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[3]}</th>
  839.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[4]}</th>
  840.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[5]}</th>
  841.                     <th width='14%' class="calendar-title">{$this->dayOfWeekTitle[6]}</th>
  842.                 </tr>
  843.                 {$list}
  844.             </table>\n
  845. HTML;
  846.     }
  847.  
  848.     /**
  849.     * get Calendar Year
  850.     *
  851.     * @param    integer $y (1970 or 2009)
  852.     *
  853.     * @return   output
  854.     */
  855.     public function calendarYear($y = '')
  856.     {
  857.         $list = '';
  858.         $i = 0;
  859.  
  860.         if ( is_numeric($y) && $y > 0 ) $this->defaultYear = $y * 1;
  861.        
  862.         $colWidth = ceil( 100 / $this->defaultPerRow );
  863.  
  864.         for ( $m = 1; $m <= 12; $m++ )
  865.         {
  866.             if ( $i == 0 ) $list .= "<tr>\n";
  867.             $list .= "<td valign='top' align='center' width='{$colWidth}%'  class='calendar-year'>" . $this->calendarMonth( $m, $y, true ) . "</td>\n";
  868.             $i++;
  869.             if ( $i == $this->defaultPerRow ){ $list .= "</tr>\n"; $i = 0; }
  870.         }
  871.  
  872.         if ( $i != 0 )
  873.         {
  874.             while ( $i != $this->defaultPerRow )
  875.             {
  876.                 $list .= "<td width='{$colWidth}%'>&nbsp;</td>\n";
  877.                 $i++;
  878.             }
  879.             $list .= "</tr>\n";
  880.         }
  881.  
  882.         $backYear = $this->defaultYear - 1;
  883.         $nextYear = $this->defaultYear + 1;
  884.         $yearTitle = $this->yearTitleFormat();
  885.         $javaScript = $this->_showJavaScript();
  886.  
  887.         return <<<HTML
  888.             {$javaScript}
  889.             <table width='100%' cellpadding='5' cellspacing='2'>
  890.                 <tr class="calendar-header">
  891.                     <td colspan='{$this->defaultPerRow}' class="calendar-header">
  892.                         <a href='calendar-{$backYear}.html' > &lt;&lt; </a>
  893.                         {$yearTitle}   
  894.                         <a href='calendar-{$nextYear}.html' > &gt;&gt; </a>
  895.                     </td>
  896.                 </tr>
  897.                 {$list}
  898.             </table>
  899. HTML;
  900.     }
  901.    
  902.     /**
  903.     * JavaScript to Show Event
  904.     *
  905.     * @param    integer $day (1-31)
  906.     *
  907.     * @return   string
  908.     */
  909.     protected function _showJavaScript()
  910.     {
  911.         return <<<HTML
  912. <script type="text/javascript">
  913.     function showEvent(id)
  914.     {
  915.         document.getElementById(id).style.display = "block";
  916.     }
  917.     function hideEvent(id)
  918.     {
  919.         document.getElementById(id).style.display = "none";
  920.     }
  921. </script>
  922. HTML;
  923.     }
  924.  
  925.     /**
  926.     * day Format
  927.     *
  928.     * @param    integer $day (1-31)
  929.     *
  930.     * @return   string
  931.     */
  932.     protected function _dayFormat($day)
  933.     {
  934.         $bg = '';
  935.        
  936.         $class = '';
  937.        
  938.         $dayFormat = '';
  939.        
  940.         $dayOfWeek  =   $this->_dayOfWeek($day);
  941.        
  942.         $tDMY       =   date('j') . '-' .  date('n') . '-' . date('Y');
  943.  
  944.         $DMY        =   $day . '-' .  $this->defaultMonth . '-' . $this->defaultYear;
  945.  
  946.         if ( isset($this->eventDay[$DMY]) )
  947.         {
  948.             $dayFormat = $this->eventDayFormat($day, $DMY);
  949.         }
  950.         else
  951.         {
  952.             $dayFormat = $this->normalDayFormat($day);
  953.         }
  954.  
  955.         if ( $tDMY == $DMY )
  956.         {
  957.             $class = "class='{$this->defaultTodayClass}'";
  958.         }
  959.         else
  960.         {
  961.             $class = "class='{$this->defaultClassCol[$dayOfWeek]}'";
  962.         }
  963.        
  964.         if ( $this->showColor && $tDMY != $DMY )
  965.         {
  966.             $bg = "style='background-color: {$this->colorArray[$dayOfWeek]};'";
  967.         }
  968.        
  969.         return "<td $class $bg>
  970.                     $dayFormat
  971.                 </td>\n";
  972.     }
  973.  
  974.     /**
  975.     * normal day format
  976.     *
  977.     * @param    integer $day (1-31)
  978.     *
  979.     * @return   integer
  980.     */
  981.     protected function normalDayFormat($day)
  982.     {
  983.         return "<div>$day</div>";
  984.     }
  985.  
  986.     /**
  987.     * normal day format
  988.     *
  989.     * @param    integer $day (1-31)
  990.     * @param    string $DMY ( date('j-n-Y') )
  991.     *
  992.     * @return   integer
  993.     */
  994.     protected function eventDayFormat($day,$DMY)
  995.     {
  996.         return "<div class='{$this->defaultEventDayClass}' onmouseover=\"showEvent('$DMY')\" onmouseout=\"hideEvent('$DMY')\" >$day
  997.                 </div>
  998.                 <div id='{$DMY}' class='popup'>{$this->eventDay[$DMY]}</div>";
  999.     }
  1000.  
  1001.     /**
  1002.     * calculate day of week
  1003.     *
  1004.     * @param    integer $day (1-31)
  1005.     *
  1006.     * @return   integer
  1007.     */
  1008.     private function _dayOfWeek($day)
  1009.     {
  1010.         return $weekDay = date('w', mktime( 0, 0, 0, $this->defaultMonth, $day, $this->defaultYear) );
  1011.     }
  1012.  
  1013.     /**
  1014.     * blank column
  1015.     *
  1016.     * @return   string
  1017.     */
  1018.     private function _blankFormat($day)
  1019.     {
  1020.         $day = ( $day == 0 ) ? '&nbsp;' : $day;
  1021.         //return "<td class='{$this->defaultBlankClass}'><div>b$day</div></td>\n";
  1022.         return "<td></td>";
  1023.     }
  1024.  
  1025.     /**
  1026.     * display page title
  1027.     *
  1028.     * @param    string  $mode
  1029.     *                       w :: display week title
  1030.     *                       m :: display month title
  1031.     *                       y :: display year title
  1032.     *
  1033.     * @return   output
  1034.     */
  1035.     public function getPageTitle( $mode = '')
  1036.     {
  1037.         switch ( $mode )
  1038.         {
  1039.             case 'w':
  1040.                 return $this->weekTitleFormat();
  1041.             break;
  1042.  
  1043.             case 'm':
  1044.                 return $this->monthTitleFormat();
  1045.             break;
  1046.  
  1047.             case 'y':
  1048.                 return $this->yearTitleFormat();
  1049.             break;
  1050.  
  1051.             default:
  1052.                 return $this->yearTitleFormat();
  1053.             break;
  1054.         }
  1055.     }
  1056.  
  1057.     /**
  1058.     * title of format in calendar week
  1059.     *
  1060.     * @return   string
  1061.     */
  1062.     public function weekTitleFormat()
  1063.     {
  1064.         $title = '';
  1065.         $monthTitle = $this->monthTitle[$this->defaultMonth-1];
  1066.         $year = ( $this->thaiYear == true ) ? ($this->defaultYear + 543) : $this->defaultYear;
  1067.  
  1068.         $title = str_replace('{week}', $this->defaultWeek, $this->defaultWeekTitleFormat);
  1069.         $title = str_replace('{month}', $monthTitle, $title);
  1070.         $title = str_replace('{year}', $year, $title);
  1071.         return $title;
  1072.     }
  1073.  
  1074.     /**
  1075.     * title of format in calendar month
  1076.     *
  1077.     * @return   string
  1078.     */
  1079.     public function monthTitleFormat()
  1080.     {
  1081.         $title = '';
  1082.         $monthTitle = $this->monthTitle[$this->defaultMonth-1];
  1083.         $year = ( $this->thaiYear == true ) ? ($this->defaultYear + 543) : $this->defaultYear;
  1084.  
  1085.         $title = str_replace('{month}', $monthTitle, $this->defaultMonthTitleFormat);
  1086.         $title = str_replace('{year}', $year, $title);
  1087.         return $title;
  1088.     }
  1089.  
  1090.     /**
  1091.     * title of format in calendar year
  1092.     *
  1093.     * @return   string
  1094.     */
  1095.     public function yearTitleFormat()
  1096.     {
  1097.         $title = '';
  1098.         $year = ( $this->thaiYear == true ) ? ($this->defaultYear + 543) : $this->defaultYear;
  1099.         $title = str_replace('{year}', $year, $this->defaultYearTitleFormat);
  1100.         return $title;
  1101.     }
  1102.    
  1103.     public function displaySimpleCalendar () {
  1104.         $this->setEventDay($this->getEvent());
  1105.         $content .= '<div align="center" class="calendarWeek">';
  1106.         $content .= $this->calendarYear();
  1107.         $content .= '</div>';
  1108.         return $content;
  1109.        
  1110.     }
  1111.    
  1112.     private function getEvent () {
  1113.         $q = db::getInstance()->q( 'SELECT * FROM sm_calendar WHERE fk_web_id = "'. $this->web_id .'" ');      
  1114.         while ($array = $q->fetch_array()) {
  1115.             list($ys, $ms, $ds) = explode('-', $array['start']);
  1116.             $start = date("d/m/Y", mktime(0, 0, 0, $ms, $ds, $ys));
  1117.             list($y, $m, $d) = explode('-', $array['end']);
  1118.             $end = date("d/m/Y", mktime(0, 0, 0, $m, $d+1, $y));
  1119.             $progress = $start;
  1120.             while ($end != $progress ) {
  1121.                 $calendarDate   =   ($ds * 1) . '-' . ($ms * 1) . '-' . ($ys * 1); 
  1122.                 $eventDay[$calendarDate] = '<div class="title" >' . stripslashes($array['title']) . '</div><div class="information" >' . stripslashes($array['remark']) . '</div>';
  1123.                 $ds++;
  1124.                 $progress = date("d/m/Y", mktime(0, 0, 0, $ms, $ds, $ys));
  1125.                 list($ds, $ms, $ys) = explode('/', $progress);
  1126.             }  
  1127.         }
  1128.         //var_dump($eventDay);
  1129.         return $eventDay;
  1130.     }
  1131.    
  1132.     public function displayAddEvent() {
  1133.  
  1134.         $content =  '<div class="calendarAddEvent">';
  1135.         if ( isset($_POST['submit']) ) {
  1136.             $error = $this->checkError();
  1137.             if ( $error ) {
  1138.                 $report = 'Un problème est survenu<br />'.$error['other'];
  1139.                
  1140.             } else {
  1141.                 $this->use_id = user::get_session()->id;
  1142.                 if ($this->save() ) {
  1143.                     $report = 'Votre réservation a bien été enregistrée';
  1144.                     $_POST = array();
  1145.                 }
  1146.             }
  1147.            
  1148.             $content .= '<h1 align="center" class="report">'.$report.'</h1>';
  1149.         }
  1150.         $content .= '<h1 id="popup_open">Cliquez ici pour ajouter un événement</h1>';
  1151.         $content .= '
  1152.                 <div  id="popup" style="display:none;" >
  1153.                 <div   style="text-align:left;">
  1154.                 <form id="add-event" action="calendar.html" method="post">
  1155.                 <label>Titre : </label><input type="text" name="title" value="'. $_POST['title'] .'" />
  1156.                 <span class="error">'. $error['title'] .'</span><br/>
  1157.                 <label>Début  : </label><input type="text" class="datepicker" name="start" value="'. $_POST['start'] .'" />
  1158.                 <span class="error">'.  $error['start'] .'</span><br/>
  1159.                 <label>Fin :</label><input type="text" class="datepicker" name="end" value="'. $_POST['end'] .'" />
  1160.                 <span class="error">'. $error['end'] .'</span><br/>
  1161.                 <label>Commentaire :</label><textarea name="remark" >'. $_POST['remark'] .'</textarea>
  1162.                 <span class="error">'. $error['remark'] .'</span><br/>
  1163.                 <label> </label><input type="submit" name="submit" value="Valider" />
  1164.                 <form>
  1165.                 </div>
  1166.                 </div>
  1167.              </div>';
  1168.         return $content;
  1169.     }
  1170.    
  1171.     public function checkError() {
  1172.         $error = array();
  1173.         $error['status'] = 0;
  1174.         $array = array( 'fk_web_id'  => $this->web_id,
  1175.                         'title'  => $_POST['title'],
  1176.                         'start'  => $_POST['start'],
  1177.                         'end'    => $_POST['end'],
  1178.                         'remark' => $_POST['remark'] );
  1179.  
  1180.         foreach ( $array AS $key => $val) {
  1181.             if (trim($val) == '' ) {
  1182.                 $error['status'] = 1;
  1183.                 $error[$key] = 'Ne laisser pas ce champs vide.';
  1184.             }
  1185.         }
  1186.  
  1187.         if ( $_POST['start'] ) {
  1188.             if (!preg_match('(^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$)', $_POST['start']) ) {
  1189.                 $error['status'] = 1;$error['start'] = 'format de la date incorrect';
  1190.             }  
  1191.         }
  1192.         if ( $_POST['end'] ) {
  1193.             if (!preg_match('(^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$)', $_POST['end']) ) {
  1194.                 $error['status'] = 1;$error['end'] = 'format de la date incorrect';
  1195.             }  
  1196.         }
  1197.         if ( $_POST['start'] and $_POST['end'] ) {
  1198.             list($d, $m, $y) = explode('/', $array['start']);
  1199.             $start = date("Y-m-d", mktime(0, 0, 0, $m, $d, $y));
  1200.             $start2 = new DateTime( $start );
  1201.             list($d, $m, $y) = explode('/', $array['end']);
  1202.             $end = date("Y-m-d", mktime(0, 0, 0, $m, $d, $y));
  1203.             $end2 = new DateTime( $end );
  1204.             $q = db::getInstance()->q( 'SELECT * FROM sm_calendar
  1205.                                         WHERE fk_web_id = "'. $this->web_id .'"
  1206.                                         AND  DATEDIFF(end,"'. $start .'") >= 0
  1207.                                             AND DATEDIFF(start,"'. $end .'") <= 0
  1208.                                         ');
  1209.              
  1210.             if ($q->num_rows) {
  1211.                 echo $q->num_rows;
  1212.                 $error['status'] = 1;
  1213.                 $error['other'] = 'Déjà reservé';
  1214.             }
  1215.             if ($start2->format('Ymd') > $end2->format('Ymd')) {
  1216.                 $error['other'] = 'La date de début ultérieur à la date de fin';
  1217.                 $error['status'] = 1;
  1218.             }
  1219.         }
  1220.         if ( $error['status'] == 1 ) {
  1221.             return $error;
  1222.         }  
  1223.         else{  
  1224.             $array = array( 'fk_web_id'  => $this->web_id,
  1225.                             'title'  => $_POST['title'],
  1226.                             'start'  => $start,
  1227.                             'end'    => $end,
  1228.                             'remark' => $_POST['remark'] );
  1229.             $this->set_fields($array);
  1230.             return false;
  1231.         }
  1232.     }
  1233.  
  1234.  
  1235.   public function displayAll() {
  1236.         $q = db::getInstance()->q( 'SELECT * FROM sm_calendar WHERE fk_web_id = "'. $this->web_id .'" ');    
  1237.  
  1238.        
  1239.         while ($array = $q->fetch_array()) {
  1240.       list($ys, $ms, $ds) = explode('-', $array['start']);
  1241.       $start = date("d/m/Y", mktime(0, 0, 0, $ms, $ds, $ys));
  1242.       list($y, $m, $d) = explode('-', $array['end']);
  1243.       $end = date("d/m/Y", mktime(0, 0, 0, $m, $d+1, $y));
  1244.       $progress = $start;
  1245.       while ($end != $progress ) {
  1246.         $calendarDate = ($ds * 1) . '-' . ($ms * 1) . '-' . ($ys * 1);  
  1247.         $eventDay[$calendarDate] = '<div class="title" >' . stripslashes($array['title']) . '</div><div class="information" >' . stripslashes($array['remark']) . '</div>';
  1248.         $ds++;
  1249.         $progress = date("d/m/Y", mktime(0, 0, 0, $ms, $ds, $ys));
  1250.         list($ds, $ms, $ys) = explode('/', $progress);
  1251.       }
  1252.     }
  1253.     return $eventDay;
  1254.   }
  1255. }
  1256. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement