Moved to Display

git-svn-id: http://svn.cleancode.org/svn/pickles@80 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
Josh Sherman 2008-10-18 13:44:02 +00:00
parent b53f52ac1e
commit 5238e3f80d
5 changed files with 0 additions and 505 deletions

View file

@ -1,108 +0,0 @@
<?php
/**
* Common Viewer Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007, 2008 Joshua John Sherman
* @link http://phpwithpickles.org
* @license http://www.gnu.org/copyleft/lesser.html
* @package PICKLES
*/
/**
* Common Viewer Class
*
* This is the class that each viewer class should be extending from.
*/
abstract class Viewer_Common extends Object {
/**
* Constructor
*
* Runs the parent's constructor and adds the model to the object.
*
* @param object $model Object for the model we're loading
*/
public function __construct(Config $config, Error $error) {
parent::__construct();
$this->config = $config;
$this->error = $error;
/**
* @todo This may need to be flipped on only for Smarty and PHP templates
*/
// Obliterates any passed in PHPSESSID (thanks Google)
if (stripos($_SERVER['REQUEST_URI'], '?PHPSESSID=') !== false) {
list($request_uri, $phpsessid) = split('\?PHPSESSID=', $_SERVER['REQUEST_URI'], 2);
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $request_uri);
exit();
}
// XHTML compliancy stuff
ini_set('arg_separator.output', '&amp;');
ini_set('url_rewriter.tags', 'a=href,area=href,frame=src,input=src,fieldset=');
header('Content-type: text/html; charset=UTF-8');
if ($this->config->getDebug() === true) {
$superglobals = array($GLOBALS, $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST, $_ENV);
?>
<div class="debug" style="border: 2px solid black; padding: 5px; margin: 10px;">
<h1>PICKLES Debug Console</h1>
<?php
/*
foreach ($superglobals as $superglobal => $array) {
?>
<h2><h2>
<?php
}
*/
?>
<h2>$_REQUEST</h2>
<pre><?php var_dump($_REQUEST); ?></pre>
<h2>$_SESSION</h2>
<pre><?php var_dump($_SESSION); ?></pre>
<h2>$_SERVER</h2>
<pre><?php var_dump($_SERVER); ?></pre>
</div>
<?php
/*
function display_buffer() {
$buffer = str_replace(
array(' ', "\r\n", "\n", "\t"),
null,
ob_get_contents()
);
ob_end_clean();
exit($buffer);
}
*/
}
}
/**
* Abstract display function that is overloaded within the loaded viewer
*/
abstract public function display();
}
?>

View file

@ -1,51 +0,0 @@
<?php
/**
* JSON Viewer Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007, 2008 Joshua John Sherman
* @link http://phpwithpickles.org
* @license http://www.gnu.org/copyleft/lesser.html
* @package PICKLES
*/
/**
* JSON Viewer
*
* Displays data in JavaScript Object Notation. Requires PHP 5 >= 5.2.0 or
* PECL json 1.2.0 or 1.2.1
*
* @link http://json.org/
* @link http://us.php.net/json_encode
* @link http://pecl.php.net/package/json
*/
class Viewer_JSON extends Viewer_Common {
/**
* Displays the data in JSON format
*/
public function display() {
if (!function_exists('json_encode')) {
echo '{ "type" : "error", "message" : "json_encode() not found" }';
} else {
echo json_encode($this->data);
}
}
}
?>

View file

@ -1,100 +0,0 @@
<?php
/**
* PHP Viewer Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007, 2008 Joshua John Sherman
* @link http://phpwithpickles.org
* @license http://www.gnu.org/copyleft/lesser.html
* @package PICKLES
*/
/**
* PHP Viewer
*
* Displays the associated PHP templates for the Model. This is
* very similar to the Smarty viewer, but less overhead since it's
* straight PHP. The PHP viewer also utilizes a different caching
* system than Smarty. The general rules around the caching will
* be the same though.
*/
class Viewer_PHP extends Viewer_Common {
private $template = null;
private $shared_template = null;
/**
* Displays the PHP templated pages
*/
public function display() {
// Establishes the template names
$this->template = SITE_PATH . '../templates/' . $this->model_name . '.php';
$this->shared_template = PICKLES_PATH . 'templates/' . $this->shared_name . '.php';
//if (filemtime($this->template)) {
// readfile('/var/www/josh/pickles/var/joshtronic.localhost/smarty/cache/home.html');
//}
/**
* @todo There's a bug with the store home page since it's a redirect, maybe
*/
if (!file_exists($this->template)) {
if (file_exists($this->shared_template)) {
$this->template = $this->shared_template;
}
}
// Brings these variables to scope
/**
* @todo Section or model needs to go, having both seems dumb.
*/
$section = $this->section;
$model = $this->model_name;
$template = $this->template;
// Loads the data from the config
$config = $this->config->getPublicData();
// Loads the data from the model
$data = $this->data;
// If there's data set, this brings it into scope
if (isset($this->data) && is_array($this->data)) {
extract($this->data);
}
// If the index.php file is present, load it, else load the template directly
/**
* @todo Should there be additional logic to allow the model or the
* template to determine whether or not the index should be loaded?
*/
if (file_exists(SITE_PATH . '../templates/index.php')) {
require_once SITE_PATH . '../templates/index.php';
}
else if (file_exists($this->template)) {
require_once $this->template;
}
/**
* @todo Resurrect my buffer clean up code
*/
}
}
?>

View file

@ -1,100 +0,0 @@
<?php
/**
* RSS Viewer Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007, 2008 Joshua John Sherman
* @link http://phpwithpickles.org
* @license http://www.gnu.org/copyleft/lesser.html
* @package PICKLES
*/
/**
* RSS Viewer
*
* Displays data in RSS version 2.0 format.
*
* @link http://cyber.law.harvard.edu/rss/rss.html
* @todo Need to add support for RSS v1.0 as well as ATOM feeds. This may
* result in my abstracting out these classes a bit more (Probably a
* Feed viewer that would take a parameter to determine which type of
* of feed to use).
*/
class Viewer_RSS extends Viewer_Common {
/**
* Displays the RSS feed data
*
* Uses a combination of configuration options and a properly formatted data
* array to create an RSS v2.0 feed.
*
* @todo Error handling is non-existant.
*/
public function display() {
if (isset($this->data->channel)) {
$channel = $this->data['channel'];
$channel = $this->config->rss->$channel;
if (isset($this->data->items)) {
$items = $this->data['items'];
}
else {
$this->error->addError('No items were provided');
}
}
else {
$this->error->addError('No channel was specified');
}
header('Content-type: application/rss+xml; charset=UTF-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<rss version="2.0">
<channel>
<title><?=$channel['title'];?></title>
<link>http://<?=$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];?></link>
<description><?=$channel['description'];?></description>
<category><?=$channel['category'];?></category>
<language><?=$channel['language'] ? $channel['language'] : 'en-us';?></language>
<?php
if (is_array($items)) {
foreach ($items as $key => $item) {
$date = date('r', strtotime($item['date']));
if ($key == 0) {
echo "<lastBuildDate>{$date}</lastBuildDate>";
}
?>
<item>
<title><?=$item['title'];?></title>
<link><?=$item['link'];?></link>
<description><![CDATA[<?=$item['description'];?>]]></description>
<author><?=$item['author'];?></author>
<pubDate><?=$date;?></pubDate>
<guid><?=$item['guid'];?></guid>
</item>
<?php
}
}
?>
</channel>
</rss>
<?php
}
}
?>

View file

@ -1,146 +0,0 @@
<?php
/**
* Smarty Viewer Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007, 2008 Joshua John Sherman
* @link http://phpwithpickles.org
* @license http://www.gnu.org/copyleft/lesser.html
* @package PICKLES
*/
/**
* Smarty Viewer
*
* Displays the associated Smarty templates for the Model.
*
* @link http://smarty.net/
*/
class Viewer_Smarty extends Viewer_Common {
/**
* Displays the Smarty generated pages
*/
public function display() {
$smarty = new Smarty();
// Establishes our paths
$smarty->template_dir = SITE_PATH . '../templates/';
$cache_dir = SMARTY_PATH . 'cache';
$compile_dir = SMARTY_PATH . 'compile';
if (!file_exists($cache_dir)) { mkdir($cache_dir, 0777, true); }
if (!file_exists($compile_dir)) { mkdir($compile_dir, 0777, true); }
$smarty->cache_dir = $cache_dir ;
$smarty->compile_dir = $compile_dir;
/**
* @todo move this to the config
*/
// Enables caching
$smarty->caching = 1;
$smarty->compile_check = true;
$smarty->cache_lifetime = 3600;
var_dump($smarty->is_cached('index.tpl', $this->model_name));
// Loads the trim whitespace filter
$smarty->load_filter('output','trimwhitespace');
// Includes the PICKLES custom Smarty functions
$directory = PICKLES_PATH . 'functions/smarty/';
if (is_dir($directory)) {
if ($handle = opendir($directory)) {
while (($file = readdir($handle)) !== false) {
if (!preg_match('/^\./', $file)) {
list($type, $name, $ext) = split('\.', $file);
require_once $directory . $file;
$smarty->register_function($name, "smarty_{$type}_{$name}");
}
}
closedir($handle);
}
}
// Establishes the template names
$template = SITE_PATH . '../templates/' . $this->model_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;
}
}
// Pass all of our controller values to Smarty
$smarty->assign('section', $this->section);
$smarty->assign('model', $this->model_name);
$smarty->assign('template', $template);
// Loads the data from the config
$data = $this->config->getPublicData();
if (isset($data) && is_array($data)) {
$smarty->assign('config', $data);
}
// Loads the model's data
if (isset($this->data) && is_array($this->data)) {
foreach ($this->data as $variable => $value) {
$smarty->assign($variable, $value);
}
}
/**
* @todo There's no error checking for the index... should it be shared,
* and should the error checking occur anyway since any shit could
* happen?
*/
/*
$template = '../templates/index.tpl';
$shared_template = str_replace('../', '../../pickles/', $template);
if (!file_exists($template)) {
if (file_exists($shared_template)) {
$template = $shared_template;
}
}
*/
// If the index.tpl file is present, load it, else load the template directly
/**
* @todo Should there be additional logic to allow the model or the
* template to determine whether or not the index should be loaded?
*/
if ($smarty->template_exists('index.tpl')) {
$smarty->display('index.tpl', $this->model_name);
}
else {
$smarty->display($template);
}
}
}
?>