<?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; classes</title>
	<atom:link href="http://binarykitten.me.uk/tag/classes/feed" rel="self" type="application/rss+xml" />
	<link>http://binarykitten.me.uk</link>
	<description></description>
	<lastBuildDate>Thu, 09 Sep 2010 10:33:55 +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>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>
	</channel>
</rss>
