Refactored Scaffold: deprecated generateFieldNames, aligned scaffold and bake view templates, set var $scaffold = CAKE_ADMIN; will scaffold only the admin actions

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5277 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-06-14 17:28:35 +00:00
parent a0184d2b41
commit c9a2da3e0c
6 changed files with 320 additions and 323 deletions

View file

@ -129,8 +129,9 @@ foreach($relations as $assocName => $assocData):
$otherControllerPath = Inflector::pluralize($otherModelKey);
$otherControllerName = Inflector::camelize($otherControllerPath);
$otherSingularVar = Inflector::variable($assocName);
$otherPluralHumanName = Inflector::humanize(Inflector::pluralize($assocName));
$otherSingularHumanName = Inflector::humanize($assocName);
$assocKey = Inflector::underscore($assocName);
$otherPluralHumanName = Inflector::humanize(Inflector::pluralize($assocKey));
$otherSingularHumanName = Inflector::humanize($assocKey);
$otherFields = $otherModelObj->_tableInfo->value;
$otherPrimaryKey = $otherModelObj->primaryKey;
?>

View file

@ -716,12 +716,11 @@ class Controller extends Object {
$this->render(null, false, $flash);
}
/**
* This function creates a $fieldNames array for the view to use.
* @todo Map more database field types to html form fields.
* @todo View the database field types from all the supported databases.
*
* @deprecated on 1.2.0.5258
* @see FormHelper::create();
*/
function generateFieldNames($data = null, $doCreateOptions = true) {
trigger_error(sprintf(__('Method generateFieldNames() is deprecated in %s: see FormHelper::create()', true), get_class($this)), E_USER_NOTICE);
$fieldNames = array();
$model = $this->modelClass;
$modelKey = $this->modelKey;

View file

@ -148,7 +148,7 @@ class Scaffold extends Object {
$var = $this->__passedVars[$j];
$this->{$var} = $controller->{$var};
}
$this->redirect = '/' . $this->viewPath;
$this->redirect = array('action'=> 'index');
if(!is_null($this->plugin)) {
$this->redirect = '/' . $this->plugin . '/' . $this->viewPath;
}
@ -174,30 +174,27 @@ class Scaffold extends Object {
$this->ScaffoldModel =& $this->controller->{$this->modelClass};
$this->scaffoldTitle = Inflector::humanize($this->viewPath);
$this->scaffoldActions = $controller->scaffold;
$this->controller->pageTitle= __('Scaffold :: ', true) . Inflector::humanize($this->action) . ' :: ' . $this->scaffoldTitle;
$this->controller->set('controllerName', $this->name);
$this->controller->set('controllerAction', $this->action);
$this->controller->set('modelClass', $this->modelClass);
$this->controller->set('modelKey', $this->modelKey);
$this->controller->set('humanSingularName', Inflector::humanize($this->modelKey));
$this->controller->set('humanPluralName', Inflector::humanize($this->viewPath));
$this->controller->pageTitle = __('Scaffold :: ', true) . Inflector::humanize($this->action) . ' :: ' . $this->scaffoldTitle;
$alias = array();
if(!empty($this->ScaffoldModel->alias)) {
$alias = array_combine(array_keys($this->ScaffoldModel->alias), array_keys($this->ScaffoldModel->alias));
}
$this->controller->set('alias', $alias);
//template variables
$modelClass = $this->controller->modelClass;
$modelKey = $this->controller->modelKey;
$primaryKey = $this->ScaffoldModel->primaryKey;
$displayField = $this->ScaffoldModel->displayField;
$singularVar = Inflector::variable($modelClass);
$pluralVar = Inflector::variable($this->controller->name);
$singularHumanName = Inflector::humanize($modelClass);
$pluralHumanName = Inflector::humanize($this->controller->name);
$fields = $this->ScaffoldModel->_tableInfo->value;
$foreignKeys = $this->ScaffoldModel->keyToTable;
$belongsTo = $this->ScaffoldModel->belongsTo;
$hasOne = $this->ScaffoldModel->hasOne;
$hasMany = $this->ScaffoldModel->hasMany;
$hasAndBelongsToMany = $this->ScaffoldModel->hasAndBelongsToMany;
$linked = array();
foreach($this->ScaffoldModel->tableToModel as $model){
if($model !== $this->modelClass) {
$linked[$model] = Inflector::tableize($model);
}
}
$this->controller->set('linked', $linked);
$this->controller->set('primaryKey', $this->ScaffoldModel->primaryKey);
$this->controller->set('displayField', $this->ScaffoldModel->getDisplayfield());
$this->controller->set(compact('modelClass', 'primaryKey', 'displayField', 'singularVar', 'pluralVar',
'singularHumanName', 'pluralHumanName', 'fields', 'foreignKeys',
'belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'));
$this->__scaffold($params);
}
/**
@ -213,20 +210,16 @@ class Scaffold extends Object {
if(isset($params['pass'][0])){
$this->ScaffoldModel->id = $params['pass'][0];
} elseif (isset($this->controller->Session) && $this->controller->Session->valid != false) {
$this->controller->Session->setFlash(sprintf(__("No id set for %s::view()", true), Inflector::humanize($this->modelKey)));
$this->controller->redirect($this->redirect);
} else {
return $this->controller->flash(sprintf(__("No id set for %s::view()", true), Inflector::humanize($this->modelKey)),
'/' . Inflector::underscore($this->controller->viewPath));
}
$this->controller->set('hasOne', $this->ScaffoldModel->hasOne);
$this->controller->set('relations', $relations = array_merge($this->ScaffoldModel->hasMany, $this->ScaffoldModel->hasAndBelongsToMany));
$this->controller->data = $this->ScaffoldModel->read();
$this->controller->set('data', $this->controller->data);
$this->controller->set('fieldNames', $this->controller->generateFieldNames($this->controller->data, false));
$this->controller->render($this->action, $this->layout, $this->__getViewFileName($this->action));
$this->controller->data = $this->ScaffoldModel->read();
$this->controller->set(Inflector::variable($this->controller->modelClass), $this->controller->data);
$this->controller->render($this->action, $this->layout, $this->__getViewFileName($params['action']));
} elseif ($this->controller->_scaffoldError('view') === false) {
return $this->__scaffoldError();
}
@ -240,10 +233,9 @@ class Scaffold extends Object {
*/
function __scaffoldIndex($params) {
if ($this->controller->_beforeScaffold('index')) {
$this->controller->set('fieldNames', $this->controller->generateFieldNames(null, false));
$this->ScaffoldModel->recursive = 0;
$this->controller->set('data', $this->controller->paginate());
$this->controller->render($this->action, $this->layout, $this->__getViewFileName($this->action));
$this->controller->set(Inflector::variable($this->controller->name), $this->controller->paginate());
$this->controller->render($this->action, $this->layout, $this->__getViewFileName($params['action']));
} elseif ($this->controller->_scaffoldError('index') === false) {
return $this->__scaffoldError();
}
@ -268,70 +260,66 @@ class Scaffold extends Object {
* @access private
*/
function __scaffoldSave($params = array(), $action = 'edit') {
$formName = 'Edit';
$formAction = 'edit';
$viewFileName = 'edit';
$success = __('updated', true);
if ($action === 'add') {
$formName = 'New';
$formAction = 'add';
$viewFileName = 'add';
$success = __('saved', true);
}
$this->controller->set('formName', $formName);
if ($this->controller->_beforeScaffold($action)) {
if (empty($this->controller->data)) {
if ($action == 'edit') {
if(isset($params['pass'][0])){
$this->ScaffoldModel->id = $params['pass'][0];
} elseif (isset($this->controller->Session) && $this->controller->Session->valid != false) {
$this->controller->Session->setFlash(sprintf(__("No id set for %s::edit()", true), Inflector::humanize($this->modelKey)));
$this->controller->redirect($this->redirect);
} else {
return $this->controller->flash(sprintf(__("No id set for %s::edit()", true), Inflector::humanize($this->modelKey)),
'/' . Inflector::underscore($this->controller->viewPath));
}
$this->controller->data = $this->ScaffoldModel->read();
$this->controller->set('fieldNames', $this->controller->generateFieldNames($this->controller->data));
$this->controller->set('data', $this->controller->data);
if ($action == 'edit' && !isset($params['pass'][0])) {
if (isset($this->controller->Session) && $this->controller->Session->valid != false) {
$this->controller->Session->setFlash(sprintf(__("No id set for %s::edit()", true), Inflector::humanize($this->modelKey)));
$this->controller->redirect($this->redirect);
} else {
$this->controller->set('fieldNames', $this->controller->generateFieldNames());
}
return $this->__scaffoldForm($formAction);
}
$this->controller->set('fieldNames', $this->controller->generateFieldNames());
$this->controller->cleanUpFields();
if ($action == 'create') {
$this->ScaffoldModel->create();
}
if ($this->ScaffoldModel->save($this->controller->data)) {
if ($this->controller->_afterScaffoldSave($action)) {
if (isset($this->controller->Session) && $this->controller->Session->valid != false) {
$this->controller->Session->setFlash(sprintf(__('The %1$s has been %2$s', true), Inflector::humanize($this->modelClass), $success));
$this->controller->redirect($this->redirect);
} else {
return $this->controller->flash(sprintf(__('The %1$s has been %2$s', true), Inflector::humanize($this->modelClass), $success), '/' . $this->viewPath);
}
} else {
return $this->controller->_afterScaffoldSaveError($action);
return $this->controller->flash(sprintf(__("No id set for %s::edit()", true), Inflector::humanize($this->modelKey)), $this->redirect);
}
} else {
if (isset($this->controller->Session) && $this->controller->Session->valid != false) {
$this->controller->Session->setFlash(__('Please correct errors below.', true));
$this->ScaffoldModel->id = $params['pass'][0];
}
if(!empty($this->controller->data)) {
$this->controller->cleanUpFields();
if ($action == 'create') {
$this->ScaffoldModel->create();
}
if ($this->ScaffoldModel->save($this->controller->data)) {
if ($this->controller->_afterScaffoldSave($action)) {
if (isset($this->controller->Session) && $this->controller->Session->valid != false) {
$this->controller->Session->setFlash(sprintf(__('The %1$s has been %2$s', true), Inflector::humanize($this->modelClass), $success));
$this->controller->redirect($this->redirect);
} else {
return $this->controller->flash(sprintf(__('The %1$s has been %2$s', true), Inflector::humanize($this->modelClass), $success), $this->redirect);
}
} else {
return $this->controller->_afterScaffoldSaveError($action);
}
} else {
if (isset($this->controller->Session) && $this->controller->Session->valid != false) {
$this->controller->Session->setFlash(__('Please correct errors below.', true));
}
}
$this->controller->set('data', $this->controller->data);
$this->controller->set('fieldNames', $this->controller->generateFieldNames($this->__rebuild($this->controller->data)));
$this->controller->validateErrors($this->ScaffoldModel);
$this->controller->render($formAction, $this->layout, $this->__getViewFileName('edit'));
}
if (empty($this->controller->data)) {
if($this->ScaffoldModel->id) {
$this->controller->data = $this->ScaffoldModel->read();
} else {
$this->controller->data = $this->ScaffoldModel->create();
}
}
$associations = am($this->ScaffoldModel->belongsTo, $this->ScaffoldModel->hasAndBelongsToMany);
foreach($associations as $assocName => $assocData) {
$this->controller->set(Inflector::pluralize(Inflector::variable($assocName)), $this->ScaffoldModel->{$assocName}->generateList());
}
return $this->__scaffoldForm($formAction);
} else if($this->controller->_scaffoldError($action) === false) {
return $this->__scaffoldError();
}
@ -398,31 +386,6 @@ class Scaffold extends Object {
return $this->controller->render($this->action, $this->layout, $pathToViewFile);
}
/**
* When forms are submited the arrays need to be rebuilt if
* an error occured, here the arrays are rebuilt to structure needed
*
* @param array $params data passed to forms
* @return array rebuilds the association arrays to pass back to Controller::generateFieldNames()
*/
function __rebuild($params) {
foreach($params as $model => $field) {
if (!empty($field) && is_array($field)) {
$match=array_keys($field);
if ($model == $match[0]) {
$count = 0;
foreach($field[$model] as $value) {
$params[$model][$count][$this->ScaffoldModel->primaryKey] = $value;
$count++;
}
unset ($params[$model][$model]);
}
}
}
return $params;
}
/**
* When methods are now present in a controller
* scaffoldView is used to call default Scaffold methods if:
@ -443,9 +406,12 @@ class Scaffold extends Object {
if (isset($db)) {
if(empty($this->scaffoldActions)) {
$this->scaffoldActions = array('index', 'list', 'view', 'add', 'create', 'edit', 'update', 'delete');
} else if(defined('CAKE_ADMIN') && $this->scaffoldActions == CAKE_ADMIN) {
$this->scaffoldActions = array(CAKE_ADMIN .'_index', CAKE_ADMIN .'_list', CAKE_ADMIN .'_view', CAKE_ADMIN .'_add', CAKE_ADMIN .'_create', CAKE_ADMIN .'_edit', CAKE_ADMIN .'_update', CAKE_ADMIN .'_delete');
}
if (in_array($params['action'], $this->scaffoldActions)) {
$params['action'] = str_replace(CAKE_ADMIN . '_', '', $params['action']);
switch($params['action']) {
case 'index':
$this->__scaffoldIndex($params);

View file

@ -24,30 +24,47 @@
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
?>
<div class="<?php echo $singularVar;?>">
<?php echo $form->create($modelClass);?>
<fieldset>
<legend><?php echo Inflector::humanize($this->action).' '. $singularHumanName;?></legend>
<?php
echo $form->create($modelClass);
echo $form->inputs($fieldNames);
echo $form->end(__('Save', true)); ?>
<div class="nav">
foreach($fields as $field) {
if($this->action == 'add' && $field['name'] == $primaryKey) {
continue;
} else if(!in_array($field['name'], array('created', 'modified', 'updated'))){
echo "\t\t".$form->input($field['name'])."\n";
}
}
foreach($hasAndBelongsToMany as $assocName => $assocData) {
echo "\t\t".$form->input($assocName)."\n";
}
?>
</fieldset>
<?php
echo $form->end(__('Submit', true));
?>
</div>
<div class="actions">
<ul>
<?php
if($formName == 'Edit') {
echo "<li>".$html->link(__('Delete ', true).$humanSingularName, array('action' => 'delete', $data[$modelClass][$primaryKey]), null, 'Are you sure you want to delete '.$data[$modelClass][$displayField])."</li>";
foreach($fieldNames as $field => $value) {
if(isset($value['foreignKey'])) {
echo '<li>' . $html->link(__('View ', true) . Inflector::humanize($value['controller']), array('action' => 'index')) . '</li>';
echo '<li>' . $html->link(__('Add ', true) . Inflector::humanize($value['modelKey']), array('action' => 'add')) . '</li>';
}
}
<?php if($this->action != 'add'):?>
<li><?php echo $html->link(__('Delete', true), array('action'=>'delete', $form->value($modelClass.'.'.$primaryKey)), null, 'Are you sure you want to delete #' . $form->value($modelClass.'.'.$primaryKey)); ?></li>
<?php endif;?>
<li><?php echo $html->link(__('List', true).' '.$pluralHumanName, array('action'=>'index'));?></li>
<?php
foreach($foreignKeys as $field => $value) {
$otherModelClass = $value['1'];
if($otherModelClass != $modelClass) {
$otherModelKey = Inflector::underscore($otherModelClass);
$otherControllerName = Inflector::pluralize($otherModelClass);
$otherControllerPath = Inflector::underscore($otherControllerName);
$otherSingularName = Inflector::variable($otherModelClass);
$otherPluralHumanName = Inflector::humanize($otherControllerPath);
$otherSingularHumanName = Inflector::humanize($otherModelKey);
echo "\t\t<li>".$html->link(__('List', true).' '.$otherPluralHumanName, array('controller'=> $otherControllerPath, 'action'=>'index'))."</li>\n";
echo "\t\t<li>".$html->link(__('New', true).' '.$otherSingularHumanName, array('controller'=> $otherControllerPath, 'action'=>'add'))."</li>\n";
}
echo "<li>".$html->link($humanPluralName, array('action' => 'index'))."</li>";
foreach($linked as $name => $controller) {
echo '<li>'.$html->link(Inflector::humanize($controller), array('controller'=> $controller)).'</li>';
}
?>
}
?>
</ul>
</div>

View file

@ -16,7 +16,7 @@
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.view.templates.scaffolds
* @subpackage cake.cake.console.libs.templates.views
* @since CakePHP(tm) v 0.10.0.1076
* @version $Revision$
* @modifiedby $LastChangedBy$
@ -24,80 +24,84 @@
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
?>
<h2><?php echo $humanPluralName;?></h2>
<div class="<?php echo $pluralVar;?>">
<h2><?php echo $pluralHumanName;?></h2>
<p><?php
echo $paginator->counter(array(
'format' => 'Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%'
));
?></p>
<table class="scaffold" cellpadding="0" cellspacing="0">
<thead>
<tr>
<?php foreach ($fieldNames as $fieldName) { ?>
<th>
<?php
echo $paginator->sort($fieldName['label'], $fieldName['name']);
?>
</th>
<?php }?>
<th><?php __('Actions'); ?></th>
</tr>
</thead>
<tbody>
<table cellpadding="0" cellspacing="0">
<tr>
<?php foreach($fields as $field):?>
<th><?php echo $paginator->sort("{$field['name']}");?></th>
<?php endforeach;?>
<th><?php __('Actions');?></th>
</tr>
<?php
$i = 0;
if(is_array($data)) {
foreach ($data as $row) {
if($i++ % 2 == 0) {
echo '<tr>';
} else {
echo '<tr class="altrow">';
}
foreach($fieldNames as $field => $value) {
?>
<td>
<?php
if(isset($value['foreignKey'])) {
$otherControllerName = $value['controller'];
$otherControllerPath = Inflector::underscore($value['controller']);
$otherModelObject =& ClassRegistry::getObject($value['modelKey']);
if(is_object($otherModelObject)) {
$displayText = $row[$alias[$value['model']]][$otherModelObject->getDisplayField()];
} else {
$displayText = $row[$alias[$value['model']]][$field];
}
echo $html->link($displayText, array('controller'=> $otherControllerPath, 'action'=>'view', $row[$modelClass][$field]));
} else {
echo $row[$modelClass][$field];
}
?>
</td>
<?php } ?>
<td class="actions">
<?php $id = $row[$modelClass][$primaryKey]; ?>
<?php echo $html->link(__('View', true), array('action' => 'view', $id)) ?>
<?php echo $html->link(__('Edit', true), array('action' => 'edit', $id)) ?>
<?php echo $html->link(__('Delete', true), array('action' => 'delete', $id), null, sprintf(__("Are you sure you want to delete id %s?", true), $id)) ?>
</td>
</tr>
<?php
foreach(${$pluralVar} as ${$singularVar}):
if($i++ % 2 == 0) {
$class = ' class=\"altrow\"';
} else {
$class = null;
}
}?>
</tbody>
echo "\n";
echo "\t<tr" . $class . ">\n";
foreach($fields as $field) {
if(in_array($field['name'], array_keys($foreignKeys))) {
$otherModelClass = $foreignKeys[$field['name']][1];
$otherModelKey = Inflector::underscore($otherModelClass);
$otherControllerName = Inflector::pluralize($otherModelClass);
$otherControllerPath = Inflector::underscore($otherControllerName);
if(isset($foreignKeys[$field['name']][2])) {
$otherModelClass = $foreignKeys[$field['name']][2];
}
$otherVariableName = Inflector::variable($otherModelClass);
$otherModelObj =& ClassRegistry::getObject($otherModelKey);
$otherPrimaryKey = $otherModelObj->primaryKey;
$otherDisplayField = $otherModelObj->displayField;
echo "\t\t<td>\n\t\t\t" . $html->link(${$singularVar}[$otherModelClass][$otherDisplayField], array('controller'=> $otherControllerPath, 'action'=>'view', ${$singularVar}[$otherModelClass][$otherPrimaryKey])) . "\n\t\t</td>\n";
} else {
echo "\t\t<td>\n\t\t\t" . ${$singularVar}[$modelClass][$field['name']] . " \n\t\t</td>\n";
}
}
echo "\t\t<td class=\"actions\">\n";
echo "\t\t\t" . $html->link(__('View', true), array('action'=>'view', ${$singularVar}[$modelClass][$primaryKey])) . "\n";
echo "\t\t\t" . $html->link(__('Edit', true), array('action'=>'edit', ${$singularVar}[$modelClass][$primaryKey])) . "\n";
echo "\t\t\t" . $html->link(__('Delete', true), array('action'=>'delete', ${$singularVar}[$modelClass][$primaryKey]), null, 'Are you sure you want to delete #' . ${$singularVar}[$modelClass][$primaryKey]) . "\n";
echo "\t\t</td>\n";
echo "\t</tr>\n";
endforeach;
echo "\n";
?>
</table>
</div>
<div class="paging">
<?php echo $paginator->prev('<< previous', array(), null, array('class'=>'disabled'));?>
| <?php echo $paginator->numbers();?>
<?php echo $paginator->next('next >>', array(), null, array('class'=>'disabled'));?>
<?php echo "\t" . $paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled')) . "\n";?>
| <?php echo $paginator->numbers() . "\n"?>
<?php echo "\t ". $paginator->next(__('next', true) .' >>', array(), null, array('class'=>'disabled')) . "\n";?>
</div>
<div class="actions">
<ul>
<?php
echo '<li>'.$html->link(__('New ', true).$humanSingularName, array('action' => 'add')).'</li>';
foreach($linked as $name => $controller) {
echo '<li>'.$html->link(Inflector::humanize($controller), array('controller'=> $controller)).'</li>';
<li><?php echo $html->link('New '.$singularHumanName, array('action'=>'add')); ?></li>
<?php
foreach($foreignKeys as $field => $value) {
$otherModelClass = $value['1'];
if($otherModelClass != $modelClass) {
$otherModelKey = Inflector::underscore($otherModelClass);
$otherControllerName = Inflector::pluralize($otherModelClass);
$otherControllerPath = Inflector::underscore($otherControllerName);
$otherVariableName = Inflector::variable($otherModelClass);
$otherPluralHumanName = Inflector::humanize($otherControllerPath);
$otherSingularHumanName = Inflector::humanize($otherModelKey);
echo "\t\t<li>" . $html->link(__('List', true) .' '.$otherPluralHumanName, array('controller'=> $otherControllerPath, 'action'=>'index')) . "</li>\n";
echo "\t\t<li>" . $html->link(__('New', true) .' '.$otherSingularHumanName, array('controller'=> $otherControllerPath, 'action'=>'add')) . "</li>\n";
}
?>
}
?>
</ul>
</div>

View file

@ -24,150 +24,160 @@
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
?>
<h2><?php echo sprintf(__("View %s", true), $humanSingularName)?></h2>
<dl>
<div class="<?php echo $singularVar;?>">
<h2><?php echo sprintf(__("View %s", true), $singularHumanName);?></h2>
<dl>
<?php
$i = 0;
foreach($fieldNames as $field => $value) {
foreach($fields as $field) {
if($i++ % 2 == 0) {
$class = 'class="altrow"';
$class = ' class="altrow"';
} else {
$class = null;
}
echo "<dt {$class}>".$value['label']."</dt>";
if(isset($value['foreignKey'])) {
$otherControllerName = $value['controller'];
$otherControllerPath = Inflector::underscore($value['controller']);
$otherModelObj =& ClassRegistry::getObject($value['modelKey']);
$othereDisplayField = $otherModelObj->getDisplayField();
$displayText = $data[$alias[$value['model']]][$othereDisplayField];
if(!empty($data[$modelClass][$field]) && (!empty($displayText))) {
echo "<dd {$class}>"
.$html->link($displayText,
array('controller'=> $otherControllerPath, 'action'=>'view',
$data[$modelClass][$field])
)
."</dd>";
} else {
echo "<dd {$class}>&nbsp;</dd>";
if(in_array($field['name'], array_keys($foreignKeys))) {
$otherModelClass = $foreignKeys[$field['name']][1];
$otherModelKey = Inflector::underscore($otherModelClass);
$otherControllerName = Inflector::pluralize($otherModelClass);
$otherControllerPath = Inflector::underscore($otherControllerName);
if(isset($foreignKeys[$field['name']][2])) {
$otherModelClass = $foreignKeys[$field['name']][2];
}
$otherSingularVar = Inflector::variable($otherModelClass);
$otherModelObj =& ClassRegistry::getObject($otherModelKey);
$otherPrimaryKey = $otherModelObj->primaryKey;
$otherDisplayField = $otherModelObj->displayField;
echo "\t\t<dt{$class}>".Inflector::humanize($otherModelClass)."</dt>\n";
echo "\t\t<dd{$class}>\n\t\t\t".$html->link(${$singularVar}[$otherModelClass][$otherDisplayField], array('controller'=> $otherControllerPath, 'action'=>'view', ${$singularVar}[$otherModelClass][$otherPrimaryKey])). "\n\t\t\t&nbsp;\n\t\t</dd>\n";
} else {
if( !empty($data[$modelClass][$field])) {
echo "<dd {$class}>".$data[$modelClass][$field]."</dd>";
} else {
echo "<dd {$class}>&nbsp;</dd>";
}
echo "\t\t<dt{$class}>".Inflector::humanize($field['name'])."</dt>\n";
echo "\t\t<dd{$class}>\n\t\t\t" . ${$singularVar}[$modelClass][$field['name']] . "\n\t\t\t&nbsp;\n\t\t</dd>\n";
}
}?>
</dl>
<div class="nav">
<ul>
<?php
echo '<li>' . $html->link(sprintf(__("Edit %s", true), $humanSingularName), array('action' => 'edit', $data[$modelClass][$primaryKey])) . '</li>';
echo '<li>' . $html->link(sprintf(__("Delete %s", true), $humanSingularName), array('action' => 'delete', $data[$modelClass][$primaryKey]), null, sprintf(__("Are you sure you want to delete id %s?", true), $data[$modelClass][$primaryKey])) . '</li>';
echo '<li>' . $html->link(sprintf(__("List %s", true), $humanPluralName), array('action' => 'index')) . '</li>';
echo '<li>' . $html->link(sprintf(__("New %s", true), $humanSingularName), array('action' => 'add')) . '</li>';
foreach($fieldNames as $field => $value) {
if(isset($value['foreignKey']) && $value['modelKey'] != $modelKey) {
echo "<li>".$html->link(sprintf(__("List %s", true), Inflector::humanize($value['controller'])), array('controller' => $value['controller'], 'action' => 'index')) . '</li>';
echo "<li>".$html->link(sprintf(__("Add %s", true), Inflector::humanize($value['controller'])), array('controller' => $value['controller'], 'action'=>'add'))."</li>";
}
}?>
</ul>
</div>
<!--hasOne relationships -->
<?php
$j = 0;
foreach ($hasOne as $associationName => $relation) {
$otherModelKey = Inflector::underscore($relation['className']);
$otherModelObj =& ClassRegistry::getObject($otherModelKey);
$otherControllerPath = Inflector::pluralize($otherModelKey);
$new = true;
if($j++ % 2 == 0) {
$class = 'class="altrow"';
} else {
$class = null;
}
echo "<div class=\"related\">";
echo "<h3>".sprintf(__("Related %s", true), Inflector::humanize($associationName))."</h3>";
if(!empty($data[$associationName])) {
echo "<dl>";
foreach($data[$associationName] as $field => $value) {
if(isset($value)) {
echo "<dt {$class}>".Inflector::humanize($field)."</dt>";
if(!empty($value)) {
echo "<dd {$class}>".$value."</dd>";
} else {
echo "<dd {$class}>&nbsp;</dd>";
}
$new = null;
}
}
echo "</dl>";
}
echo "<div class=\"actions\">";
echo "<ul>";
if($new == null) {
echo "<li>".$html->link(sprintf(__("Edit %s", true), Inflector::humanize($associationName)), array('controller'=> $otherControllerPath, 'action'=>'edit', $data[$associationName][$otherModelObj->primaryKey]))."</li>";
} else {
echo "<li>".$html->link(sprintf(__("New %s", true), Inflector::humanize($associationName)), array('controller'=> $otherControllerPath, 'action'=>'add'))."</li>";
}
echo "</ul>";
echo "</div>";
echo "</div>";
}
?>
<!--hasMany and hasAndBelongsToMany relationships -->
</dl>
</div>
<div class="actions">
<ul>
<?php
foreach($relations as $associationName => $relation) {
$otherModelKey = Inflector::underscore($relation['className']);
$otherModelObj = &ClassRegistry::getObject($otherModelKey);
$otherControllerPath = Inflector::pluralize($otherModelKey);
$otherModelName = $relation['className'];
echo "<div class=\"related\">";
echo "<h3>".sprintf(__("Related %s", true), Inflector::humanize($otherControllerPath))."</h3>";
if(isset($data[$associationName][0]) && is_array($data[$associationName])) {?>
<table cellspacing="0">
<tr>
<?php
$bFound = false;
foreach($data[$associationName][0] as $column => $value) {
if(false !== strpos($column, "_id")) {
$column = substr($column, 0, strpos($column, "_id" ));
}
echo "<th>".Inflector::humanize($column)."</th>";
}?>
<th>Actions</th>
</tr>
<?php
foreach($data[$associationName] as $row) {
echo "<tr>";
foreach($row as $column => $value) {
echo "<td>".$value."</td>";
}
echo "\t\t<li>" .$html->link(__('Edit', true)." ".$singularHumanName, array('action'=>'edit', ${$singularVar}[$modelClass][$primaryKey])). " </li>\n";
echo "\t\t<li>" .$html->link(__('Delete', true)." ".$singularHumanName, array('action'=>'delete', ${$singularVar}[$modelClass][$primaryKey]), null, 'Are you sure you want to delete #' . ${$singularVar}[$modelClass][$primaryKey] . '?'). " </li>\n";
echo "\t\t<li>" .$html->link(__('List', true)." ".$pluralHumanName, array('action'=>'index')). " </li>\n";
echo "\t\t<li>" .$html->link(__('New', true)." ".$singularHumanName, array('action'=>'add')). " </li>\n";
foreach($foreignKeys as $field => $value) {
$otherModelClass = $value['1'];
if($otherModelClass != $modelClass) {
$otherModelKey = Inflector::underscore($otherModelClass);
$otherControllerName = Inflector::pluralize($otherModelClass);
$otherControllerPath = Inflector::underscore($otherControllerName);
$otherSingularVar = Inflector::variable($otherModelClass);
$otherPluralHumanName = Inflector::humanize($otherControllerPath);
$otherSingularHumanName = Inflector::humanize($otherModelKey);
echo "\t\t<li>" .$html->link(__('List', true)." ".$otherPluralHumanName, array('controller'=> $otherControllerPath, 'action'=>'index')). " </li>\n";
echo "\t\t<li>" .$html->link(__('New', true)." ".$otherSingularHumanName, array('controller'=> $otherControllerPath, 'action'=>'add')). " </li>\n";
}
}
?>
<td class="actions">
<?php echo $html->link(__('View', true), array('controller'=> $otherControllerPath, 'action'=>'view', $row[$otherModelObj->primaryKey]))?>
<?php echo $html->link(__('Edit', true), array('controller'=> $otherControllerPath, 'action'=>'edit', $row[$otherModelObj->primaryKey]))?>
<?php echo $html->link(__('Delete', true), array('controller'=> $otherControllerPath, 'action'=>'delete', $row[$otherModelObj->primaryKey]), null, sprintf(__("Are you sure you want to delete id %s?", true), $row[$otherModelObj->primaryKey]))?>
</td>
</ul>
</div>
<?php
echo "</tr>";
$i = 0;
foreach ($hasOne as $assocName => $assocData):
$otherModelKey = Inflector::underscore($assocData['className']);
$otherControllerPath = Inflector::pluralize($otherModelKey);
$otherControllerName = Inflector::camelize($otherControllerPath);
$assocKey = Inflector::underscore($assocName);
$otherPluralHumanName = Inflector::humanize(Inflector::pluralize($assocKey));
$otherSingularHumanName = Inflector::humanize($assocKey);
$otherModelObj =& ClassRegistry::getObject($otherModelKey);
$otherFields = $otherModelObj->_tableInfo->value;
$otherPrimaryKey = $otherModelObj->primaryKey;
if($i++ % 2 == 0) {
$class = ' class="altrow"';
} else {
$class = null;
}
?>
<div class="related">
<h3><?php echo sprintf(__("Related %s", true), $otherPluralHumanName);?></h3>
<?php if(!empty(${$singularVar}[$assocName])):?>
<dl>
<?php
foreach($otherFields as $field) {
echo "\t\t<dt{$class}>".Inflector::humanize($field['name'])."</dt>\n";
echo "\t\t<dd{$class}>\n\t" .${$singularVar}[$assocName][$field['name']] ."\n&nbsp;</dd>\n";
}
?>
</table>
<?php
}?>
</dl>
<?php endif; ?>
<div class="actions">
<ul>
<?php echo "<li>".$html->link(__('New ', true).Inflector::humanize($otherModelKey), array('controller'=> $otherControllerPath, 'action'=>'add')) . '</li>'; ?>
<li><?php echo $html->link(__('Edit', true)." ".$otherSingularHumanName, array('controller'=> $otherControllerPath, 'action'=>'edit', ${$singularVar}[$assocName][$otherPrimaryKey]))."</li>\n";?>
</ul>
</div>
</div>
<?php }?>
<?php
endforeach;
$relations = array_merge($hasMany, $hasAndBelongsToMany);
$i = 0;
foreach($relations as $assocName => $assocData):
$otherModelKey = Inflector::underscore($assocData['className']);
$otherModelObj =& ClassRegistry::getObject($otherModelKey);
$otherControllerPath = Inflector::pluralize($otherModelKey);
$otherControllerName = Inflector::camelize($otherControllerPath);
$otherSingularVar = Inflector::variable($assocName);
$assocKey = Inflector::underscore($assocName);
$otherPluralHumanName = Inflector::humanize(Inflector::pluralize($assocKey));
$otherSingularHumanName = Inflector::humanize($assocKey);
$otherFields = $otherModelObj->_tableInfo->value;
$otherPrimaryKey = $otherModelObj->primaryKey;
?>
<div class="related">
<h3><?php echo sprintf(__("Related %s", true), $otherPluralHumanName);?></h3>
<?php if(!empty(${$singularVar}[$assocName])):?>
<table cellpadding = "0" cellspacing = "0">
<tr>
<?php
foreach($otherFields as $field) {
echo "\t\t<th>".Inflector::humanize($field['name'])."</th>\n";
}
?>
<th class="actions">Actions</th>
</tr>
<?php
$i = 0;
foreach(${$singularVar}[$assocName] as ${$otherSingularVar}):
if($i++ % 2 == 0) {
$class = ' class=\"altrow\"';
} else {
$class = null;
}
echo "\t\t<tr{$class}>\n";
foreach($otherFields as $field) {
echo "\t\t\t<td>".${$otherSingularVar}[$field['name']]."</td>\n";
}
echo "\t\t\t<td class=\"actions\">\n";
echo "\t\t\t\t" . $html->link(__('View', true), array('controller'=> $otherControllerPath, 'action'=>'view', ${$otherSingularVar}[$otherPrimaryKey])). "\n";
echo "\t\t\t\t" . $html->link(__('Edit', true), array('controller'=> $otherControllerPath, 'action'=>'edit', ${$otherSingularVar}[$otherPrimaryKey])). "\n";
echo "\t\t\t\t" . $html->link(__('Delete', true), array('controller'=> $otherControllerPath, 'action'=>'delete', ${$otherSingularVar}[$otherPrimaryKey]), null, 'Are you sure you want to delete #' . ${$otherSingularVar}[$otherPrimaryKey] . '?'). "\n";
echo "\t\t\t</td>\n";
echo "\t\t</tr>\n";
endforeach;
?>
</table>
<?php endif; ?>
<div class="actions">
<ul>
<li><?php echo $html->link(sprintf(__("New %s", true), $otherSingularHumanName), array('controller'=> $otherControllerPath, 'action'=>'add'));?> </li>
</ul>
</div>
</div>
<?php endforeach;?>