1: <?php
2:
3: class BootstrapSideMenu extends \Sphp\tools\MenuGen{
4: public $brandicon = "";
5: public $navbarClasses = "nav flex-column mb-sm-auto mb-0 align-items-center align-items-sm-start";
6:
7: public $template_menu_startroot = '<li><hr class="text-primary" style="height: 4px; background-color: #FFFFFF;" /></li><li class="nav-item nav-dli"><a class="nav-link nav-dlink align-middle px-0" data-bs-toggle="collapse" data-bs-target="#$mnuhref2" href="#$mnuhref2" >' .
8: '<i class="$mnuicon"></i> <span class="ms-1 d-none d-sm-inline">$mnutitle</span></a>'
9: . '<div id="$mnuhref2" aria-expanded="true"><ul class="nav flex-column pl-2 ms-1">';
10: public $template_menu_endroot = '</ul></div></li>';
11: public $template_menu_start = '<li class="nav-item nav-dli"><a class="nav-link nav-dlink collapsed align-middle px-0" data-bs-toggle="collapse" data-bs-target="#$mnuhref2" href="#$mnuhref2" >' .
12: '<i class="$mnuicon"></i> <span class="d-none d-sm-inline">$mnutitle</span></a>'
13: . '<div class="collapse" id="$mnuhref2" aria-expanded="false"><ul class="flex-column pl-2 nav ms-1">';
14: public $template_menu_end = '</ul></div></li>';
15: public $template_menu_startno = '<li class="nav-item"><a class="nav-link px-0" data-mkey="$this->mkey" href="$mnuhref"><i class="$mnuicon"></i> <span class="d-none d-sm-inline">$mnutitle</span></a>';
16: public $template_menu_endno = '</li>';
17: public $template_menulink = '<li class="nav-item"><a class="nav-link nav-dli px-0" data-mkey="$this->mkey" href="$mnuitemhref"><i class="$mnuicon"></i> <span class="d-none d-sm-inline">$mnuitemtext</span></a></li>';
18:
19: private $mkey = "";
20: private $mkeychar = "";
21: private $fixedPos = "";
22: private $rootMenu = "sidebar";
23: private $counter1 = 1;
24:
25: protected function onrun() {
26: if($this->brandicon == "") $this->brandicon = SphpBase::sphp_settings()->res_path . "/Slib/masters/default/imgs/favicon-96x96.png";
27: $this->init();
28: $this->genMenus();
29: }
30: public function setPosition($val="sticky-top") {
31: // fixed-bottom fixed-top sticky-top
32: $this->fixedPos = $val;
33: }
34: public function setRootMenu($val) {
35: $this->rootMenu = $val;
36: }
37: public function setNavBarCss($val) {
38: $this->navbarClasses = $val;
39: }
40: public function setBrandIcon($val) {
41: $this->brandicon = $val;
42: }
43: public function genMenus() {
44: $strmbar = $this->genMenuBar();
45: $mnuroot = $this->sphp_api->getMenuList("root");
46: // generate bootstrap 4 menu
47: $str1 = "";
48: foreach ($mnuroot as $mnuName => $lstMenu) {
49: if($lstMenu[4] == ""){
50: $str1 .= $this->genMenu($lstMenu);
51: }else if(SphpBase::sphp_permissions()->isPermission($lstMenu[4])){
52: $str1 .= $this->genMenu($lstMenu);
53: }
54: }
55: $this->htmlout = $strmbar[0] . $str1 . $strmbar[1];
56: }
57: private function genMenu($lstMenu,$submenu=0){
58: $mnuroot = $this->sphp_api->getMenuList($lstMenu[0]);
59: $stra = array();
60: $str = "";
61: $str1 = "";
62: if($mnuroot != null){
63: foreach ($mnuroot as $mnuName => $lstMenu2) {
64: if($lstMenu2[4] == ""){
65: $str .= $this->genMenu($lstMenu2,1);
66: }else if(SphpBase::sphp_permissions()->isPermission($lstMenu2[4])){
67: $str .= $this->genMenu($lstMenu2,1);
68: }
69: }
70: $str1 = $this->genMenuLinks($this->sphp_api->getMenuLinkList($lstMenu[0]));
71: if($str1 != ""){
72: $stra = $this->getB4Menu($lstMenu[0],$lstMenu[1],$lstMenu[2],$lstMenu[3],$submenu);
73: }else{
74: $stra = $this->getB4Menu($lstMenu[0],$lstMenu[1],$lstMenu[2],$lstMenu[3],$submenu);
75: }
76: return $stra[0] . $str . $str1 . $stra[1];
77:
78: }else{
79: $str = $this->genMenuLinks($this->sphp_api->getMenuLinkList($lstMenu[0]));
80: if($str != ""){
81: $stra = $this->getB4Menu($lstMenu[0],$lstMenu[1],$lstMenu[2],$lstMenu[3],$submenu);
82: }else{
83: $stra = $this->getB4Menu($lstMenu[0],$lstMenu[1],$lstMenu[2],$lstMenu[3],2);
84: }
85: return $stra[0] . $str . $stra[1];
86: }
87:
88: }
89: private function genMenuLinks($mnuroot){
90: $str = "";
91: if($mnuroot != null){
92: foreach ($mnuroot as $mnuLinkName => $lstMenuLink) {
93: if($lstMenuLink[4] == ""){
94: $str .= $this->getB4MenuLink($lstMenuLink[0],$lstMenuLink[1],$lstMenuLink[2],$lstMenuLink[3]);
95: }else if(SphpBase::sphp_permissions()->isPermission($lstMenuLink[4])){
96: $str .= $this->getB4MenuLink($lstMenuLink[0],$lstMenuLink[1],$lstMenuLink[2],$lstMenuLink[3]);
97: }
98: }
99: }
100: return $str;
101: }
102: private function setAjax(){
103: $this->blnAjaxLink = true;
104: SphpBase::JSServer()->getAJAX();
105: addHeaderJSFunction('menu_ajax', "function menu_ajax(url){
106: ", " getURL(url); }");
107: }
108: private function getB4Menu($mnutext,$mnuhref="",$mnuicon="",$blnAjaxLink2=false,$mnuSub=0){
109: $mnutitle = $mnutext;
110: if($mnuhref==''){
111: $mnuhref = "#";
112: }else if($blnAjaxLink2){
113: if(!$this->blnAjaxLink){
114: $this->setAjax();
115: }
116: $mnuhref = "javascript: menu_ajax('$mnuhref');";
117: }
118: $mnuhref2 = 'd'. $this->counter1;
119: $this->counter1 += 1;
120: $stro = array();
121: $vars = array(
122: '$mnuhref2' => $mnuhref2,
123: '$mnuhref' => $mnuhref,
124: '$mnuicon' => $mnuicon,
125: '$mnutitle' => $mnutitle,
126: '$this->mkey' => $this->mkey
127: );
128: // if menu under root and dropdown
129: if($mnuSub==0){
130: $stro[0] = strtr($this->template_menu_startroot,$vars);
131: $stro[1] = strtr($this->template_menu_endroot,$vars);
132: }else if($mnuSub==1){ // if sub menu and dropdown
133: $stro[0] = strtr($this->template_menu_start,$vars);
134: $stro[1] = strtr($this->template_menu_end,$vars);
135: }else{ // if menu with no links and no sub menu without dropdown
136: $stro[0] = strtr($this->template_menu_startno,$vars);
137: $stro[1] = strtr($this->template_menu_endno,$vars);
138: }
139: return $stro;
140: }
141: private function getB4MenuLink($mnuitemtext,$mnuitemhref="",$mnuicon="",$blnAjaxLink2=false){
142: $mnuitemtitle = $mnuitemtext;
143: $mnuitemtext = $mnuitemtext . $this->mkeychar;
144: $tfun = "menu_ajax";
145: if($mnuitemhref==''){
146: $mnuitemhref = "#";
147: }else if($blnAjaxLink2){
148: $mnuitemhref = "javascript: $tfun('$mnuitemhref');";
149: }
150: $vars = array(
151: '$mnuitemhref' => $mnuitemhref,
152: '$mnuicon' => $mnuicon,
153: '$mnuitemtext' => $mnuitemtext,
154: '$this->mkey' => $this->mkey
155: );
156:
157: return strtr($this->template_menulink,$vars);
158: }
159: public function genMenuBar() {
160: if($this->brandicon != ""){
161: $this->brandicon = ' <!-- Brand -->
162: <div class="col-12 text-center"><a class="" href="#">
163: <img class="img img-fluid rounded-circle shadow-4-strong" src="'. $this->brandicon .'" alt="Logo">'
164: . '</a>'
165: . '<h4>Menu</h4><hr />'
166: . '</div>';
167: }
168: $bootstrapMenu = $this->brandicon . '<div class="snavbar"><ul class="'. $this->navbarClasses .'">';
169: return array($bootstrapMenu,'</ul></div>');
170: }
171: private function setKey($key,$controlkeys="") {
172: $fkeya = array();
173: $fkeya["F1"] = 112;
174: $fkeya["F2"] = 113;
175: $fkeya["F3"] = 114;
176: $fkeya["F4"] = 115;
177: $fkeya["F5"] = 116;
178: $fkeya["F6"] = 117;
179: $fkeya["F7"] = 118;
180: $fkeya["F8"] = 119;
181: $fkeya["F9"] = 120;
182: $fkeya["F10"] = 121;
183: $fkeya["F11"] = 122;
184: $fkeya["F12"] = 123;
185: $mkeychar = "";
186: $controlkeysp = "";
187: if(!is_numeric($key)){
188: $mkeychar = strtoupper($key);
189: if(strlen($key)>1){
190: $key = $fkeya[$mkeychar];
191: }else{
192: $key = ord($mkeychar);
193: }
194: }else{
195: $mkeychar = strtoupper(chr($key));
196: }
197: if($controlkeys != ""){
198: $controlkeysp = "+";
199: }
200: $this->mkeychar = " ". strtoupper($controlkeys). $controlkeysp . $mkeychar;
201: $this->mkey = $key;
202: $controlkeysa = explode('+',$controlkeys);
203: $strkey = "";
204: foreach ($controlkeysa as $key => $value) {
205: if($value == "alt"){
206: if($strkey != "") $strkey .= " && ";
207: $strkey .= "eventer.event.altKey";
208: }else if($value == "ctrl"){
209: if($strkey != "") $strkey .= " && ";
210: $strkey .= "eventer.event.ctrlKey";
211: }else if($value == "shift"){
212: if($strkey != "") $strkey .= " && ";
213: $strkey .= "eventer.event.shiftKey";
214: }
215: }
216: if($strkey != "") $strkey .= " && ";
217: // code depend on key listner javascript code of framework
218: addHeaderJSFunction("jq_menukeyevent", "function jq_menukeyevent(eventer){", "}",true);
219: addHeaderJSFunctionCode("jq_keyevent", "menukeyevent1", 'ret = jq_menukeyevent(eventer2);',true);
220: addHeaderJSFunctionCode("jq_menukeyevent", $this->name, ' if('. $strkey .'eventer.keycode=='. $this->mkey .'){
221: if(eventer.evt==\'keyup\'){
222: $(".dropdown-item[data-mkey='.$this->mkey.']")[0].click();
223: return false;
224: }
225: }
226: ',true);
227: //console.log(eventer.keycode);
228: //console.log(eventer.event.shiftKey);
229: //console.log(eventer.event.ctrlKey);
230: //console.log(eventer.event.altKey);
231: }
232:
233: private function init() {
234: addHeaderJSFunctionCode("ready", "snavbar1", '
235: var links = jql(\'.snavbar a\');
236: jql.each(links, function (key, va) {
237: if (va.href == document.URL) {
238: jql(this).addClass(\'active\');
239: var pa = jql(this).parents(\'li.nav-dli\');
240: jql.each(pa, function (key2, va2) {
241: jql(va2).children("a.nav-dlink:first").addClass(\'active\');
242: });
243: }
244: });
245: //jql(\'.nav-dlink\').click();
246: jql.each(jql(\'.nav-dlink\'), function (key, va) {
247: va.click();
248: });
249: ',true);
250: addHeaderCSS("snavbar", '.nav-link[data-bs-toggle].collapsed:before {
251: content: " \2191";
252: }
253: .nav-link[data-bs-toggle]:not(.collapsed):before {
254: content: " \2193";
255: }
256:
257: ', true);
258:
259: }
260:
261: }
262: