Skip to content
This repository
Browse code

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...
commit 0117002f6a98ec95ea46fdaebbf0c44643332a13 1 parent 071d32f
Matthew Weier O'Phinney authored December 05, 2008
11  TODO
... ...
@@ -1,11 +1,8 @@
1  
-- Paste application changes
2  
-  - Add JS detection
3  
-    - If available, redirect to JS-enabled version
4  
-    - Add detection in active-grid action to use Zend_Paginator if JS is
5  
-      disabled
6 1
 - Import bugapp into Spindle module
7  
-- Navbar
8  
-  - links should load main content frame dynamically, w/o page refresh
  2
+- layout builders per module
  3
+  - JS that updates main content pane either 
  4
+    - from HREF
  5
+    - creating basic structure
9 6
 - Create an install script
10 7
   - should set appropriate permissions
11 8
     - data directory (world writeable)
6  application/layouts/scripts/layout.phtml
@@ -19,5 +19,9 @@ if (Zend_Controller_Front::getInstance()->getRequest()->getParam('nojs', false))
19 19
     <?= $this->render('_headline.phtml') ?>
20 20
     <?= $this->render('_mainPane.phtml') ?>
21 21
     <?= $this->render('_footer.phtml') ?>
22  
-    <?= $this->borderContainer()->captureEnd('layout') ?></body>
  22
+    <?= $this->borderContainer()->captureEnd('layout') ?>
  23
+    <? if ($this->layout()->nav): ?>
  24
+        <?= $this->layout()->nav; ?>
  25
+    <? endif ?>
  26
+</body>
23 27
 </html>
61  application/modules/spindle/Bootstrap.php
... ...
@@ -0,0 +1,61 @@
  1
+<?php
  2
+/**
  3
+ * Spindle_Bootstrap 
  4
+ * 
  5
+ * @uses       My_Module_Base
  6
+ * @package    Spindle
  7
+ * @copyright  Copyright (C) 2008 - Present, Matthew Weier O'Phinney
  8
+ * @author     Matthew Weier O'Phinney <matthew@weierophinney.net> 
  9
+ * @license    New BSD {@link http://framework.zend.com/license/new-bsd}
  10
+ * @version    $Id: $
  11
+ */
  12
+class Spindle_Bootstrap extends My_Module_Base
  13
+{
  14
+    /**
  15
+     * Spindle-specific bootstrapping
  16
+     * 
  17
+     * @return void
  18
+     */
  19
+    public function bootstrap()
  20
+    {
  21
+        $this->initConfig()
  22
+             ->checkJsEnabled();
  23
+    }
  24
+
  25
+    /**
  26
+     * Initialize configuration
  27
+     * 
  28
+     * @return Spindle_Bootstrap
  29
+     */
  30
+    public function initConfig()
  31
+    {
  32
+        $appBootstrap = $this->getAppBootstrap();
  33
+        $configMaster = $appBootstrap->config;
  34
+        $config       = new Zend_Config_Ini(
  35
+            dirname(__FILE__). '/config/spindle.ini', 
  36
+            $appBootstrap->env
  37
+        );
  38
+        $configMaster->merge($config);
  39
+        return $this;
  40
+    }
  41
+
  42
+    /**
  43
+     * Check if javascript is enabled
  44
+     * 
  45
+     * @return Spindle_Bootstrap
  46
+     */
  47
+    public function checkJsEnabled()
  48
+    {
  49
+        $appBootstrap = $this->getAppBootstrap();
  50
+        $request      = $appBootstrap->getRequest();
  51
+        if ($request->getParam('jsEnabled', false)) {
  52
+            setcookie('spindleJsEnabled', 1, strtotime('+30 days'));
  53
+            $request->setParam('jsEnabled', true);
  54
+        } elseif ($request->getCookie('spindleJsEnabled', false)) {
  55
+            $request->setParam('jsEnabled', true);
  56
+        } else {
  57
+            $request->setParam('jsEnabled', false);
  58
+        }
  59
+        return $this;
  60
+    }
  61
+}
21  application/modules/spindle/controllers/PasteController.php
@@ -46,6 +46,20 @@ public function preDispatch()
46 46
     }
