<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TBNL &#187; miscellaneous</title>
	<atom:link href="http://www.tibobeijen.nl/blog/category/miscellaneous/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tibobeijen.nl</link>
	<description>...another view on the web and how it's built</description>
	<lastBuildDate>Wed, 06 Jul 2011 19:28:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Fixing mysqldump on Zend Server CE on OS X</title>
		<link>http://www.tibobeijen.nl/blog/2011/03/01/fixing-mysqldump-on-zend-server-ce-on-os-x/</link>
		<comments>http://www.tibobeijen.nl/blog/2011/03/01/fixing-mysqldump-on-zend-server-ce-on-os-x/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 07:37:56 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[server administration]]></category>
		<category><![CDATA[zend server]]></category>

		<guid isPermaLink="false">http://www.tibobeijen.nl/?p=652</guid>
		<description><![CDATA[A while ago I installed Zend Server Community Edition on OS X which was pretty straightforward. It was only recently that I found out that, as opposed to mysql which worked fine, mysqldump didn&#8217;t work correctly and terminated with the error: mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I installed Zend Server Community Edition on OS X which was pretty straightforward. It was only recently that I found out that, as opposed to <code>mysql</code> which worked fine, <code>mysqldump</code> didn&#8217;t work correctly and terminated with the error:<br />
<code><br />
mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) when trying to connect<br />
</code><br />
Inspecting the mysql configuration contained in <code>/usr/local/zend/mysql/data/my.cnf</code> confirmed that the section [client] showed the socket as returned by executing <code>SHOW VARIABLES;</code> from the mysql client: <code>/usr/local/zend/mysql/tmp/mysql.sock</code></p>
<p>Although it is possible to specify the socket by using mysqldump&#8217;s <code>--socket</code> switch, that doesn&#8217;t really seem a &#8216;solution&#8217;. </p>
<p>Apparently mysqldump, as opposed to the mysql client does not use the server-specific settings contained in <code>/usr/local/zend/mysql/data/my.cnf</code>. The comments in my.cnf state:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"># You can copy this file to
# /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options <span style="">&#40;</span>in this
# installation this directory is /usr/local/zend/mysql/data<span style="">&#41;</span> or
# ~/.my.cnf to set user-specific options.</pre></div></div>

<p>After copying <code>/usr/local/zend/mysql/data/my.cnf</code> to <code>/etc/my.cnf</code> mysqldump worked as expected.</p>
<p>In <code>/etc/my.cnf</code> I have included only the setting needed to get mysqldump running:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"># Specifying socket to use for mysql/mysqldump
# For other settings refer to /usr/local/zend/mysql/data/my.cnf
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>client<span style="">&#93;</span></span>
<span style="color: #000099;">socket</span>      <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> /usr/local/zend/mysql/tmp/mysql.sock</span></pre></div></div>

<p>Hope this saves anyone running into the same issue some time.</p>
<p><strong>Update</strong> (alternative solutions):<br />
As Joel Clermont pointed out it is also possible to create a symlink on the socket location expected by mysqldump to the real socket location. This can be done by executing:<br />
<code><br />
ln -s /usr/local/zend/mysql/tmp/mysql.sock /tmp/mysql.sock<br />
</code></p>
<p>Another possible approach is to create a symlink at <code>/etc/my.cnf</code> to <code>/usr/local/zend/mysql/data/my.cnf</code>. This has the downside that it requires loosening the default permissions (<code>drwxr-x---</code>) on the data folder by allowing &#8216;others&#8217; to enter it. Commands to execute:<br />
<code><br />
sudo chmod o+x /usr/local/zend/mysql/data<br />
sudo ln -s /usr/local/zend/mysql/data/my.cnf my.cnf<br />
</code><br />
Granting more permissions can be a security consideration but on most development setups this probably won&#8217;t be an issue.</p>
<p>Additional info about Zend Server CE on OS X:</p>
<ul>
<li><a href="http://files.zend.com/help/Zend-Server-Community-Edition/zend-server-community-edition.htm">Zend Server CE Documentation</a></li>
<li><a href="http://akrabat.com/php/some-notes-on-zend-server-ce-for-mac-os-x/">Rob Allen: Some notes on Zend Server CE for Mac OS X</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2011/03/01/fixing-mysqldump-on-zend-server-ce-on-os-x/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Zend_Form: Building dynamic forms</title>
		<link>http://www.tibobeijen.nl/blog/2009/12/21/zend_form-building-dynamic-forms/</link>
		<comments>http://www.tibobeijen.nl/blog/2009/12/21/zend_form-building-dynamic-forms/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 10:38:33 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[miscellaneous]]></category>

		<guid isPermaLink="false">http://www.tibobeijen.nl/?p=611</guid>
		<description><![CDATA[In my previous post about Zend_Form I showed how, using Zend_Form, a form&#8217;s structure can be separated from it&#8217;s presentation and how to use custom Decorators and Validators. The example used showed a form that is tightly coupled to a record in a database: One form edits one record. There are however numerous occasions where [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="/blog/2009/12/07/using-zend_form-without-zend-framework-mvc/">my previous post about Zend_Form</a> I showed how, using Zend_Form, a form&#8217;s structure can be separated from it&#8217;s presentation and how to use custom Decorators and Validators. The example used showed a form that is tightly coupled to a record in a database: One form edits one record. There are however numerous occasions where no &#8216;one to one&#8217; connection exists and where the fields that need to be shown are not predetermined. Take for example a shopping cart that shows the amounts of each product. In this article I&#8217;ll take a look at how to:</p>
<ul>
<li>dynamically construct a form based on the data being edited</li>
<li>use subforms to reuse common parts and group related values</li>
<li>create composite &#8216;elements&#8217; by using the default Zend_Form_Element in combination with a custom decorator</li>
<li>create additional elements with Javascript and have them processed by the form when submitted</li>
</ul>
<p>Like previous article the case examined assumes a situation where Zend_Form is used in an existing project that doesn&#8217;t use the Zend Framework MVC stack. The Form <a href="/static/zend_form_dynamic/">can be viewed online</a> and all of the <a href="http://github.com/TBeijen/Zend_Form_Dynamic">sample code can be found on GitHub</a>.</p>
<h3>Case outline</h3>
<p>In this example I construct a form where a user can view and edit a task list for a week&#8217;s working days. Every task can be edited in one single form and new tasks can be added at will. When viewing the page the application fetches the tasks already entered for that week. The assumed codebase is able to generate and consume an array as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// array format used to supply data of multiple tasks to form</span>
<span style="color: #000088;">$tasks</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$timestampDay1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'current'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$taskId1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'desc'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Gotta do this'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tasks</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$timestampDay1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'current'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$taskId1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'completed'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tasks</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$timestampDay2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'current'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$taskId2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'desc'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Gotta do that'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tasks</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$timestampDay2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'current'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$taskId2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'completed'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span></pre></div></div>

<p>The &#8216;current&#8217; part will later help to distinguish between new and allready stored tasks.</p>
<h3>Dynamically building a Zend_Form</h3>
<p>Using Zend_Form typically follows these steps:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_Form<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// My_Form::init() will define elements</span>
<span style="color: #000088;">$Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDefaults</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$myData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$submittedValues</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValues</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// process values and redirect to same or other page</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>When a form&#8217;s elements need to be defined based on the data being edited, Zend_Form::init() is not suitable as at that point the data is not known yet. Providing data through the constructor to have it available in the init() method &#8216;could&#8217; work but is not a really nice solution as it means changing the parameter scheme of the constructor. Furthermore, it would ignore the method that exists specifically for adding initial data: setDefaults(). As that will be the first point at which the form &#8216;knows&#8217; what data needs to be edited, that will be the point to add elements. Or, as in this case, subforms.</p>
<h4>My_Form_TaskWeek</h4>
<p>The setDefaults() method of the week-form is changed as displayed below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_Form_TaskWeek <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setDefaults<span style="color: #009900;">&#40;</span><span style="color: #000088;">$defaults</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// first add the subforms</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clearSubForms</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$dates</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$defaults</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$dates</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$day</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$dayForm</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_SubForm_TaskDay<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addSubForm</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dayForm</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$day</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// set defaults, which will propagate to newly created subforms</span>
        parent<span style="color: #339933;">::</span><span style="color: #004000;">setDefaults</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$defaults</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For every day in $defaults (in this example the key is a timestamp, but it could be anything) a subform is added. This is done <em>before</em> calling parent::setDefaults(). With the subforms in place the call to the parent method will propagate to the subforms we&#8217;ve just created.</p>
<h4>My_Form_TaskDay</h4>
<p>In the form that shows one single day, some actions <em>are</em> performed in the init() method: A subform for both &#8216;current&#8217; and &#8216;new&#8217; elements is added. Also a &#8216;template&#8217; element is added. The subforms have just one decorator: FormElements. The template element will be used as source for new elements that are added by javascript. When setDefaults() is called, elements are added to the current subform.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">class</span> My_SubForm_TaskDay <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form_SubForm
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// create subforms having nothing but element decorator</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addSubForm</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Form_SubForm<span style="color: #339933;">,</span> <span style="color: #0000ff;">'current'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addSubForm</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Form_SubForm<span style="color: #339933;">,</span> <span style="color: #0000ff;">'new'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSubForm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'current'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FormElements'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSubForm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'new'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FormElements'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// add template element</span>
        <span style="color: #000088;">$templateElement</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createTaskElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'__template__'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSubForm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'new'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$templateElement</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setDefaults<span style="color: #009900;">&#40;</span><span style="color: #000088;">$defaults</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$subform</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSubForm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'current'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$defaults</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'current'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$subform</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createTaskElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// set defaults, which will propagate to newly created subforms &amp; elements</span>
        parent<span style="color: #339933;">::</span><span style="color: #004000;">setDefaults</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$defaults</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The method that creates an element:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> createTaskElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$values</span><span style="color: #339933;">,</span> <span style="color: #000088;">$isNew</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$elm</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$elm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clearDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$elm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> My_Decorator_TaskElement<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$elm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Errors'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'placement'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'prepend'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// add configured validator</span>
        <span style="color: #000088;">$validator</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_Validator_TaskElement<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$validator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIsNew</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$isNew</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$elm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$validator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$elm</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Creating composite elements by using custom decorator</h3>
<p>As the code above shows, creating a task element consists of three steps:</p>
<h4>Zend_Form_Element</h4>
<p>A basic Zend_Form_Element is created. This is the &#8216;root&#8217; class of which specific element-types (like Zend_Form_Element_Text) are descendants. The Form will supply each element with the element&#8217;s value which in this case will be an associative array with the keys &#8216;desc&#8217; and &#8216;completed&#8217;.</p>
<h4>My_Decorator_TaskElement</h4>
<p>A custom decorator is added: My_Decorator_TaskElement. This decorator replaces what would normally be the ViewRenderer decorator. It is responsible for rendering <em>both</em> the text and checkbox input elements and wraps them in a div. For the template element (based on the element name) the containing div will be given a classname &#8216;template&#8217; which will be of use when creating the javascript behaviour.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> render<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// get element details</span>
        <span style="color: #000088;">$elm</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$elm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$elmName</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$elm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFullyQualifiedName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// construct inputs</span>
        <span style="color: #000088;">$isCompleted</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'completed'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'completed'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$descValue</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'desc'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'desc'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$inputDesc</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'&lt;input type=&quot;text&quot; name=&quot;%s&quot; value=&quot;%s&quot; /&gt;'</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$elmName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'[desc]'</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$descValue</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$inputCompleted</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'&lt;input type=&quot;checkbox&quot; name=&quot;%s&quot; value=&quot;1&quot; %s /&gt;'</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$elmName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'[completed]'</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#40;</span><span style="color: #000088;">$isCompleted</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'checked=&quot;checked&quot;'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// wrap in div, optionally adding attribute class</span>
        <span style="color: #000088;">$elmHtml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'&lt;div class=&quot;task %s&quot;&gt;%s%s&lt;/div&gt;'</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#40;</span><span style="color: #000088;">$elm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'__template__'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'template'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$inputDesc</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$inputCompleted</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// this should be the first decorator but add the content for</span>
        <span style="color: #666666; font-style: italic;">// consistency's sake</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$elmHtml</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<h4>My_Validator_TaskElement</h4>
<p>A custom validator is added: My_Validator_TaskElement (<a href="http://github.com/TBeijen/Zend_Form_Dynamic/blob/master/htdocs/My_Validator_TaskElement.php">see code on GitHub</a>). This validator will check if the description is empty but treats new elements differently from existing ones: For existing elements it is not allowed to have an empty description. For new elements this is only allowed if the &#8216;completed&#8217; checkbox isn&#8217;t checked. (This way a user can still submit the form if he&#8217;d accidentally added a new field). </p>
<h3>Using jQuery to let the user add new elements</h3>
<p>The javascript that lets a user add a new element is implemented as a jQuery plugin. The code having the javascript initiate looks as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'form .taskDay'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dynamicForm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As can be seen each of the &#8216;day&#8217; subforms outer elements is selected and applied upon a jQuery plugin. This plugin (<a href="http://github.com/TBeijen/Zend_Form_Dynamic/blob/master/htdocs/js/jquery.dynamicform.js">on GitHub</a>)performs the following tasks:</p>
<ul>
<li>Within the element it operates on it searches for an element with classname &#8216;add&#8217; and attaches a &#8216;click&#8217; event handler to it.</li>
<li>It searches for an element &#8216;template&#8217; and stores a reference to it.</li>
<li>Whenever the user clicks the &#8216;add&#8217; button, the template element is cloned</li>
<li>Within the template element the &#8216;name&#8217; attribute of any input element is altered: The string &#8216;__template__&#8217; is replaced by a timestamp. This makes sure that multiple new tasks can be added within a day without them interfering with each other.</li>
<li>The new element is placed after the last found element with classname &#8216;task&#8217;</li>
</ul>
<p>By referring to classnames, the javascript&#8217;s dependencies on the exact HTML are kept to a minimum. This way, if needed, the HTML structure of the form elements can be changed without the need to update the javascript.</p>
<h3>Having the form process the new elements</h3>
<p>When the task form is submitted, newly added tasks will be in the $_POST array as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// is assoc. array having 'desc' and 'completed' keys</span>
<span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$dayTimestamp</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'new'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$uniqueJSCreatedValue</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The form object needs to be able to validate the input for both the existing and the new tasks, so the $_POST array is passed to the isValid() method. At this point the form doesn&#8217;t yet contain the &#8216;new&#8217; elements. Adding those is done in a similar fashion to how the existing elements are added in setDefaults(), only this time it is done in isValid(). That is the first point where the form is able to determine what the new elements are. Dynamically adding the new elements happens in the &#8216;day&#8217; subform:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_SubForm_TaskDay <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form_SubForm
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> isValid<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$subform</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSubForm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'new'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// make sure new is array (won't be in $data if nothing submitted)</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'new'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'new'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'new'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$idx</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Don't add element with idx = __template__. SetIgnore works on</span>
            <span style="color: #666666; font-style: italic;">// getValues. Template elements are submitted so this would otherwise</span>
            <span style="color: #666666; font-style: italic;">// override the previously added template element, thereby losing the</span>
            <span style="color: #666666; font-style: italic;">// setIgnore setting...</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$idx</span> <span style="color: #339933;">!==</span> <span style="color: #0000ff;">'__template__'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$subform</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createTaskElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$idx</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// call parent, which will populate newly created elements.</span>
        <span style="color: #b1b100;">return</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">//...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here the aforementioned createTaskElement() method pops up again. Notice the third parameter (true) that will make the validator treat the element as a &#8216;new&#8217; one.</p>
<h3>All done, except for &#8230; </h3>
<p>In theory now everything works. But practice tends to have those little things that go wrong which you hardly ever read about in code examples. When working on this example I noticed that getValues() doesn&#8217;t preserve the per-day timestamp keys in the array returned. Instead, it returns an indexed array like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$getValues</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #666666; font-style: italic;">// this '0' should have been a timestamp, like: 1261350000</span>
    <span style="color: #0000ff;">'0'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'current'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// etc. etc.</span>
        <span style="color: #0000ff;">'new'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// etc. etc.</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Bummer. As it appears, Zend_Form &#8216;requires&#8217; elements, subforms and display groups to have valid variable names (See <a href="http://framework.zend.com/issues/browse/ZF-4204">ZF-4204</a>). One of the reasons is problably that the names also end up in HTML id attributes which are not allowed to start with a number. The name attribute <em>is</em> though (you can run the demo form through the W3C validator). Remarkably, the task elements, having a name consisting of the database id, are processed just fine.</p>
<p>Two possible solutions for this:</p>
<ul>
<li>Avoid numeric names. Downside is that this also involves changing the existing code generating and processing the array. The day-form header decorator which uses the form&#8217;s name to display the date needs to be changed as well.</li>
<li>Add an implementation of getValues() in My_Form_TaskWeek that checks for numeric keys and changes them back to their original timestamp</li>
</ul>
<p>I choose the latter (for now) because it allows to leave almost all existing code unaltered and the resulting behaviour is unit testable (supply defaults, test what is rendered, test what is returned, etc.).</p>
<h3>Conclusion</h3>
<p>The above shows that:</p>
<ul>
<li>Subforms are very useful to repeat similar blocks within a form and to group related values in the returned value array.</li>
<li>Dynamically adding initial and newly submitted elements follows a similar pattern: Add elements based on the passed in array and then call the parent&#8217;s method. This is done in setDefaults() and isValid() respectively.</li>
</ul>
<p>The question might arise &#8216;is Zend_Form the best solution for this type of customized form?&#8217;. The default ViewHelper decorator is ignored, no default validator is used. I can&#8217;t tell if Zend_Form is the <em>best</em> solution but fact is that it provides a lot of functionallity and flexibility: The Form, Errors and HtmlTag decorators are used &#8216;as is&#8217;. Furthermore, the option to reuse parts comes almost naturally because of Zend_Form&#8217;s design.</p>
<p>Regarding the last point I&#8217;ll conclude with following example: What if, some time after having created this form, the idea is born to have some sort of per-day dashboard page where a user can view and edit things like calendar, messages and tasks? (This is the kind of stuff that happens in agile projects). Reusing My_SubForm_TaskDay would be very useful. And luckily that is easily accomplished. All that is needed is wrapping the subform in a class that will additionally:</p>
<ul>
<li>Set a view object</li>
<li>Add a submit button</li>
<li>Add a Form decorator</li>
</ul>
<p>See the code of <a href="http://github.com/TBeijen/Zend_Form_Dynamic/blob/master/htdocs/My_Form_TaskDay.php">My_Form_TaskDay on GitHub</a> or <a href="/static/zend_form_dynamic/day_only.php">see it in action online</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2009/12/21/zend_form-building-dynamic-forms/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Taming the Javascript event scope: Closures</title>
		<link>http://www.tibobeijen.nl/blog/2009/07/27/taming-the-javascript-event-scope-closures/</link>
		<comments>http://www.tibobeijen.nl/blog/2009/07/27/taming-the-javascript-event-scope-closures/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 13:52:44 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.tibobeijen.nl/?p=459</guid>
		<description><![CDATA[When doing client-side developing there are times that jQuery&#8217;s get-this-do-that nature doesn&#8217;t provide all that is needed. For more complex applications I usually find myself creating javascript objects that &#8216;control&#8217; a specific part of the page&#8217;s interaction. In the objects the application&#8217;s state is tracked, references to other objects (could be relevant DOM nodes) are [...]]]></description>
			<content:encoded><![CDATA[<p>When doing client-side developing there are times that jQuery&#8217;s get-this-do-that nature doesn&#8217;t provide all that is needed. For more complex applications I usually find myself creating javascript objects that &#8216;control&#8217; a specific part of the page&#8217;s interaction. In the objects the application&#8217;s state is tracked, references to other objects (could be relevant DOM nodes) are stored and event handlers are set.</p>
<p>One of the problems typically encountered when dealing with javascript event handlers is that they have their own take on the &#8216;this&#8217; keyword. Closures to the rescue.<br />
<span id="more-459"></span><br />
A simple (and rather useless) example:</p>
<p>html:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#scope div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>idx<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> btnController <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> controllerExample<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span>idx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;scope&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;btn 1&quot;</span> <span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;btn 2&quot;</span> <span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p>javascript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> controllerExample<span style="color: #009900;">&#40;</span>node<span style="color: #339933;">,</span>idx<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">node</span> <span style="color: #339933;">=</span> node<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">idx</span> <span style="color: #339933;">=</span> idx<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">clickCount</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setEventHandlers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
controllerExample.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">setEventHandlers</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">node</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">handleClick</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
controllerExample.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">handleClick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">clickCount</span><span style="color: #339933;">++;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">node</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'p'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>
        <span style="color: #3366CC;">'Button is clicked '</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">clickCount</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' time(s)'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>&#8216;this&#8217; is wrong</h3>
<p>Now that doesn&#8217;t work&#8230; When the click event fires the handleClick() function is executed but the scope is not the controllerExample instance. On execution the &#8216;this&#8217; keyword points to the input element. So we see &#8216;Button is clicked NaN time(s)&#8217;.</p>
<p>The Prototype javascript library has a solution for this by extending the basic function object type with the bind() and bindAsEventListener() methods. Very nice but to include Prototype just for that&#8230; bad idea. It&#8217;s perfectly possible to separately implement a similar bind() function method but that still creates an additional (besides jQuery) dependency for that method. Let&#8217;s keep the mess to a minimum and keep it &#8216;in&#8217; the controllerExample object.</p>
<h3>Closure</h3>
<p>Prototype&#8217;s bind() function uses what is called a closure: A function defined within a function. The benefit is that the inner function, which in the following example is returned by the outer function, has access to the outer functions local variables <em>after</em> the outer function has returned.</p>
<p>Properly &#8216;binding&#8217; the handleClick event handler now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">controllerExample.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">setEventHandlers</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _scope <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> getHandleClick <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// window</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>_scope<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// controllerExample</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> _scope.<span style="color: #660066;">handleClick</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>_scope<span style="color: #339933;">,</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">node</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span>getHandleClick<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>getHandleClick() returns a function that, when executing, still has access to _scope which <em>is</em> the correct scope. The console.log lines are there to illustrate that inside the inner function we can&#8217;t use &#8216;this&#8217;. The getHandleClick() function has no object scope so it&#8217;s &#8216;this&#8217; is the window scope. But the object scope can be passed into by copying &#8216;this&#8217; to a new variable _scope and use that inside the closure.</p>
<p>More information about closures: <a href="http://www.javascriptkit.com/javatutors/closures.shtml">Javascript closures 101</a> (basic) and <a href="http://www.jibbering.com/faq/faq_notes/closures.html">This article on jibbering.com</a> (advanced).</p>
<h3>routeEvent</h3>
<p>Now the above is fine if there&#8217;s just one or two event handlers to be added but is not really &#8216;generic&#8217;. So let&#8217;s create a more generic &#8216;routeEvent&#8217; method that creates a closure for the eventHandler that is passed in:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">controllerExample.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">setEventHandlers</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">node</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span>
        <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">routeEvent</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">handleClick</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">// ...</span>
controllerExample.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">routeEvent</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>eventHandler<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _scope <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> eventHandler.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>_scope<span style="color: #339933;">,</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now the handleClick() method will be called with the correct &#8216;this&#8217; and is also provided with the jQuery event object.</p>
<p>A demo putting it al together can be seen <a href="/static/event_scope_closure/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2009/07/27/taming-the-javascript-event-scope-closures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend_Test, Zend_Layout and the need to reset</title>
		<link>http://www.tibobeijen.nl/blog/2009/06/01/zend_test-zend_layout-and-the-need-to-reset/</link>
		<comments>http://www.tibobeijen.nl/blog/2009/06/01/zend_test-zend_layout-and-the-need-to-reset/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 11:55:30 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.tibobeijen.nl/?p=374</guid>
		<description><![CDATA[In a recent Zend Framework project I&#8217;ve used Zend_Test to test the functioning of the website &#8216;as a whole&#8217;. So besides testing the separate (authorization) components, the website was tested in the same way a visitor would use it. This is especially useful for testing login scenarios, so I added the test below: public function [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent Zend Framework project I&#8217;ve used Zend_Test to test the functioning of the website &#8216;as a whole&#8217;. So besides testing the separate (authorization) components, the website was tested in the same way a visitor would use it. This is especially useful for testing login scenarios, so I added the test below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testLogoutShouldDenyAccess<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">login</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #666666; font-style: italic;">// verify that profile page now doesn't contain login form</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/profile'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertQueryCount</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'form#login'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// dispatch logout page</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/login/logout'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// verify that profile now holds login form</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/profile'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertQueryCount</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'form#login'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertNotController</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'profile'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This failed on the last assertQueryCount() which left me puzzled. Performing above steps manually seemed to work fine so I was overlooking something either in my app-code or the test-code.<br />
<span id="more-374"></span><br />
Adding a var_dump at the end of the test method gave some insight.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Showed a response object with a protected variable _body containing (simplified for clarity):</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;login&lt;/h1&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;h1&gt;logout&lt;/h1&gt;
    &lt;/body&gt;
    &lt;/html&gt;
&lt;form id=&quot;login&quot; action=&quot;&quot; method=&quot;post&quot;&gt;
&lt;!-- form contents --&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Apparantly the total content of a previous request is prepended to the view part of the next request, resulting in incorrect html the assertQuery() method can&#8217;t handle. Looking into the login() helper method I made earlier:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//login</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span>
         <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPost</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
              <span style="color: #0000ff;">'un'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'testun'</span><span style="color: #339933;">,</span>
              <span style="color: #0000ff;">'pw'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'testpw'</span>
         <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/login/login'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertRedirectTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// reset</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resetRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
         <span style="color: #339933;">-&gt;</span><span style="color: #004000;">resetResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// reset post</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPost</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Apparently I forgot to reset the response object as adding that to the test method solved the issue. So for testing multiple requests in one test method one needs to reset the response to prevent incorrect html. This can be simplified by using below custom dispatch method that also allows setting POST data:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doDispatch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #000088;">$post</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// reset everything</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resetRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
         <span style="color: #339933;">-&gt;</span><span style="color: #004000;">resetResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPost</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// add post-data if given</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPost</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'un'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'testun'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'pw'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'testpw'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// dispatch</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2009/06/01/zend_test-zend_layout-and-the-need-to-reset/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing PHPUnit on XAMPP Lite (Windows)</title>
		<link>http://www.tibobeijen.nl/blog/2009/05/17/installing-phpunit-on-xampp-lite-windows/</link>
		<comments>http://www.tibobeijen.nl/blog/2009/05/17/installing-phpunit-on-xampp-lite-windows/#comments</comments>
		<pubDate>Sun, 17 May 2009 13:39:00 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[phpunit]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[xampp]]></category>

		<guid isPermaLink="false">http://www.tibobeijen.nl/?p=360</guid>
		<description><![CDATA[At my work PHPUnit is &#8216;just there&#8217; but it&#8217;s not on my home machine where I&#8217;m running an out of the box XAMPP Lite setup. So I visited the PHPUnit installation manual and all looked easy: pear channel-discover pear.phpunit.de pear install phpunit/PHPUnit But what I got was a message saying my pear installer was out [...]]]></description>
			<content:encoded><![CDATA[<p>At my work PHPUnit is &#8216;just there&#8217; but it&#8217;s not on my home machine where I&#8217;m running an out of the box <a href="http://www.apachefriends.org/en/xampp-windows.html#646">XAMPP Lite</a> setup. So I visited the <a href="http://www.phpunit.de/manual/current/en/installation.html">PHPUnit installation manual</a> and all looked easy:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit</pre></div></div>

<p>But what I got was a message saying my pear installer was out of date, 1.7.1 was needed. Running &#8216;pear -V&#8217; showed:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">E:\Xampplite\php&gt;pear -V
PEAR Version: 1.4.5
PHP Version: 5.2.5
Zend Engine Version: 2.2.0
Running on: Windows NT T1720 6.0 build 6000</pre></div></div>

<p>Allright, upgrading PEAR it is. That was not as straightforward as hoped.<br />
<span id="more-360"></span></p>
<h3>go-pear</h3>
<p>I don&#8217;t know much about PEAR so I found a lot of possible solutions ranging from manually installing specific packages to something a bit more straightforward like &#8216;go-pear&#8217;. Better start simple so &#8216;go-pear&#8217; it is. That started fine but ended up in a number of the following messages:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Warning: Cannot use a scalar value as an array in phar://go-pear.phar/PEAR/Comma
nd.php on line 268</pre></div></div>

<p>After finding the right keywords to put into google I came on <a href="http://www.phpwomen.org/wordpress/2006/11/06/bundled-go-pearphar-broken-in-52-windows-releases/">this page on PHPWomen.org</a>. Spot on. After replacing go-pear.phar (rename the old one first, you never know) &#8216;go-pear&#8217; at least stopped showing aforementioned errors but displayed some warnings (Commit Failed) that made me suspicious. And indeed, &#8216;pear -V&#8217; still showed 1.4.5.</p>
<h3>pear upgrade</h3>
<p>Luckily I had quite some google tabs open allready so I proceeded with the next strategy:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">E:\Xampplite\php&gt;pear upgrade pear
downloading PEAR-1.8.1.tgz ...
Starting to download PEAR-1.8.1.tgz (290,382 bytes)
............................................................done: 290,382 bytes
downloading Archive_Tar-1.3.3.tgz ...
Starting to download Archive_Tar-1.3.3.tgz (18,119 bytes)
...done: 18,119 bytes
downloading Console_Getopt-1.2.3.tgz ...
Starting to download Console_Getopt-1.2.3.tgz (4,011 bytes)
...done: 4,011 bytes
upgrade ok: channel://pear.php.net/Archive_Tar-1.3.3
upgrade ok: channel://pear.php.net/Console_Getopt-1.2.3
Could not delete E:\Xampplite\php\pear.bat, cannot rename E:\Xampplite\php\.tmpp
ear.bat
ERROR: commit failed</pre></div></div>

<p>Bummer. I tried renaming pear.bat but then the installer came with the same message for pear-dev.bat so I looked for a &#8216;real&#8217; solution. Once again finding the right keywords (pear upgrade cannot delete windows) Google did the rest and yielded <a href="http://www.pear-forum.org/topic1201.html">a page on pear-forum.org</a>. I was one third on my way to the &#8216;real&#8217; solution that is renaming the following files:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">rename pear.bat _pear.bat
rename peardev.bat _peardev.bat
rename pecl.bat _pecl.bat</pre></div></div>

<p>Followed by:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">_pear upgrade pear</pre></div></div>

<p>Resulting in:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">E:\Xampplite\php&gt;pear -V
PEAR Version: 1.8.1
PHP Version: 5.2.5
Zend Engine Version: 2.2.0
Running on: Windows NT T1720 6.0 build 6000</pre></div></div>

<h3>Victory</h3>
<p>Retrying installing of PHPUnit now worked.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">E:\Xampplite&gt;phpunit --version
PHPUnit 3.3.16 by Sebastian Bergmann.</pre></div></div>

<p>One final thing. It&#8217;s very convenient to add the php directory to environment path setting to allow for executing &#8216;php&#8217; or &#8216;phpunit&#8217; from whatever project directory. Do so by going to (on Vista, XP might differ):</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Control Panel -&gt;
System -&gt;
Advanced system settings -&gt;
(In the popup, bottom button) Environment Variables -&gt;
(Lower list) Select path &amp; choose 'edit'</pre></div></div>

<p>Now add the php directory to the end of the line, in this case &#8220;;E:\Xampplite\php&#8221; (without the quotes). A restart might be needed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2009/05/17/installing-phpunit-on-xampp-lite-windows/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Fixing Netbeans after Ubuntu 9 upgrade</title>
		<link>http://www.tibobeijen.nl/blog/2009/05/01/fixing-netbeans-after-ubuntu-9-upgrade/</link>
		<comments>http://www.tibobeijen.nl/blog/2009/05/01/fixing-netbeans-after-ubuntu-9-upgrade/#comments</comments>
		<pubDate>Fri, 01 May 2009 09:20:14 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.tibobeijen.nl/?p=349</guid>
		<description><![CDATA[This morning I upgraded my Ubuntu machine using the auto-update. As I just recently started using Ubuntu I&#8217;m very pleased at how some features work compared to Vista. (Vista users will probably be familiar with the auto-update restart that has a terrific feel for timing by always presenting you the choice for postponing the restart [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I upgraded my Ubuntu machine using the auto-update. As I just recently started using Ubuntu I&#8217;m very pleased at how some features work compared to Vista. (Vista users will probably be familiar with the auto-update restart that has a terrific feel for timing by always presenting you the choice for postponing the restart when you have several documents opened and are away for a coffee break.) After my self initiated restart everything worked like a charm, OpenOffice is updated to version 3 (nice for the docx workflow) but&#8230; Netbeans didn&#8217;t start. Now that&#8217;s bad for productivity. Running from the console showed the following:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Cannot find java. Please use the --jdkhome switch.</pre></div></div>

<p>a After a little bit of googling I learned there is a netbeans.conf somewhere. On my system it&#8217;s location is:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">~/netbeans-6.7-m2/etc/netbeans.conf</pre></div></div>

<p>In there, look for a line like</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">netbeans_jdkhome=&quot;/usr/lib/jvm/java-6-sun-1.6.0.13/jre&quot;</pre></div></div>

<p>and change the version number to the correct version. For me it worked like a charm, I hope it helps someone.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2009/05/01/fixing-netbeans-after-ubuntu-9-upgrade/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Annoying banners: A plea for quality</title>
		<link>http://www.tibobeijen.nl/blog/2009/04/27/annoying-banners-a-plea-for-quality/</link>
		<comments>http://www.tibobeijen.nl/blog/2009/04/27/annoying-banners-a-plea-for-quality/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 20:51:10 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[advertisement]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://www.tibobeijen.nl/?p=330</guid>
		<description><![CDATA[Banners play an essential role in many site&#8217;s business models so they are an inevitable price paid for all the free content that is available on the internet. To get a user&#8217;s attention a lot of practices are employed like animation, placement or sound (horrible). Today I stumbled on a T-mobile advert on the site [...]]]></description>
			<content:encoded><![CDATA[<p>Banners play an essential role in many site&#8217;s business models so they are an inevitable price paid for all the free content that is available on the internet. To get a user&#8217;s attention a lot of practices are employed like animation, placement or sound (horrible). Today I stumbled on a T-mobile advert on the site <a href="http://www.nu.nl/internet/1955631/worm-conficker-wordt-geleidelijk-actief.html">nu.nl</a> that indeed attracts a lot of attention but does so in a questionable way: It makes using the visited site almost impossible. (Not a new phenomenon and it&#8217;s certainly not the first time I encounter such a banner). </p>
<p>The T-mobile banner is positioned at the bottom of the viewport and is kept in place by javascript or actionscript. This means that during scrolling the banner shakes because the script reacts to the scroll event. No awards won but not a real problem either. But what <em>is</em> bad is the fact that in the orange area (note the little pun there) no link can be clicked. Something I didn&#8217;t notice at first resulting in rapid agitated clicking. See screenshot below:</p>
<div id="attachment_335" class="wp-caption aligncenter" style="width: 510px"><img src="http://www.tibobeijen.nl/blog/wp-content/uploads/2009/04/tmobile_add_scaled_text.jpg" alt="T-Mobile banner on nu.nl" title="tmobile_add_scaled_text" width="500" height="317" class="size-full wp-image-335" /><p class="wp-caption-text">T-Mobile banner on nu.nl</p></div>
<p>As a &#8216;normal&#8217; visitor that is just annoying but as a web-professional it irritates me to a great extend that there seems to be no technical need for this misbehaviour whatsoever. So what goes wrong? On a first glance there seem to be four roles involved:</p>
<ul>
<li>Nu.nl: They need adds but it seems unlikely they want banners to block parts of their site and thereby cause user frustration.</li>
<li>The banner merchant. Doubleclick, adclick or whatever their names are. I&#8217;m not really into the advertisement business but I wouldn&#8217;t be surprised if different advertisement programs exist that site publishers can use: flashy or not, type of products promoted, sound or not, full page mayhem or not. That sort of differentiations. Normally adds on nu.nl aren&#8217;t this annoying so something might have gone wrong there.</li>
<li>The developer or company developing the banner. Perhaps there&#8217;s a tight deadline. Perhaps technical knowledge is lacking. Maybe they just don&#8217;t care&#8230;</li>
<li>The advertiser, in this case T-Mobile. Bottom line is: they want to get visitors to their site. But perhaps reputation might be a consideration. I think in most cases that the people reviewing a banner proposol don&#8217;t have the knowledge to determine that a banner has these unwanted technical side-effects. Or the prototype presented doesn&#8217;t show the problems.</li>
</ul>
<p>So somewhere in the process things go wrong, be it a lack of knowledge or a lack of interest for the end-user. Sadly the result is a lack of quality having bad usability as a consequence.</p>
<p>To conclude this criticism in a more positive and constructive fashion I&#8217;ve made a quick css proof of concept showing that it&#8217;s perfectly possible to achieve the layout while preserving click access to the underlying site. </p>
<p><a href="/static/less_annoying_banner/">Bottom aligned banner proof of concept</a></p>
<p>It&#8217;s positioning is now done by using css position:absolute but could just as well have been javascript. The demo took about 15 minutes to make while watching tv. I haven&#8217;t tested it on IE6 so expect about 2 hours needed for that but even then development time seems reasonable. The advertisment html part is displayed below.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;banner&quot; style=&quot;background-color:purple;position:fixed;bottom:0;width:100%;height:80px;&quot;&gt;
	&lt;div id=&quot;bannerCenter&quot; style=&quot;width:760px;height:80px;position:relative;margin:0 auto;background-color:red;&quot;&gt;
		&lt;div id=&quot;phone&quot; style=&quot;position:absolute;width:100px;height:160px;left:660px;top:-100px;background-color:#666;z-index&quot;100;&quot;&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;</pre></div></div>

<p>Of course the real world is more complicated than can be described in a short article but it&#8217;s obvious that the examined banner is sub-par. In my opinion it would be wise, or at least nice, to take the user a bit more seriously.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2009/04/27/annoying-banners-a-plea-for-quality/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Jumping in and out of jQuery land</title>
		<link>http://www.tibobeijen.nl/blog/2009/04/02/jumping-in-and-out-of-jquery-land/</link>
		<comments>http://www.tibobeijen.nl/blog/2009/04/02/jumping-in-and-out-of-jquery-land/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 21:34:59 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.tibobeijen.nl/?p=243</guid>
		<description><![CDATA[Recently I started using jQuery in some projects. In past projects I have mainly been using Prototype and the fact that jQuery also has a $() function made me feel at home right away. That same fact put me a bit off-guard as both functions are in fact quite different: Prototype extends the selected HTML [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I started using jQuery in some projects. In past projects I have mainly been using Prototype and the fact that jQuery also has a $() function made me feel at home right away. That same fact put me a bit off-guard as both functions are in fact quite different:</p>
<ul>
<li>Prototype extends the selected HTML node with added functionality and returns it. Argument should be a HTML node or element id.</li>
<li>jQuery selects the HTML node (or nodes) and stores the selection in a jQuery object, which then is returned. Argument should be a css-like selector.</li>
</ul>
<p>So, as opposed to Prototype, jQuery&#8217;s $() method can select a single element as well as a collection of elements. The existence of <a href="http://www.prototypejs.org/api/utility#method-$$">Prototype&#8217;s $$() function</a> also kind of suggests that Prototype&#8217;s $() doesn&#8217;t do that. And&#8230; when passing just an id to jQuery&#8217;s $() (e.g. $(&#8216;myDiv&#8217;)) it is discovered soon enough that nothing is selected but air.</p>
<p>By using jQuery&#8217;s $() functionwe&#8217;re leaving the DOM-zone and enter jQuery land. Although jQuery can do a lot, situations might occur where one needs to get the &#8216;real&#8217; DOM elements. For instance, to pass them as argument to components that don&#8217;t talk jQuery. Luckily it&#8217;s just as easy to go back, by using the get() accessor.</p>
<p>Some simplified HTML code example boxes:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;codeBox&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>copy to clipboard<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">code</span>&gt;</span>Some code<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">code</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;codeBox&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>copy to clipboard<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">code</span>&gt;</span>More code<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">code</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Accompanied by the following javascript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    initCodeBox<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> initCodeBox<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.codeBox a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> htmlNode <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">siblings</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'code'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>htmlNode<span style="color: #009900;">&#41;</span> copyContentsToClipboard<span style="color: #009900;">&#40;</span>htmlNode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> copyContentsToClipboard<span style="color: #009900;">&#40;</span>htmlNode<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// js selecting the text contents of a DOM node, simplified to: </span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>htmlNode.<span style="color: #660066;">innerHTML</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It&#8217;s clear that, as long as existing code doesn&#8217;t conflict with the $() function, it&#8217;s very easy to start using jQuery in existing projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2009/04/02/jumping-in-and-out-of-jquery-land/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Internet Explorer 8</title>
		<link>http://www.tibobeijen.nl/blog/2009/03/20/internet-explorer-8/</link>
		<comments>http://www.tibobeijen.nl/blog/2009/03/20/internet-explorer-8/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 08:32:07 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[internet explorer]]></category>
		<category><![CDATA[microsoft]]></category>

		<guid isPermaLink="false">http://www.tibobeijen.nl/?p=220</guid>
		<description><![CDATA[Yesterday Internet Explorer 8 is released. I consider that a good things as it will move more people farther away from the severe case of release abuse called IE6. Improvements include integrated developer tools for css analysis and script profiling and debugging. And there is &#8216;Compatibility View&#8217;. Developers can specify, by adding a specific meta [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday Internet Explorer 8 is released. I consider that a good things as it will move more people farther away from the severe case of release abuse called IE6. <a href="http://msdn.microsoft.com/en-us/library/cc288472(VS.85).aspx">Improvements</a> include integrated developer tools for css analysis and script profiling and debugging. </p>
<p>And there is &#8216;Compatibility View&#8217;. Developers can specify, by adding a specific meta tag, that IE7 rendering should be used. There seem to be some tricky aspects related to Compatibility View:</p>
<ul>
<li>It&#8217;s <a href="http://blogs.msdn.com/ie/archive/2009/03/12/site-compatibility-and-ie8.aspx">not 100% compatible</a> with the &#8216;real&#8217; IE7</li>
<li>For intranets IE8 will behave differently, using <a href="http://blogs.msdn.com/ie/archive/2008/08/27/introducing-compatibility-view.aspx">smart defaults based on zone evaluation</a>. That by itself sounds alarming. What it means is that Microsoft (and sadly they&#8217;re right) assumes that a lot of intranets, also called line of business applications, will malfunction when confronted with a new browser.</li>
</ul>
<p>The latter will be something to take into account when developing and testing intranets. Another concern I heard about (and share) is that if a lot of developers will start using Compatibility View, a lot of bad practices will stick around and development for IE as a whole will &#8216;freeze&#8217;. Instead of move forward to a more standards compliant level.</p>
<p>As <a href="http://www.thecounter.com/stats/2009/February/browser.php">IE6 isn&#8217;t dead yet</a> there are now <em>three</em> IE versions to test for. Microsoft offers free downloadable virtual machines with IEx installed as a solution. Virtualization is &#8216;hot&#8217; but some might find Microsoft&#8217;s solution a bit of a hassle. <a href="http://my-debugbar.com/wiki/IETester/HomePage">IETester</a> looks like a nice alternative (haven&#8217;t tried it yet) altough it seems to require Vista.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2009/03/20/internet-explorer-8/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SQL injection &amp; the Kaspersky hack</title>
		<link>http://www.tibobeijen.nl/blog/2009/02/11/sql_injection-and-the-kaspersky-hack/</link>
		<comments>http://www.tibobeijen.nl/blog/2009/02/11/sql_injection-and-the-kaspersky-hack/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 18:38:50 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sql injection]]></category>

		<guid isPermaLink="false">http://www.tibobeijen.nl/?p=137</guid>
		<description><![CDATA[Last week I read an article on webwereld titled &#8216;2008 was year of the SQL injection attack&#8216;. It was based on an article with the same title on networkworld.com. Apparently SQL injection has taken over the lead from XSS. Not surprisingly the first user-comment stated that almost 100% of the exploits were certainly in PHP [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I read an article on webwereld titled &#8216;<a href="http://webwereld.nl/article/comments/id/54694">2008 was year of the SQL injection attack</a>&#8216;. It was based on an <a href="http://www.networkworld.com/news/2009/020209-sql-injection-attack.html">article with the same title on networkworld.com</a>. Apparently SQL injection has taken over the lead from XSS. Not surprisingly the first user-comment stated that almost 100% of the exploits were certainly in PHP applications written by would-be programmers. With things so obvious it&#8217;s of course unneccessary to provide factual data backing up such a statement. So, nothing to win in that discussion. Three days ago news came that <a href="http://www.theregister.co.uk/2009/02/08/kaspersky_compromise_report/">a customer database of Kaspersky was hacked</a>. By using SQL injection. On <a href="http://usa.kaspersky.com/about-us/news-press-releases.php?smnr_id=900000208">a PHP website</a>. Could commenter X be right?<br />
<span id="more-137"></span><br />
It <em>is</em> true that PHP allows novice as well as experienced programmers to achieve &#8216;what they want&#8217;. And the novice might adopt some very bad practices. But does that make PHP an insecure programming language? Compare it to driving a car. Yes, you can drive around like a maniac and wreak havoc on the unaware. But does that make a car an unsecure vehicle?  The PHP platform offers enough techniques to rule out SQL injection attacks:</p>
<ul>
<li>Prepared statements. There&#8217;s no need to concat potentially harmful data in SQL statements. Provide them as a parameter when executung a prepared statement. More efficient too.</li>
<li>ORM. If you think SQL should be behind an abstraction layer, you can do so. <a href="http://propel.phpdb.org/">Propel</a> and <a href="http://www.doctrine-project.org/">Doctrine</a> are the best known solutions for PHP.</li>
<li>Well structured programming. If, for some reason, you want to build queries the old way you can force upon yourself (or your team) some good practices. See example below.</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dataTainted</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'un'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'un'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dataTainted</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pw'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pw'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// escape all array values using a custom function of some sort</span>
<span style="color: #000088;">$dataSafe</span> <span style="color: #339933;">=</span> mysqlEscapeValues<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dataTainted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000cc; font-style: italic;">&lt;&lt;&lt;SQL
SELECT
  *
FROM
  users
WHERE
  un = &quot;{$dataSafe['un']}&quot;
AND
  pw = SHA1(&quot;{$dataSafe['pw']}&quot;)
SQL</span><span style="color: #339933;">;</span></pre></div></div>

<p>And that&#8217;s just the coding part of the story. In the area of application design a lot can be done to minimize the consequences of an exploit. For example, different database users can be set up that have different levels of access. Only parts of a website needing customer data then use the credentials that do provide such access.</p>
<p>So, for PHP there are good practices and solutions aplenty. For more information on SQL injection one can start at <a href="http://www.owasp.org/index.php/Guide_to_SQL_Injection">OWASP</a> or <a href="http://en.wikipedia.org/wiki/SQL_injection#Real-world_examples">Wikipedia</a>. And finally, the subject isn&#8217;t non-existent in <a href="http://msdn.microsoft.com/en-us/magazine/cc163917.aspx">.NET</a> and <a href="http://kb.adobe.com/selfservice/viewContent.do?externalId=585ac720">JSP</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2009/02/11/sql_injection-and-the-kaspersky-hack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

