Changeset 1794

Show
Ignore:
Timestamp:
01/23/2008 01:57:59 AM (12 months ago)
Author:
gregmac
Message:

Add support for combining multiple css files into one; Fix #311

Location:
trunk/modules/media
Files:
2 added
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/media/controllers/media.php

    r1766 r1794  
    5454                } 
    5555        } 
    56  
    57         public function css() 
    58         { 
    59                 $filename = $orig_filename = $this->uri->segment(3); 
    60                 if (substr($filename, -4) == '.css') 
    61                 { 
    62                         $filename = substr($filename, 0, -4); 
    63                 } 
     56         
     57        public function css($querystr) { 
     58                // find all the individual files  
     59                $files = explode("+", $querystr); 
    6460 
    6561                $mimetype = config::item('mimes.css'); 
    6662                $mimetype = (isset($mimetype[0])) ? $mimetype[0] : 'text/stylesheet'; 
    6763 
    68                 $this->use_cache AND $data = $this->cache->get('media.css.'.$filename); 
     64                $this->use_cache AND $data = $this->cache->get('media.css.'.$querystr); 
    6965 
    7066                if ( ! isset($data) OR empty($data)) 
    7167                { 
     68                        $data = ''; 
     69                        foreach ($files as $orig_filename) { 
     70                                $filename = $orig_filename; 
     71                                if (substr($filename, -4) == ".css")  
     72                                { 
     73                                        $filename = substr($filename, 0, -4); 
     74                                } 
     75                                 
     76                                try 
     77                                { 
     78                                        $view = new View('media/css/'.$filename, null, 'css'); 
     79                                } 
     80                                catch (Kohana_Exception $exception) 
     81                                { 
     82                                        // try to load the file as a php view (eg, file.css.php)  
     83                                        try 
     84                                        { 
     85                                                $view = new View('media/css/'.$orig_filename); 
     86                                                 
     87                                        } 
     88                                        catch (Kohana_Exception $exception) 
     89                                        { 
     90                                                // not found 
     91                                                unset($view); 
     92                                        } 
     93                                } 
     94 
     95                                if (isset($view)) { 
     96                                        $filedata = $view->render(); 
     97                                         
     98                                        ($this->pack_css) and $filedata = $this->_css_compress($filedata); 
     99                                         
     100                                        $data .= $filedata; 
     101                                } 
     102                                else 
     103                                {        
     104                                        $data .= "\n/**** stylesheet ".$filename." not found ****/\n\n\n"; 
     105                                } 
     106                        } 
     107                         
     108                        ($this->use_cache) and $this->cache->set('media.css.'.$querystr, $data, array('media'), $this->cache_lifetime); 
     109                } 
     110 
     111                $mimetype AND header('Content-type: '.$mimetype); 
     112                echo $data; 
     113        } 
     114 
     115        public function js($orig_filename) { 
     116                $filename = $orig_filename; 
     117                if (substr($filename, -3) == '.js') 
     118                { 
     119                        $filename = substr($filename, 0, -3); 
     120                } 
     121 
     122                $mimetype = Config::item('mimes.js'); 
     123                $mimetype = (isset($mimetype[0])) ? $mimetype[0] : 'text/javascript'; 
     124 
     125 
     126                $this->use_cache AND $data = $this->cache->get('media.js.'.$filename); 
     127 
     128                if ( ! isset($data) OR empty($data))  
     129                { 
    72130                        try 
    73131                        { 
    74                                 $view = new View('media/css/'.$filename, NULL, 'css'); 
     132                                $view = new View('media/js/'.$filename, NULL, 'js'); 
    75133                        } 
    76134                        catch (Kohana_Exception $exception) 
    77135                        { 
    78                                 // Try to load the file as a php view (e.g. file.css.php)  
     136                                // Try to load the file as a php view (eg, file.js.php)  
    79137                                try 
    80138                                { 
    81                                         $view = new View('media/css/'.$orig_filename); 
     139                                        $view = new View('media/js/'.$orig_filename); 
    82140                                } 
    83141                                catch (Kohana_Exception $exception) 
     
    88146                        } 
    89147 
    90                         if (isset($view)) 
    91                         { 
    92                                 $data = $view->render(); 
    93  
    94                                 if ($this->pack_css)  
    95                                 { 
    96                                         $data = $this->_css_compress($data); 
    97                                 } 
    98  
    99                                 if ($this->use_cache)  
    100                                 { 
    101                                         $this->cache->set('media.css.'.$filename, $data, array('media'), $this->cache_lifetime); 
    102                                 } 
    103                         } 
    104                         else 
    105                         {        
    106                                 $data = '/* stylesheet not found */'; 
    107                         } 
    108                 } 
    109  
    110                 $mimetype AND header('Content-type: '.$mimetype); 
    111                 echo $data; 
    112         } 
    113  
    114         public function js() 
    115         { 
    116                 $filename = $orig_filename = $this->uri->segment(3); 
    117                 if (substr($filename, -3) == '.js') 
    118                 { 
    119                         $filename = substr($filename, 0, -3); 
    120                 } 
    121  
    122                 $mimetype = Config::item('mimes.js'); 
    123                 $mimetype = (isset($mimetype[0])) ? $mimetype[0] : 'text/javascript'; 
    124  
    125  
    126                 $this->use_cache AND $data = $this->cache->get('media.js.'.$filename); 
    127  
    128                 if ( ! isset($data) OR empty($data))  
    129                 { 
    130                         try 
    131                         { 
    132                                 $view = new View('media/js/'.$filename, NULL, 'js'); 
    133                         } 
    134                         catch (Kohana_Exception $exception) 
    135                         { 
    136                                 // Try to load the file as a php view (eg, file.js.php)  
    137                                 try 
    138                                 { 
    139                                         $view = new View('media/js/'.$orig_filename); 
    140                                 } 
    141                                 catch (Kohana_Exception $exception) 
    142                                 { 
    143                                         // Not found 
    144                                         unset($view); 
    145                                 } 
    146                         } 
    147  
    148148                        if (isset($view))  
    149149                        { 
     
    156156                                } 
    157157 
    158                                 if ($this->use_cache)  
    159                                 { 
    160                                         $this->cache->set('media.js.'.$filename, $data, array('media'), $this->cache_lifetime); 
    161                                 } 
     158                                ($this->use_cache) and $this->cache->set('media.js.'.$filename, $data, array('media'), $this->cache_lifetime); 
    162159                        }  
    163160                        else  
     
    177174                // TODO: finish this for generic types 
    178175                /* issues: getting View to work with any types of files */ 
     176                 
     177                try 
     178                { 
     179                        $view = new View('media/'.$type.'/'.$filename); 
     180                } 
     181                catch (Kohana_Exception $exception) 
     182                { 
     183                        Event::run('system.404'); 
     184                } 
    179185        } 
    180186