Configuration de la Base de Données
Nous allons utiliser MySQL pour la base de données, qui contiendra trois tables : projects, tasks, et resources.
Structure de la Base de Données (SQL)
CREATE TABLE projects (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
start_date DATE,
end_date DATE,
status VARCHAR(50)
);
CREATE TABLE tasks (
id INT AUTO_INCREMENT PRIMARY KEY,
project_id INT,
name VARCHAR(100) NOT NULL,
description TEXT,
start_date DATE,
end_date DATE,
status VARCHAR(50),
FOREIGN KEY (project_id) REFERENCES projects(id)
);
CREATE TABLE resources (
id INT AUTO_INCREMENT PRIMARY KEY,
task_id INT,
name VARCHAR(100) NOT NULL,
quantity INT,
unit VARCHAR(50),
FOREIGN KEY (task_id) REFERENCES tasks(id)
);
Code PHP
Connexion à la Base de Données
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "construction_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Classe Project
<?php
class Project {
private $conn;
private $table_name = "projects";
public $id;
public $name;
public $start_date;
public $end_date;
public $status;
public function __construct($db) {
$this->conn = $db;
}
public function create() {
$query = "INSERT INTO " . $this->table_name . " (name, start_date, end_date, status) VALUES (?, ?, ?, ?)";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("ssss", $this->name, $this->start_date, $this->end_date, $this->status);
return $stmt->execute();
}
public function read() {
$query = "SELECT * FROM " . $this->table_name;
$result = $this->conn->query($query);
return $result;
}
public function update() {
$query = "UPDATE " . $this->table_name . " SET name = ?, start_date = ?, end_date = ?, status = ? WHERE id = ?";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("ssssi", $this->name, $this->start_date, $this->end_date, $this->status, $this->id);
return $stmt->execute();
}
public function delete() {
$query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("i", $this->id);
return $stmt->execute();
}
}
?>
Classe Task
<?php
class Task {
private $conn;
private $table_name = "tasks";
public $id;
public $project_id;
public $name;
public $description;
public $start_date;
public $end_date;
public $status;
public function __construct($db) {
$this->conn = $db;
}
public function create() {
$query = "INSERT INTO " . $this->table_name . " (project_id, name, description, start_date, end_date, status) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("isssss", $this->project_id, $this->name, $this->description, $this->start_date, $this->end_date, $this->status);
return $stmt->execute();
}
public function read() {
$query = "SELECT * FROM " . $this->table_name;
$result = $this->conn->query($query);
return $result;
}
public function update() {
$query = "UPDATE " . $this->table_name . " SET project_id = ?, name = ?, description = ?, start_date = ?, end_date = ?, status = ? WHERE id = ?";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("isssssi", $this->project_id, $this->name, $this->description, $this->start_date, $this->end_date, $this->status, $this->id);
return $stmt->execute();
}
public function delete() {
$query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
$stmt = $->prepare($query);
$stmt->bind_param("i", $this->id);
return $stmt->execute();
}
}
?>
Classe Resource
<?php
class Resource {
private $conn;
private $table_name = "resources";
public $id;
public $task_id;
public $name;
public $quantity;
public $unit;
public function __construct($db) {
$this->conn = $db;
}
public function create() {
$query = "INSERT INTO " . $this->table_name . " (task_id, name, quantity, unit) VALUES (?, ?, ?, ?)";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("isis", $this->task_id, $this->name, $this->quantity, $this->unit);
return $stmt->execute();
}
public function read() {
$query = "SELECT * FROM " . $this->table_name;
$result = $this->conn->query($query);
return $result;
}
public function update() {
$query = "UPDATE " . $this->table_name . " SET task_id = ?, name = ?, quantity = ?, unit = ? WHERE id = ?";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("isisi", $this->task_id, $this->name, $this->quantity, $this->unit, $this->id);
return $stmt->execute();
}
public function delete() {
$query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("i", $this->id);
return $stmt->execute();
}
}
?>
Interface Utilisateur Basique
<?php
include 'config.php';
include 'Project.php';
include 'Task.php';
include 'Resource.php';
$conn = connect_db();
$project = new Project($conn);
$task = new Task($conn);
$resource = new Resource($conn);
function main() {
global $project, $task, $resource;
while (true) {
echo "1. Add Project\n";
echo "2. Add Task\n";
echo "3. Add Resource\n";
echo "4. View Projects\n";
echo "5. View Tasks\n";
echo "6. View Resources\n";
echo "7. Update Project\n";
echo "8. Update Task\n";
echo "9. Update Resource\n";
echo "10. Delete Project\n";
echo "11. Delete Task\n";
echo "12. Delete Resource\n";
echo "13. Exit\n";
$choice = trim(fgets(STDIN));
switch ($choice) {
case '1':
echo "Enter project name: ";
$project->name = trim(fgets(STDIN));
echo "Enter start date (YYYY-MM-DD): ";
$project->start_date = trim(fgets(STDIN));
echo "Enter end date (YYYY-MM-DD): ";
$project->end_date = trim(fgets(STDIN));
echo "Enter status: ";
$project->status = trim(fgets(STDIN));
$project->create();
break;
case '2':
echo "Enter project ID: ";
$task->project_id = intval(trim(fgets(STDIN)));
echo "Enter task name: ";
$task->name = trim(fgets(STDIN));
echo "Enter task description: ";
$task->description = trim(fgets(STDIN));
echo "Enter start date (YYYY-MM-DD): ";
$task->start_date = trim(fgets(STDIN));
echo "Enter end date (YYYY-MM-DD): ";
$task->end_date = trim(fgets(STDIN));
echo "Enter status: ";
$task->status = trim(fgets(STDIN));
$task->create();
break;
case '3':
echo "Enter task ID: ";
$resource->task_id = intval(trim(fgets(STDIN)));
echo "Enter resource name: ";
$resource->name = trim(fgets(STDIN));
echo "Enter quantity: ";
$resource->quantity = intval(trim(fgets(STDIN)));
echo "Enter unit: ";
$resource->unit = trim(fgets(STDIN));
$resource->create();
break;
case '4':
$projects = $project->read();
while ($row = $projects->fetch_assoc()) {
echo "ID: " . $row['id'] . " Name: " . $row['name'] . " Start Date: " . $row['start_date'] . " End Date: " . $row['end_date'] . " Status: " . $row['status'] . "\n";
}
break;
case '5':
$tasks = $task->read();
while ($row = $tasks->fetch_assoc()) {
echo "ID: " . $row['id'] . " Project ID: " . $row['project_id'] . " Name: " . $row['name'] . " Description: " . $row['description'] . " Start Date: " . $row['start_date'] . " End Date: " . $row['end_date'] . " Status: " . $row['status'] . "\n";
}
break;
case '6':
$resources = $resource->read();
while ($row = $resources->fetch_assoc()) {
echo "ID: " . $row['id'] . " Task ID: " . $row['task_id'] . " Name: " . $row['name'] . " Quantity: " . $row['quantity'] . " Unit: " . $row['unit'] . "\n";
}
break;
case '7':
echo "Enter project ID: ";
$project->id = intval(trim(fgets(STDIN)));
echo "Enter project name: ";
$project->name = trim(fgets(STDIN));
echo "Enter start date (YYYY-MM-DD): ";
$project->start_date = trim(fgets(STDIN));
echo "Enter end date (YYYY-MM-DD): ";
$project->end_date = trim(fgets(STDIN));
echo "Enter status: ";
$project->status = trim(fgets(STDIN));
$project->update();
break;
case '8':
echo "Enter task ID: ";
$task->id = intval(trim(fgets(STDIN)));
echo "Enter project ID: ";
$task->project_id = intval(trim(fgets(STDIN)));
echo "Enter task name: ";
$task->name = trim(fgets(STDIN));
echo "Enter task description: ";
$task->description = trim(fgets(STDIN));
echo "Enter start date (YYYY-MM-DD): ";
$task->start_date = trim(fgets(STDIN));
echo "Enter end date (YYYY-MM-DD): ";
$task->end_date = trim(fgets(STDIN));
echo "Enter status: ";
$task->status = trim(fgets(STDIN));
$task->update();
break;
case '9':
echo "Enter resource ID: ";
$resource->id = intval(trim(fgets(STDIN)));
echo "Enter task ID: ";
$resource->task_id = intval(trim(fgets(STDIN)));
echo "Enter resource name: ";
$resource->name = trim(fgets(STDIN));
echo "Enter quantity: ";
$resource->quantity = intval(trim(fgets(STDIN)));
echo "Enter unit: ";
$resource->unit = trim(fgets(STDIN));
$resource->update();
break;
case '10':
echo "Enter project ID: ";
$project->id = intval(trim(fgets(STDIN)));
$project->delete();
break;
case '11':
echo "Enter task ID: ";
$task->id = intval(trim(fgets(STDIN)));
$task->delete();
break;
case '12':
echo "Enter resource ID: ";
$resource->id = intval(trim(fgets(STDIN)));
$resource->delete();
break;
case '13':
exit;
default:
echo "Invalid choice. Please try again.\n";
}
}
}
main();
?>
Conclusion
Ce code constitue une base pour un système complexe de gestion de projet pour un manœuvre dans le secteur du bâtiment. Il permet de gérer les projets, les tâches, et les ressources associées, ainsi que de créer, lire, mettre à jour et supprimer des enregistrements. Pour une application réelle, il faudrait ajouter des fonctionnalités supplémentaires, sécuriser l’application, améliorer l’interface utilisateur, et intégrer une gestion avancée des données.