<?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\Site\Viewers;

// no direct access
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\CMS\Pagination\Pagination;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Document\Feed\FeedItem;
use BlackSheepResearch\Component\UserPoints\Administrator\Viewers\UPJPane;

class LatestactivityHTML extends UserView {
    /* function _display($tpl = null) {

      $app	=  Factory::getApplication();
      $doc	=  Factory::getDocument();
      $doc->addStyleSheet(Uri::base(true).'/components/com_userpoints/assets/css/userpoints.css');

      $app_params = $app->getParams();
      $this->params = $app_params->toArray();

      $this->pagination = new Pagination( $this->total, $this->limitstart, $this->limit );

      // insert the page counter in the title of the window page
      $titlesuite =  ( $this->limitstart ) ? ' - ' . $this->pagination->getPagesCounter() : '';
      $doc->setTitle( $doc->getTitle() . $titlesuite );

      parent::display($tpl);
      } */

    public function display($tpl = null) {

        $app = Factory::getApplication();
        $doc = Factory::getDocument();
        // $doc->addStyleSheet(Uri::base(true) . '/components/com_userpoints/assets/css/userpoints.css');

        $com_params = ComponentHelper::getParams('com_userpoints');
        if (!$com_params->get('allow_user_lists')) {
            Factory::getApplication()->setHeader('Status', '403', true);
            return;
        }
        $this->useAvatarFrom = $com_params->get('useAvatarFrom','');
        $this->linkToProfile = $com_params->get('linkToProfile','');
        $this->allowGuestUserViewProfil = $com_params->get('allowGuestUserViewProfil', 1);

        $model = $this->getModel('latestactivity');
        //$view       = $this->getView  ( 'latestactivity','html' );

        $menus = $app->getMenu();
        $menu = $menus->getActive();
        $menuid = $menu->id;
        $this->params = $menus->getParams($menuid);

        $_latestactivity = $model->getLatestActivity(); // $this->get('LatestActivity');
        $this->latestactivity = $_latestactivity[0];
        $this->total = $_latestactivity[1];
        $this->limit = $_latestactivity[2];
        $this->limitstart = $_latestactivity[3];

        $this->pagination = new Pagination($this->total, $this->limitstart, $this->limit);

        // insert the page counter in the title of the window page
        $titlesuite = ( $this->limitstart ) ? ' - ' . $this->pagination->getPagesCounter() : '';
        $doc->setTitle($doc->getTitle() . $titlesuite);

        parent::display($tpl);
    }

    public function displayRSS() {
        $doc = Factory::getDocument();
        $app = Factory::getApplication();
        $menus = $app->getMenu();
        $menu = $menus->getActive();
        $menuid = $menu->id;
        $params = $menus->getParams($menuid);

        $app->input->set('limit', $params->get('count', '20'));

        $siteEmail = $app->get('mailfrom');
        $fromName = $app->get('fromname');
        $feedEmail = $app->get('feed_email', 'author');
        $doc->editor = $fromName;

        if ($feedEmail != "none") {
            $doc->editorEmail = $siteEmail;
        }
        // Get some data from the model
        $act = $this->get('LatestActivity');
        foreach ($act[0] as $item) {
            $date = ($item->insert_date ? date('r', strtotime($item->insert_date)) : '');
            // Load individual item creator class
            $feeditem = new FeedItem;
            $feeditem->title = $item->usrname;
            $feeditem->link = '';
            $feeditem->description = '<![CDATA[' . Text::_($item->rule_name) . ' / ' . $item->last_points . ']]>';
            $feeditem->date = $date;

            $item->authorEmail = $siteEmail;

            // Loads item info into RSS array
            $doc->addItem($feeditem);
        }
    }

}
