Admidio/admidio

View on GitHub
adm_program/modules/links/links_new.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php
/**
 ***********************************************************************************************
 * Create and edit weblinks
 *
 * @copyright The Admidio Team
 * @see https://www.admidio.org/
 * @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
 *
 * Parameters:
 *
 * link_uuid - UUID of the weblink that should be edited
 ***********************************************************************************************
 */
use Admidio\Exception;
use Admidio\UserInterface\Form;

try {
    require_once(__DIR__ . '/../../system/common.php');
    require(__DIR__ . '/../../system/login_valid.php');

    // Initialize and check the parameters
    $getLinkUuid = admFuncVariableIsValid($_GET, 'link_uuid', 'uuid');

    // check if the module is enabled for use
    if ((int)$gSettingsManager->get('enable_weblinks_module') === 0) {
        throw new Exception('SYS_MODULE_DISABLED');
    }

    // create weblink object
    $link = new TableWeblink($gDb);

    if ($getLinkUuid !== '') {
        $link->readDataByUuid($getLinkUuid);

        // check if the current user could edit this weblink
        if (!$link->isEditable()) {
            throw new Exception('SYS_NO_RIGHTS');
        }
    } else {
        // check if the user has the right to edit at least one category
        if (count($gCurrentUser->getAllEditableCategories('LNK')) === 0) {
            throw new Exception('SYS_NO_RIGHTS');
        }
    }

    if ($getLinkUuid !== '') {
        $headline = $gL10n->get('SYS_EDIT_WEBLINK');
    } else {
        $headline = $gL10n->get('SYS_CREATE_WEBLINK');
    }

    // add current url to navigation stack
    $gNavigation->addUrl(CURRENT_URL, $headline);

    // create html page object
    $page = new HtmlPage('admidio-weblinks-edit', $headline);

    // show form
    $form = new Form(
        'weblinks_edit_form',
        'modules/links.edit.tpl',
        SecurityUtils::encodeUrl(ADMIDIO_URL . FOLDER_MODULES . '/links/links_function.php', array('link_uuid' => $getLinkUuid, 'mode' => 'create')),
        $page
    );
    $form->addInput(
        'lnk_name',
        $gL10n->get('SYS_LINK_NAME'),
        $link->getValue('lnk_name'),
        array('maxLength' => 250, 'property' => Form::FIELD_REQUIRED)
    );
    $form->addInput(
        'lnk_url',
        $gL10n->get('SYS_LINK_ADDRESS'),
        $link->getValue('lnk_url'),
        array('type' => 'url', 'maxLength' => 2000, 'property' => Form::FIELD_REQUIRED)
    );
    $form->addSelectBoxForCategories(
        'lnk_cat_id',
        $gL10n->get('SYS_CATEGORY'),
        $gDb,
        'LNK',
        Form::SELECT_BOX_MODUS_EDIT,
        array('property' => Form::FIELD_REQUIRED, 'defaultValue' => $link->getValue('cat_uuid'))
    );
    $form->addEditor(
        'lnk_description',
        $gL10n->get('SYS_DESCRIPTION'),
        $link->getValue('lnk_description')
    );
    $form->addSubmitButton('btn_save', $gL10n->get('SYS_SAVE'), array('icon' => 'bi-check-lg'));

    $page->assignSmartyVariable('nameUserCreated', $link->getNameOfCreatingUser());
    $page->assignSmartyVariable('timestampUserCreated', $link->getValue('lnk_timestamp_create'));
    $page->assignSmartyVariable('nameLastUserEdited', $link->getNameOfLastEditingUser());
    $page->assignSmartyVariable('timestampLastUserEdited', $link->getValue('lnk_timestamp_change'));
    $form->addToHtmlPage();
    $gCurrentSession->addFormObject($form);

    $page->show();
} catch (Exception $e) {
    $gMessage->show($e->getMessage());
}