Public/Menu.php
authorAlan Knowles <alan@roojs.com>
Wed, 7 Mar 2018 05:41:05 +0000 (13:41 +0800)
committerAlan Knowles <alan@roojs.com>
Wed, 7 Mar 2018 05:41:05 +0000 (13:41 +0800)
Public/Menu.php [new file with mode: 0644]

diff --git a/Public/Menu.php b/Public/Menu.php
new file mode 100644 (file)
index 0000000..66c92a9
--- /dev/null
@@ -0,0 +1,149 @@
+<?php
+
+/**
+ * output the menu as a Javascript file...
+ *
+ */
+
+require_once 'Pman.php';
+
+class Pman_Cms_Public_Menu extends Pman {
+    
+    function getAuth()
+    {
+        return true; // everyone allowed in..
+    }
+    
+    /**
+     *
+     */
+    
+    function get($name = '',  $opts = array())
+    {
+       
+        $name = empty($name) ? 'main-menu' : $name;
+        
+        $this->loadMenu($name);
+        
+        $name = preg_replace('/[^A-Z0-9]+/i', '_', $name);
+        
+        
+        
+        
+        
+        $ret = "
+Roo.namespace('Pman.Cms.Menu');
+
+Pman.Cms.Menu.{$name}= new Roo.XComponent({
+
+  _strings : { },
+
+  part     :  ['Pman.Cms.Menu', '{$name}' ],
+  order    : '001-{$name}',
+  region   : 'center',
+  parent   : false,
+  name     : 'unnamed module',
+  disabled : false, 
+  permname : '', 
+  _tree : function()
+  {
+    var _this = this;
+    var MODULE = this;
+    return {
+       xtype : 'NavGroup',
+       xns : Roo.bootstrap,
+       '|xns' : 'Roo.bootstrap',
+       items  : [
+        "
+        .    $this->treeToJS() . "
+        
+          ]
+    };
+  }
+});
+";
+        header("Content-type: text/javascript");
+        
+        echo $ret;
+        exit;
+    }
+    function treeToJs($start = 0, $depth = 0)
+    {
+        
+        $ar = $start == 0 ?  $this->basePage->children : $this->menu[$start]->children;
+        $ret = '';
+        
+        foreach($ar as $do) {
+            
+            $xt = $depth == 0 ? 'NavItem' : 'MenuItem';
+            if (strlen($ret)) {
+                $ret.=",";
+            }
+            
+            $ret .="
+    {
+      xtype : '$xt',
+      html : ". json_encode($do->title) .",
+" . ( $depth > 1 ? "      type : 'submenu', " : "" ) . "
+      ".  (strlen($do->page_link) ?
+           " href : baseURL + " . json_encode($do->page_link) . ",
+      preventDefault : false, "  :
+           " preventDefault : true, " ) . "
+      xns : Roo.bootstrap,
+      '|xns' : 'Roo.bootstrap'";
+            
+            if (!empty($do->children)) {
+                $ret.=",
+      menu : {
+        xtype : 'Menu',
+        xns : Roo.bootstrap,
+        '|xns' : 'Roo.bootstrap',
+        items  : [        
+                " . $this->treeToJs($do->id,$depth+1) . "
+        ]
+      }";
+            } 
+           $ret .="
+    }";
+    
+        }
+            
+        return $ret;
+       
+        
+        
+        
+    }
+    
+    function loadMenu($name)
+    {
+        //DB_DataObject::debugLevel(1);
+        $m = DB_DataObject::Factory('cms_page');
+        $m->children = array();
+        
+        $m->page_type_id = DB_DataObject::factory('core_enum')->lookup('cms_page_type', 'menu');
+        $m->orderBy('seq_id ASC');
+        $ar = $m->fetchAll();
+        
+        
+        foreach($ar as $i=>$p) {
+            $p->children = array();
+            $this->menu[$p->id] = &$ar[$i];
+            if (!$p->parent_id && $p->page_link == $name) {
+                $this->basePage = &$ar[$i];
+            }
+            
+        }
+        foreach($ar as $i=>$p) {
+            $this->menu[$p->parent_id]->children[] = &$ar[$i];
+        }
+        
+       // print_r($this->menu);
+        
+    }
+    
+    
+}