📄 bake.php.svn-base
字号:
$otherModelKey = Inflector::underscore($otherModelName); $otherModelObj =& ClassRegistry::getObject($otherModelKey); $otherControllerName = $this->__controllerName($otherModelName); $otherControllerPath = $this->__controllerPath($otherControllerName); $displayField = $otherModelObj->getDisplayField(); $viewView .= "\t<dd> <?php echo \$html->link(\$".$singularName."['{$alias[$count]}']['{$displayField}'], '{$admin_url}/" . $otherControllerPath . "/view/' .\$".$singularName."['{$alias[$count]}']['{$otherModelObj->primaryKey}'])?></dd>\n"; $count++; } else { $viewView .= "\t<dd> <?php echo \$".$singularName."['{$modelObj->name}']['{$field}']?></dd>\n"; } } $viewView .= "</dl>\n"; $viewView .= "<ul class=\"actions\">\n"; $viewView .= "\t<li><?php echo \$html->link('Edit " . $singularHumanName . "', '{$admin_url}/{$controllerPath}/edit/' . \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}']) ?> </li>\n"; $viewView .= "\t<li><?php echo \$html->link('Delete " . $singularHumanName . "', '{$admin_url}/{$controllerPath}/delete/' . \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}'], null, 'Are you sure you want to delete: id ' . \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}'] . '?') ?> </li>\n"; $viewView .= "\t<li><?php echo \$html->link('List " . $pluralHumanName ."', '{$admin_url}/{$controllerPath}/index') ?> </li>\n"; $viewView .= "\t<li><?php echo \$html->link('New " . $singularHumanName . "', '{$admin_url}/{$controllerPath}/add') ?> </li>\n"; foreach( $fieldNames as $field => $value ) { if( isset( $value['foreignKey'] ) ) { $otherModelName = $this->__modelName($value['model']); if($otherModelName != $currentModelName) { $otherControllerName = $this->__controllerName($otherModelName); $otherControllerPath = $this->__controllerPath($otherControllerName); $otherSingularHumanName = $this->__singularHumanName($value['controller']); $otherPluralHumanName = $this->__pluralHumanName($value['controller']); $viewView .= "\t<li><?php echo \$html->link('List " . $otherSingularHumanName . "', '{$admin_url}/" . $otherControllerPath . "/index/')?> </li>\n"; $viewView .= "\t<li><?php echo \$html->link('New " . $otherPluralHumanName . "', '{$admin_url}/" . $otherControllerPath . "/add/')?> </li>\n"; } } } $viewView .= "</ul>\n\n"; $viewView .= "</div>\n"; foreach ($modelObj->hasOne as $associationName => $relation) { $new = true; $otherModelName = $this->__modelName($relation['className']); $otherControllerName = $this->__controllerName($otherModelName); $otherControllerPath = $this->__controllerPath($otherModelName); $otherSingularName = $this->__singularName($associationName); $otherPluralHumanName = $this->__pluralHumanName($associationName); $otherSingularHumanName = $this->__singularHumanName($associationName); $viewView .= "<div class=\"related\">\n"; $viewView .= "<h3>Related " . $otherPluralHumanName . "</h3>\n"; $viewView .= "<?php if(!empty(\$".$singularName."['{$associationName}'])): ?>\n"; $viewView .= "<dl>\n"; $viewView .= "\t<?php foreach(\$".$singularName."['{$associationName}'] as \$field => \$value): ?>\n"; $viewView .= "\t\t<dt><?php echo \$field ?></dt>\n"; $viewView .= "\t\t<dd> <?php echo \$value ?></dd>\n"; $viewView .= "\t<?php endforeach; ?>\n"; $viewView .= "</dl>\n"; $viewView .= "<?php endif; ?>\n"; $viewView .= "<ul class=\"actions\">\n"; $viewView .= "\t<li><?php echo \$html->link('Edit " . $otherSingularHumanName . "', '{$admin_url}/" .$otherControllerPath."/edit/' . \$".$singularName."['{$associationName}']['" . $modelObj->{$otherModelName}->primaryKey . "']);?></li>\n"; $viewView .= "\t<li><?php echo \$html->link('New " . $otherSingularHumanName . "', '{$admin_url}/" .$otherControllerPath."/add/');?> </li>\n"; $viewView .= "</ul>\n"; $viewView .= "</div>\n"; } $relations = array_merge($modelObj->hasMany, $modelObj->hasAndBelongsToMany); foreach($relations as $associationName => $relation) { $otherModelName = $this->__modelName($relation['className']); $otherControllerName = $this->__controllerName($otherModelName); $otherControllerPath = $this->__controllerPath($otherModelName); $otherSingularName = $this->__singularName($associationName); $otherPluralHumanName = $this->__pluralHumanName($associationName); $otherSingularHumanName = $this->__singularHumanName($associationName); $otherModelKey = Inflector::underscore($otherModelName); $otherModelObj =& ClassRegistry::getObject($otherModelKey); $viewView .= "<div class=\"related\">\n"; $viewView .= "<h3>Related " . $otherPluralHumanName . "</h3>\n"; $viewView .= "<?php if(!empty(\$".$singularName."['{$associationName}'])):?>\n"; $viewView .= "<table cellpadding=\"0\" cellspacing=\"0\">\n"; $viewView .= "<tr>\n"; $viewView .= "<?php foreach(\$".$singularName."['{$associationName}']['0'] as \$column => \$value): ?>\n"; $viewView .= "<th><?php echo \$column?></th>\n"; $viewView .= "<?php endforeach; ?>\n"; $viewView .= "<th>Actions</th>\n"; $viewView .= "</tr>\n"; $viewView .= "<?php foreach(\$".$singularName."['{$associationName}'] as \$".$otherSingularName."):?>\n"; $viewView .= "<tr>\n"; $viewView .= "\t<?php foreach(\$".$otherSingularName." as \$column => \$value):?>\n"; $viewView .= "\t\t<td><?php echo \$value;?></td>\n"; $viewView .= "\t<?php endforeach;?>\n"; $viewView .= "\t<td nowrap>\n"; $viewView .= "\t\t<?php echo \$html->link('View', '{$admin_url}/" . $otherControllerPath . "/view/' . \$".$otherSingularName."['{$otherModelObj->primaryKey}']);?>\n"; $viewView .= "\t\t<?php echo \$html->link('Edit', '{$admin_url}/" . $otherControllerPath . "/edit/' . \$".$otherSingularName."['{$otherModelObj->primaryKey}']);?>\n"; $viewView .= "\t\t<?php echo \$html->link('Delete', '{$admin_url}/" . $otherControllerPath . "/delete/' . \$".$otherSingularName."['{$otherModelObj->primaryKey}'], null, 'Are you sure you want to delete: id ' . \$".$otherSingularName."['{$otherModelObj->primaryKey}'] . '?');?>\n"; $viewView .= "\t</td>\n"; $viewView .= "</tr>\n"; $viewView .= "<?php endforeach; ?>\n"; $viewView .= "</table>\n"; $viewView .= "<?php endif; ?>\n\n"; $viewView .= "<ul class=\"actions\">\n"; $viewView .= "\t<li><?php echo \$html->link('New " . $otherSingularHumanName . "', '{$admin_url}/" .$otherControllerPath."/add/');?> </li>\n"; $viewView .= "</ul>\n"; $viewView .= "</div>\n"; } //-------------------------[ADD]-------------------------// $addView = null; $addView .= "<h2>New " . $singularHumanName . "</h2>\n"; $addView .= "<form action=\"<?php echo \$html->url('{$admin_url}/{$controllerPath}/add'); ?>\" method=\"post\">\n"; $addView .= $this->generateFields($controllerObj->generateFieldNames(null, true)); $addView .= $this->generateSubmitDiv('Add'); $addView .= "</form>\n"; $addView .= "<ul class=\"actions\">\n"; $addView .= "<li><?php echo \$html->link('List {$pluralHumanName}', '{$admin_url}/{$controllerPath}/index')?></li>\n"; foreach ($modelObj->belongsTo as $associationName => $relation) { $otherModelName = $this->__modelName($associationName); if($otherModelName != $currentModelName) { $otherControllerName = $this->__controllerName($otherModelName); $otherControllerPath = $this->__controllerPath($otherModelName); $otherSingularName = $this->__singularName($associationName); $otherPluralName = $this->__pluralHumanName($associationName); $addView .= "<li><?php echo \$html->link('View " . $otherPluralName . "', '{$admin_url}/" .$otherControllerPath."/index/');?></li>\n"; $addView .= "<li><?php echo \$html->link('Add " . $otherPluralName . "', '{$admin_url}/" .$otherControllerPath."/add/');?></li>\n"; } } $addView .= "</ul>\n"; //-------------------------[EDIT]-------------------------// $editView = null; $editView .= "<h2>Edit " . $singularHumanName . "</h2>\n"; $editView .= "<form action=\"<?php echo \$html->url('{$admin_url}/{$controllerPath}/edit/'.\$html->tagValue('{$modelObj->name}/{$modelObj->primaryKey}')); ?>\" method=\"post\">\n"; $editView .= $this->generateFields($controllerObj->generateFieldNames(null, true)); $editView .= "<?php echo \$html->hidden('{$modelObj->name}/{$modelObj->primaryKey}')?>\n"; $editView .= $this->generateSubmitDiv('Save'); $editView .= "</form>\n"; $editView .= "<ul class=\"actions\">\n"; $editView .= "<li><?php echo \$html->link('Delete','{$admin_url}/{$controllerPath}/delete/' . \$html->tagValue('{$modelObj->name}/{$modelObj->primaryKey}'), null, 'Are you sure you want to delete: id ' . \$html->tagValue('{$modelObj->name}/{$modelObj->primaryKey}'));?>\n"; $editView .= "<li><?php echo \$html->link('List {$pluralHumanName}', '{$admin_url}/{$controllerPath}/index')?></li>\n"; foreach ($modelObj->belongsTo as $associationName => $relation) { $otherModelName = $this->__modelName($associationName); if($otherModelName != $currentModelName) { $otherControllerName = $this->__controllerName($otherModelName); $otherControllerPath = $this->__controllerPath($otherModelName); $otherSingularName = $this->__singularName($associationName); $otherPluralName = $this->__pluralHumanName($associationName); $editView .= "<li><?php echo \$html->link('View " . $otherPluralName . "', '{$admin_url}/" .$otherControllerPath."/index/');?></li>\n"; $editView .= "<li><?php echo \$html->link('Add " . $otherPluralName . "', '{$admin_url}/" .$otherControllerPath."/add/');?></li>\n"; } } $editView .= "</ul>\n"; //------------------------------------------------------------------------------------// if(!file_exists(VIEWS.$controllerPath)) { mkdir(VIEWS.$controllerPath); } $filename = VIEWS . $controllerPath . DS . $admin . 'index.thtml'; $this->__createFile($filename, $indexView); $filename = VIEWS . $controllerPath . DS . $admin . 'view.thtml'; $this->__createFile($filename, $viewView); $filename = VIEWS . $controllerPath . DS . $admin . 'add.thtml'; $this->__createFile($filename, $addView); $filename = VIEWS . $controllerPath . DS . $admin . 'edit.thtml'; $this->__createFile($filename, $editView); }/** * Action to create a Controller. * */ function doController() { $this->hr(); $this->stdout('Controller Bake:'); $this->hr(); $uses = array(); $helpers = array(); $components = array(); $wannaUseSession = 'y'; $wannaDoScaffolding = 'y'; $useDbConfig = 'default'; $this->__doList($useDbConfig, 'Controllers'); $enteredController = ''; while ($enteredController == '') { $enteredController = $this->getInput('Enter a number from the list above, or type in the name of another controller.'); if ($enteredController == '' || intval($enteredController) > count($this->__controllerNames)) { $this->stdout('Error:'); $this->stdout("The Controller name you supplied was empty, or the number \nyou selected was not an option. Please try again."); $enteredController = ''; } } if (intval($enteredController) > 0 && intval($enteredController) <= count($this->__controllerNames) ) { $controllerName = $this->__controllerNames[intval($enteredController) - 1]; } else { $controllerName = $enteredController; } $controllerPath = $this->__controllerPath($controllerName); $doItInteractive = $this->getInput("Would you like bake to build your controller interactively?\nWarning: Choosing no will overwrite {$controllerClassName} controller if it exist.", array('y','n'), 'y'); if (low($doItInteractive) == 'y' || low($doItInteractive) == 'yes') { $this->interactive = true; $wannaDoUses = $this->getInput("Would you like this controller to use other models besides '" . $this->__modelName($controllerName) . "'?", array('y','n'), 'n'); if (low($wannaDoUses) == 'y' || low($wannaDoUses) == 'yes') { $usesList = $this->getInput("Please provide a comma separated list of the classnames of other models you'd like to use.\nExample: 'Author, Article, Book'"); $usesListTrimmed = str_replace(' ', '', $usesList); $uses = explode(',', $usesListTrimmed); } $wannaDoHelpers = $this->getInput("Would you like this controller to use other helpers besides HtmlHelper and FormHelper?", array('y','n'), 'n'); if (low($wannaDoHelpers) == 'y' || low($wannaDoHelpers) == 'yes') { $helpersList = $this->getInput("Please provide a comma separated list of the other helper names you'd like to use.\nExample: 'Ajax, Javascript, Time'"); $helpersListTrimmed = str_replace(' ', '', $helpersList); $helpers = explode(',', $helpersListTrimmed); } $wannaDoComponents = $this->getInput("Would you like this controller to use any components?", array('y','n'), 'n'); if (low($wannaDoComponents) == 'y' || low($wannaDoComponents) == 'yes') { $componentsList = $this->getInput("Please provide a comma separated list of the component names you'd like to use.\nExample: 'Acl, MyNiftyHelper'"); $componentsListTrimmed = str_replace(' ', '', $componentsList); $components = explode(',', $componentsListTrimmed); } $wannaUseSession = $this->getInput("Would you like to use Sessions?", array('y','n'), 'y'); $wannaDoScaffolding = $this->getInput("Would you like to include some basic class methods (index(), add(), view(), edit())?", array('y','n'), 'n'); } if (low($wannaDoScaffolding) == 'y' || low($wannaDoScaffolding) == 'yes') { $wannaDoAdmin = $this->getInput("Would you like to create the methods for admin routing?", array('y','n'), 'n'); } else { $wannaUseScaffold = $this->getInput("Would you like to use scaffolding?", array('y','n'), 'y'); } $admin = null; $admin_url = null; if ((low($wannaDoAdmin) == 'y' || low($wannaDoAdmin) == 'yes')) { require(CONFIGS.'core.php'); if(defined('CAKE_ADMIN')) { $admin = CAKE_ADMIN.'_'; $admin_url = '/'.CAKE_ADMIN; } else { $adminRoute = ''; $this->stdout('You need to enable CAKE_ADMIN in /app/config/core.php to use admin routing.'); $this->stdout('What would you like the admin route to be?'); $this->stdout('Example: www.example.com/admin/controller'); while ($adminRoute == '') { $adminRoute = $this->getInput("What would you like the admin route to be?", null, 'admin'); } if($this->__addAdminRoute($adminRoute) !== true){ $this->stdout('Unable to write to /app/config/core.php.'); $this->stdout('You need to enable CAKE_ADMIN in /app/config/core.php to use admin routing.'); exit(); } else { $admin = $adminRoute . '_'; $admin_url = '/'.$adminRoute; } } } if (low($wannaDoScaffolding) == 'y' || low($wannaDoScaffolding) == 'yes') { loadModels(); $actions = $this->__bakeActions($controllerName, null, null, $wannaUseSession); if($admin) { $actions .= $this->__bakeActions($controllerName, $admin, $admin_url, $wannaUseSession); } } if($this->interactive === true) { $this->stdout(''); $this->hr(); $this->stdout('The following controller will be created:'); $this->hr(); $this->stdout("Controller Name: $controllerName"); if(count($uses)) { $this->stdout("Uses: ", false); foreach($uses as $use) { if ($use != $uses[count($uses) - 1]) { $this->stdout(ucfirst($use) . ", ", false); } else { $this->stdout(ucfirst($use)); } } } if(count($helpers)) { $this->stdout("Helpers: ", false); foreach($helpers as $help) { if ($help != $helpers[count($helpers) - 1]) { $this->stdout(ucfirst($help) . ", ", false); } else { $this->stdout(ucfirst($help)); } } } if(count($components)) { $this->stdout("Components: ", false); foreach($components as $comp) { if ($comp != $components[count($components) - 1]) { $this->stdout(ucfirst($comp) . ", ", false); } else { $this->stdout(ucfirst($comp)); } } } $this->hr(); $looksGood = $this->getInput('Look okay?', array('y','n'), 'y'); if (low($looksGood) == 'y' || low($looksGood) == 'yes') { $this->bakeController($controllerName, $uses, $helpers, $components, $actions, $wannaUseScaffold); if ($this->doUnitTest()) { $this->bakeUnitTest('controller', $controllerName);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -