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 | * Code Igniter Application Controller Class |
| 20 | * |
| 21 | * This class object is the the super class the every library in |
| 22 | * Code Igniter will be assigned to. |
| 23 | * |
| 24 | * @package CodeIgniter |
| 25 | * @subpackage Libraries |
| 26 | * @category Libraries |
| 27 | * @author Rick Ellis |
| 28 | * @link http://www.codeigniter.com/user_guide/general/controllers.html |
| 29 | */ |
| 30 | class Controller extends CI_Base { |
| 31 | |
| 32 | var $ci_is_loaded = array(); |
| 33 | var $_ci_models = array(); |
| 34 | var $_ci_scaffolding = FALSE; |
| 35 | var $_ci_scaff_table = FALSE; |
| 36 | |
| 37 | /** |
| 38 | * Constructor |
| 39 | * |
| 40 | * Loads the base classes needed to run CI, and runs the "autoload" |
| 41 | * routine which loads the systems specified in the "autoload" config file. |
| 42 | */ |
| 43 | function Controller() |
| 44 | { |
| 45 | parent::CI_Base(); |
| 46 | |
| 47 | // Assign all the class objects that were instantiated by the |
| 48 | // front controller to local class variables so that CI can be |
| 49 | // run as one big super object. |
| 50 | $this->_ci_assign_core(); |
| 51 | |
| 52 | // Load everything specified in the autoload.php file |
| 53 | $this->load->_ci_autoloader($this->_ci_autoload()); |
| 54 | |
| 55 | // This allows anything loaded using $this->load (viwes, files, etc.) |
| 56 | // to become accessible from within the Controller class functions. |
| 57 | foreach ($this->ci_is_loaded as $val) |
| 58 | { |
| 59 | $this->load->$val =& $this->$val; |
| 60 | } |
| 61 | |
| 62 | log_message('debug', "Controller Class Initialized"); |
| 63 | } |
| 64 | // END Controller() |
| 65 | |
| 66 | // -------------------------------------------------------------------- |
| 67 | |
| 68 | /** |
| 69 | * Initialization Handler |
| 70 | * |
| 71 | * Looks for the existence of a handler method and calls it |
| 72 | * |
| 73 | * @access private |
| 74 | * @param string the item that is being loaded |
| 75 | * @param mixed any additional parameters |
| 76 | * @return void |
| 77 | */ |
| 78 | function _ci_initialize($what, $params = FALSE) |
| 79 | { |
| 80 | $method = '_ci_init_'.strtolower(str_replace(EXT, '', $what)); |
| 81 | |
| 82 | if ( ! method_exists($this, $method)) |
| 83 | { |
| 84 | $method = substr($method, 4); |
| 85 | |
| 86 | if ( ! file_exists(APPPATH.'init/'.$method.EXT)) |
| 87 | { |
| 88 | if ( ! file_exists(BASEPATH.'init/'.$method.EXT)) |
| 89 | { |
| 90 | log_message('error', "Unable to load the requested class: ".$what); |
| 91 | show_error("Unable to load the class: ".$what); |
| 92 | } |
| 93 | |
| 94 | include(BASEPATH.'init/'.$method.EXT); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | include(APPPATH.'init/'.$method.EXT); |
| 99 | } |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | if ($params === FALSE) |
| 104 | { |
| 105 | $this->$method(); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | $this->$method($params); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | // END _ci_initialize() |
| 114 | |
| 115 | // -------------------------------------------------------------------- |
| 116 | |
| 117 | /** |
| 118 | * Loads and instantiates the requested model class |
| 119 | * |
| 120 | * @access private |
| 121 | * @param string |
| 122 | * @return array |
| 123 | */ |
| 124 | function _ci_load_model($model, $name = '', $db_conn = FALSE) |
| 125 | { |
| 126 | if ($name == '') |
| 127 | { |
| 128 | $name = $model; |
| 129 | } |
admin | b4473d4 | 2006-08-27 15:46:31 +0000 | [diff] [blame] | 130 | |
admin | 0d29605 | 2006-08-27 15:48:38 +0000 | [diff] [blame] | 131 | $obj =& get_instance(); |
| 132 | if (in_array($name, $obj->_ci_models)) |
admin | b4473d4 | 2006-08-27 15:46:31 +0000 | [diff] [blame] | 133 | { |
| 134 | return; |
| 135 | } |
| 136 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 137 | if (isset($this->$name)) |
| 138 | { |
| 139 | show_error('The model name you are loading is the name of a resource that is already being used: '.$name); |
| 140 | } |
| 141 | |
| 142 | $model = strtolower($model); |
| 143 | |
| 144 | if ( ! file_exists(APPPATH.'models/'.$model.EXT)) |
| 145 | { |
| 146 | show_error('Unable to locate the model you have specified: '.$model); |
| 147 | } |
| 148 | |
| 149 | if ($db_conn !== FALSE) |
| 150 | { |
| 151 | if ($db_conn === TRUE) |
| 152 | $db_conn = ''; |
| 153 | |
| 154 | $this->_ci_init_database($db_conn, FALSE, TRUE); |
| 155 | } |
| 156 | |
| 157 | if ( ! class_exists('Model')) |
| 158 | { |
| 159 | require_once(BASEPATH.'libraries/Model'.EXT); |
| 160 | } |
| 161 | |
| 162 | require_once(APPPATH.'models/'.$model.EXT); |
| 163 | |
| 164 | $model = ucfirst($model); |
| 165 | $this->$name = new $model(); |
| 166 | $this->_ci_models[] = $name; |
| 167 | $this->_ci_assign_to_models(); |
| 168 | } |
| 169 | // END _ci_load_model() |
| 170 | |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
| 175 | * Assign to Models |
| 176 | * |
| 177 | * Makes sure that anything loaded by the loader class (libraries, plugins, etc.) |
| 178 | * will be available to modles, if any exist. |
| 179 | * |
| 180 | * @access public |
| 181 | * @param object |
| 182 | * @return array |
| 183 | */ |
| 184 | function _ci_assign_to_models() |
| 185 | { |
| 186 | $obj =& get_instance(); |
| 187 | if (count($obj->_ci_models) == 0) |
| 188 | { |
| 189 | return; |
| 190 | } |
| 191 | foreach ($obj->_ci_models as $model) |
| 192 | { |
| 193 | $obj->$model->_assign_libraries(); |
| 194 | } |
| 195 | } |
| 196 | // END _ci_assign_to_models() |
| 197 | |
| 198 | |
| 199 | // -------------------------------------------------------------------- |
| 200 | |
| 201 | /** |
| 202 | * Auto-initialize Core Classes |
| 203 | * |
| 204 | * This initializes the core systems that are specified in the |
| 205 | * libraries/autoload.php file, as well as the systems specified in |
| 206 | * the $autoload class array above. |
| 207 | * |
| 208 | * It returns the "autoload" array so we can pass it to the Loader |
| 209 | * class since it needs to autoload plugins and helper files |
| 210 | * |
| 211 | * The config/autoload.php file contains an array that permits |
| 212 | * sub-systems to be loaded automatically. |
| 213 | * |
| 214 | * @access private |
| 215 | * @return array |
| 216 | */ |
| 217 | function _ci_autoload() |
| 218 | { |
| 219 | include_once(APPPATH.'config/autoload'.EXT); |
| 220 | |
| 221 | if ( ! isset($autoload)) |
| 222 | { |
| 223 | return FALSE; |
| 224 | } |
| 225 | |
| 226 | if (count($autoload['config']) > 0) |
| 227 | { |
| 228 | foreach ($autoload['config'] as $key => $val) |
| 229 | { |
| 230 | $this->config->load($val); |
| 231 | } |
| 232 | } |
| 233 | unset($autoload['config']); |
| 234 | |
admin | d48ef1c | 2006-09-21 06:22:05 +0000 | [diff] [blame] | 235 | // A little tweak to remain backward compatible |
| 236 | // The $autoload['core'] item was deprecated |
| 237 | if ( ! isset($autoload['libraries'])) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 238 | { |
admin | d48ef1c | 2006-09-21 06:22:05 +0000 | [diff] [blame] | 239 | $autoload['libraries'] = $autoload['core']; |
| 240 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 241 | } |
| 242 | |
admin | d48ef1c | 2006-09-21 06:22:05 +0000 | [diff] [blame] | 243 | foreach ($autoload['libraries'] as $item) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 244 | { |
| 245 | $this->_ci_initialize($item); |
| 246 | } |
admin | d48ef1c | 2006-09-21 06:22:05 +0000 | [diff] [blame] | 247 | unset($autoload['libraries']); |
| 248 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 249 | return $autoload; |
| 250 | } |
| 251 | // END _ci_autoload() |
| 252 | |
| 253 | // -------------------------------------------------------------------- |
| 254 | |
| 255 | /** |
| 256 | * Assign the core classes to the global $CI object |
| 257 | * |
| 258 | * By assigning all the classes instantiated by the front controller |
| 259 | * local class variables we enable everything to be accessible using |
| 260 | * $this->class->function() |
| 261 | * |
| 262 | * @access private |
| 263 | * @return void |
| 264 | */ |
| 265 | function _ci_assign_core() |
| 266 | { |
| 267 | foreach (array('Config', 'Input', 'Benchmark', 'URI', 'Output') as $val) |
| 268 | { |
| 269 | $class = strtolower($val); |
| 270 | $this->$class =& _load_class('CI_'.$val); |
| 271 | $this->ci_is_loaded[] = $class; |
| 272 | } |
| 273 | |
| 274 | $this->lang =& _load_class('CI_Language'); |
| 275 | $this->ci_is_loaded[] = 'lang'; |
| 276 | |
| 277 | // In PHP 4 the Controller class is a child of CI_Loader. |
| 278 | // In PHP 5 we run it as its own class. |
| 279 | if (floor(phpversion()) >= 5) |
| 280 | { |
| 281 | $this->load = new CI_Loader(); |
| 282 | } |
| 283 | |
| 284 | $this->ci_is_loaded[] = 'load'; |
| 285 | } |
| 286 | // END _ci_assign_core() |
| 287 | |
| 288 | // -------------------------------------------------------------------- |
| 289 | |
| 290 | /** |
| 291 | * Initialize Scaffolding |
| 292 | * |
| 293 | * This initializing function works a bit different than the |
| 294 | * others. It doesn't load the class. Instead, it simply |
| 295 | * sets a flag indicating that scaffolding is allowed to be |
| 296 | * used. The actual scaffolding function below is |
| 297 | * called by the front controller based on whether the |
| 298 | * second segment of the URL matches the "secret" scaffolding |
| 299 | * word stored in the application/config/routes.php |
| 300 | * |
| 301 | * @access private |
| 302 | * @param string the table to scaffold |
| 303 | * @return void |
| 304 | */ |
| 305 | function _ci_init_scaffolding($table = FALSE) |
| 306 | { |
| 307 | if ($table === FALSE) |
| 308 | { |
| 309 | show_error('You must include the name of the table you would like access when you initialize scaffolding'); |
| 310 | } |
| 311 | |
| 312 | $this->_ci_scaffolding = TRUE; |
| 313 | $this->_ci_scaff_table = $table; |
| 314 | } |
| 315 | // END _ci_init_scaffolding() |
| 316 | |
| 317 | // -------------------------------------------------------------------- |
| 318 | |
| 319 | /** |
| 320 | * Initialize Database |
| 321 | * |
| 322 | * @access private |
| 323 | * @param mixed database connection values |
| 324 | * @param bool whether to return the object for multiple connections |
| 325 | * @return void |
| 326 | */ |
| 327 | function _ci_init_database($params = '', $return = FALSE, $active_record = FALSE) |
| 328 | { |
| 329 | if ($this->_ci_is_loaded('db') == TRUE AND $return == FALSE AND $active_record == FALSE) |
| 330 | { |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | // Load the DB config file if needed |
| 335 | if (is_string($params) AND strpos($params, '://') === FALSE) |
| 336 | { |
| 337 | include(APPPATH.'config/database'.EXT); |
| 338 | |
| 339 | $group = ($params == '') ? $active_group : $params; |
| 340 | |
| 341 | if ( ! isset($db[$group])) |
| 342 | { |
| 343 | show_error('You have specified an invalid database connection group: '.$group); |
| 344 | } |
| 345 | |
| 346 | $params = $db[$group]; |
| 347 | } |
| 348 | |
| 349 | // No DB specified yet? Beat them senseless... |
| 350 | if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '') |
| 351 | { |
| 352 | show_error('You have not selected a database type to connect to.'); |
| 353 | } |
| 354 | |
| 355 | // Load the DB classes. Note: Since the active record class is optional |
| 356 | // we need to dynamically create a class that extends proper parent class |
| 357 | // based on whether we're using the active record class or not. |
| 358 | // Kudos to Paul for discovering this clever use of eval() |
| 359 | |
| 360 | if ($active_record == TRUE) |
| 361 | { |
| 362 | $params['active_r'] = TRUE; |
| 363 | } |
| 364 | |
admin | 5808346 | 2006-09-24 17:58:27 +0000 | [diff] [blame] | 365 | require_once(BASEPATH.'database/DB_driver'.EXT); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 366 | |
| 367 | if ( ! isset($params['active_r']) OR $params['active_r'] == TRUE) |
| 368 | { |
admin | 5808346 | 2006-09-24 17:58:27 +0000 | [diff] [blame] | 369 | require_once(BASEPATH.'database/DB_active_record'.EXT); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 370 | |
| 371 | if ( ! class_exists('CI_DB')) |
| 372 | { |
| 373 | eval('class CI_DB extends CI_DB_active_record { }'); |
| 374 | } |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | if ( ! class_exists('CI_DB')) |
| 379 | { |
| 380 | eval('class CI_DB extends CI_DB_driver { }'); |
| 381 | } |
| 382 | } |
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 383 | |
admin | 46563d5 | 2006-09-24 18:14:22 +0000 | [diff] [blame^] | 384 | require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 385 | |
| 386 | // Instantiate the DB adapter |
admin | 46563d5 | 2006-09-24 18:14:22 +0000 | [diff] [blame^] | 387 | $driver = 'CI_DB_'. $params['dbdriver'].'_driver'; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 388 | $DB = new $driver($params); |
| 389 | |
| 390 | if ($return === TRUE) |
| 391 | { |
| 392 | return $DB; |
| 393 | } |
| 394 | |
| 395 | $obj =& get_instance(); |
| 396 | $obj->ci_is_loaded[] = 'db'; |
| 397 | $obj->db =& $DB; |
| 398 | } |
| 399 | // END _ci_init_database() |
| 400 | |
| 401 | // -------------------------------------------------------------------- |
| 402 | |
| 403 | /** |
| 404 | * Returns TRUE if a class is loaded, FALSE if not |
| 405 | * |
| 406 | * @access public |
| 407 | * @param string the class name |
| 408 | * @return bool |
| 409 | */ |
| 410 | function _ci_is_loaded($class) |
| 411 | { |
| 412 | return ( ! in_array($class, $this->ci_is_loaded)) ? FALSE : TRUE; |
| 413 | } |
| 414 | // END _ci_is_loaded() |
| 415 | |
| 416 | // -------------------------------------------------------------------- |
| 417 | |
| 418 | /** |
| 419 | * Scaffolding |
| 420 | * |
| 421 | * Initializes the scaffolding. |
| 422 | * |
| 423 | * @access private |
| 424 | * @return void |
| 425 | */ |
| 426 | function _ci_scaffolding() |
| 427 | { |
| 428 | if ($this->_ci_scaffolding === FALSE OR $this->_ci_scaff_table === FALSE) |
| 429 | { |
| 430 | show_404('Scaffolding unavailable'); |
| 431 | } |
| 432 | |
| 433 | if (class_exists('Scaffolding')) return; |
| 434 | |
| 435 | if ( ! in_array($this->uri->segment(3), array('add', 'insert', 'edit', 'update', 'view', 'delete', 'do_delete'))) |
| 436 | { |
| 437 | $method = 'view'; |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | $method = $this->uri->segment(3); |
| 442 | } |
| 443 | |
| 444 | $this->_ci_init_database("", FALSE, TRUE); |
| 445 | |
| 446 | $this->_ci_initialize('pagination'); |
| 447 | require_once(BASEPATH.'scaffolding/Scaffolding'.EXT); |
| 448 | $this->scaff = new Scaffolding($this->_ci_scaff_table); |
| 449 | $this->scaff->$method(); |
| 450 | } |
| 451 | // END _ci_scaffolding() |
| 452 | |
| 453 | } |
| 454 | // END _Controller class |
| 455 | ?> |