From ce40ca58ad9b5fb33d2fd9f27b39d6cabba98918 Mon Sep 17 00:00:00 2001 From: Gabriel Dunne Date: Mon, 3 Oct 2011 17:11:30 +0530 Subject: [PATCH] Cleaned up model.php so it's closer to a typical MVC --- .htaccess | 15 +++++++++++ index.php | 53 +++++------------------------------- lib/data.php | 37 +++++++++++++------------ lib/model.php | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 114 insertions(+), 66 deletions(-) create mode 100644 .htaccess diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..89375db --- /dev/null +++ b/.htaccess @@ -0,0 +1,15 @@ + + + +# clean urls + + +# +# RewriteEngine On +# RewriteBase / +# RewriteCond %{REQUEST_FILENAME} !-f +# RewriteCond %{REQUEST_FILENAME} !-d +# RewriteRule . index.php [L] +# + + diff --git a/index.php b/index.php index b4179b3..754dcb8 100644 --- a/index.php +++ b/index.php @@ -3,53 +3,14 @@ require 'lib/init.php'; $request = get_request(); -$template = 'default.html.tpl'; -$v = new View ( $request ); -$m = new Model ( $request ); +$v = new View ($request); +$m = new Model ($request); -list($response_format, $response_mime_type) = parse_format($request['extension'], 'html'); +$v->assign('is_single', $m->is_single()); +$v->assign('is_page', $m->is_page()); +$v->assign('entries', $m->entries); -echo '
';
-exit(print_r($request));
+header("Content-Type: {$m->response_mime_type}; charset=UTF-8"); 
 
-$content_request = join(array(LOCAL_ROOT, CONTENT_DIR, $request['dirname'], $request['filename']), DIRECTORY_SEPARATOR );
-$page_request = join(array(LOCAL_ROOT, PAGE_DIR, $request['filename']), DIRECTORY_SEPARATOR );
-
-# multiple entries
-if (is_dir($content_request)) {
-    # get config in folder, if exists
-    if (is_file($content_request . '/' . CONFIG_FILE )) {
-        $config = parse_entry(new SplFileInfo($content_request . '/' . CONFIG_FILE));
-        $template = $config['config']['template'] . '.' . $response_format . '.tpl' ;
-    }
-    list($data, $total) = get_entries($request['dirname'] . '/' . $request['filename']);
-    $v->assign('data', $data);
-    $v->assign('page_title', preg_replace('{^/|/$}', '', $request['url']));
-}
-
-# single entry
-else if (is_file($content_request)) {
-    $v->assign('data', parse_entry(new SplFileInfo($content_request)));
-    $v->assign('single', true);
-    $template = 'single.'.$response_format.'.tpl';
-}
-
-# page
-else if (is_file($page_request)) {
-    $page = parse_entry(new SplFileInfo($page_request), 1);
-    $v->assign('data', $page);
-    $template = $page['config']['template'] ? $page['config']['template'] . '.' . $response_format . '.tpl' : 'page.' . $response_format . '.tpl';
-}
-else if ($request['filename'] == 'index')
-{
-    $template = 'index.html.tpl';
-}
-else {
-    $template = '404.html.tpl';
-}
-
-$v->assign( 'total', isset($total) ? $total : 0 );
-
-header("Content-Type: {$response_mime_type}; charset=UTF-8"); 
-$v->render( $template ); 
+$v->render( $m->template ); 
diff --git a/lib/data.php b/lib/data.php
index 1fb49ab..f536711 100644
--- a/lib/data.php
+++ b/lib/data.php
@@ -38,24 +38,6 @@ function get_entries( $path = "", $args = array())
 }
 
 
-/**
- * get all pages
- */
-function get_pages()
-{
-	$path = LOCAL_ROOT . PAGE_DIR;
-	$dir_iterator = new DirectoryIterator($path);
-	$pages = array();
-	foreach($dir_iterator as $page) {
-		if ($page->isDir()) continue;
-		$arr = parse_entry($page, 1);
-		$arr['is_page'] = 1;
-		$pages[] = $arr;
-	}
-	return $pages;
-}
-
-
 /**
  * 	returns directories of a folder
  * 	@param path the path to search. defaults to the CONTENT_DIR
@@ -92,6 +74,23 @@ function get_dirs( $path = "", $args = array())
 	return $dirs;
 }
 
+/**
+ * get all pages
+ */
+function get_pages()
+{
+	$path = LOCAL_ROOT . PAGE_DIR;
+	$dir_iterator = new DirectoryIterator($path);
+	$pages = array();
+	foreach($dir_iterator as $page) {
+		if ($page->isDir()) continue;
+		$arr = parse_entry($page, 1);
+		$arr['is_page'] = 1;
+		$pages[] = $arr;
+	}
+	return $pages;
+}
+
 
 /**
  * @param splFileInfo SPLFileInfo Object
@@ -142,4 +141,4 @@ function parse_entry($fileInfo, $page = 0)
     }	
 
 	return $f;
-}
+}
\ No newline at end of file
diff --git a/lib/model.php b/lib/model.php
index 24680d5..4c95573 100644
--- a/lib/model.php
+++ b/lib/model.php
@@ -2,8 +2,81 @@
 
 class Model
 {	
+	var $content_request = null;
+	var $page_request = null;
+	
+	var $response_format = 'html';
+	var $response_mime_type = 'text/html';
+	var $template = 'default.html.tpl';
+	var $page_title = null;
 
-	function __construct()
+	var $entries = null;
+	var $config = null;
+	var $total = 0; 
+
+	function __construct( $request )
+	{
+		$this->request = $request;
+		list($this->response_format, $this->response_mime_type) = parse_format($this->request['extension'], 'html');
+		$this->parse_request( $request );
+	}
+
+	function parse_request( $request )
+	{
+		$this->content_request = join(array( LOCAL_ROOT, CONTENT_DIR, $this->request['dirname'], $this->request['filename']), DIRECTORY_SEPARATOR );
+		$this->page_request    = join(array( LOCAL_ROOT, PAGE_DIR, $this->request['filename']), DIRECTORY_SEPARATOR );
+
+	    # check if multiple entries (dir)
+	    if ($this->is_multiple()) {
+			# check if config file exists
+		    if ($this->has_config()) {
+		        $this->config = parse_entry(new SplFileInfo(join(array($content_request, CONFIG_FILE, DIRECTORY_SEPARATOR))));
+		        $this->template = $this->config['config']['template'] . '.' . $this->response_format . '.tpl' ;
+		    }
+	    	list($this->entries, $this->total) = get_entries(join(array($this->request['dirname'], $this->request['filename'], DIRECTORY_SEPARATOR)));
+	    	$this->page_title = preg_replace('{^/|/$}', '', $this->request['url']);
+		} 
+		# check if single
+		else if ($this->is_single())
+		{
+			$this->entries = parse_entry(new SplFileInfo($this->content_request));
+			$this->template = 'single.'.$this->response_format.'.tpl';
+		}
+		# check if page
+		else if ($this->is_page()) {
+    		$this->entries = parse_entry(new SplFileInfo($this->page_request), 1);
+    		$this->template = isset($page['config']['template']) ? 
+    							$this->entries['config']['template'] . '.' . $response_format . '.tpl' : 
+    							'page.' . $response_format . '.tpl';
+		}
+		# check if index
+		else if ($this->request['filename'] == 'index')
+		{
+    		$this->template = 'index.html.tpl';
+		}
+		# poop 404
+		else {
+    		$this->template = '404.html.tpl';
+		}
+	}
+
+	function has_config()
 	{
+		return is_file($this->content_request . '/' . CONFIG_FILE );
 	}
+
+	function is_multiple()
+	{
+		return is_dir($this->content_request);
+	}
+
+	function is_single()
+	{
+		return is_file($this->content_request);
+	}
+
+	function is_page()
+	{
+		return is_file($this->page_request);
+	}			
 }
-- 
2.34.1