Pastie: Add recent pastes sidebar
authorBen Klang <ben@alkaloid.net>
Tue, 18 May 2010 03:12:22 +0000 (23:12 -0400)
committerBen Klang <ben@alkaloid.net>
Tue, 18 May 2010 03:12:22 +0000 (23:12 -0400)
pastie/lib/Driver/Sql.php
pastie/lib/Highlighter/LibGeshi.php
pastie/paste.php
pastie/templates/paste.inc [new file with mode: 0644]
pastie/templates/recent.inc [new file with mode: 0644]
pastie/templates/view.inc
pastie/themes/screen.css
pastie/view.php

index 6723abb..933b1a8 100644 (file)
@@ -76,7 +76,7 @@ class Pastie_Driver_Sql extends Pastie_Driver
     public function savePaste($bin, $paste, $syntax = 'none', $title = '')
     {
         $this->_connect();
-        
+
         $id = $this->_db->nextId('mySequence');
         if (PEAR::isError($id)) {
             throw new Horde_Exception_Prior($id);
@@ -114,7 +114,7 @@ class Pastie_Driver_Sql extends Pastie_Driver
 
     /**
      * Retrieves the paste from the database.
-     * 
+     *
      * @param array $params  Array of selectors to find the paste.
      *
      * @return array  Array of paste information
@@ -177,6 +177,53 @@ class Pastie_Driver_Sql extends Pastie_Driver
         }
     }
 
+    public function getPastes($bin)
+    {
+        $query = 'SELECT paste_id, paste_uuid, paste_title, paste_syntax, '.
+                 'paste_content, paste_owner, paste_timestamp ' .
+                 'FROM pastie_pastes WHERE paste_bin = ? ' .
+                 'ORDER BY paste_timestamp DESC';
+        $values[] = 'default'; // FIXME: Horde_Share
+
+        /* Make sure we have a valid database connection. */
+        $this->_connect();
+
+        /* Log the query at a DEBUG log level. */
+        Horde::logMessage(sprintf('Pastie_Driver_Sql#getPastes(): %s', $query), 'DEBUG');
+
+        /* Execute the query. */
+        $result = $this->_db->query($query, $values);
+
+        if ($result instanceof PEAR_Error) {
+            throw new Horde_Exception_Prior($result);
+        }
+
+        $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
+        if ($row instanceof PEAR_Error) {
+            throw new Horde_Exception_Prior($row);
+        }
+
+        $pastes = array();
+        while ($row && !($row instanceof PEAR_Error)) {
+            $pastes[$row['paste_uuid']] = array(
+                'id' => $row['paste_id'],
+                'uuid' => $row['paste_uuid'],
+                'bin' => $row['paste_bin'],
+                'title' => $row['paste_title'],
+                'syntax' => $row['paste_syntax'],
+                'paste' => $row['paste_content'],
+                'owner' => $row['paste_owner'],
+                'timestamp' => new Horde_Date($row['paste_timestamp'])
+            );
+
+            /* Advance to the new row in the result set. */
+            $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
+        }
+        $result->free();
+
+        return $pastes;
+    }
+
     /**
      * Attempts to open a persistent connection to the SQL server.
      *
index da368ca..f1830d7 100644 (file)
@@ -11,6 +11,9 @@ class Pastie_Highlighter_LibGeshi extends Pastie_Highlighter {
         if ($syntax == 'none') {
             return '<pre>' . $text . '</pre>';
         } else {
+            // Since we may be coming from another syntax highlighter,
+            // we'll try downcasing the syntax name and hope we get lucky.
+            $syntax = strtolower($syntax);
             $geshi = new GeSHi($text, $syntax);
         }
         $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
index 8458391..395b7a1 100644 (file)
@@ -36,11 +36,18 @@ if ($form->validate($vars)) {
     }
 }
 
+try {
+    $recent = $pastie->driver->getPastes('default'); //FIXME: Horde_Share
+} catch (Horde_Exception $e) {
+    $notification->push($e);
+}
+
+
 $title = $form->getTitle();
 
 require PASTIE_TEMPLATES . '/common-header.inc';
 require PASTIE_TEMPLATES . '/menu.inc';
 
-$form->renderActive(null, null, $url, 'post');
+require PASTIE_TEMPLATES . '/paste.inc';
 
 require $registry->get('templates', 'horde') . '/common-footer.inc';
diff --git a/pastie/templates/paste.inc b/pastie/templates/paste.inc
new file mode 100644 (file)
index 0000000..2097b1c
--- /dev/null
@@ -0,0 +1,4 @@
+<?php
+require_once PASTIE_TEMPLATES . '/recent.inc';
+echo '<br style="clear:both;">';
+$form->renderActive(null, null, $url, 'post');
\ No newline at end of file
diff --git a/pastie/templates/recent.inc b/pastie/templates/recent.inc
new file mode 100644 (file)
index 0000000..a220e77
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+$uuid = Horde::applicationUrl('uuid');
+?>
+<div id="recentPastes">
+<h2>Recent Pastings</h2>
+<hr>
+<?php foreach ($recent as $r) { ?>
+    <div class="recentPaste">
+        <span class="pasteTitle"><a href="<?php echo $uuid . '/' . $r['uuid']; ?>"><?php echo $r['title']; ?></a></span><br>
+        <span class="pasteAuthor"><?php echo $r['owner']; ?></span> |
+        <span class="pasteSyntax"><?php echo $r['syntax']; ?></span> |
+        <span class="pasteDate"><?php echo $r['timestamp']; ?></span>
+    </div>
+    <hr>
+<?php } ?>
+</div>
\ No newline at end of file
index d491fab..c4e8e9f 100644 (file)
@@ -1,3 +1,6 @@
+<?php
+require_once PASTIE_TEMPLATES . '/recent.inc';
+?>
 <div id="showpaste">
     <div class="pasteHeader">
         <h2 class="pasteTitle"><?php echo $paste['title']; ?></h2>
@@ -7,4 +10,5 @@
     <div class="pasteContents">
     <?php echo $output; ?>
     </div>
-</div>
\ No newline at end of file
+</div>
+<br style="clear:both;">
\ No newline at end of file
index acf7cdc..f993e98 100644 (file)
@@ -1,6 +1,22 @@
 /* Insert CSS definitions here. */
+#recentPastes {
+    float: left;
+    width: 20%;
+    height: 100%;
+    border: 1px solid #c00;
+    padding: 2px;
+    overflow: hidden;
+    margin: 50px 15px 10px 15px;
+}
+
+.recentPaste {
+    margin-top: 3px;
+}
+
 #showpaste {
-    margin: 35px;
+    margin: 10px 35px 10px 35px;
+    float: right;
+    width: 70%;
 }
 
 .pasteTitle {
index ab2be4c..d7735e6 100644 (file)
@@ -19,6 +19,7 @@ $uuid = Horde_Util::getFormData('uuid');
 if (!empty($uuid)) {
     try {
         $paste = $pastie->driver->getPaste(array('uuid' => $uuid));
+        $recent = $pastie->driver->getPastes('default'); //FIXME: Horde_Share
     } catch (Horde_Exception $e) {
         $notification->push($e);
         $paste = null;