src/Controller/DefaultController.php
public function edit(): void
    {
        $aConfig = $this->getConfig();
        if (!$aConfig['CAN_EDIT']) {
            show404();
        } elseif (!static::userCan('edit')) {
            unauthorised();
        }

        $oDb    = Factory::service('Database');
        $oInput = Factory::service('Input');
        $oModel = $this->getModel();
        $oItem  = $this->getItem($aConfig['EDIT_DATA']);

        // --------------------------------------------------------------------------

        //  View Data & Assets
        $this->loadEditViewData($oItem);

        // --------------------------------------------------------------------------

        if ($oInput->post()) {
            try {

                $this->runFormValidation(static::EDIT_MODE_EDIT);
                $oDb->trans_begin();
                $this->beforeCreateAndEdit(static::EDIT_MODE_EDIT, $oItem);
                $this->beforeEdit($oItem);

                if (classUses($oModel, Localised::class)) {
                    $bResult = $oModel->update($oItem->id, $this->getPostObject(), $oItem->locale);
                } else {
                    $bResult = $oModel->update($oItem->id, $this->getPostObject());
                }

                if (!$bResult) {
                    throw new NailsException(static::EDIT_ERROR_MESSAGE . ' ' . $oModel->lastError());
                }

                if (classUses($oModel, Localised::class)) {
                    $oNewItem = $oModel->getById($oItem->id, [
                        'USE_LOCALE' => $oInput->post('locale'),
                    ]);
                } else {
                    $oNewItem = $oModel->getById($oItem->id);
                }
                $this->afterCreateAndEdit(static::EDIT_MODE_EDIT, $oNewItem, $oItem);
                $this->afterEdit($oNewItem, $oItem);
                $oDb->trans_commit();

                if (property_exists($oNewItem, 'url')) {
                    $sLink = anchor(
                        $oNewItem->url,
                        'View &nbsp;<span class="fa fa-external-link"></span>',
                        'class="btn btn-success btn-xs pull-right" target="_blank"'
                    );
                } else {
                    $sLink = '';
                }

                $oSession = Factory::service('Session', 'nails/module-auth');
                $oSession->setFlashData('success', sprintf(static::EDIT_SUCCESS_MESSAGE, $sLink));

                if (classUses($oModel, Localised::class)) {
                    redirect($aConfig['BASE_URL'] . '/edit/' . $oItem->id . '/' . $oItem->locale);
                } else {
                    redirect($aConfig['BASE_URL'] . '/edit/' . $oItem->id);
                }

            } catch (\Exception $e) {
                $oDb->trans_rollback();
                $this->data['error'] = $e->getMessage();
            }
        }

        // --------------------------------------------------------------------------

        static::addHeaderButtons($aConfig['EDIT_HEADER_BUTTONS']);

        // --------------------------------------------------------------------------

        $this->data['page']->title = $aConfig['TITLE_SINGLE'] . ' &rsaquo; Edit';
        Helper::loadView('edit');
    }
public function edit(): void
    {
        $aConfig = $this->getConfig();
        if (!$aConfig['CAN_EDIT']) {
            show404();
        } elseif (!static::userCan('edit')) {
            unauthorised();
        }

        $oDb    = Factory::service('Database');
        $oInput = Factory::service('Input');
        $oModel = $this->getModel();
        $oItem  = $this->getItem($aConfig['EDIT_DATA']);

        // --------------------------------------------------------------------------

        //  View Data & Assets
        $this->loadEditViewData($oItem);

        // --------------------------------------------------------------------------

        if ($oInput->post()) {
            try {

                $this->runFormValidation(static::EDIT_MODE_EDIT);
                $oDb->trans_begin();
                $this->beforeCreateAndEdit(static::EDIT_MODE_EDIT, $oItem);
                $this->beforeEdit($oItem);

                if (classUses($oModel, Localised::class)) {
                    $bResult = $oModel->update($oItem->id, $this->getPostObject(), $oItem->locale);
                } else {
                    $bResult = $oModel->update($oItem->id, $this->getPostObject());
                }

                if (!$bResult) {
                    throw new NailsException(static::EDIT_ERROR_MESSAGE . ' ' . $oModel->lastError());
                }

                if (classUses($oModel, Localised::class)) {
                    $oNewItem = $oModel->getById($oItem->id, [
                        'USE_LOCALE' => $oInput->post('locale'),
                    ]);
                } else {
                    $oNewItem = $oModel->getById($oItem->id);
                }
                $this->afterCreateAndEdit(static::EDIT_MODE_EDIT, $oNewItem, $oItem);
                $this->afterEdit($oNewItem, $oItem);
                $oDb->trans_commit();

                if (property_exists($oNewItem, 'url')) {
                    $sLink = anchor(
                        $oNewItem->url,
                        'View &nbsp;<span class="fa fa-external-link"></span>',
                        'class="btn btn-success btn-xs pull-right" target="_blank"'
                    );
                } else {
                    $sLink = '';
                }

                $oSession = Factory::service('Session', 'nails/module-auth');
                $oSession->setFlashData('success', sprintf(static::EDIT_SUCCESS_MESSAGE, $sLink));

                if (classUses($oModel, Localised::class)) {
                    redirect($aConfig['BASE_URL'] . '/edit/' . $oItem->id . '/' . $oItem->locale);
                } else {
                    redirect($aConfig['BASE_URL'] . '/edit/' . $oItem->id);
                }

            } catch (\Exception $e) {
                $oDb->trans_rollback();
                $this->data['error'] = $e->getMessage();
            }
        }

        // --------------------------------------------------------------------------

        static::addHeaderButtons($aConfig['EDIT_HEADER_BUTTONS']);

        // --------------------------------------------------------------------------

        $this->data['page']->title = $aConfig['TITLE_SINGLE'] . ' &rsaquo; Edit';
        Helper::loadView('edit');
    }
Edit an existing item

@return void