47 47
 
48 48
     /**
  49
+     * Post Dispatch actions
  50
+     *
  51
+     * Render and capture navigation for this application
  52
+     * 
  53
+     * @return void
  54
+     */
  55
+    public function postDispatch()
  56
+    {
  57
+        if (!$this->_getParam('jsEnabled', false)) {
  58
+            $this->getResponse()->insert('nav', $this->view->render('_nav.phtml'));
  59
+        }
  60
+    }
  61
+
  62
+    /**
49 63
      * Landing page
50 64
      * 
51 65
      * @return void
@@ -154,6 +168,13 @@ public function saveFollowupAction()
154 168
      */
155 169
     public function activeAction()
156 170
     {
  171
+        if (!$this->_getParam('jsEnabled', false)) {
  172
+            $this->view->assign(array(
  173
+                'doPagination' => true,
  174
+                'model'        => $this->getModel(),
  175
+                'page'         => $this->_getParam('page', 1),
  176
+            ));
  177
+        }
157 178
     }
158 179
 
159 180
     /**
42  application/modules/spindle/models/Paste.php
@@ -33,6 +33,11 @@ class Spindle_Model_Paste extends Spindle_Model_Model
33 33
     protected $_table;
34 34
 
35 35
     /**
  36
+     * @var bool Whether or not to use a paginator for result sets
  37
+     */
  38
+    protected $_usePaginator = false;
  39
+
  40
+    /**
36 41
      * Constructor
37 42
      * 
38 43
      * @param mixed $options 
@@ -53,6 +58,28 @@ public function __construct($options = null)
53 58
     }
54 59
 
55 60
     /**
  61
+     * Set flag indicating whether or not to use paginator
  62
+     * 
  63
+     * @param  bool $flag 
  64
+     * @return Spindle_Model_Paste
  65
+     */
  66
+    public function setUsePaginator($flag)
  67
+    {
  68
+        $this->_usePaginator = (bool) $flag;
  69
+        return $this;
  70
+    }
  71
+
  72
+    /**
  73
+     * Use a paginator?
  74
+     * 
  75
+     * @return bool
  76
+     */
  77
+    public function usePaginator()
  78
+    {
  79
+        return $this->_usePaginator;
  80
+    }
  81
+
  82
+    /**
56 83
      * Add a paste
57 84
      * 
58 85
      * @param  array|struct $data 
@@ -119,9 +146,10 @@ public function get($id)
119 146
     /**
120 147
      * Get list of active pastes, ordered by creation date (desc)
121 148
      * 
122  
-     * @return array
  149
+     * @param  null|int|array If set to use paginator, an int value indicates page; array for specific criteria
  150
+     * @return array|Zend_Paginator
123 151
      */
124  
-    public function fetchActive(array $criteria = null)
  152
