<?php
/*
 * @component UserPoints
 * @copyright Copyright (C) 2015-2022 Bernard Gilly - Adrien Roussel - Martin Brampton
 * @license : GNU/GPL
 * @Website : https://remository.com
 */

namespace BlackSheepResearch\Component\UserPoints\Administrator\Controllers;

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Toolbar\ToolbarHelper;
use BlackSheepResearch\Component\UserPoints\Administrator\AdminController;
use BlackSheepResearch\Component\UserPoints\Site\Problems\PointsUser;
use BlackSheepResearch\Component\UserPoints\Site\Problems\PointsDetails;
use BlackSheepResearch\Component\UserPoints\Site\Problems\Medal;

class ResetControl extends AdminController {

    public function resetuser() {

        $cid = implode(',', Factory::getApplication()->input->get('cid', array(), 'array'));

        ToolbarHelper::title(Text::_('AUP_USERS'), 'user');
        echo "<b>" . Text::_('AUP_CONFIRM_RESET_USER') . "</b>";
        ?>
        <p>
        <form action="index.php?option=com_userpoints&task=resetUserConfirmed" method="post" name="adminForm" id="confirm-form" class="form-validate">
            <input type="hidden" name="cid" value="<?php echo $cid; ?>">
            <input type="submit" name="Submit" value="<?php echo Text::_('JYes'); ?>">
            <input type="Submit" name="Reset" value="<?php echo Text::_('JNO'); ?>" onClick="javascript: window.history.go(-1);">
        </form>
        </p>
        <?php
    }

    public function resetUserConfirmed() {
        $app = Factory::getApplication();

        $cid = array_map('\\intval', explode(',', $app->input->get('cid', '', 'string')));

        if (count($cid)) {

            foreach ($cid as $id) {
                $pointsuser = PointsUser::withTableID($id);
                $pointsuser->refreshAll();

                // main query
                PointsDetails::deleteByReferrerID($pointsuser->referreid);

                // main query
                Medal::deleteWithRid($id);
            }
        }

        $this->redirect('index.php?option=com_userpoints&task=statistics', Text::_('AUP_SUCCESSFUL_USER_RESET'));
    }

    public function resetprofilviews() {
        $cid = Factory::getApplication()->input->get('id', 0, 'int');
        $pointsuser = PointsUser::withTableID($cid);
        $pointsuser->profileviews = 0;
        $pointsuser->save();

        $this->redirect('index.php?option=com_userpoints&task=edituser&cid[]=' . $cid);
    }

    public function resetreferraluser() {
        $cid = Factory::getApplication()->input->get('id', 0, 'int');
        $pointsuser = PointsUser::withTableID($cid);
        $pointsuser->referraluser = '';
        $pointsuser->save();

        $this->redirect('index.php?option=com_userpoints&task=edituser&cid[]=' . $cid);
    }

    public function resetreferrees() {
        $cid = Factory::getApplication()->input->get('id', 0, 'int');
        $pointsuser = PointsUser::withTableID($cid);
        $pointsuser->referrees = 0;
        $pointsuser->save();

        $this->redirect('index.php?option=com_userpoints&task=edituser&cid[]=' . $cid);
    }

}
