Subversion-Projekte lars-tiefland.cakephp

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* SVN FILE: $Id: scaffold.php 7945 2008-12-19 02:16:01Z gwoo $ */
3
/**
4
 * Scaffold.
5
 *
6
 * Automatic forms and actions generation for rapid web application development.
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
11
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
12
 *
13
 * Licensed under The MIT License
14
 * Redistributions of files must retain the above copyright notice.
15
 *
16
 * @filesource
17
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
18
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
19
 * @package       cake
20
 * @subpackage    cake.cake.libs.controller
21
 * @since         Cake v 0.10.0.1076
22
 * @version       $Revision: 7945 $
23
 * @modifiedby    $LastChangedBy: gwoo $
24
 * @lastmodified  $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
25
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
26
 */
27
/**
28
 * Scaffolding is a set of automatic views, forms and controllers for starting web development work faster.
29
 *
30
 * Scaffold inspects your database tables, and making educated guesses, sets up a
31
 * number of pages for each of your Models. These pages have data forms that work,
32
 * and afford the web developer an early look at the data, and the possibility to over-ride
33
 * scaffolded actions with custom-made ones.
34
 *
35
 * @package       cake
36
 * @subpackage    cake.cake.libs.controller
37
 */
38
class Scaffold extends Object {
39
/**
40
 * Controller object
41
 *
42
 * @var object
43
 * @access public
44
 */
45
	var $controller = null;
46
/**
47
 * Name of the controller to scaffold
48
 *
49
 * @var string
50
 * @access public
51
 */
52
	var $name = null;
53
/**
54
 * Action to be performed.
55
 *
56
 * @var string
57
 * @access public
58
 */
59
	var $action = null;
60
/**
61
 * Name of current model this view context is attached to
62
 *
63
 * @var string
64
 * @access public
65
 */
66
	var $model = null;
67
/**
68
 * Path to View.
69
 *
70
 * @var string
71
 * @access public
72
 */
73
	var $viewPath;
74
/**
75
 * Path parts for creating links in views.
76
 *
77
 * @var string Base URL
78
 * @access public
79
 */
80
	var $base = null;
81
/**
82
 * Name of layout to use with this View.
83
 *
84
 * @var string
85
 * @access public
86
 */
87
	var $layout = 'default';
88
/**
89
 * Array of parameter data
90
 *
91
 * @var array
92
 * @access public
93
 */
94
	var $params;
95
/**
96
 * File extension. Defaults to Cake's template ".ctp".
97
 *
98
 * @var array
99
 * @access public
100
 */
101
	var $ext = '.ctp';
102
/**
103
 * Sub-directory for this view file.
104
 *
105
 * @var string
106
 * @access public
107
 */
108
	var $subDir = null;
109
/**
110
 * Plugin name.
111
 *
112
 * @var string
113
 * @access public
114
 */
115
	var $plugin = null;
116
/**
117
 * List of variables to collect from the associated controller
118
 *
119
 * @var array
120
 * @access private
121
 */
122
	var $__passedVars = array('action', 'base', 'webroot', 'layout', 'name', 'viewPath', 'ext', 'params', 'data', 'plugin', 'cacheAction');
123
/**
124
 * Title HTML element for current scaffolded view
125
 *
126
 * @var string
127
 * @access public
128
 */
129
	var $scaffoldTitle = null;
130
/**
131
 * Construct and set up given controller with given parameters.
132
 *
133
 * @param string $controller_class Name of controller
134
 * @param array $params Parameters for scaffolding
135
 */
136
	function __construct(&$controller, $params) {
137
		$this->controller =& $controller;
138
 
139
		$count = count($this->__passedVars);
140
		for ($j = 0; $j < $count; $j++) {
141
			$var = $this->__passedVars[$j];
142
			$this->{$var} = $controller->{$var};
143
		}
144
 
145
		$this->redirect = array('action'=> 'index');
146
 
147
		$this->modelClass = $controller->modelClass;
148
		$this->modelKey = $controller->modelKey;
149
 
150
		if (!is_object($this->controller->{$this->modelClass})) {
151
			return $this->cakeError('missingModel', array(array('className' => $this->modelClass, 'webroot' => '', 'base' => $controller->base)));
152
		}
153
 
154
		$this->ScaffoldModel =& $this->controller->{$this->modelClass};
155
		$this->scaffoldTitle = Inflector::humanize($this->viewPath);
156
		$this->scaffoldActions = $controller->scaffold;
157
		$this->controller->pageTitle = __('Scaffold :: ', true) . Inflector::humanize($this->action) . ' :: ' . $this->scaffoldTitle;
158
 
159
		$modelClass = $this->controller->modelClass;
160
		$primaryKey = $this->ScaffoldModel->primaryKey;
161
		$displayField = $this->ScaffoldModel->displayField;
162
		$singularVar = Inflector::variable($modelClass);
163
		$pluralVar = Inflector::variable($this->controller->name);
164
		$singularHumanName = Inflector::humanize($modelClass);
165
		$pluralHumanName = Inflector::humanize($this->controller->name);
166
		$scaffoldFields = array_keys($this->ScaffoldModel->schema());
167
		$associations = $this->__associations();
168
 
169
		$this->controller->set(compact('modelClass', 'primaryKey', 'displayField', 'singularVar', 'pluralVar',
170
								'singularHumanName', 'pluralHumanName', 'scaffoldFields', 'associations'));
171
 
172
		if ($this->controller->view && $this->controller->view !== 'Theme') {
173
			$this->controller->view = 'scaffold';
174
		}
175
		$this->__scaffold($params);
176
	}
177
/**
178
 * Outputs the content of a scaffold method passing it through the Controller::afterFilter()
179
 *
180
 * @return void
181
 * @access protected
182
 */
183
	function _output() {
184
		$this->controller->afterFilter();
185
		echo($this->controller->output);
186
	}
187
/**
188
 * Renders a view action of scaffolded model.
189
 *
190
 * @param array $params Parameters for scaffolding
191
 * @return mixed A rendered view of a row from Models database table
192
 * @access private
193
 */
194
	function __scaffoldView($params) {
195
		if ($this->controller->_beforeScaffold('view')) {
196
 
197
			if (isset($params['pass'][0])) {
198
				$this->ScaffoldModel->id = $params['pass'][0];
199
			} elseif (isset($this->controller->Session) && $this->controller->Session->valid() != false) {
200
				$this->controller->Session->setFlash(sprintf(__("No id set for %s::view()", true), Inflector::humanize($this->modelKey)));
201
				$this->controller->redirect($this->redirect);
202
			} else {
203
				return $this->controller->flash(sprintf(__("No id set for %s::view()", true), Inflector::humanize($this->modelKey)),
204
																		'/' . Inflector::underscore($this->controller->viewPath));
205
			}
206
			$this->ScaffoldModel->recursive = 1;
207
			$this->controller->data = $this->ScaffoldModel->read();
208
			$this->controller->set(Inflector::variable($this->controller->modelClass), $this->controller->data);
209
			$this->controller->render($this->action, $this->layout);
210
			$this->_output();
211
		} elseif ($this->controller->_scaffoldError('view') === false) {
212
			return $this->__scaffoldError();
213
		}
214
	}
215
/**
216
 * Renders index action of scaffolded model.
217
 *
218
 * @param array $params Parameters for scaffolding
219
 * @return mixed A rendered view listing rows from Models database table
220
 * @access private
221
 */
222
	function __scaffoldIndex($params) {
223
		if ($this->controller->_beforeScaffold('index')) {
224
			$this->ScaffoldModel->recursive = 0;
225
			$this->controller->set(Inflector::variable($this->controller->name), $this->controller->paginate());
226
			$this->controller->render($this->action, $this->layout);
227
			$this->_output();
228
		} elseif ($this->controller->_scaffoldError('index') === false) {
229
			return $this->__scaffoldError();
230
		}
231
	}
232
/**
233
 * Renders an add or edit action for scaffolded model.
234
 *
235
 * @param string $action Action (add or edit)
236
 * @return mixed A rendered view with a form to edit or add a record in the Models database table
237
 * @access private
238
 */
239
	function __scaffoldForm($action = 'edit') {
240
		$this->controller->render($action, $this->layout);
241
		$this->_output();
242
	}
243
/**
244
 * Saves or updates the scaffolded model.
245
 *
246
 * @param array $params Parameters for scaffolding
247
 * @param string $action add or edt
248
 * @return mixed Success on save/update, add/edit form if data is empty or error if save or update fails
249
 * @access private
250
 */
251
	function __scaffoldSave($params = array(), $action = 'edit') {
252
		$formAction = 'edit';
253
		$success = __('updated', true);
254
		if ($action === 'add') {
255
			$formAction = 'add';
256
			$success = __('saved', true);
257
		}
258
 
259
		if ($this->controller->_beforeScaffold($action)) {
260
			if ($action == 'edit') {
261
				if (isset($params['pass'][0])) {
262
					$this->ScaffoldModel->id = $params['pass'][0];
263
				}
264
 
265
				if (!$this->ScaffoldModel->exists()) {
266
					if (isset($this->controller->Session) && $this->controller->Session->valid() != false) {
267
						$this->controller->Session->setFlash(sprintf(__("Invalid id for %s::edit()", true), Inflector::humanize($this->modelKey)));
268
						$this->controller->redirect($this->redirect);
269
					} else {
270
						return $this->controller->flash(sprintf(__("Invalid id for %s::edit()", true), Inflector::humanize($this->modelKey)), $this->redirect);
271
					}
272
				}
273
			}
274
 
275
			if (!empty($this->controller->data)) {
276
				if ($action == 'create') {
277
					$this->ScaffoldModel->create();
278
				}
279
 
280
				if ($this->ScaffoldModel->save($this->controller->data)) {
281
					if ($this->controller->_afterScaffoldSave($action)) {
282
						if (isset($this->controller->Session) && $this->controller->Session->valid() != false) {
283
							$this->controller->Session->setFlash(sprintf(__('The %1$s has been %2$s', true), Inflector::humanize($this->modelClass), $success));
284
							$this->controller->redirect($this->redirect);
285
						} else {
286
							return $this->controller->flash(sprintf(__('The %1$s has been %2$s', true), Inflector::humanize($this->modelClass), $success), $this->redirect);
287
						}
288
					} else {
289
						return $this->controller->_afterScaffoldSaveError($action);
290
					}
291
				} else {
292
					if (isset($this->controller->Session) && $this->controller->Session->valid() != false) {
293
						$this->controller->Session->setFlash(__('Please correct errors below.', true));
294
					}
295
				}
296
			}
297
 
298
			if (empty($this->controller->data)) {
299
				if ($this->ScaffoldModel->id) {
300
					$this->controller->data = $this->ScaffoldModel->read();
301
				} else {
302
					$this->controller->data = $this->ScaffoldModel->create();
303
				}
304
			}
305
 
306
			foreach ($this->ScaffoldModel->belongsTo as $assocName => $assocData) {
307
				$varName = Inflector::variable(Inflector::pluralize(preg_replace('/(?:_id)$/', '', $assocData['foreignKey'])));
308
				$this->controller->set($varName, $this->ScaffoldModel->{$assocName}->find('list'));
309
			}
310
			foreach ($this->ScaffoldModel->hasAndBelongsToMany as $assocName => $assocData) {
311
				$varName = Inflector::variable(Inflector::pluralize($assocName));
312
				$this->controller->set($varName, $this->ScaffoldModel->{$assocName}->find('list'));
313
			}
314
 
315
			return $this->__scaffoldForm($formAction);
316
		} elseif ($this->controller->_scaffoldError($action) === false) {
317
			return $this->__scaffoldError();
318
		}
319
	}
320
/**
321
 * Performs a delete on given scaffolded Model.
322
 *
323
 * @param array $params Parameters for scaffolding
324
 * @return mixed Success on delete, error if delete fails
325
 * @access private
326
 */
327
	function __scaffoldDelete($params = array()) {
328
		if ($this->controller->_beforeScaffold('delete')) {
329
			if (isset($params['pass'][0])) {
330
				$id = $params['pass'][0];
331
			} elseif (isset($this->controller->Session) && $this->controller->Session->valid() != false) {
332
				$this->controller->Session->setFlash(sprintf(__("No id set for %s::delete()", true), Inflector::humanize($this->modelKey)));
333
				$this->controller->redirect($this->redirect);
334
			} else {
335
				return $this->controller->flash(sprintf(__("No id set for %s::delete()", true), Inflector::humanize($this->modelKey)),
336
																	'/' . Inflector::underscore($this->controller->viewPath));
337
			}
338
 
339
			if ($this->ScaffoldModel->del($id)) {
340
				if (isset($this->controller->Session) && $this->controller->Session->valid() != false) {
341
					$this->controller->Session->setFlash(sprintf(__('The %1$s with id: %2$d has been deleted.', true), Inflector::humanize($this->modelClass), $id));
342
					$this->controller->redirect($this->redirect);
343
				} else {
344
					return $this->controller->flash(sprintf(__('The %1$s with id: %2$d has been deleted.', true), Inflector::humanize($this->modelClass), $id), '/' . $this->viewPath);
345
				}
346
			} else {
347
				if (isset($this->controller->Session) && $this->controller->Session->valid() != false) {
348
					$this->controller->Session->setFlash(sprintf(__('There was an error deleting the %1$s with id: %2$d', true), Inflector::humanize($this->modelClass), $id));
349
					$this->controller->redirect($this->redirect);
350
				} else {
351
					return $this->controller->flash(sprintf(__('There was an error deleting the %1$s with id: %2$d', true), Inflector::humanize($this->modelClass), $id), '/' . $this->viewPath);
352
				}
353
			}
354
		} elseif ($this->controller->_scaffoldError('delete') === false) {
355
			return $this->__scaffoldError();
356
		}
357
	}
358
/**
359
 * Show a scaffold error
360
 *
361
 * @return mixed A rendered view showing the error
362
 * @access private
363
 */
364
	function __scaffoldError() {
365
		return $this->controller->render('error', $this->layout);
366
		$this->_output();
367
	}
368
/**
369
 * When methods are now present in a controller
370
 * scaffoldView is used to call default Scaffold methods if:
371
 * <code>
372
 * var $scaffold;
373
 * </code>
374
 * is placed in the controller's class definition.
375
 *
376
 * @param array $params Parameters for scaffolding
377
 * @return mixed A rendered view of scaffold action, or showing the error
378
 * @access private
379
 */
380
	function __scaffold($params) {
381
		$db = &ConnectionManager::getDataSource($this->ScaffoldModel->useDbConfig);
382
		$admin = Configure::read('Routing.admin');
383
 
384
		if (isset($db)) {
385
			if (empty($this->scaffoldActions)) {
386
				$this->scaffoldActions = array('index', 'list', 'view', 'add', 'create', 'edit', 'update', 'delete');
387
			} elseif (!empty($admin) && $this->scaffoldActions === $admin) {
388
				$this->scaffoldActions = array($admin .'_index', $admin .'_list', $admin .'_view', $admin .'_add', $admin .'_create', $admin .'_edit', $admin .'_update', $admin .'_delete');
389
			}
390
 
391
			if (in_array($params['action'], $this->scaffoldActions)) {
392
				if (!empty($admin)) {
393
					$params['action'] = str_replace($admin . '_', '', $params['action']);
394
				}
395
				switch ($params['action']) {
396
					case 'index':
397
						$this->__scaffoldIndex($params);
398
					break;
399
					case 'view':
400
						$this->__scaffoldView($params);
401
					break;
402
					case 'list':
403
						$this->__scaffoldIndex($params);
404
					break;
405
					case 'add':
406
						$this->__scaffoldSave($params, 'add');
407
					break;
408
					case 'edit':
409
						$this->__scaffoldSave($params, 'edit');
410
					break;
411
					case 'create':
412
						$this->__scaffoldSave($params, 'add');
413
					break;
414
					case 'update':
415
						$this->__scaffoldSave($params, 'edit');
416
					break;
417
					case 'delete':
418
						$this->__scaffoldDelete($params);
419
					break;
420
				}
421
			} else {
422
				return $this->cakeError('missingAction', array(array('className' => $this->controller->name . "Controller",
423
																						'base' => $this->controller->base,
424
																						'action' => $this->action,
425
																						'webroot' => $this->controller->webroot)));
426
			}
427
		} else {
428
			return $this->cakeError('missingDatabase', array(array('webroot' => $this->controller->webroot)));
429
		}
430
	}
431
/**
432
 * Returns associations for controllers models.
433
 *
434
 * @return array Associations for model
435
 * @access private
436
 */
437
	function __associations() {
438
		$keys = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
439
		$associations = array();
440
 
441
		foreach ($keys as $key => $type) {
442
			foreach ($this->ScaffoldModel->{$type} as $assocKey => $assocData) {
443
				$associations[$type][$assocKey]['primaryKey'] = $this->ScaffoldModel->{$assocKey}->primaryKey;
444
				$associations[$type][$assocKey]['displayField'] = $this->ScaffoldModel->{$assocKey}->displayField;
445
				$associations[$type][$assocKey]['foreignKey'] = $assocData['foreignKey'];
446
				$associations[$type][$assocKey]['controller'] = Inflector::pluralize(Inflector::underscore($assocData['className']));
447
			}
448
		}
449
		return $associations;
450
	}
451
}
452
 
