<?php
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Http\HttpFactory;
/**
 * @version        56.3
 * @package        Joomla
 * @subpackage     Joom Donation
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2009 - 2023 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

class DonationController extends OSFController
{

	/**
	 * Display information
	 */
	public function display($cachable = false, array $urlparams = array())
	{
		global $loadStyle;
		$config   = DonationHelper::getConfig();
		$document = Factory::getDocument();

		DonationHelper::loadMedia();

		if (file_exists(JPATH_ROOT . '/media/com_jdonation/assets/css/custom.css') && filesize(JPATH_ROOT . '/media/com_jdonation/assets/css/custom.css') > 0)
		{
			$document->addStylesheet(Uri::base(true) . '/media/com_jdonation/assets/css/custom.css', 'text/css', null, null);
		}
		if ($config->load_twitter_bootstrap)
		{
			DonationHelper::loadBootstrap(false);
		}
		DonationHelper::loadJQuery();
		HTMLHelper::_('script', DonationHelper::getSiteUrl() . '/media/com_jdonation/assets/js/noconflict.js', false, false);

		//Clear the donation form data on donaton complete or cancel
		$viewName = $this->input->get('view', $this->defaultView, 'string');
		if ($viewName == 'complete' || $viewName == 'cancel')
		{
			$this->app->setUserState('com_jdonation.formdata', null);
			
		}

		parent::display($cachable, $urlparams);
	}

	/**
	 * Download donation receipt
	 *
	 */
	public function download_receipt()
	{
		$user = Factory::getUser();
		$f	 = $this->input->getInt('f', 0);
		if (!$user && $f == 0)
		{
			return;
		}
		$id  = $this->input->getInt('id');
		$row = Table::getInstance('Donor', 'DonationTable');
		$row->load($id);

		if (!Factory::getApplication()->isClient('administrator') && $row->user_id != $user->id && $f == 0)
		{
			return;
		}

		//Validation is OK, we can now process download the receipt
		DonationHelper::downloadInvoice($id);
	}

	/**
	 * Download a file uploaded by donor
	 */
	public function download_file()
	{

		$filePath = 'media/com_jdonation/files';
		$fileName = $this->input->get('file_name', '', 'none');
		if (file_exists(JPATH_ROOT . '/' . $filePath . '/' . $fileName))
		{
			while (@ob_end_clean()) ;
			DonationHelper::processDownload(JPATH_ROOT . '/' . $filePath . '/' . $fileName, $fileName);
			exit();
		}
		else
		{
			$this->app->enqueueMessage(Text::_('JD_FILE_NOT_EXIST'));
			$this->app->redirect('index.php?option=com_jdonation');
		}
	}

	/**
	 * Validate username which users entered on order form
	 *
	 */
	public function validate_username()
	{
		$db         = Factory::getDbo();
		$query      = $db->getQuery(true);
		$username   = $this->input->get('fieldValue', '', 'string');
		$validateId = $this->input->get('fieldId', '', 'string');
		$query->select('COUNT(*)')
			->from('#__users')
			->where('username="' . $username . '"');
		$db->setQuery($query);
		$total        = $db->loadResult();
		$arrayToJs    = array();
		$arrayToJs[0] = $validateId;
		if ($total)
		{
			$arrayToJs[1] = false;
		}
		else
		{
			$arrayToJs[1] = true;
		}
		echo json_encode($arrayToJs);
		Factory::getApplication()->close();
	}

	/**
	 * Validate email which users entered on order form to make sure it is valid
	 */
	public function validate_email()
	{
		$db         = Factory::getDbo();
		$query      = $db->getQuery(true);
		$email      = $this->input->get('fieldValue', '', 'string');
		$validateId = $this->input->get('fieldId', '', 'string');
		$query->select('COUNT(*)')
			->from('#__users')
			->where('email="' . $email . '"');
		$db->setQuery($query);
		$total        = $db->loadResult();
		$arrayToJs    = array();
		$arrayToJs[0] = $validateId;
		if (!$total)
		{
			$arrayToJs[1] = true;
		}
		else
		{
			$arrayToJs[1] = false;
		}
		echo json_encode($arrayToJs);
		Factory::getApplication()->close();
	}

	/**
	 * Get list of states for the selected country, using in AJAX request
	 */
	public function get_states()
	{
		$countryName = $this->input->get('country_name', '', 'string');
		$stateName   = $this->input->get('state_name', '', 'string');
		if (!$countryName)
		{
			$countryName = DonationHelper::getConfigValue('default_country');
		}
		$db    = Factory::getDbo();
		$query = $db->getQuery(true);
		$query->clear();
		$query->select('required')
			->from('#__jd_fields')
			->where('name=' . $db->quote('state'));
		$db->setQuery($query);
		$required = $db->loadResult();
		($required) ? $class = 'validate[required]' : $class = '';

		$query->clear();
		$query->select('country_id')
			->from('#__jd_countries')
			->where('name=' . $db->quote($countryName));
		$db->setQuery($query);
		$countryId = $db->loadResult();
		//get state
		$query->clear();
		$query->select('state_name AS value, state_name AS text')
			->from('#__jd_states')
			->where('country_id=' . (int) $countryId);;
		$db->setQuery($query);
		$states  = $db->loadObjectList();
		$options = array();
		if (count($states))
		{
			$options[] = HTMLHelper::_('select.option', '', Text::_('JD_SELECT_STATE'));
			$options   = array_merge($options, $states);
		}
		else
		{
			$options[] = HTMLHelper::_('select.option', 'N/A', Text::_('JD_NA'));
		}
		echo HTMLHelper::_('select.genericlist', $options, 'state', ' class="input-large form-select form-control' . $class . '" id="state" ', 'value', 'text',
			$stateName);

		Factory::getApplication()->close();
	}

	/**
	 * Redirect donor to donation form
	 */
	public function donation_form()
	{
		$config        = DonationHelper::getConfig();
		$campaignId    = $this->input->getInt('campaign_id', 0);
		$Itemid        = $this->input->getInt('Item_id');
		$amount        = $this->input->getFloat('amount', 0);
		$rdAmount      = $this->input->getFloat('rd_amount', 0);
		$paymentMethod = $this->input->getCmd('payment_method', '');

		$url = DonationHelperRoute::getDonationFormRoute($campaignId, $Itemid);
		if ($amount > 0)
		{
			$url .= '&amount=' . $amount;
		}

		if ($rdAmount > 0)
		{
			$url .= '&rd_amount=' . $rdAmount;
		}

		if ($paymentMethod)
		{
			$url .= '&payment_method=' . $paymentMethod;
		}

		$this->setRedirect(Route::_($url, false, (int) $config->use_https));
	}

	static function convertCurrency(){
		$jinput				= Factory::getApplication()->input;
		$cur_from			= $jinput->getString('cur_from');
		$cur_to				= $jinput->getString('cur_to');
		$http				= HttpFactory::getHttp();
        $url				= 'http://free.currencyconverterapi.com/api/v5/convert?q='.$cur_from.'_'.$cur_to.'&compact=y';
        $response			= $http->get($url);
		if ($response->code == 200)
        {
            $data = $response->body;
            $returnArr = json_decode($data);
            $converted = $returnArr->{$cur_from.'_'.$cur_to}->val;
        }
	}

    public function export()
    {
        if (!$this->app->isClient('administrator'))
        {
            //Check permission
            $user          = Factory::getUser();
            $receiveUserId = $this->input->getInt('filter_receive_user_id');
            if (!($user->authorise('core.admin', 'com_jdonation') || ($receiveUserId > 0 && $user->id == $receiveUserId)))
            {
                $app = Factory::getApplication();
                $app->enqueueMessage(Text::_('JD_YOUR_ARE_NOT_ALLOW_TO_EXPORT_DONORS'), 'error');
                $app->redirect('index.php');

                return false;
            }
        }

        require_once JPATH_ROOT . '/components/com_jdonation/helper/data.php';
        $config = DonationHelper::getConfig();
        $model  = $this->getModel('donors', array('remember_states' => true));
        $rows   = $model->limitstart(0)
            ->limit(0)
            ->filter_order('tbl.payment_date')
            ->filter_order_Dir('ASC')
            ->getData();
        if (count($rows))
        {
            $db    = Factory::getDbo();
            $query = $db->getQuery(true);
            $query->select('*')
                ->from('#__jd_fields')
                ->where('published=1')
                ->order('ordering');
            $db->setQuery($query);
            $rowFields   = $db->loadObjectList();
            $fieldValues = array();
            $donorIds    = array();
            if (count($rowFields))
            {
                foreach ($rows as $row)
                {
                    $donorIds[] = $row->id;
                }
                $query->clear();
                $query->select('donor_id, field_id, field_value')
                    ->from('#__jd_field_value')
                    ->where('donor_id IN (' . implode(',', $donorIds) . ')');
                $db->setQuery($query);
                $rowFieldValues = $db->loadObjectList();
                for ($i = 0, $n = count($rowFieldValues); $i < $n; $i++)
                {
                    $rowFieldValue                                                   = $rowFieldValues[$i];
                    $fieldValues[$rowFieldValue->donor_id][$rowFieldValue->field_id] = $rowFieldValue->field_value;
                }
            }
            DonationHelperData::csvExport($rows, $config, $rowFields, $fieldValues);
        }
        else
        {
            $this->app->enqueueMessage(Text::_('JD_THERE_ARE_NO_DONOR_RECORDS_TO_EXPORT'));
            $this->app->redirect('index.php?option=com_jdonation&view=donors');
        }
    }

    /***
     * Get search parameters from search module and performing redirect
     */
    public function search()
    {
        $search			= $this->input->getString('filter_search', '');
        $Itemid			= $this->input->getInt('Itemid', 0);
        $url = 'index.php?option=com_jdonation&view=search';
        if ($search)
        {
            $url        .= '&filter_search=' . $search;
        }
        $url            .= '&Itemid=' . $Itemid;
        $this->app->redirect(Route::_($url, false, 0));
    }

	public function populateUserData()
	{
		$input			= Factory::getApplication()->input;
		$userId			= $input->getInt('user_id', 0);
		$data			= array();
		if ($userId > 0)
		{
			$user		= Factory::getUser($userId);
			$name		= $user->name;
			$nameArr    = explode(" ", $name);
			if(count($nameArr) > 2)
			{
				$data['first_name']	= $nameArr[0];
				$last_name = "";
				for($i=1; $i< count($nameArr); $i++)
				{
					$last_name .= $nameArr[$i]. " ";
				}
				$last_name = substr($last_name, 0, strlen($last_name) - 1);
				$data['last_name']	= $last_name;
			}
			else
			{
				$data['first_name']	= $name;
				$data['last_name']	= "";
			}
			$email		= $user->email;
			
			$data['email']	= $user->email;
		}
		echo json_encode($data);
		Factory::getApplication()->close();
	}
}
