<?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>Edward A. Webb (.com) &#187; CakePHP</title>
	<atom:link href="http://edwardawebb.com/category/programming/php-programming/cakephp/feed/" rel="self" type="application/rss+xml" />
	<link>http://edwardawebb.com</link>
	<description>get all his digital goodness 24/7</description>
	<lastBuildDate>Fri, 27 Aug 2010 21:52:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Nested trees in CakePHP</title>
		<link>http://edwardawebb.com/web-development/cakephp/nested-trees-cakephp</link>
		<comments>http://edwardawebb.com/web-development/cakephp/nested-trees-cakephp#comments</comments>
		<pubDate>Tue, 24 Mar 2009 14:00:19 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[nested lists]]></category>
		<category><![CDATA[nested tables]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">http://edwardawebb.com/?p=438</guid>
		<description><![CDATA[This article started as a footnote to an article I wrote yesterday, using ACL in CakePHP.  I wanted to outline how the hierarchy structure of the Aro and Aco tables worked, and just what lft and rght columns provided over parent_ID. I soon realized that my topic, although used by ACL was much broader. Not [...]]]></description>
			<content:encoded><![CDATA[<p>This article started as a footnote to an article I wrote yesterday, <a href="http://edwardawebb.com/programming/php-programming/cakephp/started-acl-cakephp"title="Some advice to get started with ACL component in CakePHP" >using ACL in CakePHP</a>.  I wanted to outline how the hierarchy structure of the Aro and Aco tables worked, and just what <strong>lft</strong> and <strong>rght</strong> columns provided over parent_ID.</p>
<p>I soon realized that my topic, although used by ACL was much broader. Not only is it a standard practice in referential DB&#8217;s requiring a hierarchal structure, but it is used by Cake&#8217;s Tree behavior as well.  So in order to keep some..order in my site I broke it out into its very own article.</p>
<p><span id="more-438"></span></p>
<h3>Where do they come up with this stuff?</h3>
<p>Well the principle here is not something those magical Cake developers concocted after a wild bender, nope its a pretty standard way to enhance the performance and usability of structured tree queries, known as <strong>nested set model.</strong> Fortunate for us the team knows and follows so many great standards, and this is just another example.</p>
<p><strong><br />
</strong></p>
<h3>Why not use just Parent_ID to create trees?</h3>
<p>Mike Hillyer wrote an article on the MySQL developer site and states the problem very well;</p>
<blockquote><p>&#8220;Most users at one time or another have dealt with hierarchical data in a SQL database and no doubt learned that the management of hierarchical data is not what a relational database is intended for. The tables of a relational database are not hierarchical (like XML), but are simply a flat list.&#8221;</p></blockquote>
<p><cite>http://dev.mysql.com/tech-resources/articles/hierarchical-data.html</cite></p>
<p>I strongly encourage you t read his full article as he discusses the limitations that exist using a simple &#8216;Adjacency List Model&#8217; as he describes it. In summary though;</p>
<ul>
<li>You need to know how many levels in a tree first, and then create a join for each level</li>
<li> Take caution when deleting nodes to be sure you don&#8217;t orphan any records.</li>
</ul>
<p>So is there a better way, sure is!</p>
<h4>Nested Set Model</h4>
<p>Mike not only details the concept very well, but has some great visual aids that make understanding this concept really easy.  There are numerous perks (again, I ask that you read the full article) but in summary;</p>
<ul>
<li>No joins needed to pull back entire tree, no need to know the depth first</li>
<li>Easily determine leaf-nodes (childless)</li>
<li>Best of all, use count to easily create indented html</li>
</ul>
<p>That last bullet is worth noting again.  With a single SQL statement we can return all items in a tree along with the count of their parents.  This parental count can then be used to know how far to indent, or how many nested UL tags to use for any given node.  The result is a nicely formatted tree, or nested list.</p>
<h3>What about updating  or adding records?</h3>
<p>Ok, your probably pointing out how much more complicated it is to add, or update records that use a Nested List Model. You have to determine where it will fit, determine its new lft and rght values, and update any affected records lft and rght values.</p>
<p>Well that&#8217;s true. But think of it this way. How often do you read from a record compared to how often you create or update one. You might add a new product every now and then, but your customers will list your products 100 times that count.  So where do you want ease and performance to lie?</p>
<p>So I hope that clears up the general reasoning behind the lft and rght columns used in ACL and other Nested Lists.  If not, read the <a href="http://dev.mysql.com/tech-resources/articles/hierarchical-data.html" rel="nofollow" title="Great article explaining the ins and outs of nested lists in a MySQL table" >article by Mike Hillyer</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://edwardawebb.com/web-development/cakephp/nested-trees-cakephp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Getting Started with ACL in CakePHP</title>
		<link>http://edwardawebb.com/web-development/cakephp/started-acl-cakephp</link>
		<comments>http://edwardawebb.com/web-development/cakephp/started-acl-cakephp#comments</comments>
		<pubDate>Tue, 24 Mar 2009 00:18:26 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[USer]]></category>

		<guid isPermaLink="false">http://edwardawebb.com/?p=432</guid>
		<description><![CDATA[The ACL component of CakePHP can be a daunting undertaking for those new to CakePHP or ACLs. Once you take the plunge though you&#8217;ll never look back. The flexibility and power of the ACL component are worthy of your site, I promise. In order to help some fellow bakers into the water I wanted to [...]]]></description>
			<content:encoded><![CDATA[<p>The ACL component of CakePHP can be a daunting undertaking for those new to CakePHP or ACLs.  Once you take the plunge though you&#8217;ll never look back.  The flexibility and power of the ACL component are worthy of your site, I promise.  In order to help some fellow bakers into the water I wanted to offer some advice.</p>
<h3><span id="more-432"></span>What is ACL?</h3>
<p>ACL, or Access Control List is a common means to control access to applications or sites at a granular level. <strong>The basic premise is simple, you have &#8216;whos&#8217; and &#8216;whats&#8217;.  The combination determines <em>who</em> can access <em>what</em>.</strong></p>
<h3>Why ACL?</h3>
<p>Unless you want access to be all or nothing, then you need to consider ACLs. This allows some users here, and other users there.</p>
<h3>How to get started with ACL</h3>
<p>The first thing I find is that a lot of people seem to think ACL and Auth are useless with out each other.  <strong>Although they compliment each other very nicely, the use of ACL does not require the use of Auth</strong>.  To illustrate this point I&#8217;ll admit that I don&#8217;t even use Auth. :O  But don&#8217;t tell anyone, they&#8217;ll take away my Baking degree. In fact I have developed a rather robust User model that handles authorization, registratoin, etc, etc quite nicely., but I am getting off topic.</p>
<p>Just remember ACL determines who can do what, Auth determines how they get in.</p>
<h3>The ACL Basics</h3>
<p>I strongly suggest you check out the <a href="http://book.cakephp.org/view/171/Access-Control-Lists" rel="nofollow" title="Read the CakePHP Manual's ACL section"  target="_blank">ACL section of the CakePHP Manual</a>. If you can tolerate all the reference to Lord of The Rings, you&#8217;ll find the information you need. Just know that you want the Database method as appose to ini files.</p>
<h3>Migrating an Existing Site to ACL</h3>
<p>First grab yourself a sheet of paper. Think about this&#8230;<br />
The whos and whats of your site.  A simple example may look like this;</p>
<h4>The Whos</h4>
<p>Keeping it simple, we have users and admins.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #660033;">---------------------------------------------------------------</span>
  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #c20cb9; font-weight: bold;">users</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">4</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Test
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">5</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Jesse
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">6</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Sister
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>administrators
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Eddie
&nbsp;
<span style="color: #660033;">---------------------------------------------------------------</span></pre></div></div>

<h4>The Whats</h4>
<p>This is where you need to really spend some time planning. Think about commonalities across models or areas of your site. Creating the right heirarchy will save alot of hassle down the road.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #660033;">---------------------------------------------------------------</span>
  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Entire_Site
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Main_Models
&nbsp;
      <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">4</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Users
&nbsp;
      <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">5</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Toolboxes
&nbsp;
      <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">6</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Items
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Aux_Models
&nbsp;
      <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">7</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Actions
&nbsp;
      <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">8</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Priorities
&nbsp;
      <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Settings
&nbsp;
      <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">10</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>Botchecks
&nbsp;
<span style="color: #660033;">---------------------------------------------------------------</span></pre></div></div>

<p>The key in my example is that Users should be able to create and read instances of the main models, but only read the auxiliary models.  By collapsing these in a tree format I can just declare that explicitly at the Main and Aux levels, and let the sub-models inherit those permissions, neat!</p>
<h4>Now the Code to put it all together &#8211; acos, aros and aros_acos</h4>
<p>This is a simple controller I roughed out to help implement ACL in one of my existing sites.  </p>
<ul>
<li>Start by initializing the tables with action initAcl.</li>
<li>Next assign permissions (connecting the who and whats)</li>
<li>Finally test out permissions</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
 * Created on Mar 22, 2009
 *
 * @Author Eddie Webb
 * 
 * Set up basic ACL tables based on existing users
 * based on my readings of the book at cakephp.org 
 * 
 */</span>
 <span style="color: #000000; font-weight: bold;">class</span> AclprepController <span style="color: #000000; font-weight: bold;">extends</span> AppController <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;">'Aclprep'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$uses</span> <span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$components</span> <span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Acl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">function</span> initAcl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//FIRST</span>
	<span style="color: #666666; font-style: italic;">/*
	 * creat tables by running cake schema run create DbAcl
	 */</span>
&nbsp;
&nbsp;
&nbsp;
	<span style="color: #666666; font-style: italic;">/*
	 * Define our main user groups, to keep it simpel i have users and admins
	 */</span>	
	 <span style="color: #666666; font-style: italic;">//always declare an Aro object to create and save</span>
		<span style="color: #000088;">$aro</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Aro<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;">//iterate through groups adding to aro table</span>
	<span style="color: #000088;">$groups</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'users'</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'administrators'</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Iterate and create ARO groups</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$groups</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//Remember to call create() when saving in loops...</span>
		<span style="color: #000088;">$aro</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</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;">//Save data</span>
		<span style="color: #000088;">$aro</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #666666; font-style: italic;">/*
	 * next we add our existing add users to users group
	 * ! adds all users to user group, you may add some logic to 
	 * ! detemrine admins based on role, or edit manually later
	 * 
	 * the   **whos**
	 */</span>	
&nbsp;
&nbsp;
	<span style="color: #000088;">$aro</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Aro<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #666666; font-style: italic;">//pull users form existing user table</span>
		<span style="color: #000088;">$users</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'list'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		debug<span style="color: #009900;">&#40;</span><span style="color: #000088;">$users</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$users</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;">$aroList</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>
				<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
					<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span>
					<span style="color: #0000ff;">'parent_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
					<span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'User'</span><span style="color: #339933;">,</span>
					<span style="color: #0000ff;">'foreign_key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$key</span><span style="color: #339933;">,</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;">//print to screen to verify layout</span>
		debug<span style="color: #009900;">&#40;</span><span style="color: #000088;">$aroList</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
		<span style="color: #666666; font-style: italic;">//now save!</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aroList</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//Remember to call create() when saving in loops...</span>
			<span style="color: #000088;">$aro</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</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;">//Save data</span>
			<span style="color: #000088;">$aro</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/*
	 * now on to  *whats* can they access
	 * 
	 * for my layout I have the entire site as a parent, two sub groups that contain all models.
	 * 
	 */</span>
&nbsp;
&nbsp;
		<span style="color: #000088;">$aco</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Aco<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;">//admin can access whole site</span>
		<span style="color: #000088;">$controllers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Entire_Site'</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//Iterate and create ARO groups</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$controllers</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//Remember to call create() when saving in loops...</span>
			<span style="color: #000088;">$aco</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</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;">//Save data</span>
			<span style="color: #000088;">$aco</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
				<span style="color: #000088;">$aco</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Aco<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;">//users have different permissions on Main and Auxilary models</span>
		<span style="color: #000088;">$controllers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Main_Models'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'parent_id'</span><span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1'</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Aux_Models'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'parent_id'</span><span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1'</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//Iterate and create ACO objects</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$controllers</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//Remember to call create() when saving in loops...</span>
			<span style="color: #000088;">$aco</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</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;">//Save data</span>
			<span style="color: #000088;">$aco</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
	<span style="color: #666666; font-style: italic;">/* 
	 * now the more details ACOs and their parents (refer to tree in post above)
	 */</span>
		<span style="color: #000088;">$aco</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Aco<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;">//Here's all of our sub-ACO info in an array we can iterate through</span>
<span style="color: #000088;">$controllers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Users'</span><span style="color: #339933;">,</span>
					<span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'User'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'parent_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Toolboxes'</span><span style="color: #339933;">,</span>
					<span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Toolbox'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'parent_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Items'</span><span style="color: #339933;">,</span>
					<span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Item'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'parent_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #cc66cc;">3</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Actions'</span><span style="color: #339933;">,</span>
					<span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Action'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'parent_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #cc66cc;">4</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Priorities'</span><span style="color: #339933;">,</span>
					<span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Priority'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'parent_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #cc66cc;">5</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Settings'</span><span style="color: #339933;">,</span>
					<span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Setting'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'parent_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #cc66cc;">6</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Botchecks'</span><span style="color: #339933;">,</span>
					<span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Botcheck'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'parent_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//Iterate and create ACO nodes</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$controllers</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//Remember to call create() when saving in loops...</span>
			<span style="color: #000088;">$aco</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</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;">//Save data</span>
			<span style="color: #000088;">$aco</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #990000;">die</span><span style="color: #339933;">;</span> <span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> assignPermissions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//give admins rights to everything!(top aco)</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allow</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'administrators'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Entire_Site'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//give users right to create and read main models</span>
               <span style="color: #666666; font-style: italic;">//updates and deletes are set at a user level (so only owners can edit or delete their items)</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allow</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Main_Models'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'create'</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;">Acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allow</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Main_Models'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'read'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//let them use (read) aux, but nothing else!</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allow</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Aux_Models'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'read'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'done'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> checkPermissions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//These all return true:</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'administrators'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Settings'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Items'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'create'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Actions'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'read'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//Remember, we can use the model/foreign key syntax </span>
		<span style="color: #666666; font-style: italic;">//for our user AROs</span>
		<span style="color: #666666; font-style: italic;">// think can &lt;User/Model&gt; &lt;x&gt; access &lt;Model&gt; ,&lt;action&gt;</span>
		<span style="color: #666666; font-style: italic;">// can    User   2356    acsess   Weapons</span>
		<span style="color: #666666; font-style: italic;">//$this-&gt;Acl-&gt;check(array('model' =&gt; 'User', 'foreign_key' =&gt; 2356), 'Weapons');</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'and dissallows...'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//But these return false:	</span>
<span style="color: #666666; font-style: italic;">//users can not delete or edit auxilary models (inherited)</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Actions'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'delete'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Actions'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'create'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//nor can they edit or delete main models (until we assign that on an individual basis)</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Items'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'delete'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Items'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'update'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'done'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>The final touches</h3>
<p>The final touches will come as you update or create your model actions.  WHen a user creates a new Toolbox (for example)  you will immediately grant that user update and delete privileges for that toolbox.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//example code when a user creates a model</span>
<span style="color: #666666; font-style: italic;">//let user with id 1234 update toolbox with id 5678</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allow</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'User'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'foreign_key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1234</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'model'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Toolbox'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'foreign_key'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'5678'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'update'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next time around you can use ACL to verify those rights to prevent anyone else the same privilege.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//example code when a user attempts action a model</span>
<span style="color: #666666; font-style: italic;">//can user with id 1234 in fact update toolbox with id 5678?</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'User'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'foreign_key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1234</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'model'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Toolbox'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'foreign_key'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'5678'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'update'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Making Changes to ACO or ARO tables from the Database<br />
<h3>
Couldn&#8217;t resist huh, just had to know what the underlying tables looked like? Good for you.  If your none too familiar with hierarchical structures in referential DB tables your probably wondering, what the deal with <strong>lft</strong> and <strong>rght</strong>, and why doesn&#8217;t just changing a parent ID move things around.  (You may have discovered this trying to move yourself from users to administrators through the DB.)</p>
<p>For all the lovely details, may I suggest <a href="http://edwardawebb.com/programming/php-programming/cakephp/nested-trees-cakephp">Nested Trees in CakePHP</a> (though the article really applies more generally to nested lists in any referential Database)</p>
]]></content:encoded>
			<wfw:commentRss>http://edwardawebb.com/web-development/cakephp/started-acl-cakephp/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Validating Optional Fields in CakePHP &#8211; The dreaded OR scenario</title>
		<link>http://edwardawebb.com/web-development/cakephp/validating-optional-fields-cakephp-dreaded-scenario</link>
		<comments>http://edwardawebb.com/web-development/cakephp/validating-optional-fields-cakephp-dreaded-scenario#comments</comments>
		<pubDate>Thu, 12 Feb 2009 00:01:32 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://edwardawebb.com/?p=416</guid>
		<description><![CDATA[So I am proud to say I frequent the CakePHP group on Google, and try to offer my assistance when I can. A lot of times I see people asking how they can validate optional fields, or validate fields that are dependent on others. An example, sure: Imagine you have an employees model.. yeah all [...]]]></description>
			<content:encoded><![CDATA[<p>So I am proud to say I frequent the CakePHP group on Google, and try  to offer my assistance when I can.  A lot of times I see people asking how they can<strong> validate optional fields, or validate fields that are dependent on others</strong>.</p>
<p>An example, sure:  Imagine you have an employees model.. yeah all modeled up, that looks good, nice work.</p>
<p>When you get to validation you realize&#8230;.<br />
To register they must enter <strong>either</strong> their PIN # <strong>or</strong> their First and Last Name.<br />
..but not all three.</p>
<p>THere are other scenarios you might think of&#8230; Cell or Work phone&#8230;. Email or mailing address.. etc..etc.</p>
<p>What gets a little more interesting here is that we should provide unique errors for each possible situation. Initially we can just ask for PIN or names.   If they enter just the PIN then they are all set, don&#8217;t bother them with either name.  If the user enters only their first name we say OK, they are trying names, but we need to ask for their last name, and not worry about the PIN. Same for just their last name, a unique error asking for first name.</p>
<p>As of yet I don&#8217;t believe CakePHP has a built in validation rule for such, but that&#8217;s no problem. Because CakePHP plays so nice, and is so extensible that it only takes a little bit of thought to make this a relatively simple feat.  Curious?  Read on!</p>
<h2><span id="more-416"></span>Validating Optional Fields in a CakePHP Model</h2>
<p>I am only going to share the model here because that is where the validation occurs. The corresponding form and controller could be baked or scaffolded, nothing special.</p>
<ul>
<li>Model</li>
<li>Demo</li>
</ul>
<p>The controller and other views are just as any other CakePHP collections.</p>
<p>My example will follow the one proposed in the beginning of this article. Users must enter a PIN number or the must enter a first and last name. But we will not require all three.</p>
<p>The trick is using one common method to check all related fields that has an awareness into the field that it is checking. THis allows you to compare the fields, determine the scenario, and print back the proper response for all them.</p>
<h2>The Model</h2>
<p>This is where all the magic happens, focus on the <strong>$validates</strong> values and the <strong>validateDependentFields()</strong> function.</p>
<h4>/app/models/user.php</h4>
<pre lang="php" hl_start="71" hl_count="42">
<?php
class User extends AppModel{
    var $name = 'User';
 	var $actsAs = array ('Userban'=>array());
    var $displayField = 'first_name';
	var $recursive = 0;
	var $validate = array(
        'username' => array(
		        'required' => array('rule'=>VALID_NOT_EMPTY,'message'=>'Please enter your login name'),
		        'pattern' => array('rule' => array('custom','/[a-zA-Z0-9\_\-]{6,30}$/i'),'message'=>'Must be 6 characters or longer with no spaces.'),
				'unique' => array('rule' => array('validateUniqueUsername'),'message'=>'This username is already in use, please try another.'),
    		),
        'first_name' => array(
		        'required' => array('rule'=>'validateDependentFields'),
		        'length' => array( 'rule' => array('maxLength', 60),'message'=>'That names a bit too long, keep it under 60 characters' )
    		),
		'last_name' => array(
		        'required' => array('rule'=>'validateDependentFields'),
		        'length' => array( 'rule' => array('maxLength', 60),'message'=>'That names a bit too long, keep it under 60 characters' )
    		),
		'employee_pin' => array(
		        'required' => array('rule'=>'validateDependentFields'),
		        'length' => array( 'rule' => array('maxLength', 60),'message'=>'That names a bit too long, keep it under 60 characters' )
    		),
		'password' => array(
						'required' => array('rule' => array('custom','/(?=^.{4,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/'),'message'=>'Must be 6 characters or longer'),
						'length' => array( 'rule' => 'validatePassword','message'=>'Your passwords dont match!' )
					),
		'email' => array('rule'=>'email','message'=>'Please enter your email address')
	);

	/**
	 * query functions
	 *
	 */

	/**
	 * validation functions
	 */

	function validatePassword(){
		$passed=true;
			//only run if there are two password feield (like NOT on the contact or signin pages..)
		if(isset($this->data['User']['confirmpassword'])){

			if($this->data['User']['password'] != $this->data['User']['confirmpassword']){
		    	//die('you fail');
		    	$this->invalidate('checkpassword');
		    	//they didnt condifrm password
		    	$passed=false;
		 	}else{
				//hash passwordbefore saving
				$this->data['User']['password']=md5($this->data['User']['password']);
			}
		}

		return $passed;
	}

	/**
	 * see whats up
	 *
	 *
	 */
	function validateDependentFields(&#038;$field){
		//assume the best of people <img src='http://edwardawebb.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' title="Validating Optional Fields in CakePHP   The dreaded OR scenario" />
		$passed=true;

		//we capture the value of the current field into $field

		switch(true){
			case array_key_exists('first_name',$field):
			//checking first name field
				//if pin is set then we dont care about first anme
				if(isset($this->data['User']['employee_pin']) &#038;&#038; !empty($this->data['User']['employee_pin'])){
					$passed=true;
				}else{
					//no pin, if this field is empty, scold them
					if(empty($this->data['User']['first_name'])) $passed="Please enter your First Name";
				}
			break;
			case array_key_exists('last_name',$field):
				//again, if Pin is set we skip, otherwise scold
				if(isset($this->data['User']['employee_pin']) &#038;&#038; !empty($this->data['User']['employee_pin'])){
					$passed=true;
				}else{
					//no pin, if this field is empty, scold them
					if(empty($this->data['User']['last_name'])) $passed="Please enter your Last Name";
				}
			break;
			case array_key_exists('employee_pin',$field):
				//checking PIN. ONly scold if empty  and first and last empty too
				if(
					empty($this->data['User']['first_name'])
					&#038;&#038; empty($this->data['User']['last_name'])
					&#038;&#038; empty($this->data['User']['employee_pin'])
				){
					$passed="You must enter your PIN, or name";
				}
			break;

		}

		return $passed;
	}

}?>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://edwardawebb.com/web-development/cakephp/validating-optional-fields-cakephp-dreaded-scenario/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Logical Bot Check in CakePHP not CAPTCHA</title>
		<link>http://edwardawebb.com/web-development/cakephp/logical-bot-check-cakephp-captcha</link>
		<comments>http://edwardawebb.com/web-development/cakephp/logical-bot-check-cakephp-captcha#comments</comments>
		<pubDate>Sat, 07 Feb 2009 21:58:57 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[bot checks]]></category>
		<category><![CDATA[captcha]]></category>

		<guid isPermaLink="false">http://edwardawebb.com/?p=387</guid>
		<description><![CDATA[Spam is bad agreed? Of course. But making readers painfully decode swirled letters and numbers is nearly as bad. That is why many sites will use logic questions to prevent spam and bot activity. Implementing logic questions on your mail forms and registration is easy for your readers and simple for you. This tutorial walks [...]]]></description>
			<content:encoded><![CDATA[<p>Spam is bad agreed?  Of course.  But making readers painfully decode swirled letters and numbers is nearly as bad. That is why many sites will<strong> use logic questions to prevent spam and bot activity</strong>.</p>
<p><strong>Implementing logic questions on your mail forms and registration is easy for your readers and simple for you.</strong> This tutorial walks through the steps to create one using CakePHP.  The solution will pick  a random question and answer stored in the database and based on desired difficulty. Hooray!</p>
<h2><span id="more-387"></span>Problem</h2>
<p>There be spam bots trying to get your treasure!  Or at least submit annoying emails and comments.</p>
<h2>Solution</h2>
<p>Ask the kind of simple and random questions that a bot just wouldn&#8217;t understand.  Some of my favorites;</p>
<ul>
<li>&#8220;Just one question &#8211; What is GOD spelled backwards?&#8221;</li>
<li>&#8220;Is this a question?&#8221;</li>
</ul>
<h2>Implementation</h2>
<p>So its all fine and dandy hard code a question into your forms, then hard code a check into your controllers, but it sure isn&#8217;t OO, and its definitely not CakePHP.</p>
<p>WHat if instead <strong>we threw a whole bunch of random questions into a DB, and pulled one each time someone went to a form</strong>?  Yes that would be nice indeed.  We will need to hold  a corresponding answer as well.</p>
<p>THere are a few parts to this solution, so pay attention.</p>
<ul>
<li>Managing site Settings
<ul>
<li>Model</li>
<li>Controller</li>
<li>Views</li>
</ul>
</li>
</ul>
<ul>
<li>Managing the Questions and Answers
<ul>
<li>Model</li>
<li>Controller</li>
<li>Views (add/edit etc.)</li>
</ul>
</li>
</ul>
<ul>
<li>Supplying the Q&amp;A to forms
<ul>
<li>BeforeFilter method</li>
</ul>
</li>
</ul>
<h3>Managing Site Settings &#8211; Settings</h3>
<p>Now this MVC group is optional.  I deliver a lot of prototypes to clients and found that asking them to edit any source file just introduces more problems.  For example if they want to change the site&#8217;s title, or tagline, or other metadata. TO solve this I provide a simple Settings model that stores these values in a user friendly way.<br />
<a href="http://edwardawebb.com/programming/php-programming/cakephp/global-site-settings-component-cakephp" title="Read how to use a Global Settings Component for your CakePHP Site"><br />
Read how to use a Global Settings Component for your CakePHP Site</a></p>
<p>If it is a personal site or you just don&#8217;t care for this you&#8217;ll need to adjust  how we obtain in the difficulty levels in the next model.</p>
<h3></h3>
<h3>Managing Questions and Answers &#8211; BotCheck</h3>
<p><strong>At the heart of my solution is a Model. I named it BotCheck</strong>, but you could call it PussNBoots for all I care.</p>
<h4>app/models/botcheck.php</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Botcheck <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
&nbsp;
&nbsp;
&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;">'Botcheck'</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;">'question'</span> <span style="color: #339933;">=&gt;</span> VALID_NOT_EMPTY<span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'answer'</span> <span style="color: #339933;">=&gt;</span> VALID_NOT_EMPTY<span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'difficulty'</span> <span style="color: #339933;">=&gt;</span> VALID_NOT_EMPTY<span style="color: #339933;">,</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;">$recursive</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>	
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$minDiff</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$maxDiff</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * set level to use
 * 
 */</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> setMinMax<span style="color: #009900;">&#40;</span><span style="color: #000088;">$minMax</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>	
		<span style="color: #666666; font-style: italic;">//import settings model to pull from method getBotcheckMinMax</span>
		App<span style="color: #339933;">::</span><span style="color: #004000;">import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Model'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Setting'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$ms</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> Setting<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$minMax</span><span style="color: #339933;">=</span><span style="color: #000088;">$ms</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBotcheckMinMax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//debug(Configure::read('App.botcheck_min_difficulty'));</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//now set the locAL varable </span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">minDiff</span><span style="color: #339933;">=</span><span style="color: #000088;">$minMax</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'min'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">maxDiff</span><span style="color: #339933;">=</span><span style="color: #000088;">$minMax</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'max'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>	
&nbsp;
	<span style="color: #009900;">&#125;</span> 
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Grabs a random question and answer from the database. 
	 * Only use minmaxoverride to circumvent table settings.
	 * 
	 * @param array minMaxOverride
	 *
	 */</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> getFreshBotcheck<span style="color: #009900;">&#40;</span><span style="color: #000088;">$minMax</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>			
&nbsp;
		<span style="color: #666666; font-style: italic;">//we need to load the settings form the settings table/model</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMinMax</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;">// set conditions for highest and lowest difficulty to pull</span>
		<span style="color: #000088;">$conditions</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'difficulty &gt;='</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">minDiff</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'difficulty &lt;='</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">maxDiff</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
&nbsp;
		<span style="color: #666666; font-style: italic;">//get nuimber of rows that match</span>
		<span style="color: #000088;">$rows</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findCount</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conditions</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
		<span style="color: #000088;">$record</span><span style="color: #339933;">=</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//use random number as page (doesnt depend on record ids)</span>
		<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conditions</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
		<span style="color: #666666; font-style: italic;">//we let answers be sets seperated by commas, make it an array after making it all lowercase</span>
		<span style="color: #000088;">$answers</span><span style="color: #339933;">=</span><span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</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;">'Botcheck'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'answer'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
		<span style="color: #666666; font-style: italic;">//simplify results into a ['question'] and ['answer'] keyed variable for direct pull</span>
		<span style="color: #000088;">$fbc</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'question'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$result</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;">'Botcheck'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'question'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'answers'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$answers</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$fbc</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4>app/controllers/botchecks_controller.php</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> BotchecksController <span style="color: #000000; font-weight: bold;">extends</span> AppController <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;">'Botchecks'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$helpers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Html'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Form'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$uses</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Botcheck'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Setting'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$user</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;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Role'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'rights'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;=</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/admin/'</span><span style="color: #339933;">.</span>Inflector<span style="color: #339933;">::</span><span style="color: #004000;">underscore</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #009900;">&#41;</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;">action</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> admin_index<span style="color: #009900;">&#40;</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;">Botcheck</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">recursive</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'botchecks'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">paginate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'bcMin'</span><span style="color: #339933;">,</span>Configure<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'App.botcheck_min_difficulty'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'bcMax'</span><span style="color: #339933;">,</span>Configure<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'App.botcheck_max_difficulty'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> admin_view<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$id</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;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid Botcheck.'</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'botcheck'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Botcheck</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> admin_add<span style="color: #009900;">&#40;</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: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Botcheck</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Botcheck</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The Botcheck has been saved'</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</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;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The Botcheck could not be saved. Please, try again.'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> admin_edit<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$id</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid Botcheck'</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</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;">Botcheck</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The Botcheck has been saved'</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</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;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The Botcheck could not be saved. Please, try again.'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Botcheck</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</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: #000000; font-weight: bold;">function</span> admin_delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$id</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;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid id for Botcheck'</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #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;">Botcheck</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">del</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Botcheck deleted'</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4>app/views/botchecks</h4>
<p><strong>These are standard CakePHP views (view/add/edit/delete) that can be baked via the console based on the above controller.</strong><br />
The views will allow you, or the application admin to add lots random questions and answers.  THe nice thing is that answers are an array. So you can ask &#8220;what do plants need to survive?&#8221; and accept &#8220;air&#8221; or &#8220;oxygen&#8221; or &#8220;water&#8221;..etc. etc.</p>
<h3>Supplying Random Logic Questions to User Forms</h3>
<p>Now we have a bunch of questions that are ready to be loaded at will.  We will take a look at my User&#8217;s controller to see<strong> how we pull a random question and answer pair and store them to session</strong>. This way if the user forgets there email, or another form field we can hang on to the same question and answer. No reason to make them feel like they are on  quiz show.</p>
<h4>app/controllers/users_controller.php &#8211; snippet</h4>
<pre lang="php" hl_start="72" hl_count="8">
<?php

	/**
	 * This sweet controller was written by
	 * @author Edward A Webb edwardawebb.com
	 *
	 */
class UsersController extends AppController {

	var $name = 'Users';
	var $uses =array('User','Botcheck');
	var $helpers = array('Html', 'Form');

	/*
	 * These variable hold a question and answer called on each load.
	 * these values are merely defaults in case a db error should occur. Ideally they will never be used.
	 * See beforeFilter()
	 */
	var $whatWeAsk = 'What is eight minus three?';
	var $humanWouldType= array(5, 'five');
	/**
	 * Botcheck must be included as a uses
	 */

	function beforeRender(){
		$this->pageTitle= ' // '.Inflector::humanize(Inflector::underscore($this->name)).' '.$this->action;

	}

	function beforeFilter(){
		//die('Not empty?: '.!empty($this->data));

		//grab a fresh botcheck question from the db
		//UNLESS there is already one in session AND there is data being passed from a form
		if($this->Session->check('botcheck') &#038;&#038; !empty($this->data)){
			//die('we got your stickin session');
			//This indicates we are likely just checking info entered,
			//be sure to use the same botcheck (hard for users to psychic.)
			$bc=$this->Session->read('botcheck');
		}else{
			//we must get a unique botcheck, since one does not exist, or it is likley from a past event
			$bc=$this->Botcheck->getFreshBotcheck();
			//save it for validation once they attempt to answer
			$this->Session->write('botcheck',$bc);
		}
		//set it to a variable used by actions
		$this->whatWeAsk=$bc['question'];
		$this->humanWouldType=$bc['answers'];
		//die($this->whatWeAsk);
	}
/**
..... MORE CODE
**/
	function notify_friends_and_family($wishListId){
		$merror=0;
		$someone=$this->checkSession(1,null,true);
		$wishListArr=$this->User->WishList->find('WishList.id = '.$wishListId);
		if (!$wishListId &#038;&#038; empty($this->data)) {
			$this->Session->setFlash('Um... Like, OK.  I dont\'t wanna be  buzzkill, but like, you totally need to need to visit that page from a wish list. Otherwise I am like totally unable to help you, K?', true);
			$this->redirect(array('controller'=>'WishLists','action'=>'index'));
		}elseif($someone['User']['id']!=$wishListArr['WishList']['owner_id']){
			$this->Session->setFlash('Wow! Your totally trying to notify people of a list you um, like don\'t own.  Maybe you should like come here from one of your list. Then you like, don\'t even need to worry and stuff, K?', true);
			$this->redirect(array('controller'=>'WishLists','action'=>'index'));

		}

		//is submitted?
		if (!empty($this->data)) {
			//botcheck!
			if(!in_array(strtolower($this->data['User']['check']),$this->humanWouldType))
			{
			  	$this->User->invalidate('check');
				$merror=1;
			}
			//set modified dates to now
			if ($merror==0) {

				$wishListArr=$this->User->WishList->find('WishList.id = '.$this->data['WishListId']);
				$this->Email->shareemail($this->data['email'],$someone['User']['first_name'],$wishListArr);
				$this->Session->setFlash(__('Your messages were sent.', true));
				$this->redirect(array('controller'=>'WishLists','action'=>'view',$this->data['WishListId']));
			} else {
				$this->Session->setFlash(__('Please Correct These Errors', true));
			}
		}
		// grab botcheck question
		$this->set('botQuestion',$this->whatWeAsk);
		$this->set('wishlist',$wishListArr);

	}
</pre>
<p>Notice 3 things;</p>
<ol>
<li>We declare our BotCheck model in the &#8216;Uses&#8217; line</li>
<li>We set default values for min and max </li>
<li>We add some logic in Before Filter to retrieve a fresh pair if it is a freshly visited form</li>
</ol>
<p>The method included (notify friends and family ) allows users to send emails to up to 6 addresses at once. Although they can&#8217;t adjust the message, its still worth keeping spam bots out.</p>
<h4>app/views/users/notify_friends_and_family.ctp</h4>
<pre lang="php" hl_start="19" hl_count="1">
<div class="wishLists form">
<h3>Ok, lets let everyone know about your <strong><?php echo $wishlist['WishList']['title'].'.'; ?></strong></h3>
<h4>OR, <?php echo $html->link('Go right to your list','/wish_lists/view/'.$wishlist['WishList']['id']);?>, or <?php echo $html->link('add your first item','/items/add/'.$wishlist['WishList']['id']); ?></h4>
<form method="post" enctype="multipart/form-data" action="<?php $html->link('/users/notify_friends_and_family/') ?>">
<fieldset>
<legend><?php __('Send WishList');?></legend>

		<?php
			// we set userid by session just before saving, prevents any trickery
			echo $form->input('email.First Recipient');
			echo $form->input('email.2');
			echo $form->input('email.3');
			echo $form->input('email.4');
			echo $form->input('email.5');
			echo $form->input('email.6');
			echo $form->input('WishListId',array('type'=>'hidden','value'=>$wishlist['WishList']['id']));
		?>	


	   	   <?php echo $form->input('User.check', array('size'=>'10','label'=>$botQuestion,'error'=>'Are you human?'));?>
		
</fieldset>

<?php echo $form->end('Submit');?>
</div>
<div class="actions">
<ul>
<li><?php echo $html->link(__('List WishLists', true), array('action'=>'index'));?></li>
<li><?php echo $html->link(__('List Users', true), array('controller'=> 'users', 'action'=>'index')); ?> </li>
<li><?php echo $html->link(__('New Owner', true), array('controller'=> 'users', 'action'=>'add')); ?> </li>
<li><?php echo $html->link(__('List Items', true), array('controller'=> 'items', 'action'=>'index')); ?> </li>
<li><?php echo $html->link(__('New Item', true), array('controller'=> 'items', 'action'=>'add')); ?> </li>
</ul>
</div>
</pre>
<h2>Summary</h2>
<p>SO now when someone lands on a page that we are checking they see this;<br />
<a href="http://edwardawebb.com/wp-content/uploads/2009/02/botcheck.png"><img src="http://edwardawebb.com/wp-content/uploads/2009/02/botcheck-300x210.png" alt="Bot Check - Logic Question" title="Bot Check - Logic Question" width="300" height="210" class="aligncenter size-medium wp-image-395" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://edwardawebb.com/web-development/cakephp/logical-bot-check-cakephp-captcha/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Global Site Settings Component for CakePHP</title>
		<link>http://edwardawebb.com/web-development/cakephp/global-site-settings-component-cakephp</link>
		<comments>http://edwardawebb.com/web-development/cakephp/global-site-settings-component-cakephp#comments</comments>
		<pubDate>Sat, 07 Feb 2009 21:31:16 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[cakephp component]]></category>

		<guid isPermaLink="false">http://edwardawebb.com/?p=389</guid>
		<description><![CDATA[When delivering an application that will be administered by clients I found it best to keep them out of the source code. THis means that any options, any at all, should be editable from a frontend.  Site name, taglines and metadata are examples of such settings.  Any constants that will be changed (Oxy-moronic I know) [...]]]></description>
			<content:encoded><![CDATA[<p>When delivering an application that will be administered by clients I found it best to keep them out of the source code. THis means that any options, any at all, should be editable from a frontend.  Site name, taglines and metadata are examples of such settings.  Any constants that will be changed (Oxy-moronic I know) can also go here.</p>
<h2><span id="more-389"></span>The Problem</h2>
<p>Letting your client edit  site_details.php just to change the site&#8217;s tagline ends up in 500 errors or the like.</p>
<h2>The Solution</h2>
<p>Implement a simple settings component that records simple key => value pairs.</p>
<ul>
<li>Model</li>
<li>Controller</li>
<li>View</li>
<li>Using the Values in your Code</li>
</ul>
<p>This allows them to tinker with any values you deem fit.  For example the KEY &#8216;site_name&#8217; could have a VALUE of &#8216;Charlie&#8217;s World&#8217;.</p>
<h2>Implementation</h2>
<h3>The Settings Model</h3>
<p>THe single method in the model will read only the values we care about for a particular controller and return them to that controller. <strong>To get an idea of how to call values from a controller <a href="http://edwardawebb.com/programming/php-programming/cakephp/logical-bot-check-cakephp-captcha">read this article on my logical botcheck</a>.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Setting <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;">'Setting'</span><span style="color: #339933;">;</span>
	<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;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> 
				<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>  
 				<span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rule'</span><span style="color: #339933;">=&gt;</span>VALID_NOT_EMPTY<span style="color: #339933;">,</span><span style="color: #0000ff;">'message'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Must not be empty!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>  
 				<span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'maxLength'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'message'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'That Key is a bit too long, keep it under 20 characters'</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;">'value'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> 
				<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>  
 				<span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rule'</span><span style="color: #339933;">=&gt;</span>VALID_NOT_EMPTY<span style="color: #339933;">,</span><span style="color: #0000ff;">'message'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Must not be empty!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>  
 				<span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'maxLength'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'message'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'That Value is a bit too long, keep it under 60 characters'</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><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> getBotcheckMinMax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$min</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$max</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$full_result</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'key like'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'botcheck_%_difficulty'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$full_result</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$record</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Setting'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'key'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'botcheck_min_difficulty'</span><span style="color: #339933;">:</span>
				<span style="color: #000088;">$min</span><span style="color: #339933;">=</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Setting'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'botcheck_max_difficulty'</span><span style="color: #339933;">:</span>
				<span style="color: #000088;">$max</span><span style="color: #339933;">=</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Setting'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">default</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'min'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$min</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'max'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//die($min.'--'.$max);</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>The Settings Controller</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> SettingsController <span style="color: #000000; font-weight: bold;">extends</span> AppController <span style="color: #009900;">&#123;</span>
&nbsp;
&nbsp;
&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;">'Settings'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$helpers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Html'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Form'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
	<span style="color: #666666; font-style: italic;">/*
&nbsp;
	 * Lockdown all access to admins
 (this uses my custom method for security,
	 *  you'll need to use auth or whatever you prefer to prevent guest or lowly users from access
	 */</span>	
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> beforeFilter<span style="color: #009900;">&#40;</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;">checkSession</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</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;">Setting</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">recursive</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'settings'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">paginate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> view<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$id</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;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid Setting.'</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>
&nbsp;
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'index'</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>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'setting'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Setting</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</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>
&nbsp;
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> add<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</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;">Setting</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Setting</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</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;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The Setting has been saved'</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>
&nbsp;
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'index'</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> <span style="color: #b1b100;">else</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;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The Setting could not be saved. Please, try again.'</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>
&nbsp;
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> edit<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$id</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</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;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid Setting'</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>
&nbsp;
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'index'</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>
&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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;">Setting</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</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;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The Setting has been saved'</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>
&nbsp;
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'index'</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> <span style="color: #b1b100;">else</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;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The Setting could not be saved. Please, try again.'</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>
&nbsp;
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</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;">data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Setting</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$id</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;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid id for Setting'</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>
&nbsp;
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'index'</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>
&nbsp;
&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;">Setting</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">del</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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;">setFlash</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Setting deleted'</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>
&nbsp;
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'index'</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>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>The Settings Views</h3>
<p>These are standard CakePHP views and can be baked from the model above, no point in wasting precious bits on that.</p>
<h3> Using the Settings Values in your Code </h3>
<p>OK, so we can add, edit and delete all the settings we want.<br />
<a title="See example using the Settings stored by this component" href="http://edwardawebb.com/programming/php-programming/cakephp/logical-bot-check-cakephp-captcha"><br />
Using these values is as simple as calling them from another controller</a></p>
<h2>Summary</h2>
<p>The end result is a editable collections of Settings that can be used by various other pages or controllers.<br />
<a href="http://edwardawebb.com/wp-content/uploads/2009/02/settingscom.png"><img src="http://edwardawebb.com/wp-content/uploads/2009/02/settingscom-300x86.png" alt="The View page of our Settings Component" title="The View page of our Settings Component" width="300" height="86" class="aligncenter size-medium wp-image-390" /></a><br />
Well there you have it. A over simplified explanation of my Settings component.  Feel free to ask questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://edwardawebb.com/web-development/cakephp/global-site-settings-component-cakephp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Piwik to Analyze visits on a CakePHP site</title>
		<link>http://edwardawebb.com/web-development/cakephp/piwik-analyze-visits-cakephp-site</link>
		<comments>http://edwardawebb.com/web-development/cakephp/piwik-analyze-visits-cakephp-site#comments</comments>
		<pubDate>Wed, 28 Jan 2009 17:53:24 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[analytics]]></category>
		<category><![CDATA[piwik]]></category>

		<guid isPermaLink="false">http://edwardawebb.com/?p=356</guid>
		<description><![CDATA[This is another article based on current search terms for my site. A good number of searches for &#8220;piwik + cakephp&#8221; are landing on various pages of my site. So if your one of those folks I&#8217;ll assume you want to use Piwik Analytics in your CakePHP site. Well your in luck, the solution is [...]]]></description>
			<content:encoded><![CDATA[<p>This is another article based on current search terms for my site.  A good number of searches for &#8220;piwik + cakephp&#8221; are landing on various pages of my site.</p>
<p>So if your one of those folks I&#8217;ll assume you want to use Piwik Analytics in your CakePHP site.</p>
<p>Well your in luck, the solution is nice and easy.  CakePHp provides some nice variables that we can use for detailed page tracking.<br/><br/><span id="more-356"></span></p>
<h2>The Problem</h2>
<p>You love Piwik, and/or You love Cake  and just want the two to get along nicely.<br />
<br/><br/><br />
<h2>The Solution</h2>
<p>They couldn&#8217;t get along better!  Two outstanding products that make you feel all warm and fuzzy when you smoosh them together, so let&#8217;s get started.<br/><br/><br/></p>
<h2>Implementing Piwik in a CakePHP application</h2>
<p>First thing is first, you need to install Piwik.  I recommend that you dedicate a distinct sub-domain to Piwik, since it can serve multiple sites.<br />
<br/><br />
For instance, I serve analytics to all my sites from one Piwik install.  The installation does not need to share a domain or server with the applications it tracks.<br />
<br/><br />
The install process for Piwik is well documented, and much like any other LAMP application. Just create a DB, upload the files, and run the install scripts.<br />
<strong><br/><br />
The next step is adding a new site to Piwik.  THat is also well documented, but can be found here is you need help, <a href="http://edwardawebb.com/web-development/multiple-sites-piwik"title="How to add a new site to your Piwik install" >Adding multiple sites to Piwik</a>.</strong><br />
<br/><br />
Ok so once you add the site to Piwik it will give you a snippet of JavaScript to insert in your site. <strong> For CakePHP sites we&#8217;ll add this snippet to our default layout. (And any other layouts you want to track)</strong><br/></p>
<h4>Piwik Site Snippet</h4>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;!-- Piwik --&gt;
&lt;a href=&quot;http://piwik.org&quot; title=&quot;Free analytics&quot; onclick=&quot;window.open(this.href);return(false);&quot;&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> pkBaseURL <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;https:&quot;</span> <span style="color: #339933;">==</span> document.<span style="color: #660066;">location</span>.<span style="color: #660066;">protocol</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;https://piwik.example.com/&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://piwik.example.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>unescape<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;%3Cscript src='&quot;</span> <span style="color: #339933;">+</span> pkBaseURL <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;piwik.js' type='text/javascript'%3E%3C/script%3E&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
piwik_action_name <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
piwik_idsite <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
piwik_url <span style="color: #339933;">=</span> pkBaseURL <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;piwik.php&quot;</span><span style="color: #339933;">;</span>
piwik_log<span style="color: #009900;">&#40;</span>piwik_action_name<span style="color: #339933;">,</span> piwik_idsite<span style="color: #339933;">,</span> piwik_url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&lt;object&gt;&lt;noscript&gt;&lt;p&gt;Free analytics &lt;img src=&quot;http://piwik.example.com/piwik.php?idsite=10&quot; style=&quot;border:0&quot; alt=&quot;&quot;/&gt;&lt;/p&gt;&lt;/noscript&gt;&lt;/object&gt;&lt;/a&gt;
&lt;!-- End Piwik Tag --&gt;</pre></div></div>

<p><br/><br />
Ok so the code&#8217;s in the layout, but you may find an issue with Piwik&#8217;s method of obtaining the url. Luckily we can build our own variable to pass to piwik.<br />
<br/><br />
This way we ensure that the pages list accurately reflects the layout of our site, like this;</p>
<p><div id="attachment_357" class="wp-caption aligncenter" style="width: 498px"><img class="size-full wp-image-357" title="Sample Pages Hierarchy" src="http://edwardawebb.com/wp-content/uploads/2009/01/greenlife.png" alt="Sample Pages Hierarchy" width="488" height="705" /><p class="wp-caption-text">Sample Pages Hierarchy</p></div><br />
<br/><br />
In order to ensure our urls are captured correctly I wrote a small php snippet to create the page url to use.  This varies slightly from 1.1 to 1.2 based on the changes for parameters from $params to $this->params. <br/></p>
<h4>CakePHP 1.1 app/views/layouts/default.ctp</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #666666; font-style: italic;">//determine whtehr to use url (which doesnt work on homepge ) or not.</span>
	<span style="color: #000088;">$pageUrl</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: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</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: #666666; font-style: italic;">//we need to concat our own url</span>
		<span style="color: #000088;">$pageWeSee</span><span style="color: #339933;">=</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'controller'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//only append action if not 'info' pages ('index' is hidden in url, and should be hidden here)</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pageWeSee</span><span style="color: #339933;">!==</span><span style="color: #0000ff;">'info'</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$pageWeSee</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//any parmaeterrs we should know about?</span>
		<span style="color: #000088;">$ps</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$ps</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$p</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">//end for each</span>
		<span style="color: #000088;">$pageUrl</span><span style="color: #339933;">=</span><span style="color: #000088;">$pageWeSee</span><span style="color: #339933;">.</span><span style="color: #000088;">$ps</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: #666666; font-style: italic;">//oh how sweet, a fully built url for us</span>
		<span style="color: #000088;">$pageUrl</span><span style="color: #339933;">=</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</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: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">//echo  '&lt;br/&gt;Using:&gt;'.$pageUrl.'&lt;';</span>
	<span style="color: #666666; font-style: italic;">//echo $arraykeys-&gt;getKeys($params['pass']);</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><br/></p>
<h4>CakePHP 1.2 app/views/layouts/default.ctp</h4>

<div class="wp_syntax"><div 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;">/**
	*Prep url for piwik
	*/</span>
	<span style="color: #000088;">$params</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//determine whtehr to use url (which doesnt work on homepge ) or not.</span>
	<span style="color: #000088;">$pageUrl</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: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</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: #666666; font-style: italic;">//we need to concat our own url</span>
		<span style="color: #000088;">$pageWeSee</span><span style="color: #339933;">=</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'controller'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//only append action if not 'info' pages ('index' is hidden in url, and should be hidden here)</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pageWeSee</span><span style="color: #339933;">!==</span><span style="color: #0000ff;">'info'</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$pageWeSee</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//any parmaeterrs we should know about?</span>
		<span style="color: #000088;">$ps</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$ps</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$p</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">//end for each</span>
		<span style="color: #000088;">$pageUrl</span><span style="color: #339933;">=</span><span style="color: #000088;">$pageWeSee</span><span style="color: #339933;">.</span><span style="color: #000088;">$ps</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: #666666; font-style: italic;">//oh how sweet, a fully built url for us</span>
		<span style="color: #000088;">$pageUrl</span><span style="color: #339933;">=</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</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: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">//echo  '&lt;br/&gt;Using:&gt;'.$pageUrl.'&lt;';</span>
	<span style="color: #666666; font-style: italic;">//echo $arraykeys-&gt;getKeys($params['pass']);</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><br/><br/></p>
<p>So all together the footer of your layouts should look like this;</p>
<h4>app/views/layouts/default.ctp</h4>

<div class="wp_syntax"><div 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;">/**
	*Prep url for piwik
	*/</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//determine whtehr to use url (which doesnt work on homepge ) or not.</span>
	<span style="color: #000088;">$pageUrl</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: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</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: #666666; font-style: italic;">//we need to concat our own url</span>
		<span style="color: #000088;">$pageWeSee</span><span style="color: #339933;">=</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'controller'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//only append action if not 'info' pages ('index' is hidden in url, and should be hidden here)</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pageWeSee</span><span style="color: #339933;">!==</span><span style="color: #0000ff;">'info'</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$pageWeSee</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//any parmaeterrs we should know about?</span>
		<span style="color: #000088;">$ps</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$ps</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$p</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">//end for each</span>
		<span style="color: #000088;">$pageUrl</span><span style="color: #339933;">=</span><span style="color: #000088;">$pageWeSee</span><span style="color: #339933;">.</span><span style="color: #000088;">$ps</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: #666666; font-style: italic;">//oh how sweet, a fully built url for us</span>
		<span style="color: #000088;">$pageUrl</span><span style="color: #339933;">=</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</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: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">//echo  '&lt;br/&gt;Using:&gt;'.$pageUrl.'&lt;';</span>
	<span style="color: #666666; font-style: italic;">//echo $arraykeys-&gt;getKeys($params['pass']);</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!-- Piwik --&gt;
	&lt;a href=&quot;http://piwik.org&quot; title=&quot;Website analytics&quot; onclick=&quot;window.open(this.href);return(false);&quot;&gt;
	&lt;script type=&quot;text/javascript&quot;&gt;
	var pkBaseURL = ((&quot;https:&quot; == document.location.protocol) ? &quot;https://piwik.example.com/&quot; : &quot;http://piwik.example.com/&quot;);
	document.write(unescape(&quot;%3Cscript src='&quot; + pkBaseURL + &quot;piwik.js' type='text/javascript'%3E%3C/script%3E&quot;));
	&lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot;&gt;
	&lt;!--
	piwik_action_name = '<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$pageUrl</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>';
	piwik_idsite = 5;
	piwik_url = pkBaseURL + &quot;piwik.php&quot;;
	piwik_log(piwik_action_name, piwik_idsite, piwik_url);
	//--&gt;
	&lt;/script&gt;&lt;object&gt;
	&lt;noscript&gt;&lt;p&gt;Website analytics &lt;img src=&quot;http://piwik.example.com/piwik.php&quot; style=&quot;border:0&quot; alt=&quot;piwik&quot;/&gt;&lt;/p&gt;
	&lt;/noscript&gt;&lt;/object&gt;&lt;/a&gt;
&lt;!-- /Piwik --&gt;</pre></div></div>

<p><br/></p>
<h3>The Result</h3>
<p>On any give page like http://example.com/posts/view/10042 the resulting piwik code would look like;</p>
<pre lang="javascript" hl_start="7" hl_count="1">
<!-- Piwik -->
	<a href="http://piwik.org" title="Free analytics" onclick="window.open(this.href);return(false);">
	<script type="text/javascript">
	var pkBaseURL = (("https:" == document.location.protocol) ? "https://piwik.example.com/" : "http://piwik.example.com/");
	document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
	</script><script type="text/javascript">
	piwik_action_name = 'posts/view/10042';
	piwik_idsite = 5;
	piwik_url = pkBaseURL + "piwik.php";
	piwik_log(piwik_action_name, piwik_idsite, piwik_url);
	</script>
	<object><noscript>

Free analytics <img src="http://piwik.example.com/piwik.php?idsite=10" style="border:0" alt=" Using Piwik to Analyze visits on a CakePHP site"  title="Using Piwik to Analyze visits on a CakePHP site" />

</noscript></object></a>

<!-- End Piwik Tag -->
</pre>
]]></content:encoded>
			<wfw:commentRss>http://edwardawebb.com/web-development/cakephp/piwik-analyze-visits-cakephp-site/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add an RSS Feed to your CakePHP Models</title>
		<link>http://edwardawebb.com/web-development/cakephp/add-rss-feed-cakephp-models</link>
		<comments>http://edwardawebb.com/web-development/cakephp/add-rss-feed-cakephp-models#comments</comments>
		<pubDate>Wed, 28 Jan 2009 00:27:10 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[feed]]></category>
		<category><![CDATA[rss]]></category>

		<guid isPermaLink="false">http://edwardawebb.com/?p=348</guid>
		<description><![CDATA[I recently developed a prototype for a client who insisted that the site include an RSS feed for the articles of the site. Not being one to turn down a challenge, and being aware of the Power of Cake, I assured them it could be completed with such a feature. The newsfeed would provide the [...]]]></description>
			<content:encoded><![CDATA[<p>I recently developed a prototype for a client who insisted that the site<strong> include an RSS feed for the articles of the site</strong>.  Not being one to turn down a challenge, and being aware of the Power of Cake, I assured them it could be completed with such a feature.</p>
<p>The newsfeed would provide the latest articles to be consumed by an aggregation software or other feed reader.</p>
<p>So in the text that follows I&#8217;ll outline what is involved to<strong> add news feed or rss feeds to a particular model of your CakePHP site.</strong></p>
<p><span id="more-348"></span></p>
<h2>The Problem</h2>
<p>News feeds are everywhere and essential in todays web development.  Your sites are just as good as anyone elses, and deserve this critical feature.</p>
<h2>The Solution</h2>
<p><strong>Implement a url that can be consumed by web browsers or feed readers to publicize your latest content to the world.</strong></p>
<h2>Implementing a Feed Generator in CakePHP</h2>
<p>If we think about this rationally there are 3 peices;</p>
<ul>
<li>A controller action to retrieve the latest articles</li>
<li>A view to generate the xml formatted excerpts</li>
<li>A routing piece to provide an acceptable url</li>
</ul>
<h3>The RSS Feed Action</h3>
<p>No need to over complicate things here. Imagine you have a standard Model, oh let&#8217;s say &#8216;Posts&#8217;.  All we need to do here is add a newsfeed action.<br />
By default the action will pull 5 posts within the last month, but the number can be overridden by passing a different number in the url (as with any action parameter).</p>
<h4>app/controllers/posts_controller.php -snippet</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #009933; font-style: italic;">/**
	 * Returns an array of all public posts less than one month old, orderd by date
	 *
	 * @return unknown
	 */</span> 
	<span style="color: #000000; font-weight: bold;">function</span> newsfeed<span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$count</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">recursive</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$posts</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'is_public = 1 
				AND is_published = 1 
				AND Post.date_modified &gt;= now() - INTERVAL 1 MONTH'</span>
					<span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Post.date_modified DESC'</span><span style="color: #339933;">,</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'requested'</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: #b1b100;">return</span> <span style="color: #000088;">$posts</span><span style="color: #339933;">;</span>
                 <span style="color: #009900;">&#125;</span>
                 <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'posts'</span><span style="color: #339933;">,</span><span style="color: #000088;">$posts</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It would be really helpful if CakePHP provided an RSS helper. Zoot alors !  They do!<br />
<h4>app/controllers/posts_controller.php -snippet</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$helpers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Rss'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$components</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'RequestHandler'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>The XML Generating View</h3>
<p>Just like any other view, all we need to do is transform some provided data into a formatted page. This time it will be strict XML without all the html clutter.</p>
<h4>app/views/posts/rss/newsfeed.ctp</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> rss_transform<span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</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: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Post'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'link'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'posts'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Post'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'guid'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'posts'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Post'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Post'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'body'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'pubDate'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Post'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'date_added'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>				
	<span style="color: #009900;">&#41;</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;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'items'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rss</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$posts</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rss_transform'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'channelData'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$channelData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>And of course sticking with format we should want to add an RSS layout.</p>
<h4>app/views/layouts/rss/default.ctp</h4>

<div class="wp_syntax"><div 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;">$rss</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$channelData</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: #0000ff;">'Recent News | Digital Business'</span><span style="color: #339933;">,</span>
		 <span style="color: #0000ff;">'link'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'posts'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'rss'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		 <span style="color: #0000ff;">'url'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'posts'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'rss'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		 <span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'The best Digital Business RSS Feed on the web'</span><span style="color: #339933;">,</span>
		 <span style="color: #0000ff;">'language'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'en-us'</span>
		 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$channel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rss</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$channelData</span><span style="color: #339933;">,</span> <span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$rss</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">document</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$channel</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>The RSS compliant URL</h3>
<p>You only need to add 2 lines to your routes configuration file for CakePHP to catch and handle the url to pint to your RSS feed.  You might use feed.rss, I choice live.rss.</p>
<h4>app/config/routes.php -snippet</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * ...allow rssextensions
 * and send live.rss to the rss feed
 */</span>
	Router<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/live'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'posts'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'newsfeed'</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;">// see my posts on sitemaps to use this next line ;)	</span>
Router<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/sitemap'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'sitemaps'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	Router<span style="color: #339933;">::</span><span style="color: #004000;">parseExtensions</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rss'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Yes, you&#8217;ll notice an additional route there for <a href="http://edwardawebb.com/programming/php-programming/cakephp/generating-dynamic-sitemaps-cakephp">dynamic sitemaps</a>, very useful as well.</p>
<p>Sweet!   Now just visit http://example.com/live.rss, or throw that url in your favorite Feed Reader to see the results.</p>
<h4>http://example.com/live.rss</h4>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rss</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;channel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Recent News | Digital Business<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://digbiz.localhost/posts.rss<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;posts</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;index</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rss</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The best Digital Business RSS Feed on the web<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;language<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>en-us<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/language<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>This is a Public, Published General News Article<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://digbiz.localhost/posts/view/1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;guid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://digbiz.localhost/posts/view/1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/guid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>It should be visible by ALL users and guests.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pubDate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Tue, 22 Jul 2008 11:22:51 -0400<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pubDate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>New Public News<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://digbiz.localhost/posts/view/6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;guid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://digbiz.localhost/posts/view/6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/guid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Lets spice this up a bit...<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pubDate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fri, 01 Aug 2008 13:00:25 -0400<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pubDate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Neil Hair Guest Essay in Democrat and Chronicle<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://digbiz.localhost/posts/view/9<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;guid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://digbiz.localhost/posts/view/9<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/guid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> Dr. Neil Hair comments on the Virtual Workforce of the
				future.
				http://www.democratandchronicle.com/apps/pbcs.dll/article?AID=2008810050343<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pubDate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Mon, 06 Oct 2008 09:03:36 -0400<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pubDate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/channel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rss<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #808080; font-style: italic;">&lt;!-- 0.3199s --&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://edwardawebb.com/web-development/cakephp/add-rss-feed-cakephp-models/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Create nested trees that expand and collapse in CakePHP</title>
		<link>http://edwardawebb.com/web-development/cakephp/create-nested-trees-expand-collapse-cakephp</link>
		<comments>http://edwardawebb.com/web-development/cakephp/create-nested-trees-expand-collapse-cakephp#comments</comments>
		<pubDate>Sat, 24 Jan 2009 14:39:45 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[javasccript]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">http://edwardawebb.com/?p=332</guid>
		<description><![CDATA[This is a horrible tittle, and I apologize.  What I mean is that we can show nested items based on parent-child relationships that will expand and collapse with a simple, click. The trick requires prototype and scriptalicious. The reason is ease of use and aesthetics.   If a user lands on a page, let&#8217;s say &#8216;Categories&#8217;  [...]]]></description>
			<content:encoded><![CDATA[<p>This is a horrible tittle, and I apologize.  What I mean is that we can <strong>show nested items based on parent-child relationships that will expand and collapse with a simple, click. </strong>The trick requires prototype and scriptalicious.</p>
<p>The reason is ease of use and aesthetics.   If a user lands on a page, let&#8217;s say &#8216;Categories&#8217;  and sees all the categories sprawled out across the page they&#8217;ll retreat in horror.</p>
<div id="attachment_333" class="wp-caption aligncenter" style="width: 160px"><img class="size-thumbnail wp-image-333" title="Expanded tree items - woah!" src="http://edwardawebb.com/wp-content/uploads/2009/01/expanded-150x150.png" alt="Defaulting to expanded trees is overwhelming to visitors" width="150" height="150" /><p class="wp-caption-text">Defaulting to expanded trees is overwhelming to visitors</p></div>
<p>Instead it would be much nicer to only show the top-most categories, and let them dive down where necessary.</p>
<div id="attachment_334" class="wp-caption aligncenter" style="width: 160px"><img class="size-thumbnail wp-image-334" title="Collapsed trees allow for quicker navigation" src="http://edwardawebb.com/wp-content/uploads/2009/01/collapsed-150x150.png" alt="Showing the condensed tree is much more aesthetically pleasing" width="150" height="150" /><p class="wp-caption-text">Showing the condensed tree is much more aesthetically pleasing</p></div>
<p>We can take it a step further by adding a nice slide effect so the sub-items rolldown like a window shade, and retreat in the same manner.  <a href="http://thegreenlifelist.org/categories" rel="nofollow" title="Tree that uses javascript to slide sub-items or branches into view"  target="_blank">See the sliding tree items here</a>.</p>
<h2><span id="more-332"></span>Using Nested Trees with Slide effect</h2>
<p>The feat is pretty straight forward and I walk through the code in 4 areas.</p>
<ul>
<li>The Controller</li>
<li>The Layout</li>
<li>The View</li>
<li>The Helper</li>
</ul>
<h3>The Controller to retrieve nested or threaded items</h3>
<p>There is only a minor change here being that we use findAllThreaded.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</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;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'isHunting'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//this tells my tree what type of link to use you prolly wont need this.</span>
              <span style="color: #666666; font-style: italic;">// the rest is required</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">recursive</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAllThreaded</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Extra parameters added       </span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'categories'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>The Layout will load required JavaScript libraries</h3>
<p>Our view is going to get a little trickier.  We&#8217;ll need to load Prototype and Scriptalicious in the layout.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;/js/prototype.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;/js/scriptaculous.js&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>Alternately, and preferably use the Javascript helper to load these libraries.</p>
<h3>The View to display our nested or threaded tree</h3>
<p>Now because I have two types of trees (remember the &#8216;isHunting&#8217; variable from our controller?  I add this little snippet to specify the type of tree I need. You may decide one tree is suitable for your needs.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$isHunting</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;h2&gt;Please Select Category&lt;/h2&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$tree</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">showHunt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Category/name'</span><span style="color: #339933;">,</span><span style="color: #000088;">$categories</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;h2&gt;Category Tree&lt;/h2&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$tree</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Category/name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$categories</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If the variable &#8216;isHunting&#8217; is true, then the tree items are links to send the visitor to another controller pre-populated. Otherwise they jump to the details about that Category.</p>
<h3>The Tree Helper</h3>
<p>This is based on the original Tree helper found in the bakery by James Hall <a href="http://bakery.cakephp.org/articles/view/threaded-lists" rel="nofollow" >http://bakery.cakephp.org/articles/view/threaded-lists</a></p>
<p>It was just too static for my needs, so here&#8217;s the enhanced version that leverages prototype and includes links as well as small icons to indicate whether the item is expanded or not.</p>
<p><strong>The time it takes to slide out or in for a particular branch can be adjusted in the loadScript method.  &#8216;Chitlens&#8217; is the number of sub-items or branches below.  This allows the velocity to remain steady by adjusting the total time accordingly.</strong></p>
<p>Also the font-size is reduced by 3% on each sub-level.  This code can be commented out to keep all levels the same size.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #000000; font-weight: bold;">class</span> TreeHelper <span style="color: #000000; font-weight: bold;">extends</span> Helper
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$helpers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Html'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Arraykeys'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$uniqueId</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
   <span style="color: #000000; font-weight: bold;">function</span> loadScript<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;
   	&lt;script type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text/javascript<span style="color: #000099; font-weight: bold;">\&quot;</span> language=<span style="color: #000099; font-weight: bold;">\&quot;</span>javascript<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;
	function showHide(id,chitlens,thisLI){
		var x=document.getElementById(id);
		var minusx=document.getElementById('minusx');
		var plusx=document.getElementById('plusx');			
		if(x) {
			var timeToTake=.2*chitlens;
			var isHidden=x.style.display=='none';
			if(isHidden){
				Effect.Grow(id,{duration : timeToTake, direction :'top-left'});
					thisLI.innerHTML=minusx.innerHTML;
			}else{
				Effect.Shrink(id,{duration : timeToTake, direction :'top-left'});
				if(thisLI){
					thisLI.innerHTML=plusx.innerHTML;
				}
			}						
		}
	}   	
	&lt;/script&gt;
	&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div class=&quot;notice&quot;&gt;Click on the + to expand sub-levels&lt;/div&gt;'</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">function</span> getUniqueId<span style="color: #009900;">&#40;</span><span style="color: #000088;">$level</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   	<span style="color: #000088;">$letter</span><span style="color: #339933;">=</span><span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">65</span><span style="color: #339933;">+</span><span style="color: #000088;">$level</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   	<span style="color: #000088;">$ret</span><span style="color: #339933;">=</span><span style="color: #000088;">$letter</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">uniqueId</span><span style="color: #339933;">;</span>
   	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">uniqueId</span><span style="color: #339933;">++;</span>
   	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">function</span> extraDivs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   	<span style="color: #000088;">$output</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'&lt;span id=&quot;minusx&quot; style=&quot;display:none;&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">image</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'icons/condense.png'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'alt'</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><span style="color: #0000ff;">'&lt;/span&gt;'</span><span style="color: #339933;">;</span>
   	<span style="color: #000088;">$output</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">'&lt;span id=&quot;plusx&quot; style=&quot;display:none;&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">image</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'icons/expand.png'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'alt'</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><span style="color: #0000ff;">'&lt;/span&gt;'</span><span style="color: #339933;">;</span>
   	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> show<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  	<span style="color: #000088;">$output</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadScript</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$modelName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fieldName</span><span style="color: #009900;">&#41;</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;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">list_element</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$modelName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fieldName</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">extraDivs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&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;">$output</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> showHunt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  	<span style="color: #000088;">$output</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadScript</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$modelName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fieldName</span><span style="color: #009900;">&#41;</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;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">list_element_hunt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$modelName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fieldName</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">extraDivs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&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;">$output</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> list_element<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$modelName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fieldName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$level</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
&nbsp;
  	 <span style="color: #000088;">$tab</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;	&quot;</span><span style="color: #339933;">;</span>
  	 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$modelName</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;Company&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  	 	<span style="color: #000088;">$modelPage</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;companies&quot;</span><span style="color: #339933;">;</span>
  	 <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$modelName</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;Category&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  	 	<span style="color: #000088;">$modelPage</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;categories&quot;</span><span style="color: #339933;">;</span>
  	 <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
  	<span style="color: #000088;">$bulletStyle</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'list-style-type:none'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$tabs</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">str_repeat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tab</span><span style="color: #339933;">,</span> <span style="color: #000088;">$level</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$li_tabs</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tabs</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$tab</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$fontstyle</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;">$level</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   		<span style="color: #000088;">$fontpercent</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">-</span><span style="color: #000088;">$level</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
   		<span style="color: #000088;">$fontstyle</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'font-size:'</span><span style="color: #339933;">.</span><span style="color: #000088;">$fontpercent</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'%;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tabs</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;ul class=<span style="color: #000099; font-weight: bold;">\&quot;</span>tree<span style="color: #000099; font-weight: bold;">\&quot;</span> style=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$fontstyle</span><span style="color: #339933;">.</span><span style="color: #000088;">$bulletStyle</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$hasChild</span><span style="color: #339933;">=</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'children'</span><span style="color: #009900;">&#93;</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: #339933;">;</span>
     <span style="color: #000088;">$howmany</span><span style="color: #339933;">=</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</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>
&nbsp;
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hasChild</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
&nbsp;
      	<span style="color: #000088;">$tId</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'tree'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUniqueId</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$level</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      	<span style="color: #000088;">$hidDiv</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'&lt;div id=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$tId</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; style=&quot;display:none; &quot;&gt;'</span><span style="color: #339933;">;</span>    
&nbsp;
      	<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$li_tabs</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;li id=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$tId</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;li<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">.</span>
      				<span style="color: #0000ff;">&quot;&lt;span onclick=<span style="color: #000099; font-weight: bold;">\&quot;</span>showHide('&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$tId</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;',&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$howmany</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,this);<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">.</span>
      				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">image</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'icons/expand.png'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'alt'</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>
      				<span style="color: #0000ff;">&quot;&lt;/span&gt;&quot;</span><span style="color: #339933;">.</span>
      				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$fieldName</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$modelPage</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/view/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      	<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$hidDiv</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">list_element</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'children'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$modelName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fieldName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$level</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$li_tabs</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/li&gt;&quot;</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;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$li_tabs</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;li&gt;&quot;</span><span style="color: #339933;">.</span>
      				<span style="color: #0000ff;">&quot;&lt;span&gt;&quot;</span><span style="color: #339933;">.</span>
      				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">image</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'icons/condense.png'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'alt'</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>
      				<span style="color: #0000ff;">&quot;&lt;/span&gt;&quot;</span><span style="color: #339933;">.</span>
      				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$fieldName</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$modelPage</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/view/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;/li&gt;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$tabs</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/ul&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> list_element_hunt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$modelName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fieldName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$level</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
&nbsp;
  	 <span style="color: #000088;">$tab</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;	&quot;</span><span style="color: #339933;">;</span>
  	 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$modelName</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;Company&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  	 	<span style="color: #000088;">$modelPage</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;companies&quot;</span><span style="color: #339933;">;</span>
  	 <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$modelName</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;Category&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  	 	<span style="color: #000088;">$modelPage</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;categories&quot;</span><span style="color: #339933;">;</span>
  	 <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
  	<span style="color: #000088;">$bulletStyle</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'list-style-type:none'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$tabs</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">str_repeat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tab</span><span style="color: #339933;">,</span> <span style="color: #000088;">$level</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$li_tabs</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tabs</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$tab</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$fontstyle</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;">$level</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   		<span style="color: #000088;">$fontpercent</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">-</span><span style="color: #000088;">$level</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
   		<span style="color: #000088;">$fontstyle</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'font-size:'</span><span style="color: #339933;">.</span><span style="color: #000088;">$fontpercent</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'%;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tabs</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;ul class=<span style="color: #000099; font-weight: bold;">\&quot;</span>tree<span style="color: #000099; font-weight: bold;">\&quot;</span> style=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$fontstyle</span><span style="color: #339933;">.</span><span style="color: #000088;">$bulletStyle</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$hasChild</span><span style="color: #339933;">=</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'children'</span><span style="color: #009900;">&#93;</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: #339933;">;</span>
     <span style="color: #000088;">$howmany</span><span style="color: #339933;">=</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</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>
&nbsp;
       <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hasChild</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
&nbsp;
      	<span style="color: #000088;">$tId</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'tree'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUniqueId</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$level</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      	<span style="color: #000088;">$hidDiv</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'&lt;div id=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$tId</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; style=&quot;display:none; &quot;&gt;'</span><span style="color: #339933;">;</span>    
&nbsp;
      	<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$li_tabs</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;li id=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$tId</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;li<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">.</span>
      				<span style="color: #0000ff;">&quot;&lt;span onclick=<span style="color: #000099; font-weight: bold;">\&quot;</span>showHide('&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$tId</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;',&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$howmany</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,this);<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">.</span>
      				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">image</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'icons/expand.png'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'alt'</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>
      				<span style="color: #0000ff;">&quot;&lt;/span&gt;&quot;</span><span style="color: #339933;">.</span>
      				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$fieldName</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;/products/add/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Add a new product using '</span><span style="color: #339933;">.</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$fieldName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      	<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$hidDiv</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">list_element</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'children'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$modelName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fieldName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$level</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$li_tabs</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/li&gt;&quot;</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;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$li_tabs</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;li&gt;&quot;</span><span style="color: #339933;">.</span>
      				<span style="color: #0000ff;">&quot;&lt;span&gt;&quot;</span><span style="color: #339933;">.</span>
      				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">image</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'icons/condense.png'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'alt'</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>
      				<span style="color: #0000ff;">&quot;&lt;/span&gt;&quot;</span><span style="color: #339933;">.</span>
      				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$fieldName</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;/products/add/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Add a new product using '</span><span style="color: #339933;">.</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$modelName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$fieldName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;/li&gt;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$tabs</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/ul&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong><br />
You&#8217;ll notice some static references to Company and Category which could definitely be cleaned up to use the inflection class, but I just never got to it.</strong></p>
<p>Additionally you can just eliminate the showHunt and list_elements_hunt methods. And remove the reference in the View if you only need one type of tree.</p>
<p>Anyway thats it.  I&#8217;m sure many of you will point out the repetitive code, lack of inflection and other areas that could use improvement, which is fine. I wrote this code quite a while back to land a prototyping job and just don;t care to update it.  At least its a start for those without anything to go on. <img src='http://edwardawebb.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' title="Create nested trees that expand and collapse in CakePHP" /> </p>
]]></content:encoded>
			<wfw:commentRss>http://edwardawebb.com/web-development/cakephp/create-nested-trees-expand-collapse-cakephp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Complex Validation in CakePHP 1.2</title>
		<link>http://edwardawebb.com/web-development/cakephp/complex-validation-cakephp-12</link>
		<comments>http://edwardawebb.com/web-development/cakephp/complex-validation-cakephp-12#comments</comments>
		<pubDate>Tue, 18 Nov 2008 23:43:30 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[unique field]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://edwardawebb.com/?p=237</guid>
		<description><![CDATA[With version 1.2 we got a much more powerful validation feature. I think that many people migrating from version 1.1 may not realize how much it offers. I offer a simple Users model. It has fields like username, password and email that require special and perhaps multiple validation rules. Here&#8217;s how we&#8217;ll get tricky; Username [...]]]></description>
			<content:encoded><![CDATA[<p>With version 1.2 we got a much more powerful validation feature.  I think that many people migrating from version 1.1 may not realize how much it offers.</p>
<p>I offer a simple Users model. It has fields like username, password and email that require special and perhaps multiple validation rules.  Here&#8217;s how we&#8217;ll get tricky;</p>
<ul>
<li>Username must be atleast X characters without spaces,</li>
<li>Username must of course, Be unique!</li>
<li>Email must be valid email and Unique</li>
<li>Password must be confirmed during registration or renewal process<br />
That is to say compared.</li>
</ul>
<p>We can also set a unique error message to each rule, and it will apply to both our add and edit forms.<br />
<span id="more-237"></span></p>
<h3>Controller</h3>
<p>No special work is done in the controller, so you can just use your basic scaffolded controller action and $this-&gt;Model-&gt;save() method.  All the trickery can be handled within the model.</p>
<h3>Model</h3>
<p>Here&#8217;s where the magic happens.  We can use multiple rules per field. This included minimum / maximum length requirements; and for username, a special RegEX. The password field also has a basic RegEx check before calling in a method to compare the two password fields before signing off and hashing the field.  Each individual rule is assigned its own error message.</p>
<h4>1.2 Users &#8211; In app/models/user.php</h4>
<p>[sourcecode language="php"]</p>
<p><?php<br />
class User extends AppModel{<br />
var $name = 'User';<br />
var $actsAs = array ('Userban'=>array());<br />
var $displayField = &#8216;first_name&#8217;;<br />
var $recursive = 0;<br />
var $validate = array(<br />
&#8216;username&#8217; => array(<br />
&#8216;required&#8217; => array(&#8216;rule&#8217;=>VALID_NOT_EMPTY,&#8217;message&#8217;=>&#8217;Please enter your login name&#8217;),<br />
&#8216;pattern&#8217; => array(&#8216;rule&#8217; => array(&#8216;custom&#8217;,'/[a-zA-Z0-9\_\-]{6,30}$/i&#8217;),&#8217;message&#8217;=>&#8217;Must be 4 characters or longer with no spaces.&#8217;),<br />
&#8216;unique&#8217; => array(&#8216;rule&#8217; => array(&#8216;validateUniqueUsername&#8217;),&#8217;message&#8217;=>&#8217;This username is already in use, please try another.&#8217;),<br />
),<br />
&#8216;first_name&#8217; => array(<br />
&#8216;required&#8217; => array(&#8216;rule&#8217;=>VALID_NOT_EMPTY,&#8217;message&#8217;=>&#8217;You\&#8217;ll need a name friends and family will recognize!&#8217;),<br />
&#8216;length&#8217; => array( &#8216;rule&#8217; => array(&#8216;maxLength&#8217;, 60),&#8217;message&#8217;=>&#8217;That names a bit too long, keep it under 60 characters&#8217; )<br />
),<br />
&#8216;last_name&#8217; => array(<br />
&#8216;required&#8217; => array(&#8216;rule&#8217;=>VALID_NOT_EMPTY,&#8217;message&#8217;=>&#8217;You\&#8217;ll need a name friends and family will recognize!&#8217;),<br />
&#8216;length&#8217; => array( &#8216;rule&#8217; => array(&#8216;maxLength&#8217;, 60),&#8217;message&#8217;=>&#8217;That names a bit too long, keep it under 60 characters&#8217; )<br />
),<br />
&#8216;password&#8217; => array(<br />
&#8216;required&#8217; => array(&#8216;rule&#8217; => array(&#8216;custom&#8217;,'/[a-zA-Z0-9\_\-]{6,}$/i&#8217;),&#8217;message&#8217;=>&#8217;Must be 6 characters or longer&#8217;),<br />
&#8216;length&#8217; => array( &#8216;rule&#8217; => &#8216;validatePassword&#8217;,'message&#8217;=>&#8217;Your passwords dont match!&#8217; )<br />
),<br />
&#8216;email&#8217; => array(&#8216;rule&#8217;=>&#8217;email&#8217;,'message&#8217;=>&#8217;Please enter your email address&#8217;)<br />
);</p>
<p>/**<br />
* validation functions<br />
*/</p>
<p>/**<br />
* Check for existing user<br />
*/<br />
function validateUniqueUsername(){<br />
$error=0;<br />
//Attempt to load based on data in the field<br />
$someone = $this->findByUsername($this->data['User']['username']);<br />
// if we get a result, this user name is in use, try again!<br />
if (isset($someone['User']))<br />
{<br />
$error++;<br />
//debug($someone);<br />
//exit;</p>
<p>}<br />
return $error==0;<br />
}</p>
<p>function validatePassword(){<br />
$passed=true;<br />
//only run if there are two password feield (like NOT on the contact or signin pages..)<br />
if(isset($this->data['User']['confirmpassword'])){</p>
<p>if($this->data['User']['password'] != $this->data['User']['confirmpassword']){<br />
//die(&#8216;you fail&#8217;);<br />
$this->invalidate(&#8216;checkpassword&#8217;);<br />
//they didnt condifrm password<br />
$passed=false;<br />
}else{<br />
//hash passwordbefore saving<br />
$this->data['User']['password']=md5($this->data['User']['password']);<br />
}<br />
}</p>
<p>return $passed;<br />
}</p>
<p>}?></p>
<p>[/sourcecode]</p>
<h3>View</h3>
<h4>in app/views/users/register.ctp,</h4>
<p>[sourcecode language="php"]</p>
<div id="view_page">
<h2>register</h2>
<form action="<?php echo $html->url(&#8216;/users/register&#8217;); ?>&#8221; method=&#8221;post&#8221;></p>
<div class="required">
	<?php echo $form->input(&#8216;User.username&#8217;);?></p>
</div>
<div class="required">
 	<?php echo $form->input(&#8216;User.password&#8217;, array(&#8216;size&#8217; => &#8217;30&#8242;,&#8217;div&#8217;=> false ));?>
</div>
<div class="required">
	<?php echo $form->input(&#8216;User.confirmpassword&#8217;, array(&#8216;size&#8217; => &#8217;30&#8242;,&#8217;type&#8217;=>&#8217;password&#8217;,'div&#8217;=> false ));?>
</div>
<p> 	<?php echo $form->input(&#8216;User.first_name&#8217;, array(&#8216;size&#8217; => &#8217;30&#8242;));?></p>
<p>	<?php echo $form->input(&#8216;User.last_name&#8217;, array(&#8216;size&#8217; => &#8217;20&#8242;));?></p>
<div class="required">
 	<?php echo $form->input(&#8216;User.email&#8217;, array(&#8216;size&#8217; => &#8217;40&#8242;,&#8217;div&#8217;=> false ));?><br />
	<?php echo $form->error(&#8216;User.duplicateemail&#8217;,'Sorry, <b> That email is already is use</b><br />&#8216;.$html->link(&#8216;Is it yours?&#8217;,'/users/resetpassword/&#8217;.$this->data['User']['email']).&#8217; &#8216;,array(&#8216;escape&#8217;=>false))?>
</div>
<div class="required">
	<?php echo $form->input(&#8216;User.check&#8217;, array(&#8216;size&#8217;=>&#8217;10&#8242;,&#8217;label&#8217;=>$botQuestion,&#8217;error&#8217;=>&#8217;Are you human?&#8217;,'div&#8217;=>false));?><br />
	<?php echo $form->error(&#8216;User.&#8217;, &#8216;Are you human!&#8217;);?>
</div>
<div class="submit">
	<?php echo $form->submit(&#8216;Register&#8217;);?>
</div>
</form>
</div>
<p><?php echo pr($this->data);?><br />
[/sourcecode]</p>
<p>As you can see, the limits to validation are endless because worse scenario you write your own method unique to the situation.</p>
<p>Post Script:</p>
<p>Any feedback on the new syntax highlighting would be appreciated.  This one allows direct copy to your clipboard, and plain text viewing but trashes the formatting.  Maybe someone knows of another plugin for wordpress to do the same. If not I think I&#8217;ll stick with GeSHi</p>
]]></content:encoded>
			<wfw:commentRss>http://edwardawebb.com/web-development/cakephp/complex-validation-cakephp-12/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Reset Lost Passwords in CakePHP</title>
		<link>http://edwardawebb.com/web-development/cakephp/reset-lost-passwords-cakephp</link>
		<comments>http://edwardawebb.com/web-development/cakephp/reset-lost-passwords-cakephp#comments</comments>
		<pubDate>Thu, 23 Oct 2008 23:05:38 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[tickets]]></category>
		<category><![CDATA[users]]></category>

		<guid isPermaLink="false">http://edwardawebb.com/?p=209</guid>
		<description><![CDATA[Allowing users to create passwords is critical. But what happens when they want to change their password, or worse yet a user forgets their password. Will they be forever banned, unable to remember that hastily typed string?! Allowing users to send password reset tickets to their original email is a pretty good way to solve [...]]]></description>
			<content:encoded><![CDATA[<p>Allowing users to create passwords is critical. But what happens when they want to change their password, or worse yet a user forgets their password. Will they be  forever banned, unable to remember that hastily typed string?!</p>
<p><strong>Allowing users to send password reset tickets to their original email</strong> is a pretty good way to solve this, and is seen throughout the web.</p>
<p>&nbsp;</p>
<p><strong><br />
Its a snap in CakePHP as well.  Here&#8217;s how it works.</strong></p>
<ol>
<li>User enters their email address.</li>
<li>Cake checks the user table for a match, and sends out a 24 hour ticket.</li>
<li>User receives ticket, follows link, and is then allowed to enter a new password.</li>
<li>Ticket is &#8216;Punched&#8217; so to speak, and the user can login as usual.</li>
</ol>
<p>To manage these tasks we&#8217;ll rely on the users model and our new tickets model.</p>
<p><span id="more-209"></span></p>
<p>&nbsp;</p>
<p>First off let me be clear. I don&#8217;t use Auth component. Just a matter of taste.  So in my example I will be interacting directly with my users model, and sessions for any authentication points.</p>
<p>Second I use my own <a href="http://edwardawebb.com/programming/php-programming/cakephp/simple-email-component-cakephp">email component</a> as well, but again you can easily change those areas to meet your needs.</p>
<p>&nbsp;</p>
<h3>Users Controller</h3>
<p>You should already have this, so we&#8217;ll just look at the new actions.</p>
<ul>
<li>Create Ticket</li>
<li>Use Ticket</li>
<li>Enter New Password</li>
</ul>
<h4>in app/controllers/users_controller.php</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #009933; font-style: italic;">/**
	 * This sweet controller was written by
	 * @author Edward A Webb edwardawebb.com
	 * 
	 */</span>
<span style="color: #000000; font-weight: bold;">class</span> UsersController <span style="color: #000000; font-weight: bold;">extends</span> AppController <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;">'Users'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$uses</span> <span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Ticket'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$helpers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Html'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Form'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$components</span> <span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Email'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Ticketmaster'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> resetpassword<span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//grab a fresh botcheck question from the db</span>
<span style="color: #666666; font-style: italic;">// for this example youll need to static code these, my botcheck article is coming soon though</span>
		<span style="color: #666666; font-style: italic;">// $bc=$this-&gt;Botcheck-&gt;getFreshBotcheck();</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">whatWeAsk</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Is water a liquid at room temperature?&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">humanWouldType</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Yes'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'of course'</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;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'botQuestion'</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">whatWeAsk</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$email</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">//show form</span>
		<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//already entered email</span>
			<span style="color: #000088;">$botcheck</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'check'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">//set email to passed variable if present</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$email</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// make sure whave email and a check</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$email</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;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">invalidate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$botcheck</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;">humanWouldType</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				 	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">invalidate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'check'</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: #666666; font-style: italic;">//email entered, check for it</span>
					<span style="color: #000088;">$account</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findByEmail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$account</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'isBanned'</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;">//banned user, tell em where to go</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;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;h3&gt;This account is locked due to violation of terms&lt;/h3&gt;'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
					<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$account</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;h3&gt;We Don\'t have such and email on record.&lt;/h3&gt;'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #009900;">&#125;</span>
					<span style="color: #000088;">$hashyToken</span><span style="color: #339933;">=</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mdY'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4000000</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4999999</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Ticketmaster</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hashyToken</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;">Email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">useremail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #339933;">,</span><span style="color: #000088;">$account</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Ticket'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'hash'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$hashyToken</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Ticket'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'data'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$email</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Ticket'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'expires'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Ticketmaster</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getExpirationDate</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Ticket</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
						<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'An email has been sent with instructions to reset your password'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</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;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Ticket could not be issued'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> useticket<span style="color: #009900;">&#40;</span><span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//purge all expired tickets</span>
		<span style="color: #666666; font-style: italic;">//built into check</span>
		<span style="color: #000088;">$results</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Ticketmaster</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkTicket</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hash</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;">$results</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//now pull up mine IF still present</span>
			<span style="color: #000088;">$passTicket</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findByEmail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Ticket'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'data'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Ticketmaster</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">voidTicket</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hash</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;">'tokenreset'</span><span style="color: #339933;">,</span><span style="color: #000088;">$passTicket</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</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;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Enter your new password below'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users/newpassword/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$passTicket</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Your ticket is lost or expired.'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> newpassword<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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>
&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;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tokenreset'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//user is not logged in, BUT has TOKEN in hand</span>
		<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// But you only want authenticated users to access this action.</span>
<span style="color: #666666; font-style: italic;">//lines like the one below 'checkSession are  authentication code, so you can ignore these or use Auth</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkSession</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'/users/edit/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">//But youll need to read the user info somehow, and only the user who owns the profile </span>
			<span style="color: #000088;">$attempter</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;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">//make sure its the admin or the rigth user</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attempter</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">!=</span><span style="color: #000088;">$id</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$attempter</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Role'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'rights'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">//not  the user, not the admin and not a reset request via toekns</span>
				<span style="color: #666666; font-style: italic;">/*
				 * SHAME
				 */</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Userban</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">banuser</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Edit Anothers Password'</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;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Your account has been banned'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>	
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</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;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tokenreset'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$id</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;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tokenreset'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$id</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;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid id for User'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users/index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</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>				
&nbsp;
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">//delkete session token and dlete used ticket from table</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;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tokenreset'</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;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The User\'s Password has been updated'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</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;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please correct errors below.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<h2>The rest is new</h2>
<h3>Tickets Model</h3>
<h4>in app/models/ticket.php</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Ticket <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;">'Ticket'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$recursive</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>&nbsp;</p>
<h4>MySQL query to build the ticket table</h4>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</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;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`prefix_tickets`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> int<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;">`hash`</span> varchar<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;">`data`</span> varchar<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;">`expires`</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: #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;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`hash`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`hash`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ;</pre></div></div>

<p>&nbsp;</p>
<h3>Ticketmaster Component</h3>
<p>This manages tickets, creation, validation and destruction.</p>
<h4>in app/controllers/components/ticketmaster.php</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> TicketmasterComponent <span style="color: #000000; font-weight: bold;">extends</span> object<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$sitename</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'Site to use in Email message'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$linkdomain</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'example.com'</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//how many hours to honor token</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$hours</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">24</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
 *  Startup - Link the component to the controller.
 */</span> 
    <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$controller</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;">controller</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000088;">$controller</span><span style="color: #339933;">;</span>    	
    <span style="color: #009900;">&#125;</span>	
	<span style="color: #000000; font-weight: bold;">function</span> getExpirationDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$date</span><span style="color: #339933;">=</span><span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'%c'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$date</span><span style="color: #339933;">=</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$date</span><span style="color: #339933;">+=</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hours</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$expired</span><span style="color: #339933;">=</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d H:i:s'</span><span style="color: #339933;">,</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$expired</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> createMessage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$token</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000088;">$ms</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'&lt;html&gt;&lt;head&gt;&lt;title&gt;Password Reset Request&lt;/title&gt;&lt;/head&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$ms</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'&lt;body&gt;Your email has been used in a password reset request at '</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sitename</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br/&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$ms</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">'If you did not initiate this request, then ignore this message.&lt;br/&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$ms</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">'  Copy the link below into your browser to reset your password.&lt;br/&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$ms</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">'&lt;a href=&quot;http://'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">linkdomain</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/users/useticket/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$token</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;Reset Password&lt;/a&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$ms</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">'&lt;/body&gt;&lt;/html&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$ms</span><span style="color: #339933;">=</span><span style="color: #990000;">wordwrap</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ms</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">70</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$ms</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> purgeTickets<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Ticket</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Ticket.expires &lt;= now() LIMIT 1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>	
&nbsp;
	<span style="color: #666666; font-style: italic;">/*
	 * actually for logical reason well be indiscrimnate and clean ALL tockets for this email
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> voidTicket<span style="color: #009900;">&#40;</span><span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Ticket</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteAll</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hash'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> checkTicket<span style="color: #009900;">&#40;</span><span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">purgeTickets</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$ret</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$tick</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Ticket</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findByHash</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hash</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;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tick</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//no more ticket			</span>
		<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$ret</span><span style="color: #339933;">=</span><span style="color: #000088;">$tick</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>&nbsp;</p>
<h3>User Views</h3>
<p>Its always nice to let users interact with actions, so;</p>
<p>First this form allows users to create a ticket that will get them into the form above without their password.</p>
<h4>in app/views/users/resetpassword.ctp</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;div id=&quot;view_page&quot;&gt;
&lt;h2&gt;Reset Lost Password&lt;/h2&gt;
&lt;form action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users/resetpassword/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; method=&quot;post&quot;&gt;
&lt;div class=&quot;required&quot;&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;">label</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/email'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'E-mail'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
 	<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;">text</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/email'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'size'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'40'</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>
	<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;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/email'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Please enter email'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;
&lt;div&gt;
	&lt;p&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;">'User.check'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'size'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'10'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'label'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$botQuestion</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'error'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Are you human?'</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;/p&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;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/check'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Are you human!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;
&nbsp;
&lt;div class=&quot;submit&quot;&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;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Reset Password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;</pre></div></div>

<p>&nbsp;</p>
<p>Next the useticket action of the controller will allow users to link from the email they received with a unique &#8216;token&#8217;, like a temporary pass.</p>
<p>They will be redirected here, and allowed to enter a new password.</p>
<h4>in app/views/users/newpassword.ctp</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;div id=&quot;view_page&quot;&gt;
	&lt;h2&gt;New Password&lt;/h2&gt;
	&lt;form action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users/newpassword/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/id'</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>&quot; method=&quot;post&quot;&gt;
	&lt;div class=&quot;required&quot;&gt; 
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'last_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	 	<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;">hidden</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/first_name'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'size'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'60'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'enabled'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'false'</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;/div&gt;
	&lt;div class=&quot;required&quot;&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;">hidden</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/last_name'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'size'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'60'</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;/div&gt;
	&lt;div class=&quot;required&quot;&gt; 
		&lt;b&gt;Username: &lt;/b&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	 	<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;">hidden</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/username'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'size'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'60'</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;/div&gt;
	&lt;div class=&quot;required&quot;&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;">label</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	 	<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;">password</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/password'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'size'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'30'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'value'</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><span style="color: #000000; font-weight: bold;">?&gt;</span>
		<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;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Please enter the Password.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/div&gt;
	&lt;div class=&quot;required&quot;&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;">label</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/confirmpassword'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Confirm Password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	 	<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;">password</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/confirmpassword'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'size'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'30'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'value'</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><span style="color: #000000; font-weight: bold;">?&gt;</span>
		<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;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/confirmpassword'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Please enter the Password Again.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
		<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;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/checkpassword'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Please Be Sure Passwords Match.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/div&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;">hidden</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User/id'</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;div class=&quot;submit&quot;&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;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Save'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/div&gt;
	&lt;/form&gt;
&lt;/div&gt;
&lt;ul class=&quot;actions&quot;&gt;
&lt;li&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Return Home'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/li&gt;
&lt;/ul&gt;</pre></div></div>

<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://edwardawebb.com/web-development/cakephp/reset-lost-passwords-cakephp/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
