<?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>Jon Lebensold &#187; free code</title>
	<atom:link href="http://lebensold.net/category/free-code/feed" rel="self" type="application/rss+xml" />
	<link>http://lebensold.net</link>
	<description>thoughts on web development, technology and media</description>
	<lastBuildDate>Tue, 22 Jun 2010 16:04:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Two level-deep WordPress sidebar pages</title>
		<link>http://lebensold.net/free-code/two-level-deep-wordpress-sidebar-pages</link>
		<comments>http://lebensold.net/free-code/two-level-deep-wordpress-sidebar-pages#comments</comments>
		<pubDate>Sat, 11 Oct 2008 10:38:09 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[free code]]></category>
		<category><![CDATA[freecode]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[sidebar]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/?p=233</guid>
		<description><![CDATA[I gave up trying to find a sidebar plugin in WordPress that would let me display sub-pages to the current page, so I ended up writing one very quickly. In the case of the code here, "Gallery" is a top-level page that is holding two levels of sub-pages for a website I'm putting together. The [...]]]></description>
			<content:encoded><![CDATA[<p>I gave up trying to find a sidebar plugin in WordPress that would let me display sub-pages to the current page, so I ended up writing one very quickly. In the case of the code here, "Gallery" is a top-level page that is holding two levels of sub-pages for a website I'm putting together. The functions should be placed in the themes' functions.php file.</p>
<p><i>NOTE: this is by no means perfect, its just a hack that I whipped up in an hour that I figured should have been doable without hacking WordPress. I woudn't suggest writing any kind of lower-level code like this, but seeing as WordPress uses SQL calls, at least keeping them in their respective functions limits the exposure to the view.</i></p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> parentParentIsGallery<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$parentId</span>  , <span style="color: #0000ff;">$galleryPageId</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<a href="http://www.php.net/global"><span style="color: #000066;">global</span></a> <span style="color: #0000ff;">$wpdb</span>;
	<span style="color: #0000ff;">$pr</span> = <span style="color: #0000ff;">$wpdb</span>-&gt;<span style="color: #006600;">get_row</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SELECT ID, post_title, post_status,
post_parent FROM $wpdb-&gt;posts WHERE ID = &quot;</span>.<span style="color: #0000ff;">$parentId</span>.<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/intval"><span style="color: #000066;">intval</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pr</span>-&gt;<span style="color: #006600;">post_parent</span><span style="color: #66cc66;">&#41;</span> == <a href="http://www.php.net/intval"><span style="color: #000066;">intval</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$galleryPageId</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> getSyblings<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<a href="http://www.php.net/global"><span style="color: #000066;">global</span></a> <span style="color: #0000ff;">$wpdb</span>;
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$wpdb</span>-&gt;<span style="color: #006600;">get_results</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SELECT ID, post_title, post_status,
post_parent FROM $wpdb-&gt;posts WHERE post_parent = $id AND post_status='publish'&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> myMenu<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$galleryPageId</span> , <span style="color: #0000ff;">$postId</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<a href="http://www.php.net/global"><span style="color: #000066;">global</span></a> <span style="color: #0000ff;">$post</span>;
&nbsp;
		<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span> getSyblings<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$galleryPageId</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$ps</span> <span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$post</span>-&gt;<span style="color: #006600;">ID</span> == <span style="color: #0000ff;">$ps</span>-&gt;<span style="color: #006600;">ID</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #0000ff;">$class</span> = <span style="color: #ff0000;">&quot;current_page_item&quot;</span>;
			<span style="color: #b1b100;">else</span>
				<span style="color: #0000ff;">$class</span>= <span style="color: #ff0000;">&quot;&quot;</span>;
			<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;
&lt;li class='$class' &gt;&lt;a href='&quot;</span>.get_permalink<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ps</span>-&gt;<span style="color: #006600;">ID</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">&quot;'&gt;$ps-&gt;post_title&lt;/a&gt;&lt;/li&gt;
&nbsp;
<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ps</span>-&gt;<span style="color: #006600;">ID</span> == <span style="color: #0000ff;">$postId</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;
&lt;ul class='subpage'&gt;&quot;</span>;
				<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span> getSyblings<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$postId</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$child</span> <span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
&nbsp;
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$post</span>-&gt;<span style="color: #006600;">ID</span> == <span style="color: #0000ff;">$child</span>-&gt;<span style="color: #006600;">ID</span><span style="color: #66cc66;">&#41;</span>
					<span style="color: #0000ff;">$class</span> = <span style="color: #ff0000;">&quot;current_subpage_item&quot;</span>;
				<span style="color: #b1b100;">else</span>
					<span style="color: #0000ff;">$class</span>= <span style="color: #ff0000;">&quot;&quot;</span>;				
&nbsp;
					<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;
&lt;li class='$class'&gt;&lt;a href='&quot;</span>.get_permalink<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$child</span>-&gt;<span style="color: #006600;">ID</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">&quot;'&gt;$child-&gt;post_title&lt;/a&gt;&lt;/li&gt;
&nbsp;
&quot;</span>;
&nbsp;
				<span style="color: #66cc66;">&#125;</span>
				<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;&lt;/ul&gt;
&nbsp;
&quot;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>And here's the relevant part of sidebar.php:</p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #0000ff;">$galleryPageId</span> = <span style="color: #cc66cc;">5</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$post</span>-&gt;<span style="color: #006600;">post_parent</span> == <span style="color: #0000ff;">$galleryPageId</span> || <span style="color: #0000ff;">$post</span>-&gt;<span style="color: #006600;">ID</span> == <span style="color: #0000ff;">$galleryPageId</span><span style="color: #66cc66;">&#41;</span>: <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;li <span style="color: #000000; font-weight: bold;">class</span>=<span style="color: #ff0000;">&quot;widget&quot;</span>&gt;
&lt;h2&gt;&lt;?php <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$parent_title</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span> <span style="color: #000000; font-weight: bold;">&lt;?php</span> _e<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>; ?&gt;&lt;/h2&gt;
&lt;ul <span style="color: #000000; font-weight: bold;">class</span>=<span style="color: #ff0000;">&quot;list-page&quot;</span>&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> myMenu<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$galleryPageId</span> , <span style="color: #0000ff;">$post</span>-&gt;<span style="color: #006600;">ID</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>
                      &lt;/ul&gt;
&lt;/li&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">elseif</span><span style="color: #66cc66;">&#40;</span>parentParentIsGallery<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$post</span>-&gt;<span style="color: #006600;">post_parent</span> , <span style="color: #0000ff;">$galleryPageId</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>: <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;li <span style="color: #000000; font-weight: bold;">class</span>=<span style="color: #ff0000;">&quot;widget&quot;</span>&gt;
&lt;h2&gt;&lt;?php <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$parent_title</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span> <span style="color: #000000; font-weight: bold;">&lt;?php</span> _e<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>; ?&gt;&lt;/h2&gt;
&lt;ul <span style="color: #000000; font-weight: bold;">class</span>=<span style="color: #ff0000;">&quot;list-page&quot;</span>&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> myMenu<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$galleryPageId</span> , <span style="color: #0000ff;">$post</span>-&gt;<span style="color: #006600;">post_parent</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>
                      &lt;/ul&gt;
&lt;/li&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #b1b100;">endif</span>;?&gt;
&nbsp;</pre>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/free-code/two-level-deep-wordpress-sidebar-pages/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A Unit of Work in PHP</title>
		<link>http://lebensold.net/php/a-unit-of-work-in-php</link>
		<comments>http://lebensold.net/php/a-unit-of-work-in-php#comments</comments>
		<pubDate>Tue, 05 Aug 2008 21:25:34 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[free code]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[poeaa]]></category>
		<category><![CDATA[unit of work]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/?p=105</guid>
		<description><![CDATA[A couple of days ago I was looking at the Repository Pattern and how you could implement it in PHP.
According to Evans' Domain-Driven-Design book, a Repository depends on a Unit of Work.
Why Bother Using A Unit of Work?
The Unit of Work is an enterprise design pattern that facilitates the persistence of many objects across a [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago I was looking at <a href='http://jon.lebensold.ca/zend/repository-pattern-in-php-not-quite-yet'>the Repository Pattern</a> and how you could implement it in PHP.</p>
<p>According to Evans' Domain-Driven-Design book, a Repository depends on a Unit of Work.</p>
<h1>Why Bother Using A Unit of Work?</h1>
<p>The Unit of Work is an enterprise design pattern that facilitates the persistence of many objects across a single or several data sources. It’s helpful to think of the Unit of Work as a way of putting everything that you would want to update, insert and delete into one bag before sending it to the database.<br />
Functionally, the Unit of Work simplifies transactional requests and helps mitigate concurrency issues on multi-user systems. </p>
<p>I wrote a Unit of Work that depends on Doctrine. Assuming we have a Project model with a name property, the consuming class can perform operations like so: </p>
<pre class="php">&nbsp;
<span style="color: #0000ff;">$t</span> = Doctrine::<span style="color: #006600;">getTable</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Project'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$lastProjects</span> = <span style="color: #0000ff;">$t</span>-&gt;<span style="color: #006600;">findByName</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'new project'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$unitOfWork</span> = <span style="color: #000000; font-weight: bold;">new</span> UnitOfWork<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// prepare an UPDATE</span>
    <span style="color: #0000ff;">$lastProjects</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>-&gt;<span style="color: #006600;">username</span> = <span style="color: #ff0000;">'old project'</span>;
    <span style="color: #0000ff;">$unitOfWork</span>-&gt;<span style="color: #006600;">RegisterModelForCreateOrUpdate</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$lastProjects</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// prepare a CREATE</span>
    <span style="color: #0000ff;">$project</span> = <span style="color: #000000; font-weight: bold;">new</span> Project<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$project</span>-&gt;<span style="color: #006600;">name</span> = <span style="color: #ff0000;">'new project name'</span>;
&nbsp;
    <span style="color: #0000ff;">$unitOfWork</span>-&gt;<span style="color: #006600;">RegisterModelForCreateOrUpdate</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$project</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// prepare a DELETE</span>
    <span style="color: #0000ff;">$unitOfWork</span>-&gt;<span style="color: #006600;">RegisterModelForDelete</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$lastProjects</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// perform the transaction</span>
<span style="color: #0000ff;">$unitOfWork</span>-&gt;<span style="color: #006600;">CommitAll</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>A commit already leverages the existing Doctrine connection and handles the transaction for you, ensuring that the Unit of Work either commits, or throws an Exception.</p>
<p>The UnitOfWork is also an excellent place to include logging code or event additional column data (such as UpdatedAt and UpdatedBy which might exist in all Doctrine models for concurrency / logging purposes.) </p>
<p>I haven't found a better way of doing object-comparison in PHP arrays, however I've pulled out that code into existsInCollections() if anyone has any ideas.</p>
<pre class="php">&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> UnitOfWork <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Collection of models to be persisted
	 *
	 * @var array Doctrine_Record
	 */</span>
	protected <span style="color: #0000ff;">$createOrUpdateCollection</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Collection of models to be persisted
	 *
	 * @var array Doctrine_Record
	 */</span>
	protected <span style="color: #0000ff;">$deleteCollection</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Add a model object to the create collection
	 *
	 * @param Doctrine_Record $model
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> RegisterModelForCreateOrUpdate<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$model</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// code to check to see if the model exists already</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">existsInCollections</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$model</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		    throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'model already in another collection for this transaction'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// no? add it</span>
			<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">createOrUpdateCollection</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$model</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Add a model object to the delete collection
	 *
	 * @param Doctrine_Record $model
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> RegisterModelForDelete<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$model</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// code to check to see if the model exists already</span>
		    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">existsInCollections</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$model</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    		    throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'model already in another collection for this transaction'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// no? add it</span>
			<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">deleteCollection</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$model</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Clear the Unit of Work
	 *
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ClearAll<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">deleteCollection</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">createOrUpdateCollection</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Perform a Commit and clear the Unit Of Work. Throw an Exception if it fails and roll back.
	 *
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> CommitAll<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
    	<span style="color: #0000ff;">$manager</span> = Doctrine_Manager::<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    	<span style="color: #0000ff;">$conn</span> = <a href="http://www.php.net/array_pop"><span style="color: #000066;">array_pop</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$manager</span>-&gt;<span style="color: #006600;">getConnections</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
    	try <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$conn</span>-&gt;<span style="color: #006600;">beginTransaction</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">performCreatesOrUpdates</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$conn</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">performDeletes</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$conn</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #0000ff;">$conn</span>-&gt;<span style="color: #006600;">commit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    	<span style="color: #66cc66;">&#125;</span>
    	catch<span style="color: #66cc66;">&#40;</span>Doctrine_Exception <span style="color: #0000ff;">$e</span><span style="color: #66cc66;">&#41;</span>
    	<span style="color: #66cc66;">&#123;</span>
    	    <span style="color: #0000ff;">$conn</span>-&gt;<span style="color: #006600;">rollback</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>	    
&nbsp;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">ClearAll</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> performCreatesOrUpdates<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$conn</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
	    <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">createOrUpdateCollection</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$model</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #0000ff;">$model</span>-&gt;<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$conn</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> performDeletes<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$conn</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">deleteCollection</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$model</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #0000ff;">$model</span>-&gt;<span style="color: #006600;">delete</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$conn</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> existsInCollections<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$model</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
	   <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">createOrUpdateCollection</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$m</span><span style="color: #66cc66;">&#41;</span>
	   <span style="color: #66cc66;">&#123;</span>
	        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$model</span>-&gt;__toString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #0000ff;">$m</span>-&gt;__toString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
	   <span style="color: #66cc66;">&#125;</span>
&nbsp;
	   <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">deleteCollection</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$m</span><span style="color: #66cc66;">&#41;</span>
	   <span style="color: #66cc66;">&#123;</span>
	        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$model</span>-&gt;__toString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #0000ff;">$m</span>-&gt;__toString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
	   <span style="color: #66cc66;">&#125;</span>
&nbsp;
	   <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/php/a-unit-of-work-in-php/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Zend with Flickr: random photos from users or groups</title>
		<link>http://lebensold.net/development/zend-with-flickr-random-photos-from-users-or-groups</link>
		<comments>http://lebensold.net/development/zend-with-flickr-random-photos-from-users-or-groups#comments</comments>
		<pubDate>Mon, 07 Jul 2008 23:15:25 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[free code]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/?p=94</guid>
		<description><![CDATA[I'm currently working on a project that involves pulling photos randomly. While my initial programmer instinct tells me I should write something that would enable my client to upload photos locally. However, there are some missed social networking opportunities when you decide to host your own images.
By putting the photos on Flickr, my client can [...]]]></description>
			<content:encoded><![CDATA[<p>I'm currently working on a project that involves pulling photos randomly. While my initial programmer instinct tells me I should write something that would enable my client to upload photos locally. However, there are some missed social networking opportunities when you decide to host your own images.</p>
<p>By putting the photos on Flickr, my client can keep posting photos and build a pool of images that other people can contribute to. From a development perspective, all I needed to do was leverage the Zend_Service_Flickr classes and write a simple wrapper.</p>
<p><iframe src="http://jon.lebensold.ca/demo/ZendFlickrRandomPhotos/" width="600" height="420"></iframe></p>
<p>The frontend page looks like this:</p>
<pre class="php">&nbsp;
<span style="color: #0000ff;">$randomPhotos</span> = <span style="color: #000000; font-weight: bold;">new</span> RandomPhotosFromFlickr<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'YOUR_API_KEY'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$numberOfPhotos</span> = <span style="color: #cc66cc;">3</span>;
&nbsp;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;
&lt;h2&gt;Group Photos&lt;/h2&gt;
&nbsp;
&quot;</span>;
<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$randomPhotos</span>-&gt;<span style="color: #006600;">FromGroup</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span>, <span style="color: #ff0000;">&quot;GROUP@ID&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$p</span><span style="color: #66cc66;">&#41;</span>
	<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;img src='&quot;</span>.<span style="color: #0000ff;">$p</span>-&gt;<span style="color: #006600;">Small</span>-&gt;<span style="color: #006600;">uri</span>.<span style="color: #ff0000;">&quot;' /&gt;&quot;</span>;
&nbsp;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;
&lt;h2&gt;User's Photos&lt;/h2&gt;
&nbsp;
&quot;</span>;
<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$randomPhotos</span>-&gt;<span style="color: #006600;">FromUser</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span>, <span style="color: #ff0000;">&quot;user@yahoo.com&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$p</span><span style="color: #66cc66;">&#41;</span>
	<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;img src='&quot;</span>.<span style="color: #0000ff;">$p</span>-&gt;<span style="color: #006600;">Small</span>-&gt;<span style="color: #006600;">uri</span>.<span style="color: #ff0000;">&quot;' /&gt;&quot;</span>;
&nbsp;</pre>
<p>In this example, $p is a <a href="http://framework.zend.com/manual/en/zend.service.flickr.html#zend.service.flickr.classes.image">Zend_Service_Flickr object</a>, so you can pull any kind of data while letting the simple class I wrote handle the selection of random photos:</p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> RandomPhotosFromFlickr
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/**
	 * API key for Flickr
	 *
	 * @var string
	 */</span>
	protected <span style="color: #0000ff;">$apiKey</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * photoset used for retrieving random photos
	 *
	 * @var Zend_Service_Flickr_ResultSet
	 */</span>
	protected <span style="color: #0000ff;">$currentPhotoset</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * number of photos in the current photoset
	 *
	 * @var integer
	 */</span>
	protected <span style="color: #0000ff;">$totalPhotoCount</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Class constructor
	 * We need your Flickr API Key
	 * Go to http://www.flickr.com/services/api/keys/apply/ to get one
	 * @param string $apiKey
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$apiKey</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">apiKey</span> = <span style="color: #0000ff;">$apiKey</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Supply a username (a user's email address) and the number of photos you'd like returned
	 *
	 * @param integer $numberOfPhotos
	 * @param string $username
	 * @return Zend_Service_Flickr_ResultSet
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FromUser<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span>, <span style="color: #0000ff;">$username</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$flickr</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Flickr<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">apiKey</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span> = <span style="color: #0000ff;">$flickr</span>-&gt;<span style="color: #006600;">userSearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$username</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span>-&gt;<span style="color: #006600;">totalResultsAvailable</span>;
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span> = <span style="color: #0000ff;">$flickr</span>-&gt;<span style="color: #006600;">userSearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$username</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'per_page'</span> =&gt; <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getPhotosFromCurrentPhotoset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Supply a group ID and the number of photos you'd like returned
	 *
	 * @param integer $numberOfPhotos
	 * @param string $groupId
	 * @return Zend_Service_Flickr_ResultSet
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FromGroup<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span>, <span style="color: #0000ff;">$groupId</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0000ff;">$flickr</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Flickr<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">apiKey</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span> = <span style="color: #0000ff;">$flickr</span>-&gt;<span style="color: #006600;">groupPoolGetPhotos</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$groupId</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span>-&gt;<span style="color: #006600;">totalResultsAvailable</span>;
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span> = <span style="color: #0000ff;">$flickr</span>-&gt;<span style="color: #006600;">groupPoolGetPhotos</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$groupId</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'per_page'</span> =&gt; <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getPhotosFromCurrentPhotoset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Return an integer within the photo index range
	 *
	 * @return integer
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getRandomPhotoIndex<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/rand"><span style="color: #000066;">rand</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span> , <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span> - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Get a photo object from the Flickr Web Service
	 * Provide an indice from the current photo set
	 *
	 * @param integer $index
	 * @return Zend_Service_Flickr_Image
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getPhoto<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$index</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span>-&gt;<span style="color: #006600;">seek</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$index</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span>-&gt;<span style="color: #006600;">current</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * using $this-&gt;currentPhotoset, grab a collection of random photos
	 *
	 * @param integer $numberOfPhotos
	 * @return Zend_Service_Flickr_ResultSet
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getPhotosFromCurrentPhotoset<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// first collect up a list of IDs (making sure there are no duplicates</span>
		<span style="color: #0000ff;">$photoIds</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$photoIds</span><span style="color: #66cc66;">&#41;</span> &lt; <span style="color: #0000ff;">$numberOfPhotos</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0000ff;">$id</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getRandomPhotoIndex</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>! <a href="http://www.php.net/in_array"><span style="color: #000066;">in_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span>, <span style="color: #0000ff;">$photoIds</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #0000ff;">$photoIds</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$id</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">$randomPhotos</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// query the web service to grab the photo objects</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$photoIds</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #0000ff;">$randomPhotos</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getPhoto</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$randomPhotos</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>you can <a href="http://jon.lebensold.ca/demo/ZendFlickrRandomPhotos/RandomPhotosFromFlickr.zip">download the full source</a> if you'd like to try it yourself. Please note that I've omitted bundling my code with a copy of the Zend Framework.</p>
<p>Using Zend Framework's Zend_Service_Flickr </p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/development/zend-with-flickr-random-photos-from-users-or-groups/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Decoupled validation with Zend_Form, Zend_Validate and Zend MVC</title>
		<link>http://lebensold.net/development/decoupled-validation-with-zend_form-zend_validate-and-zend-mvc</link>
		<comments>http://lebensold.net/development/decoupled-validation-with-zend_form-zend_validate-and-zend-mvc#comments</comments>
		<pubDate>Wed, 04 Jun 2008 19:56:32 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[free code]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/development/decoupled-validation-with-zend_form-zend_validate-and-zend-mvc</guid>
		<description><![CDATA[Lately I've been experimenting a lot with trying to serialize a form and send it over the wire as a means of doing asynchronous postback of partially validated forms. 
I've come up with a little prototype that's a PHP version of an ASP.NET implementation I did a couple months ago for a project I was [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I've been experimenting a lot with trying to serialize a form and send it over the wire as a means of doing asynchronous postback of partially validated forms. </p>
<p>I've come up with a little prototype that's a PHP version of an ASP.NET implementation I did a couple months ago for a project I was working on. </p>
<p>The PHP version was surprisingly easier to implement, due to the simplicity of Zend_Form when compared to the bulldozer we-control-everything-and-rewrite-your-user-control-id's approach of ASP.NET forms.</p>
<p>Essentially, I've created a class with a simple form (1 lookup list (one-to-many for the data modelers out there) and a couple of simple textboxes).</p>
<p>While I think that it's crucial to decouple business rules from the actual form implementation, I think that Zend_Validate validators, as well as custom validators, are an excellent way of creating testable, declaritive validation rules that can be applied to form elements as you see fit. The other decoupling you'll find in this example is from the postback model, essentially allowing you to drop the same form into any part of a Zend Framework application and posting to an Ajax Controller service for validation purposes. What's nice about this is that no business rules are left on the client. I've used prototype for a little styling.</p>
<p>The finishing touch in all of this is that I can leverage the server-side validation routine for both the asynchronous and synchronous calls, so that postback and validation run through the same validation code, essentially providing a second interface with to the same form object.</p>
<p>I'm going to look into extending this example to provide not only one-to-one mapping and one-to-many mapping (lookup lists), but also many-to-one mapping (generating sub-forms dynamically and performing partial validation). Not sure exactly how to do that, so comments would be greatly appreciated!</p>
<p>Here's a quick demonstration.<br />
<iframe src="http://jon.lebensold.ca/demo/decoupled-validation/html/" width="550" height="450" border="0"></iframe></p>
<p>and now for the code. The webservice bit is all contained in the IndexController for the sake of brevity. In a full blown application, you would likely have a Controller (or several controllers) that would offer different asynchronous entry points into your MVC framework.</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> IndexController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">f</span> = <span style="color: #000000; font-weight: bold;">new</span> RegistrationForm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">setMode</span> = <span style="color: #ff0000;">&quot;setMode($('wsTab').childNodes[0]);&quot;</span>;
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">f</span>-&gt;<span style="color: #006600;">isValid</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_POST</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">render</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;FormSuccess&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_request-&gt;<span style="color: #006600;">isPost</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">errorElements</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">f</span>-&gt;<span style="color: #006600;">getMessages</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">setMode</span> = <span style="color: #ff0000;">&quot;setMode($('pbTab').childNodes[0]);&quot;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Ajax JSON call is made through this controller
	 *
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> asyncAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//prepare a JSON response</span>
		<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">viewRenderer</span>-&gt;<span style="color: #006600;">setNoRender</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">getHelper</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'layout'</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">disableLayout</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
     	<span style="color: #808080; font-style: italic;">//Map the form from the client-side call</span>
     	<span style="color: #0000ff;">$myFormData</span> = Zend_Json::<span style="color: #006600;">decode</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">getParam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fs&quot;</span><span style="color: #66cc66;">&#41;</span> , Zend_Json::<span style="color: #006600;">TYPE_ARRAY</span><span style="color: #66cc66;">&#41;</span>;
     	<span style="color: #0000ff;">$form</span> = <span style="color: #000000; font-weight: bold;">new</span> RegistrationForm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
     	<span style="color: #0000ff;">$form</span>-&gt;<span style="color: #006600;">isValid</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$myFormData</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
     	<span style="color: #808080; font-style: italic;">//return the result</span>
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getResponse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">setHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Content-Type'</span>, <span style="color: #ff0000;">'application/json'</span><span style="color: #66cc66;">&#41;</span>
							   -&gt;<span style="color: #006600;">setBody</span><span style="color: #66cc66;">&#40;</span>Zend_Json::<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$form</span>-&gt;<span style="color: #006600;">getMessages</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>You'll notice that I've pulled out the form into its own class (RegistrationForm): </p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> RegistrationForm <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
    	<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setAttrib</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;id&quot;</span> , <span style="color: #ff0000;">&quot;frm&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    	<span style="color: #0000ff;">$onetomany</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Select<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'sad'</span><span style="color: #66cc66;">&#41;</span>;
    	<span style="color: #0000ff;">$onetomany</span>-&gt;<span style="color: #006600;">setLabel</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'One to many:'</span><span style="color: #66cc66;">&#41;</span>
    			  -&gt;<span style="color: #006600;">setMultiOptions</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getOneToMany</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    			  -&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Onetomany<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$usernameRequiredValidator</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_NotEmpty<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$usernameRequiredValidator</span>-&gt;<span style="color: #006600;">setMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Please enter a username.&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$usernameStringLengthValidator</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_StringLength<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span>,<span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$usernameStringLengthValidator</span>-&gt;<span style="color: #006600;">setMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Username is too short! (8 character minimum)&quot;</span>, Zend_Validate_StringLength::<span style="color: #006600;">TOO_SHORT</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$usernameStringLengthValidator</span>-&gt;<span style="color: #006600;">setMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Username is too long! (less than 20 characters)&quot;</span>, Zend_Validate_StringLength::<span style="color: #006600;">TOO_LONG</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    	<span style="color: #0000ff;">$username</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Text<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'username'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$username</span>-&gt;<span style="color: #006600;">setLabel</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Username:'</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$usernameRequiredValidator</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$usernameStringLengthValidator</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">setRequired</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    	<span style="color: #0000ff;">$name</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Text<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$name</span> -&gt;<span style="color: #006600;">setLabel</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Name:'</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'StringLength'</span>, <span style="color: #000000; font-weight: bold;">true</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">setRequired</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;    	
&nbsp;
        <span style="color: #0000ff;">$password</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Password<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'password'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$password</span>-&gt;<span style="color: #006600;">setLabel</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'password:'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">addElements</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        	<span style="color: #0000ff;">$onetomany</span>,
            <span style="color: #0000ff;">$username</span>,
            <span style="color: #0000ff;">$name</span>,
            <span style="color: #0000ff;">$password</span>
&nbsp;
        <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setDecorators</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        	<span style="color: #ff0000;">'FormElements'</span>,
            <span style="color: #ff0000;">'Fieldset'</span>,
            <span style="color: #ff0000;">'Form'</span>
&nbsp;
        <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getElements</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$element</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
        	<span style="color: #0000ff;">$element</span>-&gt;<span style="color: #006600;">setDecorators</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
                     <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ViewHelper'</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'class'</span> =&gt; <span style="color: #ff0000;">'formText'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
                     <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Label'</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'class'</span> =&gt; <span style="color: #ff0000;">'label'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
            		 <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'HtmlTag'</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'tag'</span> =&gt; <span style="color: #ff0000;">'dd'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #0000ff;">$element</span>-&gt;<span style="color: #006600;">class</span> = <span style="color: #ff0000;">'formText'</span>;
&nbsp;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getOneToMany<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$ar</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;please select&quot;</span>;
		<span style="color: #0000ff;">$ar</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;car&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;car&quot;</span>;
		<span style="color: #0000ff;">$ar</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;rollerBlade&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;roller blades&quot;</span>;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$ar</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>In my post about enterprise validation, I described a scenario where you would create custom validators that could be applied declaratively in C# using the Enterprise Library. The same thinking was applied here with the One To Many by creating a custom validator. This allows you to test the validation (and your business rules) without thinking about the form, the UI or even session:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Onetomany <span style="color: #000000; font-weight: bold;">extends</span> Zend_Validate_Abstract
<span style="color: #66cc66;">&#123;</span>
    const NOT_VALID  = <span style="color: #ff0000;">'stringLengthTooLong'</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * @var array
     */</span>
    protected <span style="color: #0000ff;">$_messageTemplates</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        self::<span style="color: #006600;">NOT_VALID</span> =&gt; <span style="color: #ff0000;">&quot;one to many was not valid choice&quot;</span>,
&nbsp;
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * @var array
     */</span>
    protected <span style="color: #0000ff;">$_messageVariables</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <span style="color: #ff0000;">'not_valid'</span> =&gt; <span style="color: #ff0000;">'_not_valid'</span>,
&nbsp;
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    protected <span style="color: #0000ff;">$_not_valid</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * Returns the min option
     *
     * @return integer
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getNotValid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;_not_valid;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * Defined by Zend_Validate_Interface
     * @param  string $value
     * @return boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> isValid<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// more business rules would go somewhere here...</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$value</span> == <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #0000ff;">$this</span>-&gt;_error<span style="color: #66cc66;">&#40;</span>self::<span style="color: #006600;">NOT_VALID</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_messages<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
        <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The last bit of magic is the Javascript. My weapon of choice (as mentioned above) is prototype, however I'm sure you could do the same thing with any other javascript framework:</p>
<pre class="javascript">Event.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span>window , <span style="color: #3366CC;">&quot;load&quot;</span> , <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
		&lt;?= $this-&gt;setMode ?&gt;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">function</span> attachWebServiceObserver<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> url = <span style="color: #3366CC;">&quot;/index/async/&quot;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> f = JSON.<span style="color: #006600;">stringify</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'frm'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">serialize</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #006600;">Request</span><span style="color: #66cc66;">&#40;</span>url , <span style="color: #66cc66;">&#123;</span>
			method: <span style="color: #3366CC;">'post'</span>,
			parameters: <span style="color: #3366CC;">'fs='</span> + f,
			onComplete : doAjaxCall.<span style="color: #006600;">bindAsEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> attachPostBackObserver<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'frm'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">submit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//this ID comes from the RegistrationForm class</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> setMode<span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	$$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.tab'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>emt<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> emt.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span>background : <span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	e.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>background :<span style="color: #3366CC;">&quot;#FFFF99&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!FIRST_LOAD<span style="color: #66cc66;">&#41;</span>
		clearErrors<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>e.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">id</span> ==<span style="color: #3366CC;">&quot;wsTab&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		Event.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span>, <span style="color: #3366CC;">'click'</span>, attachWebServiceObserver<span style="color: #66cc66;">&#41;</span>;
		Event.<span style="color: #006600;">stopObserving</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span>, <span style="color: #3366CC;">'click'</span>, attachPostBackObserver<span style="color: #66cc66;">&#41;</span>;
		$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span> = <span style="color: #3366CC;">&quot;Submit Asynchronously&quot;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>e.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">id</span> ==<span style="color: #3366CC;">&quot;pbTab&quot;</span> <span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		Event.<span style="color: #006600;">stopObserving</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span>, <span style="color: #3366CC;">'click'</span>, attachWebServiceObserver<span style="color: #66cc66;">&#41;</span>;
		Event.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span>,<span style="color: #3366CC;">'click'</span>, attachPostBackObserver<span style="color: #66cc66;">&#41;</span>;
		$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span> = <span style="color: #3366CC;">&quot;Submit With Postback&quot;</span>;
	<span style="color: #66cc66;">&#125;</span>
	FIRST_LOAD = <span style="color: #003366; font-weight: bold;">false</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> doAjaxCall<span style="color: #66cc66;">&#40;</span>tr<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	clearErrors<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>formIsValid<span style="color: #66cc66;">&#40;</span>tr.<span style="color: #006600;">responseJSON</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'frm'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">submit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	displayErrorMessages<span style="color: #66cc66;">&#40;</span>tr.<span style="color: #006600;">responseJSON</span><span style="color: #66cc66;">&#41;</span>;	
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> clearErrors<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'dvPostbackErrors'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">update</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'dvPostbackErrors'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	$$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;.errormessage&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		Element.<span style="color: #006600;">remove</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	$$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;input.formText &quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		e.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>border : <span style="color: #3366CC;">&quot;1px solid #CCC&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	$$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;select.formText &quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		e.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>border : <span style="color: #3366CC;">&quot;1px solid #CCC&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> formIsValid<span style="color: #66cc66;">&#40;</span>errorJson<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>isNaN<span style="color: #66cc66;">&#40;</span>errorJson<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> displayErrorMessages<span style="color: #66cc66;">&#40;</span>errorJson<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span>formElement <span style="color: #000066; font-weight: bold;">in</span> errorJson<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> elementObj = <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;errorJson.&quot;</span>+formElement<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span>error <span style="color: #000066; font-weight: bold;">in</span> elementObj<span style="color: #66cc66;">&#41;</span>
			displayError<span style="color: #66cc66;">&#40;</span>formElement , <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;errorJson.&quot;</span>+formElement+<span style="color: #3366CC;">&quot;.&quot;</span>+error<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> displayError<span style="color: #66cc66;">&#40;</span>emt , msg<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> tpl = <span style="color: #3366CC;">&quot;&lt;span class='errormessage' &gt;&quot;</span>+msg+<span style="color: #3366CC;">&quot;&lt;/span&gt;&quot;</span>;
&nbsp;
	markInvalidTextbox<span style="color: #66cc66;">&#40;</span>emt<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	Element.<span style="color: #006600;">insert</span><span style="color: #66cc66;">&#40;</span>emt , <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;after&quot;</span> : tpl <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> markInvalidTextbox<span style="color: #66cc66;">&#40;</span>emt<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	$<span style="color: #66cc66;">&#40;</span>emt<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>border: <span style="color: #3366CC;">&quot;2px solid red&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>In the version that you can download, I include the <a href="http://www.json.org/js.html">JSON.stringify class</a> (courtesy of Crawford) to serialize the form and send it the AsyncAction in my IndexController.</p>
<p>This architectural approach allows you to keep all the validation rules server-side, while allowing you to pick and choose when and how you postback. Because this example works with both postback and asynchronous calls, it's a little verbose. </p>
<p>Regardless, you can <a href="http://jon.lebensold.ca/wp-content/uploads/2008/AsyncValidationManager.zip">download the whole project and try it out for yourself</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/development/decoupled-validation-with-zend_form-zend_validate-and-zend-mvc/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Demo: AJAX Autocompleter with Script.aculo.us and a Zend JSON web service</title>
		<link>http://lebensold.net/creative-stuff/demo-ajax-autocompleter-with-scriptaculous-and-a-zend-json-web-service</link>
		<comments>http://lebensold.net/creative-stuff/demo-ajax-autocompleter-with-scriptaculous-and-a-zend-json-web-service#comments</comments>
		<pubDate>Mon, 24 Mar 2008 09:16:52 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Creative stuff]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[free code]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/creative-stuff/demo-ajax-autocompleter-with-scriptaculous-and-a-zend-json-web-service</guid>
		<description><![CDATA[Today I'm posting a sample project of a modification of a Script.aculo.us auto-completer script that will work with a Zend JSON web service. This example could easily be expanded by providing a richer data collection to the client (currently it's just a string array).
Here's a demo:





    

Implementing the code is really only [...]]]></description>
			<content:encoded><![CDATA[<p>Today I'm posting a sample project of a <a href="http://wiki.script.aculo.us/scriptaculous/show/Autocompleter.Local">modification of a Script.aculo.us auto-completer script</a> that will work with a Zend JSON web service. This example could easily be expanded by providing a richer data collection to the client (currently it's just a string array).</p>
<p>Here's a demo:</p>
<link rel="stylesheet" type="text/css" href="/demo/html/public/css/autocomplete.css" >
<p><script type="text/javascript" src="/demo/html/public/js/prototype.js"></script></p>
<p><script type="text/javascript" src="/demo/html/public/js/scriptaculous-1.8/scriptaculous.js"></script></p>
<p><script type="text/javascript" src="/demo/html/public/js/autocompleter.js"></script></p>
<input id="tbAutoCompleter" name="tbAutoCompleter" />
<div id="dvAutoCompleter" class="auto_complete" >    </div>
<p><script type="text/javascript" src="/demo/wp_ac.js"></script></p>
<p>Implementing the code is really only one line once you have the back end web service configured:</p>
<pre class="javascript"> <span style="color: #003366; font-weight: bold;">var</span> demoAutocompleter = <span style="color: #003366; font-weight: bold;">new</span> MyAjaxAutocompleter<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'tbAutoCompleter'</span>, <span style="color: #3366CC;">'dvAutoCompleter'</span>, <span style="color: #3366CC;">'/demo/html/Ajaxservice/index'</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>where 'tbAutoCompleter' corresponds to the textbox you wish to bind, 'dvAutoCompleter' is the control in question and '/demo/html/Ajaxservice/index' is the actual Web service giving you JSON data.</p>
<p>I'm still having a hard time POSTing to a Zend controller in a manner as elegant as you would find in ASP.NET's web services, however the code isn't really that bad. My AjaxServiceController looks like this:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> AjaxserviceController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">contextSwitch</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
             -&gt;<span style="color: #006600;">addActionContext</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'index'</span>, <span style="color: #ff0000;">'json'</span><span style="color: #66cc66;">&#41;</span>
             -&gt;<span style="color: #006600;">initContext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
       	<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">viewRenderer</span>-&gt;<span style="color: #006600;">setNoRender</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
       	<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">getHelper</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'layout'</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">disableLayout</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
       <span style="color: #808080; font-style: italic;">/* [Json response] */</span>
		 <span style="color: #0000ff;">$data</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;2008&quot;</span>,<span style="color: #ff0000;">&quot;3d&quot;</span>,<span style="color: #ff0000;">&quot;social&quot;</span>,<span style="color: #ff0000;">&quot;software&quot;</span>,<span style="color: #ff0000;">&quot;technology&quot;</span>,<span style="color: #ff0000;">&quot;tips&quot;</span>,
<span style="color: #ff0000;">&quot;tools&quot;</span>,<span style="color: #ff0000;">&quot;toread&quot;</span>,<span style="color: #ff0000;">&quot;travel&quot;</span>,<span style="color: #ff0000;">&quot;tutorial&quot;</span>,
<span style="color: #ff0000;">&quot;tutorials&quot;</span>,<span style="color: #ff0000;">&quot;tv&quot;</span>,<span style="color: #ff0000;">&quot;typography&quot;</span>, <span style="color: #ff0000;">&quot;wiki&quot;</span>,<span style="color: #ff0000;">&quot;windows&quot;</span>,<span style="color: #ff0000;">&quot;wishlist&quot;</span>,<span style="color: #ff0000;">&quot;wordpress&quot;</span>
,<span style="color: #ff0000;">&quot;work&quot;</span>,<span style="color: #ff0000;">&quot;writing&quot;</span>,<span style="color: #ff0000;">&quot;youtube&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		 <span style="color: #0000ff;">$responseData</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">array_isearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">getParam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'search'</span><span style="color: #66cc66;">&#41;</span> , <span style="color: #0000ff;">$data</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
       try <span style="color: #66cc66;">&#123;</span>
&nbsp;
           <span style="color: #0000ff;">$responseDataJsonEncoded</span> = Zend_Json::<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$responseData</span><span style="color: #66cc66;">&#41;</span>;
           <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getResponse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">setHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Content-Type'</span>, <span style="color: #ff0000;">'application/json'</span><span style="color: #66cc66;">&#41;</span>
							   -&gt;<span style="color: #006600;">setBody</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$responseDataJsonEncoded</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
       <span style="color: #66cc66;">&#125;</span> catch<span style="color: #66cc66;">&#40;</span>Zend_Json_Exception <span style="color: #0000ff;">$e</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
           <span style="color: #808080; font-style: italic;">// handle and generate HTTP error code response, see below</span>
       <span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> array_isearch<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$str</span>, <span style="color: #0000ff;">$array</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    	<span style="color: #0000ff;">$returnArray</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color: #000066;">preg_match</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/$str/i'</span>,<span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            	<span style="color: #0000ff;">$returnArray</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$v</span>;
&nbsp;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$returnArray</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>There's not much here, mostly just disabling the Zend Layout, specifying the content and doing the search. The $data array could easily be replaced with a data source (like a collection from Zend_Db).</p>
<p><center><a style="border: 0px none #FFF;" href="/demo/SampleAutoCompletionZend15.zip"><img  style="border: 0px none #FFF;"src="/wp-content/uploads/2008/downloadSearchIcon.png" /><br />
<br />
Download Ajax Auto Completer Sample Zend Framework 1.5 project<br />
</a></center></p>
<p>The nice thing about web service AJAX controls is that they live independently of the application calling them, so you can essentially separate out your data layer in the same way that you would separate Zend_Db resources from a Zend_Controller.</p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/creative-stuff/demo-ajax-autocompleter-with-scriptaculous-and-a-zend-json-web-service/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
