admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Loader Class |
| 20 | * |
| 21 | * Loads views and files |
| 22 | * |
| 23 | * @package CodeIgniter |
| 24 | * @subpackage Libraries |
| 25 | * @author Rick Ellis |
| 26 | * @category Loader |
| 27 | * @link http://www.codeigniter.com/user_guide/libraries/loader.html |
| 28 | */ |
| 29 | class CI_Loader { |
| 30 | |
| 31 | var $ob_level; |
| 32 | var $cached_vars = array(); |
| 33 | var $helpers = array(); |
| 34 | var $plugins = array(); |
| 35 | var $scripts = array(); |
| 36 | var $languages = array(); |
| 37 | var $view_path = ''; |
| 38 | |
| 39 | /** |
| 40 | * Constructor |
| 41 | * |
| 42 | * Sets the path to the view files and gets the initial output |
| 43 | * buffering level |
| 44 | * |
| 45 | * @access public |
| 46 | */ |
| 47 | function CI_Loader() |
| 48 | { |
| 49 | $this->view_path = APPPATH.'views/'; |
| 50 | $this->ob_level = ob_get_level(); |
| 51 | |
| 52 | log_message('debug', "Loader Class Initialized"); |
| 53 | } |
| 54 | // END CI_Loader() |
| 55 | |
| 56 | // -------------------------------------------------------------------- |
| 57 | |
| 58 | /** |
| 59 | * Class Loader |
| 60 | * |
| 61 | * This function lets users load and instantiate classes. |
| 62 | * It is designed to be called from a user's app controllers. |
| 63 | * |
| 64 | * @access public |
| 65 | * @param string the name of the class |
| 66 | * @param mixed any initialization parameters |
| 67 | * @return void |
| 68 | */ |
| 69 | function library($class, $param = FALSE) |
| 70 | { |
| 71 | if ($class == '') |
| 72 | return; |
| 73 | |
| 74 | $obj =& get_instance(); |
| 75 | $obj->_ci_initialize($class, $param); |
| 76 | $obj->_ci_assign_to_models(); |
| 77 | } |
| 78 | // END library() |
| 79 | |
| 80 | // -------------------------------------------------------------------- |
| 81 | |
| 82 | /** |
| 83 | * Model Loader |
| 84 | * |
| 85 | * This function lets users load and instantiate models. |
| 86 | * |
| 87 | * @access public |
| 88 | * @param string the name of the class |
| 89 | * @param mixed any initialization parameters |
| 90 | * @return void |
| 91 | */ |
| 92 | function model($model, $name = '', $db_conn = FALSE) |
| 93 | { |
| 94 | if ($model == '') |
| 95 | return; |
| 96 | |
| 97 | $obj =& get_instance(); |
| 98 | $obj->_ci_load_model($model, $name, $db_conn); |
| 99 | } |
| 100 | // END library() |
| 101 | |
| 102 | // -------------------------------------------------------------------- |
| 103 | |
| 104 | /** |
| 105 | * Database Loader |
| 106 | * |
| 107 | * @access public |
| 108 | * @param string the DB credentials |
| 109 | * @param bool whether to return the DB object |
| 110 | * @param bool whether to enable active record (this allows us to override the config setting) |
| 111 | * @return mixed |
| 112 | */ |
| 113 | function database($db = '', $return = FALSE, $active_record = FALSE) |
| 114 | { |
| 115 | $obj =& get_instance(); |
| 116 | |
| 117 | if ($return === TRUE) |
| 118 | { |
| 119 | return $obj->_ci_init_database($db, TRUE, $active_record); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | $obj->_ci_init_database($db, FALSE, $active_record); |
| 124 | $obj->_ci_assign_to_models(); |
| 125 | } |
| 126 | } |
| 127 | // END database() |
| 128 | |
| 129 | // -------------------------------------------------------------------- |
| 130 | |
| 131 | /** |
| 132 | * Scaffolding Loader |
| 133 | * |
| 134 | * @access public |
| 135 | * @param string |
| 136 | * @return void |
| 137 | */ |
| 138 | function scaffolding($table = '') |
| 139 | { |
| 140 | if ($table == FALSE) |
| 141 | { |
| 142 | show_error('You must include the name of the table you would like access when you initialize scaffolding'); |
| 143 | } |
| 144 | |
| 145 | $obj =& get_instance(); |
| 146 | $obj->_ci_init_scaffolding($table); |
| 147 | } |
| 148 | // END scaffolding() |
| 149 | |
| 150 | // -------------------------------------------------------------------- |
| 151 | |
| 152 | /** |
| 153 | * Load View |
| 154 | * |
| 155 | * This function is used to load a "view" file. It has three parameters: |
| 156 | * |
| 157 | * 1. The name of the "view" file to be included. |
| 158 | * 2. An associative array of data to be extracted for use in the view. |
| 159 | * 3. TRUE/FALSE - whether to return the data or load it. In |
| 160 | * some cases it's advantageous to be able to retun data so that |
| 161 | * a developer can process it in some way. |
| 162 | * |
| 163 | * @access public |
| 164 | * @param string |
| 165 | * @param array |
| 166 | * @param bool |
| 167 | * @return void |
| 168 | */ |
| 169 | function view($view, $vars = array(), $return = FALSE) |
| 170 | { |
| 171 | return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return)); |
| 172 | } |
| 173 | // END view() |
| 174 | |
| 175 | // -------------------------------------------------------------------- |
| 176 | |
| 177 | /** |
| 178 | * Load File |
| 179 | * |
| 180 | * This is a generic file loader |
| 181 | * |
| 182 | * @access public |
| 183 | * @param string |
| 184 | * @param bool |
| 185 | * @return string |
| 186 | */ |
| 187 | function file($path, $return = FALSE) |
| 188 | { |
| 189 | return $this->_ci_load(array('path' => $path, 'return' => $return)); |
| 190 | } |
| 191 | // END file() |
| 192 | |
| 193 | // -------------------------------------------------------------------- |
| 194 | |
| 195 | /** |
| 196 | * Set Variables |
| 197 | * |
| 198 | * Once variables are set they become availabe within |
| 199 | * the controller class and its "view" files. |
| 200 | * |
| 201 | * @access public |
| 202 | * @param array |
| 203 | * @return void |
| 204 | */ |
| 205 | function vars($vars = array()) |
| 206 | { |
| 207 | $vars = $this->_ci_object_to_array($vars); |
| 208 | |
| 209 | if (is_array($vars) AND count($vars) > 0) |
| 210 | { |
| 211 | foreach ($vars as $key => $val) |
| 212 | { |
| 213 | $this->cached_vars[$key] = $val; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | // END vars() |
| 218 | |
| 219 | // -------------------------------------------------------------------- |
| 220 | |
| 221 | /** |
| 222 | * Load Helper |
| 223 | * |
| 224 | * This function loads the specified helper file. |
| 225 | * |
| 226 | * @access public |
| 227 | * @param mixed |
| 228 | * @return void |
| 229 | */ |
| 230 | function helper($helpers = array()) |
| 231 | { |
| 232 | if ( ! is_array($helpers)) |
| 233 | { |
| 234 | $helpers = array($helpers); |
| 235 | } |
| 236 | |
| 237 | foreach ($helpers as $helper) |
| 238 | { |
| 239 | if (isset($this->helpers[$helper])) |
| 240 | { |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper'); |
| 245 | |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 246 | if (file_exists(APPPATH.'helpers/'.$helper.EXT)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 247 | { |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 248 | include_once(APPPATH.'helpers/'.$helper.EXT); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 249 | } |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 250 | else |
| 251 | { |
| 252 | if (file_exists(BASEPATH.'helpers/'.$helper.EXT)) |
| 253 | { |
| 254 | include_once(BASEPATH.'helpers/'.$helper.EXT); |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | show_error('Unable to load the requested file: helpers/'.$helper.EXT); |
| 259 | } |
| 260 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 261 | |
| 262 | $this->helpers[$helper] = TRUE; |
| 263 | } |
| 264 | |
| 265 | log_message('debug', 'Helpers loaded: '.implode(', ', $helpers)); |
| 266 | } |
| 267 | // END helper() |
| 268 | |
| 269 | // -------------------------------------------------------------------- |
| 270 | |
| 271 | /** |
| 272 | * Load Helpers |
| 273 | * |
| 274 | * This is simply an alias to the above function in case the |
| 275 | * user has written the plural form of this function. |
| 276 | * |
| 277 | * @access public |
| 278 | * @param array |
| 279 | * @return void |
| 280 | */ |
| 281 | function helpers($helpers = array()) |
| 282 | { |
| 283 | $this->helper($helpers); |
| 284 | } |
| 285 | // END helpers() |
| 286 | |
| 287 | // -------------------------------------------------------------------- |
| 288 | |
| 289 | /** |
| 290 | * Load Plugin |
| 291 | * |
| 292 | * This function loads the specified plugin. |
| 293 | * |
| 294 | * @access public |
| 295 | * @param array |
| 296 | * @return void |
| 297 | */ |
| 298 | function plugin($plugins = array()) |
| 299 | { |
| 300 | if ( ! is_array($plugins)) |
| 301 | { |
| 302 | $plugins = array($plugins); |
| 303 | } |
| 304 | |
| 305 | foreach ($plugins as $plugin) |
| 306 | { |
| 307 | if (isset($this->plugins[$plugin])) |
| 308 | { |
| 309 | continue; |
| 310 | } |
| 311 | |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 312 | $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 313 | |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 314 | if (file_exists(APPPATH.'plugins/'.$plugin.EXT)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 315 | { |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 316 | include_once(APPPATH.'plugins/'.$plugin.EXT); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 317 | } |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 318 | else |
| 319 | { |
| 320 | if (file_exists(BASEPATH.'plugins/'.$plugin.EXT)) |
| 321 | { |
| 322 | include_once(BASEPATH.'plugins/'.$plugin.EXT); |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | show_error('Unable to load the requested file: plugins/'.$plugin.EXT); |
| 327 | } |
| 328 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 329 | |
| 330 | $this->plugins[$plugin] = TRUE; |
| 331 | } |
| 332 | |
| 333 | log_message('debug', 'Plugins loaded: '.implode(', ', $plugins)); |
| 334 | } |
| 335 | // END plugin() |
| 336 | |
| 337 | // -------------------------------------------------------------------- |
| 338 | |
| 339 | /** |
| 340 | * Load Script |
| 341 | * |
| 342 | * This function loads the specified include file from the |
| 343 | * application/scripts/ folder |
| 344 | * |
| 345 | * @access public |
| 346 | * @param array |
| 347 | * @return void |
| 348 | */ |
| 349 | function script($scripts = array()) |
| 350 | { |
| 351 | if ( ! is_array($scripts)) |
| 352 | { |
| 353 | $scripts = array($scripts); |
| 354 | } |
| 355 | |
| 356 | foreach ($scripts as $script) |
| 357 | { |
| 358 | if (isset($this->scripts[$script])) |
| 359 | { |
| 360 | continue; |
| 361 | } |
| 362 | |
| 363 | $script = strtolower(str_replace(EXT, '', $script)); |
| 364 | |
| 365 | if ( ! file_exists(APPPATH.'scripts/'.$script.EXT)) |
| 366 | { |
| 367 | show_error('Unable to load the requested script: scripts/'.$script.EXT); |
| 368 | } |
| 369 | |
| 370 | include_once(APPPATH.'scripts/'.$script.EXT); |
| 371 | |
| 372 | $this->scripts[$script] = TRUE; |
| 373 | } |
| 374 | |
| 375 | log_message('debug', 'Scripts loaded: '.implode(', ', $scripts)); |
| 376 | } |
| 377 | // END script() |
| 378 | |
| 379 | // -------------------------------------------------------------------- |
| 380 | |
| 381 | /** |
| 382 | * Load Plugins |
| 383 | * |
| 384 | * This is simply an alias to the above function in case the |
| 385 | * user has written the plural form of this function. |
| 386 | * |
| 387 | * @access public |
| 388 | * @param array |
| 389 | * @return void |
| 390 | */ |
| 391 | function plugins($plugins = array()) |
| 392 | { |
| 393 | $this->plugin($plugins); |
| 394 | } |
| 395 | // END plugins() |
| 396 | |
| 397 | // -------------------------------------------------------------------- |
| 398 | |
| 399 | /** |
| 400 | * Loads a language file |
| 401 | * |
| 402 | * @access public |
| 403 | * @param string |
| 404 | * @return void |
| 405 | */ |
| 406 | function language($file = '', $lang = '', $return = FALSE) |
| 407 | { |
| 408 | $obj =& get_instance(); |
| 409 | return $obj->lang->load($file, $lang, $return); |
| 410 | } |
| 411 | // END language() |
| 412 | |
| 413 | // -------------------------------------------------------------------- |
| 414 | |
| 415 | /** |
| 416 | * Loads a config file |
| 417 | * |
| 418 | * @access public |
| 419 | * @param string |
| 420 | * @return void |
| 421 | */ |
| 422 | function config($file = '') |
| 423 | { |
| 424 | $obj =& get_instance(); |
| 425 | $obj->config->load($file); |
| 426 | } |
| 427 | // END config() |
| 428 | |
| 429 | // -------------------------------------------------------------------- |
| 430 | |
| 431 | /** |
| 432 | * Set the Path to the "views" folder |
| 433 | * |
| 434 | * @access private |
| 435 | * @param string |
| 436 | * @return void |
| 437 | */ |
| 438 | function _ci_set_view_path($path) |
| 439 | { |
| 440 | $this->view_path = $path; |
| 441 | } |
| 442 | // END _ci_set_view_path() |
| 443 | |
| 444 | // -------------------------------------------------------------------- |
| 445 | |
| 446 | /** |
| 447 | * Loader |
| 448 | * |
| 449 | * This function isn't called directly. It's called from |
| 450 | * the two functions above. It's used to load views and files |
| 451 | * |
| 452 | * @access private |
| 453 | * @param array |
| 454 | * @return void |
| 455 | */ |
| 456 | function _ci_load($data) |
| 457 | { |
| 458 | $OUT =& _load_class('CI_Output'); |
| 459 | |
| 460 | // This allows anything loaded using $this->load (viwes, files, etc.) |
| 461 | // to become accessible from within the Controller and Model functions. |
| 462 | $obj =& get_instance(); |
| 463 | foreach ($obj->ci_is_loaded as $val) |
| 464 | { |
| 465 | if ( ! isset($this->$val)) |
| 466 | { |
| 467 | $this->$val =& $obj->$val; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | // Set the default data variables |
| 472 | foreach (array('view', 'vars', 'path', 'return') as $val) |
| 473 | { |
| 474 | $$val = ( ! isset($data[$val])) ? FALSE : $data[$val]; |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * Extract and cached variables |
| 479 | * |
| 480 | * You can either set variables using the dedicated |
| 481 | * $this->load_vars() function or via the second |
| 482 | * parameter of this function. We'll |
| 483 | * merge the two types and cache them so that |
| 484 | * views that are embedded within other views |
| 485 | * can have access to these variables. |
| 486 | * |
| 487 | */ |
| 488 | |
| 489 | if (is_array($vars)) |
| 490 | { |
| 491 | $this->cached_vars = array_merge($this->cached_vars, $vars); |
| 492 | } |
| 493 | extract($this->cached_vars); |
| 494 | |
| 495 | // Set the path to the requested file |
| 496 | if ($path == '') |
| 497 | { |
| 498 | $ext = pathinfo($view, PATHINFO_EXTENSION); |
admin | 6ac4bea | 2006-09-02 17:46:15 +0000 | [diff] [blame] | 499 | $file = ($ext == '') ? $view.EXT : $view; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 500 | $path = $this->view_path.$file; |
| 501 | } |
| 502 | else |
| 503 | { |
| 504 | $x = explode('/', $path); |
| 505 | $file = end($x); |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Buffer the output |
| 510 | * |
| 511 | * We buffer the output for two reasons: |
| 512 | * 1. Speed. You get a significant speed boost. |
| 513 | * 2. So that the final rendered template can be |
| 514 | * post-processed by the output class. Why do we |
| 515 | * need post processing? For one thing, in order to |
| 516 | * show the elapsed page load time. Unless we |
| 517 | * can intercept the content right before it's sent to |
| 518 | * the browser and then stop the timer, it won't be acurate. |
| 519 | * |
| 520 | */ |
| 521 | |
| 522 | if ( ! file_exists($path)) |
| 523 | { |
| 524 | show_error('Unable to load the requested file: '.$file); |
| 525 | } |
| 526 | |
| 527 | ob_start(); |
| 528 | |
| 529 | include($path); |
| 530 | log_message('debug', 'File loaded: '.$path); |
| 531 | |
| 532 | // Return the file data if requested to |
| 533 | if ($return === TRUE) |
| 534 | { |
| 535 | $buffer = ob_get_contents(); |
| 536 | ob_end_clean(); |
| 537 | |
| 538 | return $buffer; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Flush the buffer... or buff the flusher? |
| 543 | * |
| 544 | * In order to permit templates (views) to be nested within |
| 545 | * other views, we need to flush the content back out whenever |
| 546 | * we are beyond the first level of output buffering so that |
| 547 | * it can be seen and included properly by the first included |
| 548 | * template and any subsequent ones. Oy! |
| 549 | * |
| 550 | */ |
| 551 | if (ob_get_level() > $this->ob_level + 1) |
| 552 | { |
| 553 | ob_end_flush(); |
| 554 | } |
| 555 | else |
| 556 | { |
| 557 | $OUT->set_output(ob_get_contents()); |
| 558 | ob_end_clean(); |
| 559 | } |
| 560 | } |
| 561 | // END _load() |
| 562 | |
| 563 | // -------------------------------------------------------------------- |
| 564 | |
| 565 | /** |
| 566 | * Autoloader |
| 567 | * |
| 568 | * The config/autoload.php file contains an array that permits sub-systems, |
| 569 | * plugins, and helpers to be loaded automatically. |
| 570 | * |
| 571 | * @access private |
| 572 | * @param array |
| 573 | * @return void |
| 574 | */ |
| 575 | function _ci_autoloader($autoload) |
| 576 | { |
| 577 | if ($autoload === FALSE) |
| 578 | { |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | foreach (array('helper', 'plugin', 'script') as $type) |
| 583 | { |
| 584 | if (isset($autoload[$type])) |
| 585 | { |
admin | 2e5872a | 2006-09-06 02:32:55 +0000 | [diff] [blame] | 586 | $this->$type($autoload[$type]); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | } |
| 590 | // END _ci_autoloader() |
| 591 | |
| 592 | // -------------------------------------------------------------------- |
| 593 | |
| 594 | /** |
| 595 | * Object to Array |
| 596 | * |
| 597 | * Takes an object as input and convers the class variables to array key/vals |
| 598 | * |
| 599 | * @access public |
| 600 | * @param object |
| 601 | * @return array |
| 602 | */ |
| 603 | function _ci_object_to_array($object) |
| 604 | { |
admin | 2e5872a | 2006-09-06 02:32:55 +0000 | [diff] [blame] | 605 | return (is_object($object)) ? get_object_vars($object) : $object; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 606 | } |
admin | 2e5872a | 2006-09-06 02:32:55 +0000 | [diff] [blame] | 607 | // END _ci_object_to_array() |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 608 | |
| 609 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 610 | ?> |