cvs: presentations /slides/mvc/example add.php add_c.inc common.js ui.inc /slides/mvc/example/model example.db items.inc

From: Date: Tue, 21 Feb 2006 21:26:21 +0000
Subject: cvs: presentations /slides/mvc/example add.php add_c.inc common.js ui.inc /slides/mvc/example/model example.db items.inc
Groups: php.pres 
Request: Send a blank email to pres+get-1259@lists.php.net to get a copy of this message
rasmus Tue Feb 21 21:26:21 2006 UTC Modified files: /presentations/slides/mvc/example add.php add_c.inc common.js ui.inc /presentations/slides/mvc/example/model example.db items.inc Log: Update MVC example and check in the DB so rsync will take care of cleaning up the crap every hour

http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/add.php?r1=1.1&r2=1.2&diff_format=u Index: presentations/slides/mvc/example/add.php diff -u presentations/slides/mvc/example/add.php:1.1 presentations/slides/mvc/example/add.php:1.2 --- presentations/slides/mvc/example/add.php:1.1 Wed Jan 25 02:32:01 2006 +++ presentations/slides/mvc/example/add.php Tue Feb 21 21:26:21 2006 @@ -6,17 +6,13 @@ <div id="tItems" class="ta"> <table cellspacing="0" cellpadding="3" width="100%"> <?php foreach($items as $i=>$elem) { - $price = number_format($elem['price'],2); $s = $i%2; echo <<<EOB - <tr class="it$s" - onmouseover="this.className='its';" - onmouseout="this.className='it$s';" - onclick="postData('add.php','formName=fItem&load_item={$elem['id']}');"> + <tr id="{$elem['id']}" class="it$s"> <td>{$elem['id']}</td> <td>{$elem['sdesc']}</td> <td>{$elem['cat']}</td> - <td align="right">\$$price</td> + <td align="right">\${$elem['fprice']}</td> </tr> EOB; @@ -26,12 +22,18 @@ </div><br /> <form name="fItem" action="javascript:postForm('add.php','fItem')"> <input type="hidden" name="formName" value="fItem" /> - <input type="hidden" id="f_id" name="id" value="" /> - <?php field('Short Description','sdesc','text',$item, array('size'=>39))?> - <?php field('Category','cat','select',$item,array('options'=>$categories))?> - <?php field('Price','price','text',$item,array('size'=>8,'dir'=>'RTL'))?> - <br /> - <?php field('Details','ldesc','textarea',$item)?><br /> + <input type="hidden" name="id" id="f_id" value="" /> + <input type="text" class="f" id="f_sdesc" name="sdesc" size="39" maxlength="128" value="Short Description" dir="LTR" /> + <select class="f" name="cat" id="f_cat" size="1"> + <option selected>Category</option> +<?php foreach($categories as $cat) echo <<<EOB + <option value="{$cat}">{$cat}</option> + +EOB; +?> + </select> + <input type="text" class="f" id="f_price" name="price" size="8" maxlength="128" value="Price" dir="RTL" /><br /> + <textarea style="width: 100%" name="ldesc" class="f" id="f_ldesc" rows="5" wrap="soft">Details</textarea><br /> <input name="f_submit" type="submit" value="Add Item" /> <input type="reset" value="Clear" /> </form> http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/add_c.inc?r1=1.1&r2=1.2&diff_format=u Index: presentations/slides/mvc/example/add_c.inc diff -u presentations/slides/mvc/example/add_c.inc:1.1 presentations/slides/mvc/example/add_c.inc:1.2 --- presentations/slides/mvc/example/add_c.inc:1.1 Wed Jan 25 02:32:01 2006 +++ presentations/slides/mvc/example/add_c.inc Tue Feb 21 21:26:21 2006 @@ -2,6 +2,7 @@ include './model/db.inc'; include './model/items.inc'; if($_SERVER['REQUEST_METHOD']=='POST') { + header("Content-type: application/json"); // Load an item entry from backend and send JSON request to populate form if(isset($_POST['load_item'])) { $entry = load_items($_POST['load_item']); @@ -37,6 +38,6 @@ // Initialize view data if(!isset($categories)) load_list('categories'); -if(!isset($item)) $item = array(); +if(!isset($item)) $item = array('cat'=>''); $items = load_items(); ?> http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/common.js?r1=1.1&r2=1.2&diff_format=u Index: presentations/slides/mvc/example/common.js diff -u presentations/slides/mvc/example/common.js:1.1 presentations/slides/mvc/example/common.js:1.2 --- presentations/slides/mvc/example/common.js:1.1 Wed Jan 25 02:32:01 2006 +++ presentations/slides/mvc/example/common.js Tue Feb 21 21:26:21 2006 @@ -1,66 +1,71 @@ // Catch JSON replies var fN = function callBack(o) { - if(o.readyState == 4) { - myDebug(o.responseText); - var resp = eval('(' + o.responseText + ')'); - if(resp['debug']) { - myDebug(resp["debug"]); - } - // Put up a Status message and fade it out - if(resp['status']) { - var el = document.getElementById(resp['elem']); - div = document.createElement("div"); - div.className='status'; - div.innerHTML = resp['status']; - pos = ygPos.getPos(el); - ygPos.setPos(div,[pos[0],pos[1]+40]); - document.body.appendChild(div); - fnc = function() { - if(resp['reset']) { document.forms[resp['formName']].reset(); } - window.location.reload(false); - } - fade(div,4,fnc); + myDebug(o.responseText); + var resp = eval('(' + o.responseText + ')'); + if(resp['debug']) { + myDebug(resp["debug"]); + } + // Put up a Status message and fade it out + if(resp['status']) { + div = document.createElement("div"); + div.className='status'; + div.innerHTML = resp['status']; + pos = YAHOO.util.Dom.getXY(resp['elem']); + div.style.visibility = 'hidden' + document.body.appendChild(div); + YAHOO.util.Dom.setXY(div,[pos[0],pos[1]+40]); + div.style.visibility = 'visible' + fnc = function() { + if(resp['reset']) { document.forms[resp['formName']].reset(); } + window.location.reload(false); } - // Flash the field that had the error - if(resp['validate_error']) { - var el = document.getElementById(resp['validate_error']); - var over = el.cloneNode(true); - over.style.zIndex=9999; - addClass(over,'error'); - ygPos.setPos(over,ygPos.getPos(el)); - document.body.appendChild(over); - fnc = function() { - over.parentNode.removeChild(over); - } - fade(over,0.5); unfade(over,0.5); - fade(over,0.5); unfade(over,0.5); - fade(over,3,fnc); - } - // Fill in formName - if(resp['load_item']) { - var oForm = document.forms[resp['formName']]; - for(var k in resp.load_item[0]) { - if(oForm['f_'+k]) { - if(resp.load_item[0][k].length) - oForm['f_'+k].value = resp.load_item[0][k]; - else oForm['f_'+k].value = oForm['desc_'+k].value; - } + fade(div,2,fnc); + } + // Flash the field that had the error + if(resp['validate_error']) { + var el = document.getElementById(resp['validate_error']); + var over = el.cloneNode(true); + over.style.position = 'absolute'; + over.style.zIndex=9999; + addClass(over,'error'); + over.style.visibility = 'hidden' + document.body.appendChild(over); + YAHOO.util.Dom.setXY(over, YAHOO.util.Dom.getXY(el)); + over.style.visibility = 'visible' + + // Whoa - nested onCompletes can look a bit confusing ;) + fade(over,0.5, function() { + unfade(over,0.5, function() { + fade(over,2, function() { + over.parentNode.removeChild(over); + } ); + } ); + } ); + } + // Fill in formName + if(resp['load_item']) { + var oForm = document.forms[resp['formName']]; + for(var k in resp.load_item[0]) { + if(oForm['f_'+k]) { + if(resp.load_item[0][k].length) + oForm['f_'+k].value = resp.load_item[0][k]; + else oForm['f_'+k].value = oForm['desc_'+k].value; } } - } + } } +var callback = { success:fN } + // Post form fields from formName to target function postForm(target,formName) { - var req_o = ygConn.getObject(); - ygConn.http.setForm(formName); - ygConn.http.asyncRequest(req_o,'POST',target,false,fN,null); + YAHOO.util.Connect.setForm(formName); + YAHOO.util.Connect.asyncRequest('POST',target,callback); } function postData(target,postData) { - var req_o = ygConn.getObject(); - ygConn.http.asyncRequest(req_o,'POST',target,false,fN,null,postData); + YAHOO.util.Connect.asyncRequest('POST',target,callback,postData); } function addClass(o,cls) { @@ -72,21 +77,99 @@ } function fade(o, dur, fnc) { - var a = [ 1, .3, 0 ]; - var oAnim = new ygAnim_Fade(o, dur, a); - if(fnc) oAnim.onComplete=fnc; - oAnim.setStart(1); + var oAnim = new YAHOO.util.Anim(o, {opacity: {from: 1, to: 0}}, dur); + if(fnc) oAnim.onComplete.subscribe(fnc); oAnim.animate(); } function unfade(o, dur, fnc) { - var a = [ 0, .7, 1 ]; - var oAnim = new ygAnim_Fade(o, dur, a); - if(fnc) oAnim.onComplete=fnc; - oAnim.setStart(0); + var oAnim = new YAHOO.util.Anim(o, {opacity: {from: 0, to: 1}}, dur); + if(fnc) oAnim.onComplete.subscribe(fnc); oAnim.animate(); } +// Javascript for the item table +function clickItemPost(event) { + var target = YAHOO.util.Event.getTarget(event, true); + if(target.nodeName=='TD') target = target.parentNode; + postData('add.php','formName=fItem&load_item=' + target.id); +} + +function mouseoverItem() { + this.className = 'its'; +} + +function mouseoutItemEven() { + this.className = 'it0'; +} + +function mouseoutItemOdd() { + this.className = 'it1'; +} + +function fancyItems() { + var oT = document.getElementById('tItems'); + var oTr = oT.getElementsByTagName('TR'); + for(var i=0; i<oTr.length; i++){ + YAHOO.util.Event.addListener(oTr[i], "mouseover", mouseoverItem); + YAHOO.util.Event.addListener(oTr[i], "mouseout", i%2 ? mouseoutItemOdd:mouseoutItemEven); + YAHOO.util.Event.addListener(oTr[i], "click", clickItemPost); + } +} + +// Javascript for the form entry section + +function onfocusFormElem(event) { + var target = YAHOO.util.Event.getTarget(event, true); + var descElem = document.getElementById('desc_'+target.name); + if(document.all) { addClass(this,'iefocus') } + if(this.value == descElem.value) { this.value = ''; } + +} + +function onblurFormElem(event) { + var target = YAHOO.util.Event.getTarget(event, true); + var descElem = document.getElementById('desc_'+target.name); + if(document.all) { delClass(this,'iefocus') } + if(this.value == '') { this.value = descElem.value; } +} + +function addHidden(oElm) { + var newE = document.createElement("INPUT") + newE.type = "hidden"; newE.value = oElm.value; + newE.name = "desc_"+oElm.name; newE.id = newE.name; + oElm.parentNode.insertBefore(newE, oElm); +} + +function fancyForm() { + var oF = document.forms['fItem']; + var oElm = oF.getElementsByTagName('INPUT'); + var els = oElm.length; + for(var i=0; i<els; i++) { + if(oElm[i].type != 'hidden' && oElm[i].type != 'submit' && oElm[i].type != 'reset') { + YAHOO.util.Event.addListener(oElm[i], "focus", onfocusFormElem); + YAHOO.util.Event.addListener(oElm[i], "blur", onblurFormElem); + addHidden(oElm[i]); i++; els++; + } + } + oElm = oF.getElementsByTagName('TEXTAREA'); + els = oElm.length; + for(var i=0; i<els; i++) { + if(oElm[i].type != 'hidden') { + YAHOO.util.Event.addListener(oElm[i], "focus", onfocusFormElem); + YAHOO.util.Event.addListener(oElm[i], "blur", onblurFormElem); + addHidden(oElm[i]); i++; els++; + } + } + oElm = oF.getElementsByTagName('SELECT'); + els = oElm.length; + for(var i=0; i<els; i++) { + if(oElm[i].type != 'hidden') { + addHidden(oElm[i]); i++; els++; + } + } +} + function myDebug(msg) { var oDebug = document.getElementById('debug'); if(typeof(msg)=='object') { http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/ui.inc?r1=1.2&r2=1.3&diff_format=u Index: presentations/slides/mvc/example/ui.inc diff -u presentations/slides/mvc/example/ui.inc:1.2 presentations/slides/mvc/example/ui.inc:1.3 --- presentations/slides/mvc/example/ui.inc:1.2 Thu Jan 26 20:14:08 2006 +++ presentations/slides/mvc/example/ui.inc Tue Feb 21 21:26:21 2006 @@ -1,85 +1,32 @@ <?php -function head($title="Default") { - echo <<<EOB -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> +function head($title="Default") { ?> +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" + "http://www.w3.org/TR/html4/loose.dtd"> +<html lang="en"> <head> - <title>$title</title> - <style type="text/css" media="screen">@import "styles.php";</style> - <script language="javascript" type="text/javascript" src="lib.js"></script> + <title><?php echo $title?></title> + <style type="text/css" media="screen">@import "styles.css";</style> + <script language="javascript" type="text/javascript" src="/yui/YAHOO.js"></script> + <script language="javascript" type="text/javascript" src="/yui/dom.js"></script> + <script language="javascript" type="text/javascript" src="/yui/event.js"></script> + <script language="javascript" type="text/javascript" src="/yui/connection.js"></script> + <script language="javascript" type="text/javascript" src="/yui/animation.js"></script> <script language="javascript" type="text/javascript" src="common.js"></script> </head> <body> + <br /> <div class="main"> - -EOB; +<?php } -function foot() { - echo <<<EOB +function foot() { ?> </div><br clear="left"/> + <font size="-1">Server -&gt; Client JSON Messages</font><br /> <div id="debug" class="debug"> </div> + <script>fancyItems(); fancyForm();</script> </body> </html> -EOB; -} - -function field($title,$name,$type,$entry,$extra=null) { - $fill = !empty($entry[$name]) ? $entry[$name] : $title; - $float = !empty($extra['float']) ? "style=\"float:{$extra['float']};\"" : ''; - - switch($type) { - case 'text': - $size = isset($extra['size']) ? $extra['size'] : 64; - $maxl = isset($extra['maxl']) ? $extra['maxl'] : 128; - $dir = isset($extra['dir']) ? $extra['dir'] : 'LTR'; - - echo <<<EOB -<input type="hidden" name="desc_$name" id="desc_$name" value="$title" /> - <input type="text" $readonly class="f" id="f_$name" name="$name" size="$size" - maxlength="$maxl" value="$fill" dir="$dir" $float - onfocus="if(document.all) { addClass(this,'iefocus') } - if (this.value == '$title') { this.value = '' }" - onblur ="if(document.all) { delClass(this,'iefocus') } - if (this.value == '') { this.value = '$title' }" /> -EOB; - break; - - case 'select': - $size = isset($extra['size']) ? $extra['size'] : 1; - echo <<<EOB -<input type="hidden" name="desc_$name" id="desc_$name" value="$title" /> - <select class="f" name="$name" id="f_$name" size="$size"> - <option selected>$title</option> - -EOB; - foreach($extra['options'] as $k=>$v) { - if(isset($extra['options_field'])) $v = $v[$extra['options_field']]; - if($v==$fill) echo " <option selected value=\"$v\">$v</option>\n"; - else echo " <option value=\"$v\">$v</option>\n"; - } - echo " </select>"; - break; - - case 'textarea': - $cols = isset($extra['cols']) ? $extra['cols'] : 63; - $rows = isset($extra['rows']) ? $extra['rows'] : 5; - $wrap = isset($extra['wrap']) ? $extra['wrap'] : 'soft'; - echo <<<EOB -<input type="hidden" name="desc_$name" id="desc_$name" value="$title" /> - <textarea - name="$name" class="f" id="f_$name" cols="$cols" - rows="$rows" wrap="$wrap" $float - onfocus="if(document.all) { addClass(this,'iefocus') } - if (this.value == '$title') { this.value = '' }" - onblur ="if(document.all) { delClass(this,'iefocus') } - if (this.value == '') { this.value = '$title' }">$fill</textarea> -EOB; - break; - } - echo "\n"; +<?php } - ?> http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/model/example.db?r1=1.2&r2=1.3&diff_format=u Index: presentations/slides/mvc/example/model/example.db http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/model/items.inc?r1=1.1&r2=1.2&diff_format=u Index: presentations/slides/mvc/example/model/items.inc diff -u presentations/slides/mvc/example/model/items.inc:1.1 presentations/slides/mvc/example/model/items.inc:1.2 --- presentations/slides/mvc/example/model/items.inc:1.1 Wed Jan 25 02:32:01 2006 +++ presentations/slides/mvc/example/model/items.inc Tue Feb 21 21:26:21 2006 @@ -61,6 +61,11 @@ } catch (PDOException $e) { db_fatal_error($e->getMessage()); } + // Some databases can do this right in the SELECT + // SQLite can't, so we add a formatted column ourselves + foreach($rows as $i=>$row) { + $rows[$i]['fprice'] = number_format($row['price'],2); + } return $rows; }
« previous php.pres (#1259) next »