Browse files

show, edit and readme.

  • Loading branch information...
1 parent ae58f88 commit ce386ded04a1109ddc65d880a91c058f5d828de0 @daschl daschl committed Dec 12, 2012
Showing with 208 additions and 4 deletions.
  1. +41 −0 README.md
  2. +75 −4 index.php
  3. +58 −0 templates/beers/edit.twig.html
  4. +17 −0 templates/beers/show.twig.html
  5. +17 −0 templates/breweries/show.twig.html
View
41 README.md
@@ -0,0 +1,41 @@
+# Couchbase PHP Beer-Sample Application
+
+This sample application demonstrates the usage of the Couchbase PHP SDK 1.1 in
+combination with the Couchbase Server 2.0 release.
+
+The official tutorial belonging to this application can be found
+[here](http://www.couchbase.com/docs/couchbase-sdk-php-1.1/tutorial.html).
+
+It is a very simple web application that should show off the basics on how to
+interact with Couchbase Server 2.0 on both key-based and view-based operations.
+
+## Requirements
+Please make sure to have PHP 5.3 and Composer available. You also need to have
+the Couchbase Extension (Version 1.1 or higher) installed.
+
+## Installation
+Clone the application (or follow along in the tutorial), and run
+
+```
+php composer.phar install
+```
+
+Make sure to clone the application inside the /beersample-php subdirectory of
+the WEBROOT, because otherwise you need to change all the absolute links in
+there as well.
+
+## Configuration
+The application should be able to run out of the box if you have the beer-sample
+dataset installed and Couchbase Server 2.0 is running on your local machine. You
+can tune the settings on top of the `index.php` file:
+
+```php
+define("SILEX_DEBUG", true);
+define("COUCHBASE_HOSTS", "127.0.0.1");
+define("COUCHBASE_BUCKET", "beer-sample");
+define("COUCHBASE_PASSWORD", "");
+define("COUCHBASE_CONN_PERSIST", true);
+define("INDEX_DISPLAY_LIMIT", 20);
+```
+
+Have fun!
View
79 index.php
@@ -38,6 +38,8 @@
define("COUCHBASE_PASSWORD", "");
define("COUCHBASE_CONN_PERSIST", true);
+define("INDEX_DISPLAY_LIMIT", 20);
+
/**
* Init Block.
*
@@ -81,7 +83,9 @@
// List all Beers (GET /beers)
$app->get('/beers', function() use ($app, $cb) {
- $results = $cb->view("beer", "by_name", array('limit' => 20));
+ $results = $cb->view("beer", "by_name", array(
+ 'limit' => INDEX_DISPLAY_LIMIT
+ ));
$beers = array();
foreach($results['rows'] as $row) {
@@ -102,7 +106,9 @@
// List all Breweries (GET /breweries)
$app->get('/breweries', function() use ($app, $cb) {
- $results = $cb->view("brewery", "by_name", array('limit' => 20));
+ $results = $cb->view("brewery", "by_name", array(
+ 'limit' => INDEX_DISPLAY_LIMIT
+ ));
$breweries = array();
foreach($results['rows'] as $row) {
@@ -122,6 +128,39 @@
);
});
+
+// Show a beer (GET /beers/show/<ID>)
+$app->get('/beers/show/{id}', function($id) use ($app, $cb) {
+ $beer = $cb->get($id);
+ if($beer) {
+ $beer = json_decode($beer, true);
+ $beer['id'] = $id;
+ } else {
+ return $app->redirect('/beers');
+ }
+
+ return $app['twig']->render(
+ 'beers/show.twig.html',
+ compact('beer')
+ );
+});
+
+// Show a brewery (GET /breweries/show/<ID>)
+$app->get('/breweries/show/{id}', function($id) use ($app, $cb) {
+ $brewery = $cb->get($id);
+ if($brewery) {
+ $brewery = json_decode($brewery, true);
+ $brewery['id'] = $id;
+ } else {
+ return $app->redirect('/breweries');
+ }
+
+ return $app['twig']->render(
+ 'breweries/show.twig.html',
+ compact('brewery')
+ );
+});
+
// Delete Beer (GET /beers/delete/<ID>)
$app->get('/beers/delete/{id}', function($id) use ($app, $cb) {
$cb->delete($id);
@@ -134,12 +173,44 @@
return $app->redirect('/beersample-php/breweries');
});
+// Store submitted Beer Data (POST /beers/edit/<ID>)
+$app->post('/beers/edit/{id}', function(Request $request, $id) use ($app, $cb) {
+ $data = $request->request;
+
+ $newbeer = array();
+ foreach($data as $name => $value) {
+ $name = str_replace('beer_', '', $name);
+ $newbeer[$name] = $value;
+ }
+
+ $newbeer['type'] = 'beer';
+ $cb->set($id, json_encode($newbeer));
+
+ return $app->redirect('/beersample-php/beers/show/' . $id);
+});
+
+// Show Beer Form
+$app->get('/beers/edit/{id}', function($id) use ($app, $cb) {
+ $beer = $cb->get($id);
+ if($beer) {
+ $beer = json_decode($beer, true);
+ $beer['id'] = $id;
+ } else {
+ return $app->redirect('/beers');
+ }
+
+ return $app['twig']->render(
+ 'beers/edit.twig.html',
+ compact('beer')
+ );
+});
+
// Search via AJAX for beers (GET /beers/search)
$app->get('/beers/search', function(Request $request) use ($app, $cb) {
$input = strtolower($request->query->get('value'));
$options = array(
- 'limit' => 20,
+ 'limit' => INDEX_DISPLAY_LIMIT,
'startkey' => $input,
'endkey' => $input . '\uefff'
);
@@ -167,7 +238,7 @@
$input = strtolower($request->query->get('value'));
$options = array(
- 'limit' => 20,
+ 'limit' => INDEX_DISPLAY_LIMIT,
'startkey' => $input,
'endkey' => $input . '\uefff'
);
View
58 templates/beers/edit.twig.html
@@ -0,0 +1,58 @@
+{% extends "layout.twig.html" %}
+
+{% block content %}
+<h3>Edit Beer</h3>
+
+<form method="post" action="/beersample-php/beers/edit/{{beer.id}}">
+ <fieldset>
+ <legend>General Info</legend>
+ <div class="span12">
+ <div class="span6">
+ <label>Name</label>
+ <input type="text" name="beer_name" placeholder="The name of the beer." value="{{beer.name}}">
+
+ <label>Description</label>
+ <input type="text" name="beer_description" placeholder="A short description." value="{{beer.description}}">
+ </div>
+ <div class="span6">
+ <label>Style</label>
+ <input type="text" name="beer_style" placeholder="Bitter? Sweet? Hoppy?" value="{{beer.style}}">
+
+ <label>Category</label>
+ <input type="text" name="beer_category" placeholder="Ale? Stout? Lager?" value="{{beer.category}}">
+ </div>
+ </div>
+ </fieldset>
+ <fieldset>
+ <legend>Details</legend>
+ <div class="span12">
+ <div class="span6">
+ <label>Alcohol (ABV)</label>
+ <input type="text" name="beer_abv" placeholder="The beer's ABV" value="{{beer.abv}}">
+
+ <label>Biterness (IBU)</label>
+ <input type="text" name="beer_ibu" placeholder="The beer's IBU" value="{{beer.ibu}}">
+ </div>
+ <div class="span6">
+ <label>Beer Color (SRM)</label>
+ <input type="text" name="beer_srm" placeholder="The beer's SRM" value="{{beer.srm}}">
+
+ <label>Universal Product Code (UPC)</label>
+ <input type="text" name="beer_upc" placeholder="The beer's UPC" value="{{beer.upc}}">
+ </div>
+ </div>
+ </fieldset>
+ <fieldset>
+ <legend>Brewery</legend>
+ <div class="span12">
+ <div class="span6">
+ <label>Brewery</label>
+ <input type="text" name="beer_brewery_id" placeholder="The brewery" value="{{beer.brewery_id}}">
+ </div>
+ </div>
+ </fieldset>
+ <div class="form-actions">
+ <button type="submit" class="btn btn-primary">Save changes</button>
+ </div>
+</form>
+{% endblock %}
View
17 templates/beers/show.twig.html
@@ -0,0 +1,17 @@
+{% extends "layout.twig.html" %}
+
+{% block content %}
+<h3>Show Details for Beer "{{beer.name}}"</h3>
+<table class="table table-striped">
+ <tbody>
+ {% for key,attribute in beer %}
+ <c:forEach items="${beer}" var="item">
+ <tr>
+ <td><strong>{{key}}</strong></td>
+ <td>{{attribute}}</td>
+ </tr>
+ </c:forEach>
+ {% endfor %}
+ </tbody>
+</table>
+{% endblock %}
View
17 templates/breweries/show.twig.html
@@ -0,0 +1,17 @@
+{% extends "layout.twig.html" %}
+
+{% block content %}
+<h3>Show Details for Brewery "{{brewery.name}}"</h3>
+<table class="table table-striped">
+ <tbody>
+ {% for key,attribute in brewery %}
+ <c:forEach items="${beer}" var="item">
+ <tr>
+ <td><strong>{{key}}</strong></td>
+ <td>{{attribute}}</td>
+ </tr>
+ </c:forEach>
+ {% endfor %}
+ </tbody>
+</table>
+{% endblock %}

0 comments on commit ce386de

Please sign in to comment.