<?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;

use seekquarry\yioop\configs as C;

/**
 * Draws the page an email recipient lands on when they follow a group's
 * unsubscribe link, using the normal site layout. Depending on the
 * request it shows one of three things: a notice that the link was not
 * valid, a confirmation prompt with an Unsubscribe button, or a message
 * that the unsubscribe is done. The confirm button repeats the signed
 * token and the one-click marker, so the same endpoint a mail client's
 * one-click request reaches is the one the button reaches.
 *
 * @author Chris Pollett
 */
class UnsubscribeView extends View
{
    /**
     * This view is drawn on the normal web layout.
     * @var string
     */
    public $layout = "web";
    /**
     * Draws the unsubscribe message and, when confirming, the button.
     *
     * @param array $data fields set by the unsubscribe activity in
     *      ApiController: UNSUBSCRIBE_STATE (invalid, confirm, or done),
     *      GROUP_NAME, and UNSUBSCRIBE_TOKEN
     */
    public function renderView($data)
    {
        $state = $data['UNSUBSCRIBE_STATE'] ?? "invalid";
        $scope = $data['UNSUBSCRIBE_SCOPE'] ?? "group";
        ?>
        <div class="unsubscribe-page"><?php
        if ($state == "invalid") {
            ?><p><?= tl('api_unsubscribe_invalid') ?></p><?php
        } else if ($state == "done" && $scope == "all") {
            ?><p><?= tl('api_unsubscribe_all_done',
                $data['UNSUBSCRIBE_EMAIL']) ?></p><?php
        } else if ($state == "done") {
            ?><p><?= tl('api_unsubscribe_done',
                $data['GROUP_NAME']) ?></p><?php
        } else {
            if ($scope == "all") {
                ?><p><?= tl('api_unsubscribe_all_confirm',
                    $data['UNSUBSCRIBE_EMAIL']) ?></p><?php
            } else {
                ?><p><?= tl('api_unsubscribe_confirm',
                    $data['GROUP_NAME']) ?></p><?php
            }
            ?>
            <form method="post" action="<?= C\SHORT_BASE_URL ?>">
            <input type="hidden" name="c" value="api" >
            <input type="hidden" name="a" value="unsubscribe" >
            <input type="hidden" name="token"
                value="<?= $data['UNSUBSCRIBE_TOKEN'] ?>" >
            <input type="hidden" name="List-Unsubscribe"
                value="One-Click" >
            <button type="submit"><?=
                tl('api_unsubscribe_button') ?></button>
            </form><?php
        }
        ?>
        </div>
        <?php
    }
}
