<?php
/**
 * FW Food menu 5.1.1
 * @copyright C 2019 Fastw3b
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @link http://www.fastw3b.net/ Official website
 **/
defined( '_JEXEC' ) or die( 'Restricted access' );

class menuModelUsersection extends JModelLegacy {
    function loadProfile($user_id) {
    	$profile = $this->getTable('profile');
        $input = JFactory::getApplication()->input;
        $id = $input->getInt('id', $user_id);
		if ($id) {
			$profile->load($id);
		}
    	return $profile;
    }
	function changePassword() {
		$user = JFactory::getUser();
		if (!$user->id) {
			$this->setError(JText::_('FWFM_LOGIN_REGISTER'));
			return;
		}
		$input = JFactory::getApplication()->input;
		$password = $input->getRaw('password');
		$password1 = $input->getRaw('password1');
		$password2 = $input->getRaw('password2');
		if ($password and $password1 and $password2) {
			$db = JFactory::getDBO();
			$db->setQuery('SELECT password FROM `#__users` WHERE id = '.(int)$user->id);
			if ($sys_password = $db->loadResult()) {
				if (JUserHelper::verifyPassword($password, $sys_password)) {
					if ($password1 == $password2) {
						$db->setQuery('UPDATE `#__users` SET password = '.$db->quote(JUserHelper::hashPassword($password1)).' WHERE id = '.(int)$user->id);
						if ($db->execute()) {
							$this->setError(JText::_('FWFM_PASSWORD_CHANGED'));
							return true;
						} else {
							$this->setError(JText::_('FWFM_DB_ERROR', $db->getError()));
						}
					} else $this->setError('FWFM_NEW_PASSWORDS_NOT_MATCH');
				} else $this->setError('FWFM_CURRENT_PASSWORD_NOT_MATCH');
			} else $this->setError(JText::_('FWFM_DB_ERROR', $db->getError()));
		} else {
			$this->setError(JText::_('FWFM_ALL_FIELDS_REQUIRED'));
		}
	}
	function getActiveLayout($view) {
		$app = JFactory::getApplication();
		$layout = $app->input->getCmd('layout', $app->getParams()->get('layout', 'default'));

		if ($layout == 'default') {
			$management = array();
			$app->triggerEvent('ongetUsersectionManagementMenu', array('com_fwfoodmenu', $view, &$management));
			if ($management) {
				$keys = array_keys($management);
				$first_section = $management[$keys[0]];
				if (!empty($first_section['layout'])) {
					$layout = $first_section['layout'];
				}
			} else {
				$account = array();
				$app->triggerEvent('ongetUsersectionAccountMenu', array('com_fwfoodmenu', $view, &$account));
				if ($account) {
					$keys = array_keys($account);
					$first_section = $account[$keys[0]];
					if (!empty($first_section['layout'])) {
						$layout = $first_section['layout'];
					}
				}
			}
		}
		return $layout;
	}
}
