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

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

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use BlackSheepResearch\Component\UserPoints\Administrator\Model;
use BlackSheepResearch\Component\UserPoints\Site\Problems\TemplateInvitation;

class Templateinvite extends Model {

    function __construct() {
        parent::__construct();
    }

    function _load_templateinvite() {
        $app = Factory::getApplication();
        // Get the pagination request variables
        list($limitstart, $limit) = TemplateInvitation::getLimitsFromRequest();
        $total = TemplateInvitation::countAll();
        $result = TemplateInvitation::getRange($limitstart, $limit);
        return array($result, $total, $limit, $limitstart);
    }

    function _edit_templateinvite() {
        $cid = Factory::getApplication()->input->get('cid', array(0), 'array');
        if (!is_array($cid)) {
            $cid = array(0);
        }
        return new TemplateInvitation($cid[0]);
    }

    function _delete_templateinvite() {
        $cid = array_map('\\intval', Factory::getApplication()->input->get('cid', array(), 'array'));
        TemplateInvitation::deleteMany($cid);
        $this->redirect('index.php?option=com_userpoints&task=templateinvite');
    }

    function _save_templateinvite($apply = 0) {
        $app = Factory::getApplication();
        $post = $app->input->getArray(array());
        $post['emailbody'] = $app->input->get('emailbody', '', 'raw');

        (new TemplateInvitation())->save($post);

        $msg = Text::_('AUP_TEMPLATESAVED');
        $id = $app->input->getInt('id');
        if (!$apply OR!$id) {
            $this->redirect('index.php?option=com_userpoints&task=templateinvite', $msg);
        } else {
            $this->redirect('index.php?option=com_userpoints&task=edittemplateinvite&cid[]=' . $id, $msg);
        }
    }

}
