<?php
class ClassGlobsiProvider extends ClassConfig {
  public function __construct(){}
  public function __destruct(){}
  
  public function getConfig(){
      $oPDOLink = ClassConfig::databaseConnect();
      $rowsConfig = array();
      $sql="
      SELECT *
      FROM globsi_providers_config
      WHERE is_active=TRUE
      ";
      $execSQL = $oPDOLink->prepare($sql);
      $execSQL->execute(array());
      $rows = $execSQL->fetchAll(PDO::FETCH_ASSOC);
      foreach($rows as $k=>$v){
          $erpConfig[$rows[$k]['k']] = $rows[$k]['v'];
      }
      return $erpConfig;
  }
    
	public function createProvider($data, $ext_id){
		$this->_data = $data;
		$this->_extId = $ext_id;
		
		$config = ClassConfig::getConfig();
		$oPDOLink = ClassConfig::foreignDatabaseConnect($_SESSION['globsi_providers']['db_server'], $_SESSION['globsi_providers']['db_name'], $_SESSION['globsi_providers']['db_user'], $_SESSION['globsi_providers']['db_password']);
		
		//IF password and confirmPassword are not identical, displaying an error message
		if($this->_data['formCreateProviderFieldPassword'] != $this->_data['formCreateProviderFieldConfirmPassword']){
			$message['state']='failed';
			$message['css_class']='failed';
			$message['translation_code'] = 'checkRegisterForm_notSamePassword';
			return $message;
		}
		else {
			$activation_code = rand();
			$sql="
			INSERT INTO user_user(email, password, activation_code, firstname, lastname, 
					core_lang_id, core_country_id, core_currency_id, core_theme_id,
					is_active, comment
			)VALUES(
					:email, :password, :activation_code,
					:firstname, :lastname, 
					12, 44, 4, 1,
					true, 'user created during installation process')
			";
			$execSQL = $oPDOLink->prepare($sql);
			if($execSQL->execute(array(
					':email'=>$this->_data['formCreateProviderFieldEmail'],
					':password'=>sha1($this->_data['formCreateProviderFieldPassword'].'-k3P[8x&'),
					':activation_code'=>$activation_code,
					':firstname'=>$this->_data['formCreateProviderFieldFirstname'],
					':lastname'=>$this->_data['formCreateProviderFieldLastname']
			))){
				$newUserId = $oPDOLink->lastInsertId('user_user_id_seq');
				//$isVipOfferActive = $this->_checkIfVipOfferActive();
				//$isEverythingForFreeActive = $this->_checkIfEverythingForFreeActive();
				// core user detail
				$sql="
				INSERT INTO user_detail(user_id, ext_id)
				VALUES (:user_id, :ext_id)
				";
				$execSQL = $oPDOLink->prepare($sql);
				$execSQL->execute(array(':user_id'=>$newUserId, ':ext_id'=>$this->_extId));
				
				// features for user
				$sql="
				INSERT INTO useruser_corefeature_rel(user_id, core_feature_id)
				VALUES (
					(SELECT id FROM user_user WHERE email=:email LIMIT 1),
					(SELECT id FROM core_feature WHERE code=:core_feature_code LIMIT 1)
				);
				";
				$execSQL = $oPDOLink->prepare($sql);
				$execSQL->execute(array(':email'=>$this->_data['formCreateProviderFieldEmail'], ':core_feature_code'=>'home'));
				$execSQL->execute(array(':email'=>$this->_data['formCreateProviderFieldEmail'], ':core_feature_code'=>'my-profile'));
				$execSQL->execute(array(':email'=>$this->_data['formCreateProviderFieldEmail'], ':core_feature_code'=>'logout'));
				
				$message['user_id'] = $newUserId;
				$message['activation_code'] = $activation_code;
				$message['status'] = "green";
				$message['content'] = 'User created with success';
				//TODO change confirmation_registration by register_form_success when registrations will need an activation by clic on link in an email
				//$message['translation_code'] = 'register_form_success'; //'confirmation_registration';
				
				//TODO send an email to admin to indicate that there is a new registration
				return $message;
			} else {
				$message['status']='red';
				$message['content'] = 'User has not been created';
				//$message['translation_code'] = 'register_form_failed';
				return $message;
			}
		}
  }

	public function getClient($user_id){
    $this->_userId = $user_id;
		
		$config = ClassConfig::getConfig();
		$oPDOLink = ClassConfig::foreignDatabaseConnect($_SESSION['globsi_providers']['db_server'], $_SESSION['globsi_providers']['db_name'], $_SESSION['globsi_providers']['db_user'], $_SESSION['globsi_providers']['db_password']);

    $sql="
    SELECT ud.ext_id, uu.*
    FROM user_user uu
			LEFT JOIN user_detail ud
			  ON uu.id=ud.user_id
    WHERE uu.id=:user_id;
    ";
    $execSQL = $oPDOLink->prepare($sql);
    $execSQL->execute(array(':user_id'=>$this->_userId));
    $row = $execSQL->fetch(PDO::FETCH_ASSOC);
    return $row;
	}
    
}
