Author: pmacadden
Date: 2010-09-19 00:08:24 +0200 (Sun, 19 Sep 2010)
New Revision: 30925

Added:
   plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenu.class.php
   plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenuComponent.class.php
   plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenuItem.class.php
   plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenuSeparator.class.php
Removed:
   plugins/pmJSCookMenuPlugin/trunk/lib/helper/
Log:
plugin refactored.

Added: plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenu.class.php
===================================================================
--- plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenu.class.php                 
        (rev 0)
+++ plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenu.class.php 2010-09-18 
22:08:24 UTC (rev 30925)
@@ -0,0 +1,359 @@
+<?php
+
+/**
+ * Represents the Composite in the Composite pattern.
+ * Represents a menu and all it's children.
+ *
+ * @author Patricio Mac Adden <[email protected]>
+ */
+class pmJSCookMenu extends pmJSCookMenuComponent
+{
+  /**
+   * The composite children.
+   * @var array
+   */
+  protected $children;
+  
+  /**
+   * Indicates if the composite is a root.
+   * @var boolean
+   */
+  protected $is_root;
+  
+  /**
+   * Indicates the composite's orientation (just needed by root).
+   * @var string
+   */
+  protected $orientation;
+  
+  /**
+   * Indicates the composite's theme (just needed by root).
+   * @var string
+   */
+  protected $theme;
+    
+  /**
+   * Constructor.
+   */
+  public function __construct()
+  {
+    parent::__construct();
+    
+    $this->children = array();
+    $this->is_root = false;
+    $this->orientation = null;
+    $this->theme = null;
+  }
+  
+  /**
+        * Create an instance of pmJSCookMenu from a yaml file.
+        * 
+        * @param string $yaml_file The yaml file path
+        * @return pmJSCookMenu
+        */
+  public static function createFromYaml($yaml_file)
+  {
+    $yaml = sfYaml::load($yaml_file);
+    $yaml = array_pop($yaml);
+    
+    $root = new pmJSCookMenu();
+    
+    $root_attrs = array(
+      "credentials",
+      "description",
+      "icon",
+      "orientation",
+      "target",
+      "theme",
+      "title",
+      "url"
+    );
+    
+    foreach ($root_attrs as $attr)
+    {
+      if (isset($yaml[$attr]))
+      {
+        $method = "set".ucfirst($attr);
+        call_user_func(array($root, $method), $yaml[$attr]);
+        unset($yaml[$attr]);
+      }
+    }
+    
+    if (isset($yaml["root"]) && $yaml["root"] == true)
+    {
+      $root->setRoot();
+      unset($yaml["root"]);
+    }
+    
+    $separator_count = 0;
+    foreach ($yaml as $name => $arr_menu)
+    {
+      if ($name == "separator")
+      {
+        $item = new pmJSCookMenuSeparator();
+        $root->addChild("$name$separator_count", $item);
+        $separator_count++;
+      }
+      else
+      {
+        $item = self::createMenu($arr_menu);
+        $root->addChild($name, $item);
+      }
+    }
+    
+    return $root;
+  }
+  
+  /**
+        * Create a submenu from an array.
+        * 
+        * @param array $arr The array
+        * @return pmJSCookMenuComponent
+        */
+  protected static function createMenu($arr)
+  {
+    $item = null;
+    if (array_key_exists("menu", $arr))
+    {
+      $item = new pmJSCookMenu();
+      $separator_count = 0;
+      foreach ($arr["menu"] as $name => $submenu)
+      {
+        if ($name == "separator")
+        {
+          $sitem = new pmJSCookMenuSeparator();
+          $item->addChild("$name$separator_count", $sitem);
+          $separator_count++;
+        }
+        else
+        {
+          $sitem = self::createMenu($submenu);
+          $item->addChild($name, $sitem);
+        }
+      }
+    }
+    else
+    {
+      $item = new pmJSCookMenuItem();
+    }
+    if (array_key_exists("credentials", $arr)) 
$item->setCredentials($arr["credentials"]);
+    if (array_key_exists("description", $arr)) 
$item->setDescription($arr["description"]);
+    if (array_key_exists("icon", $arr)) $item->setIcon($arr["icon"]);
+    if (array_key_exists("target", $arr)) $item->setTarget($arr["target"]);
+    if (array_key_exists("title", $arr)) $item->setTitle($arr["title"]);
+    if (array_key_exists("url", $arr)) $item->setUrl($arr["url"]);
+    
+    return $item;
+  }
+  
+  /**
+        * Get the children attribute value.
+        * 
+        * @return array
+        */
+  public function getChildren()
+  {
+    return $this->children;
+  }
+  
+  /**
+        * Get a child.
+        *
+        * @param string $name The child name
+        * @return mixed
+        */
+  public function getChild($name)
+  {
+    $child = null;
+    
+    if (isset($this->children[$name]))
+    {
+      $child = $this->children[$name];
+    }
+    
+    return $child;
+  }
+
+  /**
+        * Adds a child.
+        *
+        * @param string $name The child name
+        * @param pmJSCookMenuComponent $component The child
+        * @return pmJSCookMenu The current object (for fluent API support)
+        */
+  public function addChild($name, pmJSCookMenuComponent $component)
+  {
+    $this->children[$name] = $component;
+    
+    return $this;
+  }
+  
+  /**
+        * Removes a child.
+        *
+        * @param string $name The child name
+        * @return pmJSCookMenu The current object (for fluent API support)
+        */
+  public function removeChild($name)
+  {
+    unset($this->children[$name]);
+    
+    return $this;
+  }
+
+  /**
+        * Set the current object as the root.
+        * 
+        * @return pmJSCookMenu The current object (for fluent API support)
+        */
+  public function setRoot()
+  {
+    $this->is_root = true;
+    
+    return $this;
+  }
+  
+  /**
+        * Unset the current object as the root.
+        * 
+        * @return pmJSCookMenu (for fluent API support)
+        */
+  public function unsetRoot()
+  {
+    $this->is_root = false;
+    
+    return $this;
+  }
+  
+  /**
+        * Returns if the current object is the root.
+        * 
+        * @return boolean
+        */
+  public function isRoot()
+  {
+    return $this->is_root;
+  }
+  
+  /**
+        * Set the value of orientation attribute.
+        * 
+        * @param string $v The new orientation
+        * @return pmJSCookMenu The current object (for fluent API support)
+        */
+  public function setOrientation($v)
+  {
+    $this->orientation = $v;
+    
+    return $this;
+  }
+  
+  /**
+        * Get the orientation attribute value.
+        * 
+        * @return string
+        */
+  public function getOrientation()
+  {
+    return $this->orientation;
+  }
+  
+  /**
+        * Set the value of theme attribute.
+        * 
+        * @param string $v The new theme
+        * @return pmJSCookMenu The current object (for fluent API support)
+        */
+  public function setTheme($v)
+  {
+    $this->theme = $v;
+    
+    return $this;
+  }
+  
+  /**
+        * Get the theme attribute value.
+        * 
+        * @return string
+        */
+  public function getTheme()
+  {
+    return $this->theme;
+  }
+  
+  /**
+   * Renders the pmJSCookMenu.
+   *
+   * @return string
+   */
+  public function render()
+  {
+    $context = sfContext::getInstance();
+    $user = $context->getUser();
+    
+    $context->getConfiguration()->loadHelpers(array("I18N", "Url"));
+    
+    $has_credentials = $user->hasCredential($this->getCredentials());
+    
+    $js = "";
+    
+    if ($has_credentials)
+    {
+      $orientation = $this->getOrientation();
+      $theme = $this->getTheme();
+    
+      if ($this->isRoot())
+      {
+        $request = $context->getRequest();
+        $uri_prefix = $request->getUriPrefix();
+        $relative_url_root = $request->getRelativeUrlRoot();
+       
+        $js .= <<<EOF
+<div id="jscookmenu"></div>
+<script>
+var theme = "$theme";
+var my${theme}Base = 
"$uri_prefix$relative_url_root/pmJSCookMenuPlugin/images/$theme/";
+var cmBase = my${theme}Base;
+
+var jscookmenu = 
+[
+EOF;
+      }
+      else
+      {
+        $description = __($this->getDescription());
+        $icon = $this->getIcon() ? image_tag($this->getIcon()): "null";
+        if ($icon != "null") $icon = "'$icon'";
+        $target = $this->getTarget();
+        $title = __($this->getTitle());
+        $url = $this->getUrl() ? url_for($this->getUrl()) : "null";
+        if ($url != "null") $url = "'$url'";
+    
+        $js .= "[$icon, '$title', $url, '$target', '$description',";
+      }
+    
+      foreach ($this->getChildren() as $child)
+      {
+        $js .= $child->render().",";
+      }
+    
+      $js = substr($js, 0, -1);
+
+      if ($this->isRoot())
+      {
+        $js .= <<<EOF
+];
+
+cmDraw("jscookmenu", jscookmenu, "$orientation", cm$theme);
+</script>
+EOF;
+      }
+      else
+      {
+        $js .= "]";
+      }
+    }
+    
+    return $js;
+  }
+}
\ No newline at end of file

