implement request #3: show recent pastes in "New paste" screen sidebar
authorChristian Weiske <cweiske@cweiske.de>
Mon, 7 May 2012 20:22:59 +0000 (22:22 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 7 May 2012 20:22:59 +0000 (22:22 +0200)
data/templates/base.htm
data/templates/index.htm
data/templates/repo-sidebar-list.htm [new file with mode: 0644]
src/phorkie/Database/Adapter/Elasticsearch/Search.php
src/phorkie/Database/Adapter/Null/Search.php
src/phorkie/Database/ISearch.php
www/index.php

index 8c6b4b0..1fbde4d 100644 (file)
@@ -2,6 +2,7 @@
 <!DOCTYPE html>
 <html>
  <head>
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <link rel="stylesheet" href="{{css}}"/>
   <link rel="stylesheet" href="/phorkie.css" />
   <title>{% block title %}{% endblock %} - {{title}}</title>
index 05c4c05..df1977a 100644 (file)
@@ -26,3 +26,15 @@ $(document).ready(function() {
 });
 </script>
 {% endblock %}
+
+
+{% block sidebar %}
+ {% if recents.results %}
+ <h3>Recently created</h3>
+ <ul>
+ {% for repo in recents.repos %}
+   {% include 'repo-sidebar-list.htm' %}
+ {% endfor %}
+ </ul>
+ {% endif %}
+{% endblock %}
\ No newline at end of file
diff --git a/data/templates/repo-sidebar-list.htm b/data/templates/repo-sidebar-list.htm
new file mode 100644 (file)
index 0000000..c20e8b6
--- /dev/null
@@ -0,0 +1,7 @@
+ <li>
+  <a href="{{repo.getLink('display')}}">
+   {{repo.id}}
+   {{repo.getDescription}}
+  </a><br/>
+  &nbsp;&nbsp;&nbsp;<span title="{{repo.crdate|date('c')}}">{{dh.get(repo.crdate)}}</span>
+ </li>
index d077747..e960e04 100644 (file)
@@ -3,12 +3,75 @@ namespace phorkie;
 
 class Database_Adapter_Elasticsearch_Search implements Database_ISearch
 {
+    protected static $sortMap = array(
+        'id' => array('id', 'asc'),
+        'crdate' => array('crdate', 'desc'),
+        'tstamp' => array('tstamp', 'desc'),
+    );
+
     public function __construct()
     {
         $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
     }
 
     /**
+     * List all repositories
+     *
+     * @param integer $page    Page of search results, starting with 0
+     * @param integer $perPage Number of results per page
+     * @param string  $sort    Sort order. Allowed values:
+     *                         - id     - repository id
+     *                         - crdate - creation date
+     *                         - tstamp - modification date
+     *
+     * @return Search_Result Search result object
+     */
+    public function listAll($page = 0, $perPage = 10, $sort = 'id', $sortOrder = null)
+    {
+        list($sortField, $orderField) = $this->getSortField($sort, $sortOrder);
+        $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+            $this->searchInstance . 'repo/_search',
+            \HTTP_Request2::METHOD_GET
+        );
+        $r->setBody(
+            json_encode(
+                (object)array(
+                    'from'  => $page * $perPage,
+                    'size'  => $perPage,
+                    'sort'  => array(
+                        $sortField => $orderField
+                    ),
+                    'query' => (object)array(
+                        'match_all' => (object)array()
+                    ),
+                )
+            )
+        );
+        $httpRes = $r->send();
+        $jRes = json_decode($httpRes->getBody());
+        if (isset($jRes->error)) {
+            throw new Exception(
+                'Search exception: ' . $jRes->error, $jRes->status
+            );
+        }
+
+        $sres = new Search_Result();
+        $sres->results = $jRes->hits->total;
+        $sres->page    = $page;
+        $sres->perPage = $perPage;
+
+        foreach ($jRes->hits->hits as $hit) {
+            $r = new Repository();
+            $r->loadById($hit->_source->id);
+            $r->crdate = strtotime($hit->_source->crdate);
+            $sres->repos[] = $r;
+        }
+
+        return $sres;
+    }
+
+
+    /**
      * Search for a given term and return repositories that contain it
      * in their description, file names or file content
      *
@@ -77,6 +140,22 @@ class Database_Adapter_Elasticsearch_Search implements Database_ISearch
 
         return $sres;
     }
+
+    protected function getSortField($sort, $sortOrder)
+    {
+        if (!isset(self::$sortMap[$sort])) {
+            throw new Exception('Invalid sort parameter: ' . $sort);
+        }
+        if ($sortOrder !== 'asc' && $sortOrder !== 'desc') {
+            throw new Exception('Invalid sortOrder parameter: ' . $sortOrder);
+        }
+
+        $data = self::$sortMap[$sort];
+        if ($sortOrder !== null) {
+            $data[1] = $sortOrder;
+        }
+        return $data;
+    }
 }
 
 ?>
index 0731932..579c13a 100644 (file)
@@ -11,6 +11,15 @@ class Database_Adapter_Null_Search implements Database_ISearch
         $sres->perPage = $perPage;
         return $sres;
     }
+
+    public function listAll($page = 0, $perPage = 10, $sort = 'id', $sortOrder = null)
+    {
+        $sres = new Search_Result();
+        $sres->results = 0;
+        $sres->page    = $page;
+        $sres->perPage = $perPage;
+        return $sres;
+    }
 }
 
 ?>
index b18d43a..c33867b 100644 (file)
@@ -4,6 +4,21 @@ namespace phorkie;
 interface Database_ISearch
 {
     public function search($term, $page = 0, $perPage = 10);
+
+    /**
+     * List all repositories
+     *
+     * @param integer $page    Page of search results, starting with 0
+     * @param integer $perPage Number of results per page
+     * @param string  $sort    Sort order. Allowed values:
+     *                         - id     - repository id
+     *                         - crdate - creation date
+     *                         - tstamp - modification date
+     *
+     * @return Search_Result Search result object
+     */
+    public function listAll($page = 0, $perPage = 10, $sort = 'id', $sortOrder = null);
+
 }
 
 ?>
index d926a1d..54b1e3a 100644 (file)
@@ -20,12 +20,15 @@ if ($repopo->process($_POST)) {
 $phork = array(
     '1' => new File(null, null)
 );
+$db = new Database();
 render(
     'index',
     array(
-        'files' => $phork,
+        'files'       => $phork,
         'description' => '',
-        'htmlhelper' => new HtmlHelper(),
+        'htmlhelper'  => new HtmlHelper(),
+        'recents'     => $db->getSearch()->listAll(0, 5, 'crdate', 'desc'),
+        'dh'          => new \Date_HumanDiff(),
     )
 );
 ?>