blob: 5fdbd9f9fa0502921573251f6af9757e6289e65b [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
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 }
adminb4473d42006-08-27 15:46:31 +0000130
admin0d296052006-08-27 15:48:38 +0000131 $obj =& get_instance();
132 if (in_array($name, $obj->_ci_models))
adminb4473d42006-08-27 15:46:31 +0000133 {
134 return;
135 }
136
adminb0dd10f2006-08-25 17:25:49 +0000137 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
admind48ef1c2006-09-21 06:22:05 +0000235 // A little tweak to remain backward compatible
236 // The $autoload['core'] item was deprecated
237 if ( ! isset($autoload['libraries']))
adminb0dd10f2006-08-25 17:25:49 +0000238 {
admind48ef1c2006-09-21 06:22:05 +0000239 $autoload['libraries'] = $autoload['core'];
240
adminb0dd10f2006-08-25 17:25:49 +0000241 }
242
admind48ef1c2006-09-21 06:22:05 +0000243 foreach ($autoload['libraries'] as $item)
adminb0dd10f2006-08-25 17:25:49 +0000244 {
245 $this->_ci_initialize($item);
246 }
admind48ef1c2006-09-21 06:22:05 +0000247 unset($autoload['libraries']);
248
adminb0dd10f2006-08-25 17:25:49 +0000249 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
admin58083462006-09-24 17:58:27 +0000365 require_once(BASEPATH.'database/DB_driver'.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000366
367 if ( ! isset($params['active_r']) OR $params['active_r'] == TRUE)
368 {
adminc9a8bab2006-09-24 20:28:33 +0000369 require_once(BASEPATH.'database/DB_active_rec'.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000370
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 }
adminc1fa0742006-09-21 23:50:23 +0000383
admin46563d52006-09-24 18:14:22 +0000384 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000385
386 // Instantiate the DB adapter
admina5e812c2006-09-25 02:17:30 +0000387 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
adminb0dd10f2006-08-25 17:25:49 +0000388 $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()
admina5e812c2006-09-25 02:17:30 +0000400
401
402 // --------------------------------------------------------------------
403
404 /**
405 * Initialize Database Utilities Class
406 *
407 * @access private
408 * @param mixed database platform
409 * @param bool whether to return the object
410 * @return void
411 */
412 function _ci_init_dbutils($db = '', $return = FALSE)
413 {
414 if ($this->_ci_is_loaded('dbutils') == TRUE AND $return == FALSE)
415 {
416 return;
417 }
418
419 if ($this->_ci_is_loaded('db') == FALSE)
420 {
421 $this->_ci_init_database();
422 }
423
424 require_once(BASEPATH.'database/DB_utility'.EXT);
425 require_once(BASEPATH.'database/drivers/'.$this->db->dbdriver.'/'.$this->db->dbdriver.'_utility'.EXT);
426
427
428 // Instantiate the DB adapter
429 $driver = 'CI_DB_'.$this->db->dbdriver.'_utility';
430 $DB = new $driver();
431
432 if ($return === TRUE)
433 {
434 return $DB;
435 }
436
437 $obj =& get_instance();
438 $obj->ci_is_loaded[] = 'dbutils';
439 $obj->dbutil =& $DB;
440 }
441 // END _ci_init_database()
442
adminb0dd10f2006-08-25 17:25:49 +0000443 // --------------------------------------------------------------------
444
445 /**
446 * Returns TRUE if a class is loaded, FALSE if not
447 *
448 * @access public
449 * @param string the class name
450 * @return bool
451 */
452 function _ci_is_loaded($class)
453 {
454 return ( ! in_array($class, $this->ci_is_loaded)) ? FALSE : TRUE;
455 }
456 // END _ci_is_loaded()
457
458 // --------------------------------------------------------------------
459
460 /**
461 * Scaffolding
462 *
463 * Initializes the scaffolding.
464 *
465 * @access private
466 * @return void
467 */
468 function _ci_scaffolding()
469 {
470 if ($this->_ci_scaffolding === FALSE OR $this->_ci_scaff_table === FALSE)
471 {
472 show_404('Scaffolding unavailable');
473 }
474
475 if (class_exists('Scaffolding')) return;
476
477 if ( ! in_array($this->uri->segment(3), array('add', 'insert', 'edit', 'update', 'view', 'delete', 'do_delete')))
478 {
479 $method = 'view';
480 }
481 else
482 {
483 $method = $this->uri->segment(3);
484 }
485
486 $this->_ci_init_database("", FALSE, TRUE);
487
488 $this->_ci_initialize('pagination');
489 require_once(BASEPATH.'scaffolding/Scaffolding'.EXT);
490 $this->scaff = new Scaffolding($this->_ci_scaff_table);
491 $this->scaff->$method();
492 }
493 // END _ci_scaffolding()
494
495}
496// END _Controller class
497?>