Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 2ef499e15e0753729dffab332d946d2d7d6d8536 @daschl daschl committed Dec 11, 2012
@@ -0,0 +1,5 @@
+.DS_Store
+/build
+/composer.lock
+/vendor
+phpunit.xml
@@ -0,0 +1,8 @@
+<IfModule mod_rewrite.c>
+ Options -MultiViews
+
+ RewriteEngine On
+ RewriteBase /beersample-php
+ RewriteCond %{REQUEST_FILENAME} !-f
+ RewriteRule ^ index.php [L]
+</IfModule>
@@ -0,0 +1,36 @@
+body {
+ padding-top: 20px;
+ padding-bottom: 40px;
+}
+
+/* Custom container */
+.container-narrow {
+ margin: 0 auto;
+ max-width: 700px;
+}
+.container-narrow > hr {
+ margin: 30px 0;
+}
+
+/* Main marketing message and sign up button */
+.jumbotron {
+ margin: 60px 0;
+ text-align: center;
+}
+.jumbotron h1 {
+ font-size: 72px;
+ line-height: 1;
+}
+.jumbotron .btn {
+ font-size: 21px;
+ padding: 14px 24px;
+}
+
+#map {
+ height: 400px;
+ width: 100%;
+ border: 2px solid #efefef;
+}
+#map img {
+ max-width: none;
+}

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,47 @@
+$(document).ready(function() {
+
+ /**
+ * AJAX Beer Search Filter
+ */
+ $("#beer-search").keyup(function() {
+ var content = $("#beer-search").val();
+ if(content.length >= 0) {
+ $.getJSON("/beersample-php/beers/search", {"value": content}, function(data) {
+ $("#beer-table tbody tr").remove();
+ for(var i=0;i<data.length;i++) {
+ var html = "<tr>";
+ html += "<td><a href=\"/beers/show/"+data[i].id+"\">"+data[i].name+"</a></td>";
+ html += "<td><a href=\"/breweries/show/"+data[i].brewery+"\">To Brewery</a></td>";
+ html += "<td>";
+ html += "<a class=\"btn btn-small btn-warning\" href=\"/beers/edit/"+data[i].id+"\">Edit</a>\n";
+ html += "<a class=\"btn btn-small btn-danger\" href=\"/beers/delete/"+data[i].id+"\">Delete</a>";
+ html += "</td>";
+ html += "</tr>";
+ $("#beer-table tbody").append(html);
+ }
+ });
+ }
+ });
+
+ /**
+ * AJAX Brewery Search Filter
+ */
+ $("#brewery-search").keyup(function() {
+ var content = $("#brewery-search").val();
+ if(content.length >= 0) {
+ $.getJSON("/beersample-php/breweries/search", {"value": content}, function(data) {
+ $("#brewery-table tbody tr").remove();
+ for(var i=0;i<data.length;i++) {
+ var html = "<tr>";
+ html += "<td><a href=\"/beers/show/"+data[i].id+"\">"+data[i].name+"</a></td>";
+ html += "<td>";
+ html += "<a class=\"btn btn-small btn-danger\" href=\"/beers/delete/"+data[i].id+"\">Delete</a>";
+ html += "</td>";
+ html += "</tr>";
+ $("#brewery-table tbody").append(html);
+ }
+ });
+ }
+ });
+
+});

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,6 @@
+{
+ "require": {
+ "silex/silex": "1.0.x-dev",
+ "twig/twig": ">=1.8,<2.0-dev"
+ }
+}
188 index.php
@@ -0,0 +1,188 @@
+<?php
+/**
+ * Copyright (C) 2009-2012 Couchbase, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
+ * IN THE SOFTWARE.
+ */
+
+use Symfony\Component\HttpFoundation\Request;
+use Silex\Application;
+use Silex\Provider\TwigServiceProvider;
+
+/**
+ * Config Block.
+ *
+ * To keep all settings (not many) organized in one place, they are defined
+ * here as constants. They could be done inline as well, but keeping them in
+ * one place makes it better organized and easier to refactor.
+ */
+define("SILEX_DEBUG", true);
+define("COUCHBASE_HOSTS", "127.0.0.1");
+define("COUCHBASE_BUCKET", "beer-sample");
+define("COUCHBASE_PASSWORD", "");
+define("COUCHBASE_CONN_PERSIST", true);
+
+/**
+ * Init Block.
+ *
+ * This block requires the autoloader and initializes the Silex Application.
+ * It also connects to our Couchbase Cluster and registeres the template
+ * engine (Twig).
+ */
+
+// Autoloader
+require_once __DIR__.'/vendor/autoload.php';
+
+// Silex-Application Bootstrap
+$app = new Application();
+$app['debug'] = SILEX_DEBUG;
+
+// Connecting to Couchbase
+$cb = new Couchbase(
+ COUCHBASE_HOSTS,
+ "beer-sample",
+ COUCHBASE_PASSWORD,
+ COUCHBASE_BUCKET,
+ COUCHBASE_CONN_PERSIST
+);
+
+// Register the Template Engine
+$app->register(new TwigServiceProvider(), array(
+ 'twig.path' => __DIR__.'/templates'
+));
+
+/**
+ * Action Block.
+ *
+ * From here on all actions are defined as simple closures. They render view
+ * templates or direct responses as needed.
+ */
+
+// Show the Welcome Page (GET /)
+$app->get('/', function() use ($app, $cb) {
+ return $app['twig']->render('welcome.twig.html');
+});
+
+// List all Beers (GET /beers)
+$app->get('/beers', function() use ($app, $cb) {
+ $results = $cb->view("beer", "by_name", array('limit' => 20));
+
+ $beers = array();
+ foreach($results['rows'] as $row) {
+ $doc = $cb->get($row['id']);
+ if($doc) {
+ $doc = json_decode($doc, true);
+ $beers[] = array(
+ 'name' => $doc['name'],
+ 'brewery' => $doc['brewery_id'],
+ 'id' => $row['id']
+ );
+ }
+
+ }
+
+ return $app['twig']->render('beers/index.twig.html', compact('beers'));
+});
+
+// List all Breweries (GET /breweries)
+$app->get('/breweries', function() use ($app, $cb) {
+ $results = $cb->view("brewery", "by_name", array('limit' => 20));
+
+ $breweries = array();
+ foreach($results['rows'] as $row) {
+ $doc = $cb->get($row['id']);
+ if($doc) {
+ $breweries[] = array(
+ 'name' => $row['key'],
+ 'id' => $row['id']
+ );
+ }
+
+ }
+
+ return $app['twig']->render(
+ 'breweries/index.twig.html',
+ compact('breweries')
+ );
+});
+
+// Delete Beer (GET /beers/delete/<ID>)
+
+
+// Delete Brewery (GET /breweries/delete/<ID>)
+
+
+// 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,
+ 'startkey' => $input,
+ 'endkey' => $input . '\uefff'
+ );
+ $results = $cb->view("beer", "by_name", $options);
+
+ $beers = array();
+ foreach($results['rows'] as $row) {
+ $doc = $cb->get($row['id']);
+ if($doc) {
+ $doc = json_decode($doc, true);
+ $beers[] = array(
+ 'name' => $doc['name'],
+ 'brewery' => $doc['brewery_id'],
+ 'id' => $row['id']
+ );
+ }
+
+ }
+
+ return $app->json($beers, 200);
+});
+
+// Search via AJAX for breweries (GET /breweries/search)
+$app->get('/breweries/search', function(Request $request) use ($app, $cb) {
+ $input = strtolower($request->query->get('value'));
+
+ $options = array(
+ 'limit' => 20,
+ 'startkey' => $input,
+ 'endkey' => $input . '\uefff'
+ );
+ $results = $cb->view("brewery", "by_name", $options);
+
+ $breweries = array();
+ foreach($results['rows'] as $row) {
+ $doc = $cb->get($row['id']);
+ if($doc) {
+ $doc = json_decode($doc, true);
+ $breweries[] = array(
+ 'name' => $doc['name'],
+ 'id' => $row['id']
+ );
+ }
+
+ }
+
+ return $app->json($breweries, 200);
+});
+
+// Run the Application
+$app->run();
+?>
@@ -0,0 +1,31 @@
+{% extends "layout.twig.html" %}
+
+{% block content %}
+<h3>Browse Beers</h3>
+
+<form class="navbar-search pull-left">
+ <input id="beer-search" type="text" class="search-query" placeholder="Search for Beers">
+</form>
+
+<table id="beer-table" class="table table-striped">
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Brewery</th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for beer in beers %}
+ <tr>
+ <td><a href="/beersample-php/beers/show/{{beer.id}}">{{beer.name}}</a></td>
+ <td><a href="/beersample-php/breweries/show/{{beer.brewery}}">To Brewery</a></td>
+ <td>
+ <a class="btn btn-small btn-warning" href="/beersample-php/beers/edit/{{beer.id}}">Edit</a>
+ <a class="btn btn-small btn-danger" href="/beersample-php/beers/delete/{{beer.id}}">Delete</a>
+ </td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+{% endblock %}
@@ -0,0 +1,29 @@
+{% extends "layout.twig.html" %}
+
+{% block content %}
+<h3>Browse Breweries</h3>
+
+<form class="navbar-search pull-left">
+ <input id="brewery-search" type="text" class="search-query" placeholder="Search for Breweries">
+</form>
+
+<table id="brewery-table" class="table table-striped">
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for brewery in breweries %}
+ <tr>
+ <td><a href="/beersample-php/breweries/show/{{brewery.id}}">{{brewery.name}}</a></td>
+ <td>
+ <a class="btn btn-small btn-warning" href="/beersample-php/breweries/edit/{{brewery.id}}">Edit</a>
+ <a class="btn btn-small btn-danger" href="/beersample-php/breweries/delete/{{brewery.id}}">Delete</a>
+ </td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+{% endblock %}
Oops, something went wrong.

0 comments on commit 2ef499e

Please sign in to comment.