Skip to content

Commit

Permalink
show, edit and readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
daschl committed Dec 12, 2012
1 parent ae58f88 commit ce386de
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 4 deletions.
41 changes: 41 additions & 0 deletions 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!
79 changes: 75 additions & 4 deletions index.php
Expand Up @@ -38,6 +38,8 @@
define("COUCHBASE_PASSWORD", ""); define("COUCHBASE_PASSWORD", "");
define("COUCHBASE_CONN_PERSIST", true); define("COUCHBASE_CONN_PERSIST", true);


define("INDEX_DISPLAY_LIMIT", 20);

/** /**
* Init Block. * Init Block.
* *
Expand Down Expand Up @@ -81,7 +83,9 @@


// List all Beers (GET /beers) // List all Beers (GET /beers)
$app->get('/beers', function() use ($app, $cb) { $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(); $beers = array();
foreach($results['rows'] as $row) { foreach($results['rows'] as $row) {
Expand All @@ -102,7 +106,9 @@


// List all Breweries (GET /breweries) // List all Breweries (GET /breweries)
$app->get('/breweries', function() use ($app, $cb) { $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(); $breweries = array();
foreach($results['rows'] as $row) { foreach($results['rows'] as $row) {
Expand All @@ -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>) // Delete Beer (GET /beers/delete/<ID>)
$app->get('/beers/delete/{id}', function($id) use ($app, $cb) { $app->get('/beers/delete/{id}', function($id) use ($app, $cb) {
$cb->delete($id); $cb->delete($id);
Expand All @@ -134,12 +173,44 @@
return $app->redirect('/beersample-php/breweries'); 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) // Search via AJAX for beers (GET /beers/search)
$app->get('/beers/search', function(Request $request) use ($app, $cb) { $app->get('/beers/search', function(Request $request) use ($app, $cb) {
$input = strtolower($request->query->get('value')); $input = strtolower($request->query->get('value'));


$options = array( $options = array(
'limit' => 20, 'limit' => INDEX_DISPLAY_LIMIT,
'startkey' => $input, 'startkey' => $input,
'endkey' => $input . '\uefff' 'endkey' => $input . '\uefff'
); );
Expand Down Expand Up @@ -167,7 +238,7 @@
$input = strtolower($request->query->get('value')); $input = strtolower($request->query->get('value'));


$options = array( $options = array(
'limit' => 20, 'limit' => INDEX_DISPLAY_LIMIT,
'startkey' => $input, 'startkey' => $input,
'endkey' => $input . '\uefff' 'endkey' => $input . '\uefff'
); );
Expand Down
58 changes: 58 additions & 0 deletions 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 %}
17 changes: 17 additions & 0 deletions 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 %}
17 changes: 17 additions & 0 deletions 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.