Created
April 12, 2012 21:06
-
-
Save dgs700/2370994 to your computer and use it in GitHub Desktop.
CakePHP base MV(C) base controller class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// cakePHP application wide controller settings | |
class AppController extends Controller { | |
var $components = array('Session', 'Cookie'); | |
var $language = null; | |
var $languages = array(); | |
var $file = null; | |
var $ext = '.php'; | |
//var $path = array(); | |
function beforeFilter() { | |
//set the application wide language variable if it is not already set for some reason | |
$this->_setLanguage(); | |
//if (Configure::read('Config.language') === null) { | |
Configure::write('Config.language', $this->language); | |
//} | |
$this->set('fileroot', Configure::read('Production.webroot')); | |
$this->set('imgroot', 'http://img.midpenbgc.org' . Configure::read('fileroot') . 'images/'); | |
$this->set('simgroot', 'https://www.midpenbgc.org' . Configure::read('fileroot') . 'images/'); | |
$this->set('server', Configure::read('server')); | |
} | |
function beforeRender() { | |
//if a translation for a particular view exists, switch the view path var- currently views/spa/pages/* | |
if (file_exists(VIEWS . $this->language . DS . $this->viewPath . DS . $this->file . '.php')) { | |
$this->viewPath = $this->language . DS . $this->viewPath; | |
} | |
} | |
//we are setting language based on a) the previous link b) the browser setting | |
//there is no persistance. for persisting the language setting use the code at the bottom | |
function _setLanguage() { | |
$env_lang = env('HTTP_ACCEPT_LANGUAGE'); | |
if (isset($this->params['language'])) { //first check the request route parameters | |
$this->language = $this->params['language']; | |
} elseif (isset($env_lang)) { //second check the users browser setting | |
$this->languages = split('[,;]', env('HTTP_ACCEPT_LANGUAGE')); | |
foreach ($this->languages as $key => $langKey) { | |
$langKey = strtolower($langKey); | |
$langKey = substr($langKey, 0, 2); | |
if ($langKey == 'es') { | |
$this->language = 'spa'; | |
return true; | |
} elseif ($langKey == 'zh') { | |
$this->language = 'chi'; | |
return true; | |
} else { | |
$this->language = 'eng'; | |
return true; | |
} | |
} | |
} else { //third default to english | |
$this->language = 'eng'; | |
} | |
/* if ($this->Cookie->read('lang') && !$this->Session->check('Config.language')) { | |
$this->Session->write('Config.language', $this->Cookie->read('lang')); | |
} | |
else if (isset($this->params['language']) && ($this->params['language'] != $this->Session->read('Config.language'))) { | |
$this->Session->write('Config.language', $this->params['language']); | |
$this->Cookie->write('lang', $this->params['language'], null, '20 days'); | |
} | |
*/ | |
} | |
//set nav element classes based on path info | |
function configNav($path) { | |
$page = $subpage = $title = $section = null; | |
if (!empty($path[0])) { | |
$page = $path[0]; | |
} | |
if (!empty($path[1])) { | |
$subpage = $path[1]; | |
} | |
//if (!empty($path[$count - 1])) { | |
// $title = Inflector::humanize($path[$count - 1]); | |
//} | |
$this->set(compact('page', 'subpage', 'title')); | |
// initialize menu classes to an empty string | |
for ($x = 0; $x < 10; $x++) { | |
$who[$x] = $change[$x] = $programs[$x] = $help[$x] = $news[$x] = $members[$x] = ''; | |
} | |
$subMenu0 = array('overview', 'locations', 'history', 'leadership', 'directors', '', ''); | |
$subMenu1 = array('overview', 'mission', 'partners', 'stories', '', '', ''); | |
$subMenu2 = array('overview', 'descriptions', 'camps', 'character', 'educational', 'sports', 'health', 'arts'); | |
$subMenu3 = array('overview', 'visit', 'donation', 'volunteer', 'events', 'past', ''); | |
$subMenu4 = array('overview', 'calendar', 'newsletter', '', '', ''); | |
$subMenu5 = array('overview', 'sanmateo', 'delue', 'registration', 'resources', 'publications', ''); | |
if ($page == "who") { | |
$who[0] = 'current-menu-item'; | |
for ($x = 0; $x < count($subMenu0); $x++) { | |
if ($subpage == $subMenu0[$x]) | |
$who[$x + 1] = 'current-submenu-item'; | |
} | |
}elseif ($page == "change") { | |
$change[0] = 'current-menu-item'; | |
for ($x = 0; $x < count($subMenu1); $x++) { | |
if ($subpage == $subMenu1[$x]) | |
$change[$x + 1] = 'current-submenu-item'; | |
} | |
}elseif ($page == "programs") { | |
$programs[0] = 'current-menu-item'; | |
for ($x = 0; $x < count($subMenu2); $x++) { | |
if ($subpage == $subMenu2[$x]) | |
$programs[$x + 1] = 'current-submenu-item'; | |
} | |
}elseif ($page == "help") { | |
$help[0] = 'current-menu-item'; | |
for ($x = 0; $x < count($subMenu3); $x++) { | |
if ($subpage == $subMenu3[$x]) | |
$help[$x + 1] = 'current-submenu-item'; | |
} | |
}elseif ($page == "news") { | |
$news[0] = 'current-menu-item'; | |
for ($x = 0; $x < count($subMenu4); $x++) { | |
if ($subpage == $subMenu4[$x]) | |
$news[$x + 1] = 'current-submenu-item'; | |
} | |
}elseif ($page == "members") { | |
$members[0] = 'current-menu-item'; | |
for ($x = 0; $x < count($subMenu5); $x++) { | |
if ($subpage == $subMenu5[$x]) | |
$members[$x + 1] = 'current-submenu-item'; | |
} | |
} | |
$this->set('who', $who); | |
$this->set('change', $change); | |
$this->set('programs', $programs); | |
$this->set('help', $help); | |
$this->set('news', $news); | |
$this->set('members', $members); | |
$this->set('lang', $this->language); | |
} | |
//filter out bogus spammer and hacker entries | |
function filterScum($first = '', $last = '', $address = '') { | |
$input = $first . $last . $address; | |
return (preg_match("/http/i", $input)) ? true : false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment