Contao\DC_Table::editAll PHP Method

editAll() public method

Auto-generate a form to edit all records that are currently shown
public editAll ( integer $intId = null, integer $ajaxId = null ) : string
$intId integer
$ajaxId integer
return string
    public function editAll($intId = null, $ajaxId = null)
    {
        if ($GLOBALS['TL_DCA'][$this->strTable]['config']['notEditable']) {
            throw new InternalServerErrorException('Table "' . $this->strTable . '" is not editable.');
        }
        $return = '';
        $this->import('BackendUser', 'User');
        /** @var SessionInterface $objSession */
        $objSession = \System::getContainer()->get('session');
        // Get current IDs from session
        $session = $objSession->all();
        $ids = $session['CURRENT']['IDS'];
        if ($intId != '' && \Environment::get('isAjaxRequest')) {
            $ids = array($intId);
        }
        // Save field selection in session
        if (\Input::post('FORM_SUBMIT') == $this->strTable . '_all' && \Input::get('fields')) {
            $session['CURRENT'][$this->strTable] = \Input::post('all_fields');
            $objSession->replace($session);
        }
        // Add fields
        $fields = $session['CURRENT'][$this->strTable];
        if (!empty($fields) && is_array($fields) && \Input::get('fields')) {
            $class = 'tl_tbox';
            // Walk through each record
            foreach ($ids as $id) {
                $this->intId = $id;
                $this->procedure = array('id=?');
                $this->values = array($this->intId);
                $this->blnCreateNewVersion = false;
                $this->strPalette = \StringUtil::trimsplit('[;,]', $this->getPalette());
                $objVersions = new \Versions($this->strTable, $this->intId);
                $objVersions->initialize();
                // Add meta fields if the current user is an administrator
                if ($this->User->isAdmin) {
                    if ($this->Database->fieldExists('sorting', $this->strTable)) {
                        array_unshift($this->strPalette, 'sorting');
                    }
                    if ($this->Database->fieldExists('pid', $this->strTable)) {
                        array_unshift($this->strPalette, 'pid');
                    }
                    // Ensure a minimum configuration
                    foreach (array('pid', 'sorting') as $f) {
                        if (!isset($GLOBALS['TL_DCA'][$this->strTable]['fields'][$f]['label'])) {
                            $GLOBALS['TL_DCA'][$this->strTable]['fields'][$f]['label'] =& $GLOBALS['TL_LANG']['MSC'][$f];
                        }
                        if (!isset($GLOBALS['TL_DCA'][$this->strTable]['fields'][$f]['inputType'])) {
                            $GLOBALS['TL_DCA'][$this->strTable]['fields'][$f]['inputType'] = 'text';
                        }
                        if (!isset($GLOBALS['TL_DCA'][$this->strTable]['fields'][$f]['eval'])) {
                            $GLOBALS['TL_DCA'][$this->strTable]['fields'][$f]['eval'] = array('rgxp' => 'natural');
                        }
                    }
                }
                // Begin current row
                $strAjax = '';
                $blnAjax = false;
                $return .= '
<div class="' . $class . '">';
                $class = 'tl_box';
                $formFields = array();
                // Get the field values
                $objRow = $this->Database->prepare("SELECT * FROM " . $this->strTable . " WHERE id=?")->limit(1)->execute($this->intId);
                // Store the active record
                $this->objActiveRecord = $objRow;
                foreach ($this->strPalette as $v) {
                    // Check whether field is excluded
                    if ($GLOBALS['TL_DCA'][$this->strTable]['fields'][$v]['exclude']) {
                        continue;
                    }
                    if ($v == '[EOF]') {
                        if ($blnAjax && \Environment::get('isAjaxRequest')) {
                            return $strAjax . '<input type="hidden" name="FORM_FIELDS_' . $id . '[]" value="' . \StringUtil::specialchars(implode(',', $formFields)) . '">';
                        }
                        $blnAjax = false;
                        $return .= "\n  " . '</div>';
                        continue;
                    }
                    if (preg_match('/^\\[.*\\]$/', $v)) {
                        $thisId = 'sub_' . substr($v, 1, -1) . '_' . $id;
                        $blnAjax = $ajaxId == $thisId && \Environment::get('isAjaxRequest') ? true : false;
                        $return .= "\n  " . '<div id="' . $thisId . '">';
                        continue;
                    }
                    if (!in_array($v, $fields)) {
                        continue;
                    }
                    $this->strField = $v;
                    $this->strInputName = $v . '_' . $this->intId;
                    $formFields[] = $v . '_' . $this->intId;
                    // Set the default value and try to load the current value from DB (see #5252)
                    if (array_key_exists('default', $GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField])) {
                        $this->varValue = is_array($GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['default']) ? serialize($GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['default']) : $GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['default'];
                    }
                    if ($objRow->{$v} !== false) {
                        $this->varValue = $objRow->{$v};
                    }
                    // Convert CSV fields (see #2890)
                    if ($GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['eval']['multiple'] && isset($GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['eval']['csv'])) {
                        $this->varValue = \StringUtil::trimsplit($GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['eval']['csv'], $this->varValue);
                    }
                    // Call load_callback
                    if (is_array($GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['load_callback'])) {
                        foreach ($GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['load_callback'] as $callback) {
                            if (is_array($callback)) {
                                $this->import($callback[0]);
                                $this->varValue = $this->{$callback[0]}->{$callback[1]}($this->varValue, $this);
                            } elseif (is_callable($callback)) {
                                $this->varValue = $callback($this->varValue, $this);
                            }
                        }
                    }
                    // Re-set the current value
                    $this->objActiveRecord->{$this->strField} = $this->varValue;
                    // Build the row and pass the current palette string (thanks to Tristan Lins)
                    $blnAjax ? $strAjax .= $this->row($this->strPalette) : ($return .= $this->row($this->strPalette));
                }
                // Close box
                $return .= '
  <input type="hidden" name="FORM_FIELDS_' . $this->intId . '[]" value="' . \StringUtil::specialchars(implode(',', $formFields)) . '">
</div>';
                // Save record
                if (\Input::post('FORM_SUBMIT') == $this->strTable && !$this->noReload) {
                    // Call the onsubmit_callback
                    if (is_array($GLOBALS['TL_DCA'][$this->strTable]['config']['onsubmit_callback'])) {
                        foreach ($GLOBALS['TL_DCA'][$this->strTable]['config']['onsubmit_callback'] as $callback) {
                            if (is_array($callback)) {
                                $this->import($callback[0]);
                                $this->{$callback[0]}->{$callback[1]}($this);
                            } elseif (is_callable($callback)) {
                                $callback($this);
                            }
                        }
                    }
                    // Create a new version
                    if ($this->blnCreateNewVersion) {
                        $objVersions->create();
                        // Call the onversion_callback
                        if (is_array($GLOBALS['TL_DCA'][$this->strTable]['config']['onversion_callback'])) {
                            @trigger_error('Using the onversion_callback has been deprecated and will no longer work in Contao 5.0. Use the oncreate_version_callback instead.', E_USER_DEPRECATED);
                            foreach ($GLOBALS['TL_DCA'][$this->strTable]['config']['onversion_callback'] as $callback) {
                                if (is_array($callback)) {
                                    $this->import($callback[0]);
                                    $this->{$callback[0]}->{$callback[1]}($this->strTable, $this->intId, $this);
                                } elseif (is_callable($callback)) {
                                    $callback($this->strTable, $this->intId, $this);
                                }
                            }
                        }
                    }
                    // Set the current timestamp (-> DO NOT CHANGE ORDER version - timestamp)
                    if ($GLOBALS['TL_DCA'][$this->strTable]['config']['dynamicPtable']) {
                        $this->Database->prepare("UPDATE " . $this->strTable . " SET ptable=?, tstamp=? WHERE id=?")->execute($this->ptable, time(), $this->intId);
                    } else {
                        $this->Database->prepare("UPDATE " . $this->strTable . " SET tstamp=? WHERE id=?")->execute(time(), $this->intId);
                    }
                }
            }
            // Submit buttons
            $arrButtons = array();
            $arrButtons['save'] = '<button type="submit" name="save" id="save" class="tl_submit" accesskey="s">' . $GLOBALS['TL_LANG']['MSC']['save'] . '</button>';
            $arrButtons['saveNclose'] = '<button type="submit" name="saveNclose" id="saveNclose" class="tl_submit" accesskey="c">' . $GLOBALS['TL_LANG']['MSC']['saveNclose'] . '</button>';
            // Call the buttons_callback (see #4691)
            if (is_array($GLOBALS['TL_DCA'][$this->strTable]['edit']['buttons_callback'])) {
                foreach ($GLOBALS['TL_DCA'][$this->strTable]['edit']['buttons_callback'] as $callback) {
                    if (is_array($callback)) {
                        $this->import($callback[0]);
                        $arrButtons = $this->{$callback[0]}->{$callback[1]}($arrButtons, $this);
                    } elseif (is_callable($callback)) {
                        $arrButtons = $callback($arrButtons, $this);
                    }
                }
            }
            if (count($arrButtons) < 3) {
                $strButtons = implode(' ', $arrButtons);
            } else {
                $strButtons = array_shift($arrButtons) . ' ';
                $strButtons .= '<div class="split-button">';
                $strButtons .= array_shift($arrButtons) . '<button type="button" id="sbtog">' . \Image::getHtml('navcol.svg') . '</button> <ul class="invisible">';
                foreach ($arrButtons as $strButton) {
                    $strButtons .= '<li>' . $strButton . '</li>';
                }
                $strButtons .= '</ul></div>';
            }
            // Add the form
            $return = '

<form action="' . ampersand(\Environment::get('request'), true) . '" id="' . $this->strTable . '" class="tl_form" method="post" enctype="' . ($this->blnUploadable ? 'multipart/form-data' : 'application/x-www-form-urlencoded') . '">
<div class="tl_formbody_edit nogrid">
<input type="hidden" name="FORM_SUBMIT" value="' . $this->strTable . '">
<input type="hidden" name="REQUEST_TOKEN" value="' . REQUEST_TOKEN . '">' . ($this->noReload ? '

<p class="tl_error">' . $GLOBALS['TL_LANG']['ERR']['general'] . '</p>' : '') . $return . '

</div>

<div class="tl_formbody_submit">

<div class="tl_submit_container">
  ' . $strButtons . '
</div>

</div>
</form>';
            // Set the focus if there is an error
            if ($this->noReload) {
                $return .= '

<script>
  window.addEvent(\'domready\', function() {
    Backend.vScrollTo(($(\'' . $this->strTable . '\').getElement(\'label.error\').getPosition().y - 20));
  });
</script>';
            }
            // Reload the page to prevent _POST variables from being sent twice
            if (\Input::post('FORM_SUBMIT') == $this->strTable && !$this->noReload) {
                if (isset($_POST['saveNclose'])) {
                    \System::setCookie('BE_PAGE_OFFSET', 0, 0);
                    $this->redirect($this->getReferer());
                }
                $this->reload();
            }
        } else {
            $options = '';
            $fields = array();
            // Add fields of the current table
            $fields = array_merge($fields, array_keys($GLOBALS['TL_DCA'][$this->strTable]['fields']));
            // Add meta fields if the current user is an administrator
            if ($this->User->isAdmin) {
                if ($this->Database->fieldExists('sorting', $this->strTable) && !in_array('sorting', $fields)) {
                    array_unshift($fields, 'sorting');
                }
                if ($this->Database->fieldExists('pid', $this->strTable) && !in_array('pid', $fields)) {
                    array_unshift($fields, 'pid');
                }
            }
            // Show all non-excluded fields
            foreach ($fields as $field) {
                if ($field == 'pid' || $field == 'sorting' || !$GLOBALS['TL_DCA'][$this->strTable]['fields'][$field]['exclude'] && !$GLOBALS['TL_DCA'][$this->strTable]['fields'][$field]['eval']['doNotShow'] && (strlen($GLOBALS['TL_DCA'][$this->strTable]['fields'][$field]['inputType']) || is_array($GLOBALS['TL_DCA'][$this->strTable]['fields'][$field]['input_field_callback']) || is_callable($GLOBALS['TL_DCA'][$this->strTable]['fields'][$field]['input_field_callback']))) {
                    $options .= '
  <input type="checkbox" name="all_fields[]" id="all_' . $field . '" class="tl_checkbox" value="' . \StringUtil::specialchars($field) . '"> <label for="all_' . $field . '" class="tl_checkbox_label">' . (($GLOBALS['TL_DCA'][$this->strTable]['fields'][$field]['label'][0] ?: ($GLOBALS['TL_LANG']['MSC'][$field][0] ?: $field)) . ' <span style="color:#999;padding-left:3px">[' . $field . ']</span>') . '</label><br>';
                }
            }
            $blnIsError = $_POST && empty($_POST['all_fields']);
            // Return the select menu
            $return .= '

<form action="' . ampersand(\Environment::get('request'), true) . '&amp;fields=1" id="' . $this->strTable . '_all" class="tl_form" method="post">
<div class="tl_formbody_edit">
<input type="hidden" name="FORM_SUBMIT" value="' . $this->strTable . '_all">
<input type="hidden" name="REQUEST_TOKEN" value="' . REQUEST_TOKEN . '">' . ($blnIsError ? '

<p class="tl_error">' . $GLOBALS['TL_LANG']['ERR']['general'] . '</p>' : '') . '

<div class="tl_tbox">
<div class="widget">
<fieldset class="tl_checkbox_container">
  <legend' . ($blnIsError ? ' class="error"' : '') . '>' . $GLOBALS['TL_LANG']['MSC']['all_fields'][0] . '</legend>
  <input type="checkbox" id="check_all" class="tl_checkbox" onclick="Backend.toggleCheckboxes(this)"> <label for="check_all" style="color:#a6a6a6"><em>' . $GLOBALS['TL_LANG']['MSC']['selectAll'] . '</em></label><br>' . $options . '
</fieldset>' . ($blnIsError ? '
<p class="tl_error">' . $GLOBALS['TL_LANG']['ERR']['all_fields'] . '</p>' : (\Config::get('showHelp') && strlen($GLOBALS['TL_LANG']['MSC']['all_fields'][1]) ? '
<p class="tl_help tl_tip">' . $GLOBALS['TL_LANG']['MSC']['all_fields'][1] . '</p>' : '')) . '
</div>
</div>

</div>

<div class="tl_formbody_submit">

<div class="tl_submit_container">
  <button type="submit" name="save" id="save" class="tl_submit" accesskey="s">' . $GLOBALS['TL_LANG']['MSC']['continue'] . '</button>
</div>

</div>
</form>';
        }
        // Return
        return '
<div id="tl_buttons">
<a href="' . $this->getReferer(true) . '" class="header_back" title="' . \StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['backBTTitle']) . '" accesskey="b" onclick="Backend.getScrollOffset()">' . $GLOBALS['TL_LANG']['MSC']['backBT'] . '</a>
</div>' . $return;
    }