PageRenderTime 32ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/include/global/genmenu.php

http://opixido-ocms.googlecode.com/
PHP | 324 lines | 169 code | 65 blank | 90 comment | 49 complexity | 4e31e598a331e26dbf72a50993f2bcb4 MD5 | raw file
Possible License(s): GPL-3.0, Apache-2.0, BSD-3-Clause, LGPL-2.1, GPL-2.0
  1. <?php
  2. #
  3. # This file is part of oCMS.
  4. #
  5. # oCMS is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # oCMS is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with oCMS. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # @author Celio Conort / Opixido
  19. # @copyright opixido 2009
  20. # @link http://code.google.com/p/opixido-ocms/
  21. # @package ocms
  22. #
  23. class genMenu {
  24. /* Proprietes de la classe genMenu */
  25. private $site; //Objet de type site
  26. private $tabHeader; //Tableau contenant les url's du menu topright
  27. public $tabPrincipal; //Celui contenant les url's du menu principal
  28. private $tabFooter; //Celui contenant les url's du menu du footer
  29. public $separator;
  30. public $visible = true;
  31. private $level = 1;
  32. /**
  33. * Menu creation
  34. *
  35. * @param gensite $site
  36. * @param string $nom_menu string ID/name of the menu
  37. * @param int $id_menu numerical ID of the menu
  38. * @param array $row_menu s_rubrique row of the menu
  39. * @return genMenu
  40. */
  41. function genMenu($site, $nom_menu = '', $id_menu = 0, $row_menu = array()) {
  42. global $headRootId;
  43. global $rootId;
  44. global $footRootId;
  45. global $_Gconfig;
  46. $this->site = $site;
  47. /**
  48. * Concats menus
  49. */
  50. if (ake($_Gconfig['menus'], $nom_menu)) {
  51. $this->conf = $_Gconfig['menus'][$nom_menu];
  52. } else {
  53. $this->conf = $_Gconfig['menus']['__default__'];
  54. }
  55. /**
  56. * If no $row, selecting it in database
  57. */
  58. if (!count($row_menu)) {
  59. if ($id_menu && !$nom_menu) {
  60. $row_menu = GetRowFromId('s_rubrique', (int) $id_menu);
  61. } else if ($nom_menu && !$id_menu) {
  62. $row_menu = GetRowFromFieldLike('s_rubrique', 'rubrique_url_' . LG_DEF, mes($nom_menu));
  63. }
  64. }
  65. $this->row = $row_menu;
  66. $this->id_menu = $row_menu['rubrique_id'];
  67. $this->nom_menu = $row_menu['rubrique_url_' . LG_DEF];
  68. /**
  69. * Cache for menu
  70. */
  71. $this->cache2 = new GenCache('arbo' . md5($this->site->g_url->topRubId . '-' . $this->site->getCurId() . '_' . $this->nom_menu), GetParam('date_update_arbo'));
  72. if (!$this->cache2->cacheExists()) {
  73. $this->genTab();
  74. }
  75. }
  76. /**
  77. * Returns full HTML menu
  78. *
  79. * @return string
  80. */
  81. function getTab() {
  82. if (!$this->cache2->cacheExists())
  83. $this->cache2->saveCache($this->gen($this->tabPrincipal, 2));
  84. return $this->cache2->getCache();
  85. }
  86. /**
  87. * Generates HTML Menu
  88. *
  89. */
  90. function genTab() {
  91. global $rootId;
  92. if (!count($this->tabPrincipal))
  93. $this->tabPrincipal = $this->site->g_url->recursRub($this->id_menu, 1, $this->conf['max_levels']);
  94. return $this->tabPrincipal;
  95. }
  96. /**
  97. * Loops the items of the menu
  98. *
  99. *
  100. * @param array $tab
  101. * @param int $rootId
  102. * @return unknown
  103. */
  104. function gen($tab, $rootId = '') {
  105. if (!$this->visible) {
  106. return;
  107. }
  108. $ulId = empty($rootId) ? '' : ' id="menu_' . $this->nom_menu . '"';
  109. $divid = empty($rootId) ? '' : ' id="div_menu_' . $this->nom_menu . '"';
  110. if ($divid) {
  111. $html = '<div' . $divid . '><ul' . $ulId . ' class="level_' . $this->level . '">';
  112. } else {
  113. $html = '<ul' . $ulId . ' class="level_' . $this->level . '">';
  114. }
  115. if (!is_array($tab) || !count($tab)) {
  116. return '';
  117. }
  118. $cpt = 1;
  119. $nbM = 0;
  120. $nbTot = count($tab);
  121. foreach ($tab as $key => $value) {
  122. $nbM++;
  123. if (strlen($this->separator) && $cpt > 1) {
  124. $tpl = new genTemplate();
  125. $tpl->loadTemplate($this->conf['tpl_name'], $this->conf['tpl_folder']);
  126. $tpl->set('titre', $this->separator);
  127. $html .= $tpl->gen();
  128. }
  129. $tpl = new genTemplate();
  130. $tpl->loadTemplate($this->conf['tpl_name'], $this->conf['tpl_folder']);
  131. if (!empty($rootId)) {
  132. if ($this->conf['use_images']) {
  133. $value['titre'] = $this->genImageMenu($value['titre'], $this->nom_menu, $cpt, $value['selected']) . '';
  134. } else if ($this->conf['use_premade_images']) {
  135. $value['titre'] = '<img src="' . BU . '/img/menu_' . $this->nom_menu . '/' . $cpt . ($value['selected'] ? '-roll' : '') . '.png" alt=' . alt($value['titre']) . ' />';
  136. } else {
  137. $value['titre'] = $value['titre'] . '';
  138. }
  139. }
  140. $style = ' class="';
  141. $style .= akev($value, 'selected') ? 'selected ' : '';
  142. $style .= $nbM == $nbTot ? 'dernier ' : '';
  143. $style .= $nbM == 1 ? 'premier ' : '';
  144. $style .= $nbM != $nbTot && $nbM != 1 ? 'milieu ' : '';
  145. $style .= '" ';
  146. if (!akev($value, 'id')) {
  147. $value['idmenu'] = nicename($value['titre']);
  148. } else {
  149. $value['idmenu'] = $value['id'];
  150. }
  151. $style .= akev($value, 'style');
  152. $tpl->set('lien', getLgUrl(akev($value, 'url')));
  153. $tpl->set('titre', akev($value, 'titre'));
  154. $tpl->set('id', empty($rootId) ? ' id="ml' . akev($value, 'idmenu') . '" ' : ' id="menu_' . $this->nom_menu . '_' . $cpt . '" ');
  155. $tpl->set('style', akev($value, 'style'));
  156. $tpl->set('classa', akev($value, 'selected') ? ' class="selected" title=' . alt($value['titre'] . ' ' . t('rubrique_selectionnee')) . ' ' : '');
  157. $tpl->set('style', $style);
  158. $tpl->set('level', $this->level);
  159. //debug(akev($value,'selected']);
  160. $this->level++;
  161. if (is_array(akev($value, 'sub'))) {
  162. $tpl->set('sub', $this->gen(akev($value, 'sub')));
  163. } else if (akev($value, 'selected') && $this->conf['open_selected'] && $this->conf['max_open_selected'] >= $this->level) {
  164. $sub = $this->site->g_url->recursRub(akev($value, 'id'), 1, 1);
  165. $tpl->set('sub', $this->gen($sub));
  166. }
  167. $this->level--;
  168. $html .= $tpl->gen();
  169. $cpt++;
  170. }
  171. if ($divid) {
  172. $html .='</ul></div>';
  173. } else {
  174. $html .='</ul>';
  175. }
  176. return $html;
  177. }
  178. /**
  179. * Adds an element to the menu
  180. *
  181. * @param unknown_type $tab
  182. * @param unknown_type $titre
  183. * @param unknown_type $url
  184. * @param unknown_type $style
  185. */
  186. function addMenu($tab, $titre, $url, $style = '') {
  187. /*
  188. Ajoute manuellement un element au menu $tab
  189. @tab Menu auquel ajouter (tabFooter,tabHeader,tabPrincipal)
  190. */
  191. $t = &$this->$tab;
  192. $t[$url]['titre'] = $titre;
  193. $t[$url]['url'] = $url;
  194. $t[$url]['sub'] = '';
  195. $t[$url]['style'] = $style;
  196. }
  197. /**
  198. * Changes configuration options of the menu
  199. *
  200. * @param string $conf name of option
  201. * @param unknown_type $nb
  202. * @return unknown
  203. */
  204. function addConf($conf, $nb = 0) {
  205. $val = $this->conf[$conf];
  206. if ($nb) {
  207. $val = $this->conf[$conf][$nb - 1];
  208. }
  209. if ($this->conf[$conf]) {
  210. return '&amp;' . $conf . '=' . $val;
  211. }
  212. }
  213. /**
  214. * Generates images for the menu
  215. *
  216. * @param unknown_type $val
  217. * @param unknown_type $nom_menu
  218. * @param unknown_type $nb
  219. * @param unknown_type $selected
  220. * @return unknown
  221. */
  222. function genImageMenu($val, $nom_menu, $nb = 0, $selected) {
  223. if ($this->conf['caps']) {
  224. $val = majuscules($val);
  225. }
  226. //$srcNormal = IMG_GENERATOR.'?nb='.$nb.'&text='.str_replace('-','%2d',urlencode($val));
  227. $srcNormal = 'nb=' . $nb;
  228. if (is_array($this->conf['profiles']) && $this->conf['profiles'][$nb - 1]) {
  229. $srcNormal .= '&amp;profile=' . $this->conf['profiles'][$nb - 1];
  230. } else {
  231. $srcNormal .= $this->addConf('profile');
  232. }
  233. $srcNormal .= $this->addConf('width', $nb);
  234. $srcNormal .= $this->addConf('imgW', $nb);
  235. if ($selected) {
  236. $srcNormal .= '&textColor=' . $GLOBALS['menu_' . $this->nom_menu . '_' . $nb];
  237. }
  238. if ($this->conf['rollover']) {
  239. $srcOver = IMG_GENERATOR . '?text=' . str_replace('-', '%2d', urlencode($val));
  240. if (is_array($this->conf['rollovers']) && $this->conf['rollovers'][$nb - 1]) {
  241. $srcOver .= '&amp;profile=' . $this->conf['rollovers'][$nb - 1];
  242. } else {
  243. $srcOver .= '&amp;profile=' . $this->conf['rollover'];
  244. }
  245. $srcOver .= $this->addConf('width', $nb);
  246. $srcOver .= $this->addConf('imgW', $nb);
  247. //$srcOver .= $this->addConf('b',$nb);
  248. }
  249. $srcNormal = getImgTextSrc($val, '', $srcNormal);
  250. $return = '<img src="' . $srcNormal . '" ';
  251. if ($srcOver) {
  252. $return .=' onmouseover="swapImage(\'' . $srcOver . '\',this)" ';
  253. }
  254. $return .= ' alt=' . alt($val) . ' />';
  255. //if($barre[$nom_menu][$nb] == 'oui') $return .= '<img src="/img/base/tirets_vert.gif" alt="" />';
  256. return $return;
  257. //}
  258. }
  259. }