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