<?php
session_start();
$currentBaseDir = dirname($_SERVER['PHP_SELF']);
include($_SESSION['basedir']."core.php");

$svaTool = new svaTool();

//print_r($_REQUEST);


$errorCode = 0;
$opId= $userId= $username= $pwd= $userType=$profileId= $onwerId= $lang= $citizen=$firstName=$lastName=$primaryPhone= $secondaryPhone=$homePhone=$mobilePhone=$email=$primAddress=$secondAddress=$profession=$char1=$char2=$char3=$char4=$char5=$char6=$char7=$char8=$char9=$char10= $status="";

$password = NULL;
$confPasswd = NULL;
// Ajout de nouvel utilisateur

if(isset($_REQUEST['actionType'])) {
	$opId = $_REQUEST['actionType'] ;
}


if($opId == 1 ) $label = "CREE"; else $label = "MODIFIE";

if (isset($_REQUEST['lastName']) && strlen($_REQUEST['lastName']) > 0)
    $lastName = addslashes($_REQUEST['lastName']);
else
    $errorCode = 1;
if (isset($_REQUEST['firstName']) && strlen($_REQUEST['firstName']) > 0)
    $firstName = addslashes($_REQUEST['firstName']);
else
    $errorCode = 2;

if ((isset($_REQUEST['msisdn']) && is_numeric($_REQUEST['msisdn'])) || $_REQUEST['msisdn'] == "")
    $mobilePhone = addslashes($_REQUEST['msisdn']);
else
    $errorCode = 3;


if (isset($_REQUEST['login']) && $svaTool->regularStringCheck($_REQUEST['login']))
    $login = addslashes($_REQUEST['login']);
else
    $errorCode = 5;

if (isset($_REQUEST['email']) && strlen($_REQUEST['email']) > 0 && $svaTool->regularEmailCheck($_REQUEST['email']))
    $email = addslashes($_REQUEST['email']);
else
    $errorCode = 6;
	
if( $opId == 1 ){

		if (isset($_REQUEST['password']) && strlen($_REQUEST['password']) > 0)
			$password = md5($_REQUEST['password']);
		else
			$errorCode = 7;
		
		if (isset($_REQUEST['confPasswd']) && strlen($_REQUEST['confPasswd']) > 0)
			$confPasswd = md5($_REQUEST['confPasswd']);
		else
			$errorCode = 8;
		
		if (strcmp($password, $confPasswd) != 0) {
		
			$errorCode = 9;
		}

}
$countNode =  count($_REQUEST['roleList']);

if($countNode == 0 && $countNode == "" ){
	
	 $errorCode = 10;
	
}









if ($errorCode == 0) {
  
  if( $opId == 1 ) {
	
	  
  }
  else {
	  
	  $userId = $_REQUEST['userId'];
	  // Droppting current data
	  //$dba->UpdateAddDelRolesDetails(3, $roleIds, 0, 0 ,0, 0 , 0, 0, 0, 0,0,0,0,  0);
	  $dba->UpdateAddDelUserRoles(3,  $userId, 0, 0 ) ;
	  
  }
 
$res = $dba->UpdateAddDelUsers($opId, $userId, $login, $password, $userType,$profileId, $_SESSION['ownerId'], $lang, $citizen,$firstName,$lastName,$primaryPhone, $secondaryPhone,$homePhone,$mobilePhone,$email,$primAddress,$secondAddress,$profession,$char1,$char2,$char3,$char4,$char5,$char6,$char7,$char8,$char9,$char10, 0 );

if($opId == 1) $userId = $res;


$opId = 1;
foreach($_REQUEST['roleList'] as $selected) {
	
$res = $dba->UpdateAddDelUserRoles($opId,   $userId, $selected, 0 ) ;
 
}





   //$errorCode = 10;
}




switch ($errorCode) {

    case 0 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"COMPTE $label AVEC SUCCES !\"  }"; 
        break;
    case 1 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE NOM ENTRE N'EST PAS VALIDE !\"  }";
        break;
    case 2 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE PRENOM ENTRE N'EST PAS VALIDE !\"  }"; 
        break;
    case 3 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE NUMERO DE TELEPHONE ENTRE N'EST PAS VALIDE.\"  }"; 
        break;
    case 4 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER LE PROFIL DE L'UTILISATEUR.\"  }";
        break;
    case 5 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE LOGIN EST INVALIDE.\"  }"; 
        break;
    case 6 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! L'EMAIL EST INVALIDE.\"  }";
        break;
    /*case 7 : $msg =  "ERREUR ! LE MOT DE PASSE EST INVALIDE.";
        break;*/
    case 8 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LE MOT DE PASSE DE CONFIRMATION EST INVALIDE .!\"  }"; 
        break;
    case 9 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! LES MOTS DE PASSE NE CONCORDENT PAS.\"  }";
        break;
	case 10 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER UN ROLE.\"  }";
        break;
    case 13 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! IMPOSSIBLE DE CREER LE COMPTE. VEUILLEZ RE-ESSAYER ULTERIEUREMENT!\"  }";  break;
		case 14 : $msg = "{ \"errorCode\": $errorCode,\"errorMessage\" :\"ERREUR ! VEUILLEZ SELECTIONNER LE PLAN TARIFAIRE ASSOCIE AU COMPTE !\"  }";   break;
   
	 
}


echo $msg ;

?>