0% found this document useful (0 votes)
34 views17 pages

LAB11 PHP Ma026

The document describes an MVC implementation for an employee information and leave management system. It includes models for accessing employee and leave data from a database, controllers for handling requests and displaying views, and basic views for listing, adding, editing employees and leaves. The models define methods for common database operations like selecting, inserting, updating and deleting records. The controllers handle requests and display the appropriate views. This allows managing employee data and leave requests with basic CRUD functionality through an MVC pattern.

Uploaded by

kronza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views17 pages

LAB11 PHP Ma026

The document describes an MVC implementation for an employee information and leave management system. It includes models for accessing employee and leave data from a database, controllers for handling requests and displaying views, and basic views for listing, adding, editing employees and leaves. The models define methods for common database operations like selecting, inserting, updating and deleting records. The controllers handle requests and display the appropriate views. This allows managing employee data and leave requests with basic CRUD functionality through an MVC pattern.

Uploaded by

kronza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

PRACTICAL-11

MVC
Que-1.
• Implement simple employee information management functionality using MVC. Manage Employee
Data like Employee ID, name, department.Provide insert, view, update and delete functionality using
appropriate design
• Also Manage Leaves Taken by Employee: id, emp-id, leave date, leave reason, leave-status. Provide
facility to request leave, approve disapprove leave and list leave records.
File Strucutre:

Code:

1 MA026
Index.php:
<?php

require_once './Controller/EmployeeController.php';

use lab11_php_ma026\Controller\EmployeeController;

EmployeeController::All();

Config.php:
<?php

class Config
{
protected const HOST = "localhost";
protected const DATABASE = "employee_leave";
protected const USERNAME = "root";
protected const PASSWORD = "";
protected const PORT = 3308;
}

Model\Database.php
<?php
require_once __DIR__ . '/../config.php';

use LAB11\Config;
use PDO;
use PDOException;

class Database extends Config


{
private static $pdo;

private function __construct()


{
}

public static function getInstance()


{
if (!isset(self::$pdo)) {
try {
$dsn = "mysql:host=" . self::HOST . ";port=" .
self::PORT . ";dbname=" . self::DATABASE;
self::$pdo = new PDO($dsn, self::USERNAME,
self::PASSWORD);
self::$pdo->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
}

2 MA026
return self::$pdo;
}
}

Model\EmployeeModel.php
<?php

require_once __DIR__ . '/./Database.php';

use PDO;
use PDOException;

class EmployeeModel
{
private function __construct()
{
}

public static function All(): array


{
try {
$pdo = Database::getInstance();
$query = "SELECT * FROM Employee";
$stmt = $pdo->query($query);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Error fetching employees: " . $e->getMessage());
}
}

public static function Insert($name, $department)


{
try {
$pdo = Database::getInstance();
$query = "INSERT INTO Employee (name, department) VALUES
(:name, :department)";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':department', $department);
return $stmt->execute();
} catch (PDOException $e) {
die("Error inserting employee: " . $e->getMessage());
}
}

public static function ViewById($empid)


{
try {
$pdo = Database::getInstance();
$query = "SELECT * FROM Employee WHERE empid = :empid";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':empid', $empid);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
3 MA026
} catch (PDOException $e) {
die("Error fetching employee by ID: " . $e->getMessage());
}
}

public static function Update($empid, $name, $department)


{
try {
$pdo = Database::getInstance();
$query = "UPDATE Employee SET name = :name, department =
:department WHERE empid = :empid";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':empid', $empid);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':department', $department);
return boolval($stmt->execute());
} catch (PDOException $e) {
die("Error updating employee: " . $e->getMessage());
}
}

public static function Delete($empid)


{
try {
$pdo = Database::getInstance();
$query = "DELETE FROM Employee WHERE empid = :empid";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':empid', $empid);
return $stmt->execute();
} catch (PDOException $e) {
die("Error deleting employee: " . $e->getMessage());
}
}
}

Model\LeaveModel.php
<?php

use lab11_php_ma026\Model\Database;
use PDOException;

