<?php
/**
* @package com_profilespaarchive
* @author JoomShaper http://www.joomshaper.com
* @copyright Copyright (c) 2010 - 2017 JoomShaper
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
*/

// No Direct Access
defined ('_JEXEC') or die('Resticted Aceess');

class plgUserProfilespaarchive extends JPlugin{
	protected static $avatar = array();
	protected static $last_avatar = '';
	function onContentPrepareData($context, $data){
		// Check we are manipulating a valid form.
		if (!in_array($context, array('com_users.profile','com_users.registration','com_users.user','com_admin.profile'))){
			return true;
		}
 
		$userId = isset($data->id) ? $data->id : 0;
 
		// Load the profile data from the database.
		$db = JFactory::getDbo();
		$db->setQuery(
			'SELECT profile_key, profile_value FROM #__user_profiles' .
			' WHERE user_id = '.(int) $userId .
			' AND profile_key LIKE \'profilespaarchive.%\'' .
			' ORDER BY ordering'
		);

		$results = $db->loadRowList();
 
		// Check for a database error.
		if ($db->getErrorNum()) {
			$this->_subject->setError($db->getErrorMsg());
			return false;
		}

		// Merge the profile data.
		if (isset($results) && !empty($results)) {
			$data->profilespaarchive = array();
			foreach ($results as $v) {
				$k = str_replace('profilespaarchive.', '', $v[0]);
				$data->profilespaarchive[$k] = json_decode($v[1], true);
			}
		}

		return true;
	}
 
	/**
	 * @param	JForm	The form to be altered.
	 * @param	array	The associated data for the form.
	 * @return	boolean
	 * @since	1.6
	 */
	function onContentPrepareForm($form, $data){


		$doc = \JFactory::getDocument();
		$plg_path = \JURI::root(true).'/plugins/user/profilespaarchive';
		$doc->addStyleSheet($plg_path.'/assets/css/style.css');

		// Load user_profile plugin language
		$lang = JFactory::getLanguage();
		$lang->load('plg_user_profilespaarchive', JPATH_ADMINISTRATOR);
 
		if (!($form instanceof JForm)) {
			$this->_subject->setError('JERROR_NOT_A_FORM');
			return false;
		}
		// Check we are manipulating a valid form.
		if (!in_array($form->getName(), array('com_users.profile', 'com_users.registration','com_users.user','com_admin.profile'))) {
			return true;
		}

		if ($form->getName()=='com_users.profile')
		{
			// Add the profile fields to the form.
			JForm::addFormPath(dirname(__FILE__).'/profiles');
			$form->loadFile('profile', false);
 
			// Toggle whether the designation field is required.
			if ($this->params->get('profile-require_designation', 1) > 0) {
				$form->setFieldAttribute('designation', 'required', $this->params->get('profile-require_designation') == 2, 'profilespaarchive');
			} else {
				$form->removeField('designation', 'profilespaarchive');
			}

			// Toggle whether the description field is required.
			if ($this->params->get('profile-require_description', 1) > 0) {
				$form->setFieldAttribute('description', 'required', $this->params->get('profile-require_description') == 2, 'profilespaarchive');
			} else {
				$form->removeField('description', 'profilespaarchive');
			}

			// Toggle whether the socials field is required.
			if ($this->params->get('profile-require_socials', 1) > 0) {
				$form->setFieldAttribute('socials', 'required', $this->params->get('profile-require_socials') == 2, 'profilespaarchive');
			} else {
				$form->removeField('socials', 'profilespaarchive');
			}
		}
 
		//In this example, we treat the frontend registration and the back end user create or edit as the same. 
		elseif ($form->getName()=='com_users.registration' || $form->getName()=='com_users.user' ){		
			// Add the registration fields to the form.
			JForm::addFormPath(dirname(__FILE__).'/profiles');
			$form->loadFile('profile', false);

			// Toggle whether the designation field is required.
			if ($this->params->get('register-require_designation', 1) > 0) {
				$form->setFieldAttribute('designation', 'required', $this->params->get('register-require_designation') == 2, 'profilespaarchive');
			} else {
				$form->removeField('designation', 'profilespaarchive');
			}

			// Toggle whether the description field is required.
			if ($this->params->get('register-require_description', 1) > 0) {
				$form->setFieldAttribute('description', 'required', $this->params->get('register-require_description') == 2, 'profilespaarchive');
			} else {
				$form->removeField('description', 'profilespaarchive');
			}

			// Toggle whether the socials field is required.
			if ($this->params->get('register-require_socials', 1) > 0) {
				$form->setFieldAttribute('socials', 'required', $this->params->get('register-require_socials') == 2, 'profilespaarchive');
			} else {
				$form->removeField('socials', 'profilespaarchive');
			}
		}			
	}

	// check file type
	protected static function fileExtensionCheck($file, $allowed){
		$ext = pathinfo($file['name']['profilespaarchive']['author_avatar'], PATHINFO_EXTENSION);
		if(in_array( strtolower($ext), $allowed) ) {
			return true;
		}
		return false;
	}

