blob: 74b233ef3f524847db6ff8ae9b07faa48810a57c [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
admind48ef1c2006-09-21 06:22:05 +0000304 foreach ($autoload['libraries'] as $item)
adminb0dd10f2006-08-25 17:25:49 +0000305 {
admin7981a9a2006-09-26 07:52:09 +0000306 $this->_ci_init_class($item);
adminb0dd10f2006-08-25 17:25:49 +0000307 }
admind48ef1c2006-09-21 06:22:05 +0000308 unset($autoload['libraries']);
309
adminb0dd10f2006-08-25 17:25:49 +0000310 return $autoload;
311 }
adminb0dd10f2006-08-25 17:25:49 +0000312
313 // --------------------------------------------------------------------
314
315 /**
316 * Assign the core classes to the global $CI object
317 *
318 * By assigning all the classes instantiated by the front controller
319 * local class variables we enable everything to be accessible using
320 * $this->class->function()
321 *
322 * @access private
323 * @return void
324 */
325 function _ci_assign_core()
326 {
327 foreach (array('Config', 'Input', 'Benchmark', 'URI', 'Output') as $val)
328 {
329 $class = strtolower($val);
330 $this->$class =& _load_class('CI_'.$val);
adminb0dd10f2006-08-25 17:25:49 +0000331 }
332
333 $this->lang =& _load_class('CI_Language');
adminb0dd10f2006-08-25 17:25:49 +0000334
335 // In PHP 4 the Controller class is a child of CI_Loader.
336 // In PHP 5 we run it as its own class.
337 if (floor(phpversion()) >= 5)
338 {
339 $this->load = new CI_Loader();
340 }
adminb0dd10f2006-08-25 17:25:49 +0000341 }
adminb0dd10f2006-08-25 17:25:49 +0000342
343 // --------------------------------------------------------------------
344
345 /**
346 * Initialize Scaffolding
347 *
348 * This initializing function works a bit different than the
349 * others. It doesn't load the class. Instead, it simply
350 * sets a flag indicating that scaffolding is allowed to be
351 * used. The actual scaffolding function below is
352 * called by the front controller based on whether the
353 * second segment of the URL matches the "secret" scaffolding
354 * word stored in the application/config/routes.php
355 *
356 * @access private
357 * @param string the table to scaffold
358 * @return void
359 */
360 function _ci_init_scaffolding($table = FALSE)
361 {
362 if ($table === FALSE)
363 {
364 show_error('You must include the name of the table you would like access when you initialize scaffolding');
365 }
366
367 $this->_ci_scaffolding = TRUE;
368 $this->_ci_scaff_table = $table;
369 }
adminb0dd10f2006-08-25 17:25:49 +0000370
371 // --------------------------------------------------------------------
372
373 /**
374 * Initialize Database
375 *
376 * @access private
377 * @param mixed database connection values
378 * @param bool whether to return the object for multiple connections
379 * @return void
380 */
381 function _ci_init_database($params = '', $return = FALSE, $active_record = FALSE)
admin7981a9a2006-09-26 07:52:09 +0000382 {
adminb0dd10f2006-08-25 17:25:49 +0000383 if ($this->_ci_is_loaded('db') == TRUE AND $return == FALSE AND $active_record == FALSE)
384 {
385 return;
386 }
387
388 // Load the DB config file if needed
389 if (is_string($params) AND strpos($params, '://') === FALSE)
390 {
391 include(APPPATH.'config/database'.EXT);
392
393 $group = ($params == '') ? $active_group : $params;
394
395 if ( ! isset($db[$group]))
396 {
397 show_error('You have specified an invalid database connection group: '.$group);
398 }
399
400 $params = $db[$group];
401 }
402
403 // No DB specified yet? Beat them senseless...
404 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
405 {
406 show_error('You have not selected a database type to connect to.');
407 }
408
409 // Load the DB classes. Note: Since the active record class is optional
410 // we need to dynamically create a class that extends proper parent class
411 // based on whether we're using the active record class or not.
412 // Kudos to Paul for discovering this clever use of eval()
413
414 if ($active_record == TRUE)
415 {
416 $params['active_r'] = TRUE;
417 }
418
admin58083462006-09-24 17:58:27 +0000419 require_once(BASEPATH.'database/DB_driver'.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000420
421 if ( ! isset($params['active_r']) OR $params['active_r'] == TRUE)
422 {
adminc9a8bab2006-09-24 20:28:33 +0000423 require_once(BASEPATH.'database/DB_active_rec'.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000424
425 if ( ! class_exists('CI_DB'))
426 {
427 eval('class CI_DB extends CI_DB_active_record { }');
428 }
429 }
430 else
431 {
432 if ( ! class_exists('CI_DB'))
433 {
434 eval('class CI_DB extends CI_DB_driver { }');
435 }
436 }
adminc1fa0742006-09-21 23:50:23 +0000437
admin46563d52006-09-24 18:14:22 +0000438 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000439
440 // Instantiate the DB adapter
admina5e812c2006-09-25 02:17:30 +0000441 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
adminb0dd10f2006-08-25 17:25:49 +0000442 $DB = new $driver($params);
443
444 if ($return === TRUE)
445 {
446 return $DB;
447 }
448
449 $obj =& get_instance();
adminb0dd10f2006-08-25 17:25:49 +0000450 $obj->db =& $DB;
451 }
admina5e812c2006-09-25 02:17:30 +0000452
adminb0dd10f2006-08-25 17:25:49 +0000453 // --------------------------------------------------------------------
454
455 /**
456 * Returns TRUE if a class is loaded, FALSE if not
457 *
458 * @access public
459 * @param string the class name
460 * @return bool
461 */
462 function _ci_is_loaded($class)
463 {
admine79dc712006-09-26 03:52:45 +0000464 return ( ! isset($this->$class) OR ! is_object($this->$class)) ? FALSE : TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000465 }
admine79dc712006-09-26 03:52:45 +0000466
adminb0dd10f2006-08-25 17:25:49 +0000467 // --------------------------------------------------------------------
468
469 /**
470 * Scaffolding
471 *
472 * Initializes the scaffolding.
473 *
474 * @access private
475 * @return void
476 */
477 function _ci_scaffolding()
478 {
479 if ($this->_ci_scaffolding === FALSE OR $this->_ci_scaff_table === FALSE)
480 {
481 show_404('Scaffolding unavailable');
482 }
483
484 if (class_exists('Scaffolding')) return;
485
486 if ( ! in_array($this->uri->segment(3), array('add', 'insert', 'edit', 'update', 'view', 'delete', 'do_delete')))
487 {
488 $method = 'view';
489 }
490 else
491 {
492 $method = $this->uri->segment(3);
493 }
494
495 $this->_ci_init_database("", FALSE, TRUE);
admin7981a9a2006-09-26 07:52:09 +0000496 $this->_ci_init_class('pagination');
adminb0dd10f2006-08-25 17:25:49 +0000497 require_once(BASEPATH.'scaffolding/Scaffolding'.EXT);
498 $this->scaff = new Scaffolding($this->_ci_scaff_table);
499 $this->scaff->$method();
500 }
adminb0dd10f2006-08-25 17:25:49 +0000501
502}
503// END _Controller class
504?>