scaffold.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: scaffold_8php-source.html 675 2008-12-26 00:27:14Z gwoo $ */
00003 /**
00004  * Scaffold.
00005  *
00006  * Automatic forms and actions generation for rapid web application development.
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.libs.controller
00023  * @since           Cake v 0.10.0.1076
00024  * @version         $Revision: 675 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 /**
00030  * Scaffolding is a set of automatic views, forms and controllers for starting web development work faster.
00031  *
00032  * Scaffold inspects your database tables, and making educated guesses, sets up a
00033  * number of pages for each of your Models. These pages have data forms that work,
00034  * and afford the web developer an early look at the data, and the possibility to over-ride
00035  * scaffolded actions with custom-made ones.
00036  *
00037  * @package     cake
00038  * @subpackage  cake.cake.libs.controller
00039  */
00040 class Scaffold extends Object{
00041 /**
00042  *  Name of view to render
00043  *
00044  * @var string
00045  */
00046      var $actionView = null;
00047 /**
00048  * Class name of model
00049  *
00050  * @var unknown_type
00051  */
00052      var $modelKey = null;
00053 /**
00054  * Controller object
00055  *
00056  * @var Controller
00057  */
00058      var $controllerClass = null;
00059 /**
00060  * Name of scaffolded Model
00061  *
00062  * @var string
00063  */
00064      var $modelName = null;
00065 /**
00066  * Title HTML element for current scaffolded view
00067  *
00068  * @var string
00069  */
00070      var $scaffoldTitle = null;
00071 /**
00072  * Base URL
00073  *
00074  * @var string
00075  */
00076      var $base = false;
00077 /**
00078  * Construct and set up given controller with given parameters.
00079  *
00080  * @param object $controller instance of controller
00081  * @param array $params
00082  */
00083     function __construct(&$controller, $params) {
00084         $this->controllerClass =& $controller;
00085         $this->actionView = $controller->action;
00086         $this->modelKey  = ucwords(Inflector::singularize($controller->name));
00087         $this->scaffoldTitle = Inflector::humanize($this->modelKey);
00088         $this->viewPath = Inflector::underscore($controller->name);
00089         $this->controllerClass->pageTitle = $this->scaffoldTitle;
00090         $this->controllerClass->pageTitle = 'Scaffold :: ' . Inflector::humanize($controller->action) . ' :: ' .
00091                                                             Inflector::humanize(Inflector::pluralize($this->modelKey));
00092         $this->__scaffold($params);
00093     }
00094 /**
00095  * Renders a view view of scaffolded Model.
00096  *
00097  * @param array $params
00098  * @return A rendered view of a row from Models database table
00099  * @access private
00100  */
00101     function __scaffoldView($params) {
00102         if ($this->controllerClass->_beforeScaffold('view')) {
00103 
00104             if (isset($params['pass'][0])) {
00105                 $this->controllerClass->{$this->modelKey}->id = $params['pass'][0];
00106 
00107             } elseif (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid() != false) {
00108                 $this->controllerClass->Session->setFlash('No id set for ' . Inflector::humanize($this->modelKey) . '::view().');
00109                 $this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));
00110 
00111             } else {
00112                 return $this->controllerClass->flash('No id set for ' . Inflector::humanize($this->modelKey) . '::view().',
00113                                                                         '/' . Inflector::underscore($this->controllerClass->viewPath));
00114             }
00115 
00116             $this->controllerClass->params['data'] = $this->controllerClass->{$this->modelKey}->read();
00117             $this->controllerClass->set('data', $this->controllerClass->params['data']);
00118             $this->controllerClass->set('fieldNames',$this->controllerClass->generateFieldNames(
00119                                                     $this->controllerClass->params['data'], false));
00120 
00121             if (file_exists(APP . 'views' . DS . $this->viewPath . DS . 'scaffold.view.thtml')) {
00122                 return $this->controllerClass->render($this->actionView, '',
00123                                                                     APP . 'views' . DS . $this->viewPath . DS . 'scaffold.view.thtml');
00124 
00125             } elseif (file_exists(APP . 'views' . DS . 'scaffold' . DS . 'scaffold.view.thtml')) {
00126                 return $this->controllerClass->render($this->actionView, '',
00127                                                                     APP . 'views' . DS . 'scaffold' . DS . 'scaffold.view.thtml');
00128             } else {
00129                 return $this->controllerClass->render($this->actionView, '',
00130                                                                     LIBS . 'view' . DS . 'templates' . DS . 'scaffolds' . DS . 'view.thtml');
00131             }
00132         } elseif ($this->controllerClass->_scaffoldError('view') === false) {
00133             return $this->__scaffoldError();
00134         }
00135     }
00136 /**
00137  * Renders List view of scaffolded Model.
00138  *
00139  * @param array $params
00140  * @return A rendered view listing rows from Models database table
00141  * @access private
00142  */
00143     function __scaffoldIndex($params) {
00144         if ($this->controllerClass->_beforeScaffold('index')) {
00145             $this->controllerClass->set('fieldNames', $this->controllerClass->generateFieldNames(null, false));
00146             $this->controllerClass->{$this->modelKey}->recursive = 0;
00147             $this->controllerClass->set('data', $this->controllerClass->{$this->modelKey}->findAll());
00148 
00149             if (file_exists(APP . 'views' . DS . $this->viewPath . DS . 'scaffold.index.thtml')) {
00150                 return $this->controllerClass->render($this->actionView, '',
00151                                                                     APP . 'views' . DS . $this->viewPath . DS . 'scaffold.index.thtml');
00152 
00153             } elseif (file_exists(APP . 'views' . DS . 'scaffold' . DS . 'scaffold.index.thtml')) {
00154                 return $this->controllerClass->render($this->actionView, '',
00155                                                                     APP . 'views' . DS . 'scaffold' . DS . 'scaffold.index.thtml');
00156             } else {
00157                 return $this->controllerClass->render($this->actionView, '',
00158                                                                     LIBS . 'view' . DS . 'templates' . DS . 'scaffolds' . DS . 'index.thtml');
00159             }
00160         } elseif ($this->controllerClass->_scaffoldError('index') === false) {
00161             return $this->__scaffoldError();
00162         }
00163     }
00164 /**
00165  * Renders an Add or Edit view for scaffolded Model.
00166  *
00167  * @param array $params
00168  * @param string $params add or edit
00169  * @return A rendered view with a form to edit or add a record in the Models database table
00170  * @access private
00171  */
00172     function __scaffoldForm($params = array(), $type) {
00173         $thtml = 'edit';
00174         $form = 'Edit';
00175 
00176         if ($type === 'add') {
00177             $thtml = 'add';
00178             $form = 'Add';
00179         }
00180 
00181         if ($this->controllerClass->_beforeScaffold($type)) {
00182             if ($type == 'edit') {
00183 
00184                 if (isset($params['pass'][0])) {
00185                     $this->controllerClass->{$this->modelKey}->id = $params['pass'][0];
00186 
00187                 } elseif (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid() != false) {
00188                     $this->controllerClass->Session->setFlash('No id set for ' . Inflector::humanize($this->modelKey) . '::edit().');
00189                     $this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));
00190 
00191                 } else {
00192                     return $this->controllerClass->flash('No id set for ' . Inflector::humanize($this->modelKey) . '::edit().',
00193                                                                         '/' . Inflector::underscore($this->controllerClass->viewPath));
00194                 }
00195 
00196                 $this->controllerClass->params['data']=$this->controllerClass->{$this->modelKey}->read();
00197                 $this->controllerClass->set('fieldNames', $this->controllerClass->generateFieldNames(
00198                                                                         $this->controllerClass->params['data']));
00199                 $this->controllerClass->set('data', $this->controllerClass->params['data']);
00200             } else {
00201                 $this->controllerClass->set('fieldNames', $this->controllerClass->generateFieldNames());
00202             }
00203             $this->controllerClass->set('type', $form);
00204 
00205             if (file_exists(APP . 'views' . DS . $this->viewPath . DS . 'scaffold.' . $thtml . '.thtml')) {
00206                 return $this->controllerClass->render($this->actionView, '',
00207                                                                     APP . 'views' . DS . $this->viewPath . DS . 'scaffold.' . $thtml . '.thtml');
00208             } elseif (file_exists(APP . 'views' . DS . 'scaffold' . DS . 'scaffold.' . $thtml . '.thtml')) {
00209                 return $this->controllerClass->render($this->actionView, '',
00210                                                                     APP . 'views' . DS . 'scaffold' . DS . 'scaffold.' . $thtml . '.thtml');
00211             } else {
00212                 return $this->controllerClass->render($this->actionView, '',
00213                                                                     LIBS . 'view' . DS . 'templates' . DS . 'scaffolds' . DS . 'edit.thtml');
00214             }
00215         } elseif ($this->controllerClass->_scaffoldError($type) === false) {
00216             return $this->__scaffoldError();
00217         }
00218     }
00219 /**
00220  * Saves or updates a model.
00221  *
00222  * @param array $params
00223  * @param string $type create or update
00224  * @return success on save/update, add/edit form if data is empty or error if save or update fails
00225  * @access private
00226  */
00227     function __scaffoldSave($params = array(), $type) {
00228         $thtml = 'edit';
00229         $form = 'Edit';
00230         $success = 'updated';
00231         $formError = 'edit';
00232 
00233         if ($this->controllerClass->_beforeScaffold($type)) {
00234             if (empty($this->controllerClass->params['data'])) {
00235                 if ($type === 'create') {
00236                     $formError = 'add';
00237                 }
00238                 return $this->__scaffoldForm($params, $formError);
00239             }
00240             $this->controllerClass->set('fieldNames', $this->controllerClass->generateFieldNames());
00241             $this->controllerClass->cleanUpFields();
00242 
00243             if ($type == 'create') {
00244                 $this->controllerClass->{$this->modelKey}->create();
00245                 $thtml = 'add';
00246                 $form = 'Add';
00247                 $success = 'saved';
00248             }
00249 
00250             if ($this->controllerClass->{$this->modelKey}->save($this->controllerClass->params['data'])) {
00251 
00252                 if ($this->controllerClass->_afterScaffoldSave($type)) {
00253 
00254                     if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid() != false) {
00255                         $this->controllerClass->Session->setFlash('The ' . Inflector::humanize($this->modelKey) . ' has been ' . $success . '.');
00256                         $this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));
00257                     } else {
00258                         return $this->controllerClass->flash('The ' . Inflector::humanize($this->modelKey) . ' has been ' . $success . '.',
00259                                                                             '/' . Inflector::underscore($this->controllerClass->viewPath));
00260                     }
00261                 } else {
00262                     return $this->controllerClass->_afterScaffoldSaveError($type);
00263                 }
00264             } else {
00265                 if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid() != false) {
00266                     $this->controllerClass->Session->setFlash('Please correct errors below.');
00267                 }
00268                 $this->controllerClass->set('data', $this->controllerClass->params['data']);
00269                 $this->controllerClass->set('fieldNames',
00270                                                         $this->controllerClass->generateFieldNames($this->__rebuild($this->controllerClass->params['data'])));
00271                 $this->controllerClass->validateErrors($this->controllerClass->{$this->modelKey});
00272                 $this->controllerClass->set('type', $form);
00273 
00274                 if (file_exists(APP . 'views' . DS . $this->viewPath . DS . 'scaffold.' . $thtml . '.thtml')) {
00275                     return $this->controllerClass->render($this->actionView, '',
00276                                                                         APP . 'views' . DS . $this->viewPath . DS . 'scaffold.' . $thtml . '.thtml');
00277                 } elseif (file_exists(APP . 'views' . DS . 'scaffold' . DS . 'scaffold.' . $thtml . '.thtml')) {
00278                     return $this->controllerClass->render($this->actionView, '',
00279                                                                         APP . 'views' . DS . 'scaffold' . DS . 'scaffold.' . $thtml . '.thtml');
00280                 } else {
00281                     return $this->controllerClass->render($this->actionView, '',
00282                                                                         LIBS . 'view' . DS . 'templates' . DS . 'scaffolds' . DS . 'edit.thtml');
00283                 }
00284             }
00285         } elseif ($this->controllerClass->_scaffoldError($type) === false) {
00286             return $this->__scaffoldError();
00287         }
00288     }
00289 /**
00290  * Performs a delete on given scaffolded Model.
00291  *
00292  * @param array $params
00293  * @return success on delete error if delete fails
00294  * @access private
00295  */
00296     function __scaffoldDelete($params = array()) {
00297         if ($this->controllerClass->_beforeScaffold('delete')) {
00298             $id = $params['pass'][0];
00299 
00300             if ($this->controllerClass->{$this->modelKey}->del($id)) {
00301 
00302                 if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid() != false) {
00303                     $this->controllerClass->Session->setFlash('The ' . Inflector::humanize($this->modelKey) . ' with id: ' . $id . ' has been deleted.');
00304                     $this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));
00305                 } else {
00306                     return $this->controllerClass->flash('The ' . Inflector::humanize($this->modelKey) . ' with id: ' . $id . ' has been deleted.',
00307                                                                         '/' . Inflector::underscore($this->controllerClass->viewPath));
00308                 }
00309             } else {
00310                 if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid() != false) {
00311                     $this->controllerClass->Session->setFlash('There was an error deleting the ' . Inflector::humanize($this->modelKey) . ' with the id ' . $id);
00312                     $this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));
00313                 } else {
00314                     return $this->controllerClass->flash('There was an error deleting the ' . Inflector::humanize($this->modelKey) . ' with the id ' . $id,
00315                                                                         '/' . Inflector::underscore($this->controllerClass->viewPath));
00316                 }
00317             }
00318         } elseif ($this->controllerClass->_scaffoldError('delete') === false) {
00319             return $this->__scaffoldError();
00320         }
00321     }
00322 /**
00323  * Enter description here...
00324  *
00325  * @return unknown
00326  */
00327     function __scaffoldError() {
00328         if (file_exists(APP . 'views' . DS . $this->viewPath . DS . 'scaffold.error.thtml')) {
00329             return $this->controllerClass->render($this->actionView, '',
00330                                                                 APP . 'views' . DS . $this->viewPath . DS . 'scaffold.error.thtml');
00331         } elseif (file_exists(APP . 'views' . DS . 'scaffold' . DS . 'scaffold.error.thtml')) {
00332             return $this->controllerClass->render($this->actionView, '',
00333                                                                 APP . 'views' . DS . 'scaffold' . DS . 'scaffold.error.thtml');
00334         } else {
00335             return $this->controllerClass->render($this->actionView, '',
00336                                                                 LIBS . 'view' . DS . 'templates' . DS . 'errors' . DS . 'scaffold_error.thtml');
00337         }
00338     }
00339 /**
00340  * When forms are submited the arrays need to be rebuilt if
00341  * an error occured, here the arrays are rebuilt to structure needed
00342  *
00343  * @param array $params data passed to forms
00344  * @return array rebuilds the association arrays to pass back to Controller::generateFieldNames()
00345  */
00346     function __rebuild($params) {
00347         foreach ($params as $model => $field) {
00348             if (!empty($field) && is_array($field)) {
00349                 $match = array_keys($field);
00350 
00351                 if ($model == $match[0]) {
00352                     $count = 0;
00353 
00354                     foreach ($field[$model] as $value) {
00355                         $params[$model][$count][$this->controllerClass->{$this->modelKey}->{$model}->primaryKey] = $value;
00356                         $count++;
00357                     }
00358                     unset ($params[$model][$model]);
00359                 }
00360             }
00361         }
00362         return $params;
00363     }
00364 /**
00365  * When methods are now present in a controller
00366  * scaffoldView is used to call default Scaffold methods if:
00367  * <code>
00368  * var $scaffold;
00369  * </code>
00370  * is placed in the controller's class definition.
00371  *
00372  * @param string $url
00373  * @param string $controller_class
00374  * @param array $params
00375  * @since Cake v 0.10.0.172
00376  * @access private
00377  */
00378     function __scaffold($params) {
00379         if (!in_array('Form', $this->controllerClass->helpers)) {
00380             $this->controllerClass->helpers[] = 'Form';
00381         }
00382         if ($this->controllerClass->constructClasses()) {
00383             $db =& ConnectionManager::getDataSource($this->controllerClass->{$this->modelKey}->useDbConfig);
00384 
00385             if (isset($db)) {
00386                 if ($params['action'] === 'index' || $params['action'] === 'list' || $params['action'] === 'view'
00387                         || $params['action'] === 'add' || $params['action'] === 'create'
00388                         || $params['action'] === 'edit' || $params['action'] === 'update'
00389                         || $params['action'] === 'delete') {
00390 
00391                     switch($params['action']) {
00392                         case 'index':
00393                             $this->__scaffoldIndex($params);
00394                         break;
00395                         case 'view':
00396                             $this->__scaffoldView($params);
00397                         break;
00398                         case 'list':
00399                             $this->__scaffoldIndex($params);
00400                         break;
00401                         case 'add':
00402                             $this->__scaffoldForm($params, 'add');
00403                         break;
00404                         case 'edit':
00405                             $this->__scaffoldForm($params, 'edit');
00406                         break;
00407                         case 'create':
00408                             $this->__scaffoldSave($params, 'create');
00409                         break;
00410                         case 'update':
00411                             $this->__scaffoldSave($params, 'update');
00412                         break;
00413                         case 'delete':
00414                             $this->__scaffoldDelete($params);
00415                         break;
00416                     }
00417                 } else {
00418                     return $this->cakeError('missingAction',
00419                                                     array(array('className' => Inflector::camelize($params['controller'] . "Controller"),
00420                                                                     'base' => $this->controllerClass->base,
00421                                                                     'action' => $params['action'],
00422                                                                     'webroot' => $this->controllerClass->webroot)));
00423                 }
00424             } else {
00425                 return $this->cakeError('missingDatabase', array(array('webroot' => $this->controllerClass->webroot)));
00426             }
00427         } else {
00428             return $this->cakeError('missingModel', array(array('className' => $this->modelKey, 'webroot' => '', 'base' => $this->base)));
00429         }
00430     }
00431 }
00432 ?>