<?php
use Joomla\CMS\Factory;
/**
 * @version         1.9.7
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Tuan Pham Ngoc
 * @copyright		Copyright (C) 2011 - 2024 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

// No direct access
defined('_JEXEC') or die();
/**
 * View class for a list of EDocman.
 */
class EDocmanViewSummaryHtml extends OSViewHtml
{
	/**
	 * Preare data for the view before displaying
	 */
    function display($tpl = null)
	{
		$db         = Factory::getDbo();
		$config     = EDocmanHelper::getConfig();
		$query      = $db->getQuery(true);
		$user       = Factory::getUser();
		$userId     = $user->id;
		$documents  = [];

		$session    = Factory::getSession();
        $massDownloadIds = $session->get('massDownloadIds', '');
        if($massDownloadIds != "")
        {
            $massDownloadIds = explode(",", $massDownloadIds);
            if(count($massDownloadIds))
            {
                for($i = 0;$i<count($massDownloadIds);$i++)
                {
                    $documentId                 = $massDownloadIds[$i];
                    $query->select('*')->from('#__edocman_documents')->where('id = '.$documentId);
                    $db->setQuery($query);
                    $item                       = $db->loadObject();
                    $query->clear();
                    $canEdit					= $user->authorise('core.edit',		'com_edocman.document.'.$documentId);
                    $canDownload= $user->authorise('edocman.download', 'com_edocman.document.'.$documentId) ;
                    $canDownload				= ($user->authorise('core.admin',       'com_config') || ($item->created_user_id == $userId && $item->created_user_id > 0)) || (($item->user_ids =="" || $item->user_ids == 0) && ($canDownload || $canEdit) || ($item->user_ids != '' && $item->user_ids != '0' && in_array($userId, explode(',', $item->user_ids)))) ;
                    if(!$canDownload)
                    {
                        unset($massDownloadIds[$i]);
                    }
                }
                if(count($massDownloadIds))
                {
                    $query->select('*')->from('#__edocman_documents')->where('id in (' . implode(",", $massDownloadIds) . ')');
                    $db->setQuery($query);
                    $documents = $db->loadObjectList();
                }
            }
        }
		$this->documents	   = $documents;
		$this->itemid		   = $this->input->getInt('Itemid',0);
        $this->config          = $config;
        $this->bootstrapHelper = new EDocmanHelperBootstrap($config->twitter_bootstrap_version);
        parent::display($tpl);
	}
}
