<?php
/**
 * @package    VP Advanced User
 * @author     Abhishek Das <info@virtueplanet.com>
 * @copyright  Copyright (C) 2012-2019 VirtuePlanet Services LLP. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @link       https://www.virtueplanet.com
 */

defined('_JEXEC') or die;

jimport('joomla.user.user');

class VPAUser extends JUser
{
	protected static $instances = array();

	protected static $hasProfilePicture = null;

	/**
	 * Constructor activating the default information of the language
	 *
	 * @param   integer             $identifier  The primary key of the user to load (optional).
	 * @param   JUserWrapperHelper  $userHelper  The JUserWrapperHelper for the static methods.
	 *
	 * @since   1.0
	 */
	public function __construct($identifier = 0, JUserWrapperHelper $userHelper = null)
	{
		parent::__construct($identifier, $userHelper);
		
		if($this->id > 0)
		{
			$this->loadAdvancedData();
		}
		else
		{
			$this->vpadvanceduserdata = array();
		}
	}
	
	/**
	 * Returns the global User object, only creating it if it doesn't already exist.
	 *
	 * @param   integer             $identifier  The primary key of the user to load (optional).
	 * @param   JUserWrapperHelper  $userHelper  The JUserWrapperHelper for the static methods.
	 *
	 * @return  JUser  The User object.
	 *
	 * @since   11.1
	 */
	public static function getInstance($identifier = 0, JUserWrapperHelper $userHelper = null)
	{
		if (null === $userHelper)
		{
			$userHelper = new JUserWrapperHelper;
		}

		// Find the user id
		if (!is_numeric($identifier))
		{
			if (!$id = $userHelper->getUserId($identifier))
			{
				// If the $identifier doesn't match with any id, just return an empty VPAUser.
				return new VPAUser;
			}
		}
		else
		{
			$id = $identifier;
		}

		// If the $id is zero, just return an empty VPAUser.
		// Note: don't cache this user because it'll have a new ID on save!
		if ($id === 0)
		{
			return new VPAUser;
		}

		// Check if the user ID is already cached.
		if (empty(self::$instances[$id]))
		{
			$user = new VPAUser($id, $userHelper);
			self::$instances[$id] = $user;
		}

		return self::$instances[$id];
	}
	
	public static function hasProfilePicture()
	{
		if(self::$hasProfilePicture === null)
		{
			$db    = JFactory::getDbo();
			$query = $db->getQuery(true);
			$query->select($db->quoteName('name'))
			      ->select($db->quoteName('state'))
			      ->from($db->quoteName('#__vpadvanceduser_fields'))
			      ->where($db->quoteName('name') . ' = ' . $db->quote('profile_picture'))
			      ->where($db->quoteName('type') . ' = ' . $db->quote('avatar'));

			$db->setQuery($query);
			$result = $db->loadObject();
			
			self::$hasProfilePicture = !empty($result) ? ($result->state == 1) :  false;
		}
		
		return self::$hasProfilePicture;
	}
	
	protected function loadAdvancedData()
	{
		// Get the dispatcher and load the users plugins.
		$dispatcher = JEventDispatcher::getInstance();
		JPluginHelper::importPlugin('user');
		
		// Trigger the data preparation event.
		$results = $dispatcher->trigger('onContentPrepareData', array('com_vpadvanceduser.user', $this));

		// Check for errors encountered while preparing the data.
		if (count($results) && in_array(false, $results, true))
		{
			$this->setError($dispatcher->getError());
			$this->vpadvanceduserdata = false;
		}
	}
}
