Last commit for src/views/elements/ManageaccountElement.php: 2addb500315b7393a90fe66431d7832b1e7386c7

Adjust copyrights years

Chris Pollett [2024-01-03 21:Jan:rd]
Adjust copyrights years
<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2023  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 - 2023
 * @filesource
 */
namespace seekquarry\yioop\views\elements;

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

/**
 * Element responsible for displaying the user account features
 * that someone can modify for their own SeekQuarry/Yioop account.
 *
 * @author Chris Pollett
 */
class ManageaccountElement extends Element
{
    /**
     * Draws a view with a summary of a user's account together with
     * a form for updating user info such as password as well as with
     * useful links for groups, etc
     *
     * @param array $data anti-CSRF token
     */
    public function render($data)
    {
        $token = C\CSRF_TOKEN . "=" . $data[C\CSRF_TOKEN];
        $admin_url = htmlentities(B\controllerUrl('admin', true));
        $feed_url =  htmlentities(B\feedsUrl("", "",
            true, "group")). "$token";
        $base_url = "{$admin_url}a=manageAccount&amp;$token";
        $edit_or_no_url = $base_url . (
            (isset($data['EDIT_USER'])) ? "&amp;edit=false":"&amp;edit=true");
        $icon_helper = $this->view->helper('iconlink');
        $is_mobile = !empty($_SERVER['MOBILE']);
        if (isset($data['EDIT_USER'])) {
            return $this->renderEditUser($data);
        }
        $more_groups =
            (count(($data['RECENT_GROUPS'] ?? [])) < $data['NUM_GROUPS']);
        ?>
        <div class="current-activity">
            <div class="account-info">
            <h2><img class='home-icon'
                src="<?= $data['USER']['USER_ICON'] ?>" alt="<?=
                    tl('manageaccount_element_icon') ?>" >
                <div><?= $data['USERNAME'] ?></div> <?=
                    $icon_helper->renderButton($edit_or_no_url,
                    'account_settings'); ?>
            </h2>
            </div>
            <?php
            if (isset($data['CRAWL_MANAGER']) && $data['CRAWL_MANAGER']) {
                ?>
                <h2 class="medium-margin"><?php
                e(tl('manageaccount_element_search_and_index')); ?>
                </h2>
                <?=$this->view->element("crawlcontrols")->render($data) ?>
                <?php
            } ?>
            <h2 class="medium-margin"><?=
            tl('manageaccount_element_groups')?></h2>
            <?=$this->view->element("socialcontrols")->render($data) ?>
            <?php
            $this->view->helper("grouplist")->render($data['GROUPS'], $token,
                $icon_helper, "admin", "account");
            if ($more_groups) {?>
                <div class="more-groups"><b><a href="<?=$feed_url?>"><?=
                    tl('manageaccount_element_more_groups')?></a></b></div><?php
            }
            if (!empty($data['THREAD_RECOMMENDATIONS']) ||
                !empty($data['GROUP_RECOMMENDATIONS']) ||
                !empty($data['RESOURCE_RECOMMENDATIONS'])) {
                ?>
                <h2><?=tl('manageaccount_element_recommendations')?></h2>
                <div class="access-result">
                <?php
                if (!empty($data['THREAD_RECOMMENDATIONS'])) {
                    ?><b><?=tl('manageaccount_element_rec_threads')
                    ?></b><ul><?php
                    foreach ($data['THREAD_RECOMMENDATIONS'] as
                        $thread => $threadName) { ?>
                        <li><a href="<?=htmlentities(B\feedsUrl("thread",
                            $thread, true, "group")) .
                                $token ?>" ><?= $threadName ?></a></li>
                        <?php
                    }?></ul><?php
                }
                ?><br><?php
                if (!empty($data['GROUP_RECOMMENDATIONS'])) {
                    ?><b><?=tl('manageaccount_element_rec_groups')
                    ?></b><ul><?php
                    foreach ($data['GROUP_RECOMMENDATIONS'] as
                        $group_id => $group_name) { ?>
                        <li><a href="<?= htmlentities(B\feedsUrl("group",
                            $group_id, true, "group")) .
                            $token ?>" ><?= $group_name ?></a></li>
                        <?php
                    }?></ul><?php
                }
                ?><br><?php
                if (!empty($data['RESOURCE_RECOMMENDATIONS'])) {
                    ?><b><?=tl('manageaccount_element_rec_resources')
                    ?></b><ul><?php
                    foreach ($data['RESOURCE_RECOMMENDATIONS'] as
                        $recommendation) {
                        $encoded_name = str_replace(" ",
                            "+", $recommendation[3]); ?><li>
                        <a href="<?= htmlentities(B\controllerUrl("group")) .
                            "/$recommendation[0]/$recommendation[2]?" .
                            "$token&page_id=$recommendation[1]&" .
                            "sf=$recommendation[4]&arg=media&n=$encoded_name"
                            ?>" ><?= $recommendation[3] ?></a></li>
                        <?php
                    }?></div><?php
                }
            }
            ?>
        </div>
        <?php
    }
    /**
     *  Renders the form used to control account settings such as
     *  icon photo, email, password, etc. for a user
     *
     *  @param array $data has field with current values for these setting
     *   (except password) so that forms can be pre-populated.
     */
    public function renderEditUser($data)
    {
        $token = C\CSRF_TOKEN . "=" . $data[C\CSRF_TOKEN];
        $admin_url = htmlentities(B\controllerUrl('admin', true));
        $base_url = "{$admin_url}a=manageAccount&amp;$token";
        $password_or_no_url = $base_url .(
            (isset($data['EDIT_PASSWORD'])) ? "&amp;edit_pass=false":
            "&amp;edit_pass=true");
        $edit_or_no_url = $base_url . (
            (isset($data['EDIT_USER'])) ? "&amp;edit=false":"&amp;edit=true");
        $disabled = (isset($data['EDIT_USER'])) ? "" : "disabled='disabled'";
        $is_mobile = !empty($_SERVER['MOBILE']);
        ?>
        <div class="current-activity">
        <div class="account-info" tabindex="1">
        <?= $this->view->helper("close")->render($edit_or_no_url); ?>
        <form id="changeUserForm" method="post" class="account-details"
        autocomplete="off" enctype="multipart/form-data">
        <h2><?=tl('manageaccount_element_account_details')
        ?></h2><?php
        $row_col_span = 'rowspan="8"';
        if (isset($data['EDIT_PASSWORD'])) {
            $row_col_span = 'rowspan="13"';
        }
        if (!empty($data['USER']['IS_BOT_USER'])) {
            $row_col_span = 'rowspan="18"';
        }
        if ($is_mobile) {
            $row_col_span = 'class="center" style="width:300px"';
        } ?>
        <input type="hidden" name="c" value="admin" >
        <input type="hidden" name="<?= C\CSRF_TOKEN ?>" value="<?=
            $data[C\CSRF_TOKEN] ?>" >
        <input type="hidden" name="a" value="manageAccount" >
        <input type="hidden" name="arg" value="updateuser" >
        <table class="name-table">
        <tr>
        <td <?=$row_col_span?> class="user-icon-td" ><img
        class='user-icon' id='current-icon'
        src="<?= $data['USER']['USER_ICON'] ?>" alt="<?=
            tl('manageaccount_element_icon') ?>" ><?php
        $this->view->helper("fileupload")->render('current-icon',
            'user_icon', 'user-icon',  C\THUMB_SIZE, 'image',
            ['image/png', 'image/gif', 'image/jpeg']);
        if ($is_mobile) { ?>
            </td></tr></table><table class="name-table">
            <?php
        } else {
            e('</td>');
        }?>
        <th class="table-label"><label for="user-name"><?=
        tl('manageaccount_element_username') ?>:</label></th>
        <td><input type="text" id="user-name"
            name="user_name"  maxlength="<?= C\NAME_LEN ?>"
            value="<?= $data['USER']['USER_NAME'] ?>"
            class="narrow-field" disabled="disabled" ></td>
            </tr>
        <tr><th class="table-label"><label for="first-name"><?php
            e(tl('manageaccount_element_firstname')); ?>:</label></th>
        <td><input type="text" id="first-name"
            name="FIRST_NAME"  maxlength="<?= C\NAME_LEN?>"
            value="<?php e($data['USER']['FIRST_NAME']); ?>"
            class="narrow-field" <?php e($disabled);?> ></td></tr>
        <tr><th class="table-label"><label for="last-name"><?php
            e(tl('manageaccount_element_lastname')); ?>:</label></th>
        <td><input type="text" id="last-name"
            name="LAST_NAME"  maxlength="<?= C\NAME_LEN ?>"
            value="<?php e($data['USER']['LAST_NAME']); ?>"
            class="narrow-field" <?php e($disabled);?> ></td></tr>
        <tr><th class="table-label"><label for="e-mail"><?php
            e(tl('manageaccount_element_email')); ?>:</label></th>
        <td><input type="email" id="e-mail"
            name="EMAIL"  maxlength="<?= C\LONG_NAME_LEN ?>"
            <?php e($disabled);?>
            value="<?php e($data['USER']['EMAIL']); ?>"
            class="narrow-field" ></td></tr>
        <tr>
        <th class="table-label"><label for="locale"><b><?=
            tl('options_element_language_label')
            ?></b></label></th>
        <td><?php
            $this->view->element("language")->render($data); ?></td>
        </tr><?php
        if (!empty($data['yioop_bot_configuration'])) {
            ?>
            <tr>
            <th class="table-label"><label for="is_bot"><?php
                    e(tl('manageaccount_element_is_bot'));
                    ?></label></th>
            <td><input type="checkbox" id="is_bot"
                       name="IS_BOT_USER" value="true"
                    <?php if (!empty($data['USER']['IS_BOT_USER'])
                        && $data['USER']['IS_BOT_USER'] == true) {
                            e("checked='checked'");
                    } ?>
                >
            </td></tr><?php
            if ($data['USER']['IS_BOT_USER'] == true) { ?>
                <tr><th class="table-label">
                    <label for="bot-unique-token"><?php
                    e(tl('manageaccount_element_bot_unique_token'))
                    ?></label></th>
                    <td><input type="text" id="bot-unique-token"
                        name="BOT_TOKEN" value="<?php
                        e($data['USER']['BOT_TOKEN']); ?>"
                        class="narrow-field" >
                    </td>
                </tr>
                <tr>
                    <th class="table-label">
                    <label for="bot-callback-url"><?php
                    e(tl('manageaccount_element_bot_callback_url'));
                    ?></label></th>
                    <td><input type="text" id="bot-callback-url"
                        name="BOT_CALLBACK_URL" value="<?php
                        e($data['USER']['CALLBACK_URL']); ?>"
                        class="narrow-field" >
                    </td>
                </tr>
                <?php
            }
        }?>
        <tr><th class="table-label"><label for="password"><a href="<?php
        e($password_or_no_url);?>"><?php
        e(tl('manageaccount_element_password'))?></a></label></th>
        <td><input type="password" id="password"
            name="password"  maxlength="<?= C\LONG_NAME_LEN
            ?>" class="narrow-field" >
        </td></tr>
        <?php if (isset($data['EDIT_PASSWORD'])) { ?>
        <tr><th class="table-label"><label for="new-password"><?php
            e(tl('manageaccount_element_new_password'))?></label></th>
            <td><input type="password" id="new-password"
                name="new_password"  maxlength="<?=
                C\LONG_NAME_LEN?>" class="narrow-field" >
            </td></tr>
        <tr><th class="table-label"><label for="retype-password"><?php
            e(tl('manageaccount_element_retype_password'));
            ?></label></th>
            <td><input type="password" id="retype-password"
                name="retype_password"  maxlength="<?=
                C\LONG_NAME_LEN?>" class="narrow-field" >
            </td></tr>
            <?php
            $question_sets = [];
            if (C\RECOVERY_MODE == C\EMAIL_AND_QUESTIONS_RECOVERY) {
                $question_sets = [
                    tl('manageaccount_element_new_recovery_qa') =>
                    $data['RECOVERY']];
            }
            $i = 0;
            foreach ($question_sets as $name => $set) {
                $first = true;
                $num = count($set);
                foreach ($set as $question) {
                    if ($first) { ?>
                        <tr><th class="table-label"
                            rowspan='<?= $num
                            ?>' style="max-width:2in;"><?php
                            e($name);
                        ?></th><td class="table-input border-top">
                    <?php
                    } else { ?>
                        <tr><td class="table-input">
                    <?php
                    }
                    $this->view->helper("options")->render(
                        "question-$i", "question_$i",
                        $question, $data['RECOVERY_ANSWERS'][$i]);
                    $first = false;
                    e("</td></tr>");
                    $i++;
                }
            }
        } ?>
        <tr><td></td>
        <td class="center"><button
            class="button-box" type="submit"><?php
            e(tl('manageaccount_element_save')); ?></button></td></tr>
        </table>
        </form>
        </div>
        </div><?php
    }
}
ViewGit