Skip to content

Commit

Permalink
Allow frontend and backend have different themes. Needs more testing.…
Browse files Browse the repository at this point in the history
… CSS convention: url('../images/logo.png')and not url('media/images/logo.png'). Admin path detection is based on urls, so we must follow this convention to work. Same applies for javascript as well.
  • Loading branch information
sandeepone committed Mar 30, 2013
1 parent cb13897 commit e03a77a
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 86 deletions.
161 changes: 86 additions & 75 deletions modules/gleez/classes/controller/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,92 @@
/**
* Media Controller
*
* @package Gleez
* @category Controller
* @author Sandeep Sangamreddi - Gleez
* @copyright (c) 2012 Gleez Technologies
* @license http://gleezcms.org/license
* @package Gleez\Media\Controller
* @author Sandeep Sangamreddi - Gleez
* @copyright (c) 2011-2013 Gleez Technologies
* @license http://gleezcms.org/license Gleez CMS License
*/
class Controller_Media extends Controller {

/** @var Kohana_Config The configuration settings */
public $config;

/**
* The before() method is called before controller action.
*/
public function before()
{
parent::before();

// Load config
$this->config = Kohana::$config->load('media');
}

/**
* Static file serving (CSS, JS, images)
*/
public function action_serve()
{
// Get the file path from the request
$file = $this->request->param('file');

// Find the file extension
$ext = pathinfo($file, PATHINFO_EXTENSION);

// Remove the extension from the filename
$file = substr($file, 0, -(strlen($ext) + 1));

if ($file_name = Kohana::find_file($this->config->get('public_dir', 'media'), $file, $ext))
{
// Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed
$this->response->check_cache(sha1($this->request->uri()).filemtime($file_name), $this->request);

// Send the file content as the response
$this->response->body(file_get_contents($file_name));

// Set the proper headers to allow caching
$this->response->headers('content-type', File::mime_by_ext($ext));
$this->response->headers('last-modified', date('r', filemtime($file_name)));
//this is ignored by check_cache
$this->response->headers('cache-control', 'public, max-age=2592000');

if ($this->config->get('cache', FALSE))
{
// Save the contents to the public directory for future requests
$public_path = $this->config->get('public_dir', 'media').DIRECTORY_SEPARATOR.$file.'.'.$ext;
$directory = dirname($public_path);

if (! is_dir($directory))
{
// Recursively create the directories needed for the file
System::mkdir($directory, 0777, TRUE);
}

file_put_contents($public_path, $this->response->body());
}
}
else
{
Kohana::$log->add(LOG::ERROR, 'Media controller error while loading file: `:file`',
array(
':file' => $file
)
);
// Return a 404 status
$this->response->status(404);
}
}


/** @var Kohana_Config The configuration settings */
public $config;

/**
* The before() method is called before controller action.
*/
public function before()
{
parent::before();

// Load config
$this->config = Kohana::$config->load('media');

if ($this->request->param('type', FALSE))
{
Theme::set_admin_theme();
}
}

/**
* Static file serving (CSS, JS, images)
*/
public function action_serve()
{
// Get the file path from the request
$file = $this->request->param('file');

// Find the file extension
$ext = pathinfo($file, PATHINFO_EXTENSION);

// Remove the extension from the filename
$file = substr($file, 0, -(strlen($ext) + 1));

if ($file_name = Kohana::find_file('media', $file, $ext))
{
// Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed
$this->response->check_cache(sha1($this->request->uri()) . filemtime($file_name), $this->request);

// Send the file content as the response
$this->response->body(file_get_contents($file_name));

// Set the proper headers to allow caching
$this->response->headers('content-type', File::mime_by_ext($ext));
$this->response->headers('last-modified', date('r', filemtime($file_name)));
//this is ignored by check_cache
$this->response->headers('cache-control', 'public, max-age=2592000');

if ($this->config->get('cache', FALSE))
{
//set base path
$path = $this->config->get('public_dir', 'media');

//override path if we're in admin
if ($this->request->param('type', FALSE))
{
$path = $path.DIRECTORY_SEPARATOR.'admin';
}

// Save the contents to the public directory for future requests
$public_path = $path.DIRECTORY_SEPARATOR. $file . '.' . $ext;
$directory = dirname($public_path);

if (!is_dir($directory))
{
// Recursively create the directories needed for the file
System::mkdir($directory, 0777, TRUE);
}

file_put_contents($public_path, $this->response->body());
}
}
else
{
Kohana::$log->add(LOG::ERROR, 'Media controller error while loading file: `:file`', array(
':file' => $file
));
// Return a 404 status
$this->response->status(404);
}
}

}
46 changes: 36 additions & 10 deletions modules/gleez/classes/gleez/theme.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php defined("SYSPATH") or die("No direct script access.");

/**
* Theme helper for adding content to views
*
Expand Down Expand Up @@ -38,29 +37,32 @@ class Gleez_Theme {
public static function load_themes()
{
$config = Kohana::$config->load('site');
$modules = Kohana::modules();

//set admin theme based on path info
$path = Request::detect_uri();
Theme::$is_admin = ( $path == "/admin" || !strncmp($path, "/admin/", 7) );

if (self::$is_admin)
{
// Load the admin theme
self::$admin_theme_name = $config->get('admin_theme', self::$admin_theme_name);
$array = array(
self::$admin_theme_name => THEMEPATH . self::$admin_theme_name
);
$theme = THEMEPATH . self::$admin_theme_name;
}
else
{
// Load the site theme
self::$site_theme_name = $config->get('theme', self::$site_theme_name);
$array = array(
self::$site_theme_name => THEMEPATH . self::$site_theme_name
);
$theme = THEMEPATH . self::$site_theme_name;
}

// Merging array of modules
Kohana::modules(Arr::merge($array, Kohana::modules()));
// set modules with active theme
array_unshift($modules, $theme);

Kohana::modules($modules);

// Clean up
unset($array);
unset($modules, $theme);
}

/**
Expand Down Expand Up @@ -116,4 +118,28 @@ public static function available()
return $themes;
}

public static function set_admin_theme()
{
$config = Kohana::$config->load('site');
$modules = Kohana::modules();

// Load the theme info from config
self::$admin_theme_name = $config->get('admin_theme', self::$admin_theme_name);
self::$site_theme_name = $config->get('theme', self::$site_theme_name);

//unset base site theme while in admin
if (($key = array_search(THEMEPATH . self::$site_theme_name.DIRECTORY_SEPARATOR, $modules)) !== false)
{
unset($modules[$key]);
$modules = array_values($modules); // reindex
}

// set modules with active theme
array_unshift($modules, THEMEPATH . self::$admin_theme_name);

Kohana::modules($modules);

// Clean up
unset($modules);
}
}
33 changes: 33 additions & 0 deletions modules/gleez/classes/gleez/url.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@ public static function is_remote($url)
OR (strpos(strtolower($url), 'ftp://') !== FALSE);
}

/**
* Fetches an absolute site URL based on a URI segment.
* Added admin Theme support
*
* echo URL::site('foo/bar');
*
* @param string $uri Site URI to convert
* @param mixed $protocol Protocol string or [Request] class to use protocol from
* @param boolean $index Include the index_page in the URL
* @return string
* @uses URL::base
*/
public static function site($uri = '', $protocol = NULL, $index = TRUE)
{
// Chop off possible scheme, host, port, user and pass parts
$path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));

if ( ! UTF8::is_ascii($path))
{
// Encode all non-ASCII characters, as per RFC 1738
$path = preg_replace_callback('~([^/]+)~', 'URL::_rawurlencode_callback', $path);
}

//allow admin theme to serve its own media assets
if(Theme::$is_admin)
{
$path = str_replace(array('media', 'media/admin'), 'media/admin', $path);
}

// Concat the URL
return URL::base($protocol, $index).$path;
}

/**
* Test whether a URL is the home page
*
Expand Down
2 changes: 1 addition & 1 deletion modules/gleez/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
));

// Static file serving (CSS, JS, images)
Route::set('media', 'media/<file>', array('file' => '.+') )
Route::set('media', 'media(/<type>)/<file>', array('file' => '.+', 'type' => 'admin') )
->defaults(array(
'controller' => 'media',
'action' => 'serve',
Expand Down

0 comments on commit e03a77a

Please sign in to comment.