class LeaveModel
{
public function getAllLeaves()
{
try {
$pdo = Database::getInstance();
$query = "SELECT * FROM leaves";
$stmt = $pdo->query($query);
return $stmt->fetchAll();
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
}
4 MA026
public function requestLeave($data)
{
try {
$pdo = Database::getInstance();
$query = "INSERT INTO leaves (empid, leave_date,
leave_reason, leave_status) VALUES (?, ?, ?, ?)";
$stmt = $pdo->prepare($query);
$stmt->execute([$data['empid'], $data['leave_date'],
$data['leave_reason'], 'Pending']);
return true;
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
}

public function approveLeave($leaveId)


{
try {
$pdo = Database::getInstance();
$query = "UPDATE leaves SET leave_status = 'Approved' WHERE
leave_id = ?";
$stmt = $pdo->prepare($query);
$stmt->execute([$leaveId]);
return true;
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
}

public function disapproveLeave($leaveId)


{
try {
$pdo = Database::getInstance();
$query = "UPDATE leaves SET leave_status = 'Disapproved'
WHERE leave_id = ?";
$stmt = $pdo->prepare($query);
$stmt->execute([$leaveId]);
return true;
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
}
}

Controller\EmployeeController.php
<?php

require_once __DIR__ . '/../Model/EmployeeModel.php';


require_once __DIR__ . '/../Model/LeaveModel.php';
require_once __DIR__ . '/../Controller/LeaveController.php';

use LAB11\Controllers\LeaveController;

5 MA026
use LAB11\Model\EmployeeModel;

class EmployeeController extends EmployeeModel


{
private function __construct()
{
}
public static function All(): array
{
$employees = EmployeeModel::All();
require_once __DIR__ . '/../View/employee/list.php';
return [];
}
public static function Insert($name, $department)
{
if (EmployeeModel::Insert($name, $department)) {
echo <<<EOL
<script>
alert('record is Inserted...')
</script>
EOL;
} else
echo <<<EOL
<script>
alert('faild to Insert...')
</script>
EOL;
}
public static function ViewById($empid)
{
$employee = EmployeeModel::ViewById($empid);
require_once __DIR__ . '/../View/employee/view.php';
LeaveController::listLeaves();
}
public static function UpdateView($empid, $name, $department)
{
require_once __DIR__ . '/../View/employee/edit.php';
}
public static function Update($empid, $name, $department)
{
if (EmployeeModel::Update($empid, $name, $department)) {
echo <<<EOL
<script>
alert('record is upated...')
</script>
EOL;
} else
echo <<<EOL
<script>
alert('faild to update...')
</script>
EOL;
}
6 MA026
public static function Delete($empid)
{
if (EmployeeModel::Delete($empid)) {
echo <<<EOL
<script>
alert('record is deleted...')
</script>
EOL;
}
}
}
Controller\LeaveController

<?php

use lab11_php_ma026\Models\LeaveModel;

class LeaveController
{
public static function listLeaves()
{
$leaveModel = new LeaveModel();

$leaves = $leaveModel->getAllLeaves();

require_once __DIR__ . '/../View/leave/list.php';


}

public static function requestLeave($data)


{
$leaveModel = new LeaveModel();

$success = $leaveModel->requestLeave($data);

if ($success) {
header("Location: index.php?page=leave&action=list");
} else {
die("Failed to request leave.");
}
}

public static function approveLeave($leaveId)


{
$leaveModel = new LeaveModel();

$success = $leaveModel->approveLeave($leaveId);
if ($success) {
echo <<<EOL
<script>
alert('leave approved...')
</script>
EOL;
} else
echo <<<EOL
7 MA026
<script>
alert('leave rejected...')
</script>
EOL;
}

public static function disapproveLeave($leaveId)


{
$leaveModel = new LeaveModel();

$success = $leaveModel->disapproveLeave($leaveId);

if ($success) {
echo <<<EOL
<script>
alert('leave disapproved...')
</script>
EOL;
} else
echo <<<EOL
<script>
alert('failed to disapprove leave...')
</script>
EOL;
}
}

View\employee\list.php
<?php

use lab11_php_ma026\Controller\EmployeeController;
use lab11_php_ma026\Model\EmployeeModel;

if (isset($_POST['view'])) {
EmployeeController::ViewById($_POST['empid']);
}
if (isset($_POST['update'])) {
$empid = $_POST['empid'];
$employee = EmployeeModel::ViewById($_POST['empid']);
EmployeeController::UpdateView($empid, $employee['name'],
$employee['department']);
}
if (isset($_POST['delete'])) {
$empid = $_POST['empid'];
EmployeeController::Delete($empid);
}
if (isset($_POST['insert'])) {
header("location:./View/employee/add.php");
}

unset($_POST);
?>

8 MA026
<!DOCTYPE html>
<html>

<head>
<title>Employee List</title>
</head>

<body>
<h1>Employee List</h1>
<table>
<thead>
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($employees as $employee) : ?>
<tr>
<td><?php echo $employee['empid']; ?></td>
<td><?php echo $employee['name']; ?></td>
<td><?php echo $employee['department']; ?></td>
<td>
<table>
<tr>
<td>
<form action="index.php?page=employee&action=view" method="post">
<input type="hidden" name="empid" value="<?php echo $employee['empid'];
?>">
<button type="submit" name="view">View</button>
</form>
</td>
<td>
<form action="./" method="post">
<input type="hidden" name="empid" value="<?php echo
$employee['empid']; ?>">
<button type="submit" name="update">Update</button>
</form>

</td>
<td>
<form action="index.php?page=employee&action=delete" method="post">
<input type="hidden" name="empid" value="<?php echo
$employee['empid']; ?>">
<button type="submit" name="delete">Delete</button>
</form>
</td>
</tr>
</table>
</td>

9 MA026
</tr>
<?php endforeach; ?>
</tbody>
</table>
<form action="#" method="post">
<button type="submit" name="insert">Add Employee</button>
</form>
</body>

</html>

View\employee\view.php

<!DOCTYPE html>
<html>

<head>
<title>View Employee</title>
</head>

<body>
<h1>View Employee</h1>
<p><strong>Employee ID:</strong> <?php echo $employee['empid']; ?></p>
<p><strong>Name:</strong> <?php echo $employee['name']; ?></p>
<p><strong>Department:</strong> <?php echo $employee['department']; ?></p>
<a href="index.php?page=employee&action=edit&id=<?php echo $employee['empid'];
?>">Edit</a>
<a href="index.php?page=employee&action=delete&id=<?php echo $employee['empid'];
?>">Delete</a>
</body>

</html>

View\employee\Add.php
<?php

use lab11_php_ma026\Controller\EmployeeController;

require_once __DIR__ . '/../../Controller/EmployeeController.php';


if (isset($_POST['Insert'])) {
EmployeeController::Insert($_POST['name'], $_POST['department']);
}
?>
<!DOCTYPE html>
<html>

<head>
<title>Add Employee</title>
</head>

<body>

10 MA026
<h1>Add Employee</h1>
<form action="" method="post">
<input type="hidden" name="empid">

<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>

<label for="department">Department:</label>
<input type="text" id="department" name="department" required><br>
<button type="submit" name="Insert">Insert</button>
</form>
</body>

</html>
View\employee\edit.php
<?php

use lab11_php_ma026\Controller\EmployeeController;

if (isset($_POST['UpdateData'])) {
EmployeeController::Update($empid, $_POST['name'],
$_POST['department']);
}
?>
<!DOCTYPE html>
<html>

<head>
<title>Edit Employee</title>
</head>

<body>
<h1>Edit Employee</h1>
<form action="" method="post">
<input type="hidden" name="empid" value="<?php echo $empid; ?>">

<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php echo $name; ?>" required><br>

<label for="department">Department:</label>
<input type="text" id="department" name="department" value="<?php echo $department;
?>" required><br>
<button type="submit" name="UpdateData">UpdateData</button>
</form>
</body>

</html>

View\leave\list.php
<?php

11 MA026
use lab11_php_ma026\Controllers\LeaveController;

if (isset($_POST['approve'])) {
LeaveController::approveLeave($_POST['leave_id']);
}
if (isset($_POST['disapprove']))
LeaveController::disapproveLeave($_POST['leave_id']);

if (isset($_POST['request_leave']))
header("location:request.php");
?>
<!DOCTYPE html>
<html>

<head>
<title>Leave List</title>
</head>

<body>
<h1>Leave List</h1>
<table>
<thead>
<tr>
<th>Leave ID</th>
<th>Employee ID</th>
<th>Leave Date</th>
<th>Leave Reason</th>
<th>Leave Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($leaves as $leave) : ?>
<tr>
<td><?php echo $leave['leave_id']; ?></td>
<td><?php echo $leave['empid']; ?></td>
<td><?php echo $leave['leave_date']; ?></td>
<td><?php echo $leave['leave_reason']; ?></td>
<td><?php echo $leave['leave_status']; ?></td>
<td>
<table>
<tr>
<td>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="leave_id" value="<?php echo $leave['leave_id'];
?>">
<button type="submit" name="approve">Approve</button>
</form>
</td>
<td>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="leave_id" value="<?php echo $leave['leave_id'];
?>">
12 MA026
<button type="submit" name="disapprove">Disapprove</button>
</form>
</td>
</tr>
</table>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<button type="submit" name="request_leave">Request Leave</button>
</form>
</body>

</html>

View\leave\request.php
<!DOCTYPE html>
<html>

<head>
<title>Request Leave</title>
<!-- Include Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>

<body>
<div class="container mt-5">
<h1 class="text-center">Request Leave</h1>
<form action="index.php?page=leave&action=submit_request" method="post">
<!-- Leave request form fields -->
<div class="form-group">
<label for="leave_date">Leave Date:</label>
<input type="date" class="form-control" id="leave_date" name="leave_date" required>
</div>
<div class="form-group">
<label for="leave_reason">Leave Reason:</label>
<textarea class="form-control" id="leave_reason" name="leave_reason" rows="4"
required></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit Request</button>
</form>
</div>

<!-- Include Bootstrap JS and jQuery (optional) -->


<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>

</html>
13 MA026
Output:

Add Employee

List Employee

14 MA026
View Employee

Edit Employee

15 MA026
Delete Employee

After delete

Leave List

Approved Leave

16 MA026
Disapprove Leave

17 MA026

You might also like