Contao\DC_Folder::edit PHP Method

edit() public method

Auto-generate a form to rename a file or folder
public edit ( ) : string
return string
    public function edit()
    {
        $return = '';
        $this->noReload = false;
        $this->isValid($this->intId);
        if (!file_exists(TL_ROOT . '/' . $this->intId) || !$this->isMounted($this->intId)) {
            throw new AccessDeniedException('File or folder "' . $this->intId . '" is not mounted or cannot be found.');
        }
        $objModel = null;
        $objVersions = null;
        // Add the versioning routines
        if ($this->blnIsDbAssisted && \Dbafs::shouldBeSynchronized($this->intId)) {
            if (stristr($this->intId, '__new__') === false) {
                $objModel = \FilesModel::findByPath($this->intId);
                if ($objModel === null) {
                    $objModel = \Dbafs::addResource($this->intId);
                }
                $this->objActiveRecord = $objModel;
            }
            $this->blnCreateNewVersion = false;
            /** @var FilesModel $objModel */
            $objVersions = new \Versions($this->strTable, $objModel->id);
            if (!$GLOBALS['TL_DCA'][$this->strTable]['config']['hideVersionMenu']) {
                // Compare versions
                if (\Input::get('versions')) {
                    $objVersions->compare();
                }
                // Restore a version
                if (\Input::post('FORM_SUBMIT') == 'tl_version' && \Input::post('version') != '') {
                    $objVersions->restore(\Input::post('version'));
                    $this->reload();
                }
            }
            $objVersions->initialize();
        } else {
            // Unset the database fields
            $GLOBALS['TL_DCA'][$this->strTable]['fields'] = array_intersect_key($GLOBALS['TL_DCA'][$this->strTable]['fields'], array('name' => true, 'protected' => true));
        }
        // Build an array from boxes and rows (do not show excluded fields)
        $this->strPalette = $this->getPalette();
        $boxes = \StringUtil::trimsplit(';', $this->strPalette);
        if (!empty($boxes)) {
            // Get fields
            foreach ($boxes as $k => $v) {
                $boxes[$k] = \StringUtil::trimsplit(',', $v);
                foreach ($boxes[$k] as $kk => $vv) {
                    if ($GLOBALS['TL_DCA'][$this->strTable]['fields'][$vv]['exclude'] || !isset($GLOBALS['TL_DCA'][$this->strTable]['fields'][$vv])) {
                        unset($boxes[$k][$kk]);
                    }
                }
                // Unset a box if it does not contain any fields
                if (empty($boxes[$k])) {
                    unset($boxes[$k]);
                }
            }
            // Render boxes
            $class = 'tl_tbox';
            foreach ($boxes as $v) {
                $return .= '
<div class="' . $class . ' cf">';
                // Build rows of the current box
                foreach ($v as $vv) {
                    $this->strField = $vv;
                    $this->strInputName = $vv;
                    // Load the current value
                    if ($vv == 'name') {
                        $objFile = is_dir(TL_ROOT . '/' . $this->intId) ? new \Folder($this->intId) : new \File($this->intId);
                        $this->strPath = str_replace(TL_ROOT . '/', '', $objFile->dirname);
                        $this->strExtension = $objFile->origext != '' ? '.' . $objFile->origext : '';
                        $this->varValue = $objFile->filename;
                        // Fix hidden Unix system files
                        if (strncmp($this->varValue, '.', 1) === 0) {
                            $this->strExtension = '';
                        }
                        // Clear the current value if it is a new folder
                        if (\Input::post('FORM_SUBMIT') != 'tl_files' && \Input::post('FORM_SUBMIT') != 'tl_templates' && $this->varValue == '__new__') {
                            $this->varValue = '';
                        }
                    } else {
                        $this->varValue = $objModel !== null ? $objModel->{$vv} : null;
                    }
                    // 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);
                            }
                        }
                    }
                    // Build row
                    $return .= $this->row();
                }
                $class = 'tl_box';
                $return .= '
  <input type="hidden" name="FORM_FIELDS[]" value="' . \StringUtil::specialchars($this->strPalette) . '">
</div>';
            }
        }
        // Versions overview
        if ($GLOBALS['TL_DCA'][$this->strTable]['config']['enableVersioning'] && !$GLOBALS['TL_DCA'][$this->strTable]['config']['hideVersionMenu'] && $this->blnIsDbAssisted && \Dbafs::shouldBeSynchronized($this->intId)) {
            $version = $objVersions->renderDropdown();
        } else {
            $version = '';
        }
        // Submit buttons
        $arrButtons = array();
        $arrButtons['save'] = '<button type="submit" name="save" id="save" class="tl_submit" accesskey="s">' . $GLOBALS['TL_LANG']['MSC']['save'] . '</button>';
        if (!\Input::get('nb')) {
            $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 buttons and end the form
        $return .= '
</div>

<div class="tl_formbody_submit">

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

</div>
</form>';
        // Begin the form (-> DO NOT CHANGE THIS ORDER -> this way the onsubmit attribute of the form can be changed by a field)
        $return = $version . '
<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>
' . \Message::generate() . '
<form action="' . ampersand(\Environment::get('request'), true) . '" id="' . $this->strTable . '" class="tl_form" method="post"' . (!empty($this->onsubmit) ? ' onsubmit="' . implode(' ', $this->onsubmit) . '"' : '') . '>
<div class="tl_formbody_edit">
<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;
        // Reload the page to prevent _POST variables from being sent twice
        if (\Input::post('FORM_SUBMIT') == $this->strTable && !$this->noReload) {
            // Trigger 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);
                    }
                }
            }
            // Save the current version
            if ($this->blnCreateNewVersion && $objModel !== null) {
                $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, $objModel->id, $this);
                        } elseif (is_callable($callback)) {
                            $callback($this->strTable, $objModel->id, $this);
                        }
                    }
                }
            }
            // Set the current timestamp (-> DO NOT CHANGE THE ORDER version - timestamp)
            if ($this->blnIsDbAssisted && $objModel !== null) {
                $this->Database->prepare("UPDATE " . $this->strTable . " SET tstamp=? WHERE id=?")->execute(time(), $objModel->id);
            }
            // Redirect
            if (isset($_POST['saveNclose'])) {
                \Message::reset();
                \System::setCookie('BE_PAGE_OFFSET', 0, 0);
                $this->redirect($this->getReferer());
            }
            // Reload
            if ($this->blnIsDbAssisted && $this->objActiveRecord !== null) {
                $this->redirect($this->addToUrl('id=' . $this->urlEncode($this->objActiveRecord->path)));
            } else {
                $this->redirect($this->addToUrl('id=' . $this->urlEncode($this->strPath . '/' . $this->varValue) . $this->strExtension));
            }
        }
        // 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>';
        }
        return $return;
    }