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

        $oDb    = Factory::service('Database');
        $oInput = Factory::service('Input');
        $oModel = $this->getModel();

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

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

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

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

                $this->runFormValidation(static::EDIT_MODE_CREATE);
                $oDb->trans_begin();
                $this->beforeCreateAndEdit(static::EDIT_MODE_CREATE);
                $this->beforeCreate();

                if (classUses($oModel, Localised::class)) {

                    /** @var Locale $oLocale */
                    $oLocale = Factory::service('Locale');
                    /** @var \Nails\Common\Factory\Locale $oItemLocale */
                    $oItemLocale = Factory::factory('Locale');
                    $oLocale->setFromString($oItemLocale, $oInput->post('locale'));

                    $oItem = $oModel->create($this->getPostObject(), true, $oItemLocale);

                } else {
                    $oItem = $oModel->create($this->getPostObject(), true);
                }

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

                $this->afterCreateAndEdit(static::EDIT_MODE_CREATE, $oItem);
                $this->afterCreate($oItem);
                $oDb->trans_commit();

                if (property_exists($oItem, 'url')) {
                    $sLink = anchor(
                        $oItem->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::CREATE_SUCCESS_MESSAGE, $sLink));

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

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

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

        $oUri           = Factory::service('Uri');
        $iExistingId    = (int) $oUri->segment(5) ?: null;
        $sDesiredLocale = $oUri->segment(6) ?: null;
        if (classUses($oModel, Localised::class) && !$iExistingId) {

            /** @var Locale $oLocale */
            $oLocale = Factory::service('Locale');
            foreach ($aConfig['FIELDS'] as $oField) {
                if ($oField->key === 'locale') {
                    $oField->info .= ' New items must be created in ' . $oLocale->getDefautLocale()->getDisplayLanguage();
                    break;
                }
            }

        } elseif (classUses($oModel, Localised::class)) {

            //  Validate the ID, and only allow creation of new locales
            $aExisting = $oModel->getAll([
                'NO_LOCALISE_FILTER' => true,
                'where'              => [
                    ['id', $iExistingId],
                ],
            ]);

            if (empty($aExisting)) {
                show404();
            }

            //  Test if new locales can be created, filter out existing locales from the options list
            $aExistingLocales = arrayExtractProperty($aExisting, 'locale');
            foreach ($aConfig['FIELDS'] as $oField) {
                if ($oField->key === 'locale') {

                    $aDiff = array_diff(
                        array_keys($oField->options),
                        array_values($aExistingLocales)
                    );

                    if (empty($aDiff)) {
                        $oSession = Factory::service('Session', 'nails/module-auth');
                        $oSession->setFlashData('error', 'No more variations of this item can be created.');
                        $this->returnToIndex();
                    }

                    $oField->options = array_intersect_key(
                        $oField->options,
                        array_flip($aDiff)
                    );

                    $oField->default = $sDesiredLocale;


                    unset($this->aConfig['CREATE_READONLY_FIELDS'][array_search('locale', $this->aConfig['CREATE_READONLY_FIELDS'])]);
                    break;
                }
            }
        }

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

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

        $oDb    = Factory::service('Database');
        $oInput = Factory::service('Input');
        $oModel = $this->getModel();

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

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

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

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

                $this->runFormValidation(static::EDIT_MODE_CREATE);
                $oDb->trans_begin();
                $this->beforeCreateAndEdit(static::EDIT_MODE_CREATE);
                $this->beforeCreate();

                if (classUses($oModel, Localised::class)) {

                    /** @var Locale $oLocale */
                    $oLocale = Factory::service('Locale');
                    /** @var \Nails\Common\Factory\Locale $oItemLocale */
                    $oItemLocale = Factory::factory('Locale');
                    $oLocale->setFromString($oItemLocale, $oInput->post('locale'));

                    $oItem = $oModel->create($this->getPostObject(), true, $oItemLocale);

                } else {
                    $oItem = $oModel->create($this->getPostObject(), true);
                }

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

                $this->afterCreateAndEdit(static::EDIT_MODE_CREATE, $oItem);
                $this->afterCreate($oItem);
                $oDb->trans_commit();

                if (property_exists($oItem, 'url')) {
                    $sLink = anchor(
                        $oItem->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::CREATE_SUCCESS_MESSAGE, $sLink));

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

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

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

        $oUri           = Factory::service('Uri');
        $iExistingId    = (int) $oUri->segment(5) ?: null;
        $sDesiredLocale = $oUri->segment(6) ?: null;
        if (classUses($oModel, Localised::class) && !$iExistingId) {

            /** @var Locale $oLocale */
            $oLocale = Factory::service('Locale');
            foreach ($aConfig['FIELDS'] as $oField) {
                if ($oField->key === 'locale') {
                    $oField->info .= ' New items must be created in ' . $oLocale->getDefautLocale()->getDisplayLanguage();
                    break;
                }
            }

        } elseif (classUses($oModel, Localised::class)) {

            //  Validate the ID, and only allow creation of new locales
            $aExisting = $oModel->getAll([
                'NO_LOCALISE_FILTER' => true,
                'where'              => [
                    ['id', $iExistingId],
                ],
            ]);

            if (empty($aExisting)) {
                show404();
            }

            //  Test if new locales can be created, filter out existing locales from the options list
            $aExistingLocales = arrayExtractProperty($aExisting, 'locale');
            foreach ($aConfig['FIELDS'] as $oField) {
                if ($oField->key === 'locale') {

                    $aDiff = array_diff(
                        array_keys($oField->options),
                        array_values($aExistingLocales)
                    );

                    if (empty($aDiff)) {
                        $oSession = Factory::service('Session', 'nails/module-auth');
                        $oSession->setFlashData('error', 'No more variations of this item can be created.');
                        $this->returnToIndex();
                    }

                    $oField->options = array_intersect_key(
                        $oField->options,
                        array_flip($aDiff)
                    );

                    $oField->default = $sDesiredLocale;


                    unset($this->aConfig['CREATE_READONLY_FIELDS'][array_search('locale', $this->aConfig['CREATE_READONLY_FIELDS'])]);
                    break;
                }
            }
        }

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

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

@return void