Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP-GTK/CouchDB PoC code to set status of an in/out board
<?php
$couch_dsn = "http://localhost:5984/";
$couch_db = "inout";
// libraries come from http://github.com/dready92/PHP-on-Couch
require_once "../lib/couch.php";
require_once "../lib/couchClient.php";
require_once "../lib/couchDocument.php";
$client = new couchClient($couch_dsn,$couch_db);
class InOutGtk extends GtkWindow {
public function __construct() {
parent::__construct();
$this->set_default_size(400,100);
$this->set_title('In/Out Board');
$this->connect_simple('destroy', array('gtk', 'main_quit'));
$table = new GtkTable(4,2);
$this->add($table);
$lblName = new GtkLabel("Name:");
$txtName = new GtkEntry($_SERVER["USER"]);
$btnIn = new GtkButton('In');
$btnIn->connect('clicked', array($this, 'statusButtonClick'), $txtName);
$btnOut = new GtkButton('Out');
$btnOut->connect('clicked', array($this, 'statusButtonClick'), $txtName);
$table->attach($lblName, 0, 1, 0, 1);
$table->attach($txtName, 1, 4, 0, 1);
$table->attach($btnIn, 2, 3, 1, 2);
$table->attach($btnOut, 3, 4, 1, 2);
$this->show_all();
}
public function statusButtonClick($button,$uname) {
// send message that we are in
echo $uname->get_text()." ".$button->get_label()."\n";
sendStatus($button->get_label(), $uname->get_text());
}
}
function alert($msg) {
$dialog = new GtkDialog('Alert', null, Gtk::DIALOG_MODAL); // create a new dialog
$dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
$top_area = $dialog->vbox;
$top_area->pack_start($hbox = new GtkHBox());
$stock = GtkImage::new_from_stock(Gtk::STOCK_DIALOG_WARNING,
Gtk::ICON_SIZE_DIALOG);
$hbox->pack_start($stock, 0, 0); // stuff in the icon
$hbox->pack_start(new GtkLabel($msg)); // and the msg
$dialog->add_button(Gtk::STOCK_OK, Gtk::RESPONSE_OK);
$dialog->set_has_separator(false); // don't display the set_has_separator
$dialog->show_all(); // show the dialog
$dialog->run(); // the dialog in action
$dialog->destroy(); // done. close the dialog box.
}
function sendStatus($status, $uname) {
global $client;
if ($uname=='') {
alert("Please enter your name!"); //popup message box
return(1);
}
try {
$doc = $client->getDoc($uname);
} catch (Exception $e) {
if($e->getCode()==404) {
$doc = new couchDocument($client);
$doc->set( array('_id'=>$uname, 'status'=>$status));
alert("User didn't exist, created it and set status to $status");
return(0);
} else {
alert("Something weird happened :".$e->getMessage()." (errcode=".$e->getCode().")");
return(1);
}
}
$doc->status = $status;
try {
$client->storeDoc($doc);
} catch (Exception $e) {
alert("Document storage failed : ".$e->getMessage()."<BR>");
}
alert("Set status to $status");
}
new InOutGtk();
Gtk::main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment