blob: 06e54b2d1a4b86b302577704785c33045276afa7 [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
131 if (in_array($name, $this->_ci_models))
132 {
133 return;
134 }
135
adminb0dd10f2006-08-25 17:25:49 +0000136 if (isset($this->$name))
137 {
138 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
139 }
140
141 $model = strtolower($model);
142
143 if ( ! file_exists(APPPATH.'models/'.$model.EXT))
144 {
145 show_error('Unable to locate the model you have specified: '.$model);
146 }
147
148 if ($db_conn !== FALSE)
149 {
150 if ($db_conn === TRUE)
151 $db_conn = '';
152
153 $this->_ci_init_database($db_conn, FALSE, TRUE);
154 }
155
156 if ( ! class_exists('Model'))
157 {
158 require_once(BASEPATH.'libraries/Model'.EXT);
159 }
160
161 require_once(APPPATH.'models/'.$model.EXT);
162
163 $model = ucfirst($model);
164 $this->$name = new $model();
165 $this->_ci_models[] = $name;
166 $this->_ci_assign_to_models();
167 }
168 // END _ci_load_model()
169
170
171 // --------------------------------------------------------------------
172
173 /**
174 * Assign to Models
175 *
176 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
177 * will be available to modles, if any exist.
178 *
179 * @access public
180 * @param object
181 * @return array
182 */
183 function _ci_assign_to_models()
184 {
185 $obj =& get_instance();
186 if (count($obj->_ci_models) == 0)
187 {
188 return;
189 }
190 foreach ($obj->_ci_models as $model)
191 {
192 $obj->$model->_assign_libraries();
193 }
194 }
195 // END _ci_assign_to_models()
196
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Auto-initialize Core Classes
202 *
203 * This initializes the core systems that are specified in the
204 * libraries/autoload.php file, as well as the systems specified in
205 * the $autoload class array above.
206 *
207 * It returns the "autoload" array so we can pass it to the Loader
208 * class since it needs to autoload plugins and helper files
209 *
210 * The config/autoload.php file contains an array that permits
211 * sub-systems to be loaded automatically.
212 *
213 * @access private
214 * @return array
215 */
216 function _ci_autoload()
217 {
218 include_once(APPPATH.'config/autoload'.EXT);
219
220 if ( ! isset($autoload))
221 {
222 return FALSE;
223 }
224
225 if (count($autoload['config']) > 0)
226 {
227 foreach ($autoload['config'] as $key => $val)
228 {
229 $this->config->load($val);
230 }
231 }
232 unset($autoload['config']);
233
234 if ( ! is_array($autoload['core']))
235 {
236 $autoload['core'] = array($autoload['core']);
237 }
238
239 foreach ($autoload['core'] as $item)
240 {
241 $this->_ci_initialize($item);
242 }
243
244 return $autoload;
245 }
246 // END _ci_autoload()
247
248 // --------------------------------------------------------------------
249
250 /**
251 * Assign the core classes to the global $CI object
252 *
253 * By assigning all the classes instantiated by the front controller
254 * local class variables we enable everything to be accessible using
255 * $this->class->function()
256 *
257 * @access private
258 * @return void
259 */
260 function _ci_assign_core()
261 {
262 foreach (array('Config', 'Input', 'Benchmark', 'URI', 'Output') as $val)
263 {
264 $class = strtolower($val);
265 $this->$class =& _load_class('CI_'.$val);
266 $this->ci_is_loaded[] = $class;
267 }
268
269 $this->lang =& _load_class('CI_Language');
270 $this->ci_is_loaded[] = 'lang';
271
272 // In PHP 4 the Controller class is a child of CI_Loader.
273 // In PHP 5 we run it as its own class.
274 if (floor(phpversion()) >= 5)
275 {
276 $this->load = new CI_Loader();
277 }
278
279 $this->ci_is_loaded[] = 'load';
280 }
281 // END _ci_assign_core()
282
283 // --------------------------------------------------------------------
284
285 /**
286 * Initialize Scaffolding
287 *
288 * This initializing function works a bit different than the
289 * others. It doesn't load the class. Instead, it simply
290 * sets a flag indicating that scaffolding is allowed to be
291 * used. The actual scaffolding function below is
292 * called by the front controller based on whether the
293 * second segment of the URL matches the "secret" scaffolding
294 * word stored in the application/config/routes.php
295 *
296 * @access private
297 * @param string the table to scaffold
298 * @return void
299 */
300 function _ci_init_scaffolding($table = FALSE)
301 {
302 if ($table === FALSE)
303 {
304 show_error('You must include the name of the table you would like access when you initialize scaffolding');
305 }
306
307 $this->_ci_scaffolding = TRUE;
308 $this->_ci_scaff_table = $table;
309 }
310 // END _ci_init_scaffolding()
311
312 // --------------------------------------------------------------------
313
314 /**
315 * Initialize Database
316 *
317 * @access private
318 * @param mixed database connection values
319 * @param bool whether to return the object for multiple connections
320 * @return void
321 */
322 function _ci_init_database($params = '', $return = FALSE, $active_record = FALSE)
323 {
324 if ($this->_ci_is_loaded('db') == TRUE AND $return == FALSE AND $active_record == FALSE)
325 {
326 return;
327 }
328
329 // Load the DB config file if needed
330 if (is_string($params) AND strpos($params, '://') === FALSE)
331 {
332 include(APPPATH.'config/database'.EXT);
333
334 $group = ($params == '') ? $active_group : $params;
335
336 if ( ! isset($db[$group]))
337 {
338 show_error('You have specified an invalid database connection group: '.$group);
339 }
340
341 $params = $db[$group];
342 }
343
344 // No DB specified yet? Beat them senseless...
345 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
346 {
347 show_error('You have not selected a database type to connect to.');
348 }
349
350 // Load the DB classes. Note: Since the active record class is optional
351 // we need to dynamically create a class that extends proper parent class
352 // based on whether we're using the active record class or not.
353 // Kudos to Paul for discovering this clever use of eval()
354
355 if ($active_record == TRUE)
356 {
357 $params['active_r'] = TRUE;
358 }
359
360 require_once(BASEPATH.'drivers/DB_driver'.EXT);
361
362 if ( ! isset($params['active_r']) OR $params['active_r'] == TRUE)
363 {
364 require_once(BASEPATH.'drivers/DB_active_record'.EXT);
365
366 if ( ! class_exists('CI_DB'))
367 {
368 eval('class CI_DB extends CI_DB_active_record { }');
369 }
370 }
371 else
372 {
373 if ( ! class_exists('CI_DB'))
374 {
375 eval('class CI_DB extends CI_DB_driver { }');
376 }
377 }
378
379 require_once(BASEPATH.'drivers/DB_'.$params['dbdriver'].EXT);
380
381 // Instantiate the DB adapter
382 $driver = 'CI_DB_'. $params['dbdriver'];
383 $DB = new $driver($params);
384
385 if ($return === TRUE)
386 {
387 return $DB;
388 }
389
390 $obj =& get_instance();
391 $obj->ci_is_loaded[] = 'db';
392 $obj->db =& $DB;
393 }
394 // END _ci_init_database()
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Returns TRUE if a class is loaded, FALSE if not
400 *
401 * @access public
402 * @param string the class name
403 * @return bool
404 */
405 function _ci_is_loaded($class)
406 {
407 return ( ! in_array($class, $this->ci_is_loaded)) ? FALSE : TRUE;
408 }
409 // END _ci_is_loaded()
410
411 // --------------------------------------------------------------------
412
413 /**
414 * Scaffolding
415 *
416 * Initializes the scaffolding.
417 *
418 * @access private
419 * @return void
420 */
421 function _ci_scaffolding()
422 {
423 if ($this->_ci_scaffolding === FALSE OR $this->_ci_scaff_table === FALSE)
424 {
425 show_404('Scaffolding unavailable');
426 }
427
428 if (class_exists('Scaffolding')) return;
429
430 if ( ! in_array($this->uri->segment(3), array('add', 'insert', 'edit', 'update', 'view', 'delete', 'do_delete')))
431 {
432 $method = 'view';
433 }
434 else
435 {
436 $method = $this->uri->segment(3);
437 }
438
439 $this->_ci_init_database("", FALSE, TRUE);
440
441 $this->_ci_initialize('pagination');
442 require_once(BASEPATH.'scaffolding/Scaffolding'.EXT);
443 $this->scaff = new Scaffolding($this->_ci_scaff_table);
444 $this->scaff->$method();
445 }
446 // END _ci_scaffolding()
447
448}
449// END _Controller class
450?>