<?php
/**
 * @package     SP Author Archive
 *
 * @copyright   Copyright (C) 2010 - 2017 JoomShaper. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('JPATH_PLATFORM') or die;

class JFormFieldSbfile extends JFormField{

	protected $type = 'Sbfile';
	protected $accept;

	public function __get($name) {
		switch ($name) {
			case 'accept':
			return $this->$name;
		}
		return parent::__get($name);
	}

	public function __set($name, $value) {
			switch ($name) {
			case 'accept':
				$this->$accept = (string) $value;
			break;

			default:
			parent::__set($name, $value);
		}
	}
	
	public function setup(SimpleXMLElement $element, $value, $group = null)
	{
		$return = parent::setup($element, $value, $group);

		if ($return)
		{
			$this->accept = (string) $this->element['accept'];
		}

		return $return;
	}
	protected function getInput(){
		// Initialize some field attributes.
		$accept    = !empty($this->accept) ? ' accept="' . $this->accept . '"' : '';
		$size      = !empty($this->size) ? ' size="' . $this->size . '"' : '';
		$class     = !empty($this->class) ? ' class="' . $this->class . '"' : '';
		$disabled  = $this->disabled ? ' disabled' : '';
		$required  = $this->required ? ' required aria-required="true"' : '';
		$autofocus = $this->autofocus ? ' autofocus' : '';
		$multiple  = $this->multiple ? ' multiple' : '';

		// Initialize JavaScript field attributes.
		$onchange = $this->onchange ? ' onchange="' . $this->onchange . '"' : '';

		// Including fallback code for HTML5 non supported browsers.
		JHtml::_('jquery.framework');
		JHtml::_('script', 'system/html5fallback.js', false, true);
		$app = JFactory::getApplication();

		$file_preview = '';
		// Get the input.
		$input = JFactory::getApplication()->input;
		$Itemid = $input->get('id',0,'INT');

		if ($app->isAdmin()) {
			// avatar
			$avatar='';
			$jinput = JFactory::getApplication()->input;
			
			if ( $jinput->get('id') !='' && self::getUserProfileById($jinput->get('id')) !='' && self::getUserProfileById($jinput->get('id')) ) {
				$avatar = json_decode(self::getUserProfileById($jinput->get('id'))->profile_value)->avatar;
			}
			

			if ( isset($avatar) && $avatar ) {
				$file_preview = '<div class="authorarchive-profile-image-preview">';
				$file_preview .= '<h4>user profile picture</h4>';
				$file_preview .= '<img src="'.JURI::root(true). $avatar . '" width="150" />';
				$file_preview .= '</div>';
			}

	
			// has attachment
			if ($this->value && $this->fieldname =='attachment') {
				$file_preview  = '<div class="spauthorarchive-attached-file">';
				$file_preview .= '<a id="spauthorarchive-attachment-file" href="' . JURI::root(true). '/'. $this->value . '">' . $this->value . '</a> ';
				if (isset($this->value) && $this->value) {
					$file_preview .= ' <a id="spauthorarchive-remove-attachment" class="spauthorarchive-remove-attachment" href="#" data-file="' . JPATH_ROOT . '/' . $this->value . '" data-id="'. $Itemid .'">('. JText::_('COM_SPAUTHORARCHIVE_REMOVE_ATTACHMENT') . ')</a>';
				}

				$file_preview .= '</div>';
			}
		}

		//$jinput = JFactory::getApplication()->input;
		if ($app->isSite()) {
			$jinput = JFactory::getApplication()->input;
			$user = JFactory::getUser();

			if (self::getUserProfileById($user->get('id')) !='' && self::getUserProfileById($user->get('id'))) {
				$avatar = json_decode(self::getUserProfileById($user->get('id'))->profile_value)->avatar;
			}

			if ( isset($avatar) && $avatar ) {
				$file_preview = '<div class="authorarchive-profile-image-preview">';
				$file_preview .= '<h4>Your profile picture</h4>';
				$file_preview .= '<img src="'.JURI::root(true). $avatar . '" width="150" />';
				$file_preview .= '</div>';
			}
		}

		return '<input type="file" name="' . $this->name . '" id="' . $this->id . '" ' . $accept
			. $disabled . $class . $size . $onchange . $required . $autofocus . $multiple . ' />' . $file_preview;
	}

	protected static function getUserProfileById( $user_id = NULL ){
		// Get a database object.
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);
		$sql = "SELECT * FROM #__user_profiles WHERE user_id = $user_id";
		$db->setQuery($sql);
		return $db->loadObject();
	}

}
