From b43fd4df4452ca8fce542d029eac15f11f4d90de Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Sun, 29 Jun 2008 01:31:04 +0000 Subject: [PATCH] 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 --- classes/Controller.php | 5 +- classes/Mail.php | 43 +++++++++ config/verynicenoise.com.xml | 7 ++ jLib.php | 4 +- smarty/functions/function.contact_form.php | 19 ++++ .../function.draw_pagination.php | 0 .../{ => functions}/function.pagination.php | 0 smarty/templates/ajax.tpl | 1 + static/css/messages.css | 39 ++++++++ static/js/ajax.js | 91 +++++++++++++++++++ 10 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 classes/Mail.php create mode 100644 smarty/functions/function.contact_form.php rename smarty/{ => functions}/function.draw_pagination.php (100%) rename smarty/{ => functions}/function.pagination.php (100%) create mode 100644 smarty/templates/ajax.tpl create mode 100644 static/css/messages.css create mode 100644 static/js/ajax.js diff --git a/classes/Controller.php b/classes/Controller.php index ca70e0c..8e504b0 100644 --- a/classes/Controller.php +++ b/classes/Controller.php @@ -88,9 +88,10 @@ class Controller { $smarty->assign('session', $_SESSION); } - // Load it up + // Load it up! 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() { diff --git a/classes/Mail.php b/classes/Mail.php new file mode 100644 index 0000000..e22ca27 --- /dev/null +++ b/classes/Mail.php @@ -0,0 +1,43 @@ +assign('type', $type); + $smarty->assign('message', $message); + } +} + +?> diff --git a/config/verynicenoise.com.xml b/config/verynicenoise.com.xml index fad7d7d..5aa4766 100755 --- a/config/verynicenoise.com.xml +++ b/config/verynicenoise.com.xml @@ -25,4 +25,11 @@ User Account Logout + + VNN + + verynicenoise@gmail.com + joshsherman@gmail.com + + diff --git a/jLib.php b/jLib.php index d05bac5..3d2b962 100755 --- a/jLib.php +++ b/jLib.php @@ -51,7 +51,7 @@ if (Config::getSession() && !isset($_SESSION)) { // Smarty default stuff if (Config::getSmarty()) { - require_once 'contrib/smarty/Smarty.class.php'; + require_once 'contrib/smarty/libs/Smarty.class.php'; $smarty = new Smarty(); @@ -71,7 +71,7 @@ if (Config::getSmarty()) { $smarty->load_filter('output','trimwhitespace'); // Include custom Smarty functions - $directory = JLIB_PATH . 'smarty/'; + $directory = JLIB_PATH . 'smarty/functions/'; if (is_dir($directory)) { if ($handle = opendir($directory)) { while (($file = readdir($handle)) !== false) { diff --git a/smarty/functions/function.contact_form.php b/smarty/functions/function.contact_form.php new file mode 100644 index 0000000..70f144f --- /dev/null +++ b/smarty/functions/function.contact_form.php @@ -0,0 +1,19 @@ + + Email:
+

+ Subject:
+

+ Message:
+

+ + + '; + + return $form; +} + +?> diff --git a/smarty/function.draw_pagination.php b/smarty/functions/function.draw_pagination.php similarity index 100% rename from smarty/function.draw_pagination.php rename to smarty/functions/function.draw_pagination.php diff --git a/smarty/function.pagination.php b/smarty/functions/function.pagination.php similarity index 100% rename from smarty/function.pagination.php rename to smarty/functions/function.pagination.php diff --git a/smarty/templates/ajax.tpl b/smarty/templates/ajax.tpl new file mode 100644 index 0000000..0ba68e9 --- /dev/null +++ b/smarty/templates/ajax.tpl @@ -0,0 +1 @@ +{$type}|{$message} diff --git a/static/css/messages.css b/static/css/messages.css new file mode 100644 index 0000000..9060d02 --- /dev/null +++ b/static/css/messages.css @@ -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; +} diff --git a/static/js/ajax.js b/static/js/ajax.js new file mode 100644 index 0000000..c481f8f --- /dev/null +++ b/static/js/ajax.js @@ -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); + } +}