Browse | Submit New Snippet | Create Package

 

ThemeView

Type:
Class
Category:
Other
License:
Open Project Public License (OPPL)
Language:
PHP
 
Description:
This is a custom theme view class that will allow you to theme your cake applications.

If you use it and make changes you must submit those changes back for everyone to use.

Versions Of This Snippet::

Larry E. Masters
Snippet ID Download Version Date Posted Author Delete
1411.0.2.142006-06-19 19:32Larry E. Masters
Changes since last version::
Updated to latest version of CakePHP
Plugins can also use themes.
1001.0.0.12006-05-17 10:55Nimrod Abing
Changes since last version::
Fallback to using default layouts and pages if they cannot be found in the current theme yet.
281.0.0.02006-02-03 20:31Larry E. Masters

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: :1.0.2.14

<?php
/* SVN FILE: $Id: theme.php 14 2006-06-20 00:30:16Z phpnut $ */
/**
 * A custom view class that is used for themeing
 *
 *
 * CakeBlog :  Multi-user Weblog System <http://cakeforge.org/projects/cakeblog//>
 * Copyright (c) 2006, Larry E. Masters. Shorewood, IL. 60431
 *
 * Author(s): Larry E. Masters aka PhpNut <phpnut@gmail.com>
 *
 * Licensed under the Open Project Public License(OPPL) version 1.0
 * Redistributions of files must retain the above copyright notice.
 * You may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at:
 * License page: http://www.nextco.com/licenses/oppl/
 * Copyright page: http://www.nextco.com/copyright/
 *
 * Licensees holding a valid NextCo Commercial License may
 * use this file in accordance with the License Agreement.
 * See http://www.nextco.com/licenses/ for licensing information.
 *
 * @filesource
 * @author       Larry E. Masters aka PhpNut <phpnut@gmail.com>
 * @copyright    Copyright (c) 2006, Larry E. Masters. Shorewood, IL. 60431
 * @link         http://www.larrymasters.com
 * @package      cakeblog
 * @subpackage   cakeblog.views
 * @since        CakeBlog v 1.0.0.0
 * @version      $Revision: 14 $
 * @modifiedby   $LastChangedBy: phpnut $
 * @lastmodified $Date: 2006-06-19 19:30:16 -0500 (Mon, 19 Jun 2006) $
 * @license      http://www.nextco.com/licenses/oppl/
 */

/**
 * Short description for file.
 *
 * Long description for file
 *
 * @package    cakeblog
 * @subpackage cakeblog.views
 * @since      CakeBlog v 1.0.0.0
 */
