Your IP : 216.73.217.142


Current Path : /var/www/consult-e-syn/public_html/plugins/ats/akeebasubs/
Upload File :
Current File : /var/www/consult-e-syn/public_html/plugins/ats/akeebasubs/akeebasubs.php

<?php
/**
 * @package   ats
 * @copyright Copyright (c)2011-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
 * @license   GNU General Public License version 3, or later
 */

defined('_JEXEC') or die();

use Akeeba\Subscriptions\Site\Model\JoomlaUsers;
use FOF40\Container\Container;
use FOF40\Date\Date;
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\User\User;

/**
 * Akeeba Subscriptions integration for Akeeba Ticket System
 *
 * Tip: You can fork this plugin to create integrations with other subscription components / services.
 */
class plgAtsAkeebasubs extends CMSPlugin
{
	/** @var array Active and inactive subscriptions per user ID */
	private static $subsPerUser = [];

	/** @var \Joomla\Database\DatabaseDriver|JDatabaseDriver */
	public $db;

	/** @var null|bool Is Akeeba Subscriptions installed on this site? */
	private $hasSubscriptionsComponent = null;

	public function __construct(&$subject, $config = [])
	{
		parent::__construct($subject, $config);

		if (!defined('FOF40_INCLUDED') && !@include_once(JPATH_LIBRARIES . '/fof40/include.php'))
		{
			throw new RuntimeException('This extension requires FOF 4.', 500);
		}
	}

	/**
	 * Is the Akeeba Subscriptions component installed?
	 *
	 * @return  bool  True if Akeeba Subscriptions is installed
	 */
	public function onAtsHasSubscriptionsComponent()
	{
		if (is_null($this->hasSubscriptionsComponent))
		{
			$this->hasSubscriptionsComponent = Folder::exists(JPATH_ADMINISTRATOR . '/components/com_akeebasubs');

			if ($this->hasSubscriptionsComponent && !defined('AKEEBASUBS_VERSION'))
			{
				include_once JPATH_ADMINISTRATOR . '/components/com_akeebasubs/version.php';
			}

			if ($this->hasSubscriptionsComponent)
			{
				$this->hasSubscriptionsComponent = Folder::exists(JPATH_ADMINISTRATOR . '/components/com_akeebasubs/View');
			}
		}

		return $this->hasSubscriptionsComponent;
	}

	/**
	 * Get a list of the subscription levels (IDs and names) for a particular user account.
	 *
	 * @param   JUser  $user  The user to check
	 *
	 * @return  object  An object with arrays for active and inactive subscriptions
	 */
	public function onAtsGetSubscriptionsList(User $user = null)
	{
		// Sanity checks: please do not remove the following three if-blocks if you fork this plugin.
		$blankReturn = (object) [
			'active'   => [],
			'inactive' => [],
		];

		if ($user->guest)
		{
			return $blankReturn;
		}

		if ($user->username == 'system')
		{
			return $blankReturn;
		}

		if (!$this->onAtsHasSubscriptionsComponent())
		{
			return $blankReturn;
		}

		if (!array_key_exists($user->id, self::$subsPerUser) && ($user->id <= 0))
		{
			self::$subsPerUser[$user->id] = $blankReturn;
		}

		// Inside this if-block we fetch the actual data from the subscriptions component
		if (!array_key_exists($user->id, self::$subsPerUser))
		{
			$db    = $this->db;
			$query = $db->getQuery(true)
				->select([
					$db->qn('l.title'),
					$db->qn('s.state'),
					$db->qn('s.publish_up'),
					$db->qn('s.publish_down'),
				])->from(
					$db->qn('#__akeebasubs_subscriptions', 's')
				)->leftJoin(
					$db->qn('#__akeebasubs_levels', 'l') . ' ON(' .
					$db->qn('l.akeebasubs_level_id') . ' = ' . $db->qn('s.akeebasubs_level_id')
					. ')'
				)->where(
					$db->qn('s.user_id') . ' = ' . (int) $user->id
				);
			try
			{
				$rawSubs = $db->setQuery($query)->loadObjectList() ?? [];
			}
			catch (Exception $e)
			{
				$rawSubs = [];
			}

			if (empty($rawSubs))
			{
				self::$subsPerUser[$user->id] = (object) $blankReturn;

				return self::$subsPerUser[$user->id];
			}

			self::$subsPerUser[$user->id] = [
				'active'   => [],
				'inactive' => [],
			];

			$jNow = new Date();

			foreach ($rawSubs as $sub)
			{
				$key = ($sub->state === 'C') ? 'active' : 'inactive';

				if ($key === 'active')
				{
					$jStart = new Date($sub->publish_up);

					if ($jStart->getTimestamp() > $jNow->getTimestamp())
					{
						$key = 'inactive';
					}
				}

				if ($key === 'active')
				{
					$jEnd = new Date($sub->publish_down);

					if ($jEnd->getTimestamp() < $jNow->getTimestamp())
					{
						$key = 'inactive';
					}
				}

				self::$subsPerUser[$user->id][$key][] = $sub->title;
			}

			self::$subsPerUser[$user->id]['active']   = array_unique(self::$subsPerUser[$user->id]['active']);
			self::$subsPerUser[$user->id]['inactive'] = array_unique(self::$subsPerUser[$user->id]['inactive']);

			self::$subsPerUser[$user->id]['inactive'] = array_diff(self::$subsPerUser[$user->id]['inactive'], self::$subsPerUser[$user->id]['active']);

			self::$subsPerUser[$user->id] = (object) self::$subsPerUser[$user->id];
		}

		// Return the subscriptions list for this user from the cache
		return self::$subsPerUser[$user->id];
	}

