117 lines
4.6 KiB
PHP
117 lines
4.6 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
session_start();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$first_name = $_POST['first_name'];
|
|
$last_name = $_POST['last_name'];
|
|
$email = $_POST['email'];
|
|
$password = $_POST['password'];
|
|
$company_name = $_POST['company_name'] ?? null;
|
|
$phone = $_POST['phone'] ?? null;
|
|
$role = $_POST['role'] ?? 'guest';
|
|
$functions = isset($_POST['functions']) ? $_POST['functions'] : [];
|
|
$bni_group_id = isset($_POST['bni_group_id']) && !empty($_POST['bni_group_id']) ? $_POST['bni_group_id'] : null;
|
|
|
|
// New fields
|
|
$nip = $_POST['nip'] ?? null;
|
|
$industry = $_POST['industry'] ?? null;
|
|
$company_size_revenue = $_POST['company_size_revenue'] ?? null;
|
|
$business_description = $_POST['business_description'] ?? null;
|
|
|
|
if (empty($firstName) || empty($lastName) || empty($email) || empty($password)) {
|
|
$_SESSION['error_message'] = 'Imię, nazwisko, email i hasło są wymagane.';
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
// Only members can be in a group
|
|
if ($role !== 'member') {
|
|
$bni_group_id = null;
|
|
}
|
|
|
|
$pdo = db();
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
// Insert person details first
|
|
$sql = 'INSERT INTO people (first_name, last_name, email, password, company_name, phone, role, bni_group_id, nip, industry, company_size_revenue, business_description) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([$first_name, $last_name, $email, password_hash($password, PASSWORD_DEFAULT), $company_name, $phone, $role, $bni_group_id, $nip, $industry, $company_size_revenue, $business_description]);
|
|
$personId = $pdo->lastInsertId();
|
|
|
|
// Handle file uploads now that we have a personId
|
|
$upload_dir = 'uploads/people/' . $personId . '/';
|
|
if (!is_dir($upload_dir)) {
|
|
mkdir($upload_dir, 0777, true);
|
|
}
|
|
|
|
$file_fields = [
|
|
'company_logo' => 'company_logo_path',
|
|
'person_photo' => 'person_photo_path',
|
|
'gains_sheet' => 'gains_sheet_path',
|
|
'top_wanted_contacts' => 'top_wanted_contacts_path',
|
|
'top_owned_contacts' => 'top_owned_contacts_path'
|
|
];
|
|
|
|
$file_paths_to_update = [];
|
|
|
|
foreach ($file_fields as $form_field_name => $db_column_name) {
|
|
if (isset($_FILES[$form_field_name]) && $_FILES[$form_field_name]['error'] == UPLOAD_ERR_OK) {
|
|
$tmp_name = $_FILES[$form_field_name]['tmp_name'];
|
|
$original_name = basename($_FILES[$form_field_name]['name']);
|
|
$file_ext = pathinfo($original_name, PATHINFO_EXTENSION);
|
|
$new_filename = uniqid($form_field_name . '_', true) . '.' . $file_ext;
|
|
$destination = $upload_dir . $new_filename;
|
|
|
|
if (move_uploaded_file($tmp_name, $destination)) {
|
|
$file_paths_to_update[$db_column_name] = $destination;
|
|
}
|
|
}
|
|
}
|
|
|
|
// If there are files, update the newly created person record
|
|
if (!empty($file_paths_to_update)) {
|
|
$sql_parts = [];
|
|
$params = [];
|
|
foreach ($file_paths_to_update as $column => $path) {
|
|
$sql_parts[] = "$column = ?";
|
|
$params[] = $path;
|
|
}
|
|
$params[] = $personId;
|
|
$sql = "UPDATE people SET " . implode(', ', $sql_parts) . " WHERE id = ?";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
}
|
|
|
|
// Assign functions
|
|
if (!empty($functions)) {
|
|
$sql = "INSERT INTO user_functions (user_id, function_id) VALUES (?, ?)";
|
|
$stmt = $pdo->prepare($sql);
|
|
foreach ($functions as $functionId) {
|
|
$stmt->execute([$personId, $functionId]);
|
|
}
|
|
}
|
|
|
|
$pdo->commit();
|
|
$_SESSION['success_message'] = 'Osoba dodana pomyślnie.';
|
|
|
|
} catch (PDOException $e) {
|
|
$pdo->rollBack();
|
|
error_log('Create failed: ' . $e->getMessage());
|
|
if ($e->errorInfo[1] == 1062) {
|
|
$_SESSION['error_message'] = 'Błąd: Konto z tym adresem email już istnieje.';
|
|
} else {
|
|
$_SESSION['error_message'] = 'Błąd podczas dodawania osoby: ' . $e->getMessage();
|
|
}
|
|
} catch (Exception $e) {
|
|
if ($pdo->inTransaction()) {
|
|
$pdo->rollBack();
|
|
}
|
|
error_log('File upload or other error: ' . $e->getMessage());
|
|
$_SESSION['error_message'] = 'Błąd: ' . $e->getMessage();
|
|
}
|
|
|
|
header('Location: index.php');
|
|
exit();
|
|
} |