Advertisement
Guest User

Untitled

a guest
Apr 24th, 2013
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.92 KB | None | 0 0
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Calendar_week {
  4.  
  5.     var $CI;
  6.     var $lang;
  7.     //var $local_time;
  8.     var $template       = '';
  9.     var $start_day      = 'sunday';
  10.     var $month_type     = 'long';
  11.     var $day_type       = 'abr';
  12.     var $week_days = Array();
  13.     var $date = '';
  14.     var $url        = '';
  15.  
  16.     /**
  17.      * Constructor
  18.      *
  19.      * Loads the calendar language file and sets the default time reference
  20.      *
  21.      * @access  public
  22.      */
  23.     function Calendar_week($config = array()){
  24.         $this->CI =& get_instance();
  25.        
  26.         if ( ! in_array('calendar_lang'.EXT, $this->CI->lang->is_loaded, TRUE)) {
  27.             $this->CI->lang->load('calendar');
  28.         }
  29.        
  30.         if (count($config) > 0) {
  31.             $this->initialize($config);
  32.         }
  33.        
  34.         if ($this->date==null){
  35.             $this->date = date(mktime());
  36.         }
  37.        
  38.         $this->set_week();
  39.        
  40.         log_message('debug', "Calendar_week Class Initialized");
  41.     }
  42.    
  43.     /**
  44.      * Initialize the user preferences
  45.      *
  46.      * Accepts an associative array as input, containing display preferences
  47.      *
  48.      * @access  public
  49.      * @param   array   config preferences
  50.      * @return  void
  51.      */
  52.     function initialize($config = array()) {
  53.         foreach ($config as $key => $val) {
  54.             if (isset($this->$key)) {
  55.                 $this->$key = $val;
  56.             }
  57.         }
  58.     }
  59.    
  60.     function set_range(){
  61.         switch ($this->start_day){
  62.             case 'sunday':
  63.                 return range(0,6);
  64.                 break;
  65.  
  66.             case 'monday':
  67.                 return range(1,7);
  68.                 break;
  69.             case 'tuesday':
  70.                 return range(2,8);
  71.                 break;
  72.  
  73.             case 'wednesday':
  74.                 return range(3,9);
  75.                 break;
  76.  
  77.             case 'thursday':
  78.                 return range(4,10);
  79.                 break;
  80.  
  81.             case 'friday':
  82.                 return range(5,11);
  83.                 break;
  84.  
  85.             case 'saturday':
  86.                 return range(6,12);
  87.                 break;             
  88.         }
  89.         return range(0,6);
  90.     }
  91.    
  92.     function set_week() {
  93.         $week_days = $this->set_range();
  94.         $week_day = date('w',$this->date);
  95.    
  96.        foreach($week_days as $key=>$day) {
  97.           if($day == $week_day) {
  98.               $week_days[$key] = $this->date;
  99.           } elseif($day < $week_day) {
  100.              $week_days[$key] = strtotime('-'.$week_day+$day.' day',$this->date);
  101.           } elseif($day > $week_day) {
  102.              $week_days[$key] = strtotime('+'.$day-$week_day.' day',$this->date);
  103.           }
  104.        }
  105.        
  106.        $this->week_days = $week_days;
  107.     }
  108.    
  109.     function get_week(){
  110.         return $this->week_days ;
  111.     }
  112.    
  113.     // --------------------------------------------------------------------
  114.  
  115.     /**
  116.      * Generate the calendar
  117.      *
  118.      * @access  public
  119.      * @return  string
  120.      */
  121.     function generate($data=''){
  122.         $days = $this->get_day_names();
  123.         $months = $this->get_month_name();
  124.        
  125.         $tmpHeader = '';
  126.         $tmpContent = '';
  127.  
  128.         for ($i=0;$i<count($this->week_days);$i++){
  129.             if ( (date('l', $this->week_days[$i])=='Saturday') || (date('l', $this->week_days[$i])=='Sunday') ){
  130.                 $tmpHeader .= '<td>'.$days[date('w', $this->week_days[$i])].' '.date('d', $this->week_days[$i]).' ' . $months[date('n', $this->week_days[$i])-1] .'</td>';
  131.                 $tmpContent .= '<td><div class="container" style="background: #ccc;"></div></td>';             
  132.             } else {
  133.                 $tmpHeader .= '<td>'.$days[date('w', $this->week_days[$i])].' '.date('d', $this->week_days[$i]).' ' . $months[date('n', $this->week_days[$i])-1] .'</td>';
  134.                 $tmpContent .= '<td><div class="container">'.$data[$this->week_days[$i]].'</div></td>';
  135.             }
  136.         }
  137.        
  138.         $before = strtotime('-4 day',$this->week_days[0]);
  139.         $after = strtotime('+1 day',$this->week_days[count($this->week_days)-1]);
  140.        
  141.         $template = '
  142.                     <span id="displayCalendarBefore">
  143.                         <a href="' . site_url($this->url . date('Y',$before).'/'.date('m',$before).'/'.date('d',$before)) . '">before</a>
  144.                     </span>
  145.                     <span id="calendar">
  146.                         <table>
  147.                             <tr class="calendarDay">'.$tmpHeader.'</tr>
  148.                             <tr class="calendarDetail">'.$tmpContent.'</tr>
  149.                         </table>
  150.                     </span>
  151.                     <span id="displayCalendarAfter">
  152.                         <a href="' . site_url($this->url . date('Y',$after).'/'.date('m',$after).'/'.date('d',$after)) . '">after</a>
  153.                     </span>';
  154.         return $template ;
  155.     }  
  156.    
  157.     /**
  158.      * Get Month Name
  159.      *
  160.      * Generates a textual month name based on the numeric
  161.      * month provided.
  162.      *
  163.      * @access  public
  164.      * @param   integer the month
  165.      * @return  string
  166.      */
  167.     function get_month_name() {
  168.         if ($this->month_type == 'short') {
  169.             $month_names = array('01' => 'cal_jan', '02' => 'cal_feb', '03' => 'cal_mar', '04' => 'cal_apr', '05' => 'cal_may', '06' => 'cal_jun', '07' => 'cal_jul', '08' => 'cal_aug', '09' => 'cal_sep', '10' => 'cal_oct', '11' => 'cal_nov', '12' => 'cal_dec');
  170.         } else {
  171.             $month_names = array('01' => 'cal_january', '02' => 'cal_february', '03' => 'cal_march', '04' => 'cal_april', '05' => 'cal_may', '06' => 'cal_june', '07' => 'cal_july', '08' => 'cal_august', '09' => 'cal_september', '10' => 'cal_october', '11' => 'cal_november', '12' => 'cal_december');
  172.         }
  173.        
  174.         $months = array();
  175.         foreach ($month_names as $val) {           
  176.             $months[] = ($this->CI->lang->line($val) === FALSE) ? ucfirst($val) : $this->CI->lang->line($val);
  177.         }
  178.    
  179.         return $months;
  180.     }  
  181.    
  182.     /**
  183.      * Get Day Names
  184.      *
  185.      * Returns an array of day names (Sunday, Monday, etc.) based
  186.      * on the type.  Options: long, short, abrev
  187.      *
  188.      * @access  public
  189.      * @param   string
  190.      * @return  array
  191.      */
  192.     function get_day_names($day_type = '')
  193.     {
  194.         if ($day_type != '')
  195.             $this->day_type = $day_type;
  196.    
  197.         if ($this->day_type == 'long') {
  198.             $day_names = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
  199.         } elseif ($this->day_type == 'short') {
  200.             $day_names = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
  201.         } else {
  202.             $day_names = array('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa');
  203.         }
  204.    
  205.         $days = array();
  206.         foreach ($day_names as $val) {         
  207.             $days[] = ($this->CI->lang->line('cal_'.$val) === FALSE) ? ucfirst($val) : $this->CI->lang->line('cal_'.$val);
  208.         }
  209.         return $days;
  210.     }      
  211. }
  212.  
  213. // END Calendar_week class
  214.  
  215. /* End of file Calendar_week.php */
  216. /* Location: ./system/application/libraries/Calendar_week.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement