<?php
/**
 * @copyright	Copyright © 2017 - All rights reserved.
 * @license		GNU General Public License v2.0
 * @generator	http://xdsoft/joomla-module-generator/
 */
defined('_JEXEC') or die;

require_once __DIR__ . '/helper.php'; // Include the helper functions only once 
require_once(JPATH_BASE.'/maintenance/config.php');

$app=JFactory::getApplication();
$doc=JFactory::getDocument();
$doc->addStyleSheet("/modules/mod_alusers/assets/css/newstyle.css");
$doc->addScript("/modules/mod_alusers/assets/js/script.js");

$user=JFactory::getUser(); 
$profile=JUserHelper::getProfile($user->id);
$lang=JFactory::getLanguage()->getTag();
$crmUser=ModAlusersHelper::getUserFromCRM((int)$user->nUserID);

if ((int)$user->nUserID>0)
{
  $value = $app->input->cookie->get('alSessionID', null); // Get the cookie
  if ($value == null) // If there's no cookie value, manually set it
  {
    $app->input->cookie->set('alSessionID', md5((int)$user->nUserID), (time()+30), $app->get('cookie_path', '/'), $app->get('cookie_domain'), $app->isSSLConnection()); // Set the cookie
    $app->input->cookie->set('alUserName', $user->name, (time()+30), $app->get('cookie_path', '/'), $app->get('cookie_domain'), $app->isSSLConnection()); // Set the cookie
  }
}

//  ha még új a user, és nincs benne a crm rendszerben, akkor megpróbál egy user szinkronizációt
if ($user->nUserID===NULL) 
{ 
  file_get_contents("http://www.archline.hu/maintenance/sync_user.php");
  $res=ModAlusersHelper::getIdFromEmail($user->username);
  if ((int)$res>0) $user->nUserID=$res;
}

//  hírlevél beállítások mentése
if (isset($_POST) && !empty($_POST) && isset($_POST['nl']) ) 
{ 
  if ((int)$_POST['del_email']==0 && trim($_POST['new_email'])=='') ModAlusersHelper::saveNewsletters($user->nUserID,$_POST['newsletter']);
  ModAlusersHelper::newEmail($user->nUserID,$_POST['new_email']);
  ModAlusersHelper::delEmail($user->nUserID,(int)$_POST['del_email']);
  $user=JFactory::getUser(); // újra beolvassa az adatbázisból a rekordot mentés után
  $crmUser=ModAlusersHelper::getUserFromCRM((int)$user->nUserID);  // újra beolvassa az adatbázisból a rekordot mentés után
  JFactory::getApplication()->enqueueMessage(JText::_('MOD_ARCHLINE_NEWSLETTER_SETTINGS_SUCCESS'),'message');
}

//  személyes adatok mentése
if (isset($_POST) && !empty($_POST) && isset($_POST['ps']) ) 
{ 
  ModAlusersHelper::savePersonalSettings($user->nUserID,$_POST);
  $user=JFactory::getUser();  // újra beolvassa az adatbázisból a rekordot mentés után
  $crmUser=ModAlusersHelper::getUserFromCRM((int)$user->nUserID);  // újra beolvassa az adatbázisból a rekordot mentés után
  JFactory::getApplication()->enqueueMessage(JText::_('MOD_ARCHLINE_PERSONAL_DATA_SUCCESS'),'message');
}

//  új jelszó mentése
if (isset($_POST) && !empty($_POST) && isset($_POST['pw']) ) 
{
  $pass1=trim(addslashes($_POST['password1'])); 
  $pass2=trim(addslashes($_POST['password2'])); 

  if ($pass1!=$pass2) {JFactory::getApplication()->enqueueMessage(JText::_('MOD_ARCHLINE_PASSWORD_CHANGE_ERROR'),'error');}
  elseif (strlen($pass1)<6 || strlen($pass1)>32) {JFactory::getApplication()->enqueueMessage(JText::_('MOD_ARCHLINE_PASSWORD_CHANGE_ERROR2'),'error');}
  else
  {
    ModAlusersHelper::savePassword($user->id,$pass1); 
    JFactory::getApplication()->enqueueMessage(JText::_('MOD_ARCHLINE_PASSWORD_CHANGE_SUCCESS'),'message');
  }
}

$newsletterEmails=ModAlusersHelper::getNewsletterEmails($user->nUserID);
$countries=ModAlusersHelper::getCountries();
$profs=ModAlusersHelper::getProfessions();

$emails=ModAlusersHelper::getEmails($user->email);
if ($emails && count($emails)>0)
{
  foreach ($emails as $tmp) {$ids[]=$tmp->nUserID;}
  $kulcsok=ModAlusersHelper::getAllKeyData($ids);
} 

require JModuleHelper::getLayoutPath('mod_alusers', $params->get('layout', 'default'));