blob: 23c050e18142854e113c5ae0cd783e853ac1c4ee [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 {
admin41a16852006-09-26 18:17:19 +000085 // First figure out what variable we're going to assign the class to
admin7981a9a2006-09-26 07:52:09 +000086 if ($varname == '')
87 {
88 $varname = ( ! is_null($this->_ci_last_handle)) ? $this->_ci_last_handle : strtolower(str_replace('CI_', '', $class));
89 }
90
91 // Are there any parameters?
92 if ($params === NULL AND $this->_ci_last_params !== NULL)
93 {
94 $params = $this->_ci_last_params;
95 }
96
97 // Instantiate the class
98 if ( ! is_null($params))
99 {
100 $this->$varname = new $class($params);
101 }
102 else
103 {
104 $this->$varname = new $class;
105 }
106
107 $this->_ci_last_params = NULL;
108 $this->_ci_last_handle = NULL;
109 }
110
111 // --------------------------------------------------------------------
112
113 /**
114 * Initialization Handler
115 *
116 * This function loads the requested class.
adminb0dd10f2006-08-25 17:25:49 +0000117 *
118 * @access private
119 * @param string the item that is being loaded
120 * @param mixed any additional parameters
121 * @return void
122 */
admin7981a9a2006-09-26 07:52:09 +0000123 function _ci_init_class($class, $params = NULL)
124 {
125 // Prep the class name
admine79dc712006-09-26 03:52:45 +0000126 $class = strtolower(str_replace(EXT, '', $class));
adminb0dd10f2006-08-25 17:25:49 +0000127
admin7981a9a2006-09-26 07:52:09 +0000128 // These are used by $this->init_class() above.
129 // They lets us dynamically set the object name and pass parameters
130 $this->_ci_last_handle = $class;
131 $this->_ci_last_params = $params;
132
133 // Does THIS file (Controller.php) contain an initialization
134 // function that maps to the requested class?
135
136 $method = '_ci_init_'.$class;
137
138 if (method_exists($this, $method))
139 {
140 if (is_null($params))
adminb0dd10f2006-08-25 17:25:49 +0000141 {
142 $this->$method();
143 }
144 else
145 {
146 $this->$method($params);
admin7981a9a2006-09-26 07:52:09 +0000147 }
148
149 // We're done...
150 return TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000151 }
admin7981a9a2006-09-26 07:52:09 +0000152
153 // Lets search for the requested library file and load it.
154 // We'll assume that the file we load contains a call to
155 // $obj->init_class() so that the class can get instantiated.
156 // For backward compatibility we'll test for filenames that are
157 // both uppercase and lower.
admin7981a9a2006-09-26 07:52:09 +0000158 foreach (array(ucfirst($class), $class) as $filename)
159 {
160 for ($i = 1; $i < 3; $i++)
161 {
162 $path = ($i % 2) ? APPPATH : BASEPATH;
163
164 if (file_exists($path.'libraries/'.$filename.EXT))
165 {
166 include_once($path.'libraries/'.$filename.EXT);
167 return TRUE;
168 }
169 }
170
171 }
172
173 // If we got this far we were unable to find the requested class
174 log_message('error', "Unable to load the requested class: ".$class);
175 show_error("Unable to load the class: ".$class);
adminb0dd10f2006-08-25 17:25:49 +0000176 }
adminb0dd10f2006-08-25 17:25:49 +0000177
178 // --------------------------------------------------------------------
179
180 /**
181 * Loads and instantiates the requested model class
182 *
183 * @access private
184 * @param string
185 * @return array
186 */
admin7981a9a2006-09-26 07:52:09 +0000187 function _ci_init_model($model, $name = '', $db_conn = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000188 {
189 if ($name == '')
190 {
191 $name = $model;
192 }
adminb4473d42006-08-27 15:46:31 +0000193
admin0d296052006-08-27 15:48:38 +0000194 $obj =& get_instance();
195 if (in_array($name, $obj->_ci_models))
adminb4473d42006-08-27 15:46:31 +0000196 {
197 return;
198 }
199
adminb0dd10f2006-08-25 17:25:49 +0000200 if (isset($this->$name))
201 {
202 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
203 }
204
205 $model = strtolower($model);
206
207 if ( ! file_exists(APPPATH.'models/'.$model.EXT))
208 {
209 show_error('Unable to locate the model you have specified: '.$model);
210 }
211
212 if ($db_conn !== FALSE)
213 {
214 if ($db_conn === TRUE)
215 $db_conn = '';
216
217 $this->_ci_init_database($db_conn, FALSE, TRUE);
218 }
219
220 if ( ! class_exists('Model'))
221 {
222 require_once(BASEPATH.'libraries/Model'.EXT);
223 }
224
225 require_once(APPPATH.'models/'.$model.EXT);
226
227 $model = ucfirst($model);
228 $this->$name = new $model();
229 $this->_ci_models[] = $name;
230 $this->_ci_assign_to_models();
admin7981a9a2006-09-26 07:52:09 +0000231 }
adminb0dd10f2006-08-25 17:25:49 +0000232
233 // --------------------------------------------------------------------
234
235 /**
236 * Assign to Models
237 *
238 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
239 * will be available to modles, if any exist.
240 *
241 * @access public
242 * @param object
243 * @return array
244 */
245 function _ci_assign_to_models()
246 {
247 $obj =& get_instance();
248 if (count($obj->_ci_models) == 0)
249 {
250 return;
251 }
252 foreach ($obj->_ci_models as $model)
253 {
254 $obj->$model->_assign_libraries();
255 }
admin7981a9a2006-09-26 07:52:09 +0000256 }
adminb0dd10f2006-08-25 17:25:49 +0000257
258 // --------------------------------------------------------------------
259
260 /**
261 * Auto-initialize Core Classes
262 *
263 * This initializes the core systems that are specified in the
264 * libraries/autoload.php file, as well as the systems specified in
265 * the $autoload class array above.
266 *
267 * It returns the "autoload" array so we can pass it to the Loader
268 * class since it needs to autoload plugins and helper files
269 *
270 * The config/autoload.php file contains an array that permits
271 * sub-systems to be loaded automatically.
272 *
273 * @access private
274 * @return array
275 */
276 function _ci_autoload()
277 {
278 include_once(APPPATH.'config/autoload'.EXT);
279
280 if ( ! isset($autoload))
281 {
282 return FALSE;
283 }
284
285 if (count($autoload['config']) > 0)
286 {
287 foreach ($autoload['config'] as $key => $val)
288 {
289 $this->config->load($val);
290 }
291 }
292 unset($autoload['config']);
293
admind48ef1c2006-09-21 06:22:05 +0000294 // A little tweak to remain backward compatible
295 // The $autoload['core'] item was deprecated
296 if ( ! isset($autoload['libraries']))
adminb0dd10f2006-08-25 17:25:49 +0000297 {
admind48ef1c2006-09-21 06:22:05 +0000298 $autoload['libraries'] = $autoload['core'];
299
adminb0dd10f2006-08-25 17:25:49 +0000300 }
301
admin6e00bab2006-09-26 08:13:06 +0000302 $exceptions = array('dbutil', 'dbexport');
303
admind48ef1c2006-09-21 06:22:05 +0000304 foreach ($autoload['libraries'] as $item)
adminb0dd10f2006-08-25 17:25:49 +0000305 {
admin6e00bab2006-09-26 08:13:06 +0000306 if ( ! in_array($item, $exceptions))
307 {
308 $this->_ci_init_class($item);
309 }
310 else
311 {
312 $this->_ci_init_dbextra($item);
313 }
adminb0dd10f2006-08-25 17:25:49 +0000314 }
admind48ef1c2006-09-21 06:22:05 +0000315 unset($autoload['libraries']);
316
adminb0dd10f2006-08-25 17:25:49 +0000317 return $autoload;
318 }
adminb0dd10f2006-08-25 17:25:49 +0000319
320 // --------------------------------------------------------------------
321
322 /**
323 * Assign the core classes to the global $CI object
324 *
325 * By assigning all the classes instantiated by the front controller
326 * local class variables we enable everything to be accessible using
327 * $this->class->function()
328 *
329 * @access private
330 * @return void
331 */
332 function _ci_assign_core()
333 {
334 foreach (array('Config', 'Input', 'Benchmark', 'URI', 'Output') as $val)
335 {
336 $class = strtolower($val);
337 $this->$class =& _load_class('CI_'.$val);
adminb0dd10f2006-08-25 17:25:49 +0000338 }
339
340 $this->lang =& _load_class('CI_Language');
adminb0dd10f2006-08-25 17:25:49 +0000341
342 // In PHP 4 the Controller class is a child of CI_Loader.
343 // In PHP 5 we run it as its own class.
344 if (floor(phpversion()) >= 5)
345 {
346 $this->load = new CI_Loader();
347 }
adminb0dd10f2006-08-25 17:25:49 +0000348 }
adminb0dd10f2006-08-25 17:25:49 +0000349
350 // --------------------------------------------------------------------
351
352 /**
353 * Initialize Scaffolding
354 *
355 * This initializing function works a bit different than the
356 * others. It doesn't load the class. Instead, it simply
357 * sets a flag indicating that scaffolding is allowed to be
358 * used. The actual scaffolding function below is
359 * called by the front controller based on whether the
360 * second segment of the URL matches the "secret" scaffolding
361 * word stored in the application/config/routes.php
362 *
363 * @access private
364 * @param string the table to scaffold
365 * @return void
366 */
367 function _ci_init_scaffolding($table = FALSE)
368 {
369 if ($table === FALSE)
370 {
371 show_error('You must include the name of the table you would like access when you initialize scaffolding');
372 }
373
374 $this->_ci_scaffolding = TRUE;
375 $this->_ci_scaff_table = $table;
376 }
adminb0dd10f2006-08-25 17:25:49 +0000377
378 // --------------------------------------------------------------------
379
380 /**
381 * Initialize Database
382 *
383 * @access private
384 * @param mixed database connection values
385 * @param bool whether to return the object for multiple connections
386 * @return void
387 */
388 function _ci_init_database($params = '', $return = FALSE, $active_record = FALSE)
admin7981a9a2006-09-26 07:52:09 +0000389 {
adminb0dd10f2006-08-25 17:25:49 +0000390 if ($this->_ci_is_loaded('db') == TRUE AND $return == FALSE AND $active_record == FALSE)
391 {
392 return;
393 }
394
395 // Load the DB config file if needed
396 if (is_string($params) AND strpos($params, '://') === FALSE)
397 {
398 include(APPPATH.'config/database'.EXT);
399
400 $group = ($params == '') ? $active_group : $params;
401
402 if ( ! isset($db[$group]))
403 {
404 show_error('You have specified an invalid database connection group: '.$group);
405 }
406
407 $params = $db[$group];
408 }
409
410 // No DB specified yet? Beat them senseless...
411 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
412 {
413 show_error('You have not selected a database type to connect to.');
414 }
415
416 // Load the DB classes. Note: Since the active record class is optional
417 // we need to dynamically create a class that extends proper parent class
418 // based on whether we're using the active record class or not.
419 // Kudos to Paul for discovering this clever use of eval()
420
421 if ($active_record == TRUE)
422 {
423 $params['active_r'] = TRUE;
424 }
425
admin58083462006-09-24 17:58:27 +0000426 require_once(BASEPATH.'database/DB_driver'.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000427
428 if ( ! isset($params['active_r']) OR $params['active_r'] == TRUE)
429 {
adminc9a8bab2006-09-24 20:28:33 +0000430 require_once(BASEPATH.'database/DB_active_rec'.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000431
432 if ( ! class_exists('CI_DB'))
433 {
434 eval('class CI_DB extends CI_DB_active_record { }');
435 }
436 }
437 else
438 {
439 if ( ! class_exists('CI_DB'))
440 {
441 eval('class CI_DB extends CI_DB_driver { }');
442 }
443 }
adminc1fa0742006-09-21 23:50:23 +0000444
admin46563d52006-09-24 18:14:22 +0000445 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000446
447 // Instantiate the DB adapter
admina5e812c2006-09-25 02:17:30 +0000448 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
adminb0dd10f2006-08-25 17:25:49 +0000449 $DB = new $driver($params);
450
451 if ($return === TRUE)
452 {
453 return $DB;
454 }
455
456 $obj =& get_instance();
adminb0dd10f2006-08-25 17:25:49 +0000457 $obj->db =& $DB;
458 }
admina5e812c2006-09-25 02:17:30 +0000459
adminb0dd10f2006-08-25 17:25:49 +0000460 // --------------------------------------------------------------------
461
462 /**
admin6e00bab2006-09-26 08:13:06 +0000463 * Initialize Database Ancillary Classes
464 *
465 * @access private
466 * @param str class name
467 * @return void
468 */
469 function _ci_init_dbextra($class)
470 {
471 $map = array('dbutil' => 'DB_utility', 'dbexport' => 'DB_export');
472 require_once(BASEPATH.'database/'.$map[$class].EXT);
473
474 $this->init_class('CI_'.$map[$class], $class);
475 }
476
477 // --------------------------------------------------------------------
478
479 /**
adminb0dd10f2006-08-25 17:25:49 +0000480 * Returns TRUE if a class is loaded, FALSE if not
481 *
482 * @access public
483 * @param string the class name
484 * @return bool
485 */
486 function _ci_is_loaded($class)
487 {
admine79dc712006-09-26 03:52:45 +0000488 return ( ! isset($this->$class) OR ! is_object($this->$class)) ? FALSE : TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000489 }
admine79dc712006-09-26 03:52:45 +0000490
adminb0dd10f2006-08-25 17:25:49 +0000491 // --------------------------------------------------------------------
492
493 /**
494 * Scaffolding
495 *
496 * Initializes the scaffolding.
497 *
498 * @access private
499 * @return void
500 */
501 function _ci_scaffolding()
502 {
503 if ($this->_ci_scaffolding === FALSE OR $this->_ci_scaff_table === FALSE)
504 {
505 show_404('Scaffolding unavailable');
506 }
507
508 if (class_exists('Scaffolding')) return;
509
510 if ( ! in_array($this->uri->segment(3), array('add', 'insert', 'edit', 'update', 'view', 'delete', 'do_delete')))
511 {
512 $method = 'view';
513 }
514 else
515 {
516 $method = $this->uri->segment(3);
517 }
518
519 $this->_ci_init_database("", FALSE, TRUE);
admin7981a9a2006-09-26 07:52:09 +0000520 $this->_ci_init_class('pagination');
adminb0dd10f2006-08-25 17:25:49 +0000521 require_once(BASEPATH.'scaffolding/Scaffolding'.EXT);
522 $this->scaff = new Scaffolding($this->_ci_scaff_table);
523 $this->scaff->$method();
524 }
adminb0dd10f2006-08-25 17:25:49 +0000525
526}
527// END _Controller class
528?>