class ThemeView extends View {
/**
 * Enter description here...
 *
 * @var unknown_type
 */
	var $themeElement;
/**
 * Enter description here...
 *
 * @var unknown_type
 */
   var $themeLayout;
/**
 * Enter description here...
 *
 * @var unknown_type
 */
   var $themePath;
/**
 * Enter description here...
 *
 * @param unknown_type $controller
 */
	function __construct (&$controller) {
    	parent::__construct($controller);
    	$this->theme =& $controller->theme;
    	$this->themeWeb = $this->theme.'/';
    	$this->themeElement = VIEWS.$this->theme.DS.'elements'.DS;
    	$this->themeLayout =  VIEWS.$this->theme.DS.'layouts'.DS;
    	$this->themePath = VIEWS.$this->theme.DS;
	}
/**
 * Enter description here...
 *
 * @param unknown_type $code
 * @param unknown_type $name
 * @param unknown_type $message
 */
	function error($code, $name, $message) {
		$file = $this->themeLayout.'error'.$this->ext;
		if(!file_exists($file)) {
			$file = LAYOUTS.'error'.$this->ext;
		}
		header ("HTTP/1.0 {$code} {$name}");
		print ($this->_render($file, array('code' => $code,
														'name' => $name,
														'message' => $message)));
	}
/**
 * Enter description here...
 *
 * @param unknown_type $name
 * @param unknown_type $params
 * @return unknown
 */
	function renderElement($name, $params = array()) {
		$file = $this->themeElement.$name.$this->ext;
		if (!is_null($this->plugin)) {
			if (file_exists(APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'elements' . DS . $this->theme . DS . $name . $this->ext)) {
				$file = APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'elements' . DS . $this->theme . DS . $name . $this->ext;
				$params = array_merge_recursive($params, $this->loaded);
				return $this->_render($file, array_merge($this->_viewVars, $params), false);
			} elseif (file_exists(APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'elements' . DS . $name . $this->ext)) {
				$file = APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'elements' . DS . $name . $this->ext;
				$params = array_merge_recursive($params, $this->loaded);
				return $this->_render($file, array_merge($this->_viewVars, $params), false);
			}
		}

		if (!file_exists($file)) {
			$file = ELEMENTS.$name.$this->ext;
			if (!file_exists($file)) {
				return "(Error rendering {$name})";
			}
		}
		$params=array_merge_recursive($params, $this->loaded);
		return $this->_render($file, array_merge($this->_viewVars, $params), false);
	}
/**
 * Enter description here...
 *
 * @param unknown_type $action
 * @return unknown
 */
	function _getViewFileName($action) {
		$action = Inflector::underscore($action);
		$paths = Configure::getInstance();

		if (!is_null($this->webservices)) {
			$type = strtolower($this->webservices) . DS;
		} else {
			$type = null;
		}

		$position = strpos($action, '..');

		if ($position === false) {
		} else {
			$action = explode('/', $action);
			$i = array_search('..', $action);
			unset($action[$i - 1]);
			unset($action[$i]);
			$action='..' . DS . implode(DS, $action);
		}

		foreach($paths->viewPaths as $path) {
			if (file_exists($path . $this->themePath . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext)) {
				$viewFileName = $path . $this->themePath .  $this->viewPath . DS . $this->subDir . $type . $action . $this->ext;
				return $viewFileName;
			} elseif (file_exists($path . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext)) {
				$viewFileName = $path . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext;
				return $viewFileName;
			}
		}

		if ($viewFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . 'errors' . DS . $type . $action . '.thtml')) {
		} elseif($viewFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . $this->viewPath . DS . $type . $action . '.thtml')) {
		} else {
			$viewFileName =  $this->themePath . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext;
		}

		return $viewFileName;
	}
/**
 * Enter description here...
 *
 * @return unknown
 */
	function _getLayoutFileName() {
		if (isset($this->webservices) && !is_null($this->webservices)) {
			$type = strtolower($this->webservices) . DS;
		} else {
			$type = null;
		}

		if (isset($this->plugin) && !is_null($this->plugin)) {
			if (file_exists(APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'layouts' . DS .  $this->theme . DS . $this->layout . $this->ext)) {
				$layoutFileName = APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'layouts' . DS . $this->layout . $this->ext;
				return $layoutFileName;
			} elseif (file_exists(APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'layouts' . DS . $this->layout . $this->ext)) {
				$layoutFileName = APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'layouts' . DS . $this->layout . $this->ext;
				return $layoutFileName;
			}
		}

		if (file_exists($this->themeLayout . $this->subDir . $type . "{$this->layout}$this->ext")) {
			$layoutFileName = $this->themeLayout . $this->subDir . $type . "{$this->layout}$this->ext";
		}  elseif (file_exists(LAYOUTS . $this->subDir . $type . "{$this->layout}$this->ext")) {
			$layoutFileName = LAYOUTS . $this->subDir . $type . "{$this->layout}$this->ext";
		}  elseif ($layoutFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . "layouts" . DS . $type . "{$this->layout}.thtml")) {
		} else {
			$layoutFileName = LAYOUTS . $type . "{$this->layout}$this->ext";
		}

		return $layoutFileName;
	}

/**
 * Enter description here...
 *
 * @param unknown_type $action
 * @param unknown_type $layout
 * @return unknown
 */
	function pluginView($action, $layout) {
		$viewFileName = APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . $this->theme . DS . $this->viewPath . DS . $action . $this->ext;

		if (file_exists($viewFileName)) {
			$this->render($action, $layout, $viewFileName);
		} elseif (file_exists(APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . $this->viewPath . DS . $action . $this->ext)) {
			$viewFileName = APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . $this->viewPath . DS . $action . $this->ext;
			$this->render($action, $layout, $viewFileName);
		} else{
			return $this->cakeError('missingView', array(array(
											'className' => $this->controller->name,
											'action' => $action,
											'file' => $viewFileName,
											'base' => $this->base)));
		}
	}
}

?>
		

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..