PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/templates/ja_zeolite_ii/libs/menu/base.class.php

http://vanphongphamdm.googlecode.com/
PHP | 586 lines | 476 code | 62 blank | 48 comment | 140 complexity | faaeda3d3815d5e5a1da41d9e95f6493 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, Apache-2.0
  1. <?php
  2. /*
  3. #------------------------------------------------------------------------
  4. T3 Framework for Joomla 1.5
  5. #------------------------------------------------------------------------
  6. #Copyright (C) 2004-2009 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
  7. #@license - GNU/GPL, http://www.gnu.org/copyleft/gpl.html
  8. #Author: J.O.O.M Solutions Co., Ltd
  9. #Websites: http://www.joomlart.com - http://www.joomlancers.com
  10. #------------------------------------------------------------------------
  11. */
  12. defined('_JEXEC') or die('Restricted access');
  13. if (!defined ('_JA_BASE_MENU_CLASS')) {
  14. define ('_JA_BASE_MENU_CLASS', 1);
  15. class JAMenuBase extends JObject{
  16. var $_params = null;
  17. var $children = null;
  18. var $open = null;
  19. var $items = null;
  20. var $Itemid = 0;
  21. var $showSeparatedSub = false;
  22. var $_tmpl = null;
  23. function __construct( &$params ){
  24. global $Itemid;
  25. $this->_params = $params;
  26. $this->Itemid = $Itemid;
  27. //$this->loadMenu();
  28. }
  29. function createParameterObject($param, $path='', $type='menu') {
  30. return new JParameter($param, $path);
  31. }
  32. function getPageTitle ($params) {
  33. return $params->get ('page_title');
  34. }
  35. function loadMenu(){
  36. $user =& JFactory::getUser();
  37. $children = array ();
  38. $aid = $user->get('aid', 0);
  39. // Get Menu Items
  40. $items = &JSite::getMenu();
  41. $rows = $items->getItems('menutype', $this->getParam('menutype'));
  42. if(!count($rows))
  43. $rows = $items->getItems('menutype', 'mainmenu');
  44. if(!count($rows)) return;
  45. // first pass - collect children
  46. $cacheIndex = array();
  47. $this->items = array();
  48. foreach ($rows as $index => $v) {
  49. $v->name = str_replace ('&', '&amp;', str_replace ('&amp;', '&', $v->name));
  50. if ($v->access <= $aid) {
  51. $pt = $v->parent;
  52. $list = @ $children[$pt] ? $children[$pt] : array ();
  53. $v->megaparams = $this->parseTitle ($v->name);
  54. $v->megaparams->set('class', str_replace (',', ' ', $v->megaparams->get('class', '')));
  55. $v->name = trim($v->megaparams->get('title'));
  56. //Load params added by plugin
  57. $vparams = new JParameter ($v->params);
  58. //get mega params
  59. $megaparams = $vparams->toObject();
  60. if ($megaparams) {
  61. foreach (get_object_vars($megaparams) as $mega_name=>$mega_value) {
  62. if (preg_match ('/mega_(.+)/', $mega_name, $matches)) {
  63. if ($matches[1] == 'colxw') {
  64. if (preg_match_all ('/([^\s]+)=([^\s]+)/', $mega_value, $colwmatches)) {
  65. for ($i=0;$i<count($colwmatches[0]);$i++) {
  66. $v->megaparams->set ($colwmatches[1][$i],$colwmatches[2][$i]);
  67. }
  68. }
  69. } else {
  70. if (is_array($mega_value)) $mega_value = implode (',', $mega_value);
  71. $v->megaparams->set ($matches[1], $mega_value);
  72. }
  73. }
  74. }
  75. }
  76. //reset cols for group item
  77. if ($v->megaparams->get('group')) $v->megaparams->set('cols', 1);
  78. if ($this->getParam('megamenu')) {
  79. $modules = $this->loadModules ($v->megaparams);
  80. //Update title: clear title if not show
  81. if (!$v->megaparams->get ('showtitle', 1)) $v->name = '';
  82. //if ($v->name == 'FAQ') {print_r ($v->megaparams);exit;}
  83. if ($modules && count($modules)>0) {
  84. $v->content = "";
  85. $total = count($modules);
  86. $cols = min($v->megaparams->get('cols'), $total);
  87. for ($col=0;$col<$cols;$col++) {
  88. $pos = ($col == 0 ) ? 'first' : (($col == $cols-1) ? 'last' :'');
  89. if ($cols > 1) $v->content .= $this->beginSubMenuModules($v->id, 1, $pos, $col, true);
  90. $i = $col;
  91. while ($i<$total) {
  92. $mod = $modules[$i];
  93. $i += $cols;
  94. $mod_params = new JParameter($mod->params);
  95. $v->content .= JModuleHelper::renderModule($mod, array('style'=>$v->megaparams->get('style','jaxhtml')));
  96. }
  97. if ($cols > 1) $v->content .= $this->endSubMenuModules($v->id, 1, true);
  98. }
  99. $v->cols = $cols;
  100. $v->content = trim($v->content);
  101. $this->items[$v->id] = $v;
  102. }
  103. }
  104. switch ($v->type)
  105. {
  106. case 'separator' :
  107. $v->url = '#';
  108. break;
  109. case 'url' :
  110. if ((strpos($v->link, 'index.php?') !== false) && (strpos($v->link, 'Itemid=') === false)) {
  111. $v->url = $v->link.'&amp;Itemid='.$v->id;
  112. } else {
  113. $v->url = $v->link;
  114. }
  115. break;
  116. default :
  117. $router = JSite::getRouter();
  118. $v->url = $router->getMode() == JROUTER_MODE_SEF ? 'index.php?Itemid='.$v->id : $v->link.'&Itemid='.$v->id;
  119. break;
  120. }
  121. // Handle SSL links
  122. $iParams = $this->createParameterObject($v->params);
  123. $iSecure = $iParams->def('secure', 0);
  124. if ($v->home == 1) {
  125. $v->url = JURI::base();
  126. } elseif (strcasecmp(substr($v->url, 0, 4), 'http') && (strpos($v->link, 'index.php?') !== false)) {
  127. $v->url = JRoute::_($v->url, true, $iSecure);
  128. } else {
  129. $v->url = str_replace('&', '&amp;', $v->url);
  130. }
  131. //calculate menu column
  132. if (!isset($v->clssfx)) {
  133. $v->clssfx = $iParams->get('pageclass_sfx', '');
  134. if ($v->megaparams->get('cols')) {
  135. $v->cols = $v->megaparams->get('cols');
  136. $v->col = array();
  137. for ($i=0;$i<$v->cols;$i++) {
  138. if ($v->megaparams->get("col$i")) $v->col[$i]=$v->megaparams->get("col$i");
  139. }
  140. }
  141. }
  142. $v->_idx = count($list);
  143. array_push($list, $v);
  144. $children[$pt] = $list;
  145. $cacheIndex[$v->id] = $index;
  146. $this->items[$v->id] = $v;
  147. }
  148. }
  149. $this->children = $children;
  150. //unset item load module but no content
  151. foreach ($this->items as $v) {
  152. if (($v->megaparams->get('modid') || $v->megaparams->get('modname') || $v->megaparams->get('modpos'))
  153. && !isset($this->children[$v->id]) && (!isset($v->content) || !$v->content)) {
  154. $this->remove_item($this->items[$v->id]);
  155. unset($this->items[$v->id]);
  156. }
  157. }
  158. // second pass - collect 'open' menus
  159. $open = array (
  160. $this->Itemid
  161. );
  162. $count = 20; // maximum levels - to prevent runaway loop
  163. $id = $this->Itemid;
  164. while (-- $count)
  165. {
  166. if (isset($cacheIndex[$id])) {
  167. $index = $cacheIndex[$id];
  168. if (isset ($rows[$index]) && $rows[$index]->parent > 0) {
  169. $id = $rows[$index]->parent;
  170. $open[] = $id;
  171. } else {
  172. break;
  173. }
  174. }
  175. }
  176. $this->open = $open;
  177. // $this->items = $rows;
  178. }
  179. function remove_item ($item) {
  180. $result = array();
  181. foreach ($this->children[$item->parent] as $o) {
  182. if ($o->id != $item->id) {
  183. $result[] = $o;
  184. }
  185. }
  186. $this->children[$item->parent] = $result;
  187. }
  188. function parseTitle ($title) {
  189. //replace escape character
  190. $title = str_replace (array('\\[','\\]'), array('%open%', '%close%'), $title);
  191. $regex = '/([^\[]*)\[([^\]]*)\](.*)$/';
  192. if (preg_match ($regex, $title, $matches)) {
  193. $title = $matches[1];
  194. $params = $matches[2];
  195. $desc = $matches[3];
  196. } else {
  197. $title = $title;
  198. $params = '';
  199. $desc = '';
  200. }
  201. $title = str_replace (array('%open%', '%close%'), array('[',']'), $title);
  202. $desc = str_replace (array('%open%', '%close%'), array('[',']'), $desc);
  203. $result = new JParameter('');
  204. $result->set('title', trim($title));
  205. $result->set('desc', trim($desc));
  206. if ($params) {
  207. if (preg_match_all ('/([^\s]+)=([^\s]+)/', $params, $matches)) {
  208. for ($i=0;$i<count($matches[0]);$i++) {
  209. $result->set ($matches[1][$i],$matches[2][$i]);
  210. }
  211. }
  212. }
  213. return $result;
  214. }
  215. function loadModules($params) {
  216. //Load module
  217. $modules = array();
  218. switch ($params->get ('subcontent')) {
  219. case 'mod':
  220. $ids = preg_split ('/,/', $params->get ('subcontent-mod-modules',''));
  221. foreach ($ids as $id) {
  222. if ($module=$this->getModule ($id)) $modules[] = $module;
  223. }
  224. return $modules;
  225. break;
  226. case 'pos':
  227. $poses = preg_split ('/,/', $params->get ('subcontent-pos-positions',''));
  228. foreach ($poses as $pos) {
  229. $modules = array_merge ($modules, $this->getModules ($pos));
  230. }
  231. return $modules;
  232. break;
  233. default:
  234. return $this->loadModules_ ($params); //load as old method
  235. }
  236. return null;
  237. }
  238. function loadModules_($params) {
  239. //Load module
  240. $modules = array();
  241. if (($modid = $params->get('modid'))) {
  242. $ids = preg_split ('/,/', $modid);
  243. foreach ($ids as $id) {
  244. if ($module=$this->getModule ($id)) $modules[] = $module;
  245. }
  246. return $modules;
  247. }
  248. if (($modname = $params->get('modname'))) {
  249. $names = preg_split ('/,/', $modname);
  250. foreach ($names as $name) {
  251. if (($module=$this->getModule (0, $name))) $modules[] = $module;
  252. }
  253. return $modules;
  254. }
  255. if (($modpos = $params->get('modpos'))) {
  256. $poses = preg_split ('/,/', $modpos);
  257. foreach ($poses as $pos) {
  258. $modules = array_merge ($modules, $this->getModules ($pos));
  259. }
  260. return $modules;
  261. }
  262. return null;
  263. }
  264. function getModules ($position) {
  265. return JModuleHelper::getModules ($position);
  266. }
  267. function getModule ($id=0, $name='') {
  268. $result = null;
  269. $modules =& JModuleHelper::_load();
  270. $total = count($modules);
  271. for ($i = 0; $i < $total; $i++)
  272. {
  273. // Match the name of the module
  274. if ($modules[$i]->id == $id || $modules[$i]->name == $name)
  275. {
  276. return $modules[$i];
  277. }
  278. }
  279. return null;
  280. }
  281. function genMenuItem($item, $level = 0, $pos = '', $ret = 0)
  282. {
  283. $data = '';
  284. $tmp = $item;
  285. // Print a link if it exists
  286. $active = $this->genClass ($tmp, $level, $pos);
  287. $id='id="menu' . $tmp->id . '"';
  288. $iParams = new JParameter ( $item->params );
  289. $itembg = '';
  290. if ($this->getParam('menu_images') && $iParams->get('menu_image') && $iParams->get('menu_image') != -1) {
  291. if ($this->getParam('menu_background')) {
  292. $itembg = 'style="background-image:url('.JURI::base().'/images/stories/'.$iParams->get('menu_image').');"';
  293. $txt = '<span class="menu-title">' . $tmp->name . '</span>';
  294. } else {
  295. $txt = '<span class="menu-image"><img src="'.JURI::base().'/images/stories/'.$iParams->get('menu_image').'" alt="'.$tmp->name.'" title="'.$tmp->name.'" /></span><span class="menu-title">' . $tmp->name . '</span>';
  296. }
  297. } else {
  298. $txt = '<span class="menu-title">' . $tmp->name . '</span>';
  299. }
  300. //Add page title to item
  301. if ($tmp->megaparams->get('desc')) {
  302. $txt .= '<span class="menu-desc">'. $tmp->megaparams->get('desc').'</span>';
  303. }
  304. if (isset ($itembg) && $itembg) {
  305. $txt = "<span class=\"has-image\" $itembg>".$txt."</span>";
  306. }
  307. $title = "title=\"$tmp->name\"";
  308. if ($tmp->type == 'menulink')
  309. {
  310. $menu = &JSite::getMenu();
  311. $alias_item = clone($menu->getItem($tmp->query['Itemid']));
  312. if (!$alias_item) {
  313. return false;
  314. } else {
  315. $tmp->url = $alias_item->link;
  316. }
  317. }
  318. if ($tmp->name) {
  319. if ($tmp->type == 'separator')
  320. {
  321. $data = '<a href="#" '.$active.' '.$id.' '.$title.'>'.$txt.'</a>';
  322. } else {
  323. if ($tmp->url != null)
  324. {
  325. switch ($tmp->browserNav)
  326. {
  327. default:
  328. case 0:
  329. // _top
  330. $data = '<a href="'.$tmp->url.'" '.$active.' '.$id.' '.$title.'>'.$txt.'</a>';
  331. break;
  332. case 1:
  333. // _blank
  334. $data = '<a href="'.$tmp->url.'" target="_blank" '.$active.' '.$id.' '.$title.'>'.$txt.'</a>';
  335. break;
  336. case 2:
  337. // window.open
  338. $attribs = 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,'.$this->getParam('window_open');
  339. // hrm...this is a bit dickey
  340. $link = str_replace('index.php', 'index2.php', $tmp->url);
  341. $data = '<a href="'.$link.'" onclick="window.open(this.href,\'targetWindow\',\''.$attribs.'\');return false;" '.$active.' '.$id.' '.$title.'>'.$txt.'</a>';
  342. break;
  343. }
  344. } else {
  345. $data = '<a '.$active.' '.$id.' '.$title.'>'.$txt.'</a>';
  346. }
  347. }
  348. }
  349. //for megamenu
  350. if ($this->getParam ('megamenu')) {
  351. //For group
  352. if ($tmp->megaparams->get('group') && $data)
  353. $data = "<div class=\"group-title\">$data</div>";
  354. if (isset($item->content) && $item->content) {
  355. if ($item->megaparams->get('group')){
  356. $data .= "<div class=\"group-content\">{$item->content}</div>";
  357. }else{
  358. $data .= $this->beginMenuItems($item->id, $level+1, true);
  359. $data .= $item->content;
  360. $data .= $this->endMenuItems($item->id, $level+1, true);
  361. }
  362. }
  363. }
  364. if ($ret) return $data; else echo $data;
  365. }
  366. function getParam($paramName, $default=null){
  367. return $this->_params->get($paramName, $default);
  368. }
  369. function setParam($paramName, $paramValue){
  370. return $this->_params->set($paramName, $paramValue);
  371. }
  372. function beginMenu($startlevel=0, $endlevel = 10){
  373. echo "<div>";
  374. }
  375. function endMenu($startlevel=0, $endlevel = 10){
  376. echo "</div>";
  377. }
  378. function beginMenuItems($pid=0, $level=0){
  379. echo "<ul>";
  380. }
  381. function endMenuItems($pid=0, $level=0){
  382. echo "</ul>";
  383. }
  384. function beginSubMenuItems($pid=0, $level=0, $pos='', $i, $return = false){
  385. //for megamenu menu
  386. }
  387. function endSubMenuItems($pid=0, $level=0, $return = false){
  388. //for megamenu menu
  389. }
  390. function beginMenuItem($mitem=null, $level = 0, $pos = ''){
  391. $active = $this->genClass ($mitem, $level, $pos);
  392. echo "<li $active>";
  393. }
  394. function endMenuItem($mitem=null, $level = 0, $pos = ''){
  395. echo "</li>";
  396. }
  397. function genClass ($mitem, $level, $pos) {
  398. $iParams = new JParameter ( $mitem->params );
  399. $active = in_array($mitem->id, $this->open);
  400. $cls = ($level?"":"menu-item{$mitem->_idx}"). ($active?" active":"").($pos?" $pos-item":"");
  401. if (@$this->children[$mitem->id] && $level < $this->getParam ('endlevel')) $cls .= " haschild";
  402. return $cls?"class=\"$cls\"":"";
  403. }
  404. function hasSubMenu($level) {
  405. $pid = $this->getParentId ($level);
  406. if (!$pid) return false;
  407. return $this->hasSubItems ($pid);
  408. }
  409. function hasSubItems($id){
  410. if (@$this->children[$id]) return true;
  411. return false;
  412. }
  413. function genMenu($startlevel=0, $endlevel = -1){
  414. $this->setParam('startlevel', $startlevel);
  415. $this->setParam('endlevel', $endlevel==-1?10:$endlevel);
  416. $this->beginMenu($startlevel, $endlevel);
  417. if ($this->getParam('startlevel') == 0) {
  418. //First level
  419. $this->genMenuItems (0, 0);
  420. }else{
  421. //Sub level
  422. $pid = $this->getParentId($this->getParam('startlevel'));
  423. if ($pid)
  424. $this->genMenuItems ($pid, $this->getParam('startlevel'));
  425. }
  426. $this->endMenu($startlevel, $endlevel);
  427. }
  428. /*
  429. $pid: parent id
  430. $level: menu level
  431. $pos: position of parent
  432. */
  433. function genMenuItems($pid, $level) {
  434. if (@$this->children[$pid]) {
  435. //Detect description. If some items have description, must generate empty description for other items
  436. $hasDesc = false;
  437. foreach ($this->children[$pid] as $row) {
  438. if ($row->megaparams->get('desc')) {
  439. $hasDesc = true;
  440. break;
  441. }
  442. }
  443. if ($hasDesc) {
  444. //Update empty description with a space - &nbsp;
  445. foreach ($this->children[$pid] as $row) {
  446. if (!$row->megaparams->get('desc')) {
  447. $row->megaparams->set('desc', '&nbsp;');
  448. }
  449. }
  450. }
  451. $j = 0;
  452. $cols = $pid && $this->getParam('megamenu') && isset($this->items[$pid]) && isset($this->items[$pid]->cols) && $this->items[$pid]->cols ? $this->items[$pid]->cols : 1;
  453. $total = count ($this->children[$pid]);
  454. $tmp = $pid?$this->items[$pid]:new stdclass();
  455. if ($cols > 1) {
  456. $fixitems = count($tmp->col);
  457. if ($fixitems < $cols) {
  458. $fixitem = array_sum($tmp->col);
  459. $leftitem = $total-$fixitem;
  460. $items = ceil ($leftitem/($cols-$fixitems));
  461. for ($m=0;$m<$cols && $leftitem > 0;$m++) {
  462. if (!isset($tmp->col[$m]) || !$tmp->col[$m]) {
  463. if ($leftitem > $items) {
  464. $tmp->col[$m] = $items;
  465. $leftitem -= $items;
  466. } else {
  467. $tmp->col[$m] = $leftitem;
  468. $leftitem = 0;
  469. }
  470. }
  471. }
  472. $cols = count ($tmp->col);
  473. $tmp->cols = $cols;
  474. }
  475. } else {
  476. $tmp->col = array($total);
  477. }
  478. $this->beginMenuItems($pid, $level);
  479. for ($col=0;$col<$cols && $j<$total;$col++) {
  480. $pos = ($col == 0 ) ? 'first' : (($col == $cols-1) ? 'last' :'');
  481. $this->beginSubMenuItems($pid, $level, $pos, $col);
  482. $i = 0;
  483. while ($i < $tmp->col[$col] && $j<$total) {
  484. //foreach ($this->children[$pid] as $row) {
  485. $row = $this->children[$pid][$j];
  486. $pos = ($i == 0 ) ? 'first' : (($i == count($this->children[$pid])-1) ? 'last' :'');
  487. $this->beginMenuItem($row, $level, $pos);
  488. $this->genMenuItem( $row, $level, $pos);
  489. // show menu with menu expanded - submenus visible
  490. if ($this->getParam('megamenu') && $row->megaparams->get('group')) $this->genMenuItems( $row->id, $level ); //not increase level
  491. else if ($level < $this->getParam('endlevel')) $this->genMenuItems( $row->id, $level+1 );
  492. $this->endMenuItem($row, $level, $pos);
  493. $j++;$i++;
  494. }
  495. $this->endSubMenuItems($pid, $level);
  496. }
  497. $this->endMenuItems($pid, $level);
  498. }
  499. }
  500. function indentText($level, $text) {
  501. echo "\n";
  502. for ($i=0;$i<$level;++$i) echo " ";
  503. echo $text;
  504. }
  505. function getParentId ($level) {
  506. if (!$level || (count($this->open) < $level)) return 0;
  507. return $this->open[count($this->open)-$level];
  508. }
  509. function getParentText ($level) {
  510. $pid = $this->getParentId ($level);
  511. if ($pid) {
  512. return $this->items[$pid]->name;
  513. }else return "";
  514. }
  515. function genMenuHead () {
  516. if (isset($this->_css) && $this->_css) {
  517. echo "<link href=\"{$this->_css}\" rel=\"stylesheet\" type=\"text/css\" />";
  518. }
  519. if (isset($this->_js) && $this->_js) {
  520. echo "<script src=\"{$this->_js}\" language=\"javascript\" type=\"text/javascript\"></script>";
  521. }
  522. }
  523. }
  524. }