Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from flopal/master
Browse files Browse the repository at this point in the history
Ajout de la création de profile
  • Loading branch information
Lawrence Deneiple authored Apr 13, 2020
2 parents 7418d8d + d276836 commit 440ecac
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 30 deletions.
7 changes: 7 additions & 0 deletions AttestationsGenerator/AttestationsGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,18 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="CreateProfile.xaml.cs">
<DependentUpon>CreateProfile.xaml</DependentUpon>
</Compile>
<Compile Include="Profile.cs" />
<Compile Include="AttestationsGenerator.xaml.cs">
<DependentUpon>AttestationsGenerator.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="CreateProfile.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">
Expand Down
1 change: 1 addition & 0 deletions AttestationsGenerator/AttestationsGenerator.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<DockPanel Loaded="Window_Loaded">
<Menu DockPanel.Dock="Top" Height="23">
<MenuItem Header="_Options" Height="23">
<MenuItem Header="_Créer un profile" Click="Create_Profile_Click" />
<MenuItem Header="_Générer un template de fichier profiles.json" Click="Generate_Profiles_Click" />
<MenuItem Header="_Charger le fichier profiles.json" Click="Load_Profiles_Click" />
</MenuItem>
Expand Down
64 changes: 34 additions & 30 deletions AttestationsGenerator/AttestationsGenerator.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ private void Button_Click(object sender, RoutedEventArgs e)
{
if (Profiles_ComboBox.Items.Count == 0)
{
MessageBox.Show("Veuillez charger un fichier profiles.json avec des profiles remplis avant de générer une attestation.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
_ = MessageBox.Show("Veuillez charger un fichier profiles.json avec des profiles remplis avant de générer une attestation.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.FileName = "attestation-deplacement-fr-" + DateTime.Now.ToString("dd-MM-yyyy-HH-mm") + ".pdf";
saveFileDialog.Filter = "Fichier PDF | *.pdf";
SaveFileDialog saveFileDialog = new SaveFileDialog
{
FileName = "attestation-deplacement-fr-" + DateTime.Now.ToString("dd-MM-yyyy-HH-mm") + ".pdf",
Filter = "Fichier PDF | *.pdf"
};
if (saveFileDialog.ShowDialog() ?? false)
{
try
Expand All @@ -43,9 +45,8 @@ private void Button_Click(object sender, RoutedEventArgs e)
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(saveFileDialog.FileName);
PdfFormWidget formWidget = doc.Form as PdfFormWidget;
for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
foreach (PdfField field in formWidget.FieldsWidget)// for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
{
PdfField field = formWidget.FieldsWidget.List[i] as PdfField;
if (field is PdfTextBoxFieldWidget)
{
PdfTextBoxFieldWidget textBoxField = field as PdfTextBoxFieldWidget;
Expand Down Expand Up @@ -75,9 +76,11 @@ private void Button_Click(object sender, RoutedEventArgs e)
case "Minute":
textBoxField.Text = DateTime.Now.ToString("mm");
break;
default:
break;
}
}
if (field is PdfCheckBoxWidgetFieldWidget)
else if (field is PdfCheckBoxWidgetFieldWidget)
{
PdfCheckBoxWidgetFieldWidget checkBoxField = field as PdfCheckBoxWidgetFieldWidget;
if (checkBoxField.Name == Exit_Reasons_ComboBox.Text)
Expand All @@ -87,60 +90,61 @@ private void Button_Click(object sender, RoutedEventArgs e)
}
}
doc.SaveToFile(saveFileDialog.FileName);
Process.Start("explorer.exe", saveFileDialog.FileName);
_ = Process.Start("explorer.exe", saveFileDialog.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
_ = MessageBox.Show(ex.Message, "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}

private Profile GetFromFullName(string fullname)
private Profile GetFromFullName(string fullname) => profiles.Find(profil => profil.Fullname == fullname);

private void Create_Profile_Click(object sender, RoutedEventArgs e)
{
foreach (Profile profile in profiles)
new CreateProfile().ShowDialog();
if (LoadProfilesFile())
{
if (profile.Fullname.Equals(fullname))
{
return profile;
}
_ = MessageBox.Show("Votre fichier profiles.json à été correctement lu.", "Réussite", MessageBoxButton.OK, MessageBoxImage.Information);
}
return null;
}

private void Generate_Profiles_Click(object sender, RoutedEventArgs e)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string configPath = Path.Combine(path, "AttestationsGenerator");
Directory.CreateDirectory(configPath);
_ = Directory.CreateDirectory(configPath);
if (!File.Exists(configPath + "\\profiles.json"))
{
List<Profile> templateProfiles = new List<Profile>();
templateProfiles.Add(new Profile("Fake 1", "01 Septembre 1901", "Fake Place 1", "Fake Address 1", "Fake City 1"));
templateProfiles.Add(new Profile("Fake 2", "02 Octobre 1902", "Fake Place 2", "Fake Address 2", "Fake City 2"));
List<Profile> templateProfiles = new List<Profile>
{
new Profile("Fake 1", "01 Septembre 1901", "Fake Place 1", "Fake Address 1", "Fake City 1"),
new Profile("Fake 2", "02 Octobre 1902", "Fake Place 2", "Fake Address 2", "Fake City 2")
};
File.WriteAllText(configPath + "\\profiles.json", JsonConvert.SerializeObject(templateProfiles, Formatting.Indented));
MessageBox.Show("Votre fichier profiles.json à été généré.", "Réussite", MessageBoxButton.OK, MessageBoxImage.Information);
_ = MessageBox.Show("Votre fichier profiles.json à été généré.", "Réussite", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
MessageBox.Show("Vous avez déjà un fichier profiles.json.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
_ = MessageBox.Show("Vous avez déjà un fichier profiles.json.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

private void Load_Profiles_Click(object sender, RoutedEventArgs e)
{
if (loadProfilesFile())
if (LoadProfilesFile())
{
MessageBox.Show("Votre fichier profiles.json à été correctement lu.", "Réussite", MessageBoxButton.OK, MessageBoxImage.Information);
_ = MessageBox.Show("Votre fichier profiles.json à été correctement lu.", "Réussite", MessageBoxButton.OK, MessageBoxImage.Information);
}
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
loadProfilesFile();
_ = LoadProfilesFile();
}

private Boolean loadProfilesFile()
private Boolean LoadProfilesFile()
{
try
{
Expand All @@ -162,10 +166,10 @@ private Boolean loadProfilesFile()
if (Profiles_ComboBox.Items.Contains(profile.Fullname))
{
Profiles_ComboBox.Items.Clear();
MessageBox.Show("Vous ne pouvez pas avoir plusieurs profiles avec le même nom et le même prénom.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
_ = MessageBox.Show("Vous ne pouvez pas avoir plusieurs profiles avec le même nom et le même prénom.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
Profiles_ComboBox.Items.Add(profile.Fullname);
_ = Profiles_ComboBox.Items.Add(profile.Fullname);
}
if (Profiles_ComboBox.Items.Count > 0)
{
Expand All @@ -174,14 +178,14 @@ private Boolean loadProfilesFile()
}
else
{
Directory.CreateDirectory(configPath);
_ = Directory.CreateDirectory(configPath);
}
Exit_Reasons_ComboBox.SelectedIndex = 0;
return true;
}
catch (Exception)
{
MessageBox.Show("Il y a eu un problème pendant la lecture du fichier profiles.json.\r\n\r\nVeuillez le vérifier.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
_ = MessageBox.Show("Il y a eu un problème pendant la lecture du fichier profiles.json.\r\n\r\nVeuillez le vérifier.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
Exit_Reasons_ComboBox.SelectedIndex = 0;
return false;
}
Expand Down
23 changes: 23 additions & 0 deletions AttestationsGenerator/CreateProfile.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Window x:Class="AttestationsGenerator.CreateProfile"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AttestationsGenerator"
mc:Ignorable="d"
Title="Créer un profile" Height="230" Width="320">
<Grid>
<Label Content="Nom complet : " HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="FullName" HorizontalAlignment="Left" Margin="150,17,0,0" VerticalAlignment="Top" Width="152"/>
<Label Content="Date de naissance :" HorizontalAlignment="Left" Margin="10,40,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="DateBirth" HorizontalAlignment="Left" Margin="150,47,0,0" VerticalAlignment="Top" Width="152"/>
<Label Content="Lieu de naissance :" HorizontalAlignment="Left" Margin="10,70,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="CityBirth" HorizontalAlignment="Left" Margin="150,77,0,0" VerticalAlignment="Top" Width="152"/>
<Label Content="Adresse :" HorizontalAlignment="Left" Margin="10,100,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="Address" HorizontalAlignment="Left" Margin="150,107,0,0" VerticalAlignment="Top" Width="152"/>
<Label Content="Ville : " HorizontalAlignment="Left" Margin="10,130,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="City" HorizontalAlignment="Left" Margin="150,137,0,0" VerticalAlignment="Top" Width="152"/>
<Button Content="Créer le profile" HorizontalAlignment="Left" Margin="110,170,0,0" VerticalAlignment="Top" Width="100" Click="Create_Profile"/>

</Grid>
</Window>
63 changes: 63 additions & 0 deletions AttestationsGenerator/CreateProfile.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace AttestationsGenerator
{
/// <summary>
/// Logique d'interaction pour Window1.xaml
/// </summary>
public partial class CreateProfile : Window
{

public CreateProfile()
{
InitializeComponent();
}

private List<TextBox> get_all_textbox()
{
return new List<TextBox>()
{
FullName,
DateBirth,
CityBirth,
Address,
City
};
}

private void Create_Profile(object sender, RoutedEventArgs e)
{
foreach (TextBox tb in get_all_textbox())
{
if (string.IsNullOrEmpty(tb.Text) || string.IsNullOrWhiteSpace(tb.Text))
{
_ = MessageBox.Show("Veuillez remplir tous les champs.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
}
Profile p = new Profile(FullName.Text, DateBirth.Text, CityBirth.Text, Address.Text, City.Text);
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string configPath = Path.Combine(path, "AttestationsGenerator");
if (!File.Exists(configPath + "\\profiles.json"))
{
List<Profile> templateProfiles = new List<Profile>
{
p
};
File.WriteAllText(configPath + "\\profiles.json", JsonConvert.SerializeObject(templateProfiles, Formatting.Indented));
_ = MessageBox.Show("Votre fichier profiles.json à été généré.", "Réussite", MessageBoxButton.OK, MessageBoxImage.Information);
} else
{
List<Profile> profiles = JsonConvert.DeserializeObject<List<Profile>>(File.ReadAllText(configPath + "\\profiles.json"));
profiles.Add(p);
File.WriteAllText(configPath + "\\profiles.json", JsonConvert.SerializeObject(profiles, Formatting.Indented));
}
this.Close();
}
}
}

0 comments on commit 440ecac

Please sign in to comment.