Author: Jonathan.Wage
Date: 2010-01-27 21:09:39 +0100 (Wed, 27 Jan 2010)
New Revision: 27252

Modified:
   
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenu.class.php
   
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSite.class.php
   
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSiteManager.class.php
   plugins/sfSympalPlugin/trunk/lib/sfSympalContext.php
Log:
[1.4][sfSympalPlugin][1.0] Adding functionality to find the current menu item 
based on the request uri if it is not set


Modified: 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenu.class.php
===================================================================
--- 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenu.class.php
 2010-01-27 17:01:29 UTC (rev 27251)
+++ 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenu.class.php
 2010-01-27 20:09:39 UTC (rev 27252)
@@ -114,6 +114,11 @@
     return $this->_route;
   }
 
+  public function getUrl(array $options = array())
+  {
+    return url_for($this->getRoute(), $options);
+  }
+
   public function setRoute($route)
   {
     $this->_route = $route;

Modified: 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSite.class.php
===================================================================
--- 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSite.class.php
     2010-01-27 17:01:29 UTC (rev 27251)
+++ 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSite.class.php
     2010-01-27 20:09:39 UTC (rev 27252)
@@ -165,18 +165,6 @@
     $this->setLevel($this->_menuItem['level']);
   }
 
-  public function isCurrent($bool = null)
-  {
-    $currentMenuItem = sfSympalContext::getInstance()->getCurrentMenuItem();
-
-    if ($currentMenuItem && $currentMenuItem->exists())
-    {
-      return $this->_menuItem['id'] == $currentMenuItem['id'];
-    } else {
-      return false;
-    }
-  }
-
   public function getTopLevelParent()
   {
     $obj = $this;
@@ -214,6 +202,18 @@
     }
   }
 
+  public function isCurrent($bool = null)
+  {
+    $currentMenuItem = sfSympalContext::getInstance()->getCurrentMenuItem();
+
+    if ($currentMenuItem && $currentMenuItem->exists())
+    {
+      return $this->_menuItem['id'] == $currentMenuItem['id'];
+    } else {
+      return false;
+    }
+  }
+
   public function isCurrentAncestor()
   {
     $menuItem = sfSympalContext::getInstance()->getCurrentMenuItem();
@@ -225,4 +225,9 @@
 
     return false;
   }
+
+  public function getDoctrineMenuItem()
+  {
+    return 
Doctrine_Core::getTable('sfSympalMenuItem')->find($this->_menuItem['id']);
+  }
 }
\ No newline at end of file

Modified: 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSiteManager.class.php
===================================================================
--- 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSiteManager.class.php
      2010-01-27 17:01:29 UTC (rev 27251)
+++ 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalMenuPlugin/lib/menu/sfSympalMenuSiteManager.class.php
      2010-01-27 20:09:39 UTC (rev 27252)
@@ -17,6 +17,10 @@
     if ($cache = $this->_getCache())
     {
       $this->_rootSlugs = $cache->get('SYMPAL_MENU_ROOT_SLUGS');
+      if (!is_array($this->_rootSlugs))
+      {
+        $this->initialize();
+      }
     }
   }
 
@@ -30,11 +34,42 @@
     return self::$_instance;
   }
 
-  public function getHierarchies()
+  public function getMenus()
   {
-    $this->initialize();
+    $menus = array();
+    foreach ($this->_rootSlugs as $slug)
+    {
+      $menus[$slug] = $this->getMenu($slug);
+    }
+    return $menus;
+  }
 
-    return $this->_hierarchies;
+  public function findCurrentMenuItem($menu = null)
+  {
+    $currentUri = sfContext::getInstance()->getRequest()->getUri();
+
+    if (is_null($menu))
+    {
+      foreach ($this->getMenus() as $menu)
+      {
+        if ($found = $this->findCurrentMenuItem($menu))
+        {
+          return $found;
+        }
+      }
+    } else {
+      if ($menu->getUrl(array('absolute' => true)) === $currentUri)
+      {
+        return $menu->getDoctrineMenuItem();
+      }
+      foreach ($menu->getChildren() as $child)
+      {
+        if ($found = $this->findCurrentMenuItem($child))
+        {
+          return $found;
+        }
+      }
+    }
   }
 
   public function clear()

Modified: plugins/sfSympalPlugin/trunk/lib/sfSympalContext.php
===================================================================
--- plugins/sfSympalPlugin/trunk/lib/sfSympalContext.php        2010-01-27 
17:01:29 UTC (rev 27251)
+++ plugins/sfSympalPlugin/trunk/lib/sfSympalContext.php        2010-01-27 
20:09:39 UTC (rev 27252)
@@ -27,6 +27,11 @@
 
   public function getCurrentMenuItem()
   {
+    if (!$this->_currentMenuItem)
+    {
+      $this->_currentMenuItem = 
sfSympalMenuSiteManager::getInstance()->findCurrentMenuItem();
+    }
+
     return $this->_currentMenuItem;
   }
 

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to