Updates to make shared modules work, code refactoring and such.
git-svn-id: http://svn.cleancode.org/svn/pickles@83 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
parent
74bb829a5a
commit
54fea0d023
19 changed files with 128 additions and 120 deletions
|
@ -199,9 +199,9 @@ class Config extends Object {
|
|||
|
||||
$additional = null;
|
||||
|
||||
if (strpos($requested_module, '/') !== false) {
|
||||
list($requested_module, $additional) = split('/', $requested_module, 2);
|
||||
$additional = '/' . $additional;
|
||||
if (strpos($requested_module, '_') !== false) {
|
||||
list($requested_module, $additional) = split('_', $requested_module, 2);
|
||||
$additional = '_' . $additional;
|
||||
}
|
||||
|
||||
if (isset($this->modules->shared->module)) {
|
||||
|
@ -219,7 +219,8 @@ class Config extends Object {
|
|||
}
|
||||
}
|
||||
|
||||
return 'home';
|
||||
return false;
|
||||
//return 'home';
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -107,45 +107,31 @@ class Controller extends Object {
|
|||
}
|
||||
else {
|
||||
// Loads the requested module's information
|
||||
$module_file = '../modules/' . $module_name . '.php';
|
||||
|
||||
/**
|
||||
* @todo Rename "section" to something like "current" or "selected"
|
||||
* @todo Figure out WTF "event" is being used for
|
||||
*/
|
||||
if (strpos($module_name, '/') === false) {
|
||||
$class = $module_name;
|
||||
$section = $module_name;
|
||||
$event = null;
|
||||
}
|
||||
else {
|
||||
$class = strtr($module_name, '/', '_');
|
||||
list($section, $event) = split('/', $module_name);
|
||||
}
|
||||
$module_filename = $module_name;
|
||||
$module_file = '../modules/' . $module_filename . '.php';
|
||||
$module_class = strtr($module_filename, '/', '_');
|
||||
$module_name = split('_', $module_class);
|
||||
|
||||
// Establishes the shared module information
|
||||
$shared_module_name = $config->getSharedModule($module_name);
|
||||
$shared_module_file = PICKLES_PATH . 'modules/' . $shared_module_name . '.php';
|
||||
$shared_module_filename = $config->getSharedModule($module_class);
|
||||
$shared_module_file = PICKLES_PATH . 'common/modules/' . $shared_module_filename . '.php';
|
||||
$shared_module_class = strtr($shared_module_filename, '/', '_');
|
||||
$shared_module_name = split('_', $shared_module_class);
|
||||
|
||||
// Tries to load the site level module
|
||||
if (file_exists($module_file)) {
|
||||
require_once $module_file;
|
||||
|
||||
if (class_exists($class)) {
|
||||
$module = new $class($config, $db, $mailer, $error);
|
||||
if (class_exists($module_class)) {
|
||||
$module = new $module_class($config, $db, $mailer, $error);
|
||||
}
|
||||
}
|
||||
// Tries to load the shared module
|
||||
else if (file_exists($shared_module_file) && $shared_module_name != false) {
|
||||
if (strpos($shared_module_name, '/') === false) {
|
||||
$class = $shared_module_name;
|
||||
}
|
||||
else {
|
||||
$class = strtr($shared_module_name, '/', '_');
|
||||
}
|
||||
require_once $shared_module_file;
|
||||
|
||||
if (class_exists($class)) {
|
||||
$module = new $class($config, $db, $mailer, $error);
|
||||
if (class_exists($shared_module_class)) {
|
||||
$module = new $shared_module_class($config, $db, $mailer, $error);
|
||||
}
|
||||
}
|
||||
// Loads the stock module
|
||||
|
@ -176,11 +162,6 @@ class Controller extends Object {
|
|||
$display_class = 'Display_' . $display_type;
|
||||
$display = new $display_class($config, $error);
|
||||
|
||||
// Sets the display's properties
|
||||
$display->module_name = $module_name;
|
||||
$display->shared_name = $shared_module_name;
|
||||
$display->section = $section;
|
||||
|
||||
// Potentially establishes caching
|
||||
$caching = $module->getCaching();
|
||||
if ($caching) {
|
||||
|
@ -208,6 +189,20 @@ class Controller extends Object {
|
|||
}
|
||||
}
|
||||
|
||||
// If the loaded module has a name, use it to override
|
||||
if ($module->name != null && $module_filename != $module->name) {
|
||||
$module_filename = $module->name;
|
||||
$module_file = '../modules/' . $module_filename . '.php';
|
||||
$module_class = strtr($module_filename, '/', '_');
|
||||
$module_name = split('_', $module_class);
|
||||
}
|
||||
|
||||
// Sets the display's properties
|
||||
$display->module_filename = $module_filename;
|
||||
$display->module_name = $module_name;
|
||||
$display->shared_module_filename = $shared_module_filename;
|
||||
$display->shared_module_name = $shared_module_name;
|
||||
|
||||
// Loads the module data into the display to be rendered
|
||||
/**
|
||||
* @todo perhaps make this a passed variable
|
||||
|
|
|
@ -62,10 +62,12 @@ class DB extends Object {
|
|||
|
||||
$this->error = $error;
|
||||
|
||||
$this->hostname = $config->database->hostname;
|
||||
$this->username = $config->database->username;
|
||||
$this->password = $config->database->password;
|
||||
$this->database = $config->database->database;
|
||||
if (isset($config->database)) {
|
||||
$this->hostname = $config->database->hostname;
|
||||
$this->username = $config->database->username;
|
||||
$this->password = $config->database->password;
|
||||
$this->database = $config->database->database;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -56,7 +56,7 @@ class Display_Smarty extends Display_Common {
|
|||
public function prepare() {
|
||||
|
||||
// Enables caching
|
||||
if ($this->caching == true) {
|
||||
if ($this->caching === true) {
|
||||
$this->smarty->caching = 1;
|
||||
$this->smarty->compile_check = true;
|
||||
|
||||
|
@ -83,38 +83,41 @@ class Display_Smarty extends Display_Common {
|
|||
closedir($handle);
|
||||
}
|
||||
}
|
||||
|
||||
// Establishes the template names
|
||||
$template = SITE_PATH . '../templates/' . $this->module_name . '.tpl';
|
||||
$shared_template = PICKLES_PATH . 'templates/' . $this->shared_name . '.tpl';
|
||||
|
||||
/**
|
||||
* @todo There's a bug with the store home page since it's a redirect
|
||||
*/
|
||||
if (!file_exists($template)) {
|
||||
if (file_exists($shared_template)) {
|
||||
$template = $shared_template;
|
||||
}
|
||||
}
|
||||
|
||||
$this->template = $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the Smarty generated pages
|
||||
*/
|
||||
public function render() {
|
||||
|
||||
// Establishes the template names
|
||||
$template = SITE_PATH . '../templates/' . $this->module_filename . '.tpl';
|
||||
|
||||
if (!file_exists($template)) {
|
||||
$shared_template = PICKLES_PATH . 'common/templates/' . ($this->shared_filname == false ? $this->module_filename : $this->shared_filename) . '.tpl';
|
||||
|
||||
if (file_exists($shared_template)) {
|
||||
$template = $shared_template;
|
||||
}
|
||||
}
|
||||
|
||||
$this->template = $template;
|
||||
|
||||
$cache_id = isset($this->cache_id) ? $this->cache_id : $this->module_name;
|
||||
$cache_id = isset($this->cache_id) ? $this->cache_id : $this->module_filename;
|
||||
|
||||
$template = $this->smarty->template_exists('index.tpl') ? 'index.tpl' : $this->template;
|
||||
|
||||
if (!$this->smarty->is_cached($template, $cache_id)) {
|
||||
|
||||
// Pass all of our controller values to Smarty
|
||||
$this->smarty->assign('section', $this->section);
|
||||
$this->smarty->assign('module', $this->module_name);
|
||||
$this->smarty->assign('template', $this->template);
|
||||
// Build the combined module name array and assign it
|
||||
$module = $this->module_name;
|
||||
array_unshift($module, $this->module_filename);
|
||||
$this->smarty->assign('module', $module);
|
||||
|
||||
// Only assign the template if it's not the index, this avoids an infinite loop.
|
||||
if ($this->template != 'index.tpl') {
|
||||
$this->smarty->assign('template', $this->template);
|
||||
}
|
||||
|
||||
// Loads the data from the config
|
||||
$data = $this->config->getPublicData();
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
<?php
|
||||
|
||||
class home extends Model {
|
||||
|
||||
public function __default() {
|
||||
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
<?php
|
||||
|
||||
class store_home extends store {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function __default() {
|
||||
$this->data['featured'] = $this->db->getRow('SELECT id, name, teaser FROM products WHERE featured = "Y" AND id = 30 ORDER BY RAND() LIMIT 1;');
|
||||
|
||||
foreach (array('gif', 'jpg', 'png') as $extension) {
|
||||
if (file_exists(getcwd() . '/images/products/' . $this->data['featured']['id'] . '/medium.' . $extension)) {
|
||||
$this->data['featured']['image'] = $extension;
|
||||
}
|
||||
}
|
||||
|
||||
$this->data['top_sellers'] = $this->db->getArray('SELECT id, name FROM products ORDER BY RAND() LIMIT 10;');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
10
common/modules/home.php
Normal file
10
common/modules/home.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
class home extends Module {
|
||||
|
||||
protected $display = DISPLAY_SMARTY;
|
||||
|
||||
public function __default() {
|
||||
|
||||
}
|
||||
}
|
|
@ -1,11 +1,12 @@
|
|||
<?php
|
||||
|
||||
class store extends Model {
|
||||
class store extends Module {
|
||||
|
||||
protected $display = DISPLAY_SMARTY;
|
||||
protected $session = true;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
public function __construct(Config $config, DB $db, Mailer $mailer, Error $error) {
|
||||
parent::__construct($config, $db, $mailer, $error);
|
||||
|
||||
// Loads up the cart in case we need it
|
||||
if (!isset($_SESSION['cart'])) {
|
||||
|
@ -24,11 +25,10 @@ class store extends Model {
|
|||
$_SESSION['cart']['count'] = $count;
|
||||
}
|
||||
|
||||
$this->data['cart'] = $_SESSION['cart'];
|
||||
$this->cart = $_SESSION['cart'];
|
||||
|
||||
// Loads the navigation
|
||||
$config = Config::getInstance();
|
||||
$this->data['subnav'] = $config->store->sections;
|
||||
$this->subnav = $config->store->sections;
|
||||
|
||||
// Loads the categories
|
||||
$categories = $this->db->getArray('SELECT id, name, permalink FROM categories WHERE parent_id IS NULL AND visible = "Y" ORDER BY weight;');
|
||||
|
@ -44,18 +44,17 @@ class store extends Model {
|
|||
}
|
||||
}
|
||||
|
||||
$this->data['categories'] = $categories;
|
||||
$this->categories = $categories;
|
||||
}
|
||||
|
||||
public function __default() {
|
||||
// Forces store/home as the first page you get when only /store is called
|
||||
$object = new store_home();
|
||||
$object = new store_home($this->config, $this->db, $this->mailer, $this->error);
|
||||
$object->__default();
|
||||
|
||||
$this->data = $object->data;
|
||||
$this->set('name', 'store/home');
|
||||
$this->name = 'store/home';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -15,8 +15,10 @@
|
|||
|
||||
class store_cart extends store {
|
||||
|
||||
protected $display = DISPLAY_SMARTY;
|
||||
|
||||
public function __default() {
|
||||
$this->data['cart'] = $_SESSION['cart'];
|
||||
$this->cart = $_SESSION['cart'];
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
<?php
|
||||
|
||||
class store_category extends store {
|
||||
|
||||
protected $display = DISPLAY_SMARTY;
|
||||
|
||||
public function __default() {
|
||||
$category = $this->db->getRow('
|
||||
|
@ -9,7 +11,7 @@ class store_category extends store {
|
|||
WHERE permalink = "' . $_REQUEST['permalink'] . '";
|
||||
');
|
||||
|
||||
$this->data['category'] = $category;
|
||||
$this->category = $category;
|
||||
}
|
||||
}
|
||||
|
24
common/modules/store/home.php
Normal file
24
common/modules/store/home.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
class store_home extends store {
|
||||
|
||||
protected $display = DISPLAY_SMARTY;
|
||||
|
||||
public function __construct(Config $config, DB $db, Mailer $mailer, Error $error) {
|
||||
parent::__construct($config, $db, $mailer, $error);
|
||||
}
|
||||
|
||||
public function __default() {
|
||||
$this->featured = $this->db->getRow('SELECT id, name, teaser FROM products WHERE featured = "Y" AND id = 30 ORDER BY RAND() LIMIT 1;');
|
||||
|
||||
foreach (array('gif', 'jpg', 'png') as $extension) {
|
||||
if (file_exists(getcwd() . '/images/products/' . $this->featured['id'] . '/medium.' . $extension)) {
|
||||
$this->featured['image'] = $extension;
|
||||
}
|
||||
}
|
||||
|
||||
$this->top_sellers = $this->db->getArray('SELECT id, name FROM products ORDER BY RAND() LIMIT 10;');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,6 +1,6 @@
|
|||
<div class="content-left">
|
||||
{include file="../../pickles/templates/store/navigation.tpl"}<br /><br />
|
||||
{include file="../../pickles/templates/store/categories.tpl"}
|
||||
{include file="../../pickles/common/templates/store/navigation.tpl"}<br /><br />
|
||||
{include file="../../pickles/common/templates/store/categories.tpl"}
|
||||
</div>
|
||||
<div class="content-right store-cart">
|
||||
<div class="your-cart">
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<div id="store-categories">
|
||||
<div class="store-categories">
|
||||
{foreach from=$categories item=parent_category}
|
||||
<div id="{$parent_category.permalink}">
|
||||
<div class="{$parent_category.permalink}">
|
||||
<h1>{$parent_category.name}</h1>
|
||||
</div>
|
||||
<ul>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<div class="content-left">
|
||||
{include file="../../pickles/templates/store/navigation.tpl"}<br /><br />
|
||||
{include file="../../pickles/templates/store/categories.tpl"}
|
||||
{include file="../../pickles/common/templates/store/navigation.tpl"}<br /><br />
|
||||
{include file="../../pickles/common/templates/store/categories.tpl"}
|
||||
</div>
|
||||
<div class="content-right store-category">
|
||||
<div class="{$category.permalink}">
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
<div name="content-left">
|
||||
{include file="../../pickles/templates/store/navigation.tpl"}<br /><br />
|
||||
{include file="../../pickles/templates/store/categories.tpl"}
|
||||
{include file="../../pickles/common/templates/store/navigation.tpl"}<br /><br />
|
||||
{include file="../../pickles/common/templates/store/categories.tpl"}
|
||||
</div>
|
||||
<div name="content-right">
|
||||
{include file="../../pickles/templates/store/featured_product.tpl"}<br /><br />
|
||||
{include file="../../pickles/templates/store/top_sellers.tpl"}
|
||||
{include file="../../pickles/common/templates/store/featured_product.tpl"}<br /><br />
|
||||
{include file="../../pickles/common/templates/store/top_sellers.tpl"}
|
||||
</div>
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
<div id="store-subnav">
|
||||
<ol id="subnav">
|
||||
{foreach from=$subnav key=link item=label}
|
||||
<li {if $model == 'store/'|cat:$link}class="selected"{/if}>
|
||||
<div class="store-subnav">
|
||||
<ul class="subnav">
|
||||
{foreach from=$config.store key=link item=label}
|
||||
<li {if $module.0 == 'store/'|cat:$link}class="selected"{/if}>
|
||||
<a href="/{$section}/{$link}" class="{$link}">
|
||||
{$label}{if $link == 'cart' && $cart.count != 0} ({$cart.count}){/if}
|
||||
</a>
|
||||
</li>
|
||||
{/foreach}
|
||||
</ol>
|
||||
</ul>
|
||||
</div>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<div id="store-top-sellers">
|
||||
<div class="store-top-sellers">
|
||||
<h1>Top Sellers</h1>
|
||||
<div class="float-left" style="width: 175px;">
|
||||
{section name=product loop=$top_sellers start=0 loop=5 step=1}
|
||||
|
|
|
@ -64,7 +64,7 @@ function __autoload($class) {
|
|||
$filename = str_replace('_', '/', $class) . '.php';
|
||||
|
||||
$class_file = PICKLES_PATH . 'classes/' . $filename;
|
||||
$module_file = PICKLES_PATH . 'modules/' . $filename;
|
||||
$module_file = PICKLES_PATH . 'common/modules/' . $filename;
|
||||
|
||||
// Loads the class file
|
||||
if (file_exists($class_file)) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue