<?phpif(!defined('IN_VALKYRIE')) { exit;}class IndexController extends PageController { public $routes = array( // overloadable action to work with __call '*' => '/*/(str)a/(str)b/(str)c/(str)d', // deal with paginating the blog posts 'page' => '/page/(int)page_num', ); public $pre_filters = array('options'); // default action, show the first few posts public function index(Request $request, Response $response) { $request['page_num'] = 1; $this->page($request, $response); } // looking on another page of posts public function page(Request $request, Response $response) { // how many posts are there? No need for the burden of finders // or records here $num_posts = $this->dba->getValue("SELECT COUNT(ID) FROM wp_posts WHERE post_type='post' AND post_status='publish'"); // calculate the number of pages $num_pages = @ceil($num_posts / $this['posts_per_page']); // pagination using the Pager library $response['posts_pager'] = $this->pager ->numPages($num_pages) ->currentPage($request['page_num']) ->showRightOfCurrent(3) ->showLeftOfCurrent(3) ->setPath('/page/%page%/'); // template stuff $response['posts'] = $this->getFinder('posts')->findAllVisible($request['page_num'], $this['posts_per_page']); $response['main'] = 'posts.html'; } // allow for variable route names as per defined in getRoutes from // the database pages that were added, eg: wordpress' "about" page public function __call($fn, array $args = array()) { // need to bring them in this way when using __call list($request, $response) = $args; // finder for posts model definition $posts_finder = $this->getFinder('posts'); // permalink / archive / single post if(ctype_digit($request['a'])) { // single posts if($request['d'] !== NULL) { $post = $posts_finder->findAllVisible(1, 0, array( 'post_name' => $request['d'], )); if($post === NULL) { $this->setNotFound(); } $response['post'] = $post; $response['main'] = 'post.html'; // archives } else { $posts = $posts_finder->findAllVisible(1, 0, array( 'day' => $request['c'], 'month' => $request['b'], 'year' => $request['a'], )); $time = mktime(0, 0, 0, $request['b'], $request['c'], $request['a']); $date = ""; // make a nice looking date, eg: Jan 1, 2007 if($request['b']) { $date .= date("M ", $time); } if($request['c']) { $date .= date("j, ", $time); } if($request['a']) { $date .= date("Y", $time); } $response['archive_date'] = $date; $response['archive_posts'] = $posts; $response['main'] = 'archives.html'; } // page / category } else { // cached info, uses same query results as main menu $pages = $posts_finder->findVisiblePages(); // this page exists if(isset($pages[$request['a']])) { $response['post'] = $posts_finder->findPage($request['a']); $response['main'] = 'post.html'; // check if this is a category, or sub category } else { $finder = $this->getFinder('categories'); // acount for second-level sub category $category_name = $request['b'] !== NULL ? $request['b'] : $request['a']; // get the category $category = $finder->findBy('category_nicename', $category_name); if($category === NULL) { $this->setNotFound(); } // get all categories from cached results so that we can get this category's // permalink using the proper nested category nice names $categories = $finder->findAllNames(); $category->permalink = $categories[$category->getId()]['nice_name']; // get the posts in this category through a relation $response['archive_posts'] = $posts_finder->findAllVisible(1, 0, array( 'category' => $category->getId(), )); $response['category'] = $category; $response['main'] = 'category.html'; } } } /** * RSS 2.0 feed. */ public function feed(Request $request, Response $response) { @header('Content-type: text/xml; charset=' . $this['blog_charset'], true); require_once VK_APP_DIR .'/functions.php'; $posts = $this->getFinder('posts')->findAllVisible(1, 10, array()); $response['last_modified'] = $posts->current()->getRssDate(); $response['posts'] = $posts; $response->setBase('rss2.html'); }}