2020-11-30 00:19:37 +01:00
using Hermes.Model ;
using System ;
using System.Data.Entity ;
2020-09-07 22:24:37 +02:00
using System.Collections.Generic ;
using System.Windows ;
using System.Windows.Input ;
2020-12-01 21:57:15 +01:00
using System.Windows.Data ;
2020-12-04 23:29:56 +01:00
using System.IO ;
using System.Text ;
using Microsoft.Win32 ;
2020-09-07 22:24:37 +02:00
2020-11-30 00:19:37 +01:00
namespace Hermes {
public partial class MainWindow : Window {
private ModelContext dbContext = null ;
2020-12-01 21:57:15 +01:00
private CollectionViewSource citoyenCollectionViewSource = null ;
2020-12-01 22:06:27 +01:00
private bool isEnabledFilter = false ;
2020-12-29 22:56:32 +01:00
private Preferences pref = null ;
2020-11-30 00:19:37 +01:00
public MainWindow ( ) {
dbContext = ModelContext . Getinstance ( ) ;
//dbContext.Database.Log = log => System.Console.WriteLine(log);
dbContext . Database . CreateIfNotExists ( ) ;
2020-09-07 22:24:37 +02:00
2020-12-29 22:56:32 +01:00
dbContext . CitoyenSet . Load ( ) ;
dbContext . Preferences . Load ( ) ;
if ( dbContext . Preferences . Local . Count = = 0 ) {
pref = new Preferences ( ) ;
pref . civiliteViewEnabled = false ;
pref . nomViewEnabled = true ;
pref . nomNaissanceViewEnabled = false ;
pref . prenomViewEnabled = true ;
pref . ageViewEnabled = false ;
pref . professionViewEnabled = false ;
pref . typeResidenceViewEnabled = false ;
pref . mailViewEnabled = false ;
pref . telViewEnabled = false ;
pref . telPortViewEnabled = false ;
pref . quartierViewEnabled = false ;
pref . adresseViewEnabled = true ;
pref . adresseBatimentViewEnabled = false ;
pref . adresseNumeroBatimentViewEnabled = false ;
pref . adresseExtViewEnabled = false ;
pref . adresseExtCPViewEnabled = false ;
pref . adresseExtVilleViewEnabled = false ;
pref . dateCreationViewEnabled = false ;
pref . dateModificationViewEnabled = false ;
pref . windowWidth = 1024 ;
pref . windowHeight = 640 ;
pref . windowMaximized = false ;
dbContext . Preferences . Add ( pref ) ;
dbContext . SaveChanges ( ) ;
}
pref = dbContext . Preferences . Local [ 0 ] ;
2020-09-07 22:24:37 +02:00
InitializeComponent ( ) ;
2020-12-29 22:56:32 +01:00
Height = pref . windowHeight ;
Width = pref . windowWidth ;
if ( pref . windowMaximized ) {
WindowState = WindowState . Maximized ;
} else {
WindowState = WindowState . Normal ;
}
viewMenu . DataContext = pref ;
2020-09-07 22:24:37 +02:00
}
2020-11-30 00:19:37 +01:00
private void Window_Loaded ( object sender , RoutedEventArgs e ) {
2020-12-01 21:57:15 +01:00
citoyenCollectionViewSource = ( CollectionViewSource ) this . FindResource ( "citoyenCollectionViewSource" ) ;
citoyenCollectionViewSource . Source = dbContext . CitoyenSet . Local ;
2020-12-04 23:29:56 +01:00
2020-12-29 22:56:32 +01:00
if ( string . IsNullOrWhiteSpace ( pref . VilleCP ) | | string . IsNullOrWhiteSpace ( pref . Ville ) ) {
2020-12-04 23:29:56 +01:00
MessageBox . Show ( "Il s'agit du premier lancement de Hermes. Veuillez renseigner les informations sur votre commune." , "Premier lancement" , MessageBoxButton . OK , MessageBoxImage . None ) ;
2020-12-06 21:54:59 +01:00
PreferencesWindow preferencesWindow = new PreferencesWindow ( this ) ;
preferencesWindow . ShowDialog ( ) ;
2020-12-04 23:29:56 +01:00
}
2020-11-30 00:19:37 +01:00
}
private void Options_Click ( object sender , RoutedEventArgs e ) {
2020-12-06 21:54:59 +01:00
PreferencesWindow preferencesWindow = new PreferencesWindow ( this ) ;
preferencesWindow . ShowDialog ( ) ;
2020-11-30 00:19:37 +01:00
}
private void Ajouter_Click ( object sender , RoutedEventArgs e ) {
2020-12-06 21:54:59 +01:00
CitoyenWindow citoyenWindow = new CitoyenWindow ( this ) ;
citoyenWindow . ShowDialog ( ) ;
}
private void GroupEdit_Batiment ( object sender , RoutedEventArgs e ) {
if ( dgCitoyens . SelectedItems . Count = = 0 ) {
2020-12-28 00:24:12 +01:00
MessageBox . Show ( "Aucun citoyen sélectionné." , "Édition multiple" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ;
2020-12-06 21:54:59 +01:00
return ;
}
InputWindow inputBox = new InputWindow ( "Associer un bâtiment :" , "Entrez le nom d'un bâtiment :" , this ) ;
if ( inputBox . ShowDialog ( ) = = true ) {
foreach ( Citoyen citoyen in dgCitoyens . SelectedItems ) {
citoyen . AdresseBatiment = inputBox . InputText ;
citoyen . DateModification = DateTime . Now ;
}
dbContext . SaveChanges ( ) ;
}
}
private void GroupEdit_Quartier ( object sender , RoutedEventArgs e ) {
if ( dgCitoyens . SelectedItems . Count = = 0 ) {
2020-12-28 00:24:12 +01:00
MessageBox . Show ( "Aucun citoyen sélectionné." , "Édition multiple" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ;
2020-12-06 21:54:59 +01:00
return ;
}
InputWindow inputBox = new InputWindow ( "Associer un quartier :" , "Entrez le nom d'un quartier :" , this ) ;
if ( inputBox . ShowDialog ( ) = = true ) {
foreach ( Citoyen citoyen in dgCitoyens . SelectedItems ) {
citoyen . Quartier = inputBox . InputText ;
citoyen . DateModification = DateTime . Now ;
}
dbContext . SaveChanges ( ) ;
}
2020-09-07 22:24:37 +02:00
}
2020-12-28 00:24:12 +01:00
private void GroupEdit_Adresse ( object sender , RoutedEventArgs e ) {
if ( dgCitoyens . SelectedItems . Count = = 0 ) {
MessageBox . Show ( "Aucun citoyen sélectionné." , "Édition multiple" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ;
return ;
}
InputWindow inputBox = new InputWindow ( "Associer une adresse :" , "Entrez un numéro et un nom de rue :" , this ) ;
if ( inputBox . ShowDialog ( ) = = true ) {
foreach ( Citoyen citoyen in dgCitoyens . SelectedItems ) {
citoyen . Adresse = inputBox . InputText ;
citoyen . DateModification = DateTime . Now ;
}
dbContext . SaveChanges ( ) ;
}
}
2020-11-30 00:19:37 +01:00
private void Supprimer_Click ( object sender , RoutedEventArgs e ) {
if ( dgCitoyens . SelectedItems . Count > 0 ) {
MessageBoxResult result = MessageBox . Show ( "Voulez-vous supprimer ces citoyens ?" , "Suppression" , MessageBoxButton . YesNo , MessageBoxImage . Question ) ;
if ( result = = MessageBoxResult . Yes ) {
List < Citoyen > clist = new List < Citoyen > ( ) ;
foreach ( Citoyen c in dgCitoyens . SelectedItems ) {
clist . Add ( c ) ;
}
foreach ( Citoyen c in clist ) {
dbContext . CitoyenSet . Remove ( c ) ;
}
dbContext . SaveChanges ( ) ;
}
} else {
2020-12-06 23:04:04 +01:00
MessageBox . Show ( "Aucun citoyen sélectionné." , "Suppression" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ;
2020-11-30 00:19:37 +01:00
}
}
2020-12-05 12:29:30 +01:00
private void Sms_Click ( object sender , RoutedEventArgs e ) {
if ( dgCitoyens . SelectedItems . Count > 0 ) {
MessageBoxResult result = MessageBox . Show ( "Voulez-vous envoyer un SMS à ces citoyens ?" , "Envoi de SMS" , MessageBoxButton . YesNo , MessageBoxImage . Question ) ;
if ( result = = MessageBoxResult . Yes ) {
bool noTel = false ;
2020-12-05 18:47:13 +01:00
List < Citoyen > rcps = new List < Citoyen > ( ) ;
2020-12-05 12:29:30 +01:00
foreach ( Citoyen citoyen in dgCitoyens . SelectedItems ) {
2020-12-05 18:47:13 +01:00
if ( ! String . IsNullOrWhiteSpace ( citoyen . TelPort ) ) {
rcps . Add ( citoyen ) ;
} else {
2020-12-05 12:29:30 +01:00
noTel = true ;
}
}
2020-12-05 18:47:13 +01:00
if ( rcps . Count > 0 ) {
if ( noTel ) {
2020-12-30 18:46:39 +01:00
result = MessageBox . Show ( "Certains des citoyens sélectionnés ne disposent pas d'un numéro de mobile. Voulez-vous continuer ?" , "Envoi de SMS" , MessageBoxButton . YesNo , MessageBoxImage . Exclamation ) ;
if ( result = = MessageBoxResult . No ) {
return ;
}
2020-12-05 18:47:13 +01:00
}
2020-12-06 21:54:59 +01:00
SmsWindow smsWindow = new SmsWindow ( this , rcps ) ;
smsWindow . ShowDialog ( ) ;
2020-12-05 18:47:13 +01:00
} else {
2020-12-06 23:04:04 +01:00
MessageBox . Show ( "Aucun des citoyens sélectionnés ne disposent d'un numéro de mobile." , "Envoi de SMS" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ;
2020-12-05 12:29:30 +01:00
}
}
} else {
2020-12-06 23:04:04 +01:00
MessageBox . Show ( "Aucun citoyen sélectionné." , "Envoi de SMS" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ;
2020-12-05 12:29:30 +01:00
}
}
2020-12-04 23:29:56 +01:00
private void Courriel_Click ( object sender , RoutedEventArgs e ) {
if ( dgCitoyens . SelectedItems . Count > 0 ) {
MessageBoxResult result = MessageBox . Show ( "Voulez-vous envoyer un courriel à ces citoyens ?" , "Envoi de courriel" , MessageBoxButton . YesNo , MessageBoxImage . Question ) ;
if ( result = = MessageBoxResult . Yes ) {
2020-12-05 12:29:30 +01:00
bool noMail = false ;
2020-12-04 23:29:56 +01:00
List < string > mails = new List < string > ( ) ;
foreach ( Citoyen citoyen in dgCitoyens . SelectedItems ) {
if ( ! String . IsNullOrWhiteSpace ( citoyen . Mail ) ) {
2020-12-05 12:29:30 +01:00
mails . Add ( citoyen . Mail ) ;
} else {
noMail = true ;
2020-12-04 23:29:56 +01:00
}
}
if ( mails . Count > 0 ) {
2020-12-05 12:29:30 +01:00
if ( noMail ) {
2021-01-01 00:54:03 +01:00
result = MessageBox . Show ( "Certains des citoyens sélectionnés ne disposent pas d'une adresse E-Mail. Voulez-vous continuer ?" , "Envoi de courriel" , MessageBoxButton . YesNo , MessageBoxImage . Exclamation ) ;
2020-12-30 18:46:39 +01:00
if ( result = = MessageBoxResult . No ) {
return ;
}
2020-12-05 12:29:30 +01:00
}
2020-12-04 23:29:56 +01:00
Microsoft . Office . Interop . Outlook . Application app = new Microsoft . Office . Interop . Outlook . Application ( ) ;
Microsoft . Office . Interop . Outlook . MailItem mailItem = app . CreateItem ( Microsoft . Office . Interop . Outlook . OlItemType . olMailItem ) ;
foreach ( string mail in mails ) {
Microsoft . Office . Interop . Outlook . Recipient rcp = mailItem . Recipients . Add ( mail ) ;
rcp . Type = ( int ) Microsoft . Office . Interop . Outlook . OlMailRecipientType . olBCC ;
}
mailItem . Recipients . ResolveAll ( ) ;
2020-12-06 23:04:04 +01:00
MessageBox . Show ( "Assurez-vous que Microsoft OutLook soit démarré avant de continuer." , "Envoi de courriel" , MessageBoxButton . OK , MessageBoxImage . None ) ;
2020-12-04 23:29:56 +01:00
mailItem . Display ( true ) ;
} else {
2020-12-06 23:04:04 +01:00
MessageBox . Show ( "Aucun des citoyens sélectionnés ne disposent d'une adresse E-Mail." , "Envoi de courriel" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ;
2020-12-04 23:29:56 +01:00
}
}
} else {
2020-12-06 23:04:04 +01:00
MessageBox . Show ( "Aucun citoyen sélectionné." , "Envoi de courriel" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ;
2020-12-04 23:29:56 +01:00
}
}
private void Publipostage_Click ( object sender , RoutedEventArgs e ) {
if ( dgCitoyens . SelectedItems . Count > 0 ) {
MessageBoxResult result = MessageBox . Show ( "Voulez-vous réaliser une tâche de publipostage pour ces citoyens ?" , "Publipostage" , MessageBoxButton . YesNo , MessageBoxImage . Question ) ;
if ( result = = MessageBoxResult . Yes ) {
string csvPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , "hermes_tmp_datasource.csv" ) ;
2020-12-30 18:46:39 +01:00
string csvHeader = "\"Civilité\";\"Nom\";\"Nom de naissance\";\"Prénom\";\"Profession\";\"Type de résidence\";\"E-Mail\";\"Téléphone\";\"Mobile\";\"Quartier\";\"Bâtiment\";\"Numéro appartement\";\"Adresse locale\";\"Code postal local\";\"Ville locale\";\"Adresse principale\";\"Code postal principal\";\"Ville principale\";\"Adresse secondaire\";\"Code postal secondaire\";\"Ville secondaire\"" ;
2020-12-04 23:29:56 +01:00
StringBuilder sb = new StringBuilder ( ) ;
sb . AppendLine ( csvHeader ) ;
foreach ( Citoyen citoyen in dgCitoyens . SelectedItems ) {
string civilite = citoyen . Civilite = = null ? "" : citoyen . Civilite ;
string nom = citoyen . Nom = = null ? "" : citoyen . Nom ;
string nomNaissance = citoyen . NomNaissance = = null ? "" : citoyen . NomNaissance ;
string prenom = citoyen . Prenom = = null ? "" : citoyen . Prenom ;
string profession = citoyen . Profession = = null ? "" : citoyen . Profession ;
string typeResidence = citoyen . TypeResidenceLabel = = null ? "" : citoyen . TypeResidenceLabel ;
string mail = citoyen . Mail = = null ? "" : citoyen . Mail ;
string tel = citoyen . Tel = = null ? "" : citoyen . Tel ;
string telPort = citoyen . TelPort = = null ? "" : citoyen . TelPort ;
string quartier = citoyen . Quartier = = null ? "" : citoyen . Quartier ;
string batiment = citoyen . AdresseBatiment = = null ? "" : citoyen . AdresseBatiment ;
2020-12-28 00:24:12 +01:00
string numeroBatiment = citoyen . AdresseNumeroBatiment = = null ? "" : citoyen . AdresseNumeroBatiment ;
2020-12-30 18:46:39 +01:00
string adresseLocale = citoyen . Adresse = = null ? "" : citoyen . Adresse ;
string cpLocal = citoyen . AdresseCP = = null ? "" : citoyen . AdresseCP ;
string villeLocale = citoyen . AdresseVille = = null ? "" : citoyen . AdresseVille ;
2020-12-04 23:29:56 +01:00
string adressePrincipale = citoyen . AdressePrincipale = = null ? "" : citoyen . AdressePrincipale ;
string cpPrincipal = citoyen . AdressePrincipaleCP = = null ? "" : citoyen . AdressePrincipaleCP ;
string villePrincipale = citoyen . AdressePrincipaleVille = = null ? "" : citoyen . AdressePrincipaleVille ;
string adresseSecondaire = citoyen . AdresseSecondaire = = null ? "" : citoyen . AdresseSecondaire ;
string cpSecondaire = citoyen . AdresseSecondaireCP = = null ? "" : citoyen . AdresseSecondaireCP ;
string villeSecondaire = citoyen . AdresseSecondaireVille = = null ? "" : citoyen . AdresseSecondaireVille ;
2020-12-30 18:46:39 +01:00
sb . AppendLine ( $"\" { civilite } \ ";\"{nom}\";\"{nomNaissance}\";\"{prenom}\";\"{profession}\";\"{typeResidence}\";\"{mail}\";\"{tel}\";\"{telPort}\";\"{quartier}\";\"{batiment}\";\"{numeroBatiment}\";\"{adresseLocale}\";\"{cpLocal}\";{villeLocale};\"{adressePrincipale}\";\"{cpPrincipal}\";\"{villePrincipale}\";\"{adresseSecondaire}\";\"{cpSecondaire}\";\"{villeSecondaire}\"" ) ;
2020-12-04 23:29:56 +01:00
}
2021-01-01 16:28:38 +01:00
try {
File . WriteAllText ( csvPath , sb . ToString ( ) , Encoding . GetEncoding ( "ISO-8859-1" ) ) ;
} catch ( Exception ) {
MessageBox . Show ( "Erreur lors de la préparation des données pour le publipostage." , "Publipostage" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
return ;
}
2021-01-01 00:54:03 +01:00
2020-12-04 23:29:56 +01:00
OpenFileDialog ofd = new OpenFileDialog ( ) ;
ofd . Filter = "Document World|*.doc;*.docx;*.dotx|Tous les ficiers|*.*" ;
if ( ofd . ShowDialog ( ) = = true ) {
Microsoft . Office . Interop . Word . Application app = new Microsoft . Office . Interop . Word . Application ( ) ;
Microsoft . Office . Interop . Word . Document doc = app . Documents . Open ( ofd . FileName ) ;
doc . MailMerge . OpenDataSource ( csvPath , false , false , true ) ;
app . Visible = true ;
}
}
} else {
2020-12-06 23:04:04 +01:00
MessageBox . Show ( "Aucun citoyen sélectionné." , "Publipostage" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ;
2020-12-04 23:29:56 +01:00
}
}
2020-12-28 00:24:12 +01:00
private void Importer_Click ( object sender , RoutedEventArgs e ) {
OpenFileDialog ofd = new OpenFileDialog ( ) ;
2021-01-01 16:28:38 +01:00
ofd . Filter = "Fichiers de données|*.csv|Tous les fichiers|*.*" ;
2020-12-28 00:24:12 +01:00
if ( ofd . ShowDialog ( ) = = true ) {
2021-01-01 00:54:03 +01:00
if ( Path . GetExtension ( ofd . FileName ) . ToLower ( ) . Equals ( ".csv" ) ) {
2021-01-01 16:28:38 +01:00
StreamReader reader = null ;
Cursor previousCursor = Mouse . OverrideCursor ;
Mouse . OverrideCursor = Cursors . Wait ;
2021-01-01 00:54:03 +01:00
try {
2021-01-01 16:28:38 +01:00
reader = File . OpenText ( ofd . FileName ) ;
2021-01-01 00:54:03 +01:00
string csvHeader = reader . ReadLine ( ) ;
if ( csvHeader = = null ) {
return ;
}
string [ ] fields = CitoyensSerializer . ParseHeader ( csvHeader ) ;
CitoyensSerializer serializer = new CitoyensSerializer ( fields ) ;
while ( ! reader . EndOfStream ) {
string line = reader . ReadLine ( ) ;
Citoyen citoyen = serializer . Deserialize ( line ) ;
if ( citoyen ! = null ) {
dbContext . CitoyenSet . Add ( citoyen ) ;
}
}
} catch ( Exception ex ) {
2021-01-01 16:28:38 +01:00
if ( ex is IOException | | ex is UnauthorizedAccessException ) {
MessageBox . Show ( "Impossible d'ouvrir le fichier. Il est possible qu'il soit déjà utilisé par un autre programme ou que le chemin d'accès soit incorrect." , "Importer" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
} else {
MessageBox . Show ( "Erreur lors de l'import des données." , "Importer" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
}
} finally {
Mouse . OverrideCursor = previousCursor ;
if ( reader ! = null ) {
reader . Close ( ) ;
}
dbContext . SaveChanges ( ) ;
2021-01-01 00:54:03 +01:00
}
} else {
ImportWindow importWindow = new ImportWindow ( this , ofd . FileName ) ;
importWindow . ShowDialog ( ) ;
}
}
}
private void Exporter_Click ( object sender , RoutedEventArgs e ) {
if ( dgCitoyens . SelectedItems . Count = = 0 ) {
MessageBox . Show ( "Aucun citoyen sélectionné." , "Exporter" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ;
return ;
}
SaveFileDialog sfd = new SaveFileDialog ( ) ;
sfd . Filter = "Fichier CSV|*.csv" ;
sfd . OverwritePrompt = true ;
if ( sfd . ShowDialog ( ) = = true ) {
2021-01-01 16:28:38 +01:00
StreamWriter s = null ;
Cursor previousCursor = Mouse . OverrideCursor ;
Mouse . OverrideCursor = Cursors . Wait ;
2021-01-01 00:54:03 +01:00
try {
2021-01-01 16:28:38 +01:00
s = File . CreateText ( sfd . FileName ) ;
2021-01-01 00:54:03 +01:00
CitoyensSerializer serializer = new CitoyensSerializer ( ) ;
s . WriteLine ( serializer . GetCsvHeader ( ) ) ;
foreach ( Citoyen citoyen in dgCitoyens . SelectedItems ) {
s . WriteLine ( serializer . Serialize ( citoyen ) ) ;
}
2021-01-01 16:28:38 +01:00
} catch ( Exception ex ) {
if ( ex is IOException | | ex is UnauthorizedAccessException ) {
MessageBox . Show ( "Impossible d'ouvrir ou de créer le fichier. Il est possible qu'il soit déjà utilisé par un autre programme ou que le chemin d'accès soit incorrect." , "Importer" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
} else {
MessageBox . Show ( "Erreur lors de l'export des données." , "Exporter" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
}
} finally {
Mouse . OverrideCursor = previousCursor ;
if ( s ! = null ) {
s . Close ( ) ;
}
2021-01-01 00:54:03 +01:00
}
2020-12-28 00:24:12 +01:00
}
}
2020-11-30 00:19:37 +01:00
private void DgCitoyen_DoubleClick ( object sender , MouseButtonEventArgs e ) {
if ( dgCitoyens . SelectedItem ! = null ) {
2020-12-06 21:54:59 +01:00
CitoyenWindow citoyenWindow = new CitoyenWindow ( this , ( Citoyen ) dgCitoyens . SelectedItem ) ;
citoyenWindow . ShowDialog ( ) ;
2020-11-30 00:19:37 +01:00
}
2020-09-07 22:24:37 +02:00
}
2020-12-01 21:57:15 +01:00
private void Rechercher_Click ( object sender , RoutedEventArgs e ) {
2020-12-01 22:06:27 +01:00
if ( isEnabledFilter ) {
citoyenCollectionViewSource . View . Refresh ( ) ;
} else {
citoyenCollectionViewSource . Filter + = new FilterEventHandler ( CitoyenFilter ) ;
isEnabledFilter = true ;
}
2020-12-01 21:57:15 +01:00
}
2020-12-05 02:12:23 +01:00
private void Quitter_Click ( object sender , RoutedEventArgs e ) {
Close ( ) ;
}
2020-12-01 21:57:15 +01:00
private void Reinitialiser_Click ( object sender , RoutedEventArgs e ) {
2020-12-01 22:06:27 +01:00
if ( isEnabledFilter ) {
citoyenCollectionViewSource . Filter - = new FilterEventHandler ( CitoyenFilter ) ;
isEnabledFilter = false ;
}
2020-12-02 23:27:43 +01:00
ageOperationFilterComboBox . SelectedIndex = 0 ;
ageFilterTextBox . Text = null ;
professionFilterTextBox . Text = null ;
quartierFilterTextBox . Text = null ;
adresseFilterTextBox . Text = null ;
residenceFilterComboBox . SelectedIndex = 0 ;
2020-12-01 21:57:15 +01:00
}
2020-11-30 00:19:37 +01:00
protected override void OnClosed ( EventArgs e ) {
2020-12-29 22:56:32 +01:00
if ( WindowState = = WindowState . Maximized ) {
pref . windowMaximized = true ;
} else {
pref . windowMaximized = false ;
}
pref . windowHeight = ( int ) Height ;
pref . windowWidth = ( int ) Width ;
dbContext . SaveChanges ( ) ;
2020-11-30 00:19:37 +01:00
dbContext . Dispose ( ) ;
2020-09-07 22:24:37 +02:00
base . OnClosed ( e ) ;
Application . Current . Shutdown ( ) ;
}
2020-12-01 21:57:15 +01:00
private void CitoyenFilter ( object sender , FilterEventArgs e ) {
Citoyen citoyen = ( Citoyen ) e . Item ;
e . Accepted = true ;
if ( citoyen ! = null ) {
2020-12-02 23:27:43 +01:00
if ( ! String . IsNullOrEmpty ( professionFilterTextBox . Text ) ) {
if ( citoyen . Profession = = null | | ! citoyen . Profession . ToLower ( ) . Contains ( professionFilterTextBox . Text . ToLower ( ) ) ) {
2020-12-01 21:57:15 +01:00
e . Accepted = false ;
2020-12-02 23:27:43 +01:00
return ;
2020-12-01 21:57:15 +01:00
}
}
2020-12-02 23:27:43 +01:00
try {
int ageFilter = Int32 . Parse ( ageFilterTextBox . Text ) ;
if ( ! citoyen . AgeInt . HasValue
| | ( ageOperationFilterComboBox . SelectedIndex = = 0 & & ageFilter ! = citoyen . AgeInt )
| | ( ageOperationFilterComboBox . SelectedIndex = = 1 & & ageFilter > = citoyen . AgeInt )
| | ( ageOperationFilterComboBox . SelectedIndex = = 2 & & ageFilter < = citoyen . AgeInt ) ) {
e . Accepted = false ;
return ;
}
} catch ( Exception ) { }
if ( ! String . IsNullOrEmpty ( quartierFilterTextBox . Text ) ) {
if ( citoyen . Quartier = = null | | ! citoyen . Quartier . ToLower ( ) . Contains ( quartierFilterTextBox . Text . ToLower ( ) ) ) {
e . Accepted = false ;
return ;
}
}
if ( ! String . IsNullOrEmpty ( adresseFilterTextBox . Text ) ) {
2020-12-28 00:24:12 +01:00
string addrNumBat = citoyen . AdresseNumeroBatiment = = null ? "" : citoyen . AdresseNumeroBatiment ;
string addrBat = citoyen . AdresseBatiment = = null ? "" : citoyen . AdresseBatiment ;
string addr = citoyen . Adresse = = null ? "" : citoyen . Adresse ;
string addrFull = $"{addr} {addrNumBat} {addrBat}" . ToLower ( ) ;
if ( ! addrFull . Contains ( adresseFilterTextBox . Text . ToLower ( ) ) ) {
2020-12-02 23:27:43 +01:00
e . Accepted = false ;
return ;
}
}
if ( ( residenceFilterComboBox . SelectedIndex = = 1 & & citoyen . TypeResidence ! = false )
| | ( residenceFilterComboBox . SelectedIndex = = 2 & & citoyen . TypeResidence ! = true ) ) {
e . Accepted = false ;
return ;
}
2020-12-01 21:57:15 +01:00
}
}
2020-09-07 22:24:37 +02:00
}
}