[ Index ]
 

Code source de CakePHP 1.1.13.4450

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/cake/libs/controller/ -> scaffold.php (source)

   1  <?php
   2  /* SVN FILE: $Id: scaffold.php 4409 2007-02-02 13:20:59Z phpnut $ */
   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-2007, Cake Software Foundation, Inc.
  12   *                                1785 E. Sahara Avenue, Suite 490-204
  13   *                                Las Vegas, Nevada 89104
  14   *
  15   * Licensed under The MIT License
  16   * Redistributions of files must retain the above copyright notice.
  17   *
  18   * @filesource
  19   * @copyright        Copyright 2005-2007, Cake Software Foundation, Inc.
  20   * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  21   * @package            cake
  22   * @subpackage        cake.cake.libs.controller
  23   * @since            Cake v 0.10.0.1076
  24   * @version            $Revision: 4409 $
  25   * @modifiedby        $LastChangedBy: phpnut $
  26   * @lastmodified    $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
  27   * @license            http://www.opensource.org/licenses/mit-license.php The MIT License
  28   */
  29  /**
  30   * Scaffolding is a set of automatic views, forms and controllers for starting web development work faster.
  31   *
  32   * Scaffold inspects your database tables, and making educated guesses, sets up a
  33   * number of pages for each of your Models. These pages have data forms that work,
  34   * and afford the web developer an early look at the data, and the possibility to over-ride
  35   * scaffolded actions with custom-made ones.
  36   *
  37   * @package        cake
  38   * @subpackage    cake.cake.libs.controller
  39   */
  40  class Scaffold extends Object{
  41  /**
  42   *  Name of view to render
  43   *
  44   * @var string
  45   */
  46       var $actionView = null;
  47  /**
  48   * Class name of model
  49   *
  50   * @var unknown_type
  51   */
  52       var $modelKey = null;
  53  /**
  54   * Controller object
  55   *
  56   * @var Controller
  57   */
  58       var $controllerClass = null;
  59  /**
  60   * Name of scaffolded Model
  61   *
  62   * @var string
  63   */
  64       var $modelName = null;
  65  /**
  66   * Title HTML element for current scaffolded view
  67   *
  68   * @var string
  69   */
  70       var $scaffoldTitle = null;
  71  /**
  72   * Base URL
  73   *
  74   * @var string
  75   */
  76       var $base = false;
  77  /**
  78   * Construct and set up given controller with given parameters.
  79   *
  80   * @param object $controller instance of controller
  81   * @param array $params
  82   */
  83  	function __construct(&$controller, $params) {
  84          $this->controllerClass =& $controller;
  85          $this->actionView = $controller->action;
  86          $this->modelKey  = ucwords(Inflector::singularize($controller->name));
  87          $this->scaffoldTitle = Inflector::humanize($this->modelKey);
  88          $this->viewPath = Inflector::underscore($controller->name);
  89          $this->controllerClass->pageTitle = $this->scaffoldTitle;
  90          $this->controllerClass->pageTitle = 'Scaffold :: ' . Inflector::humanize($controller->action) . ' :: ' .
  91                                                              Inflector::humanize(Inflector::pluralize($this->modelKey));
  92          $this->__scaffold($params);
  93      }
  94  /**
  95   * Renders a view view of scaffolded Model.
  96   *
  97   * @param array $params
  98   * @return A rendered view of a row from Models database table
  99   * @access private
 100   */
 101  	function __scaffoldView($params) {
 102          if ($this->controllerClass->_beforeScaffold('view')) {
 103  
 104              if(isset($params['pass'][0])){
 105                  $this->controllerClass->{$this->modelKey}->id = $params['pass'][0];
 106  
 107              } elseif (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {
 108                  $this->controllerClass->Session->setFlash('No id set for ' . Inflector::humanize($this->modelKey) . '::view().');
 109                  $this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));
 110  
 111              } else {
 112                  return $this->controllerClass->flash('No id set for ' . Inflector::humanize($this->modelKey) . '::view().',
 113                                                                          '/' . Inflector::underscore($this->controllerClass->viewPath));
 114              }
 115  
 116              $this->controllerClass->params['data'] = $this->controllerClass->{$this->modelKey}->read();
 117              $this->controllerClass->set('data', $this->controllerClass->params['data']);
 118              $this->controllerClass->set('fieldNames',$this->controllerClass->generateFieldNames(
 119                                                      $this->controllerClass->params['data'], false));
 120  
 121              if (file_exists(APP . 'views' . DS . $this->viewPath . DS . 'scaffold.view.thtml')) {
 122                  return $this->controllerClass->render($this->actionView, '',
 123                                                                      APP . 'views' . DS . $this->viewPath . DS . 'scaffold.view.thtml');
 124  
 125              } elseif (file_exists(APP . 'views' . DS . 'scaffold' . DS . 'scaffold.view.thtml')) {
 126                  return $this->controllerClass->render($this->actionView, '',
 127                                                                      APP . 'views' . DS . 'scaffold' . DS . 'scaffold.view.thtml');
 128              } else {
 129                  return $this->controllerClass->render($this->actionView, '',
 130                                                                      LIBS . 'view' . DS . 'templates' . DS . 'scaffolds' . DS . 'view.thtml');
 131              }
 132          } elseif ($this->controllerClass->_scaffoldError('view') === false) {
 133              return $this->__scaffoldError();
 134          }
 135      }
 136  /**
 137   * Renders List view of scaffolded Model.
 138   *
 139   * @param array $params
 140   * @return A rendered view listing rows from Models database table
 141   * @access private
 142   */
 143  	function __scaffoldIndex($params) {
 144          if ($this->controllerClass->_beforeScaffold('index')) {
 145              $this->controllerClass->set('fieldNames', $this->controllerClass->generateFieldNames(null, false));
 146              $this->controllerClass->{$this->modelKey}->recursive = 0;
 147              $this->controllerClass->set('data', $this->controllerClass->{$this->modelKey}->findAll());
 148  
 149              if (file_exists(APP . 'views' . DS . $this->viewPath . DS . 'scaffold.index.thtml')) {
 150                  return $this->controllerClass->render($this->actionView, '',
 151                                                                      APP . 'views' . DS . $this->viewPath . DS . 'scaffold.index.thtml');
 152  
 153              } elseif (file_exists(APP . 'views' . DS . 'scaffold' . DS . 'scaffold.index.thtml')) {
 154                  return $this->controllerClass->render($this->actionView, '',
 155                                                                      APP . 'views' . DS . 'scaffold' . DS . 'scaffold.index.thtml');
 156              } else {
 157                  return $this->controllerClass->render($this->actionView, '',
 158                                                                      LIBS . 'view' . DS . 'templates' . DS . 'scaffolds' . DS . 'index.thtml');
 159              }
 160          } elseif ($this->controllerClass->_scaffoldError('index') === false) {
 161              return $this->__scaffoldError();
 162          }
 163      }
 164  /**
 165   * Renders an Add or Edit view for scaffolded Model.
 166   *
 167   * @param array $params
 168   * @param string $params add or edit
 169   * @return A rendered view with a form to edit or add a record in the Models database table
 170   * @access private
 171   */
 172  	function __scaffoldForm($params = array(), $type) {
 173          $thtml = 'edit';
 174          $form = 'Edit';
 175  
 176          if ($type === 'add') {
 177              $thtml = 'add';
 178              $form = 'Add';
 179          }
 180  
 181          if ($this->controllerClass->_beforeScaffold($type)) {
 182              if ($type == 'edit') {
 183  
 184                  if(isset($params['pass'][0])){
 185                      $this->controllerClass->{$this->modelKey}->id = $params['pass'][0];
 186  
 187                  } elseif (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {
 188                      $this->controllerClass->Session->setFlash('No id set for ' . Inflector::humanize($this->modelKey) . '::edit().');
 189                      $this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));
 190  
 191                  } else {
 192                      return $this->controllerClass->flash('No id set for ' . Inflector::humanize($this->modelKey) . '::edit().',
 193                                                                          '/' . Inflector::underscore($this->controllerClass->viewPath));
 194                  }
 195  
 196                  $this->controllerClass->params['data']=$this->controllerClass->{$this->modelKey}->read();
 197                  $this->controllerClass->set('fieldNames', $this->controllerClass->generateFieldNames(
 198                                                                          $this->controllerClass->params['data']));
 199                  $this->controllerClass->set('data', $this->controllerClass->params['data']);
 200              } else {
 201                  $this->controllerClass->set('fieldNames', $this->controllerClass->generateFieldNames());
 202              }
 203              $this->controllerClass->set('type', $form);
 204  
 205              if (file_exists(APP . 'views' . DS . $this->viewPath . DS . 'scaffold.' . $thtml . '.thtml')) {
 206                  return $this->controllerClass->render($this->actionView, '',
 207                                                                      APP . 'views' . DS . $this->viewPath . DS . 'scaffold.' . $thtml . '.thtml');
 208              } elseif (file_exists(APP . 'views' . DS . 'scaffold' . DS . 'scaffold.' . $thtml . '.thtml')) {
 209                  return $this->controllerClass->render($this->actionView, '',
 210                                                                      APP . 'views' . DS . 'scaffold' . DS . 'scaffold.' . $thtml . '.thtml');
 211              } else {
 212                  return $this->controllerClass->render($this->actionView, '',
 213                                                                      LIBS . 'view' . DS . 'templates' . DS . 'scaffolds' . DS . 'edit.thtml');
 214              }
 215          } elseif ($this->controllerClass->_scaffoldError($type) === false) {
 216              return $this->__scaffoldError();
 217          }
 218      }
 219  /**
 220   * Saves or updates a model.
 221   *
 222   * @param array $params
 223   * @param string $type create or update
 224   * @return success on save/update, add/edit form if data is empty or error if save or update fails
 225   * @access private
 226   */
 227  	function __scaffoldSave($params = array(), $type) {
 228          $thtml = 'edit';
 229          $form = 'Edit';
 230          $success = 'updated';
 231          $formError = 'edit';
 232  
 233          if ($this->controllerClass->_beforeScaffold($type)) {
 234              if (empty($this->controllerClass->params['data'])) {
 235                  if ($type === 'create') {
 236                      $formError = 'add';
 237                  }
 238                  return $this->__scaffoldForm($params, $formError);
 239              }
 240              $this->controllerClass->set('fieldNames', $this->controllerClass->generateFieldNames());
 241              $this->controllerClass->cleanUpFields();
 242  
 243              if ($type == 'create') {
 244                  $this->controllerClass->{$this->modelKey}->create();
 245                  $thtml = 'add';
 246                  $form = 'Add';
 247                  $success = 'saved';
 248              }
 249  
 250              if ($this->controllerClass->{$this->modelKey}->save($this->controllerClass->params['data'])) {
 251  
 252                  if ($this->controllerClass->_afterScaffoldSave($type)) {
 253  
 254                      if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {
 255                          $this->controllerClass->Session->setFlash('The ' . Inflector::humanize($this->modelKey) . ' has been ' . $success . '.');
 256                          $this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));
 257                      } else {
 258                          return $this->controllerClass->flash('The ' . Inflector::humanize($this->modelKey) . ' has been ' . $success . '.',
 259                                                                              '/' . Inflector::underscore($this->controllerClass->viewPath));
 260                      }
 261                  } else {
 262                      return $this->controllerClass->_afterScaffoldSaveError($type);
 263                  }
 264              } else {
 265                  if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {
 266                      $this->controllerClass->Session->setFlash('Please correct errors below.');
 267                  }
 268                  $this->controllerClass->set('data', $this->controllerClass->params['data']);
 269                  $this->controllerClass->set('fieldNames',
 270                                                          $this->controllerClass->generateFieldNames($this->__rebuild($this->controllerClass->params['data'])));
 271                  $this->controllerClass->validateErrors($this->controllerClass->{$this->modelKey});
 272                  $this->controllerClass->set('type', $form);
 273  
 274                  if (file_exists(APP . 'views' . DS . $this->viewPath . DS . 'scaffolds' . DS . 'scaffold.' . $thtml . '.thtml')) {
 275                      return $this->controllerClass->render($this->actionView, '',
 276                                                                          APP . 'views' . DS . $this->viewPath . DS . 'scaffolds' . DS . 'scaffold.' . $thtml . '.thtml');
 277                  } elseif (file_exists(APP . 'views' . DS . 'scaffold' . DS . 'scaffold.' . $thtml . '.thtml')) {
 278                      return $this->controllerClass->render($this->actionView, '',
 279                                                                          APP . 'views' . DS . 'scaffold' . DS . 'scaffold.' . $thtml . '.thtml');
 280                  } else {
 281                      return $this->controllerClass->render($this->actionView, '',
 282                                                                          LIBS . 'view' . DS . 'templates' . DS . 'scaffolds' . DS . 'edit.thtml');
 283                  }
 284              }
 285          } elseif ($this->controllerClass->_scaffoldError($type) === false) {
 286              return $this->__scaffoldError();
 287          }
 288      }
 289  /**
 290   * Performs a delete on given scaffolded Model.
 291   *
 292   * @param array $params
 293   * @return success on delete error if delete fails
 294   * @access private
 295   */
 296  	function __scaffoldDelete($params = array()) {
 297          if ($this->controllerClass->_beforeScaffold('delete')) {
 298              $id = $params['pass'][0];
 299  
 300              if ($this->controllerClass->{$this->modelKey}->del($id)) {
 301  
 302                  if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {
 303                      $this->controllerClass->Session->setFlash('The ' . Inflector::humanize($this->modelKey) . ' with id: ' . $id . ' has been deleted.');
 304                      $this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));
 305                  } else {
 306                      return $this->controllerClass->flash('The ' . Inflector::humanize($this->modelKey) . ' with id: ' . $id . ' has been deleted.',
 307                                                                          '/' . Inflector::underscore($this->controllerClass->viewPath));
 308                  }
 309              } else {
 310                  if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {
 311                      $this->controllerClass->Session->setFlash('There was an error deleting the ' . Inflector::humanize($this->modelKey) . ' with the id ' . $id);
 312                      $this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));
 313                  } else {
 314                      return $this->controllerClass->flash('There was an error deleting the ' . Inflector::humanize($this->modelKey) . ' with the id ' . $id,
 315                                                                          '/' . Inflector::underscore($this->controllerClass->viewPath));
 316                  }
 317              }
 318          } elseif ($this->controllerClass->_scaffoldError('delete') === false) {
 319              return $this->__scaffoldError();
 320          }
 321      }
 322  /**
 323   * Enter description here...
 324   *
 325   * @return unknown
 326   */
 327  	function __scaffoldError() {
 328          if (file_exists(APP . 'views' . DS . $this->viewPath . DS . 'scaffolds' . DS . 'scaffold.error.thtml')) {
 329              return $this->controllerClass->render($this->actionView, '',
 330                                                                  APP . 'views' . DS . $this->viewPath    . DS . 'scaffolds' . DS . 'scaffold.error.thtml');
 331          } elseif (file_exists(APP . 'views' . DS . 'scaffold' . DS . 'scaffold.error.thtml')) {
 332              return $this->controllerClass->render($this->actionView, '',
 333                                                                  APP . 'views' . DS . 'scaffold' . DS . 'scaffold.error.thtml');
 334          } else {
 335              return $this->controllerClass->render($this->actionView, '',
 336                                                                  LIBS . 'view' . DS . 'templates' . DS . 'errors' . DS . 'scaffold_error.thtml');
 337          }
 338      }
 339  /**
 340   * When forms are submited the arrays need to be rebuilt if
 341   * an error occured, here the arrays are rebuilt to structure needed
 342   *
 343   * @param array $params data passed to forms
 344   * @return array rebuilds the association arrays to pass back to Controller::generateFieldNames()
 345   */
 346  	function __rebuild($params) {
 347          foreach($params as $model => $field) {
 348              if (!empty($field) && is_array($field)) {
 349                  $match = array_keys($field);
 350  
 351                  if ($model == $match[0]) {
 352                      $count = 0;
 353  
 354                      foreach($field[$model] as $value) {
 355                          $params[$model][$count][$this->controllerClass->{$this->modelKey}->{$model}->primaryKey] = $value;
 356                          $count++;
 357                      }
 358                      unset ($params[$model][$model]);
 359                  }
 360              }
 361          }
 362          return $params;
 363      }
 364  /**
 365   * When methods are now present in a controller
 366   * scaffoldView is used to call default Scaffold methods if:
 367   * <code>
 368   * var $scaffold;
 369   * </code>
 370   * is placed in the controller's class definition.
 371   *
 372   * @param string $url
 373   * @param string $controller_class
 374   * @param array $params
 375   * @since Cake v 0.10.0.172
 376   * @access private
 377   */
 378  	function __scaffold($params) {
 379          if (!in_array('Form', $this->controllerClass->helpers)) {
 380              $this->controllerClass->helpers[] = 'Form';
 381          }
 382          if($this->controllerClass->constructClasses()){
 383              $db =& ConnectionManager::getDataSource($this->controllerClass->{$this->modelKey}->useDbConfig);
 384  
 385              if (isset($db)) {
 386                  if ($params['action'] === 'index' || $params['action'] === 'list' || $params['action'] === 'view'
 387                          || $params['action'] === 'add' || $params['action'] === 'create'
 388                          || $params['action'] === 'edit' || $params['action'] === 'update'
 389                          || $params['action'] === 'delete') {
 390  
 391                      switch($params['action']) {
 392                          case 'index':
 393                              $this->__scaffoldIndex($params);
 394                          break;
 395                          case 'view':
 396                              $this->__scaffoldView($params);
 397                          break;
 398                          case 'list':
 399                              $this->__scaffoldIndex($params);
 400                          break;
 401                          case 'add':
 402                              $this->__scaffoldForm($params, 'add');
 403                          break;
 404                          case 'edit':
 405                              $this->__scaffoldForm($params, 'edit');
 406                          break;
 407                          case 'create':
 408                              $this->__scaffoldSave($params, 'create');
 409                          break;
 410                          case 'update':
 411                              $this->__scaffoldSave($params, 'update');
 412                          break;
 413                          case 'delete':
 414                              $this->__scaffoldDelete($params);
 415                          break;
 416                      }
 417                  } else {
 418                      return $this->cakeError('missingAction',
 419                                                      array(array('className' => Inflector::camelize($params['controller'] . "Controller"),
 420                                                                      'base' => $this->controllerClass->base,
 421                                                                      'action' => $params['action'],
 422                                                                      'webroot' => $this->controllerClass->webroot)));
 423                  }
 424              } else {
 425                  return $this->cakeError('missingDatabase', array(array('webroot' => $this->controllerClass->webroot)));
 426              }
 427          } else {
 428              return $this->cakeError('missingModel', array(array('className' => $this->modelKey, 'webroot' => '', 'base' => $this->base)));
 429          }
 430      }
 431  }
 432  ?>


Généré le : Sun Feb 25 19:27:47 2007 par Balluche grâce à PHPXref 0.7