<?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.form.formfield');

JHtml::addIncludePath(JPATH_SITE . '/components/com_vpadvanceduser/helpers/html');

/**
 * Supports to upload avatar image
 *
 * @since  1.0
 */
class JFormFieldAvatar extends JFormField
{
	/**
	 * The form field type.
	 *
	 * @var        string
	 * @since    1.6
	 */
	protected $type = 'avatar';

	/**
	 * Method to get the field input markup.
	 *
	 * @return    string    The field input markup.
	 *
	 * @since    1.6
	 */
	protected function getInput()
	{
		JHtml::_('jquery.framework');
		JHtml::_('script', 'com_vpadvanceduser/avatar.min.js', false, true);
		
		$app       = JFactory::getApplication();
		$btn_class = $app->isAdmin() ? 'btn btn-small' : 'vp-btn';
		$isNew     = $this->form->getValue('id', 0) == 0;

		$html = array();
		
		$html[] = '<div class="vpau-avatar-container" data-vpau-base="' . htmlspecialchars(JUri::base(true) . '/') . '" data-vpau-isnew="' . (int) $isNew . '">';
		
		$html[] = '<div class="vpau-avatar-image">';
		$html[] = JHtml::_('vpaufield.avatar', $this->value, 'alt="avatar" class="img-vpau" data-vpau-image="avatar"');
		$html[] = '</div>';
		
		$html[] = '<div data-vpau-error="avatar" class="text-error vpau-avatar-error"></div>';
		
		$html[] = '<label for="' . $this->id . '_file" class="vpau-avatar-label ' . $btn_class . '">';
		$html[] = '<input type="file" name="vpaufiles[' . $this->fieldname . ']" id="' . $this->id . '_file" data-vpau-file="avatar" />';
		$html[] = '<span class="vpau-text-upload">' . JText::_('COM_VPADVANCEDUSER_UPLOAD_AVATAR') . '</span>';
		$html[] = '<span class="vpau-text-loading">' . JText::_('COM_VPADVANCEDUSER_LOADING_AVATAR') . '</span>';
		$html[] = '<div class="vpau-avatar-overlay"></div>';
		$html[] = '</label>';
		$html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '" data-vpau-value="avatar" />';
		
		$html[] = '<span class="vpau-filename" data-vpau-filename="avatar"></span>';
		
		$html[] = '</div>';
		
		return implode($html);
	}
}
