<?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;

use Joomla\Registry\Registry;
/**
 * VPAdvancedUser Html Helper
 *
 * @since  1.0
 */
abstract class JHtmlVPAUField
{
	public static function _($field)
	{
		if(self::hasMethod($field->id))
		{
			return JHtml::_('vpaufield.' . $field->id, $field->value, $field);
		}
		elseif(self::hasMethod($field->fieldname))
		{
			return JHtml::_('vpaufield.' . $field->fieldname, $field->value, $field);
		}
		elseif(self::hasMethod($field->type))
		{
			return JHtml::_('vpaufield.' . $field->type, $field->value, $field);
		}
		elseif(self::hasMethod('vp' . $field->type))
		{
			return JHtml::_('vpaufield.vp' . $field->type, $field->value, $field);
		}
		else
		{
			return JHtml::_('vpaufield.value', $field->value);
		}
	}

	public static function hasMethod($method)
	{
		if(!is_string($method))
		{
			return false;
		}
		
		return method_exists(__CLASS__, strtolower($method));
	}

	/**
	 * Returns html markup showing a date picker
	 *
	 * @param   string  $value  valid date string
	 *
	 * @return  mixed
	 */
	public static function calendar($value)
	{
		if (empty($value))
		{
			return JHtml::_('vpaufield.value', $value);
		}
		else
		{
			return JHtml::_('date', $value, null, null);
		}
	}

	/**
	 * Returns the date of birth formatted and calculated using server timezone.
	 *
	 * @param   string  $value  valid date string
	 *
	 * @return  mixed
	 */
	public static function dob($value)
	{
		if (!$value)
		{
			return '-';
		}

		return JHtml::_('date', $value, JText::_('DATE_FORMAT_LC1'), false);
	}

	/**
	 * Returns the date of birth formatted and calculated using server timezone.
	 *
	 * @param   string  $value  valid date string
	 *
	 * @return  mixed
	 */
	public static function vplist($value, $field)
	{
		if(!is_callable(array($field, 'getOptions'), false))
		{
			if(!method_exists($field, 'getOptions'))
			{
				return JHtml::_('vpaufield.value', $value);
			}
			
			$method = new ReflectionMethod($field, 'getOptions');
			$method->setAccessible(true);
			
			$options = $method->invoke($field);
		}
		else
		{
			$options = $field->getOptions();
		}

		if (!is_array($options))
		{
			return '';
		}
		
		$value = (int) $value;
		$result = '';
		
		foreach($options as $option)
		{
			if($option->value == $value)
			{
				$result = $option->text;
				break;
			}
		}

		return $result;
	}
	
	public static function google_plus($value)
	{
		return self::social_page_links('https://plus.google.com/', 'google_plus', $value);
	}
	
	public static function facebook($value)
	{
		return self::social_page_links('https://www.facebook.com/', 'facebook', $value);
	}
	
	public static function twitter($value)
	{
		return self::social_page_links('https://twitter.com/', 'twitter', $value);
	}

	private static function social_page_links($url_prefix, $social_media, $value)
	{
		if (empty($value) || trim($value) == '')
		{
			return '-';
		}
		
		$value = str_replace(' ', '', $value);
		
		// Convert website url to utf8 for display
		$value = JStringPunycode::urlToUTF8(htmlspecialchars($value));
		
		if (substr($value, 0, 4) == "http")
		{
			$link = $value;
		}
		else
		{
			$link = $url_prefix . $value;
		}

		return '<a href="' . $link . '" target="_blank">' . JText::_('COM_VPADVANCEDUSER_' . strtoupper($social_media)) . '</a>';
	}

	/**
	 * Returns a anchor tag generated from a given value
	 *
	 * @param   string  $value  url to use
	 *
	 * @return mixed|string
	 */
	public static function url($value)
	{
		if (empty($value))
		{
			return JHtml::_('vpaufield.value', $value);
		}
		else
		{
			// Convert website url to utf8 for display
			$value = JStringPunycode::urlToUTF8(htmlspecialchars($value));

			if (substr($value, 0, 4) == "http")
			{
				return '<a href="' . $value . '">' . $value . '</a>';
			}
			else
			{
				return '<a href="http://' . $value . '">' . $value . '</a>';
			}
		}
	}

	/**
	 * Return the translated strings yes or no depending on the value
	 *
	 * @param   boolean  $value  input value
	 *
	 * @return string
	 */
	public static function checkbox($value)
	{
		if ($value)
		{
			return JText::_('JYES');
		}
		else
		{
			return JText::_('JNO');
		}
	}
	
	/**
	 * Return the translated strings yes or no depending on the value
	 *
	 * @param   boolean  $value  input value
	 *
	 * @return string
	 */
	public static function tos($value)
	{
		if ($value)
		{
			return JText::_('JYES');
		}
		else
		{
			return JText::_('JNO');
		}
	}
	
	/**
	 * Return the translated strings yes or no depending on the value
	 *
	 * @param   boolean  $value  input value
	 *
	 * @return string
	 */
	public static function privacy($value)
	{
		if ($value)
		{
			return JText::_('JYES');
		}
		else
		{
			return JText::_('JNO');
		}
	}

