128 lines
5.3 KiB
PHP
128 lines
5.3 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../database/employees/EmployeeAccess.php';
|
|
|
|
// エラーメッセージを格納する変数
|
|
$errorMessage = '';
|
|
|
|
try {
|
|
// EmployeeData.csv のパスを指定(プロジェクトルートにあるファイル)
|
|
$employeeAccess = new EmployeeAccess(__DIR__ . '/../EmployeeData.csv');
|
|
|
|
$action = isset($_GET['action']) ? $_GET['action'] : '';
|
|
|
|
if ($action == 'list') {
|
|
try {
|
|
// 部分検索機能を追加:検索キーワードがあれば、社員名にキーワードが含まれるかチェック
|
|
$keyword = isset($_GET['keyword']) ? $_GET['keyword'] : "";
|
|
$allEmployees = $employeeAccess->getAll();
|
|
$employees = array();
|
|
if ($keyword != "") {
|
|
foreach ($allEmployees as $emp) {
|
|
if (strpos($emp['name'], $keyword) !== false) {
|
|
$employees[] = $emp;
|
|
}
|
|
}
|
|
} else {
|
|
$employees = $allEmployees;
|
|
}
|
|
include 'employee_list.php';
|
|
} catch (Exception $e) {
|
|
$errorMessage = "社員一覧の取得に失敗しました: " . $e->getMessage();
|
|
include 'employee_list.php';
|
|
}
|
|
} elseif ($action == 'register') {
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
try {
|
|
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
|
|
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
|
|
|
|
if ($name != '' && $email != '') {
|
|
$employeeAccess->add(array('name' => $name, 'email' => $email));
|
|
header("Location: controller.php?action=list");
|
|
exit;
|
|
} else {
|
|
$errorMessage = "名前とメールアドレスは必須です";
|
|
}
|
|
} catch (InvalidArgumentException $e) {
|
|
$errorMessage = $e->getMessage();
|
|
} catch (Exception $e) {
|
|
$errorMessage = "登録に失敗しました: " . $e->getMessage();
|
|
}
|
|
}
|
|
include 'employee_register.php';
|
|
} elseif ($action == 'update') {
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
try {
|
|
$id = isset($_POST['id']) ? $_POST['id'] : '';
|
|
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
|
|
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
|
|
|
|
if ($id != '' && $name != '' && $email != '') {
|
|
$employeeAccess->update($id, array('name' => $name, 'email' => $email));
|
|
header("Location: controller.php?action=list");
|
|
exit;
|
|
} else {
|
|
$errorMessage = "ID、名前、メールアドレスは必須です";
|
|
}
|
|
} catch (InvalidArgumentException $e) {
|
|
$errorMessage = $e->getMessage();
|
|
} catch (RuntimeException $e) {
|
|
$errorMessage = $e->getMessage();
|
|
} catch (Exception $e) {
|
|
$errorMessage = "更新に失敗しました: " . $e->getMessage();
|
|
}
|
|
include 'employee_update.php';
|
|
} else {
|
|
try {
|
|
$id = isset($_GET['id']) ? $_GET['id'] : '';
|
|
if (empty($id)) {
|
|
$errorMessage = "IDが指定されていません";
|
|
include 'employee_update.php';
|
|
} else {
|
|
$employees = $employeeAccess->getAll();
|
|
$employee = null;
|
|
foreach ($employees as $emp) {
|
|
if ($emp['id'] == $id) {
|
|
$employee = $emp;
|
|
break;
|
|
}
|
|
}
|
|
if ($employee === null) {
|
|
$errorMessage = "指定された社員が見つかりません";
|
|
}
|
|
include 'employee_update.php';
|
|
}
|
|
} catch (Exception $e) {
|
|
$errorMessage = "社員情報の取得に失敗しました: " . $e->getMessage();
|
|
include 'employee_update.php';
|
|
}
|
|
}
|
|
} elseif ($action == 'delete' && isset($_GET['id'])) {
|
|
try {
|
|
$id = $_GET['id'];
|
|
if (empty($id)) {
|
|
throw new InvalidArgumentException("IDが指定されていません");
|
|
}
|
|
$employeeAccess->delete($id);
|
|
header("Location: controller.php?action=list");
|
|
exit;
|
|
} catch (InvalidArgumentException $e) {
|
|
$errorMessage = $e->getMessage();
|
|
include 'employee_list.php';
|
|
} catch (RuntimeException $e) {
|
|
$errorMessage = $e->getMessage();
|
|
header("Location: controller.php?action=list&error=" . urlencode($errorMessage));
|
|
exit;
|
|
} catch (Exception $e) {
|
|
$errorMessage = "削除に失敗しました: " . $e->getMessage();
|
|
header("Location: controller.php?action=list&error=" . urlencode($errorMessage));
|
|
exit;
|
|
}
|
|
} else {
|
|
include 'index.php';
|
|
}
|
|
} catch (Exception $e) {
|
|
// 初期化エラーなど、致命的なエラーの場合
|
|
die("システムエラーが発生しました: " . htmlspecialchars($e->getMessage()));
|
|
}
|