Permalink
Browse files

Created module initialization routines

Modified to detect JS and set cookie when present
- If no JS detected, active action uses paginator to list active pastes
  • Loading branch information...
1 parent 071d32f commit 0117002f6a98ec95ea46fdaebbf0c44643332a13 @weierophinney committed Dec 5, 2008
View
11 TODO
@@ -1,11 +1,8 @@
-- Paste application changes
- - Add JS detection
- - If available, redirect to JS-enabled version
- - Add detection in active-grid action to use Zend_Paginator if JS is
- disabled
- Import bugapp into Spindle module
-- Navbar
- - links should load main content frame dynamically, w/o page refresh
+- layout builders per module
+ - JS that updates main content pane either
+ - from HREF
+ - creating basic structure
- Create an install script
- should set appropriate permissions
- data directory (world writeable)
@@ -19,5 +19,9 @@ if (Zend_Controller_Front::getInstance()->getRequest()->getParam('nojs', false))
<?= $this->render('_headline.phtml') ?>
<?= $this->render('_mainPane.phtml') ?>
<?= $this->render('_footer.phtml') ?>
- <?= $this->borderContainer()->captureEnd('layout') ?></body>
+ <?= $this->borderContainer()->captureEnd('layout') ?>
+ <? if ($this->layout()->nav): ?>
+ <?= $this->layout()->nav; ?>
+ <? endif ?>
+</body>
</html>
@@ -0,0 +1,61 @@
+<?php
+/**
+ * Spindle_Bootstrap
+ *
+ * @uses My_Module_Base
+ * @package Spindle
+ * @copyright Copyright (C) 2008 - Present, Matthew Weier O'Phinney
+ * @author Matthew Weier O'Phinney <matthew@weierophinney.net>
+ * @license New BSD {@link http://framework.zend.com/license/new-bsd}
+ * @version $Id: $
+ */
+class Spindle_Bootstrap extends My_Module_Base
+{
+ /**
+ * Spindle-specific bootstrapping
+ *
+ * @return void
+ */
+ public function bootstrap()
+ {
+ $this->initConfig()
+ ->checkJsEnabled();
+ }
+
+ /**
+ * Initialize configuration
+ *
+ * @return Spindle_Bootstrap
+ */
+ public function initConfig()
+ {
+ $appBootstrap = $this->getAppBootstrap();
+ $configMaster = $appBootstrap->config;
+ $config = new Zend_Config_Ini(
+ dirname(__FILE__). '/config/spindle.ini',
+ $appBootstrap->env
+ );
+ $configMaster->merge($config);
+ return $this;
+ }
+
+ /**
+ * Check if javascript is enabled
+ *
+ * @return Spindle_Bootstrap
+ */
+ public function checkJsEnabled()
+ {
+ $appBootstrap = $this->getAppBootstrap();
+ $request = $appBootstrap->getRequest();
+ if ($request->getParam('jsEnabled', false)) {
+ setcookie('spindleJsEnabled', 1, strtotime('+30 days'));
+ $request->setParam('jsEnabled', true);
+ } elseif ($request->getCookie('spindleJsEnabled', false)) {
+ $request->setParam('jsEnabled', true);
+ } else {
+ $request->setParam('jsEnabled', false);
+ }
+ return $this;
+ }
+}
@@ -46,6 +46,20 @@ public function preDispatch()
}
/**
+ * Post Dispatch actions
+ *
+ * Render and capture navigation for this application
+ *
+ * @return void
+ */
+ public function postDispatch()
+ {
+ if (!$this->_getParam('jsEnabled', false)) {
+ $this->getResponse()->insert('nav', $this->view->render('_nav.phtml'));
+ }
+ }
+
+ /**
* Landing page
*
* @return void
@@ -154,6 +168,13 @@ public function saveFollowupAction()
*/
public function activeAction()
{
+ if (!$this->_getParam('jsEnabled', false)) {
+ $this->view->assign(array(
+ 'doPagination' => true,
+ 'model' => $this->getModel(),
+ 'page' => $this->_getParam('page', 1),
+ ));
+ }
}
/**
@@ -33,6 +33,11 @@ class Spindle_Model_Paste extends Spindle_Model_Model
protected $_table;
/**
+ * @var bool Whether or not to use a paginator for result sets
+ */
+ protected $_usePaginator = false;
+
+ /**
* Constructor
*
* @param mixed $options
@@ -53,6 +58,28 @@ public function __construct($options = null)
}
/**
+ * Set flag indicating whether or not to use paginator
+ *
+ * @param bool $flag
+ * @return Spindle_Model_Paste
+ */
+ public function setUsePaginator($flag)
+ {
+ $this->_usePaginator = (bool) $flag;
+ return $this;
+ }
+
+ /**
+ * Use a paginator?
+ *
+ * @return bool
+ */
+ public function usePaginator()
+ {
+ return $this->_usePaginator;
+ }
+
+ /**
* Add a paste
*
* @param array|struct $data
@@ -119,17 +146,26 @@ public function get($id)
/**
* Get list of active pastes, ordered by creation date (desc)
*
- * @return array
+ * @param null|int|array If set to use paginator, an int value indicates page; array for specific criteria
+ * @return array|Zend_Paginator
*/
- public function fetchActive(array $criteria = null)
+ public function fetchActive($criteria = null)
{
$table = $this->getTable();
$adapter = $table->getAdapter();
$select = $adapter->select();
$select->from('paste', array('id', 'type', 'summary', 'user', 'created', 'expires'))
->where('expires IS NULL OR expires = "" OR expires > ?', date('Y-m-d H:i:s'));
- if (null !== $criteria) {
+ if ($this->usePaginator()) {
+ $page = (null === $criteria) ? 1 : (int) $criteria;
+ $paginator = new Zend_Paginator(
+ new Zend_Paginator_Adapter_DbSelect($select)
+ );
+ $paginator->setItemCountPerPage(15)
+ ->setCurrentPageNumber($page);
+ return $paginator;
+ } elseif (is_array($criteria)) {
$this->_refineSelection($select, $criteria);
} else {
$select->order('created DESC');
@@ -0,0 +1,16 @@
+<? $basePasteUrl = $this->url(
+ array(
+ 'module' => 'spindle',
+ 'controller' => 'paste',
+ ),
+ 'default',
+ true
+ ); ?>
+<h4>Applications</h4>
+<ul class="nav">
+ <li class="navhead">Pastebin<ul>
+ <li><a href="<?= $basePasteUrl . '/about' ?>">About</a></li>
+ <li><a href="<?= $basePasteUrl . '/new-paste' ?>">New Paste</a></li>
+ <li><a href="<?= $basePasteUrl . '/active' ?>">Current Pastes</a></li>
+ </ul></li>
+</ul>
@@ -0,0 +1,32 @@
+<?php if ($this->pageCount): ?>
+<div class="paginationControl">
+<!-- Previous page link -->
+<?php if (isset($this->previous)): ?>
+ <a href="<?= $this->url(array('page' => $this->previous)); ?>">
+ &lt; Previous
+ </a> |
+<?php else: ?>
+ <span class="disabled">&lt; Previous</span> |
+<?php endif; ?>
+
+<!-- Numbered page links -->
+<?php foreach ($this->pagesInRange as $page): ?>
+ <?php if ($page != $this->current): ?>
+ <a href="<?= $this->url(array('page' => $page)); ?>">
+ <?= $page; ?>
+ </a> |
+ <?php else: ?>
+ <?= $page; ?> |
+ <?php endif; ?>
+<?php endforeach; ?>
+
+<!-- Next page link -->
+<?php if (isset($this->next)): ?>
+ <a href="<?= $this->url(array('page' => $this->next)); ?>">
+ Next &gt;
+ </a>
+<?php else: ?>
+ <span class="disabled">Next &gt;</span>
+<?php endif; ?>
+</div>
+<?php endif; ?>
@@ -0,0 +1,39 @@
+<?
+$this->model->setUsePaginator(true);
+$paginator = $this->model->fetchActive($this->page);
+?>
+<? if (count($paginator)): ?>
+<table>
+ <thead>
+ <tr>
+ <th field="id" width="16em">ID</th>
+ <th field="type">Type</th>
+ <th field="user">User</th>
+ <th field="summary">Summary</th>
+ <th field="expires">Expires</th>
+ </tr>
+ </thead>
+ <? foreach ($paginator as $item): ?>
+ <? $link = $this->url(
+ array(
+ 'module' => 'spindle',
+ 'controller' => 'paste',
+ 'action' => 'display',
+ 'id' => $item['id'],
+ ),
+ 'default',
+ true
+ ); ?>
+ <tr>
+ <td><a href="<?= $link ?>"><?= $this->escape($item['id']) ?></a></td>
+ <td><?= $this->escape($item['type']) ?></td>
+ <td><?= $this->escape($item['user']) ?></td>
+ <td><?= $this->escape($item['summary']) ?></td>
+ <td><?= $this->escape($item['expires']) ?></td>
+ </tr>
+ <? endforeach ?>
+</table>
+<? endif ?>
+
+<?= $this->paginationControl($paginator, 'Sliding', 'paste/_paginationControl.phtml') ?>
+
@@ -4,5 +4,11 @@ $this->current = 'active';
$this->tabContainer()->captureStart('pastebin', array('class' => 'paste-tab')) ?>
<?= $this->render('paste/_about.phtml') ?>
<?= $this->render('paste/_new-paste.phtml') ?>
-<?= $this->render('paste/_active.phtml') ?>
+<?
+ if ($this->doPagination) {
+ echo $this->render('paste/active-pagination.phtml');
+ } else {
+ echo $this->render('paste/_active.phtml');
+ }
+?>
<?= $this->tabContainer()->captureEnd('pastebin') ?>
View
@@ -0,0 +1,41 @@
+<?php
+/**
+ * Base bootstrap class
+ *
+ * @uses My_Module_Bootstrap
+ * @package My
+ * @subpackage Module
+ * @copyright Copyright (C) 2008 - Present, Matthew Weier O'Phinney
+ * @author Matthew Weier O'Phinney <matthew@weierophinney.net>
+ * @license New BSD {@link http://framework.zend.com/license/new-bsd}
+ * @version $Id: $
+ */
+abstract class My_Module_Base implements My_Module_Bootstrap
+{
+ /**
+ * @var object Application bootstrap object
+ */
+ protected $_appBootstrap;
+
+ /**
+ * Set application bootstrap object
+ *
+ * @param object $bootstrap
+ * @return My_Module_Base
+ */
+ public function setAppBootstrap($bootstrap)
+ {
+ $this->_appBootstrap = $bootstrap;
+ return $this;
+ }
+
+ /**
+ * Retrieve application bootstrap object
+ *
+ * @return object
+ */
+ public function getAppBootstrap()
+ {
+ return $this->_appBootstrap;
+ }
+}
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Bootstrap interface
+ *
+ * @package My
+ * @subpackage Module
+ * @copyright Copyright (C) 2008 - Present, Matthew Weier O'Phinney
+ * @author Matthew Weier O'Phinney <matthew@weierophinney.net>
+ * @license New BSD {@link http://framework.zend.com/license/new-bsd}
+ * @version $Id: $
+ */
+interface My_Module_Bootstrap
+{
+ public function setAppBootstrap($bootstrap);
+ public function getAppBootstrap();
+ public function bootstrap();
+}
Oops, something went wrong.

0 comments on commit 0117002

Please sign in to comment.