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