public static function generateCreateForm($class) { // Prepare Values $schema = $class::$schema; $currentRow = 0; $controller = ' // Handle a Submission if(Form::submitted("' . $class . '-protect-form")) { // Make sure the the submission is valid if($result = ' . $class . '::verifyForm($_POST)) { // If the submission is valid, process the form if(' . $class . '::create($_POST)) { Alert::saveSuccess("Form Created", "The form data was properly submitted!"); // Clear the existing POST data unset($_POST); } } } // Display the Alerts echo Alert::display();'; // Loop through each column in the schema foreach ($schema['columns'] as $columnName => $columnRules) { // Identify and process tags that were listed for this column if (isset($schema['tags'][$columnName])) { $tags = is_array($schema['tags'][$columnName]) ? $schema['tags'][$columnName] : [$schema['tags'][$columnName]]; // Check if there are any tags that the user needs to test foreach ($tags as $tag) { switch ($tag) { // If the tag cannot be modified, don't show it on the form case Model::CANNOT_SET: continue 3; } } } // If data was submitted by the user, set the column's value to their input if (isset($_POST[$columnName])) { $value = $_POST[$columnName]; } else { $value = isset($schema['default'][$columnName]) ? $schema['default'][$columnName] : ''; } $currentRow++; $columnTitle = ucwords(str_replace("_", " ", $columnName)); $table[$currentRow][0] = $columnTitle; // Determine how to display the column switch ($columnRules[0]) { ### Strings and Text ### case "string": case "text": // Identify all string-related form variables $minLength = isset($columnRules[1]) ? (int) $columnRules[1] : 0; $maxLength = isset($columnRules[2]) ? (int) $columnRules[2] : ($columnRules[0] == "text" ? 0 : 250); // Display a textarea for strings of 101 characters or more if (!$maxLength or $maxLength > 100) { $table[$currentRow][1] = ' <textarea id="' . $columnName . '" name="' . $columnName . '"' . ($maxLength ? 'maxlength="' . $maxLength . '"' : '') . '><?php htmlspecialchars($value); ?></textarea>'; } else { $table[$currentRow][1] = ' <input id="' . $columnName . '" type="text" name="' . $columnName . '" value="<?php htmlspecialchars($value); ?>" ' . ($maxLength ? 'maxlength="' . $maxLength . '"' : '') . ' />'; } break; ### Integers ### ### Integers ### case "tinyint": // 256 // 256 case "smallint": // 65k // 65k case "mediumint": case "int": case "bigint": // Identify all string-related form variables $minRange = isset($columnRules[1]) ? (int) $columnRules[1] : null; $maxRange = isset($columnRules[2]) ? (int) $columnRules[2] : null; $maxLength = self::getLengthOfNumberType($columnRules[0], $minRange, $maxRange); // Display the form field for an integer $table[$currentRow][1] = ' <input id="' . $columnName . '" type="number" name="' . $columnName . '" value="<?php echo ((int) $value); ?>"' . ($maxLength ? 'maxlength="' . $maxLength . '"' : '') . ($minRange ? 'min="' . $minRange . '"' : '') . ($maxRange ? 'max="' . $maxRange . '"' : '') . ' />'; break; ### Floats ### ### Floats ### case "float": case "double": // Identify all string-related form variables $minRange = isset($columnRules[1]) ? (int) $columnRules[1] : null; $maxRange = isset($columnRules[2]) ? (int) $columnRules[2] : null; $maxLength = self::getLengthOfNumberType($columnRules[0], $minRange, $maxRange); // Display the form field for an integer $formHTML .= ' <input id="' . $columnName . '" type="text" name="' . $columnName . '" value="<?php echo ((int) $value); ?>"' . ($maxLength ? 'maxlength="' . ($maxLength + ceil($maxLength / 3)) . '"' : '') . ' />'; break; ### Booleans ### ### Booleans ### case "bool": case "boolean": // If the boolean types are not declared, set defaults $trueName = isset($columnRules[1]) ? $columnRules[1] : 'True'; $falseName = isset($columnRules[2]) ? $columnRules[2] : 'False'; // Display the form field for a boolean $table[$currentRow][1] = ' <select id="' . $columnName . '" name="' . $columnName . '"><?php echo str_replace(\'value="' . $value . '"\', \'value="' . $value . '" selected\', \' <option value="1">' . htmlspecialchars($trueName) . '</option> <option value="0">' . htmlspecialchars($falseName) . '</option>\'); ?> </select>'; break; ### Enumerators ### ### Enumerators ### case "enum-number": case "enum-string": // Get the available list of enumerators $enums = array_slice($columnRules, 1); // Display the form field for a boolean $table[$currentRow][1] = ' <select id="' . $columnName . '" name="' . $columnName . '"><?php echo str_replace(\'value="' . $value . '"\', \'value="' . $value . '" selected\', \''; // Handle numeric enumerators differently than string enumerators // These will have a numeric counter associated with each value if ($columnRules[0] == "enum-number") { $enumCount = count($enums); for ($i = 0; $i < $enumCount; $i++) { $table[$currentRow][1] .= ' <option value="' . $i . '">' . htmlspecialchars($enums[$i]) . '</option>'; } } else { foreach ($enums as $enum) { $table[$currentRow][1] .= ' <option value="' . htmlspecialchars($enum) . '">' . htmlspecialchars($enum) . '</option>'; } } $table[$currentRow][1] .= '\'); ?> </select>'; break; } } // Convert the data into an HTML table $table[$currentRow + 1] = ['Submit', '<input type="submit" name="submit" value="Submit" />']; $view = '?> <form action="/' . $class . '/create" method="post"><?php echo Form::prepare("' . $class . '-protect-form"); ?> ' . UI_Table::draw($table) . ' </form> <?php'; // Save the page / form to the appropriate file $pageHTML = self::generatePage($controller, $view); File::write(APP_PATH . "/controller/" . $class . "/create.php", $pageHTML); }
// Load the appropriate CRUD form $tableData = $class::readForm($lookupID); // If this is the delete page, show an option to delete it $tableData['footer'] = ['' => '', 'Option Next' => '<a href="/model/' . $class . '/view/' . $lookupID . '&' . Link::prepare("DeletedRecord") . '" onclick="return confirm(\'Are you sure you want to delete this record?\')">Delete this Record</a>']; echo UI_Table::draw($tableData); } echo '<div><a href="/model/' . $class . '">Search Records</a></div>'; break; // Generation of this model // Generation of this model case "generate": Classes_Generator::generate($class); break; // Search Table // Search Table case "search": default: // Display the Alerts echo Alert::display(); // Make sure the class exists before calling it if (method_exists($class, 'searchForm')) { echo '<a href="/model/' . $class . '/create">Create New ' . ucfirst($class) . ' Record</a>'; echo ' <a href="/model/' . $class . '/generate" onclick="return confirm(\'This will overwrite files in the /controller/' . $class . '/ directory. Are you sure you want to generate default pages?\')">Generate Default Pages For ' . ucfirst($class) . '</a>'; // Display the Model Name echo '<h2>Search the `' . ucfirst($class) . '` Table</h2>'; // Load the Search Form $tableData = $class::searchForm(); echo UI_Table::draw($tableData); echo '<a href="/model/' . $class . '/create">Create New ' . ucfirst($class) . ' Record</a>'; } }