	/**
	 * Returns the country of the user from the data stored in Akeeba Subscriptions
	 *
	 * @param   int   $userid     Joomla user id
	 * @param   bool  $isManager  Is the current user a manager?
	 *
	 * @return  array|null  An array with the country code and name, or null if we can't display it
	 */
	public function onAtsGetSubscriptionCountry($userid, $isManager)
	{
		static $cache = [];

		if (!$this->onAtsHasSubscriptionsComponent())
		{
			return null;
		}

		// I don't want to display the flag
		if ($this->params->get('displayCountry', 2) == 0)
		{
			return null;
		}
		// Display it to managers only
		elseif ($this->params->get('displayCountry', 2) == 2 && !$isManager)
		{
			return null;
		}

		if (!isset($cache[$userid]))
		{
			if ($this->isComAkeebasubsFOF3() === false)
			{
				$container = Container::getInstance('com_akeebasubs');
			}
			else
			{
				$container = \FOF30\Container\Container::getInstance('com_akeebasubs');
			}

			if (class_exists('\\Akeeba\\Subscriptions\\Site\\Model\\Users'))
			{
				// Akeeba Subscriptions 5 & 6
				$countryCode = $this->getCountryCodeAS5($userid, $container);
			}
			else
			{
				// Akeeba Subscriptions 7
				$countryCode = $this->getCountryCodeAS7($userid, $container);
			}

			$cache[$userid] = null;

			if (!is_null($countryCode))
			{
				$cache[$userid] = [
					$countryCode,
					\Akeeba\Subscriptions\Admin\Helper\Select::decodeCountry($countryCode),
				];
			}

		}

		// The user is not registered inside Akeeba Subscriptions
		if (!$cache[$userid])
		{
			return null;
		}

		return $cache[$userid];
	}

	/**
	 * Is Akeeba Subscriptions still using FOF3?
	 *
	 * @return  bool|null  NULL if it's not installed, TRUE for FOF3, FALSE otherwise
	 */
	function isComAkeebasubsFOF3(): ?bool
	{
		$path = JPATH_ADMINISTRATOR . '/components/com_akeebasubs/Model/Subscriptions.php';

		if (!@file_exists($path))
		{
			return null;
		}

		$contents = @file_get_contents($path);

		if ($contents === false)
		{
			return null;
		}

		return (strpos($contents, 'FOF30\Model\DataModel') !== false);
	}

	/**
	 * Get the country code for a user, based on information from Akeeba Subscriptions 5 or 6.
	 *
	 * @param   int                                   $userid
	 * @param   Container|\FOF30\Container\Container  $container
	 *
	 * @return  string|null
	 */
	private function getCountryCodeAS5($userid, $container)
	{
		$kuser = $container->factory->model('Users')->tmpInstance();

		$kuser->find(['user_id' => $userid]);

		$countryCode = $kuser->country;

		return $countryCode;
	}

	/**
	 * Get the country code for a user, based on information from Akeeba Subscriptions 7 or 8.
	 *
	 * @param   Container|\FOF30\Container\Container  $container
	 *
	 * @return  string|null
	 */
	private function getCountryCodeAS7($userid, $container)
	{
		/** @var Akeeba\Subscriptions\Site\Model\JoomlaUsers $juser */
		$juser = $container->factory->model('JoomlaUsers')->tmpInstance();
		$juser->find(['id' => $userid]);

		$canGetCountry = !is_null($juser) && is_object($juser) &&
			method_exists($juser, 'getProfileField');

		$countryCode = $canGetCountry ? $juser->getProfileField('akeebasubs.country') : null;

		return $countryCode;
	}

}