	function onUserBeforeSave($user, $isnew, $new){
		//Import filesystem libraries. Perhaps not necessary, but does not hurt
		jimport('joomla.filesystem.folder');
		jimport('joomla.filesystem.file');
		self::$avatar = JRequest::getVar( 'jform', null, 'files');

		//get last uploaded avatar
		self::$last_avatar = json_decode(self::existAvatar($new['id'])->profile_value)->avatar;
		
		// New avatar
		$avatar_name 	= self::$avatar['name']['profilespaarchive']['author_avatar'];
		$avatar_ext 	= strtolower(JFile::getExt($avatar_name));

		// allow types
        $allowed_types = array('image' => array('jpg', 'jpeg', 'png'));
		if ( isset($avatar_name) && $avatar_name !='' ) {
			if ( (!self::fileExtensionCheck(self::$avatar, array('png', 'jpg', 'jpeg'))) && !in_array($avatar_ext, $allowed_types) ) {
				throw new RuntimeException(JText::_('INVALID_FILE_TYPE'), 1);
			}elseif (self::$avatar['size']['profilespaarchive']['author_avatar'] > 800000) {
				throw new RuntimeException(JText::_('INVALID_FILE_SIZE'), 1);
			}
		}	
	}

 
	function onUserAfterSave($data, $isNew, $result, $error){

		$userId	= JArrayHelper::getValue($data, 'id', 0, 'int');
		$avatar_name = self::$avatar['name']['profilespaarchive']['author_avatar'];

		// has avatar
		if (isset($avatar_name) && $avatar_name != '') {
			$folder_path = JPATH_ROOT . '/media/com_spauthorarchive/users/';
			if (!JFolder::exists( $folder_path )) {
				JFolder::create($folder_path);
			}
			// Cleans the name of teh file by removing weird characters
			$filename = JFile::makeSafe($avatar_name); 
			$src = self::$avatar['tmp_name']['profilespaarchive']['author_avatar'];
		}
 
		if (($userId && $result && isset($data['profilespaarchive']) && (count($data['profilespaarchive']))) || $src){
			try{

				if (isset($avatar_name) && $avatar_name != '') {
					$exist_avatar = json_decode(self::existAvatar($userId)->profile_value)->avatar;
					if (isset($exist_avatar) && $exist_avatar != '') {
						JFile::delete(JPATH_ROOT.$exist_avatar);
					}	
				}

				$db = JFactory::getDbo();
				$query = $db->getQuery(true);
				$result = $db->setQuery('SELECT profile_value FROM #__user_profiles WHERE profile_key = profilespaarchive.avatar ');
				$db->setQuery('DELETE FROM #__user_profiles WHERE user_id = '.$userId.' AND profile_key LIKE \'profilespaarchive.%\'');

				if (!$db->query()) {
					throw new Exception($db->getErrorMsg());
				}
 				
 				if ((isset($avatar_name) && $avatar_name != '')) {
	 				$file_path = '/media/com_spauthorarchive/users/'. $userId.'-'.$filename;

					// if file upload then insert into DB
					if (JFile::upload($src, $folder_path. '/' .$userId.'-'.$filename)) {
						$data['profilespaarchive']['avatar'] = array('avatar'=> $file_path);
					}
				}

				// already has avatar and not uploaded
				if (self::$last_avatar != '' && $avatar_name =='') {
					$data['profilespaarchive']['avatar'] = array('avatar'=> self::$last_avatar);
				} elseif (self::$last_avatar == '' && $avatar_name =='') {
					$data['profilespaarchive']['avatar'] = array('avatar'=> '');
				}

				$tuples = array();
				$order	= 1;
				foreach ($data['profilespaarchive'] as $k => $v) {
					$tuples[] = '('.$userId.', '.$db->quote('profilespaarchive.'.$k).', '.$db->quote(json_encode($v)).', '.$order++.')';
				}
 
				$db->setQuery('INSERT INTO #__user_profiles VALUES '.implode(', ', $tuples));

				if (!$db->query()) {
					throw new Exception($db->getErrorMsg());
				}
			}
			catch (JException $e) {
				$this->_subject->setError($e->getMessage());
				return false;
			}
		}
 
		return true;
	}

	protected static function existAvatar($user_id){
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);
		$query->select('profile_value');
		$query->from($db->quoteName('#__user_profiles'));
		$query->where($db->quoteName('user_id')." = ".$user_id);
		$query->where($db->quoteName('profile_key') . ' = '. $db->quote('profilespaarchive.avatar'));
		$db->setQuery($query);
		$result = $db->loadObject();

		return $result;
	}

	function onUserAfterDelete($user, $success, $msg){
		if (!$success) {
			return false;
		}
 
		$userId	= JArrayHelper::getValue($user, 'id', 0, 'int');
 
		if ($userId) {
			try {
				$db = JFactory::getDbo();
				$db->setQuery(
					'DELETE FROM #__user_profiles WHERE user_id = '.$userId .
					" AND profile_key LIKE 'profilespaarchive.%'"
				);
 
				if (!$db->query()) {
					throw new Exception($db->getErrorMsg());
				}
			}
			catch (JException $e) {
				$this->_subject->setError($e->getMessage());
				return false;
			}
		}
 
		return true;
	}
 
 
 }