<?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>Aviblock.com &#187; extension</title>
	<atom:link href="http://www.aviblock.com/blog/tag/extension/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.aviblock.com/blog</link>
	<description>My musings on web development</description>
	<lastBuildDate>Thu, 03 Jun 2010 13:40:47 +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>Templatizer</title>
		<link>http://www.aviblock.com/blog/2009/05/19/templatizer/</link>
		<comments>http://www.aviblock.com/blog/2009/05/19/templatizer/#comments</comments>
		<pubDate>Tue, 19 May 2009 14:07:04 +0000</pubDate>
		<dc:creator>Avi</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[add-on]]></category>
		<category><![CDATA[Expression Engine]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.aviblock.com/blog/?p=51</guid>
		<description><![CDATA[I released an extension the other day for EE. You can find it here.
The basic idea is that creating templates in Expression Engine, especially if you don&#8217;t plan to edit them from the CP, can sometimes be a little repetitive and annoying.
Normally with EE, the templates are stored in the database, and you have the [...]]]></description>
			<content:encoded><![CDATA[<p>I released an extension the other day for EE. You can find it <a href="http://expressionengine.com/forums/viewthread/114771/">here</a>.</p>
<p>The basic idea is that creating templates in Expression Engine, especially if you don&#8217;t plan to edit them from the CP, can sometimes be a little repetitive and annoying.<br />
Normally with EE, the templates are stored in the database, and you have the option of <i>also</i> storing them on the filesystem. But in order for them to be recognized by EE, they must reside in the database at least.</p>
<p>Using this extension, you can start your template off as a physical file, and the first time you view it in the browser, it will automatically be recognized by EE and inserted into the database!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aviblock.com/blog/2009/05/19/templatizer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flash Messenger for Expression Engine</title>
		<link>http://www.aviblock.com/blog/2009/05/07/flash-messenger-for-expression-engine/</link>
		<comments>http://www.aviblock.com/blog/2009/05/07/flash-messenger-for-expression-engine/#comments</comments>
		<pubDate>Thu, 07 May 2009 16:47:11 +0000</pubDate>
		<dc:creator>Avi</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[add-on]]></category>
		<category><![CDATA[Expression Engine]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[Flash Message]]></category>
		<category><![CDATA[Flash Messenger]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[sessions]]></category>

		<guid isPermaLink="false">http://www.aviblock.com/blog/?p=42</guid>
		<description><![CDATA[A common idiom in web development is that a form is submitted via POST, and then a redirect is done to a success page. The reason for this is that if we left the result of the submission to return HTML to the browser, we would run into the infamous and confusing message "To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier." (At least the way firefox tells you). This has become none as the <a href="http://en.wikipedia.org/wiki/Post/Redirect/Get">POST/REDIRECT/GET</a> pattern.

The problem becomes that, because of the stateless nature of HTTP, this second request has absolutely no connection to the previous request. If we just wanted to display a thank you page, or a success page, this would not be much of a problem. But what if we wanted to display the same form again, and display a success message on top when its submitted?

One way of doing this, which is the way Expression Engine does it, is to pass in a parameter in the query string, like ?success=1. The problem with this is that now if somebody refreshes the page, they'll see the same message. Not the biggest deal in the world, but can we do better?]]></description>
			<content:encoded><![CDATA[<p>A common idiom in web development is that a form is submitted via POST, and then a redirect is done to a success page. The reason for this is that if we left the result of the submission to return HTML to the browser, we would run into the infamous and confusing message &#8220;To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier.&#8221; (At least the way firefox tells you). This has become none as the <a href="http://en.wikipedia.org/wiki/Post/Redirect/Get">POST/REDIRECT/GET</a> pattern.</p>
<p>The problem becomes that, because of the stateless nature of HTTP, this second request has absolutely no connection to the previous request. If we just wanted to display a thank you page, or a success page, this would not be much of a problem. But what if we wanted to display the same form again, and display a success message on top when its submitted?</p>
<p>One way of doing this, which is the way Expression Engine does it, is to pass in a parameter in the query string, like ?success=1. The problem with this is that now if somebody refreshes the page, they&#8217;ll see the same message. Not the biggest deal in the world, but can we do better?<br />
<span id="more-42"></span><br />
The answer to all the statelessness of HTTP is with sessions. There is a pattern that takes advantage of this called a &#8220;<a href="http://blog.agilephp.com/2008/06/03/flash-messages/">flash message</a>&#8220;. The basic idea is to store a value in a session when the form is submitted, and to echo it back the first time the page is displayed again, after a succesful POST.</p>
<p>I wanted this functionality in some of my Expression Engine SAEF&#8217;s and other such forms. I also wanted a way to easily set up flash messages, within the form itself, so that I didn&#8217;t have to hack any  modules, or look for a hook. </p>
<p>Here is what the API looks like. I will pick on  solspaces {exp:user:edit} which I am using to create a edit profile form.</p>
<p>Image the following form:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;h1&gt;My Profile&lt;/h1&gt;
{exp:user:edit}
&lt;label for=&quot;first_name&quot;&gt;First Name&lt;/label&gt;
&lt;input name=&quot;first_name&quot; type=&quot;text&quot; value=&quot;{first_name}&quot; /&gt;
&lt;label for=&quot;last_name&quot;&gt;Last Name&lt;/label&gt;
&lt;input name=&quot;last_name&quot; type=&quot;text&quot; value=&quot;{last_name}&quot; /&gt;
&lt;input type=&quot;submit&quot; /&gt;
{/exp:user:edit}</pre></div></div>

<p>This creates a basic profile form where the user can change his first and last name (assuming these custom member variables were set up). If we submit this form, it will return right to were it came from, with no indication as to that anything happened.</p>
<p>So we will change it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;h1&gt;My Profile&lt;/h1&gt;
{flash_msg}
{exp:user:edit}
{exp:flash_msg msg=&quot;Profile edited succesfully!&quot;}
&lt;label for=&quot;first_name&quot;&gt;First Name&lt;/label&gt;
&lt;input name=&quot;first_name&quot; type=&quot;text&quot; value=&quot;{first_name}&quot; /&gt;
&nbsp;
&lt;label for=&quot;last_name&quot;&gt;Last Name&lt;/label&gt;
&lt;input name=&quot;last_name&quot; type=&quot;text&quot; value=&quot;{last_name}&quot; /&gt;
&lt;input type=&quot;submit&quot; /&gt;
{/exp:user:edit}</pre></div></div>

<p>Now when we submit the form, we are greeted with the message &#8220;Profile edited succesfully!&#8221;. Refresh the page, and its gone!</p>
<p>And now for the exciting technical part! How is this magic done?</p>
<p>First the general overview: The plugin creates a hidden form element who&#8217;s value is a random hash. At the same time this element is created, a session variable is created with this hash and the message, as well as a flag. There is a extension on the session_end hook which looks for the presence of this form element, and the session variable. If the two hashes are the same, the flag in the session variable is set, and the plugin knows it can display the session variable.</p>
<p>Here is the extension:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sessions_end<span style="color: #009900;">&#40;</span><span style="color: #000088;">$SESS</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">global</span> <span style="color: #000088;">$IN</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>REQ <span style="color: #339933;">==</span> <span style="color: #0000ff;">'ACTION'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$IN</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">GBL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flash-hash'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flash_msg'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flash-hash'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flash_msg'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'done'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I think that code is pretty self explanatory (or at least I explained it earlier).</p>
<p>Now the plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> Flash_msg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">global</span> <span style="color: #000088;">$TMPL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$FNS</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$FNS</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">random</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'encrypt'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flash_msg'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flash_msg'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'done'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$TMPL</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">template</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$TMPL</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">swap_var_single</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flash_msg'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flash_msg'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'msg'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$TMPL</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$TMPL</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">template</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$TMPL</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">swap_var_single</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flash_msg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$TMPL</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flash_msg'</span><span style="color: #009900;">&#93;</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'done'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;flash-hash&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$hash</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;msg&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$TMPL</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch_param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'msg'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">return_data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;input name=&quot;</span>flash<span style="color: #339933;">-</span>hash<span style="color: #0000ff;">&quot; type=&quot;</span>hidden<span style="color: #0000ff;">&quot; value=&quot;</span><span style="color: #000088;">$hash</span><span style="color: #0000ff;">&quot; /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Pretty straight forward, I think. The only interesting thing here is the line:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$TMPL</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">template</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$TMPL</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">swap_var_single</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flash_msg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$TMPL</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">template</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This takes advantage of some bad OOP design on Expression Engine&#8217;s part, so that my plugin can make changes, like variable substitution, anywhere in the template!</p>
<p>There&#8217;s one problem with all of this though. What if we set one of the fields in that form to required, and left it out? We would see the output of the Expression Engine&#8217;s error page, and when we went back, the flash message would appear! We need a way at to determine if the form was successful or not. Unfortunately, a successful submit is merely a semantic thing. Its up to the programmer to determine if was successful or not. I though about this problem for a long time, and then came up with an ingenious, almost hackish solution. If only there was a hook in the Output::show_user_error() method!</p>
<p>In absence of that I thought, why don&#8217;t I just subclass the Output class, and overwrite that method, and then replace the global $OUT variable with new version!</p>
<p>As simple as this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyOutput <span style="color: #000000; font-weight: bold;">extends</span> Output <span style="color: #009900;">&#123;</span>
	 <span style="color: #000000; font-weight: bold;">function</span> show_user_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'submission'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errors</span><span style="color: #339933;">,</span> <span style="color: #000088;">$heading</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flash_msg'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'done'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">show_user_error</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errors</span><span style="color: #339933;">,</span> <span style="color: #000088;">$heading</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This just simply overwrites the show_user_error method, resets the flag, and shores off back to its parent to do the rest!</p>
<p>I simply include this little class at the bottom of the extension and then we rewrite our extension method as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sessions_end<span style="color: #009900;">&#40;</span><span style="color: #000088;">$SESS</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">global</span> <span style="color: #000088;">$IN</span><span style="color: #339933;">,</span> <span style="color: #000088;">$OUT</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>REQ <span style="color: #339933;">==</span> <span style="color: #0000ff;">'ACTION'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$IN</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">GBL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flash-hash'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$OUT</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOutput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flash_msg'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flash-hash'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flash_msg'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'done'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All I&#8217;m doing now is replacing the global $OUT variable with my own subclassed Output class!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aviblock.com/blog/2009/05/07/flash-messenger-for-expression-engine/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