+    public function fetchActive($criteria = null)
125 153
     {
126 154
         $table   = $this->getTable();
127 155
         $adapter = $table->getAdapter();
@@ -129,7 +157,15 @@ public function fetchActive(array $criteria = null)
129 157
         $select->from('paste', array('id', 'type', 'summary', 'user', 'created', 'expires'))
130 158
                ->where('expires IS NULL OR expires = "" OR expires > ?', date('Y-m-d H:i:s'));
131 159
 
132  
-        if (null !== $criteria) {
  160
+        if ($this->usePaginator()) {
  161
+            $page      = (null === $criteria) ? 1 : (int) $criteria;
  162
+            $paginator = new Zend_Paginator(
  163
+                new Zend_Paginator_Adapter_DbSelect($select)
  164
+            );
  165
+            $paginator->setItemCountPerPage(15)
  166
+                      ->setCurrentPageNumber($page);
  167
+            return $paginator;
  168
+        } elseif (is_array($criteria)) {
133 169
             $this->_refineSelection($select, $criteria);
134 170
         } else {
135 171
             $select->order('created DESC');
16  application/modules/spindle/views/scripts/_nav.phtml
... ...
@@ -0,0 +1,16 @@
  1
+<? $basePasteUrl = $this->url(
  2
+        array(
  3
+            'module'     => 'spindle',
  4
+            'controller' => 'paste',
  5
+        ),
  6
+        'default',
  7
+        true
  8
+    ); ?>
  9
+<h4>Applications</h4>
  10
+<ul class="nav">
  11
+    <li class="navhead">Pastebin<ul>
  12
+        <li><a href="<?= $basePasteUrl . '/about' ?>">About</a></li>
  13
+        <li><a href="<?= $basePasteUrl . '/new-paste' ?>">New Paste</a></li>
  14
+        <li><a href="<?= $basePasteUrl . '/active' ?>">Current Pastes</a></li>
  15
+    </ul></li>
  16
+</ul>
32  application/modules/spindle/views/scripts/paste/_paginationControl.phtml
... ...
@@ -0,0 +1,32 @@
  1
+<?php if ($this->pageCount): ?>
  2
+<div class="paginationControl">
  3
+<!-- Previous page link -->
  4
+<?php if (isset($this->previous)): ?>
  5
+  <a href="<?= $this->url(array('page' => $this->previous)); ?>">
  6
+    &lt; Previous
  7
+  </a> |
  8
+<?php else: ?>
  9
+  <span class="disabled">&lt; Previous</span> |
  10
+<?php endif; ?>
  11
+
  12
+<!-- Numbered page links -->
  13
+<?php foreach ($this->pagesInRange as $page): ?>
  14
+  <?php if ($page != $this->current): ?>
  15
+    <a href="<?= $this->url(array('page' => $page)); ?>">
  16
+        <?= $page; ?>
  17
+    </a> |
  18
+  <?php else: ?>
  19
+    <?= $page; ?> |
  20
+  <?php endif; ?>
  21
+<?php endforeach; ?>
  22
+
  23
+<!-- Next page link -->
  24
+<?php if (isset($this->next)): ?>
  25
+  <a href="<?= $this->url(array('page' => $this->next)); ?>">
  26
+    Next &gt;
  27
+  </a>
  28
+<?php else: ?>
  29
+  <span class="disabled">Next &gt;</span>
  30
+<?php endif; ?>
  31
+</div>
  32
+<?php endif; ?>
39  application/modules/spindle/views/scripts/paste/active-pagination.phtml
... ...
@@ -0,0 +1,39 @@
  1
+<?
  2
+$this->model->setUsePaginator(true);
  3
+$paginator = $this->model->fetchActive($this->page);
  4
+?>
  5
+<? if (count($paginator)): ?>
  6
+<table>
  7
+    <thead>
  8
+        <tr>
  9
+            <th field="id" width="16em">ID</th>
  10
+            <th field="type">Type</th>
  11
+            <th field="user">User</th>
  12
+            <th field="summary">Summary</th>
  13
+            <th field="expires">Expires</th>
  14
+        </tr>
  15
+    </thead>
  16
+    <? foreach ($paginator as $item): ?>
  17
+        <? $link = $this->url(
  18
+                array(
  19
+                    'module'     => 'spindle',
  20
+                    'controller' => 'paste',
  21
+                    'action'     => 'display',
  22
+                    'id'         => $item['id'],
  23
+                ),
  24
+                'default',
  25
+                true
  26
+            ); ?>
  27
+    <tr>
  28
+        <td><a href="<?= $link ?>"><?= $this->escape($item['id']) ?></a></td>
  29
+        <td><?= $this->escape($item['type']) ?></td>
  30
+        <td><?= $this->escape($item['user']) ?></td>
  31
+        <td><?= $this->escape($item['summary']) ?></td>
  32
+        <td><?= $this->escape($item['expires']) ?></td>
  33
+    </tr>
  34
+    <? endforeach ?>
  35
+</table>
  36
+<? endif ?>
  37
+
  38
+<?= $this->paginationControl($paginator, 'Sliding', 'paste/_paginationControl.phtml') ?>
  39
+
8  application/modules/spindle/views/scripts/paste/active.phtml
@@ -4,5 +4,11 @@ $this->current = 'active';
4 4
 $this->tabContainer()->captureStart('pastebin', array('class' => 'paste-tab')) ?>
5 5
 <?= $this->render('paste/_about.phtml') ?>
6 6
 <?= $this->render('paste/_new-paste.phtml') ?>
7  
-<?= $this->render('paste/_active.phtml') ?>
  7
+<? 
  8
+    if ($this->doPagination) {
  9
+        echo $this->render('paste/active-pagination.phtml');
  10
+    } else {
  11
+        echo $this->render('paste/_active.phtml');
  12
+    }
  13
+?>
8 14
 <?= $this->tabContainer()->captureEnd('pastebin') ?>
41  library/My/Module/Base.php
... ...
@@ -0,0 +1,41 @@
  1
+<?php
  2
+/**
  3
+ * Base bootstrap class
  4
+ * 
  5
+ * @uses       My_Module_Bootstrap
  6
+ * @package    My
  7
+ * @subpackage Module
  8
+ * @copyright  Copyright (C) 2008 - Present, Matthew Weier O'Phinney
  9
+ * @author     Matthew Weier O'Phinney <matthew@weierophinney.net>
  10
+ * @license    New BSD {@link http://framework.zend.com/license/new-bsd}
  11
+ * @version    $Id: $
  12
+ */
  13
+abstract class My_Module_Base implements My_Module_Bootstrap
  14
+{
  15
+    /**
  16
+     * @var object Application bootstrap object
  17
+     */
  18
+    protected $_appBootstrap;
  19
+
  20
+    /**
  21
+     * Set application bootstrap object
  22
+     * 
  23
+     * @param  object $bootstrap 
  24
+     * @return My_Module_Base
  25
+     */
  26
+    public function setAppBootstrap($bootstrap)
  27
+    {
  28
+        $this->_appBootstrap = $bootstrap;
  29
+        return $this;
  30
+    }
  31
+
  32
+    /**
  33
+     * Retrieve application bootstrap object
  34
+     * 
  35
+     * @return object
  36
+     */
  37
+    public function getAppBootstrap()
  38
+    {
  39
+        return $this->_appBootstrap;
  40
+    }
  41
+}
17  library/My/Module/Bootstrap.php
... ...
@@ -0,0 +1,17 @@
  1
+<?php
  2
+/**
  3
+ * Bootstrap interface
  4
+ * 
  5
+ * @package    My
  6
+ * @subpackage Module
  7
+ * @copyright  Copyright (C) 2008 - Present, Matthew Weier O'Phinney
  8
+ * @author     Matthew Weier O'Phinney <matthew@weierophinney.net>
  9
+ * @license    New BSD {@link http://framework.zend.com/license/new-bsd}
  10
+ * @version    $Id: $
  11
+ */
  12
+interface My_Module_Bootstrap
  13
+{
  14
+    public function setAppBootstrap($bootstrap);
  15
+    public function getAppBootstrap();
  16
+    public function bootstrap();
  17
+}
54  library/My/Plugin/Initialize.php
@@ -14,6 +14,11 @@
14 14
 class My_Plugin_Initialize extends Zend_Controller_Plugin_Abstract
15 15
 {
16 16
     /**
  17
+     * @var array Array of module bootstrap classes that have been loaded
  18
+     */
  19
+    protected $_moduleBootstraps = array();
  20
+
  21
+    /**
17 22
      * Constructor
18 23
      * 
19 24
      * @param  string $basePath Base path of application
@@ -44,33 +49,40 @@ public function routeStartup(Zend_Controller_Request_Abstract $request)
44 49
     }
45 50
 
46 51
     /**
47  
-     * Initialize configuration
  52
+     * PreDispatch actions
  53
+     *
  54
+     * Initialize module bootstraps
48 55
      * 
49  
-     * @return My_Plugin_Initialize
  56
+     * @param Zend_Controller_Request_Abstract $request 
  57
+     * @return void
50 58
      */
51  
-    public function initConfig()
  59
+    public function preDispatch(Zend_Controller_Request_Abstract $request)
52 60
     {
53  
-        $configMaster = new Zend_Config_Ini(APPLICATION_PATH . '/config/site.ini', $this->env);
54  
-        $configMaster = $configMaster->toArray();
55  
-
56  
-        $ri      = new DirectoryIterator(APPLICATION_PATH . '/modules');
57  
-        $modules = array();
58  
-        foreach ($ri as $file) {
59  
-            if (!$file->isDir() || $file->isDot()) {
60  
-                continue;
61  
-            }
62  
-            $modules[$file->getFileName()] = $file->getRealPath();
63  
-        }
64  
-
65  
-        foreach ($modules as $module => $path) {
66  
-            $configPath = $path . '/config/' . $module . '.ini';
67  
-            if (file_exists($configPath)) {
68  
-                $config = new Zend_Config_Ini($configPath, $this->env);
69  
-                $configMaster = array_merge($configMaster, $config->toArray());
  61
+        $module = $request->getModuleName();
  62
+        if (!array_key_exists($module, $this->_moduleBootstraps)) {
  63
+            $bootstrapFile = $this->front->getModuleDirectory($module) . '/Bootstrap.php';
  64
+            if (require $bootstrapFile) {
  65
+                $class = ucfirst($module) . '_Bootstrap';
  66
+                if (class_exists($class, false)) {
  67
+                    $bootstrap = new $class;
  68
+                    $bootstrap->setAppBootstrap($this);
  69
+                    $bootstrap->bootstrap();
  70
+                    $this->_moduleBootstraps[$module] = $bootstrap;
  71
+                }
  72
+            } else {
  73
+                $this->_moduleBootstraps[$module] = false;
70 74
             }
71 75
         }
  76
+    }
72 77
 
73  
-        $this->config = new Zend_Config($configMaster);
  78
+    /**
  79
+     * Initialize configuration
  80
+     * 
  81
+     * @return My_Plugin_Initialize
  82
+     */
  83
+    public function initConfig()
  84
+    {
  85
+        $this->config = new Zend_Config_Ini(APPLICATION_PATH . '/config/site.ini', $this->env, true);
74 86
         Zend_Registry::set('config', $this->config);
75 87
         return $this;
76 88
     }
23  public/js-src/spindle/main.js
@@ -8,12 +8,23 @@ dojo.provide("spindle.main");
8 8
     dojo.require("dojo.parser");
9 9
 
10 10
     dojo.addOnLoad(function() {
11  
-        if (dijit.byId("layout")) {
12  
-            spindle.navMenu = new spindle.NavMenu({
13  
-                baseUrl: spindle.baseUrl,
14  
-            });
15  
-            dijit.byId("layout").addChild(spindle.navMenu);
16  
-            dijit.byId("layout").resize();
  11
+        if (!dojo.cookie("spindleJsEnabled")) {
  12
+        // Detect JS
  13
+            var loc = dojo.doc.location;
  14
+            if (loc.search) {
  15
+                dojo.doc.location = loc.href + "&jsEnabled=1";
  16
+            } else {
  17
+                dojo.doc.location = loc.href + "?jsEnabled=1";
  18
+            }
  19
+        } else {
  20
+        // Detect layout
  21
+            if (dijit.byId("layout")) {
  22
+                spindle.navMenu = new spindle.NavMenu({
  23
+                    baseUrl: spindle.baseUrl,
  24
+                });
  25
+                dijit.byId("layout").addChild(spindle.navMenu);
  26
+                dijit.byId("layout").resize();
  27
+            }
17 28
         }
18 29
     });
19 30
 

0 notes on commit 0117002

Please sign in to comment.
Something went wrong with that request. Please try again.