Added: plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenuComponent.class.php
===================================================================
--- plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenuComponent.class.php        
                        (rev 0)
+++ plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenuComponent.class.php        
2010-09-18 22:08:24 UTC (rev 30925)
@@ -0,0 +1,205 @@
+<?php
+
+/**
+ * Abstract class that represents the Component in the Composite pattern.
+ * Provides the basic functionality of Composite and Leaf objects.
+ *
+ * @author Patricio Mac Adden <[email protected]>
+ */
+abstract class pmJSCookMenuComponent
+{
+  /**
+   * The credentials.
+   * Just authenticated users that has these credentials should
+   * be able to use this component.
+   * @var array
+   */
+  protected $credentials;
+  
+  /**
+   * The component description.
+   * @var string
+   */
+  protected $description;
+  
+  /**
+   * The component icon.
+   * @var string
+   */
+  protected $icon;
+  
+  /**
+   * The component title.
+   * @var string
+   */
+  protected $title;
+  
+  /**
+   * The component target (_blank, _self, etc.).
+   * @var string
+   */
+  protected $target;
+  
+  /**
+   * The component link.
+   * @var string
+   */
+  protected $url;
+  
+  /**
+   * Constructor.
+   */
+  public function __construct()
+  {
+    $this->credentials = array();
+    $this->icon = "";
+    $this->title = "";
+    $this->target = "_self";
+    $this->url = "";
+  }
+  
+  /**
+   * Renders the component.
+   *
+   * @return string
+   */
+  abstract public function render();
+  
+  /**
+        * Set the value of credentials attribute.
+        * 
+        * @param array $v The credentials array
+        * @return pmJSCookMenuComponent The current object (for fluent API 
support)
+        */
+  public function setCredentials($v)
+  {
+    $this->credentials = $v;
+    
+    return $this;
+  }
+
+  /**
+        * Get the credentials attribute value.
+        * 
+        * @return string
+        */
+  public function getCredentials()
+  {
+    return $this->credentials;
+  }
+  
+  /**
+        * Set the value of description attribute.
+        * 
+        * @param string $v The new description
+        * @return pmJSCookMenuComponent The current object (for fluent API 
support)
+        */
+  public function setDescription($v)
+  {
+    $this->description = $v;
+    
+    return $this;
+  }
+
+  /**
+        * Get the description attribute value.
+        * 
+        * @return string
+        */
+  public function getDescription()
+  {
+    return $this->description;
+  }
+  
+  /**
+        * Set the value of icon attribute.
+        * 
+        * @param string $v The new icon
+        * @return pmJSCookMenuComponent The current object (for fluent API 
support)
+        */
+  public function setIcon($v)
+  {
+    $this->icon = $v;
+    
+    return $this;
+  }
+  
+  /**
+        * Get the icon attribute value.
+        * 
+        * @return string
+        */
+  public function getIcon()
+  {
+    return $this->icon;
+  }
+  
+  /**
+        * Set the value of target attribute.
+        * 
+        * @param string $v The new target
+        * @return pmJSCookMenuComponent The current object (for fluent API 
support)
+        */
+  public function setTarget($v)
+  {
+    $this->target = $v;
+    
+    return $this;
+  }
+  
+  /**
+        * Get the target attribute value.
+        * 
+        * @return string
+        */
+  public function getTarget()
+  {
+    return $this->target;
+  }
+  
+  /**
+        * Set the value of title attribute.
+        * 
+        * @param string $v The new title
+        * @return pmJSCookMenuComponent The current object (for fluent API 
support)
+        */
+  public function setTitle($v)
+  {
+    $this->title = $v;
+    
+    return $this;
+  }
+
+  /**
+        * Get the title attribute value.
+        * 
+        * @return string
+        */
+  public function getTitle()
+  {
+    return $this->title;
+  }
+  
+  /**
+        * Set the value of url attribute.
+        * 
+        * @param string $v The new url
+        * @return pmJSCookMenuComponent The current object (for fluent API 
support)
+        */
+  public function setUrl($v)
+  {
+    $this->url = $v;
+    
+    return $this;
+  }
+  
+  /**
+        * Get the url attribute value.
+        * 
+        * @return string
+        */
+  public function getUrl()
+  {
+    return $this->url;
+  }
+}
\ No newline at end of file

