PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/codefight-cms/codefight/app/libraries/Cf_menu_lib.php

http://cmsdamu.googlecode.com/
PHP | 214 lines | 138 code | 41 blank | 35 comment | 36 complexity | 58e13be7768d26f3a2b132598d3b6b5f MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, AGPL-1.0, GPL-2.0
  1. <?php
  2. //If BASEPATH is not defined, simply exit.
  3. if (!defined('BASEPATH')) {
  4. exit('No direct script access allowed');
  5. }
  6. /*
  7. >menu_id | menu_active | menu_parent_id | menu_link
  8. >menu_title | menu_type | menu_meta_title | menu_meta_keywords
  9. >menu_meta_description | menu_sort
  10. Parameters that can be passed in an array:
  11. $parameters = array (
  12. 'ul_param' => 'class="xyz"...',
  13. 'li_param' => '...',
  14. 'a_param' => '...',
  15. )
  16. multi level menu created for codefight cms by damodar bashyal
  17. visit codefight.org
  18. */
  19. class Cf_menu_lib
  20. {
  21. var $CI;
  22. var $menu;
  23. var $counter;
  24. var $holder;
  25. var $menu_type;
  26. var $ul_param;
  27. var $li_param;
  28. var $a_param;
  29. var $rtn = FALSE;
  30. var $menu_list;
  31. var $echo_list;
  32. var $last_id;
  33. var $is_last;
  34. var $reduce_space;
  35. function _reset()
  36. {
  37. $this->CI =& get_instance();
  38. $this->menu = array();
  39. $this->counter = 0;
  40. $this->holder = array();
  41. $this->menu_type = 'page';
  42. $this->is_last = FALSE;
  43. $this->reduce_space = TRUE;
  44. }
  45. function get($parameters = array('menu_type' => 'page'), $rtn = FALSE)
  46. {
  47. $this->_reset();
  48. if (!is_array($parameters)) $parameters = array($parameters);
  49. foreach ($parameters as $k => $v)
  50. {
  51. $this->$k = $v;
  52. }
  53. //Do you want to get menu as array item
  54. $this->rtn = $rtn;
  55. $this->echo_list = ''; //reset to empty. b'coz found some issue.
  56. $this->CI->db->where('menu_type', $this->menu_type);
  57. //if it is to be return for admin menu, don't check if it is active
  58. if (!$rtn) {
  59. $this->CI->db->where('menu_active', 1);
  60. if (defined('CFWEBSITEID')) {
  61. $this->CI->db->like('websites_id', ',' . CFWEBSITEID . ',');
  62. }
  63. }
  64. $this->CI->db->order_by('menu_sort', 'asc');
  65. $query = $this->CI->db->get('menu');
  66. $rows = $query->result_array();
  67. return $this->_prepare($rows);
  68. }
  69. function _prepare($data = array())
  70. {
  71. //if $data is no array, return false
  72. if (!is_array($data) || empty($data)) return false;
  73. $menu_array = array();
  74. $this->last_id = 0;
  75. //Group Menu By Parent ID.
  76. //Top Level Menu has always parent ID = 0
  77. foreach ($data as $v) {
  78. $this->menu[$v['menu_parent_id']][$v['menu_id']] = array(
  79. 'title' => $v['menu_title'],
  80. 'url' => $v['menu_link'],
  81. 'id' => $v['menu_id'],
  82. 'websites_id' => $v['websites_id']);
  83. $this->last_id = $v['menu_id'];
  84. }
  85. $this->menu[0][0]['id'] = 0; //last item::needed until fix found
  86. //array_pop($this->menu[0]);
  87. //$this->last_id = $this->last_id['']
  88. //print_r($this->last_id);
  89. return $this->_list();
  90. }
  91. function _list($child = array(), $space = 0)
  92. {
  93. //if $menu is no array or empty, return false
  94. if (!is_array($this->menu) || empty($this->menu)) return FALSE;
  95. $this->reduce_space = false;
  96. //Top Level Menu has always parent ID = 0
  97. if ((isset($this->menu[0]) && is_array($this->menu[0])) || (is_array($child) && !empty($child))) {
  98. //increment counter
  99. $this->counter++;
  100. //set current menu data array to holder
  101. $this->holder[$this->counter] = (is_array($child) && !empty($child)) ? $child : $this->menu[0];
  102. //get|set params
  103. $ul_param = $li_param = $a_param = ' class=""';
  104. if (!empty($this->li_param)) $li_param = ' ' . $this->li_param;
  105. if (!empty($this->a_param)) $a_param = ' ' . $this->a_param;
  106. if ($this->counter === 1 && !empty($this->ul_param)) {
  107. if (preg_match('/class=".*"/', $this->ul_param))
  108. $this->ul_param = preg_replace('/class="(.*)"/', 'class="$1 cfm_level' . $this->counter . '"', $this->ul_param);
  109. $ul_param = ' ' . $this->ul_param;
  110. }
  111. else {
  112. $ul_param = ' class="cfm_level' . $this->counter . '"';
  113. }
  114. $this->echo_list .= "\n" . str_repeat(' ', $space) . "<ul$ul_param>";
  115. //parent menu list
  116. foreach ($this->holder[$this->counter] as $v)
  117. {
  118. if ($v['id'] == 0) $this->is_last = TRUE; //Check to see if it is last one
  119. if ($v['id'] > 0) {
  120. //menu lists
  121. if ($v['id'] == $this->last_id) {
  122. //add class last
  123. $li_param = str_replace('class="', 'class="last ', $li_param);
  124. }
  125. $this->echo_list .= "\n" . str_repeat(' ', ($space + 3)) . "<li{$li_param}>";
  126. $this->echo_list .= "\n" . str_repeat(' ', ($space + 6));
  127. if (preg_match('@(http(s)?:\/\/|javascript::void\(0\);)@', $v['url'])) {
  128. $this->echo_list .= '<a' . $a_param . ' href="' . $v['url'] . '">' . $v['title'] . '</a>';
  129. }
  130. else
  131. {
  132. if ($this->menu_type != 'page')
  133. $this->echo_list .= anchor($this->menu_type . '/c/' . $v['url'], $v['title'], $a_param);
  134. else
  135. $this->echo_list .= anchor($v['url'], $v['title'], $a_param);
  136. }
  137. if ($this->rtn) {
  138. //START:: return menu item as array
  139. $this->menu_list[$v['id']] = array(
  140. 'id' => $v['id'],
  141. 'title' => str_repeat('-', $space) . $v['title'],
  142. 'url' => $v['url'],
  143. 'websites_id' => $v['websites_id']
  144. );
  145. //END::
  146. }
  147. if (isset($this->menu[$v['id']])) {
  148. $this->_list($this->menu[$v['id']], $space + 6);
  149. $this->reduce_space = false;
  150. } else {
  151. $this->reduce_space = true;
  152. }
  153. $this->echo_list .= "\n" . str_repeat(' ', ($space + 3)) . "</li>";
  154. }
  155. }
  156. if ($this->counter > 1) $this->counter--; //taking level counter back to previous one.
  157. $this->echo_list .= "\n" . str_repeat(' ', $space) . "</ul>";
  158. //just to make it look nice
  159. if ($this->reduce_space)
  160. if ($space >= 3) $space = $space - 3;
  161. if ($this->is_last) { //if all menu listed return or echo
  162. if ($this->rtn) { //if return=true for array | used for admin menu manager
  163. return $this->menu_list;
  164. } else {
  165. return $this->echo_list;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. ?>