Diplom_O/MainForms/SettingsForm.cs

365 lines
9.1 KiB
C#
Raw Permalink Normal View History

2024-09-20 08:53:52 +05:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Diplom_O.DataBase;
namespace Diplom_O
{
public partial class SettingsForm : Form
{
private readonly ColumnConf[] tableUserColumns = {
new ColumnConf(name: "Логин", size: 140)
};
private readonly ColumnConf[] tableAccessColumns = {
new ColumnConf(name: "Форма", size: 140),
new ColumnConf(name: "Просм.", size: 80),
new ColumnConf(name: "Удал.", size: 80),
new ColumnConf(name: "Изм.", size: 80),
new ColumnConf(name: "Доб.", size: 80)
};
private readonly ColumnConf[] tableTypeStajColumns = {
new ColumnConf(name: "Название", size: 140)
};
private void resetTypeStajTable()
{
try
{
var res = new List<string[]>();
var subres = FuncDB.TypeStajListString();
foreach (var a in subres)
res.Add(new string[] { a });
this.ResetTable(
typeStajGridView,
tableTypeStajColumns,
res.ToArray()
);
}
catch (Exception e)
{
this.ShowError(errorLabel, e.Message);
}
}
private TypeStaj selectedTypeStajTable()
{
try
{
if (typeStajGridView.SelectedRows.Count != 1)
return null;
return FuncDB.TypeStajGetByName((string)typeStajGridView.SelectedRows[0].Cells[0].Value);
}
catch (Exception e)
{
this.ShowError(errorLabel, e.Message);
return null;
}
}
private void typeStajGridView_SelectionChanged(object sender, EventArgs e)
{
var type_staj = selectedTypeStajTable();
textBox1.Text = type_staj == null ? "" : type_staj.Name;
}
private void resetUserTable()
{
try
{
this.ResetTable(
userGridView,
tableUserColumns,
FuncDB.UserListTable(true)
);
}
catch (Exception e)
{
this.ShowError(errorLabel, e.Message);
}
}
private User selectedUserTable()
{
try
{
if (userGridView.SelectedRows.Count != 1)
return null;
return FuncDB.UserGetByLogin((string)userGridView.SelectedRows[0].Cells[0].Value);
}
catch (Exception e)
{
this.ShowError(errorLabel, e.Message);
return null;
}
}
private void userGridView_SelectionChanged(object sender, EventArgs e)
{
resetAccessTable();
}
private void initAccessTable()
{
try
{
accessGridView.Rows.Clear();
accessGridView.Columns.Clear();
var c = accessGridView.Columns;
for (var i = 0; i < tableAccessColumns.Length; i++)
{
if (i != 0)
c.Add(new DataGridViewCheckBoxColumn()
{
FalseValue = false,
TrueValue = true,
HeaderText = tableAccessColumns[i].name
});
else
c.Add(tableAccessColumns[i].name, tableAccessColumns[i].name);
c[i].Width = tableAccessColumns[i].size;
if (tableAccessColumns[i].warp)
c[i].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
}
foreach (var formName in FormArray.formName)
{
accessGridView.Rows.Add();
var row = accessGridView.Rows[accessGridView.Rows.Count - 1];
row.Cells[0].Value = formName;
for (var i = 0; i < FormArray.formAccess.Length; i++)
((DataGridViewCheckBoxCell)row.Cells[i + 1]).Value = false;
}
}
catch (Exception ex)
{
this.ShowError(errorLabel, ex.Message);
}
}
private void accessGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (accessGridView.SelectedCells.Count != 1)
return;
var cell = accessGridView.SelectedCells[0];
var accessId = cell.ColumnIndex;
if (accessId == 0)
return;
else
accessId--;
var formId = cell.RowIndex;
((DataGridViewCheckBoxCell)accessGridView.SelectedCells[0]).Value =
!(bool)((DataGridViewCheckBoxCell)accessGridView.SelectedCells[0]).Value;
var user = selectedUserTable();
if (user == null)
throw new Exception("Пользователь не выбран.");
var access = FuncDB.AccessGetByUserIdFormIdAccessId(user.Id, formId, accessId);
if (
(access == null && !(bool)cell.Value) ||
(access != null && (bool)cell.Value)
)
return;
if ((bool)cell.Value)
{
if (FuncDB.AccessGetByUserIdFormIdAccessId(user.Id, formId, 0) == null)
FuncDB.AccessAdd(new Access()
{
UserId = user.Id,
FormId = formId,
FormatAccess = 0
});
FuncDB.AccessAdd(new Access()
{
UserId = user.Id,
FormId = formId,
FormatAccess = accessId
});
}
else
{
if (formId == 7 && FuncDB.AccessToSettingsFullCountWithoutUserId(user) == 0)
throw new Exception("Должен хотя бы 1 польз. иметь полн. дост. к настр.");
if (formId == user.Default && accessId == 0)
throw new Exception("Нельзя снять разрешение для окна по умолчанию.");
if (accessId == 0)
{
var list = FuncDB.AccessGetByUserIdFormId(user.Id, formId);
foreach (var l in list)
FuncDB.AccessDelete(l);
}
else
{
FuncDB.AccessDelete(access);
}
}
resetAccessTable();
}
catch (Exception ex)
{
this.ShowError(errorLabel, ex.Message);
resetAccessTable();
}
}
private void resetAccessTable()
{
try
{
var user = selectedUserTable();
var res = new bool[FormArray.formName.Length, FormArray.formAccess.Length];
if (user != null)
res = FuncDB.AccessListTable(user.Id);
for (var i = 0; i < FormArray.formName.Length; i++)
for (var j = 0; j < FormArray.formAccess.Length; j++)
((DataGridViewCheckBoxCell)accessGridView.Rows[i].Cells[j + 1]).Value = res[i, j];
}
catch (Exception e)
{
this.ShowError(errorLabel, e.Message);
}
}
public SettingsForm()
{
InitializeComponent();
this.correctUpperMenu(menuStrip, 7);
initAccessTable();
resetTypeStajTable();
resetUserTable();
addUserButton.Enabled =
changeUserButton.Enabled =
delUserButton.Enabled =
accessGridView.Enabled =
button2.Enabled =
button1.Enabled =
button3.Enabled =
false;
var user = this.GetUser();
if (user == null)
return;
accessGridView.Enabled =
FuncDB.AccessGetByUserIdFormIdAccessId(user.Id, 7, 2) != null ||
FuncDB.AccessGetByUserIdFormIdAccessId(user.Id, 7, 3) != null;
delUserButton.Enabled =
button1.Enabled =
FuncDB.AccessGetByUserIdFormIdAccessId(user.Id, 7, 1) != null;
changeUserButton.Enabled =
button3.Enabled =
FuncDB.AccessGetByUserIdFormIdAccessId(user.Id, 7, 2) != null;
addUserButton.Enabled =
button2.Enabled =
FuncDB.AccessGetByUserIdFormIdAccessId(user.Id, 7, 3) != null;
}
private void addTypeStajButton_Click(object sender, EventArgs e)
{
try
{
var type = new TypeStaj()
{
Name = textBox1.Text
};
FuncDB.TypeStajAdd(type);
resetTypeStajTable();
}
catch (Exception ex)
{
this.ShowError(errorLabel, ex.Message);
}
}
private void changeTypeStajButton_Click(object sender, EventArgs e)
{
try
{
var type = selectedTypeStajTable();
if (type == null)
throw new Exception("Не вбран тип стажа.");
type.Name = textBox1.Text;
FuncDB.TypeStajChange(type);
resetTypeStajTable();
}
catch (Exception ex)
{
this.ShowError(errorLabel, ex.Message);
}
}
private void delTypeButton_Click(object sender, EventArgs e)
{
try
{
var type = selectedTypeStajTable();
if (type == null)
throw new Exception("Не вбран тип стажа.");
FuncDB.TypeStajDelete(type);
resetTypeStajTable();
}
catch (Exception ex)
{
this.ShowError(errorLabel, ex.Message);
}
}
private void addUserButton_Click(object sender, EventArgs e)
{
try
{
var form = new SupportForms.LoginForm();
form.ShowDialog();
if (!form.isCanceled)
resetUserTable();
}
catch (Exception ex)
{
this.ShowError(errorLabel, ex.Message);
}
}
private void changeUserButton_Click(object sender, EventArgs e)
{
try
{
if (selectedUserTable() == null)
throw new Exception("Не выбран пользователь.");
var form = new SupportForms.LoginForm(selectedUserTable());
form.ShowDialog();
if (!form.isCanceled)
resetUserTable();
}
catch (Exception ex)
{
this.ShowError(errorLabel, ex.Message);
}
}
private void delUserButton_Click(object sender, EventArgs e)
{
try
{
var user = selectedUserTable();
if (user == null)
throw new Exception("Не выбран пользователь.");
if (FuncDB.AccessToSettingsFullCountWithoutUserId(user) == 0)
throw new Exception("Должен хотя бы 1 польз. иметь полн. дост. к настр.");
FuncDB.UserDelete(selectedUserTable());
resetUserTable();
}
catch (Exception ex)
{
this.ShowError(errorLabel, ex.Message);
}
}
private void upperMenuClicked(object sender, ToolStripItemClickedEventArgs e)
{
this.upperMenuClicked((MenuStrip)sender);
if (this.GetNextForm() != -1)
Close();
}
}
}