	/**
	 * Get the sanitized value
	 *
	 * @param   mixed  $value  Value of the field
	 *
	 * @return  mixed  String/void
	 *
	 * @since   1.6
	 */
	public static function value($value)
	{
		if (is_string($value))
		{
			$value = trim($value);
		}

		if (empty($value))
		{
			return '-';
		}

		elseif (!is_array($value))
		{
			return htmlspecialchars($value);
		}
	}

	public static function avatar($value, $field = null, $attributes = '')
	{
		$value   = (string) $value;
		$html    = '';
		
		if(is_string($field))
		{
			$attributes = $field;
		}

		// Register avatar helper
		JLoader::register('VPAdvancedUserHelperAvatar', JPATH_SITE . '/components/com_vpadvanceduser/helpers/avatar.php');
		
		$avatarHelper = VPAdvancedUserHelperAvatar::getInstance();
		
		$url = $avatarHelper->getUrl($value);
		
		if(!empty($url))
		{
			$attributes = !empty($attributes) ? ' ' . trim($attributes) : '';
			
			$html = '<img src="' . htmlspecialchars($url) . '"' . $attributes . ' />';
		}
		
		return $html;
	}
	
	/**
	 * Get the space symbol
	 *
	 * @param   mixed  $value  Value of the field
	 *
	 * @return  string
	 *
	 * @since   1.6
	 */
	public static function spacer($value)
	{
		return '';
	}

	/**
	 * Get the sanitized helpsite link
	 *
	 * @param   mixed  $value  Value of the field
	 *
	 * @return  mixed  String/void
	 *
	 * @since   1.6
	 */
	public static function helpsite($value)
	{
		if (empty($value))
		{
			return static::value($value);
		}
		else
		{
			$pathToXml = JPATH_ADMINISTRATOR . '/help/helpsites.xml';

			$text = $value;

			if (!empty($pathToXml) && $xml = simplexml_load_file($pathToXml))
			{
				foreach ($xml->sites->site as $site)
				{
					if ((string) $site->attributes()->url == $value)
					{
						$text = (string) $site;
						break;
					}
				}
			}

			$value = htmlspecialchars($value);

			if (substr($value, 0, 4) == "http")
			{
				return '<a href="' . $value . '">' . $text . '</a>';
			}
			else
			{
				return '<a href="http://' . $value . '">' . $text . '</a>';
			}
		}
	}

	/**
	 * Get the sanitized template style
	 *
	 * @param   mixed  $value  Value of the field
	 *
	 * @return  mixed  String/void
	 *
	 * @since   1.6
	 */
	public static function templatestyle($value)
	{
		if (empty($value))
		{
			return static::value($value);
		}
		else
		{
			$db = JFactory::getDbo();
			$query = $db->getQuery(true)
				->select('title')
				->from('#__template_styles')
				->where('id = ' . $db->quote($value));
			$db->setQuery($query);
			$title = $db->loadResult();

			if ($title)
			{
				return htmlspecialchars($title);
			}
			else
			{
				return static::value('');
			}
		}
	}

	/**
	 * Get the sanitized language
	 *
	 * @param   mixed  $value  Value of the field
	 *
	 * @return  mixed  String/void
	 *
	 * @since   1.6
	 */
	public static function admin_language($value)
	{
		if (empty($value))
		{
			return static::value($value);
		}
		else
		{
			$path = JLanguage::getLanguagePath(JPATH_ADMINISTRATOR, $value);
			$file = "$value.xml";

			$result = null;

			if (is_file("$path/$file"))
			{
				$result = JLanguage::parseXMLLanguageFile("$path/$file");
			}

			if ($result)
			{
				return htmlspecialchars($result['name']);
			}
			else
			{
				return static::value('');
			}
		}
	}

	/**
	 * Get the sanitized language
	 *
	 * @param   mixed  $value  Value of the field
	 *
	 * @return  mixed  String/void
	 *
	 * @since   1.6
	 */
	public static function language($value)
	{
		if (empty($value))
		{
			return static::value($value);
		}
		else
		{
			$path = JLanguage::getLanguagePath(JPATH_SITE, $value);
			$file = "$value.xml";

			$result = null;

			if (is_file("$path/$file"))
			{
				$result = JLanguage::parseXMLLanguageFile("$path/$file");
			}

			if ($result)
			{
				return htmlspecialchars($result['name']);
			}
			else
			{
				return static::value('');
			}
		}
	}

	/**
	 * Get the sanitized editor name
	 *
	 * @param   mixed  $value  Value of the field
	 *
	 * @return  mixed  String/void
	 *
	 * @since   1.6
	 */
	public static function editor($value)
	{
		if (empty($value))
		{
			return static::value($value);
		}
		else
		{
			$db = JFactory::getDbo();
			$lang = JFactory::getLanguage();
			$query = $db->getQuery(true)
				->select('name')
				->from('#__extensions')
				->where('element = ' . $db->quote($value))
				->where('folder = ' . $db->quote('editors'));
			$db->setQuery($query);
			$title = $db->loadResult();

			if ($title)
			{
				$lang->load("plg_editors_$value.sys", JPATH_ADMINISTRATOR, null, false, true)
					|| $lang->load("plg_editors_$value.sys", JPATH_PLUGINS . '/editors/' . $value, null, false, true);
				$lang->load($title . '.sys');

				return JText::_($title);
			}
			else
			{
				return static::value('');
			}
		}
	}
}
