blob: 768b154e8f1e60d775c1698750165f22489146a2 [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
235 if ( ! is_array($autoload['core']))
236 {
237 $autoload['core'] = array($autoload['core']);
238 }
239
240 foreach ($autoload['core'] as $item)
241 {
242 $this->_ci_initialize($item);
243 }
244
245 return $autoload;
246 }
247 // END _ci_autoload()
248
249 // --------------------------------------------------------------------
250
251 /**
252 * Assign the core classes to the global $CI object
253 *
254 * By assigning all the classes instantiated by the front controller
255 * local class variables we enable everything to be accessible using
256 * $this->class->function()
257 *
258 * @access private
259 * @return void
260 */
261 function _ci_assign_core()
262 {
263 foreach (array('Config', 'Input', 'Benchmark', 'URI', 'Output') as $val)
264 {
265 $class = strtolower($val);
266 $this->$class =& _load_class('CI_'.$val);
267 $this->ci_is_loaded[] = $class;
268 }
269
270 $this->lang =& _load_class('CI_Language');
271 $this->ci_is_loaded[] = 'lang';
272
273 // In PHP 4 the Controller class is a child of CI_Loader.
274 // In PHP 5 we run it as its own class.
275 if (floor(phpversion()) >= 5)
276 {
277 $this->load = new CI_Loader();
278 }
279
280 $this->ci_is_loaded[] = 'load';
281 }
282 // END _ci_assign_core()
283
284 // --------------------------------------------------------------------
285
286 /**
287 * Initialize Scaffolding
288 *
289 * This initializing function works a bit different than the
290 * others. It doesn't load the class. Instead, it simply
291 * sets a flag indicating that scaffolding is allowed to be
292 * used. The actual scaffolding function below is
293 * called by the front controller based on whether the
294 * second segment of the URL matches the "secret" scaffolding
295 * word stored in the application/config/routes.php
296 *
297 * @access private
298 * @param string the table to scaffold
299 * @return void
300 */
301 function _ci_init_scaffolding($table = FALSE)
302 {
303 if ($table === FALSE)
304 {
305 show_error('You must include the name of the table you would like access when you initialize scaffolding');
306 }
307
308 $this->_ci_scaffolding = TRUE;
309 $this->_ci_scaff_table = $table;
310 }
311 // END _ci_init_scaffolding()
312
313 // --------------------------------------------------------------------
314
315 /**
316 * Initialize Database
317 *
318 * @access private
319 * @param mixed database connection values
320 * @param bool whether to return the object for multiple connections
321 * @return void
322 */
323 function _ci_init_database($params = '', $return = FALSE, $active_record = FALSE)
324 {
325 if ($this->_ci_is_loaded('db') == TRUE AND $return == FALSE AND $active_record == FALSE)
326 {
327 return;
328 }
329
330 // Load the DB config file if needed
331 if (is_string($params) AND strpos($params, '://') === FALSE)
332 {
333 include(APPPATH.'config/database'.EXT);
334
335 $group = ($params == '') ? $active_group : $params;
336
337 if ( ! isset($db[$group]))
338 {
339 show_error('You have specified an invalid database connection group: '.$group);
340 }
341
342 $params = $db[$group];
343 }
344
345 // No DB specified yet? Beat them senseless...
346 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
347 {
348 show_error('You have not selected a database type to connect to.');
349 }
350
351 // Load the DB classes. Note: Since the active record class is optional
352 // we need to dynamically create a class that extends proper parent class
353 // based on whether we're using the active record class or not.
354 // Kudos to Paul for discovering this clever use of eval()
355
356 if ($active_record == TRUE)
357 {
358 $params['active_r'] = TRUE;
359 }
360
361 require_once(BASEPATH.'drivers/DB_driver'.EXT);
362
363 if ( ! isset($params['active_r']) OR $params['active_r'] == TRUE)
364 {
365 require_once(BASEPATH.'drivers/DB_active_record'.EXT);
366
367 if ( ! class_exists('CI_DB'))
368 {
369 eval('class CI_DB extends CI_DB_active_record { }');
370 }
371 }
372 else
373 {
374 if ( ! class_exists('CI_DB'))
375 {
376 eval('class CI_DB extends CI_DB_driver { }');
377 }
378 }
379
380 require_once(BASEPATH.'drivers/DB_'.$params['dbdriver'].EXT);
381
382 // Instantiate the DB adapter
383 $driver = 'CI_DB_'. $params['dbdriver'];
384 $DB = new $driver($params);
385
386 if ($return === TRUE)
387 {
388 return $DB;
389 }
390
391 $obj =& get_instance();
392 $obj->ci_is_loaded[] = 'db';
393 $obj->db =& $DB;
394 }
395 // END _ci_init_database()
396
397 // --------------------------------------------------------------------
398
399 /**
400 * Returns TRUE if a class is loaded, FALSE if not
401 *
402 * @access public
403 * @param string the class name
404 * @return bool
405 */
406 function _ci_is_loaded($class)
407 {
408 return ( ! in_array($class, $this->ci_is_loaded)) ? FALSE : TRUE;
409 }
410 // END _ci_is_loaded()
411
412 // --------------------------------------------------------------------
413
414 /**
415 * Scaffolding
416 *
417 * Initializes the scaffolding.
418 *
419 * @access private
420 * @return void
421 */
422 function _ci_scaffolding()
423 {
424 if ($this->_ci_scaffolding === FALSE OR $this->_ci_scaff_table === FALSE)
425 {
426 show_404('Scaffolding unavailable');
427 }
428
429 if (class_exists('Scaffolding')) return;
430
431 if ( ! in_array($this->uri->segment(3), array('add', 'insert', 'edit', 'update', 'view', 'delete', 'do_delete')))
432 {
433 $method = 'view';
434 }
435 else
436 {
437 $method = $this->uri->segment(3);
438 }
439
440 $this->_ci_init_database("", FALSE, TRUE);
441
442 $this->_ci_initialize('pagination');
443 require_once(BASEPATH.'scaffolding/Scaffolding'.EXT);
444 $this->scaff = new Scaffolding($this->_ci_scaff_table);
445 $this->scaff->$method();
446 }
447 // END _ci_scaffolding()
448
449}
450// END _Controller class
451?>