453
/**
454
 * Scaffold View.
455
 *
456
 * @package       cake
457
 * @subpackage    cake.cake.libs.controller
458
*/
459
if (!class_exists('ThemeView')) {
460
	App::import('View', 'Theme');
461
}
462
 
463
class ScaffoldView extends ThemeView {
464
/**
465
 * Override _getViewFileName
466
 *
467
 * @return string action
468
 * @access protected
469
 */
470
	function _getViewFileName($name = null) {
471
		if ($name === null) {
472
			$name = $this->action;
473
		}
474
		$name = Inflector::underscore($name);
475
		$admin = Configure::read('Routing.admin');
476
 
477
		if (!empty($admin) && strpos($name, $admin . '_') !== false) {
478
			$name = substr($name, strlen($admin) + 1);
479
		}
480
 
481
		if ($name === 'add') {
482
			$name = 'edit';
483
		}
484
 
485
		$scaffoldAction = 'scaffold.' . $name;
486
 
487
		if (!is_null($this->subDir)) {
488
			$subDir = strtolower($this->subDir) . DS;
489
		} else {
490
			$subDir = null;
491
		}
492
 
493
		$names[] = $this->viewPath . DS . $subDir . $scaffoldAction;
494
		$names[] = 'scaffolds' . DS . $subDir . $name;
495
 
496
		$paths = $this->_paths($this->plugin);
497
 
498
		$exts = array($this->ext, '.ctp', '.thtml');
499
		foreach ($paths as $path) {
500
			foreach ($names as $name) {
501
				foreach ($exts as $ext) {
502
					if (file_exists($path . $name . $ext)) {
503
						return $path . $name . $ext;
504
					}
505
				}
506
			}
507
		}
508
 
509
		if ($name === 'scaffolds' . DS . $subDir . 'error') {
510
			return LIBS . 'view' . DS . 'errors' . DS . 'scaffold_error.ctp';
511
		}
512
 
513
		return $this->_missingView($paths[0] . $name . $this->ext, 'missingView');
514
	}
515
}
516
?>