fix to bake loading models

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4021 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2006-11-29 04:19:50 +00:00
parent 83d5e49b3d
commit 8d4fe47571

View file

@ -415,7 +415,7 @@ class Bake {
}
$wannaDoValidation = $this->getInput('Would you like to supply validation criteria for the fields in your model?', array('y','n'), 'y');
loadModel();
$tempModel = new Model(false, $useTable);
$db =& ConnectionManager::getDataSource($useDbConfig);
$modelFields = $db->describe($tempModel);
@ -1949,358 +1949,7 @@ class Bake {
}
}
/**
* Takes an array of database fields, and generates an HTML form for a View.
* This is an extraction from the Scaffold functionality.
*
* @param array $fields
* @param boolean $readOnly
* @return Generated HTML and PHP.
*/
function generateFields( $fields, $readOnly = false ) {
$strFormFields = '';
foreach( $fields as $field ) {
if(isset( $field['type'])) {
if(!isset($field['required'])) {
$field['required'] = false;
}
if(!isset( $field['errorMsg'])) {
$field['errorMsg'] = null;
}
if(!isset( $field['htmlOptions'])) {
$field['htmlOptions'] = array();
}
if( $readOnly ) {
$field['htmlOptions']['READONLY'] = "readonly";
}
switch( $field['type'] ) {
case "input" :
if(!isset( $field['size'])) {
$field['size'] = 60;
}
$strFormFields = $strFormFields.$this->generateInputDiv( $field['tagName'], $field['prompt'], $field['required'], $field['errorMsg'], $field['size'], $field['htmlOptions'] );
break;
case "checkbox" :
$strFormFields = $strFormFields.$this->generateCheckboxDiv( $field['tagName'], $field['prompt'], $field['required'], $field['errorMsg'], $field['htmlOptions'] );
break;
case "select";
case "selectMultiple";
if( "selectMultiple" == $field['type'] ) {
$field['selectAttr']['multiple'] = 'multiple';
$field['selectAttr']['class'] = 'selectMultiple';
}
if(!isset( $field['selected'])) {
$field['selected'] = null;
}
if(!isset( $field['selectAttr'])) {
$field['selectAttr'] = null;
}
if(!isset( $field['optionsAttr'])) {
$field['optionsAttr'] = null;
}
if($readOnly) {
$field['selectAttr']['DISABLED'] = true;
}
if(!isset( $field['options'])) {
$field['options'] = null;
}
$this->__modelAlias = null;
if(isset($field['foreignKey'])) {
$modelKey = Inflector::underscore($this->__modelClass);
$modelObj =& ClassRegistry::getObject($modelKey);
foreach($modelObj->belongsTo as $associationName => $value) {
if($field['model'] == $value['className']) {
$this->__modelAlias = $this->__modelName($associationName);
break;
}
}
}
$strFormFields = $strFormFields.$this->generateSelectDiv( $field['tagName'], $field['prompt'], $field['options'], $field['selected'], $field['selectAttr'], $field['optionsAttr'], $field['required'], $field['errorMsg'] );
break;
case "area";
if(!isset( $field['rows'])) {
$field['rows'] = 10;
}
if(!isset( $field['cols'])) {
$field['cols'] = 60;
}
$strFormFields = $strFormFields.$this->generateAreaDiv( $field['tagName'], $field['prompt'], $field['required'], $field['errorMsg'], $field['cols'], $field['rows'], $field['htmlOptions'] );
break;
case "date":
if (!isset($field['selected'])) {
$field['selected'] = null;
}
$strFormFields = $strFormFields . $this->generateDate($field['tagName'], $field['prompt'], $field['required'], $field['errorMsg'], null, $field['htmlOptions'], $field['selected']);
break;
case "datetime":
if (!isset($field['selected'])) {
$field['selected'] = null;
}
$strFormFields = $strFormFields . $this->generateDateTime($field['tagName'], $field['prompt'], $field['required'], $field['errorMsg'], null, $field['htmlOptions'], $field['selected']);
break;
case "time":
if (!isset($field['selected'])) {
$field['selected'] = null;
}
$strFormFields = $strFormFields . $this->generateTime($field['tagName'], $field['prompt'], $field['required'], $field['errorMsg'], null, $field['htmlOptions'], $field['selected']);
break;
default:
break;
}
}
}
return $strFormFields;
}
/**
* Generates PHP code for a View file that makes a textarea.
*
* @param string $tagName
* @param string $prompt
* @param boolean $required
* @param string $errorMsg
* @param integer $cols
* @param integer $rows
* @param array $htmlOptions
* @return Generated HTML and PHP.
*/
function generateAreaDiv($tagName, $prompt, $required=false, $errorMsg=null, $cols=60, $rows=10, $htmlAttributes=null ) {
$htmlAttributes['cols'] = $cols;
$htmlAttributes['rows'] = $rows;
$str = "\t<?php echo \$form->textarea('{$tagName}', " . $this->__attributesToArray($htmlAttributes) . ");?>\n";
$str .= "\t<?php echo \$html->tagErrorMsg('{$tagName}', 'Please enter the {$prompt}.');?>\n";
$strLabel = "\n\t<?php echo \$form->label( '{$tagName}', '{$prompt}' );?>\n";
$divClass = "optional";
if( $required ) {
$divClass = "required";
}
$strError = "";// initialize the error to empty.
$divTagInside = sprintf( "%s %s %s", $strError, $strLabel, $str );
return $this->divTag( $divClass, $divTagInside );
}
/**
* Generates PHP code for a View file that makes a checkbox, wrapped in a DIV.
*
* @param string $tagName
* @param string $prompt
* @param boolean $required
* @param string $errorMsg
* @param array $htmlOptions
* @return Generated HTML and PHP.
*/
function generateCheckboxDiv($tagName, $prompt, $required=false, $errorMsg=null, $htmlAttributes=null ) {
$strLabel = "\n\t<?php echo \$html->checkbox('{$tagName}', null, " . $this->__attributesToArray($htmlAttributes) . ");?>\n";
$strLabel .= "\t<?php echo \$form->label('{$tagName}', '{$prompt}');?>\n";
$str = "\t<?php echo \$html->tagErrorMsg('{$tagName}', 'Please check the {$prompt}.');?>\n";
$divClass = "optional";
if($required) {
$divClass = "required";
}
$strError = "";// initialize the error to empty.
$divTagInside = sprintf( "%s %s %s", $strError, $strLabel, $str);
return $this->divTag( $divClass, $divTagInside );
}
/**
* Generates PHP code for a View file that makes a date-picker, wrapped in a DIV.
*
* @param string $tagName
* @param string $prompt
* @param boolean $required
* @param string $errorMsg
* @param integer $size
* @param array $htmlOptions
* @param string $selected
* @return Generated HTML and PHP.
*/
function generateDate($tagName, $prompt, $required=false, $errorMsg=null, $size=20, $htmlAttributes=null, $selected=null ) {
$str = "\t<?php echo \$html->dateTimeOptionTag('{$tagName}', 'MDY' , 'NONE', \$html->tagValue('{$tagName}'), " . $this->__attributesToArray($htmlAttributes) . ");?>\n";
$str .= "\t<?php echo \$html->tagErrorMsg('{$tagName}', 'Please select the {$prompt}.');?>\n";
$strLabel = "\n\t<?php echo \$form->label('{$tagName}', '{$prompt}');?>\n";
$divClass = "optional";
if($required) {
$divClass = "required";
}
$strError = "";// initialize the error to empty.
$divTagInside = sprintf( "%s %s %s", $strError, $strLabel, $str );
return $this->divTag( $divClass, $divTagInside );
}
/**
* Generates PHP code for a View file that makes a time-picker, wrapped in a DIV.
*
* @param string $tagName
* @param string $prompt
* @param boolean $required
* @param string $errorMsg
* @param integer $size
* @param array $htmlOptions
* @param string $selected
* @return Generated HTML and PHP.
*/
function generateTime($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlAttributes = null, $selected = null) {
$str = "\n\t\<?php echo \$html->dateTimeOptionTag('{$tagName}', 'NONE', '24', \$html->tagValue('{$tagName}'), " . $this->__attributesToArray($htmlAttributes) . ");?>\n";
$strLabel = "\n\t<?php echo \$form->labelTag('{$tagName}', '{$prompt}');?>\n";
$divClass = "optional";
if ($required) {
$divClass = "required";
}
$strError = "";
$divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str);
return $this->divTag($divClass, $divTagInside);
}
/**
* EGenerates PHP code for a View file that makes a datetime-picker, wrapped in a DIV.
*
* @param string $tagName
* @param string $prompt
* @param boolean $required
* @param string $errorMsg
* @param integer $size
* @param array $htmlOptions
* @param string $selected
* @return Generated HTML and PHP.
*/
function generateDateTime($tagName, $prompt, $required=false, $errorMsg=null, $size=20, $htmlAttributes=null, $selected = null ) {
$str = "\t<?php echo \$html->dateTimeOptionTag('{$tagName}', 'MDY' , '12', \$html->tagValue('{$tagName}'), " . $this->__attributesToArray($htmlAttributes) . ");?>\n";
$str .= "\t<?php echo \$html->tagErrorMsg('{$tagName}', 'Please select the {$prompt}.');?>\n";
$strLabel = "\n\t<?php echo \$form->label('{$tagName}', '{$prompt}');?>\n";
$divClass = "optional";
if($required) {
$divClass = "required";
}
$strError = "";// initialize the error to empty.
$divTagInside = sprintf( "%s %s %s", $strError, $strLabel, $str );
return $this->divTag( $divClass, $divTagInside );
}
/**
* Generates PHP code for a View file that makes an INPUT field, wrapped in a DIV.
*
* @param string $tagName
* @param string $prompt
* @param boolean $required
* @param string $errorMsg
* @param integer $size
* @param array $htmlOptions
* @return Generated HTML and PHP.
*/
function generateInputDiv($tagName, $prompt, $required=false, $errorMsg=null, $size=20, $htmlAttributes=null ) {
$htmlAttributes['size'] = $size;
$str = "\t<?php echo \$form->text('{$tagName}', " . $this->__attributesToArray($htmlAttributes) . ");?>\n";
$str .= "\t<?php echo \$html->tagErrorMsg('{$tagName}', 'Please enter the {$prompt}.');?>\n";
$strLabel = "\n\t<?php echo \$form->label('{$tagName}', '{$prompt}');?>\n";
$divClass = "optional";
if($required) {
$divClass = "required";
}
$strError = "";// initialize the error to empty.
$divTagInside = sprintf( "%s %s %s", $strError, $strLabel, $str );
return $this->divTag( $divClass, $divTagInside );
}
/**
* Generates PHP code for a View file that makes a SELECT box, wrapped in a DIV.
*
* @param string $tagName
* @param string $prompt
* @param array $options
* @param string $selected
* @param array $selectAttr
* @param array $optionAttr
* @param boolean $required
* @param string $errorMsg
* @return Generated HTML and PHP.
*/
function generateSelectDiv($tagName, $prompt, $options, $selected=null, $selectAttr=null, $optionAttr=null, $required=false, $errorMsg=null) {
if($this->__modelAlias) {
$pluralName = $this->__pluralName($this->__modelAlias);
} else {
$tagArray = explode('/', $tagName);
$pluralName = $this->__pluralName($this->__modelNameFromKey($tagArray[1]));
}
$showEmpty = 'true';
if($required) {
$showEmpty = 'false';
}
if($selectAttr['multiple'] != 'multiple') {
$str = "\t<?php echo \$form->select('{$tagName}', " . "\${$pluralName}, \$html->tagValue('{$tagName}'), " . $this->__attributesToArray($selectAttr) . ", " . $this->__attributesToArray($optionAttr) . ", " . $showEmpty . ");?>\n";
$str .= "\t<?php echo \$html->tagErrorMsg('{$tagName}', 'Please select the {$prompt}.') ?>\n";
} else {
$selectedPluralName = 'selected' . ucfirst($pluralName);
$selectAttr = am(array('multiple' => 'multiple', 'class' => 'selectMultiple'), $selectAttr);
$str = "\t<?php echo \$form->select('{$tagName}', \${$pluralName}, \${$selectedPluralName}, " . $this->__attributesToArray($selectAttr) . ", " . $this->__attributesToArray($optionAttr) . ", " . $showEmpty . ");?>\n";
$str .= "\t<?php echo \$html->tagErrorMsg('{$tagName}', 'Please select the {$prompt}.');?>\n";
}
$strLabel = "\n\t<?php echo \$form->label('{$tagName}', '{$prompt}');?>\n";
$divClass = "optional";
if($required) {
$divClass = "required";
}
$strError = "";// initialize the error to empty.
$divTagInside = sprintf( "%s %s %s", $strError, $strLabel, $str );
return $this->divTag( $divClass, $divTagInside );
}
/**
* Generates PHP code for a View file that makes a submit button, wrapped in a DIV.
*
* @param string $displayText
* @param array $htmlOptions
* @return Generated HTML.
*/
function generateSubmitDiv($displayText, $htmlOptions = null) {
$str = "\n\t<?php echo \$form->button('{$displayText}', 'submit');?>\n";
$divTagInside = sprintf( "%s", $str );
return $this->divTag( 'submit', $divTagInside);
}
/**
* Returns the text wrapped in an HTML DIV, followed by a newline.
*
* @param string $class
* @param string $text
* @return Generated HTML.
*/
function divTag($class, $text) {
return sprintf('<div class="%s">%s</div>', $class, $text ) . "\n";
}
/**
* Parses the HTML attributes array, which is a common data structure in View files.
* Returns PHP code for initializing this array in a View file.
*
* @param array $htmlAttributes
* @return Generated PHP code.
*/
function __attributesToArray($htmlAttributes) {
if (is_array($htmlAttributes)) {
$keys = array_keys($htmlAttributes);
$vals = array_values($htmlAttributes);
$out = "array(";
for($i = 0; $i < count($htmlAttributes); $i++) {
//don't put vars in quotes
if(substr($vals[$i], 0, 1) != '$') {
$out .= "'{$keys[$i]}' => '{$vals[$i]}', ";
} else {
$out .= "'{$keys[$i]}' => {$vals[$i]}, ";
}
}
//Chop off last comma
if(substr($out, -2, 1) == ',') {
$out = substr($out, 0, strlen($out) - 2);
}
$out .= ")";
return $out;
} else {
return 'array()';
}
}
/**
* Outputs usage text on the standard output.
*