00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 class Scaffold extends Object{
00041
00042
00043
00044
00045
00046 var $actionView = null;
00047
00048
00049
00050
00051
00052 var $modelKey = null;
00053
00054
00055
00056
00057
00058 var $controllerClass = null;
00059
00060
00061
00062
00063
00064 var $modelName = null;
00065
00066
00067
00068
00069
00070 var $scaffoldTitle = null;
00071
00072
00073
00074
00075
00076 var $base = false;
00077
00078
00079
00080
00081
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
00096
00097
00098
00099
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
00138
00139
00140
00141
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
00166
00167
00168
00169
00170
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
00221
00222
00223
00224
00225
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
00291
00292
00293
00294
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
00324
00325
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
00341
00342
00343
00344
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
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
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 ?>