Crazy updates. Moved the Smarty functions to a dedicated folder, added shared Smarty templates, updated the VNN config file, added more shared CSS, added a mail class, added fairly common AJAX too!
git-svn-id: http://svn.cleancode.org/svn/pickles@26 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
parent
3e913ece7f
commit
b43fd4df44
10 changed files with 205 additions and 4 deletions
|
@ -88,9 +88,10 @@ class Controller {
|
||||||
$smarty->assign('session', $_SESSION);
|
$smarty->assign('session', $_SESSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load it up
|
// Load it up!
|
||||||
header('Content-type: text/html; charset=UTF-8');
|
header('Content-type: text/html; charset=UTF-8');
|
||||||
$smarty->display('index.tpl');
|
// @todo
|
||||||
|
$smarty->display(isset($_REQUEST['ajax']) ? '/var/www/josh/common/smarty/templates/ajax.tpl' : 'index.tpl');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function authenticate() {
|
private function authenticate() {
|
||||||
|
|
43
classes/Mail.php
Normal file
43
classes/Mail.php
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class Mail {
|
||||||
|
|
||||||
|
static function send($recipients = null, $prefix = null) {
|
||||||
|
|
||||||
|
global $smarty;
|
||||||
|
|
||||||
|
$defaults = Config::get('contact');
|
||||||
|
|
||||||
|
if (!isset($recipients)) {
|
||||||
|
$recipients = $defaults['recipients']['recipient'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($recipients)) {
|
||||||
|
$to = null;
|
||||||
|
foreach ($recipients as $recipient) {
|
||||||
|
$to .= (isset($to) ? ',' : '') . $recipient;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$to = $recipients;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($prefix)) {
|
||||||
|
$prefix = isset($defaults['prefix']) ? $defaults['prefix'] : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mail($to, str_replace("\n", '', (isset($prefix) ? "[{$prefix}] " : '') . $_REQUEST['subject']), stripslashes($_REQUEST['message']), "From: {$_REQUEST['email']}\r\n")) {
|
||||||
|
$type = 'success';
|
||||||
|
$message = 'Message sent successfully';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$type = 'error';
|
||||||
|
$message = 'An unexpected error has occurred';
|
||||||
|
}
|
||||||
|
|
||||||
|
$smarty->assign('type', $type);
|
||||||
|
$smarty->assign('message', $message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
|
@ -25,4 +25,11 @@
|
||||||
<user>User Account</user>
|
<user>User Account</user>
|
||||||
<logout>Logout</logout>
|
<logout>Logout</logout>
|
||||||
</admin>
|
</admin>
|
||||||
|
<contact>
|
||||||
|
<prefix>VNN</prefix>
|
||||||
|
<recipients>
|
||||||
|
<recipient>verynicenoise@gmail.com</recipient>
|
||||||
|
<recipient>joshsherman@gmail.com</recipient>
|
||||||
|
</recipients>
|
||||||
|
</contact>
|
||||||
</config>
|
</config>
|
||||||
|
|
4
jLib.php
4
jLib.php
|
@ -51,7 +51,7 @@ if (Config::getSession() && !isset($_SESSION)) {
|
||||||
|
|
||||||
// Smarty default stuff
|
// Smarty default stuff
|
||||||
if (Config::getSmarty()) {
|
if (Config::getSmarty()) {
|
||||||
require_once 'contrib/smarty/Smarty.class.php';
|
require_once 'contrib/smarty/libs/Smarty.class.php';
|
||||||
|
|
||||||
$smarty = new Smarty();
|
$smarty = new Smarty();
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ if (Config::getSmarty()) {
|
||||||
$smarty->load_filter('output','trimwhitespace');
|
$smarty->load_filter('output','trimwhitespace');
|
||||||
|
|
||||||
// Include custom Smarty functions
|
// Include custom Smarty functions
|
||||||
$directory = JLIB_PATH . 'smarty/';
|
$directory = JLIB_PATH . 'smarty/functions/';
|
||||||
if (is_dir($directory)) {
|
if (is_dir($directory)) {
|
||||||
if ($handle = opendir($directory)) {
|
if ($handle = opendir($directory)) {
|
||||||
while (($file = readdir($handle)) !== false) {
|
while (($file = readdir($handle)) !== false) {
|
||||||
|
|
19
smarty/functions/function.contact_form.php
Normal file
19
smarty/functions/function.contact_form.php
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
function smarty_function_contact_form($params, &$smarty) {
|
||||||
|
$form = '
|
||||||
|
<form action="/contact/send" method="post">
|
||||||
|
Email:<br />
|
||||||
|
<input name="email" title="required" class="contact_input" /><br /><br />
|
||||||
|
Subject:<br />
|
||||||
|
<input name="subject" title="required" class="contact_input" /><br /><br />
|
||||||
|
Message:<br />
|
||||||
|
<textarea name="message" title="required" class="contact_textarea"></textarea><br /><br />
|
||||||
|
<input type="button" value="Send" onclick="ajaxSubmit(this.parentNode); return false;" class="contact_button" />
|
||||||
|
</form>
|
||||||
|
';
|
||||||
|
|
||||||
|
return $form;
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
1
smarty/templates/ajax.tpl
Normal file
1
smarty/templates/ajax.tpl
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{$type}|{$message}
|
39
static/css/messages.css
Normal file
39
static/css/messages.css
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
div.error,
|
||||||
|
div.warning,
|
||||||
|
div.success,
|
||||||
|
div.notice
|
||||||
|
{
|
||||||
|
border: 1px solid;
|
||||||
|
font-weight: bold;
|
||||||
|
margin: 0;
|
||||||
|
padding: 4px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.error
|
||||||
|
{
|
||||||
|
background: #FCC;
|
||||||
|
border-color: #900;
|
||||||
|
color: #900;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.warning
|
||||||
|
{
|
||||||
|
background: #FC9;
|
||||||
|
border-color: #F30;
|
||||||
|
color: #F30;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.success
|
||||||
|
{
|
||||||
|
background: #CFC;
|
||||||
|
border-color: #090;
|
||||||
|
color: #090;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.notice
|
||||||
|
{
|
||||||
|
background: #CCF;
|
||||||
|
border-color: #009;
|
||||||
|
color: #009;
|
||||||
|
}
|
91
static/js/ajax.js
Normal file
91
static/js/ajax.js
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
var request = null;
|
||||||
|
|
||||||
|
function getForm(form) {
|
||||||
|
var params = 'ajax=true';
|
||||||
|
var count = form.elements.length;
|
||||||
|
|
||||||
|
for (var i = 0; i < count; i++) {
|
||||||
|
element = form.elements[i];
|
||||||
|
|
||||||
|
switch (element.type) {
|
||||||
|
case 'hidden':
|
||||||
|
case 'password':
|
||||||
|
case 'text':
|
||||||
|
case 'textarea':
|
||||||
|
// Check if it's required
|
||||||
|
if (element.title == 'required' && element.value == '') {
|
||||||
|
alert('Error: The ' + element.name + ' field is required.');
|
||||||
|
element.focus();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
params += '&' + element.name + '=' + encodeURI(element.value);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'checkbox':
|
||||||
|
case 'radio':
|
||||||
|
if (element.checked) {
|
||||||
|
params += '&' + element.name + '=' + encodeURI(element.value);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'select-one':
|
||||||
|
params += '&' + element.name + "=" + element.options[element.selectedIndex].value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createRequest() {
|
||||||
|
try {
|
||||||
|
request = new XMLHttpRequest();
|
||||||
|
} catch (trymicrosoft) {
|
||||||
|
try {
|
||||||
|
request = new ActiveXObject("Msxml12.XMLHTTP");
|
||||||
|
} catch (othermicrosoft) {
|
||||||
|
try {
|
||||||
|
request = new ActiveXObject("Microsoft.XMLHTTP");
|
||||||
|
} catch (failed) {
|
||||||
|
request = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request == null) {
|
||||||
|
alert("Error creating request object!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function ajaxSubmit(form) {
|
||||||
|
var params = '';
|
||||||
|
|
||||||
|
if (params = getForm(form)) {
|
||||||
|
createRequest();
|
||||||
|
request.open(form.method, form.action, true);
|
||||||
|
|
||||||
|
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||||
|
request.setRequestHeader("Content-length", params.length);
|
||||||
|
request.setRequestHeader("Connection", "close");
|
||||||
|
|
||||||
|
request.onreadystatechange = function() {
|
||||||
|
if (request.readyState == 4 && request.status == 200) {
|
||||||
|
|
||||||
|
// We need to split the response because the response comes
|
||||||
|
// back in this format: type | message
|
||||||
|
// Where type could be error or success (and eventually warning)
|
||||||
|
var responseText = request.responseText;
|
||||||
|
var splitResponse = responseText.split('|');
|
||||||
|
|
||||||
|
var responseElement = document.createElement('div');
|
||||||
|
responseElement.className = splitResponse[0];
|
||||||
|
var responseMessage = document.createTextNode(splitResponse[1]);
|
||||||
|
responseElement.appendChild(responseMessage);
|
||||||
|
form.insertBefore(responseElement, form.firstChild);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
request.send(params);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue