PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/helpers/breadcrumb_helper.php

https://bitbucket.org/ardinotow/autocrumb/
PHP | 377 lines | 256 code | 43 blank | 78 comment | 70 complexity | 8a760f177bb2cf96b41db4529b1a4bcd MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Breadcrumb helper
  4. *
  5. * Features:
  6. * - Easy integration. Just put <?php echo set_breadcrumb(); ?> in view files.
  7. * - Can use any delimiter.
  8. * - Easy to replace any unproper link name.
  9. * - Easy to hide any link by it's name or number segment.
  10. * - Return the breadcrumb in a list (<ul><li></li></ul>) or something else.
  11. * - Auto link beauty.
  12. * - Can unlink last segment of breadcrumb.
  13. * - Multilanguage support.
  14. * - Many more...
  15. *
  16. * Installation:
  17. * 1. Put breadcrumb_helper.php to application/helpers.
  18. * 2. Put breadcrumb.php to application/config.
  19. * 3. Load the helper either in your controller or in autoload config.
  20. * In your controller : $this->load->helper('breadcrumb') OR
  21. * In autoload : $autoload['helper'] = array('breadcrumb')
  22. * 4. Add these line to your view file: <?php echo set_breadcrumb(); ?>. I suggest that you put it on master template
  23. * so that it can save time as you don't need to add text in every view page.
  24. * 5. Change the configuration as you need.
  25. *
  26. * @package Breadcrumb
  27. * @subpackage Helpers
  28. * @category Helpers
  29. * @author Ardinoto Wahono
  30. * @version 12.01.1 (CI 2.x & CI 1.x. Reversioning. Published on January,05 2012, release 1)
  31. * @copyright Copyright (c) 2009-2012 Ardinoto Wahono, WAH-IT Web Division
  32. *
  33. * Permission is hereby granted, free of charge, to any person obtaining a copy
  34. * of this software and associated documentation files (the "Software"), to deal
  35. * in the Software without restriction, including without limitation the rights
  36. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  37. * copies of the Software, and to permit persons to whom the Software is
  38. * furnished to do so, subject to the following conditions:
  39. *
  40. * The above copyright notice and this permission notice shall be included in
  41. * all copies or substantial portions of the Software.
  42. *
  43. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  44. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  45. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  46. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  47. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  48. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  49. * THE SOFTWARE.
  50. */
  51. /**
  52. * Patch for PHP 5 - 5.2
  53. * @author Kromack
  54. * @link http://codeigniter.com/forums/viewreply/694827/
  55. */
  56. if (!function_exists('array_replace'))
  57. {
  58. function array_replace(array &$array, array &$array1)
  59. {
  60. foreach($array as $k=>$v)
  61. {
  62. if(array_key_exists($k, $array1))
  63. {
  64. $array[$k] = $array1[$k];
  65. }
  66. }
  67. return $array;
  68. }
  69. }
  70. if ( ! function_exists('set_breadcrumb'))
  71. {
  72. function set_breadcrumb($delimiter_config = '', $exclude = '')
  73. {
  74. $CI =& get_instance();
  75. $CI->load->helper('url');
  76. $CI->lang->load('breadcrumb');
  77. $CI->config->load('breadcrumb');
  78. // Load configuration
  79. $ci_version = $CI->config->item('codeigniter_version');
  80. $attr_home = $CI->config->item('attr_home');
  81. $unlink_home = $CI->config->item('unlink_home');
  82. if (empty($exclude))
  83. {
  84. $exclude = $CI->config->item('exclude');
  85. }
  86. $exclude_segment = $CI->config->item('exclude_segment');
  87. $replacer_default = $CI->config->item('replacer');
  88. $partial_replace = $CI->config->item('partial_replace');
  89. $uri = rtrim($CI->uri->uri_string(),'/');
  90. $uri_array_original = explode("/", $uri);
  91. // cahva's fix (http://codeigniter.com/forums/viewreply/855097/)
  92. $uri_array_cnt = count($uri_array_original);
  93. if (config_item('hide_number_on_last_segment') && isset($uri_array_original[$uri_array_cnt-1]) && is_numeric($uri_array_original[$uri_array_cnt-1]))
  94. {
  95. array_pop($uri_array_original);
  96. }
  97. // <-- End cahva's fix (http://codeigniter.com/forums/viewreply/855097/)
  98. // If last segment is a number ?
  99. $show_last_number = -1;
  100. $number_array = count($uri_array_original);
  101. if (! $CI->config->item('hide_number_on_last_segment'))
  102. {
  103. $l_array = $number_array - 1; // last array number
  104. if (preg_match("/^[0-9]/", $uri_array_original[$l_array]) AND ! preg_match("/[a-zA-Z]+/", $uri_array_original[$l_array]))
  105. {
  106. $show_last_number = $l_array;
  107. }
  108. }
  109. // Find segments uri that only contain a number
  110. if ($CI->config->item('hide_number'))
  111. {
  112. foreach($uri_array_original as $key => $value)
  113. {
  114. // exclude number but keep number where positioned in the last segment
  115. if (preg_match("/^[0-9]/", $value) AND ! preg_match("/[a-zA-Z]+/", $value) AND $key != $show_last_number)
  116. {
  117. $exclude_segment = array_merge($exclude_segment, array($key));
  118. }
  119. }
  120. }
  121. // Preparing the replacer, add exclude to replacer array
  122. foreach ($exclude as $value)
  123. {
  124. $prep_exclude[$value] = ''; //if exclude then it's value is set to null
  125. }
  126. $replacer = $replacer_default + $prep_exclude;
  127. // Find uri segment from $replacer and $exclude_segment
  128. $replacer_null = array();
  129. foreach ($replacer as $key => $value)
  130. {
  131. if (empty($value))
  132. {
  133. //$replacer_null[] = array_search($key, $uri_array_original, TRUE);
  134. $replacer_null[] = array_search($key, $uri_array_original);
  135. }
  136. }
  137. $skip_key = array_merge($replacer_null, $exclude_segment);
  138. $uri_array = $uri_array_original;
  139. // Change link name as mentioned on $replacer
  140. foreach ($replacer as $key => $value)
  141. {
  142. if ($value && in_array($key, $uri_array_original, TRUE))
  143. {
  144. $key_uri = array_search($key, $uri_array_original, TRUE);
  145. // Add multilanguage
  146. if (! is_array($value) && $CI->config->item('multilang'))
  147. {
  148. if ($CI->lang->line($value)) {
  149. $value = ucwords($CI->lang->line($value));
  150. }
  151. }
  152. $replacement = array($key_uri => $value);
  153. $uri_array = array_replace($uri_array, $replacement);
  154. }
  155. }
  156. // Set wrapper
  157. $wrapper = explode("|", $CI->config->item('wrapper'));
  158. $wrapper_inline = explode("|", $CI->config->item('wrapper_inline'));
  159. if ( ! $CI->config->item('use_wrapper'))
  160. {
  161. $wrapper = array('', '');
  162. $wrapper_inline = array('', '');
  163. }
  164. // Begin writing breadcrumb string
  165. $init_link = $CI->config->item('set_home');
  166. if ($init_link != "")
  167. {
  168. if ($CI->config->item('multilang'))
  169. {
  170. $init_link = $CI->lang->line('set_home');
  171. }
  172. $str_first = $wrapper[0].$wrapper_inline[0].anchor('', $init_link, $attr_home).$wrapper_inline[1];
  173. if ($unlink_home)
  174. {
  175. $str_first = $wrapper[0].$wrapper_inline[0].$init_link.$wrapper_inline[1];
  176. }
  177. } else {
  178. $str_first = $wrapper[0];
  179. }
  180. $segment = '';
  181. $i = 0;
  182. foreach ($uri_array as $value)
  183. {
  184. if ($i > 0 OR $ci_version == '2.x')
  185. {
  186. $segment .= $uri_array_original[$i].'/';
  187. // If replace value is an array
  188. if (! in_array($i, $skip_key, TRUE) && is_array($value)) // Skip link if replace value is null
  189. {
  190. $number_added_value_array = count($value);
  191. foreach ($value as $pair_values)
  192. {
  193. $pv_array = explode("|", $pair_values);
  194. $val_url = $pv_array[0];
  195. $number_pv_array = count($pv_array);
  196. if ($number_pv_array == 1)
  197. {
  198. $val_name = $pv_array[0];
  199. }
  200. else
  201. {
  202. $val_name = $pv_array[1];
  203. }
  204. // Add multilanguage
  205. if ($CI->config->item('multilang'))
  206. {
  207. if ($CI->lang->line($val_name)) {
  208. $val_name = ucwords($CI->lang->line($val_name));
  209. }
  210. }
  211. // Look up for partial replace
  212. if (! empty($partial_replace))
  213. {
  214. foreach ($partial_replace as $pkey => $pvalue)
  215. {
  216. if ($CI->config->item('multilang'))
  217. {
  218. $lang_pvalue = $CI->lang->line($pvalue)?$CI->lang->line($pvalue):$CI->lang->line($pkey);
  219. $preplace = ' '.$lang_pvalue.'_';
  220. } else {
  221. $preplace = ' '.$pvalue.'_';
  222. }
  223. if (substr_count($val_name, $pkey) > 0)
  224. {
  225. $val_name = str_replace($pkey, $preplace, $val_name);
  226. }
  227. }
  228. }
  229. // Url preparation
  230. // If no url define (array key is empty)
  231. if ($number_pv_array == 1 || $val_url == $uri_array_original[$i])
  232. {
  233. $new_segment_url = $segment;
  234. }
  235. else if ($val_url[0] == '/')
  236. {
  237. $new_segment_url = base_url().substr($val_url, 1);
  238. }
  239. else
  240. {
  241. $new_segment_url = $segment.$val_url;
  242. }
  243. $str_link[] = $new_segment_url;
  244. $str_name[] = ucwords($val_name);
  245. }
  246. }
  247. else if (! in_array($i, $skip_key, TRUE)) // If value is NOT an array
  248. {
  249. // Add multilanguage
  250. if ($CI->config->item('multilang'))
  251. {
  252. if ($CI->lang->line($value)) {
  253. $value = ucwords($CI->lang->line($value));
  254. }
  255. }
  256. // Look up for partial replace
  257. if (! empty($partial_replace))
  258. {
  259. foreach ($partial_replace as $pkey => $pvalue)
  260. {
  261. if ($CI->config->item('multilang'))
  262. {
  263. $lang_pvalue = $CI->lang->line($pvalue)?$CI->lang->line($pvalue):$CI->lang->line($pkey);
  264. $preplace = ' '.$lang_pvalue.'_';
  265. } else {
  266. $preplace = ' '.$pvalue.'_';
  267. }
  268. if (substr_count($value, $pkey) > 0)
  269. {
  270. $value = str_replace($pkey, $preplace, $value);
  271. }
  272. }
  273. }
  274. // Auto link make over
  275. if (strpos($value, "_") OR strpos($value, "-"))
  276. {
  277. $char_to_replace = $CI->config->item('strip_characters');
  278. $value = ucwords(strtolower(str_replace($char_to_replace, " ", $value)));
  279. if ($CI->config->item('strip_regexp'))
  280. {
  281. foreach($CI->config->item('strip_regexp') as $exp)
  282. {
  283. $value = preg_replace($exp, '', $value);
  284. }
  285. }
  286. }
  287. $str_link[] = $segment;
  288. $str_name[] = ucwords($value);
  289. }
  290. }
  291. $i++;
  292. }
  293. $str_last = $wrapper[1];
  294. $str = $str_first;
  295. if (isset($str_name)) {
  296. $breadcrumb_number = count($str_name);
  297. if ($breadcrumb_number > 0) {
  298. $i = 0;
  299. foreach ($str_name as $key => $val) {
  300. // If home is hidden then don't show first delimiter
  301. if ( $i == 0 && ($str == '' || $str == $wrapper[0]) ) {
  302. $delimiter = '';
  303. } elseif (empty($delimiter_config)) {
  304. $delimiter = $CI->config->item('delimiter');
  305. } else {
  306. $delimiter = $delimiter_config;
  307. }
  308. if ($val != '') {
  309. if ($key == $breadcrumb_number-1 && $CI->config->item('unlink_last_segment'))
  310. {
  311. $str .= $delimiter.$wrapper_inline[0].ucwords($val).$wrapper_inline[1];
  312. } else {
  313. $str .= $delimiter.$wrapper_inline[0].anchor($str_link[$key], $val).$wrapper_inline[1];
  314. }
  315. }
  316. $i++;
  317. }
  318. }
  319. }
  320. $str .= $str_last;
  321. clear_breadcrumb();
  322. return $str;
  323. }
  324. }
  325. if ( ! function_exists('clear_breadcrumb'))
  326. {
  327. function clear_breadcrumb()
  328. {
  329. unset($wrapper_inline);
  330. unset($wrapper);
  331. }
  332. }
  333. /* End of file breadcrumb_helper.php */
  334. /* Location: ./application/helpers/breadcrumb_helper.php */