Dolibarr / dolibarr

Dolibarr ERP CRM is a modern software package to manage your company or foundation's activity (contacts, suppliers, invoices, orders, stocks, agenda, accounting, ...). it's an open source Web application (written in PHP) designed for businesses of any sizes, foundations and freelancers.
https://www.dolibarr.org
GNU General Public License v3.0
5.52k stars 2.81k forks source link

New Functions in html.form.class.php for Enhanced Form and Table Management #30941

Open avadnc opened 2 months ago

avadnc commented 2 months ago

Feature Request

New functions for managing and controlling forms and tables through the html.form.class.php class, reducing the amount of code needed to render a table or open a form.

Use case

Improve the development of Dolibarr views by creating cleaner and more understandable code for developers.

Suggested implementation


    /**
     * Opens the table element.
     *
     * @param string $class Optional CSS class for the table.
     *
     * @return string
     */
    public function openTable($class = 'noborder centpercent')
    {
        return '<table class="' . $class . '">';
    }

    /**
     * Closes the table element.
     *
     * @return string
     */
    public function closeTable()
    {
        return '</table>';
    }

    /**
     * Creates a table row (tr) element with support for individual column classes.
     *
     * @param array  $columns  Array of columns. Each column can be a string (content) or an array with 'content' and 'class'.
     * @param string $rowClass Optional CSS class for the row.
     *
     * @return string
     */
    public function addRow($columns = array(), $rowClass = '')
    {
        $out = '<tr' . ($rowClass ? ' class="' . $rowClass . '"' : '') . '>';
        foreach ($columns as $column) {
            // Check if column is an array with content and class or just content
            if (is_array($column)) {
                $content = isset($column['content']) ? $column['content'] : '';
                $class = isset($column['class']) ? ' class="' . $column['class'] . '"' : '';
            } else {
                $content = $column;
                $class = '';
            }
            $out .= '<td' . $class . '>' . $content . '</td>';
        }
        $out .= '</tr>';
        return $out;
    }
    /**
     * Creates a header row (th) element.
     *
     * @param array  $headers  Array of headers to display.
     * @param string $class    Optional CSS class for the row.
     * @param int    $rowspan  Rowspan attribute (optional).
     * @param int    $colspan  Colspan attribute (optional).
     *
     * @return string
     */
    public function addHeaderRow($headers = array(), $class = 'liste_titre', $rowspan = 1, $colspan = 1)
    {
        $out = '<tr' . ($class ? ' class="' . $class . '"' : '') . '>';
        foreach ($headers as $header) {
            $out .= '<th'
                . ($rowspan > 1 ? ' rowspan="' . $rowspan . '"' : '')
                . ($colspan > 1 ? ' colspan="' . $colspan . '"' : '') . '>';
            $out .= $header;
            $out .= '</th>';
        }
        $out .= '</tr>';
        return $out;
    }
    /**
     * Creates a single column (td) inside a row.
     *
     * @param string $content  Content for the column (string or HTML).
     * @param string $class    Optional CSS class for the column.
     * @param int    $rowspan  Rowspan attribute (optional).
     * @param int    $colspan  Colspan attribute (optional).
     *
     * @return string
     */
    public function addColumn($content = '', $class = '', $rowspan = 1, $colspan = 1)
    {
        $out = '<td'
            . ($class ? ' class="' . $class . '"' : '')
            . ($rowspan > 1 ? ' rowspan="' . $rowspan . '"' : '')
            . ($colspan > 1 ? ' colspan="' . $colspan . '"' : '') . '>';
        $out .= $content;
        $out .= '</td>';
        return $out;
    }

    /**
     * Opens a row (tr) element.
     *
     * @param string $class Optional CSS class for the row.
     *
     * @return string
     */
    public function openRow($class = '')
    {
        return '<tr' . ($class ? ' class="' . $class . '"' : '') . '>';
    }

    /**
     * Closes a row (tr) element.
     *
     * @return string
     */
    public function closeRow()
    {
        return '</tr>';
    }

    /**
     * Opens a column (td) element.
     *
     * @param string $class   Optional CSS class for the column.
     * @param int    $rowspan Rowspan attribute (optional).
     * @param int    $colspan Colspan attribute (optional).
     *
     * @return string
     */
    public function openColumn($class = '', $rowspan = 1, $colspan = 1)
    {
        $out = '<td'
            . ($class ? ' class="' . $class . '"' : '')
            . ($rowspan > 1 ? ' rowspan="' . $rowspan . '"' : '')
            . ($colspan > 1 ? ' colspan="' . $colspan . '"' : '') . '>';
        return $out;
    }

    /**
     * Closes a column (td) element.
     *
     * @return string
     */
    public function closeColumn()
    {
        return '</td>';
    }

    /**
     * Returns the HTML input field for the given field name and value.
     *
     * @param string $name          The name of the input field.
     * @param string $value         The value to populate the input field with.
     * @param string $type          The type of input field (default is 'text').
     * @param string $class          The CSS class for the input field (default is '').
     * @return string The HTML input field as a string.
     */

    public function createInputField($name, $value = '', $type = 'text', $class = '')
    {
        $out = '<input type="' . $type . '" name="' . $name . '" value="' . dol_escape_htmltag($value) . '" class="' . $class . '">';
        return $out;
    }

    /**
     * Closes the form by returning the closing form tag.
     *
     * @return string The HTML closing form tag.
     */
    public function closeForm()
    {
        // Close the form tag
        $out = '</form>';
        return $out;
    }

    /**
     * Opens a form with the specified name, action, method, and hidden options.
     *
     * @param string              $form_name      The name of the form.
     * @param string              $action         The URL where the form data will be sent (default is an empty string).
     * @param string              $method         The HTTP method to use (default is 'POST').
     * @param array<string,string> $hiddenoptions  An associative array of hidden input fields to include (optional).
     *
     * @return string The opening form tag and hidden fields as HTML.
     */
    public function openForm($form_name, $action = '', $method = 'POST', array $hiddenoptions = array())
    {
        // Create the opening form tag with name, action, and method attributes
        $out = '<form name="' . $form_name . '" action="' . $action . '" method="' . $method . '">';

        // Add any hidden input fields provided in the $hiddenoptions array
        foreach ($hiddenoptions as $key => $value) {
            $out .= '<input type="hidden" name="' . $key . '" value="' . $value . '">';
        }

        // Add a hidden input field for a security token
        $out .= '<input type="hidden" name="token" value="' . newToken() . '">';

        return $out;
    }

Suggested steps

No response

avadnc commented 2 months ago

Example table:

print $form->openForm('addTest', $_SERVER['PHP_SELF'], 'GET', array('action' => 'test'));
print $form->openTable();

print $form->addRow(
    array(
        $langs->trans('Value'),
        array('content' => $langs->trans('Action'), 'class'=>'center')
    ),
    'liste_titre'
);

print $form->addRow(
    array(
        array('content'=>$form->createInputField('test', GETPOST('test') ? GETPOST('test') : $test, 'text', 'minwidth300 maxwidth400onsmartphone'),
            'class'=>'left'),
        array('content'=>$form->buttonsSaveCancel("Create"),'class'=>'right')
    ),
    'left'

);
print $form->closeTable();
print $form->closeForm();