diff --git a/cake/scripts/bake.php b/cake/scripts/bake.php index 1bd3250f7..eac640053 100644 --- a/cake/scripts/bake.php +++ b/cake/scripts/bake.php @@ -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 = "\ttextarea('{$tagName}', " . $this->__attributesToArray($htmlAttributes) . ");?>\n"; - $str .= "\ttagErrorMsg('{$tagName}', 'Please enter the {$prompt}.');?>\n"; - $strLabel = "\n\tlabel( '{$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\tcheckbox('{$tagName}', null, " . $this->__attributesToArray($htmlAttributes) . ");?>\n"; - $strLabel .= "\tlabel('{$tagName}', '{$prompt}');?>\n"; - $str = "\ttagErrorMsg('{$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 = "\tdateTimeOptionTag('{$tagName}', 'MDY' , 'NONE', \$html->tagValue('{$tagName}'), " . $this->__attributesToArray($htmlAttributes) . ");?>\n"; - $str .= "\ttagErrorMsg('{$tagName}', 'Please select the {$prompt}.');?>\n"; - $strLabel = "\n\tlabel('{$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\dateTimeOptionTag('{$tagName}', 'NONE', '24', \$html->tagValue('{$tagName}'), " . $this->__attributesToArray($htmlAttributes) . ");?>\n"; - $strLabel = "\n\tlabelTag('{$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 = "\tdateTimeOptionTag('{$tagName}', 'MDY' , '12', \$html->tagValue('{$tagName}'), " . $this->__attributesToArray($htmlAttributes) . ");?>\n"; - $str .= "\ttagErrorMsg('{$tagName}', 'Please select the {$prompt}.');?>\n"; - $strLabel = "\n\tlabel('{$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 = "\ttext('{$tagName}', " . $this->__attributesToArray($htmlAttributes) . ");?>\n"; - $str .= "\ttagErrorMsg('{$tagName}', 'Please enter the {$prompt}.');?>\n"; - $strLabel = "\n\tlabel('{$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 = "\tselect('{$tagName}', " . "\${$pluralName}, \$html->tagValue('{$tagName}'), " . $this->__attributesToArray($selectAttr) . ", " . $this->__attributesToArray($optionAttr) . ", " . $showEmpty . ");?>\n"; - $str .= "\ttagErrorMsg('{$tagName}', 'Please select the {$prompt}.') ?>\n"; - } else { - $selectedPluralName = 'selected' . ucfirst($pluralName); - $selectAttr = am(array('multiple' => 'multiple', 'class' => 'selectMultiple'), $selectAttr); - $str = "\tselect('{$tagName}', \${$pluralName}, \${$selectedPluralName}, " . $this->__attributesToArray($selectAttr) . ", " . $this->__attributesToArray($optionAttr) . ", " . $showEmpty . ");?>\n"; - $str .= "\ttagErrorMsg('{$tagName}', 'Please select the {$prompt}.');?>\n"; - } - $strLabel = "\n\tlabel('{$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\tbutton('{$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('