mirror of
https://github.com/ACSPRI/queXS
synced 2024-04-02 12:12:16 +00:00
Import from DCARF SVN
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/**
|
||||
* Server that exposes a class for doing a fake guestbook
|
||||
*
|
||||
* @category HTML
|
||||
* @package AJAX
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @version Release: 0.5.2
|
||||
* @link http://pear.php.net/package/HTML_AJAX
|
||||
*/
|
||||
|
||||
// include the server class
|
||||
include 'HTML/AJAX/Server.php';
|
||||
|
||||
|
||||
// extend HTML_AJAX_Server creating our own custom one with init{ClassName} methods for each class it supports calls on
|
||||
class GuestbookServer extends HTML_AJAX_Server {
|
||||
// this flag must be set to on init methods
|
||||
var $initMethods = true;
|
||||
|
||||
// init method for the test class, includes needed files an registers it for ajax
|
||||
function initGuestbook() {
|
||||
include 'guestbook.class.php';
|
||||
$this->registerClass(new Guestbook(),'guestbook',array('newEntry', 'clearGuestbook', 'deleteEntry', 'editEntry', 'updateSelect')); // specify methods so that we get case in php4
|
||||
}
|
||||
}
|
||||
|
||||
session_start();
|
||||
// create an instance of our test server
|
||||
$server = new GuestbookServer();
|
||||
|
||||
// handle requests as needed
|
||||
$server->handleRequest();
|
||||
?>
|
||||
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
/**
|
||||
* Guestbook uses HTML_AJAX_Action class to interact with the page - the
|
||||
* javascript is all written from here
|
||||
*
|
||||
* @category HTML
|
||||
* @package AJAX
|
||||
* @author Elizabeth Smith <auroraeosrose@gmail.com>
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @version Release: 0.5.2
|
||||
* @link http://pear.php.net/package/HTML_AJAX
|
||||
*/
|
||||
|
||||
/**
|
||||
* Require the action class
|
||||
*/
|
||||
require_once 'HTML/AJAX/Action.php';
|
||||
|
||||
class guestbook {
|
||||
|
||||
// constructor won't be exported
|
||||
function guestbook() {
|
||||
if (!isset($_SESSION['entries'])) {
|
||||
$_SESSION['entries'] = array();
|
||||
}
|
||||
}
|
||||
|
||||
// data is an array of objects
|
||||
function newEntry($data) {
|
||||
//validation code is identical
|
||||
$response = new HTML_AJAX_Action();
|
||||
//remove any error nodes present
|
||||
$response->removeNode('nameError');
|
||||
$response->removeNode('emailError');
|
||||
$response->removeNode('emailError2');
|
||||
$response->removeNode('commentError');
|
||||
//checking data
|
||||
if(!isset($data['name']) or empty($data['name']))
|
||||
{
|
||||
//create error div after bad name node
|
||||
$response->createNode('name', 'div', array('class' => 'error', 'innerHTML' => 'Name is a required field', 'id' => 'nameError'), 'insertAfter');
|
||||
$error = TRUE;
|
||||
}
|
||||
if(!isset($data['email']) or empty($data['email']))
|
||||
{
|
||||
//create error div after bad name node
|
||||
$response->createNode('email', 'div', array('class' => 'error', 'innerHTML' => 'Email is a required field', 'id' => 'emailError'), 'insertAfter');
|
||||
$error = TRUE;
|
||||
}
|
||||
if($this->_checkEmail($data['email']) != TRUE)
|
||||
{
|
||||
//create error div after bad name node
|
||||
$response->createNode('email', 'div', array('class' => 'error', 'innerHTML' => 'That email address is incorrect', 'id' => 'emailError2'), 'insertAfter');
|
||||
$error = TRUE;
|
||||
}
|
||||
if(!isset($data['comments']) or empty($data['comments']))
|
||||
{
|
||||
//create error div after bad name node
|
||||
$response->createNode('comments', 'div', array('class' => 'error', 'innerHTML' => 'Comment is a required field', 'id' => 'commentError'), 'insertAfter');
|
||||
$error = TRUE;
|
||||
}
|
||||
if(!isset($error))
|
||||
{
|
||||
//clean name - strip tags and html_entity it :)
|
||||
$data['name'] = htmlentities(strip_tags($data['name']));
|
||||
//clean email - strip tags it
|
||||
$data['email'] = strip_tags($data['email']);
|
||||
//clean website - strip http://if needed
|
||||
$data['website'] = strip_tags($data['website']);
|
||||
if(strpos($data['website'], 'http://') === 0)
|
||||
{
|
||||
$data['website'] = str_replace('http://', '', $data['website']);
|
||||
}
|
||||
//clean like name
|
||||
$data['comments'] = htmlentities(strip_tags($data['comments']));
|
||||
//branch here depending on if form is new
|
||||
if($data['submit'] == 'Edit Entry')
|
||||
{
|
||||
$old = $_SESSION['entries'][$data['key']];
|
||||
//merge new data over old
|
||||
foreach($data as $key => $value)
|
||||
{
|
||||
$old[$key] = $value;
|
||||
}
|
||||
$_SESSION['entries'][$data['key']] = $old;
|
||||
//replace div innerHTML, fun fun
|
||||
$response->assignAttr('entry'.$data['key'], 'innerHTML', $this->_makeDiv($data['key'], $_SESSION['entries'][$data['key']], TRUE));
|
||||
//remove hidden input
|
||||
$response->removeNode('key');
|
||||
}
|
||||
else
|
||||
{
|
||||
$data['date'] = date('j/n/Y, h:i');
|
||||
$_SESSION['entries'][] = $data;
|
||||
end($_SESSION['entries']);
|
||||
$key = key($_SESSION['entries']);
|
||||
$response->prependAttr('guestbookList', 'innerHTML', $this->_makeDiv($key, $data));
|
||||
}
|
||||
//reset the form
|
||||
$response->assignAttr('name', 'value', '');
|
||||
$response->assignAttr('email', 'value', '');
|
||||
$response->assignAttr('website', 'value', '');
|
||||
$response->assignAttr('comments', 'value', '');
|
||||
$response->assignAttr('submit', 'value', 'Add Comments');
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
// empty the guestbook
|
||||
function clearGuestbook($data) {
|
||||
$_SESSION['entries'] = array();
|
||||
$response = new HTML_AJAX_Action();
|
||||
$response->insertAlert('You will clear all entries, this cannot be undone!');
|
||||
$response->assignAttr('guestbookList', 'innerHTML', '');
|
||||
return $response;
|
||||
}
|
||||
|
||||
// delete an entry from the guestbook
|
||||
function deleteEntry($id) {
|
||||
unset($_SESSION[$id]);
|
||||
$response = new HTML_AJAX_Action();
|
||||
$response->removeNode('entry'.$id);
|
||||
return $response;
|
||||
}
|
||||
|
||||
// puts a guestbook entry back in
|
||||
function editEntry($id) {
|
||||
$data = $_SESSION['entries'][$id];
|
||||
$response = new HTML_AJAX_Action();
|
||||
//send to the form
|
||||
$response->assignAttr('name', 'value', $data['name']);
|
||||
$response->assignAttr('email', 'value', $data['email']);
|
||||
$response->assignAttr('website', 'value', $data['website']);
|
||||
$response->assignAttr('comments', 'value', $data['comments']);
|
||||
$response->assignAttr('submit', 'value', 'Edit Entry');
|
||||
$response->createNode('submit', 'input', array('id' => 'key', 'name' => 'key', 'type' => 'hidden', 'value' => $id), 'insertBefore');
|
||||
return $response;
|
||||
}
|
||||
|
||||
function updateSelect($id)
|
||||
{
|
||||
$response = new HTML_AJAX_Action();
|
||||
$attr = array('id' => $id, 'name' => $id);
|
||||
$response->replaceNode($id, 'select', $attr);
|
||||
for ($i=1;$i<=10;$i++)
|
||||
{
|
||||
$attr = array('value' => $i, 'innerHTML' => 'Option ' . $i);
|
||||
$response->createNode($id, 'option', $attr, 'append');
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
function _makeDiv($key, $data, $replace = FALSE) {
|
||||
$div = '';
|
||||
if($replace == FALSE)
|
||||
{
|
||||
$div .= '<div class="entry" id="entry'.$key.'">';
|
||||
}
|
||||
$div .= '<h3><a href="mailto:'.$data['email'].'">'.$data['name'].'</a></h3>';
|
||||
if(!empty($data['website']))
|
||||
{
|
||||
$div .= '<a href="http://'.$data['website'].'">'.$data['website'].'</a><br />';
|
||||
}
|
||||
$div .= '<p>'.$data['comments'].'</p>'
|
||||
.'<div class="small">Posted: '.$data['date'].' | '
|
||||
.'<a href="#" onclick="editentry('.$key.');">Edit</a> | '
|
||||
.'<a href="#" onclick="deleteentry('.$key.');">Delete</a></div>';
|
||||
if($replace == FALSE)
|
||||
{
|
||||
$div .= '</div>';
|
||||
}
|
||||
return $div;
|
||||
}
|
||||
|
||||
function _checkEmail($email)
|
||||
{
|
||||
//checks proper syntax
|
||||
if(preg_match( '/^[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}$/i' , $email))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
?>
|
||||
202
include/pear/docs/HTML_AJAX/examples/guestbook/index.php
Normal file
202
include/pear/docs/HTML_AJAX/examples/guestbook/index.php
Normal file
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
/**
|
||||
* A simple guestbook with the goal of not a line of javascript :)
|
||||
*
|
||||
* @category HTML
|
||||
* @package AJAX
|
||||
* @author Elizabeth Smith <auroraeosrose@gmail.com>
|
||||
* @copyright 2005 Elizabeth Smith
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @version Release: 0.5.2
|
||||
* @link http://pear.php.net/package/HTML_AJAX
|
||||
*/
|
||||
|
||||
//require the helper class - it will take care of everything else
|
||||
require_once 'HTML/AJAX/Helper.php';
|
||||
|
||||
//since we're not REALLY using a backend like a database, use sessions to store data
|
||||
session_start();
|
||||
//set up HTML_AJAX_Helper
|
||||
$ajaxHelper = new HTML_AJAX_Helper();
|
||||
//tell it what url to use for the server
|
||||
$ajaxHelper->serverUrl = 'auto_server.php';
|
||||
//add haserializer to set
|
||||
$ajaxHelper->jsLibraries[] = 'haserializer';
|
||||
// Open tags problem... I know ugly.
|
||||
$ajaxHelper->stubs[] = 'guestbook';
|
||||
|
||||
print '<?xml version="1.0" encoding="utf-8"?>';
|
||||
?>
|
||||
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN
|
||||
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>My Guestbook</title>
|
||||
<?php
|
||||
// output a javascript neded to setup HTML_AJAX
|
||||
echo $ajaxHelper->setupAJAX();
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
HTML_AJAX.onError = function(e) { alert(HTML_AJAX_Util.quickPrint(e)); }
|
||||
</script>
|
||||
<style type="text/css">
|
||||
body {
|
||||
color: #24006B;
|
||||
}
|
||||
h2 {
|
||||
text-align: center;
|
||||
color: #330099;
|
||||
}
|
||||
#guestbookForm, #guestbookList {
|
||||
width: 65%;
|
||||
margin-right: auto;
|
||||
margin-left: auto;
|
||||
padding: 1.5em;
|
||||
margin-top: 10px;
|
||||
background-color: #D5BFFF;
|
||||
border: 4px double #FFCC00;
|
||||
}
|
||||
fieldset, div.entry {
|
||||
background-color: #FFF2BF;
|
||||
border: 4px double #330099;
|
||||
padding: 0.5em;
|
||||
}
|
||||
legend {
|
||||
color: #330099;
|
||||
font-size: 0.8em;
|
||||
font-style: italic;
|
||||
}
|
||||
label {
|
||||
clear: both;
|
||||
display: block;
|
||||
float: left;
|
||||
width: 20%;
|
||||
text-align: right;
|
||||
font-weight: bold;
|
||||
}
|
||||
input {
|
||||
display: block;
|
||||
float: left;
|
||||
width: 40%;
|
||||
margin: 0 0.5em 0.5em 0.5em;
|
||||
background-color: #B38F00;
|
||||
border: 1px solid #AA80FF;
|
||||
color: #330099;
|
||||
}
|
||||
input:focus, textarea:focus {
|
||||
background-color: #D5BFFF;
|
||||
border: 1px solid #B38F00;
|
||||
}
|
||||
textarea {
|
||||
display: block;
|
||||
float: left;
|
||||
width: 40%;
|
||||
height: 10em;
|
||||
margin: 0 0.5em 0.5em 0.5em;
|
||||
background-color: #B38F00;
|
||||
border: 1px solid #AA80FF;
|
||||
color: #330099;
|
||||
}
|
||||
input[type="submit"] {
|
||||
display: block;
|
||||
width: auto;
|
||||
float: none;
|
||||
margin-right: auto;
|
||||
margin-left: auto;
|
||||
font-size: 1.5em;
|
||||
font-weight: bold;
|
||||
background-color: #330099;
|
||||
color: #FFCC00;
|
||||
border: 3px double #FFE680;
|
||||
}
|
||||
.error {
|
||||
color: #CC0000;
|
||||
font-weight: bold;
|
||||
float: left;
|
||||
}
|
||||
.small {
|
||||
font-size: 0.8em
|
||||
}
|
||||
</style>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<?php //eventually ajax_helper should set this up for you, it's not done yet?>
|
||||
<script type="text/javascript">
|
||||
function sendguestbook(form) {
|
||||
var remoteguestbook = new guestbook();
|
||||
var payload = new Object();
|
||||
for(var i = 0; i < form.elements.length; i++) {
|
||||
if (form.elements[i].name) {
|
||||
|
||||
payload[form.elements[i].name] = form.elements[i].value;
|
||||
}
|
||||
}
|
||||
remoteguestbook.newEntry(payload);
|
||||
return false;
|
||||
}
|
||||
function clearguestbook() {
|
||||
var remoteguestbook = new guestbook();
|
||||
remoteguestbook.clearGuestbook();
|
||||
}
|
||||
function deleteentry(id) {
|
||||
var remoteguestbook = new guestbook();
|
||||
remoteguestbook.deleteEntry(id);
|
||||
}
|
||||
function editentry(id) {
|
||||
var remoteguestbook = new guestbook();
|
||||
remoteguestbook.editEntry(id);
|
||||
}
|
||||
function updateselect(id) {
|
||||
var remoteguestbook = new guestbook();
|
||||
remoteguestbook.updateSelect(id);
|
||||
}
|
||||
</script>
|
||||
<h2>Welcome to the Guestbook</h2>
|
||||
<div id="guestbookList">
|
||||
<?php
|
||||
|
||||
if (isset($_SESSION['entries'])) {
|
||||
foreach($_SESSION['entries'] as $key => $data) {
|
||||
echo '<div class="entry" id="entry'.$key.'">'
|
||||
.'<h3><a href="mailto:'.$data->email.'">'.$data->name.'</a></h3>';
|
||||
if(!empty($data->website))
|
||||
{
|
||||
echo '<a href="http://'.$data->website.'">'.$data->website.'</a><br />';
|
||||
}
|
||||
echo '<p>'.$data->comments.'</p>'
|
||||
.'<div class="small">Posted: '.$data->date.' | '
|
||||
.'<a href="#" onclick="editentry('.$key.');">Edit</a> | '
|
||||
.'<a href="#" onclick="deleteentry('.$key.');">Delete</a></div>'
|
||||
.'</div>';
|
||||
}
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
|
||||
<div><a href="#" onclick="clearguestbook(this);">Clear Guestbook</a></div>
|
||||
<form id="guestbookForm" action="index.php" method="post" onsubmit="sendguestbook(this); return false;">
|
||||
<fieldset>
|
||||
<legend>Leave Your Comments</legend>
|
||||
<label for="name">Name: </label><input name="name" id="name" />
|
||||
<label for="email">Email: </label><input name="email" id="email" />
|
||||
<label for="website">Website: </label><input name="website" id="website" />
|
||||
<label for="comments">Comments: </label><textarea name="comments" id="comments"></textarea>
|
||||
<br style="clear: both" />
|
||||
<input type="submit" id="submit" name="submit" value="Add Comments" />
|
||||
</fieldset>
|
||||
</form>
|
||||
|
||||
<p>Fill a select item with a list of options - tests the HTML_AJAX_Action::replaceNode and HTML_AJAX_Action::createNode methods</p>
|
||||
<form id="testing" action="index.php" method="post" onsubmit="return false;">
|
||||
<div>
|
||||
<a href="#" onclick="updateselect('replaceme');">Gimme some options</a>
|
||||
<select id="replaceme">
|
||||
<option name="dog" id="dog">Dog</option>
|
||||
</select>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user