trongate / trongate-framework

The Trongate PHP framework
https://trongate.io
Other
1.12k stars 100 forks source link

Small adjustment to the form_helper #195

Closed domsinclair closed 2 months ago

domsinclair commented 2 months ago

This adjustment ensures that it handles the case where $attributes might not be an array. Specifically, it checks if $attributes is not set or not an array and returns an empty string in those cases. This should make the function more robust and prevent errors when $attributes is not provided or is not an array.

There is also a log error message provided to assist with debugging just in case.

The reason for this ammendment is that it allows for a more dynamic type of search bar in manage views that have been dynamically created in the first place.

For example something like this

image

This is about the only way that one can add a search facility to a dynamically created manage view in those situations where one (in this particular case the Vtl Data Generator) has no prior knowledge of the table structure and is thus able to create meaningful queries in advance.

This amendment makes this sort of thing possible

  public function manage() {
        $this->module('trongate_security');
        $this->trongate_security->_make_sure_allowed();

        $data['headline'] = 'Manage {{ModuleName}}';

        if (segment(4) !== '') {
            $data['headline'] = 'Search Results';

            // Access search parameters from the GET request
            $searchField = $_GET['search_field'];
            $searchOperator = $_GET['search_operator'];
            $searchTerm = $_GET['search_term'];

            // Execute the dynamic search query
            $all_rows = $this->execute_search_query($searchField, $searchOperator, $searchTerm);
        } else {
            $all_rows = $this->model->get('{{primaryKey}} asc');
        }

        // Pagination configuration
        $pagination_data['total_rows'] = count($all_rows);
        $pagination_data['page_num_segment'] = 3;
        $pagination_data['limit'] = $this->_get_limit();
        $pagination_data['pagination_root'] = '{{stlModuleName}}/manage';
        $pagination_data['record_name_plural'] = '{{stlModuleName}}';
        $pagination_data['include_showing_statement'] = true;
        $data['pagination_data'] = $pagination_data;

        $data['rows'] = $this->_reduce_rows($all_rows);
        $data['selected_per_page'] = $this->_get_selected_per_page();
        $data['per_page_options'] = $this->per_page_options;
        $data['view_module'] = '{{stlModuleName}}';

        $template_to_use = 'admin';
        $view_file_to_use = 'manage';

        $data['table_headers'] = '{{tableHeaders}}';
        $data['view_file'] = $view_file_to_use;
        $this->template($template_to_use, $data);
    }
and that in turn is also reflected in the view itself
<?php
$table_headers = json_decode('{{tableHeaders}}', true);
$primaryKey = '{{primaryKey}}';
?>
<h1><?= $headline ?></h1>
<?php
flashdata();
echo validation_errors();
echo '<p>';
echo anchor('{{moduleName}}/create', 'Create New {{moduleName}} Record', array("class" => "button")).'</p>';
if (strtolower(ENV) === 'dev') {
    echo anchor('api/explorer/{{moduleName}}', 'API Explorer', array("class" => "button alt"));
}
echo '</p>';

echo Pagination::display($pagination_data);

if (count($rows) > 0) { ?>
    <table id="results-tbl">
        <thead>
        <tr>
            <th colspan="<?= count($table_headers) + 1 ?>">
                <div class="search-header">
                    <div class="search-form">
                        <?php
                        echo form_open('{{moduleName}}/manage/1/', array("method" => "get"));
                        ?>
                        <div class="search-item">
                            <?= form_label('Search Field:', 'search_field') ?>
                            <?php
                            $search_field_options = json_decode('{{searchFieldOptions}}', true);
                            echo form_dropdown('search_field', $search_field_options, '', array('id' => 'search_field'));
                            ?>
                        </div>

                        <div class="search-item">
                            <?= form_label('Operator:', 'search_operator') ?>
                            <?php
                            $search_operator_options = json_decode('{{searchOperatorOptions}}', true);
                            echo form_dropdown('search_operator', $search_operator_options, '', array('id' => 'search_operator'));
                            ?>
                        </div>

                        <div class="search-item">
                            <?= form_label('Search Term:', 'search_term') ?>
                            <?= form_input('search_term', '', array('id' => 'search_term')) ?>
                        </div>
                        <div class="search-item">
                            <?= form_submit('search_submit', 'Search', array('class' => 'button ')) ?>
                        </div>
                        <?php
                        echo form_close();
                        ?>
                    </div>
                    <div class="records-per-page">
                        Records Per Page:
                        <?php
                        $dropdown_attr = array('onchange' => 'setPerPage()');
                        echo form_dropdown('per_page', $per_page_options, $selected_per_page, $dropdown_attr);
                        ?>
                    </div>
                </div>
            </th>
        </tr>
        <tr>
            <?php foreach ($table_headers as $header): ?>
                <th><?= ucfirst($header) ?></th>
            <?php endforeach; ?>
            <th>Action</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($rows as $row): ?>
            <tr>
                <?php foreach ($table_headers as $header): ?>
                    <td><?= $row->$header ?? '' ?></td>
                <?php endforeach; ?>
                <td><?= anchor('{{moduleName}}/show/'.$row->$primaryKey, 'View', array('class' => 'button alt')) ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php
} else {
    echo '<p>No {{moduleName}} records were found.</p>';
}

// Display pagination links again if there are many records
if (count($rows) > 10) {
    echo Pagination::display($pagination_data);
}
?>

the styling still leaves a lot to be desired but the functionality this amendment actively enables works .

Without the amendment we end up with this:

image

trongate commented 2 months ago

Thank you. Hail Dom!