Added: plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenuItem.class.php
===================================================================
--- plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenuItem.class.php             
                (rev 0)
+++ plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenuItem.class.php     
2010-09-18 22:08:24 UTC (rev 30925)
@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * Represents the Leaf in the Composite pattern.
+ * Represents a menu item.
+ *
+ * @author Patricio Mac Adden <[email protected]>
+ */
+class pmJSCookMenuItem extends pmJSCookMenuComponent
+{
+  /**
+   * Renders the pmJSCookMenuItem.
+   *
+   * @return string
+   */
+  public function render()
+  {
+    $context = sfContext::getInstance();
+    $user = $context->getUser();
+    
+    $context->getConfiguration()->loadHelpers(array("Asset", "I18N", "Url"));
+    
+    $has_credentials = $user->hasCredential($this->getCredentials());
+    
+    $item = "";
+    
+    if ($has_credentials)
+    {
+      $description = __($this->getDescription());
+      $icon = $this->getIcon() ? image_tag($this->getIcon()): "null";
+      if ($icon != "null") $icon = "'$icon'";
+      $target = $this->getTarget();
+      $title = __($this->getTitle());
+      $url = $this->getUrl() ? url_for($this->getUrl()) : "null";
+      if ($url != "null") $url = "'$url'";
+    
+      $item = "[$icon, '$title', $url, '$target', '$description']";
+    }
+    
+    return $item;
+  }
+}
\ No newline at end of file

Added: plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenuSeparator.class.php
===================================================================
--- plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenuSeparator.class.php        
                        (rev 0)
+++ plugins/pmJSCookMenuPlugin/trunk/lib/pmJSCookMenuSeparator.class.php        
2010-09-18 22:08:24 UTC (rev 30925)
@@ -0,0 +1,20 @@
+<?php
+
+/**
+ * Represents the Leaf in the Composite pattern.
+ * Represents a separator.
+ *
+ * @author Patricio Mac Adden <[email protected]>
+ */
+class pmJSCookMenuSeparator extends pmJSCookMenuComponent
+{
+  /**
+   * Renders the pmJSCookMenuItem.
+   *
+   * @return string
+   */
+  public function render()
+  {
+    return "_cmSplit";
+  }
+}
\ No newline at end of file

-- 
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