/ src / views / elements / UserrolesgroupsElement.php
<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026 Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett chris@pollett.org
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\views\elements;

use seekquarry\yioop as B;
use seekquarry\yioop\configs as C;

/**
 * Element used to draw the User, Roles, Groups settings screen: account
 * registration, monetization, bot configuration, and Git wiki page limits.
 * These are the account-and-content policy groups that were split off the
 * former combined Server Settings screen so machine setup and per-account
 * policy each have their own screen.
 *
 * @author Chris Pollett
 */
class UserrolesgroupsElement extends Element
{
    /**
     * Draws the form for account registration, monetization, bot
     * configuration, and Git wiki page settings.
     *
     * @param array $data holds the profile values to display together with
     *     the flags that decide which parts of the form are shown
     */
    public function render($data)
    {
        ?>
        <div class="current-activity">
        <form id="userRolesGroupsForm" method="post">
        <input type="hidden" name="c" value="admin" >
        <input type="hidden" name="<?= C\p('CSRF_TOKEN') ?>" value="<?=
            $data[C\p('CSRF_TOKEN')] ?>" >
        <input type="hidden" name="a" value="userRolesGroups" >
        <input type="hidden" name="arg" value="update" >
        <h2><?= tl('userrolesgroups_element_user_roles_groups')?></h2>
        <div class="bold">
            <div class="top-margin">
            <fieldset >
                <legend><label for="account-registration"><?=
                tl('userrolesgroups_element_account_registration') ?>
                </label>
                <?= $this->view->helper("helpbutton")->render(
                    "Account Registration", $data[c\CSRF_TOKEN])
                    ?></legend>
                <?php $this->view->helper("options")->render(
                    "account-registration", "REGISTRATION_TYPE",
                    $data['REGISTRATION_TYPES'],
                    $data['REGISTRATION_TYPE']);
                ?>
                <div id="registration-info">
                <div class="top-margin"><b><label for="mailsite-handled"><?=
                    tl('userrolesgroups_element_mailsite_handled')
                    ?></label></b>
                    <input type="checkbox" id="mailsite-handled"
                        name="MAIL_BOT_HANDLED"
                        value="true" <?php
                        if ($data['MAIL_BOT_HANDLED'] == true &&
                            $data['mailsite_for_registration'] ==
                            "true") {
                            e("checked='checked'");
                        }
                        if ($data['mailsite_for_registration'] !=
                            "true") {
                            e("disabled='disabled'");
                        } ?> >
                </div>
                <div class="top-margin"><b><label for="send-media-updater"><?=
                    tl('userrolesgroups_element_send_media_updater')
                    ?></label></b>
                    <input type="checkbox" id="send-media-updater"
                        name="SEND_MAIL_MEDIA_UPDATER"
                        value="true" <?php if (
                            $data['SEND_MAIL_MEDIA_UPDATER'] == true) {
                            e("checked='checked'");
                            }?> >
                </div>
                <div class="top-margin"><b><label for="mail-sender"><?=
                    tl('userrolesgroups_element_mail_sender')?></label></b>
                    <input type="email" id="mail-sender" name="MAIL_SENDER"
                        value="<?= $data['MAIL_SENDER'] ?>"
                        class="wide-field" >
                </div>
                <div id="mail-relay-fields">
                <div class="top-margin"><b><label for="mail-server"><?=
                    tl('userrolesgroups_element_mail_server')?></label></b>
                    <input type="text" id="mail-server" name="MAIL_SERVER"
                        value="<?=$data['MAIL_SERVER'] ?>"
                        class="wide-field" >
                </div>
                <div class="top-margin"><b><label for="mail-port"><?=
                    tl('userrolesgroups_element_mail_serverport')
                    ?></label></b>
                    <input type="text" id="mail-port" name="MAIL_SERVERPORT"
                        value="<?=$data['MAIL_SERVERPORT'] ?>"
                        class="wide-field" >
                </div>
                <div class="top-margin"><b><label for="mail-username"><?=
                    tl('userrolesgroups_element_mail_username')?></label></b>
                    <input type="text" id="mail-username" name="MAIL_USERNAME"
                        value="<?= $data['MAIL_USERNAME'] ?>"
                        class="wide-field" >
                </div>
                <div class="top-margin"><b><label for="mail-password"><?=
                    tl('userrolesgroups_element_mail_password')?></label></b>
                    <input type="password" id="mail-password"
                        name="MAIL_PASSWORD"
                        value="<?= $data['MAIL_PASSWORD'] ?>"
                        class="wide-field" >
                </div>
                <div class="top-margin"><b><label for="mail-security"><?=
                    tl('userrolesgroups_element_mail_security')?></label></b>
                    <select id="mail-security" name="MAIL_SECURITY">
                    <?php
                    $sec = $data['MAIL_SECURITY'] ?? 'starttls';
                    foreach (['plain' => 'mail_element_plain',
                        'starttls' => 'mail_element_starttls',
                        'smtps' => 'mail_element_smtps']
                        as $k => $label) {
                        $sel = ($sec === $k) ?
                            ' selected="selected"' : '';
                        ?><option value="<?= $k ?>"<?= $sel ?>><?=
                            tl($label) ?></option><?php
                    }
                    ?>
                    </select>
                </div>
                </div>
                </div>
            </fieldset>
            </div>
            <div class="top-margin">
            <fieldset>
                <legend><label for="role-limit-role"><?=
                    tl('userrolesgroups_element_role_group_limits')
                    ?></label>
                <?= $this->view->helper("helpbutton")->render(
                    "Role Group Limits", $data[C\p('CSRF_TOKEN')])
                    ?></legend>
                <div class="top-margin">
                    <b><label for="role-limit-role"><?=
                        tl('userrolesgroups_element_role') ?></label></b>
                    <select id="role-limit-role" name="selected_role"
                        onchange="showRoleLimits();">
                    <?php
                    $selected_role = $data['SELECTED_ROLE'] ?? "";
                    foreach ($data['ROLE_LIMITS'] as $role_id =>
                        $role_row) {
                        $chosen = ((string)$role_id ===
                            (string)$selected_role) ?
                            " selected=\"selected\"" : "";
                        ?><option value="<?= $role_id ?>"<?= $chosen
                            ?>><?= $role_row['NAME'] ?></option><?php
                    }
                    ?>
                    </select>
                </div>
                <?php
                foreach ($data['ROLE_LIMITS'] as $role_id => $role_row) {
                    ?>
                    <div class="role-limit-block"
                        id="role-limit-block-<?= $role_id ?>">
                    <?php
                    foreach ($data['ROLE_LIMIT_FIELDS'] as $limit_field) {
                        $limit_column = $limit_field['column'];
                        $current = $role_row[$limit_column] ?? "-1";
                        if ($current === null) {
                            $current = "-1";
                        }
                        ?>
                        <div class="top-margin">
                            <b><label><?= $limit_field['label']
                                ?></label></b>
                            <?php $this->view->helper("options")->render(
                                "role-limit-" . $role_id . "-" .
                                $limit_column, "role_limit_" . $role_id .
                                "_" . $limit_column, $limit_field['options'],
                                strval($current)); ?>
                        </div>
                        <?php
                    }
                    ?>
                        <div class="top-margin">
                            <b><label for="role-cost-<?= $role_id ?>"><?=
                                tl('userrolesgroups_element_role_cost')
                                ?></label></b>
                            <input type="number" min="0"
                                id="role-cost-<?= $role_id ?>"
                                name="role_cost_<?= $role_id ?>"
                                class="narrow-field" value="<?=
                                is_null($role_row['ROLE_COST']) ? 0 :
                                $role_row['ROLE_COST'] ?>" >
                            <?php $this->view->helper("options")->render(
                                "role-frequency-" . $role_id,
                                "role_frequency_" . $role_id,
                                $data['CHARGE_FREQUENCIES'],
                                $role_row['CHARGE_FREQUENCY'] ?? "never"); ?>
                        </div>
                    </div>
                    <?php
                }
                ?>
            </fieldset>
            </div>
            <div class="top-margin">
            <fieldset>
                <legend><label for="use-moderation"><?=
                    tl('userrolesgroups_element_moderation') ?></label>
                <?= $this->view->helper("helpbutton")->render(
                    "Moderation", $data[C\p('CSRF_TOKEN')])
                    ?></legend>
                <div class="top-margin">
                    <b><label for="use-moderation"><?=
                        tl('userrolesgroups_element_use_moderation')
                        ?></label></b>
                    <input type="checkbox" id="use-moderation"
                        name="USE_MODERATION" value="true" <?php
                        if ($data['USE_MODERATION'] == true) {
                            e("checked='checked'");
                        } ?> >
                </div>
                <div class="top-margin">
                    <b><label for="flag-threshold"><?=
                        tl('userrolesgroups_element_flag_threshold')
                        ?></label></b>
                    <input type="number" id="flag-threshold" min="1"
                        name="MODERATION_FLAG_THRESHOLD"
                        class="narrow-field"
                        value="<?= $data['MODERATION_FLAG_THRESHOLD']
                        ?>" >
                </div>
            </fieldset>
            </div>
            <div class="top-margin">
            <fieldset>
                <legend><label for="git-default-branch"><?=
                    tl('userrolesgroups_element_git') ?></label>
                <?= $this->view->helper("helpbutton")->render(
                    "Git Wiki Pages", $data[C\p('CSRF_TOKEN')])
                    ?></legend>
                <div class="top-margin">
                    <b><label for="git-default-branch"><?=
                        tl('userrolesgroups_element_git_default_branch')
                        ?></label></b>
                    <input type="text" id="git-default-branch"
                        name="GIT_DEFAULT_BRANCH" class="narrow-field"
                        value="<?= $data['GIT_DEFAULT_BRANCH'] ??
                        C\p('GIT_DEFAULT_BRANCH') ?>" >
                </div>
                <div class="top-margin">
                    <b><label for="git-max-blob-size"><?=
                        tl('userrolesgroups_element_git_max_blob_size')
                        ?></label></b>
                    <?php $this->view->helper("options")->render(
                        "git-max-blob-size", "GIT_MAX_BLOB_SIZE",
                        $data['GIT_MAX_BLOB_SIZE_OPTIONS'],
                        $data['GIT_MAX_BLOB_SIZE'] ?? "0"); ?>
                </div>
                <div class="top-margin">
                    <b><label for="git-max-push-size"><?=
                        tl('userrolesgroups_element_git_max_push_size')
                        ?></label></b>
                    <?php $this->view->helper("options")->render(
                        "git-max-push-size", "GIT_MAX_PUSH_SIZE",
                        $data['GIT_MAX_PUSH_SIZE_OPTIONS'],
                        $data['GIT_MAX_PUSH_SIZE'] ?? "0"); ?>
                </div>
            </fieldset>
            </div>
            <div class="top-margin">
            <fieldset><legend>
            <?= tl('userrolesgroups_element_monetization_config') .
            "&nbsp;" . $this->view->helper("helpbutton")->render(
                "Monetization", $data[C\p('CSRF_TOKEN')]) ?></legend>
            <div>
                <label for="monetization-type"><?=
                tl('userrolesgroups_element_monetization_type')
                    ?></label>
                <?php $this->view->helper("options")->render(
                    "monetization-type", "MONETIZATION_TYPE",
                    $data['MONETIZATION_TYPES'],
                    $data['MONETIZATION_TYPE']);
                ?>
            </div>
            <div id="payment-processing">
            <?php
            if (!C\CreditConfig::isActive()) { ?>
                <br><b class="red"><?=
                tl('userrolesgroups_element_no_payment_processing') ?></b><br>
                [<a href="https://www.seekquarry.com/adscript"><?=
                tl('userrolesgroups_element_purchase_processing')
                ?></a>]
                <?php
            }
            ?>
            </div>
            <div id="ad-location-info">
            <br><b><?=tl('userrolesgroups_element_ad_location') ?></b><br>
            <input id='ad-location-top' type='radio' name='AD_LOCATION'
                value="top" onchange="showHideScriptdiv();" <?=
                ($data['AD_LOCATION'] == 'top') ?'checked' : ''
                ?> ><label for="ad-location-top"><?=
                tl('userrolesgroups_element_top') ?></label>
            <input id='ad-location-side' type='radio' name='AD_LOCATION'
                value="side" onclick="showHideScriptdiv();" <?=
                ($data['AD_LOCATION'] == 'side') ?
                    'checked' : '' ?> ><label for="ad-location-side"><?=
                tl('userrolesgroups_element_side') ?></label>
            <input type='radio' id='ad-location-both'
                name='AD_LOCATION' value="both"
                onclick="showHideScriptdiv();" <?=
                ($data['AD_LOCATION'] == 'both') ? 'checked'
                    :'' ?> ><label for="ad-location-both"><?=
                tl('userrolesgroups_element_both') ?></label>
            <input type='radio' id='ad-location-none' name='AD_LOCATION'
                value="none" onclick="showHideScriptdiv();" <?=
                ($data['AD_LOCATION'] == 'none') ?
                    'checked' : '' ?> ><label for="ad-location-none"><?=
                tl('userrolesgroups_element_none') ?></label>
            <div id="global-adscript-config">
            <label for="global-adscript"><b><?=
            tl('userrolesgroups_element_global_adscript')
            ?></b></label>
            <textarea class="short-text-area" id="global-adscript"
                name="GLOBAL_ADSCRIPT"><?=
                html_entity_decode($data['GLOBAL_ADSCRIPT'], ENT_QUOTES)
                ?></textarea></div>
            <div id="top-adscript-config"><label for="top-adscript"><b><?=
                tl('userrolesgroups_element_top_adscript')
            ?></b></label>
            <textarea class="short-text-area" id="top-adscript"
                name="TOP_ADSCRIPT"><?=
                html_entity_decode($data['TOP_ADSCRIPT'], ENT_QUOTES)
                ?></textarea></div>
            <div id="side-adscript-config"><label for="side-adscript"><b><?=
                tl('userrolesgroups_element_side_adscript')
            ?></b></label>
            <textarea class="short-text-area" id="side-adscript"
                name="SIDE_ADSCRIPT"><?=
                html_entity_decode($data['SIDE_ADSCRIPT'], ENT_QUOTES)
            ?></textarea></div></div>
            </fieldset>
            </div>
            <div class="top-margin">
                <fieldset>
                    <legend>
            <?= tl('userrolesgroups_element_bot_configuration') .
            "&nbsp;" . $this->view->helper("helpbutton")->render(
                "Bot Configuration", $data[C\p('CSRF_TOKEN')]) ?></legend>
                <div>
                <label for="bot-configuration"><?=
                tl('userrolesgroups_element_bot_settings')
                    ?></label>
                <?php $this->view->helper("options")->render(
                    "bot-configuration","CONFIGURE_BOT",
                    $data['CONFIGURE_BOTS'],
                    $data['CONFIGURE_BOT']);
                ?>
            </div>
                </fieldset>
            </div>
            <div class="top-margin center">
            <button class="button-box" type="submit"><?=
                tl('userrolesgroups_element_save') ?></button>
            </div>
        </div>
        </form>
        </div>
        <script>
        window.onload = function()
        {
            showHideScriptdiv();
            selectMonetizationType();
            showRoleLimits();
        }
        /**
         * Method to show/block div including text area depending upon location
         * selected for the advertisement to display on search results page.
         */
        function showHideScriptdiv()
        {
            /*
             * Get the radio button list representing location for the
             * advertisement.
             */
            var ad_server_config = document.getElementsByName('AD_LOCATION');
            /*
             * Show/ block div with text area depending upon the radio
             * button value.
             */
            var ad_align = [
                ['block','block','none'], /* top[top,global,side] */
                ['none','block','block'], /* side */
                ['block','block','block'], /* both */
                ['none','none','none'], /* none */
            ];
            for (var i = 0; i < ad_server_config.length; i++){
                if (ad_server_config[i].checked) {
                    elt('top-adscript-config').style.display = ad_align[i][0];
                    elt('global-adscript-config').style.display =
                        ad_align[i][1];
                    elt('side-adscript-config').style.display = ad_align[i][2];
                    break;
                }
            }
        }
        function selectMonetizationType()
        {
            var monetization_type = elt('monetization-type').value;
            setDisplay('ad-location-info',
                (monetization_type == 'external_advertisements'));
            setDisplay('payment-processing',
                (monetization_type == 'fees_and_keywords' ||
                monetization_type == 'group_fees' ||
                monetization_type == 'keywords_advertisements'));
        }
        /*
         * Shows only the limit controls for the role picked in the role
         * drop-down, hiding every other role's block.
         */
        function showRoleLimits()
        {
            var role = elt('role-limit-role').value;
            var blocks =
                document.getElementsByClassName('role-limit-block');
            for (var i = 0; i < blocks.length; i++) {
                blocks[i].style.display =
                    (blocks[i].id == 'role-limit-block-' + role) ?
                    'block' : 'none';
            }
        }
        </script>
    <?php
    }
}
X