Revision [17408]

This is an old revision of WikkaCakeExample made by BrianKoontz on 2007-08-12 03:25:38.

 

A WikkaCake Example


See also: WikkaCake


Overview

If you have yet to read the WikkaCake page, I'd suggest starting there first. This is actually an extension of that page, and provides an example of a completely self-contained Wikka action that has been written using the CakePHP framework. This is not a Cake tutorial. Instead, I will simply provide the code as-is and provide comments that are Wikka-specific.

If one has already set up and tested an embedded WikkaCake environment, then this application should run unmodified (in fact, it is the exact code that's currently running on my own machine).

All directory names are referenced from the top-level "caketest" directory.

This application is a simple database management app for saving, modifying, and deleting servers. It was developed for managing the public nameserver DB on the OpenNIC wiki. A publicly accessible version will be available shortly once testing is completed.

config/database.php
	var $default = array('driver' => 'mysql',
								'connect' => 'mysql_pconnect',
								'host' => 'localhost',
								'login' => 'root',
								'password' => 'root',
								'database' => 'wikka_cake',
								'prefix' => '');

Comments: This is the setup I use on my personal machine (no, it's not externally accessible, so I'm not giving away any state secrets here). Obviously, you will need to modify accordingly. If you haven't done so, simply copy config/database.php.default to config/database.php and modify that. Eventually, this will go away once I work on seamlessly accessing the Wikka DB authentication params.

public_access_servers.sql
CREATE TABLE IF NOT EXISTS public_access_servers (
	id              INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
	ip_addr         VARCHAR(15) NOT NULL,
	internic_hn     VARCHAR(50),
	opennic_hn      VARCHAR(50),
	owner           VARCHAR(50) NOT NULL,
	city            VARCHAR(50),
	state           CHAR(2),
	country         CHAR(2) NOT NULL,
	email           VARCHAR(50),
	last_verified   DATE,
	comments        LONGTEXT);

Comments: This is the DB schema upon which the following code is based. If you modify this, there's a good chance you'll need to modify one or more files below. Note that Cake expect, by default, a primary key called "id".

models/server.php
<?php
	class Server extends AppModel
	{
		var $name = 'Server';
		var $useTable = "public_access_servers";

		var $validate = array(
			'ip_addr' => VALID_NOT_EMPTY,
			'owner' => VALID_NOT_EMPTY,
			'country' => VALID_NOT_EMPTY
		);
	}
?>

Comments: $useTable reflects that fact that I've named my DB table in a non-conforming way.

controllers/servers_controller.php
%%
<?php
{
var $name = 'Servers';
var $layout = 'default';
var $wikka = $this->params['wakka'];
var $scaffold;

function index()
{
$this->set('servers', $this->Server->findAll());
}
function add()
{
{
$this->redirect($this->base);
}
if(true isset($this->data['Servers']['action']) &&
false! $this->params['wakka']->IsAdmin())
{
$this->redirect($this->base);
}

$id = null;
if(isset($this->params['url']['id']))
{
$id = $this->params['url']['id'];
}
if(empty($this->data))
{
$this->Server->id = $id;
$this->data = $this->Server->read();
}
else
{
uses('sanitize');
$sanitize = new Sanitize();
if(empty($this->data))
{
$this->render();
}
if(!empty($this->data))
{
$sanitize->cleanArray($this->data);
if($this->Server->save($this->data))
{
$this->flash('Your entry has been saved.', , 1);
}
else
{
$this->set('errorMessage', 'Please correct errors below.');
$this->render();
}
}
}
}
}
?>
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki