<?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>BinaryKitten&#039;s Blog &#187; coding</title>
	<atom:link href="http://binarykitten.me.uk/tag/coding/feed" rel="self" type="application/rss+xml" />
	<link>http://binarykitten.me.uk</link>
	<description></description>
	<lastBuildDate>Tue, 07 Sep 2010 08:02:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Active Module Based Config with Zend Framework</title>
		<link>http://binarykitten.me.uk/dev/zend-framework/177-active-module-based-config-with-zend-framework.html</link>
		<comments>http://binarykitten.me.uk/dev/zend-framework/177-active-module-based-config-with-zend-framework.html#comments</comments>
		<pubDate>Mon, 04 Jan 2010 22:41:41 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://binarykitten.me.uk/?p=177</guid>
		<description><![CDATA[I&#8217;ve recently taken to using Zend Framework for a project that I needed to bring up to date. I won&#8217;t go into the pros and cons of choosing a framework as there are many much more qualified people who have done a much better job of this subject than I would or could. So Instead [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently taken to using Zend Framework for a project that I needed to bring up to date. I won&#8217;t go into the pros and cons of choosing a framework as there are many much more qualified people who have done a much better job of this subject than I would or could. So Instead I bring to you How I managed to get Active Module Based Configuration within Zend Framework.</p>
<h2><strong>The Problem</strong></h2>
<p>The Concept I wanted to achieve was to have unique Configuration based upon the module that was active. The Issue with this is that the Bootstrap files and the _init functions for ALL modules are called with no bias as to which module is active. Thus if you created a 3 modules wanted to make menu alterations in one, those alterations will be applied to all. I also wanted to have a a system where if i added extra modules i could just add extra functions to the bootstrap file and it would work in a similar way.</p>
<p>With this in mind, I set about trying to figure out the solution.<br />
<span id="more-177"></span></p>
<h2><strong>New Version</strong></h2>
<p>Though this version still works and you should read through the code to see how to implement the plugin, there is a new version available at:<br />
<a href="http://binarykitten.me.uk/dev/zend-framework/296-active-module-config-v2.html">http://binarykitten.me.uk/dev/zend-framework/296-active-module-config-v2.html</a> <br />
Please refer to this as the latest version.. Thanks</p>
<p>&#8212;&#8211; Original Post &#8212;&#8211;</p>
<h2><strong>Solution 1 &#8211; Failure</strong></h2>
<p>With the Idea that no-body is perfect, including myself. My 1st attempt ended in failure. This attempt was to modify/extend the Bootstrap class to add extra functions to the resources list.. In the end I couldn&#8217;t determine if the Module bootstrap was the active one. ok So Attempt 1 was a failure, onto the next</p>
<h2>Solution 2 &#8211; Success!</h2>
<p>A quick conversation with Matthew Weier O&#8217;Phinney (<a href="http://twitter.com/weierophinney">@weierophinney</a>) pointed me in the direction of Controller plugins and the routeShutdown method, as after the route had been finished which module was active would be able to be discerned.<br />
At this point I must apologise to Pieter Kokx ( @kokxie )  who I had a small disagreement with in the #zftalk channel. Pieter had done his best to point me down this route to start with, though being a stubborn mule I am refused to see the quality and precision of his comments.<br />
Thank you both for your help here.</p>
<p>The way that this works is that is scans the active modules bootstrap for functions starting with <span style="text-decoration: underline">activeInit</span> or <span style="text-decoration: underline">modulenameInit</span> just like the <a href="http://is.gd/64s1y">_init</a> functions but these would only be called if the module is active.<br />
I was successfully able to create the plugin and trigger the functions, unfortunately it was triggering/calling them in a static sense.. which meant that standard _init style code wouldn&#8217;t work. Luckily with a little digging in the source of the framework i found a storage of the modules and their initiated bootstrap classes. Lucky Me! So finally we call the methods within the right context.</p>
<p>So Here it is the Final Code, Please do comment, I learn from people as I hope that others can learn from me.</p>
<p>I&#8217;ve used the &#8220;Namespace&#8221; of BinaryKitten here. If you want to use a different &#8220;Namespace&#8221; Replace BinaryKitten with what you want.<br />
The &#8220;Namespace&#8221; allows for the use of the BinaryKitten folder within the Libray Folder.<br />
Remember the Controller Plugin should go in the right place for your application, if you are using the autoloader you can add the &#8220;Namespace&#8221; to be autoloaded via your application.ini</p>
<pre class="brush: text">
autoloadernamespaces[] = &quot;BinaryKitten&quot;
</pre>
<p>First off we have the Controller Plugin.<br />
This should go into the &#8220;Namespace&#8221; folder within the Library Folder and should be called ModuleConfig.php</p>
<pre class="brush: php">
&lt;?php
class BinaryKitten_ModuleConfig extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $frontController = Zend_Controller_Front::getInstance();
        $bootstrap =  $frontController-&gt;getParam(&#039;bootstrap&#039;);
        $activeModuleName = $request-&gt;getModuleName();
        $moduleList = $bootstrap-&gt;modules;

        $moduleInitName = strtolower($activeModuleName).&quot;Init&quot;;
        $moduleInitNameLength = strlen($moduleInitName);

        if (isset($moduleList[$activeModuleName])) {
            $activeModule = $moduleList[$activeModuleName];

            $bootstrapMethodNames = get_class_methods($bootstrap);
            foreach ($bootstrapMethodNames as $key=&gt;$method) {
                $runMethod = false;
                $methodNameLength = strlen($method);
                if ($moduleInitNameLength &lt; $methodNameLength &amp;&amp;
                    $moduleInitName == substr($method, 0, $moduleInitNameLength)) {
                    call_user_func(array($bootstrap,$method));
                }
            }
        } else {
            $activeModule = $bootstrap;
        }

        $methodNames = get_class_methods($activeModule);
        foreach ($methodNames as $key=&gt;$method) {
            $runMethod = false;
            $methodNameLength = strlen($method);
            if (10 &lt; $methodNameLength &amp;&amp; &#039;activeInit&#039; === substr($method, 0, 10)) {
                $runMethod = true;
            } elseif ($moduleInitNameLength &lt; $methodNameLength &amp;&amp;
                    $moduleInitName == substr($method, 0, $moduleInitNameLength)) {
                $runMethod = true;
            }
            if ($runMethod) {
                call_user_func(array($activeModule,$method));
            }
        }
    }
}
</pre>
<p>Next we need to make sure the Controller Plugin is loaded.<br />
We can do this in one of two ways. Either in the Application Bootstrap via an _init function</p>
<pre class="brush: php">
public function _initControllerPlugins()
{
    $plugin = Zend_Controller_Front::getInstance()-&gt;registerPlugin(
        new BinaryKitten_ModuleConfig()
    );
}
</pre>
<p>*&#8211; Or &#8211;*</p>
<p>We can add a line to the application.ini</p>
<pre class="brush: text">
resources.frontController.plugins.BKModuleConfig = &quot;BinaryKitten_ModuleConfig&quot;
</pre>
<p>Finally some example init code from the module bootstrap.<br />
Please remember that the activeInit*() Functions need to be public for this to work properly</p>
<pre class="brush: php">
class Default_Bootstrap extends Zend_Application_Module_Bootstrap {
    public function activeInitMenus() {
        $layout = $this
                    -&gt;bootstrap(&#039;layout&#039;)
                    -&gt;getResource(&#039;layout&#039;);

        $view = $layout-&gt;getView();
        $config = new Zend_Config_Xml(APPLICATION_PATH.&#039;/configs/navigation_default.xml&#039;,&quot;menu&quot;);
        $navigation = new Zend_Navigation($config);
        $view-&gt;navigation($navigation);
    }
    public function activeInitDoSomethingElse() {
        /* some other code */
    }
    public function defaultInitSomething() {
    	/* more code */
    }
}
</pre>
<p>We can also add module inits to the application bootstrap like so:</p>
<pre class="brush: php">
    public function modulenameInitFunction() {
    	/* place code here */
    }
</pre>
<p>Where modulename is the lowercase version of the Modules name, eg if you have the Admin module, then you would use:</p>
<pre class="brush: php">
    public function adminInitFunction() {
    	/* place code here */
    }
</pre>
<p>Hopefully someone will find this code useful.</p>
<p>[ Edit January 5th 2010 ]<br />
Thanks to:<br />
	Matthew Weier O&#8217;Phinney for pointing out places for update.<br />
	Rob Allen (@Akrabat) for the info that the plugin could be loaded via the application.ini<br />
	Elizabeth Marie Smith and Matthew Turland for questioning the use of the Reflection.</p>
<ul>
<li>Updated the Bootstrap code to properly define the functions as Public</li>
<li>Removed the reflection as this wasn&#8217;t actually required any more.</li>
<li>Updated Post to make clean up the order of things and to properly designate that the BinaryKitten is the &#8220;Namespace&#8221;<br />
&#8220;Namespace&#8221; is used to reference that we&#8217;re not using PHP5.3 Namespaces, but the Namespaces within the Zend Framework.</li>
</ul>
<p>[ Edit January 11th 2010 ]<br />
Thanks to:<br />
	septem for pointing out a typo where i had $boostrap instead of $bootstrap<br />
	Gerard Roche for pointing out that by default, the default module doesn&#8217;t require a module bootstrap (in my code it has one)</p>
<ul>
<li>Updated to fix the typos</li>
<li>Added in a quick check to see if the module exists in the modules list of bootstraps</li>
<li>Removed the _ from the function name that it searches for, this should please the people who are adamant over the Zend Coding Standards</li>
<li>Added in the functionality to have $modulenameInit() functions as well in both active module bootstrap and the application bootstrap</li>
</ul>
<p>[ Edit February 13th 2010 ]<br />
Ran the code through codesniffer against the Zend Standard supplied.. updated so no errors found. 3 Warnings are left .. they are as follows:<br />
22 | WARNING | Line exceeds 80 characters; contains 83 characters<br />
35 | WARNING | Line exceeds 80 characters; contains 84 characters<br />
38 | WARNING | Line exceeds 80 characters; contains 83 characters<br />
Don&#8217;t think it&#8217;s worth the change for 3/4 characters </p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.me.uk/dev/zend-framework/177-active-module-based-config-with-zend-framework.html/feed</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>php: processing raw Post / Get Values</title>
		<link>http://binarykitten.me.uk/dev/php/95-php-processing-raw-post-get-values.html</link>
		<comments>http://binarykitten.me.uk/dev/php/95-php-processing-raw-post-get-values.html#comments</comments>
		<pubDate>Wed, 21 Jan 2009 09:03:53 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[head]]></category>
		<category><![CDATA[post]]></category>
		<category><![CDATA[raw post]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=95</guid>
		<description><![CDATA[I just added a jquery plugin to call head requests, but because you are not doing a get or post request php won&#8217;t convert the values that are sent back to their form. To get around this we have to use the php://input stream ( is that the right word?) which is handy for a [...]]]></description>
			<content:encoded><![CDATA[<p>I just added a <a href="http://binarykitten.jkrswebsolutions.co.uk/2009/01/21/jquery-plugin-ajax-head-request/">jquery plugin</a> to call head requests, but because you are not doing a get or post request php won&#8217;t convert the values that are sent back to their form.<br />
To get around this we have to use the php://input stream ( is that the right word?) which is handy for a lot of things.</p>
<pre class="brush: php">
$data = file_get_contents(&quot;php://input&quot;);
$lines = explode(&quot;&amp;&quot;,$data);
foreach($lines as $line) {
    list($key,$value) = explode(&quot;=&quot;,$line,2);
    $_REQUEST[$key] = $value;
}
</pre>
<p>basically this grabs the data from the php://input and then splits it up into it&#8217;s component parts and then stores that within the $_REQUEST superglobal array.<br />
Why $_REQUEST  well we&#8217;re calling a head request and not a post or get request, so where else should it go.</p>
<p>The php://input is handy when doing stuff like xmlRPC or jsonRPC etc .. </p>
<p>Hopefully this will benefit someone out there</p>
<p>&lt;edit&gt;<br />
Thanks to Mortal of #php on OFTC (irc) for pointing out the unlimited explode </p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.me.uk/dev/php/95-php-processing-raw-post-get-values.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Buzzword of the Day: Hijax</title>
		<link>http://binarykitten.me.uk/dev/85-buzzword-of-the-day-hijax.html</link>
		<comments>http://binarykitten.me.uk/dev/85-buzzword-of-the-day-hijax.html#comments</comments>
		<pubDate>Wed, 14 Jan 2009 02:03:27 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Buzzword of the day]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[buzzword]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=85</guid>
		<description><![CDATA[Came across this today: being 01:58 on Wednesday January the 14th 2009 .. i declare Today&#8217;s Buzzword of the day: HIJAX Basically when you replace links in a page to perform ajax instead and update content instead of refreshing, that&#8217;s Hijax. Visit http://domscripting.com/blog/display/41 for more info]]></description>
			<content:encoded><![CDATA[<p>Came across this today: being 01:58 on Wednesday January the 14th 2009 .. i declare Today&#8217;s Buzzword of the day:</p>
<h1 align="center">HIJAX</h1>
<p>Basically when you replace links in a page to perform ajax instead and update content instead of refreshing, that&#8217;s Hijax.</p>
<p>Visit <a href="http://domscripting.com/blog/display/41" target="_blank">http://domscripting.com/blog/display/41</a> for more info</p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.me.uk/dev/85-buzzword-of-the-day-hijax.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php: smarty assign content plugin</title>
		<link>http://binarykitten.me.uk/dev/php/smarty-templates/83-php-smarty-assign-content-plugin.html</link>
		<comments>http://binarykitten.me.uk/dev/php/smarty-templates/83-php-smarty-assign-content-plugin.html#comments</comments>
		<pubDate>Mon, 12 Jan 2009 19:27:18 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Smarty]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=83</guid>
		<description><![CDATA[I needed to assign a block of content to a variable instead of just a value.. so i set about creating this plugin function smarty_block_assign_content($params, $content, &#38;$smarty) { $smarty = clone($smarty); //Copy the original class, so there&#039;s no garbage variables after we finish if(!isset($content)) return; if (!isset($params[&#039;var&#039;])) { $smarty-&#62;trigger_error(&#34;assign_content: missing &#039;var&#039; parameter&#34;, E_USER_WARNING); return; } [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to assign a block of content to a variable instead of just a value.. so i set about creating this plugin</p>
<pre class="brush: php">
function smarty_block_assign_content($params, $content, &amp;$smarty)
{
        $smarty = clone($smarty); //Copy the original class, so there&#039;s no garbage variables after we finish
        if(!isset($content))
                return;
        if (!isset($params[&#039;var&#039;])) {
                $smarty-&gt;trigger_error(&quot;assign_content: missing &#039;var&#039; parameter&quot;, E_USER_WARNING);
                return;
        }

        //Compile content to ensure all smarty tags get processed
        $smarty-&gt;assign($params[&#039;var&#039;], $content);
                $smarty-&gt;assign($k, $v);
        return ;
}
</pre>
<p>example of usage: </p>
<pre class="brush: html">
{assign_content var=&#039;menu&#039;}
  {foreach from=$menus.cats-&gt;children item=&#039;cat&#039;}
  &lt;div class=&quot;item{if $cat-&gt;active || $cat-&gt;expanded}_select{/if}&quot;&gt;
    &lt;div class=&quot;arrow&quot;&gt;&lt;/div&gt;
    {if !$cat-&gt;active}&lt;a href=&quot;{$cat-&gt;link}&quot;&gt;{/if}{$cat-&gt;text}{if !$cat-&gt;active}&lt;/a&gt;
    {/if}
  &lt;/div&gt;
  {if $cat-&gt;children|<a href="http://twitter.com/count">@count</a> ne 0 || $cat-&gt;expanded}
    {foreach from=$cat-&gt;children item=&#039;subCat&#039;}
      &lt;div class=&quot;subitem{if $subCat-&gt;active}_select{/if}&quot;&gt;
        &lt;div class=&quot;arrow&quot;&gt;&lt;/div&gt;
      {if !$subCat-&gt;active}&lt;a href=&quot;{$subCat-&gt;link}&quot;&gt;{/if}{$subCat-&gt;text}{if !$subCat-&gt;active}&lt;/a&gt;{/if}
      &lt;/div&gt;
    {/foreach}
  {/if}
  {/foreach}
{/assign_content}
</pre>
<p>This will set the code we used to generate the menu and assign it to $menu for later use.<br />
Hope you find as usefull</p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.me.uk/dev/php/smarty-templates/83-php-smarty-assign-content-plugin.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>php: Menu Classes</title>
		<link>http://binarykitten.me.uk/dev/php/74-php-menu-classes.html</link>
		<comments>http://binarykitten.me.uk/dev/php/74-php-menu-classes.html#comments</comments>
		<pubDate>Mon, 12 Jan 2009 18:52:10 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=74</guid>
		<description><![CDATA[Recently I&#8217;ve been working on my own framework (we&#8217;ve all done it i&#8217;m sure) and part of this was a menu handling system. The basis really is simple, we have a menu item which really is just link and text. That menu item could have children, could be active and/or be selected. In this case [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been working on my own framework (we&#8217;ve all done it i&#8217;m sure) and part of this was a menu handling system.</p>
<p>The basis really is simple, we have a menu item which really is just link and text. That menu item could have children, could be active and/or be selected.</p>
<p>In this case Selected is the currently menu item, which is also Active, and all it&#8217;s parents in the chain upwards are also active.</p>
<p>We also have a menu controller which makes it easier to add to the menus.</p>
<pre class="brush: php">
class menuHandler {
    public $menus=array();
    public function NewMenu() {
        $k = time();
        $this-&gt;menus[$k] = new menu_class();
        return $k;
    }
    public function AddMenu($key,menu_class $menu) {
        if (!isset($this-&gt;menus[$key])) {
            $this-&gt;menus[$key] = $menu;
        }
        return $this;
    }
    public function ItemCount($menuKey) {
        $menu = $this-&gt;menus[$menuKey];
        if (!($menu instanceof menu_class)) {
            return -1;
        }
        else {
            return count($menu-&gt;children);
        }
    }
    public function AddItem($menuKey,$itemKey=null,menu_class $item) {
        if (isset($this-&gt;menus[$menuKey])) {
            $menu = $this-&gt;menus[$menuKey];
            if (!isset($itemKey) or (is_numeric($itemKey) &amp;amp;amp;amp;&amp;amp;amp;amp; $itemKey &lt;0)) {
                $itemKey = count($menu-&gt;children);
            }
            $menu-&gt;AddSubItem($item,$itemKey,$menuKey);
        }
        return $this;
    }
    public function SetActive($id,$from=null) {
        $menu = new menu_class();
        if ($from == null) {
            $from = $this-&gt;menus;
        }
        elseif($from instanceof menu_class) {
            $from = $from-&gt;children;
        }
        foreach ($from as $key=&gt;&amp;amp;amp;amp;$menu) {
            if ($key==$id) {
                $menu-&gt;active=true;
                return true;
            }
            elseif(count($menu-&gt;children) !=0) {
                $r = $this-&gt;SetActive($id,$menu-&gt;children);
                if ($r) {
                    $menu-&gt;expanded = true;
                    return $r;
                }
            }
        }
    }
}
class menu_class {
    public $children = array();
    public $myParent=null;
    public $active = false;
    public $expanded = false;
    public $link =&#039;&#039;;
    public $text = &#039;&#039;;
    public $additional = array();

    public function AddSubItem($item,$ID=null,$parentID=null) {
        if ($item instanceof menu_class) {
        }
        elseif(is_array($item)) {
            $item = new menu_class();
            $item-&gt;text = $details[0];
            $item-&gt;link = $details[1];
        }
        if (isset($ID)) {
            if (isset($this-&gt;children[$ID])) {
                if (is_numeric($ID)) {
                    $this-&gt;children = array_insert($this-&gt;children,$item,$ID);
                }
            }
            else {
                $this-&gt;children[$ID] = $item;
            }
        }
        else {
            $this-&gt;children[] = $item;
        }
        if (!isset($parentID)) {
            $parentID = &quot;main&quot;;
        }
        $this-&gt;myParent = $parentID;
        ksort($this-&gt;children);
    }

    public function __construct($text=null,$link=null) {
        if(isset($text) &amp;amp;amp;amp;&amp;amp;amp;amp; !is_null($text)) {
            $this-&gt;text = $text;
        }
        if(isset($link) &amp;amp;amp;amp;&amp;amp;amp;amp; !is_null($link)) {
            $this-&gt;link = $link;
        }
    } 

    public function Format($text) {
        if (!empty($this-&gt;link)) {
            $text= str_replace(&quot;%href%&quot;,$this-&gt;link);
        }
        if (!empty($this-&gt;text)) {
            $text = str_replace(&quot;%text%&quot;,$this-&gt;text);
        }
        return $text;
    }
}
</pre>
<p>You will notice that i&#8217;ve used the <a href="http://binarykitten.jkrswebsolutions.co.uk/2009/01/11/php-insert-element-and-shift/">array_insert function</a> from my previous posting. This was why it was created, so that i could insert without worry that i would overwrite the code</p>
<p>here&#8217;s an example of using the code</p>
<pre class="brush: php">
$menus = new menuHandler();
$menus-&gt;AddMenu(&quot;main&quot;,new menu_class());
 $menus-&gt;AddItem(&quot;main&quot;,-1,new menu_class(&quot;TEXT&quot;,&quot;link.php&quot;));
 $menus-&gt;AddItem(&quot;main&quot;,-1,new menu_class(&quot;TEXT2&quot;,&quot;link2.php&quot;));
//we want this item to be 1st!
 $menus-&gt;AddItem(&quot;main&quot;,0,new menu_class(&quot;1st Link&quot;,&quot;homelink.php&quot;));
</pre>
<p>So far I pass the $menus out to smarty in the usual assignment method and process like as follows</p>
<pre class="brush: html">
 {foreach from=$menus.main-&gt;children item=&quot;mItem&quot;}
  &lt;div class=&quot;mainbutton&quot;&gt;&lt;a href=&quot;{$mItem-&gt;link}&quot;&gt;{$mItem-&gt;text}&lt;/a&gt;&lt;/div&gt;
 {/foreach}
</pre>
<p>For a cascaded menu i&#8217;ve used the following </p>
<pre class="brush: html">
{foreach from=$menus.cats-&gt;children item=&#039;cat&#039;}
  &lt;div class=&quot;item{if $cat-&gt;active || $cat-&gt;expanded}_select{/if}&quot;&gt;
    &lt;div class=&quot;arrow&quot;&gt;&lt;/div&gt;
    {if !$cat-&gt;active}&lt;a href=&quot;{$cat-&gt;link}&quot;&gt;{/if}{$cat-&gt;text}{if !$cat-&gt;active}&lt;/a&gt;
    {/if}
  &lt;/div&gt;
  {if $cat-&gt;children|<a href="http://twitter.com/count">@count</a> ne 0 || $cat-&gt;expanded}
    {foreach from=$cat-&gt;children item=&#039;subCat&#039;}
      &lt;div class=&quot;subitem{if $subCat-&gt;active}_select{/if}&quot;&gt;
        &lt;div class=&quot;arrow&quot;&gt;&lt;/div&gt;
      {if !$subCat-&gt;active}&lt;a href=&quot;{$subCat-&gt;link}&quot;&gt;{/if}{$subCat-&gt;text}{if !$subCat-&gt;active}&lt;/a&gt;{/if}
      &lt;/div&gt;
    {/foreach}
  {/if}
  {/foreach}
</pre>
<p>As usual any comments gratefully recieved</p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.me.uk/dev/php/74-php-menu-classes.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php: Insert element at specific Index of Array.</title>
		<link>http://binarykitten.me.uk/dev/php/52-php-insert-element-and-shift.html</link>
		<comments>http://binarykitten.me.uk/dev/php/52-php-insert-element-and-shift.html#comments</comments>
		<pubDate>Sun, 11 Jan 2009 13:41:43 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[insert]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=52</guid>
		<description><![CDATA[In a recent project I&#8217;ve been undertaking I&#8217;ve made a menu class set and sometimes i just wanted to say to add this to an array, but in a certain position. PHP does this easily via $array[position] = &#34;newdata&#34;; But unfortunately this is not so good if you only wanted to insert the newdata into [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent project I&#8217;ve been undertaking I&#8217;ve made a menu class set and sometimes i just wanted to say to add this to an array, but in a certain position. PHP does this easily via</p>
<pre class="brush: php">
$array[position] = &quot;newdata&quot;;
</pre>
<p>But unfortunately this is not so good if you only wanted to insert the newdata into that position and move the others the out of the way. So with a little help from <a title="Derick's Blog" href="http://derickrethans.nl/" target="_blank">Derick Rethans</a> and <a title="Tetraboy's Blog" href="http://www.tetraboy.com" target="_blank">Tetraboy</a> I came up with this little solution.</p>
<p>Any comments gratefully recieved.</p>
<pre>
<pre class="brush: php">
function array_insert(&amp;$array,$element,$position=null) {
  if (count($array) == 0) {
    $array[] = $element;
  }
  elseif (is_numeric($position) &amp;&amp; $position &lt; 0) {
    if((count($array)+position) &lt; 0) {
      $array = array_insert($array,$element,0);
    }
    else {
      $array[count($array)+$position] = $element;
    }
  }
  elseif (is_numeric($position) &amp;&amp; isset($array[$position])) {
    $part1 = array_slice($array,0,$position,true);
    $part2 = array_slice($array,$position,null,true);
    $array = array_merge($part1,array($position=&gt;$element),$part2);
    foreach($array as $key=&gt;$item) {
      if (is_null($item)) {
        unset($array[$key]);
      }
    }
  }
  elseif (is_null($position)) {
    $array[] = $element;
  }
  elseif (!isset($array[$position])) {
    $array[$position] = $element;
  }
  $array = array_merge($array);
  return $array;
}
</pre>
</pre>
<p>and no code would be without it&#8217;s example:</p>
<pre class="brush: php">
// create the array
$x = array(&quot;apples&quot;,&quot;bananas&quot;,&quot;pears&quot;);
//insert &quot;oranges&quot; at position 1
array_insert($x,&quot;oranges&quot;,1);
var_dump($x);
//insert &quot;pineapples&quot; 2 from the end
array_insert($x,&quot;pineapples&quot;,-2);
var_dump($x);
//insert &quot;strawberries&quot; at the end
array_insert($x,&quot;strawberries&quot;);
var_dump($x);
//insert &quot;plums&quot; at position 0 - (because the negative position goes beyond 0)
array_insert($x,&quot;pineapples&quot;,-10);
var_dump($x);
</pre>
<p>or alternatively</p>
<pre class="brush: php">
// create the array
$x = array(&quot;apples&quot;,&quot;bananas&quot;,&quot;pears&quot;);
//insert &quot;oranges&quot; at position 1
$x = array_insert($x,&quot;oranges&quot;,1);
var_dump($x);
//insert &quot;pineapples&quot; 2 from the end
$x = array_insert($x,&quot;pineapples&quot;,-2);
var_dump($x);
//insert &quot;strawberries&quot; at the end
$x = array_insert($x,&quot;strawberries&quot;);
var_dump($x);
//insert &quot;plums&quot; at position 0 - (because the negative position goes beyond 0)
$x = array_insert($x,&quot;plums&quot;,-10);
var_dump($x);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.me.uk/dev/php/52-php-insert-element-and-shift.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Long Night of Editing</title>
		<link>http://binarykitten.me.uk/personal/26-long-night-of-editing.html</link>
		<comments>http://binarykitten.me.uk/personal/26-long-night-of-editing.html#comments</comments>
		<pubDate>Wed, 07 Jan 2009 03:10:59 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[client]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[websites]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=26</guid>
		<description><![CDATA[Recently it seems that i&#8217;ve been suffering with a distinct lack of enthusiasm for any sort of coding. I&#8217;ve been trundling through the latest site (http://www.gamers.uk.net) which is been working well for the most part.. Over the last few days I&#8217;ve not really wanted to do any sort of work on it at all or [...]]]></description>
			<content:encoded><![CDATA[<p>Recently it seems that i&#8217;ve been suffering with a distinct lack of enthusiasm for any sort of coding.</p>
<p>I&#8217;ve been trundling through the latest site (<a title="Gamers" href="http://www.gamers.uk.net" target="_blank">http://www.gamers.uk.net</a>) which is been working well for the most part..</p>
<p>Over the last few days I&#8217;ve not really wanted to do any sort of work on it at all or anything else really. Friday is the deadline and so the next few nights I&#8217;m going to have to put in some late nights to get it all complete.</p>
<p>Things that are bugging me:</p>
<ol>
<li>Why couldn&#8217;t the client tell me about the postage when I asked them before?</li>
<li>Why is it so difficult to get the energy to work when you work from home?</li>
</ol>
<p>Oh well, suppose I best put myself back to work</p>
<p>BK</p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.me.uk/personal/26-long-night-of-editing.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery image Preloader</title>
		<link>http://binarykitten.me.uk/dev/jq-plugins/11-jquery-image-preloader.html</link>
		<comments>http://binarykitten.me.uk/dev/jq-plugins/11-jquery-image-preloader.html#comments</comments>
		<pubDate>Tue, 06 Jan 2009 16:20:16 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[jQuery Plugins]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=11</guid>
		<description><![CDATA[&#8211; New version with Callbacks now available &#8211; Recently I looked into preloading images via Javascript. I found all the usual ones which require you have a list of all the images you wish to preload. For me though, specifying the images is rather annoying. Also I much prefer CSS roll overs to Javascript ones [...]]]></description>
			<content:encoded><![CDATA[<h2><a href="http://binarykitten.me.uk/dev/jq-plugins/107-jquery-image-preloader-plus-callbacks.html">&#8211; New version with Callbacks now available &#8211;</a></h2>
<p>Recently I looked into preloading images via Javascript. I found all the usual ones which require you have a list of all the images you wish to preload. For me though, specifying the images is rather annoying. Also I much prefer CSS roll overs to Javascript ones so I created this to make it easier.<span id="more-11"></span></p>
<p>This &#8220;plugin&#8221; for jQuery just allows you to preload All the images based in url enclosures within the css files attached to the current page.<br />
I call it a &#8220;plugin&#8221; rather than a plugin as technically it doesn&#8217;t actually need jQuery at all so just remove the &#8220;jQuery.&#8221;  on the function</p>
<pre class="brush: javascript">
/* jQuery.preloader - v0.9 - K Reeve aka BinaryKitten
*
* v0.9
* 	Fixed .toString being .toSteing
*
* v0.8
*		Fixed sheet.href being null error (was causing issues in FF3RC1)
*
* v0.7
*		Remade the preLoadImages from jQuery to DOM
*
* v0.6
* 		Fixed IE6 Compatability!
*		Moved from jQuery to DOM
*
* v0.5
* 		Shifted the additionalimages loader in the preLoadAllImages so it wasn&#039;t called multiple times
* 		Created secondary .preLoadImages to handle additionalimages and so it can be called on itself
*/
jQuery.preLoadImages = function(imagesList) {
	var pic = new Array();
	if (typeof imageList != &#039;undefined&#039;) {
		if (imageList.constructor.toString().indexOf(&#039;Array&#039;) != -1) {
			var l = imageList.length;
			for(apicsIdx=0;apicsIdx&lt;l;apicsIdx++) {
				var c = pic.length;
				pic[c] = new Image();
				pic[c].src = imageList[apicsIdx];
			};
		}
		else {
			var c = pic.length;
			pic[c] = new Image();
			pic[c].src = imageList;
		}
	}
	pic = undefined;
}
jQuery.preLoadAllImages =  function(additionalimages) {
	var regex = new RegExp(&quot;url\((.*)\) &quot;,&#039;i&#039;);
	var pic = new Array();
	var l = document.styleSheets.length;
	for (sheetIdx=0;sheetIdx &lt; l;sheetIdx++){
	var sheet = document.styleSheets[sheetIdx];
	if (typeof sheet.href == &#039;string&#039; &amp;amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp;amp; sheet.href.length &gt; 0) {
		var spl = sheet.href.split(&#039;/&#039;);
		var filename = spl.pop();
		var path = spl.join(&#039;/&#039;)+&#039;/&#039;;
	}
	else {
		var path = &#039;./&#039;;
	}
	myRules = sheet.cssRules ? sheet.cssRules : sheet.rules;
	for (ruleIdx=0;ruleIdx&lt;myRules.length;ruleIdx++) {
		var Rule = myRules[ruleIdx];
		txt = Rule.cssText ? Rule.cssText : Rule.style.cssText
		var match = regex.exec(Rule.cssText);
		if (match != null) {
			if ( match[1].substring(0,1) == &#039;/&#039;) {
				var pic2Load = match[1];
			}
		else {
			var pic2Load = path+match[1];
		}
		if (typeof console != &#039;undefined&#039;)
			if (typeof console.log != &#039;undefined&#039;)
				console.log(pic2Load);
				var c = pic.length;
				pic[c] = new Image();
				pic[c].src = pic2Load;
			}
		};
		sheet = undefined;
		match = undefined;
		styleList = undefined;
		Rule = undefined;
	}
	if (typeof additionalimages != &#039;undefined&#039;) {
		if (additionalimages.constructor.toString().indexOf(&#039;Array&#039;) != -1) {
			var l = additionalimages.length;
			for(apicsIdx=0;apicsIdx&lt;l;apicsIdx++) {
				var c = pic.length;
				pic[c] = new Image();
				pic[c].src = additionalimages[apicsIdx];
			};
		}
		else {
			var c = pic.length;
			pic[c] = new Image();
			pic[c].src = additionalimages;
		}
	}
	regex = undefined;
	pic = undefined;
};
</pre>
<p>Thanks to <strong>Roberto</strong> for pointing this out, I forgot to add how to use it and what it does..</p>
<p><strong>What it does:</strong><br />
Basically the $.preLoadAllImages() function cycles through all your style sheets looking for background images and creates an array that will handle them. After this is done, it cycles through this array and creates a cached image for each file.<br />
If you passthrough an array or filname to the function it will preload those/that image(s) as well.</p>
<p>Finally the $.preLoadImages just preloads images passed to it.. be it a single item or an array of items.</p>
<p><strong>Usage</strong><br />
This really is the simple part.</p>
<ol>
<li>copy the code above to a new file.. save it as jquery.Preloader.js</li>
<li>make sure you know the relative filepath to this new js file</li>
<li>add jquery to the list of included files by doing <script type="text/javascript" src="/file/path/jquery.js"></script></li>
<li>add the new js file to the page: <script type="text/javascript" src="/file/path/jquery.Preloader.js"></script></li>
<li>Finally call the function you want.. you don&#8217;t have to use jquery&#8217;s document ready, as really you might want to have the images ready before hand.. but that&#8217;s down to you.. just call $.preLoadAllImages();  and Voila!</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.me.uk/dev/jq-plugins/11-jquery-image-preloader.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
