blob: 9d858e6c217d86c9ed3bdd26d86f70cd11f44e41 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?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 */
30class Controller extends CI_Base {
31
adminb0dd10f2006-08-25 17:25:49 +000032 var $_ci_models = array();
33 var $_ci_scaffolding = FALSE;
34 var $_ci_scaff_table = FALSE;
admin7981a9a2006-09-26 07:52:09 +000035 var $_ci_last_handle = NULL;
36 var $_ci_last_params = NULL;
adminb0dd10f2006-08-25 17:25:49 +000037
38 /**
39 * Constructor
40 *
41 * Loads the base classes needed to run CI, and runs the "autoload"
42 * routine which loads the systems specified in the "autoload" config file.
43 */
44 function Controller()
45 {
46 parent::CI_Base();
47
48 // Assign all the class objects that were instantiated by the
49 // front controller to local class variables so that CI can be
50 // run as one big super object.
51 $this->_ci_assign_core();
52
53 // Load everything specified in the autoload.php file
54 $this->load->_ci_autoloader($this->_ci_autoload());
55
56 // This allows anything loaded using $this->load (viwes, files, etc.)
57 // to become accessible from within the Controller class functions.
admine79dc712006-09-26 03:52:45 +000058 foreach (get_object_vars($this) as $key => $var)
adminb0dd10f2006-08-25 17:25:49 +000059 {
admine79dc712006-09-26 03:52:45 +000060 if (is_object($var))
61 {
62 $this->load->$key =& $this->$key;
63 }
adminb0dd10f2006-08-25 17:25:49 +000064 }
admine79dc712006-09-26 03:52:45 +000065
adminb0dd10f2006-08-25 17:25:49 +000066 log_message('debug', "Controller Class Initialized");
67 }
admine79dc712006-09-26 03:52:45 +000068
adminb0dd10f2006-08-25 17:25:49 +000069 // --------------------------------------------------------------------
70
71 /**
72 * Initialization Handler
73 *
admin7981a9a2006-09-26 07:52:09 +000074 * Designed to be called from the class files themselves.
75 * See: http://www.codeigniter.com/user_guide/general/creating_libraries.html
76 *
77 * @access public
78 * @param string class name
79 * @param string variable name
80 * @param mixed any additional parameters
81 * @return void
82 */
83 function init_class($class, $varname = '', $params = NULL)
84 {
85 // First figure out what variable we're going to
86 // use to instantiate the class to
87 if ($varname == '')
88 {
89 $varname = ( ! is_null($this->_ci_last_handle)) ? $this->_ci_last_handle : strtolower(str_replace('CI_', '', $class));
90 }
91
92 // Are there any parameters?
93 if ($params === NULL AND $this->_ci_last_params !== NULL)
94 {
95 $params = $this->_ci_last_params;
96 }
97
98 // Instantiate the class
99 if ( ! is_null($params))
100 {
101 $this->$varname = new $class($params);
102 }
103 else
104 {
105 $this->$varname = new $class;
106 }
107
108 $this->_ci_last_params = NULL;
109 $this->_ci_last_handle = NULL;
110 }
111
112 // --------------------------------------------------------------------
113
114 /**
115 * Initialization Handler
116 *
117 * This function loads the requested class.
adminb0dd10f2006-08-25 17:25:49 +0000118 *
119 * @access private
120 * @param string the item that is being loaded
121 * @param mixed any additional parameters
122 * @return void
123 */
admin7981a9a2006-09-26 07:52:09 +0000124 function _ci_init_class($class, $params = NULL)
125 {
126 // Prep the class name
admine79dc712006-09-26 03:52:45 +0000127 $class = strtolower(str_replace(EXT, '', $class));
adminb0dd10f2006-08-25 17:25:49 +0000128
admin7981a9a2006-09-26 07:52:09 +0000129 // These are used by $this->init_class() above.
130 // They lets us dynamically set the object name and pass parameters
131 $this->_ci_last_handle = $class;
132 $this->_ci_last_params = $params;
133
134 // Does THIS file (Controller.php) contain an initialization
135 // function that maps to the requested class?
136
137 $method = '_ci_init_'.$class;
138
139 if (method_exists($this, $method))
140 {
141 if (is_null($params))
adminb0dd10f2006-08-25 17:25:49 +0000142 {
143 $this->$method();
144 }
145 else
146 {
147 $this->$method($params);
admin7981a9a2006-09-26 07:52:09 +0000148 }
149
150 // We're done...
151 return TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000152 }
admin7981a9a2006-09-26 07:52:09 +0000153
154 // Lets search for the requested library file and load it.
155 // We'll assume that the file we load contains a call to
156 // $obj->init_class() so that the class can get instantiated.
157 // For backward compatibility we'll test for filenames that are
158 // both uppercase and lower.
159
160 foreach (array(ucfirst($class), $class) as $filename)
161 {
162 for ($i = 1; $i < 3; $i++)
163 {
164 $path = ($i % 2) ? APPPATH : BASEPATH;
165
166 if (file_exists($path.'libraries/'.$filename.EXT))
167 {
168 include_once($path.'libraries/'.$filename.EXT);
169 return TRUE;
170 }
171 }
172
173 }
174
175 // If we got this far we were unable to find the requested class
176 log_message('error', "Unable to load the requested class: ".$class);
177 show_error("Unable to load the class: ".$class);
adminb0dd10f2006-08-25 17:25:49 +0000178 }
adminb0dd10f2006-08-25 17:25:49 +0000179
180 // --------------------------------------------------------------------
181
182 /**
183 * Loads and instantiates the requested model class
184 *
185 * @access private
186 * @param string
187 * @return array
188 */
admin7981a9a2006-09-26 07:52:09 +0000189 function _ci_init_model($model, $name = '', $db_conn = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000190 {
191 if ($name == '')
192 {
193 $name = $model;
194 }
adminb4473d42006-08-27 15:46:31 +0000195
admin0d296052006-08-27 15:48:38 +0000196 $obj =& get_instance();
197 if (in_array($name, $obj->_ci_models))
adminb4473d42006-08-27 15:46:31 +0000198 {
199 return;
200 }
201
adminb0dd10f2006-08-25 17:25:49 +0000202 if (isset($this->$name))
203 {
204 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
205 }
206
207 $model = strtolower($model);
208
209 if ( ! file_exists(APPPATH.'models/'.$model.EXT))
210 {
211 show_error('Unable to locate the model you have specified: '.$model);
212 }
213
214 if ($db_conn !== FALSE)
215 {
216 if ($db_conn === TRUE)
217 $db_conn = '';
218
219 $this->_ci_init_database($db_conn, FALSE, TRUE);
220 }
221
222 if ( ! class_exists('Model'))
223 {
224 require_once(BASEPATH.'libraries/Model'.EXT);
225 }
226
227 require_once(APPPATH.'models/'.$model.EXT);
228
229 $model = ucfirst($model);
230 $this->$name = new $model();
231 $this->_ci_models[] = $name;
232 $this->_ci_assign_to_models();
admin7981a9a2006-09-26 07:52:09 +0000233 }
adminb0dd10f2006-08-25 17:25:49 +0000234
235 // --------------------------------------------------------------------
236
237 /**
238 * Assign to Models
239 *
240 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
241 * will be available to modles, if any exist.
242 *
243 * @access public
244 * @param object
245 * @return array
246 */
247 function _ci_assign_to_models()
248 {
249 $obj =& get_instance();
250 if (count($obj->_ci_models) == 0)
251 {
252 return;
253 }
254 foreach ($obj->_ci_models as $model)
255 {
256 $obj->$model->_assign_libraries();
257 }
admin7981a9a2006-09-26 07:52:09 +0000258 }
adminb0dd10f2006-08-25 17:25:49 +0000259
260 // --------------------------------------------------------------------
261
262 /**
263 * Auto-initialize Core Classes
264 *
265 * This initializes the core systems that are specified in the
266 * libraries/autoload.php file, as well as the systems specified in
267 * the $autoload class array above.
268 *
269 * It returns the "autoload" array so we can pass it to the Loader
270 * class since it needs to autoload plugins and helper files
271 *
272 * The config/autoload.php file contains an array that permits
273 * sub-systems to be loaded automatically.
274 *
275 * @access private
276 * @return array
277 */
278 function _ci_autoload()
279 {
280 include_once(APPPATH.'config/autoload'.EXT);
281
282 if ( ! isset($autoload))
283 {
284 return FALSE;
285 }
286
287 if (count($autoload['config']) > 0)
288 {
289 foreach ($autoload['config'] as $key => $val)
290 {
291 $this->config->load($val);
292 }
293 }
294 unset($autoload['config']);
295
admind48ef1c2006-09-21 06:22:05 +0000296 // A little tweak to remain backward compatible
297 // The $autoload['core'] item was deprecated
298 if ( ! isset($autoload['libraries']))
adminb0dd10f2006-08-25 17:25:49 +0000299 {
admind48ef1c2006-09-21 06:22:05 +0000300 $autoload['libraries'] = $autoload['core'];
301
adminb0dd10f2006-08-25 17:25:49 +0000302 }
303
admin6e00bab2006-09-26 08:13:06 +0000304 $exceptions = array('dbutil', 'dbexport');
305
admind48ef1c2006-09-21 06:22:05 +0000306 foreach ($autoload['libraries'] as $item)
adminb0dd10f2006-08-25 17:25:49 +0000307 {
admin6e00bab2006-09-26 08:13:06 +0000308 if ( ! in_array($item, $exceptions))
309 {
310 $this->_ci_init_class($item);
311 }
312 else
313 {
314 $this->_ci_init_dbextra($item);
315 }
adminb0dd10f2006-08-25 17:25:49 +0000316 }
admind48ef1c2006-09-21 06:22:05 +0000317 unset($autoload['libraries']);
318
adminb0dd10f2006-08-25 17:25:49 +0000319 return $autoload;
320 }
adminb0dd10f2006-08-25 17:25:49 +0000321
322 // --------------------------------------------------------------------
323
324 /**
325 * Assign the core classes to the global $CI object
326 *
327 * By assigning all the classes instantiated by the front controller
328 * local class variables we enable everything to be accessible using
329 * $this->class->function()
330 *
331 * @access private
332 * @return void
333 */
334 function _ci_assign_core()
335 {
336 foreach (array('Config', 'Input', 'Benchmark', 'URI', 'Output') as $val)
337 {
338 $class = strtolower($val);
339 $this->$class =& _load_class('CI_'.$val);
adminb0dd10f2006-08-25 17:25:49 +0000340 }
341
342 $this->lang =& _load_class('CI_Language');
adminb0dd10f2006-08-25 17:25:49 +0000343
344 // In PHP 4 the Controller class is a child of CI_Loader.
345 // In PHP 5 we run it as its own class.
346 if (floor(phpversion()) >= 5)
347 {
348 $this->load = new CI_Loader();
349 }
adminb0dd10f2006-08-25 17:25:49 +0000350 }
adminb0dd10f2006-08-25 17:25:49 +0000351
352 // --------------------------------------------------------------------
353
354 /**
355 * Initialize Scaffolding
356 *
357 * This initializing function works a bit different than the
358 * others. It doesn't load the class. Instead, it simply
359 * sets a flag indicating that scaffolding is allowed to be
360 * used. The actual scaffolding function below is
361 * called by the front controller based on whether the
362 * second segment of the URL matches the "secret" scaffolding
363 * word stored in the application/config/routes.php
364 *
365 * @access private
366 * @param string the table to scaffold
367 * @return void
368 */
369 function _ci_init_scaffolding($table = FALSE)
370 {
371 if ($table === FALSE)
372 {
373 show_error('You must include the name of the table you would like access when you initialize scaffolding');
374 }
375
376 $this->_ci_scaffolding = TRUE;
377 $this->_ci_scaff_table = $table;
378 }
adminb0dd10f2006-08-25 17:25:49 +0000379
380 // --------------------------------------------------------------------
381
382 /**
383 * Initialize Database
384 *
385 * @access private
386 * @param mixed database connection values
387 * @param bool whether to return the object for multiple connections
388 * @return void
389 */
390 function _ci_init_database($params = '', $return = FALSE, $active_record = FALSE)
admin7981a9a2006-09-26 07:52:09 +0000391 {
adminb0dd10f2006-08-25 17:25:49 +0000392 if ($this->_ci_is_loaded('db') == TRUE AND $return == FALSE AND $active_record == FALSE)
393 {
394 return;
395 }
396
397 // Load the DB config file if needed
398 if (is_string($params) AND strpos($params, '://') === FALSE)
399 {
400 include(APPPATH.'config/database'.EXT);
401
402 $group = ($params == '') ? $active_group : $params;
403
404 if ( ! isset($db[$group]))
405 {
406 show_error('You have specified an invalid database connection group: '.$group);
407 }
408
409 $params = $db[$group];
410 }
411
412 // No DB specified yet? Beat them senseless...
413 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
414 {
415 show_error('You have not selected a database type to connect to.');
416 }
417
418 // Load the DB classes. Note: Since the active record class is optional
419 // we need to dynamically create a class that extends proper parent class
420 // based on whether we're using the active record class or not.
421 // Kudos to Paul for discovering this clever use of eval()
422
423 if ($active_record == TRUE)
424 {
425 $params['active_r'] = TRUE;
426 }
427
admin58083462006-09-24 17:58:27 +0000428 require_once(BASEPATH.'database/DB_driver'.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000429
430 if ( ! isset($params['active_r']) OR $params['active_r'] == TRUE)
431 {
adminc9a8bab2006-09-24 20:28:33 +0000432 require_once(BASEPATH.'database/DB_active_rec'.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000433
434 if ( ! class_exists('CI_DB'))
435 {
436 eval('class CI_DB extends CI_DB_active_record { }');
437 }
438 }
439 else
440 {
441 if ( ! class_exists('CI_DB'))
442 {
443 eval('class CI_DB extends CI_DB_driver { }');
444 }
445 }
adminc1fa0742006-09-21 23:50:23 +0000446
admin46563d52006-09-24 18:14:22 +0000447 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000448
449 // Instantiate the DB adapter
admina5e812c2006-09-25 02:17:30 +0000450 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
adminb0dd10f2006-08-25 17:25:49 +0000451 $DB = new $driver($params);
452
453 if ($return === TRUE)
454 {
455 return $DB;
456 }
457
458 $obj =& get_instance();
adminb0dd10f2006-08-25 17:25:49 +0000459 $obj->db =& $DB;
460 }
admina5e812c2006-09-25 02:17:30 +0000461
adminb0dd10f2006-08-25 17:25:49 +0000462 // --------------------------------------------------------------------
463
464 /**
admin6e00bab2006-09-26 08:13:06 +0000465 * Initialize Database Ancillary Classes
466 *
467 * @access private
468 * @param str class name
469 * @return void
470 */
471 function _ci_init_dbextra($class)
472 {
473 $map = array('dbutil' => 'DB_utility', 'dbexport' => 'DB_export');
474 require_once(BASEPATH.'database/'.$map[$class].EXT);
475
476 $this->init_class('CI_'.$map[$class], $class);
477 }
478
479 // --------------------------------------------------------------------
480
481 /**
adminb0dd10f2006-08-25 17:25:49 +0000482 * Returns TRUE if a class is loaded, FALSE if not
483 *
484 * @access public
485 * @param string the class name
486 * @return bool
487 */
488 function _ci_is_loaded($class)
489 {
admine79dc712006-09-26 03:52:45 +0000490 return ( ! isset($this->$class) OR ! is_object($this->$class)) ? FALSE : TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000491 }
admine79dc712006-09-26 03:52:45 +0000492
adminb0dd10f2006-08-25 17:25:49 +0000493 // --------------------------------------------------------------------
494
495 /**
496 * Scaffolding
497 *
498 * Initializes the scaffolding.
499 *
500 * @access private
501 * @return void
502 */
503 function _ci_scaffolding()
504 {
505 if ($this->_ci_scaffolding === FALSE OR $this->_ci_scaff_table === FALSE)
506 {
507 show_404('Scaffolding unavailable');
508 }
509
510 if (class_exists('Scaffolding')) return;
511
512 if ( ! in_array($this->uri->segment(3), array('add', 'insert', 'edit', 'update', 'view', 'delete', 'do_delete')))
513 {
514 $method = 'view';
515 }
516 else
517 {
518 $method = $this->uri->segment(3);
519 }
520
521 $this->_ci_init_database("", FALSE, TRUE);
admin7981a9a2006-09-26 07:52:09 +0000522 $this->_ci_init_class('pagination');
adminb0dd10f2006-08-25 17:25:49 +0000523 require_once(BASEPATH.'scaffolding/Scaffolding'.EXT);
524 $this->scaff = new Scaffolding($this->_ci_scaff_table);
525 $this->scaff->$method();
526 }
adminb0dd10f2006-08-25 17:25:49 +0000527
528}
529// END _Controller class
530?>