<?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>Flipflops.org &#187; PHP</title>
	<atom:link href="http://www.flipflops.org/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.flipflops.org</link>
	<description>Flipflops.org is about web development and fairly conceptual art</description>
	<lastBuildDate>Tue, 31 Jan 2012 23:26:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>XPath to the rescue. Again?</title>
		<link>http://www.flipflops.org/2011/09/24/xpath-to-the-rescue-again/</link>
		<comments>http://www.flipflops.org/2011/09/24/xpath-to-the-rescue-again/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 20:33:07 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[XPath]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/?p=408</guid>
		<description><![CDATA[It&#8217;s odd. I don&#8217;t think about think about XPath from one month to the next. But once in a while, when my usual solutions have all come up blank. Ta-Da XPath to the rescue! Recently as part of a site I was working on, the design basically required that I inject a block of content [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s odd. I don&#8217;t think about think about XPath from one month to the next. But once in a while, when my usual solutions have all come up blank. Ta-Da XPath to the rescue!</p>
<p>Recently as part of a site I was working on, the design basically required that I inject a block of content into a nested list (part of an elaborate menu actually) &#8211; bit of a fiddle because modifying the code that generated the list was not an option.</p>
<p>My first avenues of attack were just simple str_replace() and a regex replace, but I just couldn&#8217;t get it work consistently &#8211; there were two many variables &#8211; additional attributes, white-space etc. The two constants were that the code fragment validated as xhtml (hence xml) and I would always have a class that I could use as a hook.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">// $menu - fragment of html consisting of nested UL's </span>
&nbsp;
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menu</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$nodes</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xpath</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'//*[@class = &quot;current&quot;]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nodes</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$nodes</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'div'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'text_to_replace'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$menu</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text_to_replace'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #000088;">$menu</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$menu</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><code>//*[@class = "current"]</code> finds nodes at any level with where the class attribute contains &#8216;current&#8217;.</p>
<p><code>node[0]</code> is the first instance of a node with this attribute and inject some place-holder text.</p>
<p><code>$menu = $xml->asXML();</code> our modified list.</p>
<p>Hey presto!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2011/09/24/xpath-to-the-rescue-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick debug function</title>
		<link>http://www.flipflops.org/2011/06/13/quick-debug-function/</link>
		<comments>http://www.flipflops.org/2011/06/13/quick-debug-function/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 22:55:07 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Stuff]]></category>
		<category><![CDATA[Web Things]]></category>
		<category><![CDATA[debug]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/?p=383</guid>
		<description><![CDATA[It&#8217;s not pretty and it&#8217;s not elegant, but we&#8217;ve all got a bag of quick and dirty functions that help make life just a little bit easier. This is a simple print_r() but I finally got fed up of loosing my quick debug statements and so added the line name and file number from a [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s not pretty and it&#8217;s not elegant, but we&#8217;ve all got a bag of quick and dirty functions that help make life just a little bit easier.</p>
<p>This is a simple <code>print_r()</code> but I finally got fed up of loosing my quick debug statements and so added the line name and file number from a stack trace (substitute var_export() to taste).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> pr<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;pre&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;/pre &gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #990000;">debug_backtrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$d</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;Line: &lt;b&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$d</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'line'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/b&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;hr /&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2011/06/13/quick-debug-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reorder a nested HTML list in PHP</title>
		<link>http://www.flipflops.org/2011/02/13/reorder-a-nested-html-list-in-php/</link>
		<comments>http://www.flipflops.org/2011/02/13/reorder-a-nested-html-list-in-php/#comments</comments>
		<pubDate>Sat, 12 Feb 2011 23:11:52 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Things]]></category>
		<category><![CDATA[simpleXML]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[XMLDom]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/?p=374</guid>
		<description><![CDATA[Recently I was working on website where I had to re-order a nested list (part of a navigation menu) &#8211; unfortunately I only had access to fragment of HTML so I couldn&#8217;t just manipulate the arrays from which it was built. The menu was compiled from various arrays and months within years sometimes came out [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was working on website where I had to re-order a nested list (part of a navigation menu) &#8211; unfortunately I only had access to fragment of HTML so I couldn&#8217;t just manipulate the arrays from which it was built. The menu was compiled from various arrays and months within years sometimes came out all wrong.</p>
<p>So I thought, I&#8217;d just treat it as a bit of XML (which obviously it is) and re-order it using PHPs native XML handling classes. XML is one of those things that I use frequently, but never really do anything with, and finding a solution took me rather longer than I had expected. It is a mixture of simpleXML and XMLDom.</p>
<p>One of the main problems was the lack any real examples.</p>
<p>If anyone can suggest a more elegant solution, I would love to hear it.</p>
<p>Here is my code:</p>
<h3>The UL to re-order</h3>
<p>As you can see the month names are in the wrong order</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;ul id=&quot;menu&quot;&gt;
    &lt;li class=&quot;first&quot;&gt;&lt;a href=&quot;/news/q/date/2011/&quot;&gt;2011&lt;/a&gt;&lt;/li&gt;
    &lt;li class=&quot;current last&quot;&gt;
        &lt;a href=&quot;/news/q/date/2010/&quot;&gt;2010&lt;/a&gt;
            &lt;ul&gt;
                &lt;li class=&quot;&quot;&gt;&lt;a href=&quot;/news/q/date/2010/07/&quot;&gt;July&lt;/a&gt;&lt;/li&gt;
                &lt;li class=&quot;&quot;&gt;&lt;a href=&quot;/news/q/date/2010/06/&quot;&gt;June&lt;/a&gt;&lt;/li&gt;    
                &lt;li class=&quot;&quot;&gt;&lt;a href=&quot;/news/q/date/2010/11/&quot;&gt;November&lt;/a&gt;&lt;/li&gt;
                &lt;li class=&quot; last&quot;&gt;&lt;a href=&quot;/news/q/date/2010/10/&quot;&gt;October&lt;/a&gt;&lt;/li&gt;
                &lt;li class=&quot;&quot;&gt;&lt;a href=&quot;/news/q/date/2010/09/&quot;&gt;September&lt;/a&gt;&lt;/li&gt;
                &lt;li class=&quot;&quot;&gt;&lt;a href=&quot;/news/q/date/2010/08/&quot;&gt;August&lt;/a&gt;&lt;/li&gt;
                &lt;li class=&quot;first&quot;&gt;&lt;a href=&quot;/news/q/date/2010/12/&quot;&gt;December&lt;/a&gt;&lt;/li&gt;
                &lt;li class=&quot;&quot;&gt;&lt;a href=&quot;/news/q/date/2010/05/&quot;&gt;May&lt;/a&gt;&lt;/li&gt;
           &lt;/ul&gt;
    &lt;/li&gt;
&lt;/ul&gt;</pre></td></tr></table></div>

<h3>My (woeful) solution</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// pull a node tree as an Array out using simpleXML xpath</span>
<span style="color: #000088;">$trees</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xpath</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/ul/li/ul'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$order</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// we only need to delve into XML if there are any nested &lt;ul&gt;s</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;">$trees</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$trees</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// store each node in an indexed array</span>
		<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span> 
		<span style="color: #666666; font-style: italic;">// store the month number in an index array</span>
		<span style="color: #666666; font-style: italic;">// based on the text node value of the &lt;a&gt; tag</span>
		<span style="color: #000088;">$order</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'m'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$var</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
		<span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// sort the month number array descending, but maintaining the keys</span>
	<span style="color: #990000;">arsort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$order</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
	<span style="color: #666666; font-style: italic;">// create a new XML Dom object to manipulate stuff</span>
	<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DomDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// create a holder node &lt;ul&gt;</span>
	<span style="color: #000088;">$ul</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ul'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// iterate through the array of simpleXML objects </span>
	<span style="color: #666666; font-style: italic;">// based on the order in which their keys appear in the re-ordered array</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$order</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// get the simpleXML objects into a string</span>
		<span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #990000;">dom_import_simplexml</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// get the string into an actual DOM node</span>
		<span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">importNode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// append</span>
		<span style="color: #000088;">$ul</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendChild</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//$dom-&gt;appendChild($ul);</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// unset the contents of the original &lt;ul&gt; node that we have resorted</span>
	<span style="color: #000088;">$parent</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$trees</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xpath</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'parent::*'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$parent</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ul</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// turn our simpleXML object into a DOM object</span>
	<span style="color: #000088;">$ixml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">dom_import_simplexml</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$new</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'1.0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ixml</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$new</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">importNode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ixml</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ixml</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$new</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendChild</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ixml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// fire up a DOM xpath object</span>
	<span style="color: #000088;">$xpath</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DomXpath<span style="color: #009900;">&#40;</span><span style="color: #000088;">$new</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// pull a node tree out using simpleXML xpath</span>
	<span style="color: #000088;">$tree</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/ul/li/ul'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// add our newly created DOM node conatining the re-ordered &lt;ul&gt; after the existing node</span>
	<span style="color: #000088;">$tree</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parentNode</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendChild</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">importNode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ul</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// delete the original empty node</span>
	<span style="color: #000088;">$tree</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parentNode</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeChild</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tree</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$new</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">saveHTML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2011/02/13/reorder-a-nested-html-list-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL DATE_FORMAT() AND PHP date() formats</title>
		<link>http://www.flipflops.org/2009/12/01/mysql-date_format-and-php-date-formats/</link>
		<comments>http://www.flipflops.org/2009/12/01/mysql-date_format-and-php-date-formats/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 20:21:11 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Datetime]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/2009/12/01/mysql-date_format-and-php-date-formats/</guid>
		<description><![CDATA[I&#8217;m always finding myself looking up strings to format PHP and MySQL dates. A simple d/m/Y is pretty straight forward but sometimes things can get a bit fiddly. Where ever possible I try and leave my date formatting in the database, but I still find myself doing it in PHP all the time. Here are [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m always finding myself looking up strings to format PHP and MySQL dates. A simple d/m/Y is pretty straight forward but sometimes things can get a bit fiddly. Where ever possible I try and leave my date formatting in the database, but I still find myself doing it in PHP all the time.</p>
<p>Here are a few simple cut and paste date formats.</p>
<table class="exam_table mtable" >
<thead>
<tr>
<th scope="col">PHP Date</th>
<th scope="col">MySQL Date</th>
<th scope="col">PHP Format</th>
<th scope="col">MySQL Format</th>
<th scope="col">Notes</th>
</tr>
<thead>
<tbody>
<tr>
<td>7/9/2009</td>
<td>7/9/2009</td>
<td>j/n/Y</td>
<td>%e/%c/%Y</td>
<td></td>
</tr>
<tr>
<td>07/09/2009</td>
<td>07/09/2009</td>
<td>d/m/Y</td>
<td>%d/%m/%Y</td>
<td></td>
</tr>
<tr>
<td>7/9/2009 8:07</td>
<td>7/9/2009 8:07</td>
<td>j/n/Y G:i</td>
<td>%e/%c/%Y %k:%i</td>
<td></td>
</tr>
<tr>
<td>7/9/2009 8:07 AM</td>
<td>7/9/2009 8:07 AM</td>
<td>j/n/Y G:i A</td>
<td>%e/%c/%Y %k:%i %p</td>
<td></td>
</tr>
<tr>
<td>07/09/2009 8:07 AM</td>
<td>07/09/2009 8:07 AM</td>
<td>d/m/Y G:i A</td>
<td>%d/%m/%Y %k:%i %p</td>
<td></td>
</tr>
<tr>
<td>07/09/2009 08:07 AM</td>
<td>07/09/2009 08:07 AM</td>
<td>d/m/Y H:i A</td>
<td>%d/%m/%Y %H:%i %p</td>
<td></td>
</tr>
<tr>
<td>07/09/2009 8:07 am</td>
<td></td>
<td>d/m/Y G:i a</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Mon 7th Sep 2009</td>
<td>Mon 7th Sep 2009</td>
<td>D jS M Y</td>
<td>%a %D %b %Y</td>
<td></td>
</tr>
<tr>
<td>Monday 7th September 2009</td>
<td>Monday 7th September 2009</td>
<td>l jS F Y</td>
<td>%W %D %M %Y</td>
<td></td>
</tr>
<tr>
<td>2009-09-07T08:07:50+01:00</td>
<td>Monday 7th September 2009</td>
<td>Y-m-d\TH:i:sP</td>
<td>%W %D %M %Y</td>
<td>PHP DATE_ATOM constant</td>
</tr>
<tr>
<td>Mon, 07 Sep 2009 08:07:50 +0100</td>
<td>Mon, 07 Sep 2009 08:07:50</td>
<td>D, d M Y H:i:s O</td>
<td>%a, %d %b %Y %T</td>
<td>PHP DATE_RSS constant</td>
</tr>
</tbody>
</table>
<h2>The PHP way</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'2009-09-07 08:07:50'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//or</span>
&nbsp;
<span style="color: #000088;">$dateTime</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;now&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$dateTime</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:i:s&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<h2>The MySQL way</h2>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">&nbsp;
$sqlstring <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;SELECT DATE_FORMAT('2009-09-07 08:07:50', '%d/%m/%Y') AS formated_date FROM some_table&quot;</span> ;</pre></div></div>

<ul>
<li><a href="http://php.net/manual/en/function.date.php">http://php.net/manual/en/function.date.php</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format">http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format</a></li>
</ul>
<p>If you haven&#8217;t looked at the PHP manual closely for a while you should definitely (re)acquaint yourself with the <a href="http://www.php.net/manual/en/class.datetime.php#datetime.constants.types">DateTime Class</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2009/12/01/mysql-date_format-and-php-date-formats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lithium, Doctrine &amp; doing it yourself</title>
		<link>http://www.flipflops.org/2009/10/30/lithium-doctrine-doing-it-yourself/</link>
		<comments>http://www.flipflops.org/2009/10/30/lithium-doctrine-doing-it-yourself/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 22:12:38 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ORM]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/2009/10/30/lithium-doctrine-doing-it-yourself/</guid>
		<description><![CDATA[If you use CakePHP you&#8217;ll probably be aware (or you&#8217;re dead or on holiday or something&#8230;) that Gwoo and Nate have left the project and gone elsewhere. Sort of. Well actually they have forked Cake3 into Lithium &#8211; souped up CakePHP for PHP 5.3 Unfortunately I&#8217;m not going to be using it in the immediate [...]]]></description>
			<content:encoded><![CDATA[<p>If you use CakePHP you&#8217;ll probably be aware (or you&#8217;re dead or on holiday or something&#8230;) that Gwoo and Nate have left the project and gone elsewhere. Sort of. Well actually they have forked Cake3 into <a href="http://li3.rad-dev.org/">Lithium</a> &#8211; souped up CakePHP for PHP 5.3</p>
<p>Unfortunately I&#8217;m not going to be using it in the immediate future due to the 5.3 limitation, but I had a good look through the <a href="http://rad-dev.org/lithium_examples">blog example</a> and it looks like it would be a pleasure to work with.</p>
<p>Looking round the Lithium site I also came across <a href="http://www.doctrine-project.org/">Doctrine</a> &#8211; an object relational mapper (ORM) system. I spent an evening reading the documentation and decided it was perfect for a project I&#8217;m working on at work, unfortunately the next day I discovered we aren&#8217;t up to the required PHP version yet (almost, but not quite) so on the back burner for now.</p>
<p>I&#8217;m building a small application (about 8 tables) to be integrated into an existing CMS system. The structure of the CMS is essentially front controller based with a great template engine and well developed plugin / module system. The new module I&#8217;ve had to develop just didn&#8217;t fit well into the existing code structure and so I have ended up writing a Model / Controller system within my module that utilizes the things like the DB library, routing, security, templating etc. of the parent application.</p>
<p>The setup is pretty straight forward &#8211; a base model and base controller extended by respective models and controllers for each table. The child controllers and models are mostly pretty thin &#8211; mostly just setting class specific instance variables and the calling methods in the base classes with parent:: There isn&#8217;t any fancy ORM or anything like that in the models, just good old fashioned SQL, one query or closely related group of queries per method.</p>
<p>It took me about 1 1/2 days to write the framework and about another half day to write the templates (I&#8217;ve still got a load of reporting etc. to write) but now I&#8217;ve got the views for all the CRUD operations I will need and a lot of reusable code. Thinking about it, I would probably have done about a quarter of the work, in a far less maintainable way if I hadn&#8217;t taken this route. Goes to show that as long as your goals are well defined &#8211; this kind of approach far from being overkill has saved a huge amount of time and produced very understandable code &#8211; and you don&#8217;t need an entire framework to do it (with the added advantage that if you write stuff yourself, you hopefully have a better grasp of what is going on).</p>
<p>(I&#8217;ve still written a fair amount of Cake code this week <img src='http://www.flipflops.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2009/10/30/lithium-doctrine-doing-it-yourself/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePHP Menu Helper for Tree data</title>
		<link>http://www.flipflops.org/2009/09/29/cakephp-menu-helper-for-tree-data/</link>
		<comments>http://www.flipflops.org/2009/09/29/cakephp-menu-helper-for-tree-data/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 19:11:03 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Helpers]]></category>
		<category><![CDATA[Tree]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/2009/09/29/cakephp-menu-helper-for-tree-data/</guid>
		<description><![CDATA[This is the MenuHelper I use in my CMS system. It is designed to work with data from the Tree behaviour or from any $model->find(&#8216;threaded&#8217;) call. I use it to generate multi level CSS menus (that you see at the top of most websites), contextual menus (that you see in sidebars, showing the current page [...]]]></description>
			<content:encoded><![CDATA[<p>This is the MenuHelper I use in my CMS system. It is designed to work with data from the Tree behaviour or from any $model->find(&#8216;threaded&#8217;) call. I use it to generate multi level CSS menus (that you see at the top of most websites), contextual menus (that you see in sidebars, showing the current page and its &#8216;branch&#8217;) and sitemaps &#8211; which let&#8217;s face it are basically often just the same as a top level menu but without the fancy stuff to make them pop out when you hover over them.</p>
<h2>Prerequisites, history, overview</h2>
<p>There are a couple of prerequisites here which may mean that this isn&#8217;t applicable for a lot of people. The helper is designed to deal with &#8216;page&#8217; structures organised in simple /parent/child/grand-child format e.g.</p>
<p>/home<br />
/about<br />
/about/company-history<br />
/about/company-history/gallery<br />
/about/ethos<br />
/about/vacancies<br />
/news<br />
/news/2009/jan<br />
/news/2009/feb<br />
/news/2009/mar<br />
/contact<br />
(etc.)</p>
<p>In my system I have a model &#8216;Article&#8217; with Tree behaviour attached. So in the example above &#8216;ethos&#8217; is a child of &#8216;about&#8217;. Some of these pages have real content, but others just act as place holders for other controllers / models &#8211; &#8216;news&#8217; is a placeholder for the &#8216;News&#8217; model. But all of them (even the placeholders) have other data attached like content for sidebars or meta information.</p>
<p>The helper will add a &#8216;selected-item&#8217; class to the LI corresponding to the current page, and if it has any parents it will add a &#8216;selected&#8217; class to each of those parent LIs too.</p>
<p><strong>I create a full slug for each &#8216;Article&#8217; on $model->save()</strong></p>
<p>The helper operates in 2 modes &#8216;tree&#8217; (which is default) and &#8216;context&#8217;<br />
&#8216;tree&#8217; will produce a whole list of nested ULs &#8211; can be used to generate top level navigation (works well with things like suckerfish)<br />
&#8216;context&#8217; will only produce nested ULs for the current branch</p>
<p>Using the example above if you were on the /about/ethos page and you wanted the navigation fragment in the sidebar you would use &#8216;context&#8217;<br />
/about<br />
/about/company-history<br />
/about/company-history/gallery<br />
/about/ethos<br />
/about/vacancies</p>
<h2>Database Fields</h2>
<p>These are the default field names, they can be simply overridden to match your database schema.</p>
<h3>Required Fields</h3>
<ul>
<li>name </li>
<li>slug_url</li>
</ul>
<h3>Optional Fields</h3>
<ul>
<li>title_for_navigation</li>
<li>redirect_url</li>
<li>redirect_target</li>
</ul>
<h2>Example &#8216;threaded&#8217; data</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>Article<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Find out about us
                    <span style="color: #009900;">&#91;</span>slug_url<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">/</span>about<span style="color: #339933;">-</span>us
                    <span style="color: #009900;">&#91;</span>title_for_navigation<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> About Us
                    <span style="color: #009900;">&#91;</span>redirect_url<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 
                    <span style="color: #009900;">&#91;</span>redirect_target<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 
                    <span style="color: #009900;">&#91;</span>lft<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">15</span>
                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">105</span>
                    <span style="color: #009900;">&#91;</span>rght<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">24</span>
                    <span style="color: #009900;">&#91;</span>parent_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 
                <span style="color: #009900;">&#41;</span>
&nbsp;
            <span style="color: #009900;">&#91;</span>children<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                        <span style="color: #009900;">&#40;</span>
                            <span style="color: #009900;">&#91;</span>Article<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                                <span style="color: #009900;">&#40;</span>
                                    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Philosophy
                                    <span style="color: #009900;">&#91;</span>slug_url<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">/</span>about<span style="color: #339933;">-</span>us<span style="color: #339933;">/</span>philosophy
                                    <span style="color: #009900;">&#91;</span>title_for_navigation<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 
                                    <span style="color: #009900;">&#91;</span>redirect_url<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 
                                    <span style="color: #009900;">&#91;</span>redirect_target<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 
                                    <span style="color: #009900;">&#91;</span>lft<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">16</span>
                                    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">111</span>
                                    <span style="color: #009900;">&#91;</span>rght<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">21</span>
                                    <span style="color: #009900;">&#91;</span>parent_id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">105</span>
                                <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<h2>Usage</h2>
<p>Include the helper in your controller as usual.</p>
<p>In a view / element to generate complete menu</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$menu</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setup</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menu_data</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'selected'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">here</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In a view / element to generate a context menu from the same data, set the class of the parent UL</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$menu</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setup</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menu_data</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'selected'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">here</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'context'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'menuClass'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'context-menu'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In a view / element to generate a sitemap from different data, let the helper know to use the &#8216;Sitemap&#8217; model rather than the default &#8216;Article&#8217; model and set the parent UL class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$sitemap</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MenuHelper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sitemap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setup</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span>  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'modelName'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sitemap'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'menuClass'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'sitemap'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>The Helper Code</h2>
<p><strong><a href="/scripts/menu.php.txt">Download the Helper</a></strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Menu Helper
 * 
 * @author John Elliott (http://www.flipflops.org)
 * @package app
 * @subpackage app.views.helpers
 * @version 1.0
 * @lastmodified 2009-09-29
 * 
 * This helper is designed to work with the data arrays returned by the Tree behaviour or $model-&gt;find('threaded');
 * More specifically it is designed to generate menus composed of nested ULs for use in CMS systems where all the URLs are pretty:
 * 	- naviagtion menus (usually at the top of the page)
 * 	- context menus (usually found at the side) and showing the links in the current branch
 * 	- sitemaps
 * 
 * Overview (A bit of history)
 * 
 * The helper is designed to deal with navigation / page structures organised in simple /parent/child/grand-child like structures
 * for instance:
 * 
 * /home
 * /about
 * /about/company-history
 * /about/company-history/gallery
 * /about/ethos
 * /about/vacancies
 * /news
 * /news/2009/jan
 * /news/2009/feb
 * /news/2009/mar
 * /contact
 * (etc.)
 * 
 * -------------------
 * URLs are generated in the model or controller not in the view / helper
 * -------------------
 * In the system from which this is taken, I create a full slug for each page on $model-&gt;save();
 * Some of the pages are actual pages of text and others are palceholders for content in other models
 * but they allow you do things like control the content of left / right columns, set default meta tags etc.
 * The purpose being to create really easy site structures with nice urls, the 
 * In the example above /home, /about etc. are all from a single model 'Article' which is my master model 
 * /news however is a placeholder for the 'News' model and /contact for the 'Contact' model
 * 
 *
 * 
 * Required Fields
 * ------------------
 * title / name (defaults to 'title')
 * page url (defaults to 'slug_url' e.g. /about/ethos)
 * 
 * Optional Fields
 * ------------------
 * title_for_navigation
 * redirect_url
 * redirect_target
 * 
 * Example data
 * ------------------
 * [Article] =&gt; Array
                (
                    [name] =&gt; Find out about us
                    [slug_url] =&gt; /about-us
                    [title_for_navigation] =&gt; About Us
                    [redirect_url] =&gt; 
                    [redirect_target] =&gt; 
                    [lft] =&gt; 15
                    [id] =&gt; 105
                    [rght] =&gt; 24
                    [parent_id] =&gt; 
                )
&nbsp;
            [children] =&gt; Array
                (
                    [0] =&gt; Array
                        (
                            [Article] =&gt; Array
                                (
                                    [name] =&gt; Philosophy
                                    [slug_url] =&gt; /about-us/philosophy
                                    [title_for_navigation] =&gt; 
                                    [redirect_url] =&gt; 
                                    [redirect_target] =&gt; 
                                    [lft] =&gt; 16
                                    [id] =&gt; 111
                                    [rght] =&gt; 21
                                    [parent_id] =&gt; 105
                                )
 * 
 * 
 * 
 *
 * Usage:
 * 
 * Include the helper in your controller as usual.
 * 
 * The helper operates in 2 modes 'tree' (which is default) and 'context'
 * 'tree' will produce a whole list of nested Uls - can be used to generate top level navigation (works well with things like suckerfish)
 * 'context' will only produce nested ULs for the current branch
 * 
 * Using the example above if you were on the /about/ethos page and you wanted the navigation fragment in the sidebar you would use context
 * /about
 * /about/company-history
 * /about/company-history/gallery
 * /about/ethos
 * /about/vacancies
 * 
 * In a view / element to generate complete menu
 * echo $menu-&gt;setup($menu_data, array('selected' =&gt; $this-&gt;here));
 * 
 * In a view / element to generate a context menu from the same data, set the class of the parent UL
 * echo $menu-&gt;setup($menu_data, array('selected' =&gt; $this-&gt;here, 'type' =&gt; 'context', 'menuClass' =&gt; 'context-menu'));
 * 
 * 
 * In a view / element to generate a sitemap from different data, let the helper know to use the 'Sitemap' model rather than the default 'Article' model and set the parent UL class.
 * $sitemap = new MenuHelper();
 * echo $sitemap-&gt;setup($data,  array('modelName' =&gt; 'Sitemap', 'menuClass' =&gt; 'sitemap'));
 * 
 * Version Details
 * 
 * 1.0
 * + Initial release.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> MenuHelper <span style="color: #000000; font-weight: bold;">extends</span> AppHelper <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Current page in application
	 *
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$selected</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/** Internal variable for the data
	 *
	 * @var array
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Default css class applied to the menu
	 *
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$menuClass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'menu'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Default DOM id applied to menu
	 *
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$menuId</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'top-menu'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * CSS class applied to the selected node and its parent nodes 
	 *
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$selectedClass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'selected'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * CSS class applied to the exact selected node in the tree - in addition to $selectedClass
	 *
	 * @var unknown_type
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$selectedClassItem</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'item-selected'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Default Slug
	 *
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$defaultSlug</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'home'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Type of menu to be generated:
	 * 'tree' - to generate a complete tree
	 * 'context' - to only render the specific barnch under the current page
	 *
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'tree'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Model name used in $array e.g. $data[0]['Article']['name']
	 *
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$modelName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Article'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Database column name - (i.e. a shorter version of the name / title for use only in naviagtion)
	 * e.g. A page called 'Welcome to the giant flea circus' 
	 * might be set to show up on navigation as 'home'
	 *
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$titleForNavigation</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'title_for_navigation'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Database column name for title / name
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'name'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Database column name for complete page slug e.g. /about/history/early-years
	 *
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$slugUrl</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'slug_url'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Database column name for redirect_url for instance if /about/blog redirects to http://blog.somewebsite.com
	 *
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$redirectUrl</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'redirect_url'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Target for redirect (see redirectUrl)
	 *
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$redirectTarget</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'redirect_target'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Minumum number of items required to render a context menu
	 *
	 * @var int
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$contextMinLength</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Internal Counter used in type: 'context'
	 *
	 * @var int
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$li_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Internal flag to see if the page has been matched to an item
	 *
	 * @var bool
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$matched</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Internal counter
	 *
	 * @var int
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$rootNode</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setOption<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getOption<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Setup the helper and return a string to echo
	 *
	 * @param array $array Data array containing the lists
	 * @param array $config Configuration variables to override the defaults
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setup<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// update and override the default variables </span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setOption</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// set the default slug selected if the current page does not match</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">selected</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">selected</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">defaultSlug</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">array</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$array</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
		<span style="color: #666666; font-style: italic;">// get the root node of the selected tree if this a context menu</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">type</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'context'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rootNode</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRootNode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">selected</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">buildMenu</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// if the current page has matched one of the links in the tree</span>
		<span style="color: #666666; font-style: italic;">// then get rid of the 'default_slected' placeholder</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">matched</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'default_selected'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</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;">$s</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">' class=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">selectedClass</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; '</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'default_selected'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$s</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// if this is a context menu, it looks daft if it only has 1 item </span>
		<span style="color: #666666; font-style: italic;">// if this is the case hide it</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">type</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'context'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">li_count</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">contextMinLength</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">output</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Call the menu iterator method and if it returns a string warp it up in a UL
	 *
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> buildMenu<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">menuIterator</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</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;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;ul  id=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">menuId</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; class=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">menuClass</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$str</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/ul&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Explode a url slug and get the root page
	 *
	 * @param string $string 
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> getRootNode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$rootNode</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</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;">$node</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// $node[0] will always be empty becuase the first char of $this-&gt;selected will always be '/'</span>
				<span style="color: #000088;">$rootNode</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$rootNode</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Recursive method to loop down through the data array building menus and sub menus
	 *
	 * @param array $array
	 * @param int $depth
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> menuIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$is_selected</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #000088;">$continue</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$selected</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$sub</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">type</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'context'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRootNode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">slugUrl</span><span style="color: #009900;">&#93;</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;">rootNode</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$continue</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$continue</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">// if this is the first list item set default_selected placeholder</span>
				<span style="color: #000088;">$default_selected</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">i</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$default_selected</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'default_selected'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'children'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$sub</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;ul&gt;'</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$sub</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">menuIterator</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'children'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$sub</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;/ul&gt;'</span><span style="color: #339933;">;</span>	
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">selected</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">slugUrl</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
				<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
						<span style="color: #666666; font-style: italic;">// this is the selected item or a parent node of the selected item</span>
						<span style="color: #000088;">$selected</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">' class=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">selectedClass</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; '</span><span style="color: #339933;">;</span>
						<span style="color: #000088;">$is_selected</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
						<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">matched</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">selected</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">slugUrl</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">// this is the exact selected item</span>
					<span style="color: #000088;">$selected</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">' class=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">selectedClass</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">selectedClassItem</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; '</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">// keep track if this is a contextual menu </span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">type</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'context'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">li_count</span><span style="color: #339933;">++;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
				<span style="color: #666666; font-style: italic;">// Get the name / title to be used for the link text</span>
				<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// Get the URL / target for the link</span>
				<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUrl</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #000088;">$str</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;li '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$selected</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$default_selected</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&gt;'</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$str</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a  href=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'target'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&gt;&lt;span&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/span&gt;&lt;/a&gt;'</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$str</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$sub</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$str</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Look in the data and check if this is a straight url
	 * or whether it is actually a redirect
	 *
	 * @param array $var
	 * @return array
	 */</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> getUrl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<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;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirectUrl</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirectUrl</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirectUrl</span><span style="color: #009900;">&#93;</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;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirectTarget</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirectTarget</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'target'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">' target=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirectTarget</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; '</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">slugUrl</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'target'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$url</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * See if there is a title_for_navigation 
	 *
	 * @param array $var
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</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;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">titleForNavigation</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">titleForNavigation</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">titleForNavigation</span><span style="color: #009900;">&#93;</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;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2009/09/29/cakephp-menu-helper-for-tree-data/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Messing about with Tarzan and the Amazon AAWS web service</title>
		<link>http://www.flipflops.org/2009/09/09/messing-about-with-tarzan-and-the-amazon-aaws-web-service/</link>
		<comments>http://www.flipflops.org/2009/09/09/messing-about-with-tarzan-and-the-amazon-aaws-web-service/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 22:40:46 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[Amazon AWS]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[AAWS]]></category>
		<category><![CDATA[Amazon]]></category>
		<category><![CDATA[simpleXML]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/2009/09/09/messing-about-with-tarzan-and-the-amazon-aaws-web-service/</guid>
		<description><![CDATA[At the moment I building a couple of applications running of the Amazon AWS web services &#8211; in a nutshell e-commerce sites that utilize products available via Amazon or companies selling through Amazon and then let you check out using Amazon&#8217;s secure checkout (the site owner gets commission on each product sold). The system is [...]]]></description>
			<content:encoded><![CDATA[<p>At the moment I building a couple of applications running of the <a href="http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html">Amazon AWS web services</a> &#8211; in a nutshell e-commerce sites that utilize products available via Amazon or companies selling through Amazon and then let you check out using Amazon&#8217;s secure checkout (the site owner gets commission on each product sold).</p>
<p>The system is great, but it is huge and there are an awful of options. The documentation is good, but quite hard to navigate and sometimes quite confusing, basically it just boils down to reading the API Docs.</p>
<p>I&#8217;ve been using the <a href="http://tarzan-aws.com/">Tarzan AWS</a> framework / toolkit / library to deal with the hassle of connecting to and being authenticated by the Amazon system. Tarzan is a great system, but again it boils down to reading the API as there aren&#8217;t really any tutorials.</p>
<p><strong>Finding the OfferListingId</strong></p>
<p>Products sold via Amazon are identified by all kinds of data including ISBN codes, ASIN (Amazon ID numbers), OfferListingIds etc. I guess the trick is knowing which one to use in given circumstances.</p>
<p>One real gotcha that held me up (I had to post on the Tarzan group to find the answer) was that Tarzan is designed to use the OfferListingId of a product to identify it at key points (e.g. adding a product to the remote cart) &#8211; my understanding of this is that it is because every item has an OfferListingId &#8211; but not every number has an ASIN (this could be wrong and it could be because of another reason entirely).</p>
<p>My problem was that I could not find the OfferListingIds of the products I wanted to add to the cart, and so was trying to use the ASIN. After an age of debugging and trying to figure out what the hell was wrong, I looked at the actual Tarazn code and discovered that it was set up to only deal with OfferListingIds &#8211; so I changed it to work with ASINs and the rest of my code started working how it was supposed to (i.e. I could now add items to my remote cart). </p>
<p>Ryan Parman (the guy who wrote Tarzan) kindly explained that the OfferListingId is only contained in certain responses from Amazon &#8211; I had been requesting a result set which did not include the OfferListingId &#8211; problem solved. It turns out that the OfferListingId is returned if you request a &#8216;Large&#8217; or &#8216;Offers&#8217; response group, I had been using the &#8216;Medium&#8217; group thinking that less data to work with would make my life easier. Silly me.</p>
<p><strong>Serialize SimpleXML problems</strong></p>
<p>When you add your first product to the remote Amazon shopping cart you have to use the <a href="http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/CartCreate.html">create_cart()</a> method. This creates a cart object and adds product(s) to it. Part of the response is a CartId which you need to use to access the cart again in future requests. The logical place for this is a Session. </p>
<p>My responses from Amazon via Tarzan pop out as SimpleXML objects, very handy and easy to work with. Unless you forget that PHP won&#8217;t let you serialize native objects&#8230; There are various solutions but my quick and dirty solution was to use <a href="http://uk3.php.net/manual/en/language.types.type-juggling.php">type casting</a> to change the SimpleXML object to a string e.g.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$aaws</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cart_create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">,</span> <span style="color: #000088;">$opt</span><span style="color: #339933;">,</span> AAWS_LOCALE_UK<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <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;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Cart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CartId</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cart.CartId'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Cart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CartId</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;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cart.URLEncodedHMAC'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Cart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">URLEncodedHMAC</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;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cart.CartItemId'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Cart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CartItems</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CartItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CartItemId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And it works like a treat.</p>
<p>(This is also worth a read: <a href="http://blog.makemepulse.com/2007/09/27/serialize-and-unserialize-simplexml-in-php/">Serialize and Unserialize SimpleXML in php</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2009/09/09/messing-about-with-tarzan-and-the-amazon-aaws-web-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two CakePHP behaviours to extend MeioUpload</title>
		<link>http://www.flipflops.org/2009/06/29/two-cakephp-behaviours-to-extend-meioupload/</link>
		<comments>http://www.flipflops.org/2009/06/29/two-cakephp-behaviours-to-extend-meioupload/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 15:53:24 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/2009/06/29/two-cakephp-behaviours-to-extend-meioupload/</guid>
		<description><![CDATA[I&#8217;ve been using the the Meio Upload Behaviour for a while as its a very handy piece of code, but as is so often the case it doesn&#8217;t do quite what I need. In the past I&#8217;ve got round this by hacking together a kind of supporting framework in app_controller and scattered about in my [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using the the <a href="http://www.meiocodigo.com/projects/meioupload/">Meio Upload Behaviour</a> for a while as its a very handy piece of code, but as is so often the case it doesn&#8217;t do quite what I need. In the past I&#8217;ve got round this by hacking together a kind of supporting framework in app_controller and scattered about in my models. But the other day I came across the <a href="http://github.com/jrbasso/MeioUpload/tree/master">newest version of the code by Juan Basso  on Github</a> and decided to dump my mess of spaghetti code and start from scratch.</p>
<h2>Aims</h2>
<p>The idea is that each model can have its own uploads with their own defaults, for example: </p>
<ul>
<li>Products might need to generate images at 3 different sizes with the smallest zoom cropped.</li>
<li>News items might have 2 image sizes but also need to upload a PDF press release.</li>
</ul>
<p>All of the uploaded files can be viewed and managed centrally from the Upload model.</p>
<p>The changes I wanted to make were as follows:</p>
<ol>
<li>Use a single Uploads table attached to multiple models (<a href="http://bakery.cakephp.org/articles/view/polymorphic-behavior">using the Polymorphic behaviour</a>)</li>
<li>The ability to do multiple uploads at once</li>
<li>Rename my uploaded files</li>
<li>Have an UploadVariants model / table with meta information about the thumbnails</li>
<li>Be able to use more of the PHPThumb image options and use them on a per thumbnail basis.</li>
</ol>
<p>Rather than just take the behaviour and start writing on top of it, I decided to extend the original behaviour (yes you can do this in CakePHP &#8211; its just easy to get absorbed in the framework and forget it) and then create an additional behaviour that manages the multiple uploads.</p>
<p>The underlying requirements are quite simple:</p>
<ul>
<li><a href="http://www.meiocodigo.com/projects/meioupload/">Meio Upload Behaviour</a></li>
<li><a href="http://bakery.cakephp.org/articles/view/phpthumb-component">Nate Constant’s phpThumb Component </a></li>
<li><a href="http://bakery.cakephp.org/articles/view/polymorphic-behavior"><strike>Polymorphic Behaviour</strike></a></li>
<li><a href="http://phpthumb.sourceforge.net/">PHPThumb</a></li>
</ul>
<p>(<strong>Note 09/07/2009</strong> you don&#8217;t actually need the Polymorphic Behaviour, unless you want to do fancy things with your finds)</p>
<p>And my two new behaviours:</p>
<ul>
<li><a href="http://www.flipflops.org/example/je_meio_upload.php.txt">je_meio_upload.php</a></li>
<li><a href="http://www.flipflops.org/example/je_meio_upload_polymorphic.php.txt">je_meio_upload_polymorphic.php</a></li>
</ul>
<p><strong>At the end of the article is a link to let you download a zipped working CakePHP app with everything in place</strong></p>
<h2>The models</h2>
<p>First things first. The database tables on which my models are based.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
MySQL Backup
Source Host:           localhost
Source Server Version: 5.0.51b-community-nt
Source Database:       je_meio
Date:                  2009/06/29 15:39:15
*/</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> FOREIGN_KEY_CHECKS<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>;
#<span style="color: #808080; font-style: italic;">----------------------------</span>
# <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">STRUCTURE</span> <span style="color: #993333; font-weight: bold;">FOR</span> products
#<span style="color: #808080; font-style: italic;">----------------------------</span>
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> products;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`products`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`title`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`created`</span> datetime <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`modified`</span> datetime <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`created_by`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`modified_by`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`created_name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`modified_name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8;
&nbsp;
#<span style="color: #808080; font-style: italic;">----------------------------</span>
# <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">STRUCTURE</span> <span style="color: #993333; font-weight: bold;">FOR</span> upload_variants
#<span style="color: #808080; font-style: italic;">----------------------------</span>
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> upload_variants;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`upload_variants`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`upload_id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`variant`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`filename`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`quick_type`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`width`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`height`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`created`</span> datetime <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`modified`</span> datetime <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`created_by`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`modified_by`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`created_name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`modified_name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8;
&nbsp;
#<span style="color: #808080; font-style: italic;">----------------------------</span>
# <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">STRUCTURE</span> <span style="color: #993333; font-weight: bold;">FOR</span> uploads
#<span style="color: #808080; font-style: italic;">----------------------------</span>
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> uploads;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`uploads`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`class`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'Upload'</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`foreign_id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`alt`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`filename`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`dir`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`mimetype`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`quick_type`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`filesize`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`position`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`height`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`width`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`created`</span> datetime <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`processed`</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`modified`</span> datetime <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`created_by`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`modified_by`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`created_name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`modified_name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`class`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`class`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`foreign_id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8;</pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">class</span> Upload <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Upload'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//The Associations below have been created with all possible keys, those that are not needed can be removed</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$hasMany</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'UploadVariant'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'className'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'UploadVariant'</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'foreignKey'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'upload_id'</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'dependent'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'fields'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'order'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'limit'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'offset'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'exclusive'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'finderQuery'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'counterQuery'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span>
    <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$actsAs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'Polymorphic'</span><span style="color: #339933;">,</span>
     <span style="color: #0000ff;">'JeMeioUpload'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
         <span style="color: #0000ff;">'filename'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">'dir'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'files/uploads'</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'create_directory'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'max_size'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2097152</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'max_dimension'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'w'</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'thumbnailQuality'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">90</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'useImageMagick'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'imageMagickPath'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'/usr/bin/convert'</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'allowed_mime'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'image/gif'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'image/jpeg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'image/pjpeg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'image/png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'allowed_ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.jpg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.jpeg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.png'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.gif'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'thumbsizes'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
             <span style="color: #0000ff;">'small'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'width'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">90</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'height'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
             <span style="color: #0000ff;">'medium'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'width'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">220</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'height'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">220</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
             <span style="color: #0000ff;">'large'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'width'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">800</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'height'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">600</span><span style="color: #009900;">&#41;</span>
           <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'default_class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Upload'</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'random_filename'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span>
         <span style="color: #009900;">&#41;</span>
       <span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In the Upload model, just include the Polymorphic behaviour and JeMeioUpload instead of MeioUpload. You could actually use the same defaults as the MeioUpload behaviour itself but there are two additional defaults  <em>&#8216;default_class&#8217; => &#8216;Upload&#8217;</em>, and <em>&#8216;random_filename&#8217; => true</em>. These both refer to renaming files. As the behaviour can be used to manage uploads from multiple models in a single table it allows you to set the default Upload model. You can also set whether or not you want the files to have the meaningful part replaced with a random string.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> UploadVariant <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'UploadVariant'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//The Associations below have been created with all possible keys, those that are not needed can be removed</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$belongsTo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'Upload'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'className'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Upload'</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'foreignKey'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'upload_id'</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'fields'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'order'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span>
    <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The UploadVariant Model is just as it was baked.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Product <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Product'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$validate</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notempty'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$hasMany</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'Upload'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'className'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Upload'</span><span style="color: #339933;">,</span>    
            <span style="color: #0000ff;">'foreignKey'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'foreign_id'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Upload.class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Product'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'dependent'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'order'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Upload.position ASC'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$actsAs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
     <span style="color: #0000ff;">'JeMeioUploadPolymorphic'</span>
   <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
   <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$jeMeioUploadParams</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'filename'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'dir'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'files/uploads/'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'create_directory'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'allowed_mime'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'image/jpeg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'image/pjpeg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'image/png'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'image/gif'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'allowed_ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.jpg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.jpeg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.png'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.gif'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'thumbsizes'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">'small'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'width'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'height'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'image_options'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'zc'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'large'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'width'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">800</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'height'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">400</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'image_options'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'zc'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'shop'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'width'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">265</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'height'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">265</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'image_options'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'zc'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'random_filename'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Product is a test model set up to demonstrate the multiple uploads and the Polymorphic association to the Upload model. In this case the meaningful part of the filenames will not be replaced with a random string and the &#8216;small&#8217; thumbnail will be zoom cropped. To find out more about <em>image_options</em> you will need to check the PHPThumb component and the documentation PHPThumb itself (play about and experiment) &#8211; what you can and can&#8217;t do will also be effected by whether or not you have ImageMagick on your server.</p>
<h2>The Controllers</h2>
<p>You don&#8217;t need to make any changes to your controllers at all.</p>
<h2>The Views</h2>
<p>There is nothing complicated in the views at all. Just remember to set them up properly for uploads.</p>
<h3>The uploads/add.ctp</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Upload'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;fieldset&gt;
 		&lt;legend&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Add Upload'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/legend&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'alt'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'filename'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/fieldset&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Submit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h3>The products/add.ctp</h3>
<p>This view allows up to 3 uploads to be stored in the associated Upload model.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Product'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;fieldset&gt;
 		&lt;legend&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Add Product'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/legend&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Upload.0.filename'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Upload.1.filename'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Upload.3.filename'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/fieldset&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Submit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h2>Summary</h2>
<p>The one part I&#8217;m not terribly happy with at the moment is error messages returning to the views in the Polymorphic associations (e.g. Product example). At the moment if an image or upload can&#8217;t be processed then there is no warning or error message. At the moment this is compromise I&#8217;m willing to live with. </p>
<p>I could certainly build some kind of component scaffold or something to go in the controllers to return an error message but I think the gain in responsiveness would probably be outweighed by the added complexity. Right now I can&#8217;t figure out an elegant way to get error messages from back to the views &#8211; if anybody has any suggestions please let me know.<br />
(<strong>Note</strong> if you are uploading straight into the Upload model then the error messages are unaffected)</p>
<p>There is also the problem of what do you do when in a situation like the following: If you add a new Product successfully but the associated upload has problems? Ideally you would want to redirect to the edit view for that Product but there seems to be no easy way of doing this from a Model / Behaviour.</p>
<p>One solution that I have implemented in the past is to create dedicated views for uploading &#8211; so if you do hack your hacks are easy to manage.</p>
<p><strong>I&#8217;ve baked a quick and dirty demo that you can download <a href="http://www.flipflops.org/example/je_meio_upload.zip">here</a></strong>. Its so basic that it doesn&#8217;t even show the uploaded images!</p>
<h2>Todo List</h2>
<ul>
<li>
A helper to generate things like uploads fields complete with delete checkboxes and thumbnail images.</li>
<li>
Deleting the via the Polymorphic association  (don&#8217;t think this works right now)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2009/06/29/two-cakephp-behaviours-to-extend-meioupload/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Overload $this-&gt;tags &amp; $this-&gt;map in Apphelper</title>
		<link>http://www.flipflops.org/2009/05/23/overload-this-tags-this-map-in-apphelper/</link>
		<comments>http://www.flipflops.org/2009/05/23/overload-this-tags-this-map-in-apphelper/#comments</comments>
		<pubDate>Sat, 23 May 2009 10:13:13 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/2009/05/23/overload-this-tags-this-map-in-apphelper/</guid>
		<description><![CDATA[CakePHP is full of amazingly handy little bits and pieces, the trick is discovering them by exploring the API or paying attention to people who know Cake inside out. AppHelper sits there begging to be filled, the idea being that you can overload core helper methods here; however there is a lot of debate as [...]]]></description>
			<content:encoded><![CDATA[<p>CakePHP is full of amazingly handy little bits and pieces, the trick is discovering them by exploring the API or paying attention to people who know Cake inside out.</p>
<p>AppHelper sits there begging to be filled, the idea being that you can overload core helper methods here; however there is a lot of debate as to whether or not this is actually a good approach and whether it is infact better (safer?) to overload helpers on a case by case basis (e.g. MyFormhelper extends FormHelper). (Personally I think it would be great to have the core helpers moved into base helpers so you can overload the core methods and still refer to them easily (e.g. HtmlHelper extends BaseHtmlhelper extends AppHelper, where HtmlHelper becomes an empty Class) </p>
<p>Two of the things I always have set in AppHelper are as follows:</p>
<p><strong>1. Setup $this->map</strong> how I like it. $map defined in the Formhelper (cake/libs/views/helpers/form.php) is part of the auto magic behind forms. When you create a form in a view, Cake queries the model schema and figures out what sort of field it should generate for each database field.</p>
<p>Whilst I can see the reasoning behind the way Cake generates all the &lt;select&gt; fields, I would much rather have Cake genearte a simple &lt;text&gt; field and then use a JavaScript date picker to populate the field.</p>
<p>If you take a morning or a day to mess about and customise your bake templates then you can save vast amounts of time &#8211; if you want a date field why not just have it come to life with all the hooks for your Javascript there already? (A good introduction for custom baking is <a href="http://www.ad7six.com/MiBlog/Blogs/view/CustomBakeTemplates">ad7six&#8217;s post</a>.)</p>
<p><strong>2. Redefine Tags.</strong> One of my real annoyances (and I know this is silly) is the fact that when Cake creates hidden fields it does not flag them with any specific class. This means that styles applied to other &lt;input&gt; fields can easily cascade down and cause odd little lines to appear all over your forms. Often you wouldn&#8217;t even notice this, but once you change the background colour of your form&#8230; all hell can break loose. </p>
<p>Tags are defined in /cake/libs/view/helpers/html.php so a quick and dirty solution is just to wrap a div with class around the hidden inputs so they can easily be distinguised css wise from all the other tags.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> AppHelper <span style="color: #000000; font-weight: bold;">extends</span> Helper <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">/*
   * overload the default type mappings is /cake/libs/views/helpers/form.php FormHelper
   */</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$map</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">'string'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span>    
          <span style="color: #0000ff;">'datetime'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'boolean'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'checkbox'</span><span style="color: #339933;">,</span>  
          <span style="color: #0000ff;">'timestamp'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'text'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'textarea'</span><span style="color: #339933;">,</span>  
          <span style="color: #0000ff;">'time'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'time'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'date'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span> 
          <span style="color: #0000ff;">'float'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'text'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
   <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">/*
        * overload the default html tags /cake/libs/view/helpers/html.php
        */</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tags</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'hidden'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;div class=&quot;hidden-input&quot;&gt;&lt;input type=&quot;hidden&quot; name=&quot;%s&quot; %s/&gt;&lt;/div&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If you are looking for other things to stick in your AppHelper then try <a href="http://www.pseudocoder.com/archives/2009/03/01/how-to-save-half-a-second-on-every-cakephp-requestand-maintai-reverse-routing/">Matt Curry&#8217;s app_helper url caching</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2009/05/23/overload-this-tags-this-map-in-apphelper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>At last the CakePHP books have arrived</title>
		<link>http://www.flipflops.org/2008/08/04/at-last-the-cakephp-books-have-arrived/</link>
		<comments>http://www.flipflops.org/2008/08/04/at-last-the-cakephp-books-have-arrived/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 21:07:26 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/2008/08/04/at-last-the-cakephp-books-have-arrived/</guid>
		<description><![CDATA[I&#8217;d like to say it never rains but it pours, but that would be entirely the wrong sentiment, I need something more like good things come to those who wait&#8230; Anyway after an epic wait a flurry of CakePHP books have arrived. details on Amazon details on Amazon details on Amazon view at Manning I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d like to say <em>it never rains but it pours</em>, but that would be entirely the wrong sentiment, I need something more like <em>good things come to those who wait</em>&#8230;</p>
<p>Anyway after an epic wait a flurry of CakePHP books have arrived.</p>
<div style="float: left;">
<a target="_blank" href="http://www.amazon.co.uk/gp/product/1430209771?ie=UTF8&#038;tag=flipflopsorg-21&#038;linkCode=as2&#038;camp=1634&#038;creative=6738&#038;creativeASIN=1430209771"><img src='http://www.flipflops.org/wp-content/uploads/2008/08/beginning-cakephp.jpg' alt='Beginning CakePHP Book' /><br />details on Amazon</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=flipflopsorg-21&#038;l=as2&#038;o=2&#038;a=1430209771" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />
</div>
<div style="float: left;">
<a target="_blank" href="http://www.amazon.co.uk/gp/offer-listing/1847193897?ie=UTF8&#038;tag=cakephp-books-21&#038;linkCode=am2&#038;camp=1634&#038;creative=6738&#038;creativeASIN=1847193897"><img src='http://www.flipflops.org/wp-content/uploads/2008/08/cakephp-application-development.jpg' alt='CakePHP application development book' /><br />details on Amazon</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=cakephp-books-21&#038;l=as2&#038;o=2&#038;a=1847193897" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />
</div>
<div style="float: left;">
<a target="_blank" href="http://www.amazon.co.uk/gp/offer-listing/143021578X?ie=UTF8&#038;tag=cakephp-books-21&#038;linkCode=am2&#038;camp=1634&#038;creative=6738&#038;creativeASIN=143021578X"><img src='http://www.flipflops.org/wp-content/uploads/2008/08/practical-cakephp-projects.jpg' alt='Practical CakePHP Projects' /><br />details on Amazon</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=cakephp-books-21&#038;l=as2&#038;o=2&#038;a=143021578X" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />
</div>
<div style="float: left; clear: left;">
<a href="http://www.manning.com/obrien/" target="_blank"></p>
<p><img src='http://www.flipflops.org/wp-content/uploads/2008/08/obrien_cover150.jpg' alt='Manning CakepHP in Action' /><br />
<br />view at Manning</a></p>
</div>
<p>I haven&#8217;t read any of these books yet, but I from personal experience I would say the <a href="http://www.apress.com/">Apress</a> books are bound to be good. I have no idea what the books from <a href="http://www.packtpub.com/">Packt</a> are like. I have one <a href="http://www.manning.com/">Manning</a> book which is good but is unfortunately a PDF (and I hate reading PDFs).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2008/08/04/at-last-the-cakephp-books-have-arrived/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

