Johan Broddfelt
/* Comments on code */

Generating the edit form

You did not think that we would leave the edit form without a code generator, did you? Of course we need to generate a php-file so that we can modify the code of the edit.php as well. The same strategy applies as for the list.php. So here we go. Create the file views/generic/edit_code.php

<?php
    $fields = $obj->fetchFields();
    $colCount = count($fields);
$code =
'<?php
    if (filter_input(INPUT_POST, 'save', FILTER_SANITIZE_URL) != '') {
';
        foreach ($fields as $f) {
            $name = $f->varName;
            if ($t->columnName == 'id') {
                // do nothing;
            } else {
$code .=
'        $obj->' . $name . ' = filter_input(INPUT_POST, '' . $f->columnName . '');
';
            }
        }
$code .=
'        $obj->update();
        ?>
        <div class="information">
            Changes are saved
        </div>
        <?php
    }
?>
<div>
<h1>
    <?php if ($obj->id == 0) { ?>
      New
    <?php } else { ?>
      Edit
    <?php } ?>
    ' . $obj->realClassName() . '
</h1>
<a href="index.php?module=' . $module . '&view=list" class="button">Back</a>
<form method="post" action="">
    <table class="form">
';
        foreach ($fields as $t) {
            $name = $t->varName;
$code .=
'            <tr>
                <th>
';
                    if (preg_match("/_id$/", $t->columnName, $matches)) {
                        $columnArr = explode('-', $t->columnName);
$code .= '                    ' . Db::realClassName(str_replace('_id', '', $columnArr[0])) . '
';
                    } else {
$code .= '                    ' . Db::realClassName($t->columnName) . '
';
                    }
$code .=
'                </th>
                <td>
';
                if ($t->columnName == 'id') {
$code .=
'                    <?php echo $obj->' . $name . '; ?>
                    <input type="hidden" name="id" value="<?php echo $obj->' . $name . '; ?>">
';
                } else if (preg_match("/_id$/", $t->columnName, $matches)) {
                    $columnArr = explode('-', $t->columnName);
                    if (count($columnArr) < 2) {
                        $columnArr[1] = $t->columnName;
                    }
                    $table = preg_replace("/_id$/", '', $columnArr[1]);
                    $objClassName = preg_replace('/s/', '', '' . $obj->className($table));
                    $sc = new $objClassName();
                    //print $table.' : '.$objlassName;
                    $scFields = $sc->fetchFields();
$code .=
'                    <?php
                    $sc = new ' . $objClassName . '();
                    $scs = $sc->fetchArray(' ORDER BY `' . $scFields[1]->columnName . '`,  `id`');
                    ?>
                    <select name="' . $t->columnName . '" style="width: 145px;">
                        <option value="">Select</option>
                        <?php
                        foreach ($scs as $item) {
                            ?>
                            <option value="<?php echo $item->id; ?>"
                                <?php if ($item->id == $obj->' . $name . ') { ?>
                                    selected="selected"
                                <?php } ?>
                            >
                                <?php echo $item->table_data[1]; ?>
                            </option>
                            <?php
                        }
                        ?>
                    </select>
';
                } else if ($t->realType == 'text') {
$code .=
'                    <textarea name="' . $t->columnName . '" style="width: 150px; height: 80px;"><?php echo $obj->' . $name . '; ?></textarea>
';
                } else {
$code .=
'                    <input 
';
                        if ($t->columnType == 'chk') {
$code .=
'                            type="checkbox" 
                            value="1"
                            <?php if ($obj->' . $name . ') { ?>
                                checked="checked"
                            <?php } ?>
';
                        } else {
                            if (($t->realType == 'datetime' or $t->realType == 'timestamp')) {
$code .=
'                            <?php if ($obj->' . $name . ' == '') {
                                $obj->' . $name . ' = date('Y-m-d H:i:s');
                            } ?>
';
                            }
 $code .=
'                           type="text"
                           value="<?php echo $obj->' . $name . '; ?>"
';
                        }
$code .=
'                           name="' . $t->columnName . '" 
';
                        if ($t->realType == 'date') {
$code .=
'                            class="datepicker"
';
                        }
$code .=
'                        >
';
                }
$code .=
'                </td>
            </tr>
';
        }
$code .=
'            <tr>
                <th></th>
                <td>
                    <input type="submit" name="save" value="Save" class="button">
                </td>
            </tr>
    </table>
</form>
</div>';
// Make dir if it does not exist. Named after $module
if (!is_dir('views/' . $module)) {
    mkdir('views/' . $module);
}
$fh = fopen('views/' . $module . '/' . $view . '.php', 'w');
fwrite($fh, $code);
fclose($fh);
?>
<div>
    <h1>File generated</h1>
    <pre><code>
        <?php echo str_replace('<', '<', $code); ?>
    </code></pre>
</div>

Now you can type index.php?modue=[your_module]&view=edit&generate=code and the edit file will be generated along with the code that will be printed on the screen. Enjoy!

- Framework, PHP, Generic

Follow using RSS

<< Edit on the fly Delete post and Generic Class >>

Comment

Name
Mail (Not public)
Send mail uppdates on new comments
0 comment