|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Project codeigniter-framework |
| 4 | + * Created by PhpStorm |
| 5 | + * User: 713uk13m <dev@nguyenanhung.com> |
| 6 | + * Copyright: 713uk13m <dev@nguyenanhung.com> |
| 7 | + * Date: 09/03/2023 |
| 8 | + * Time: 23:55 |
| 9 | + */ |
| 10 | +defined('BASEPATH') or exit('No direct script access allowed'); |
| 11 | + |
| 12 | +/** |
| 13 | + * HTML Breadcrumb Generating Class |
| 14 | + * |
| 15 | + * @author 713uk13m <dev@nguyenanhung.com> |
| 16 | + * @copyright 713uk13m <dev@nguyenanhung.com> |
| 17 | + * |
| 18 | + * ------------ Example Construction |
| 19 | + * Load library breadcrumb |
| 20 | + * $this->load->library('breadcrumb'); |
| 21 | + * |
| 22 | + * Custom style |
| 23 | + * $template = [ |
| 24 | + * 'tag_open' => '<ol class="breadcrumb">', |
| 25 | + * 'crumb_open' => '<li class="breadcrumb-item">', |
| 26 | + * 'crumb_active' => '<li class="breadcrumb-item active" aria-current="page">' |
| 27 | + * ]; |
| 28 | + * $this->breadcrumb->set_template($template); |
| 29 | + * |
| 30 | + * Add items |
| 31 | + * $this->breadcrumb->add_item($breadcrumb_items); |
| 32 | + * |
| 33 | + * Generate breadcrumb |
| 34 | + * $data['breadcrumb_bootstrap_style'] = $this->breadcrumb->generate(); |
| 35 | + */ |
| 36 | +class CI_Breadcrumb |
| 37 | +{ |
| 38 | + protected $CI; |
| 39 | + |
| 40 | + private $breadcrumb = array(); |
| 41 | + |
| 42 | + /** |
| 43 | + * breadcrumb layout template |
| 44 | + * |
| 45 | + * @var array |
| 46 | + */ |
| 47 | + public $template = null; |
| 48 | + |
| 49 | + /** |
| 50 | + * Newline setting |
| 51 | + * |
| 52 | + * @var string |
| 53 | + */ |
| 54 | + public $newline = "\n"; |
| 55 | + |
| 56 | + /** |
| 57 | + * Set the template from the breadcrumb config file if it exists |
| 58 | + * |
| 59 | + * @param array $config (default: array()) |
| 60 | + * |
| 61 | + * @return void |
| 62 | + */ |
| 63 | + public function __construct($config = array()) |
| 64 | + { |
| 65 | + $this->CI =& get_instance(); |
| 66 | + |
| 67 | + $this->CI->load->helper('url'); |
| 68 | + |
| 69 | + // initialize config |
| 70 | + foreach ($config as $key => $val) { |
| 71 | + $this->template[$key] = $val; |
| 72 | + } |
| 73 | + |
| 74 | + log_message('info', 'Breadcrumb Class Initialized'); |
| 75 | + } |
| 76 | + |
| 77 | + // -------------------------------------------------------------------- |
| 78 | + |
| 79 | + /** |
| 80 | + * Set the template |
| 81 | + * |
| 82 | + * @param array $template |
| 83 | + * |
| 84 | + * @return bool |
| 85 | + */ |
| 86 | + public function set_template($template) |
| 87 | + { |
| 88 | + if (!is_array($template)) { |
| 89 | + return false; |
| 90 | + } else { |
| 91 | + $this->template = $template; |
| 92 | + |
| 93 | + return true; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // -------------------------------------------------------------------- |
| 98 | + |
| 99 | + /** |
| 100 | + * Function add_item |
| 101 | + * |
| 102 | + * @param $args |
| 103 | + * |
| 104 | + * @return false|void |
| 105 | + * @author : 713uk13m <dev@nguyenanhung.com> |
| 106 | + * @copyright: 713uk13m <dev@nguyenanhung.com> |
| 107 | + * @time : 09/03/2023 57:18 |
| 108 | + */ |
| 109 | + public function add_item($args) |
| 110 | + { |
| 111 | + if (!is_array($args) or empty($args)) { |
| 112 | + return false; |
| 113 | + } else { |
| 114 | + foreach ($args as $key => $value) { |
| 115 | + $href = site_url($value); |
| 116 | + |
| 117 | + $this->breadcrumb[$href] = array( |
| 118 | + 'page' => $key, |
| 119 | + 'href' => $href |
| 120 | + ); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // -------------------------------------------------------------------- |
| 126 | + |
| 127 | + /** |
| 128 | + * Generate the breadcrumb |
| 129 | + * |
| 130 | + * @param mixed $breadcrumb_data |
| 131 | + * |
| 132 | + * @return string|void |
| 133 | + */ |
| 134 | + public function generate() |
| 135 | + { |
| 136 | + if ($this->breadcrumb) { |
| 137 | + // Compile and validate the template date |
| 138 | + $this->_compile_template(); |
| 139 | + |
| 140 | + // Build the breadcrumb |
| 141 | + $out = $this->template['tag_open'] . $this->newline; |
| 142 | + |
| 143 | + foreach ($this->breadcrumb as $key => $value) { |
| 144 | + $keys = array_keys($this->breadcrumb); |
| 145 | + |
| 146 | + if (end($keys) == $key) { |
| 147 | + $out .= $this->template['crumb_active'] . $value['page'] . $this->template['crumb_close'] . $this->newline; |
| 148 | + } else { |
| 149 | + $out .= $this->template['crumb_open']; |
| 150 | + $out .= anchor($value['href'], $value['page']); |
| 151 | + $out .= $this->template['crumb_close']; |
| 152 | + $out .= $this->newline; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + $out .= $this->template['tag_close'] . $this->newline; |
| 157 | + |
| 158 | + // Clear Breadcrumb class properties before generating the breadcrumb |
| 159 | + $this->clear(); |
| 160 | + |
| 161 | + return $out; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // -------------------------------------------------------------------- |
| 166 | + |
| 167 | + /** |
| 168 | + * Clears the breadcrumb arrays. Useful if multiple tables are being generated |
| 169 | + * |
| 170 | + * @return self |
| 171 | + */ |
| 172 | + public function clear() |
| 173 | + { |
| 174 | + $this->breadcrumb = array(); |
| 175 | + |
| 176 | + return $this; |
| 177 | + } |
| 178 | + |
| 179 | + // -------------------------------------------------------------------- |
| 180 | + |
| 181 | + /** |
| 182 | + * Compile Template |
| 183 | + * |
| 184 | + * @return void |
| 185 | + */ |
| 186 | + protected function _compile_template() |
| 187 | + { |
| 188 | + if ($this->template === null) { |
| 189 | + $this->template = $this->_default_template(); |
| 190 | + |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + $this->temp = $this->_default_template(); |
| 195 | + |
| 196 | + foreach (array('tag_open', 'tag_close', 'crumb_open', 'crumb_close', 'crumb_active') as $val) { |
| 197 | + if (!isset($this->template[$val])) { |
| 198 | + $this->template[$val] = $this->temp[$val]; |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + // -------------------------------------------------------------------- |
| 204 | + |
| 205 | + /** |
| 206 | + * Default Template |
| 207 | + * |
| 208 | + * @return array |
| 209 | + */ |
| 210 | + protected function _default_template() |
| 211 | + { |
| 212 | + return array( |
| 213 | + 'tag_open' => '<ol>', |
| 214 | + 'tag_close' => '</ol>', |
| 215 | + 'crumb_open' => '<li>', |
| 216 | + 'crumb_close' => '</li>', |
| 217 | + 'crumb_active' => '<li class="active">' |
| 218 | + ); |
| 219 | + } |
| 220 | +} |
0 commit comments