Application de gestion d'employés ب PHP MVC و PDO الجزء الرابع

imadbelasri PHP
PH

فهاد الجزء الرابع من Application de gestion d'employés ب PHP MVC و PDO غادي نزيدو الملفات الخاصة بإضافة تعديل وحذف مستخدم  وأيضا الصفحة الرئيسية لي غادي نعرضوا فيها les employés كاملين.


نظرة سريعة بالفيديو


1- إضافة الملف add.php

غادي نمشي ل dossier views غادي نزيد ملف جديد سميه add.php فيه غادي تكون فورم الإضافة ولي غادي نرسلوا المعلومات لي استرجعنا منها ل fonction addEmployes لي عندنا ف EmployesController.

الكود لي غادي تزيد فل fichier هو :

                                                    
                                                        //
<?php 
	if(isset($_POST['submit'])){
		$newEmploye = new EmployesController();
		$newEmploye->addEmploye();
	}
?>
<div class="container">
	<div class="row my-4">
		<div class="col-md-8 mx-auto">
			<div class="card">
				<div class="card-header">Ajouter un employé</div>
				<div class="card-body bg-light">
					<a href="<?php echo BASE_URL;?>" class="btn btn-sm btn-secondary mr-2 mb-2">
						<i class="fas fa-home"></i>
					</a>
					<form method="post">
						<div class="form-group">
							<label for="nom">Nom*</label>
							<input type="text" name="nom" class="form-control" placeholder="Nom">
						</div>
						<div class="form-group">
							<label for="prenom">Prénom*</label>
							<input type="text" name="prenom" class="form-control" placeholder="Prénom">
						</div>
						<div class="form-group">
							<label for="mat">Matricule*</label>
							<input type="text" name="mat" class="form-control" placeholder="Matricule">
						</div>
						<div class="form-group">
							<label for="depart">Département*</label>
							<input type="text" name="depart" class="form-control" placeholder="Département">
						</div>
						<div class="form-group">
							<label for="poste">Poste*</label>
							<input type="text" name="poste" class="form-control" placeholder="Poste">
						</div>
						<div class="form-group">
							<label for="date_emb">Date Embauche*</label>
							<input type="date" name="date_emb" class="form-control">
						</div>
						<div class="form-group">
							<select class="form-control" name="statut">
								<option value="1">Active</option>
								<option value="0">Résilié</option>
							</select>
						</div>
						<div class="form-group">
							<button type="submit" class="btn btn-primary" name="submit">Valider</button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
                                                    
                                                

2- إضافة الملف update.php

دائما ف dossier views غادي نزيد ملف جديد سميه update.php فيه غادي تكون فورم التعديل ولي كتستقبل ل id ديال ل employe لي غادي نديرو عليه تعديل.

منبعد غادي نرسلوا المعلومات لي استرجعنا منها ل fonction updateEmployes لي عندنا ف EmployesController.

الكود لي غادي تزيد فل fichier هو :

                                                        
                                                            //
<?php 
	if(isset($_POST['id'])){
		$exitEmploye = new EmployesController();
		$employe = $exitEmploye->getOneEmploye();
	}else{
		Redirect::to('home');
	}
	if(isset($_POST['submit'])){
		$exitEmploye = new EmployesController();
		$exitEmploye->updateEmploye();
	}
?>
<div class="container">
	<div class="row my-4">
		<div class="col-md-8 mx-auto">
			<div class="card">
				<div class="card-header">Modifier un employé</div>
				<div class="card-body bg-light">
					<a href="<?php echo BASE_URL;?>" class="btn btn-sm btn-secondary mr-2 mb-2">
						<i class="fas fa-home"></i>
					</a>
					<form method="post">
						<div class="form-group">
							<label for="nom">Nom*</label>
							<input type="text" name="nom" class="form-control" placeholder="Nom"
							value="<?php echo $employe->nom; ?>"
							>
						</div>
						<div class="form-group">
							<label for="prenom">Prénom*</label>
							<input type="text" name="prenom" class="form-control" placeholder="Prénom"
							value="<?php echo $employe->prenom; ?>"
							>
						</div>
						<div class="form-group">
							<label for="mat">Matricule*</label>
							<input type="text" name="mat" class="form-control" placeholder="Matricule"
								value="<?php echo $employe->matricule; ?>">
						</div>
						<div class="form-group">
							<label for="depart">Département*</label>
							<input type="text" name="depart" class="form-control" placeholder="Département"
							value="<?php echo $employe->depart; ?>">
							<input type="hidden" name="id" value="<?php echo $employe->id;?>">
						</div>
						<div class="form-group">
							<label for="poste">Poste*</label>
							<input type="text" name="poste" class="form-control" placeholder="Poste"
							value="<?php echo $employe->poste; ?>">
						</div>
						<div class="form-group">
							<label for="date_emb">Date Embauche*</label>
							<input type="date" name="date_emb" class="form-control">
						</div>
						<div class="form-group">
							<select class="form-control" name="statut">
								<option value="1" <?php echo $employe->statut ? 'selected' : ''; ?>>Active</option>
								<option value="0"
								<?php echo !$employe->statut ? 'selected' : ''; ?>
								>Résilié</option>
							</select>
						</div>
						<div class="form-group">
							<button type="submit" class="btn btn-primary" name="submit">Valider</button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
                                                        
                                                    

3- إضافة الملف delete.php

دائما ف dossier views غادي نزيد ملف جديد سميه delete.php فيه غادي نستقبل ل id ديال ل employe لي غادي نمسح منبعد كنفذ ل fonction deleteEmploye لي عندنا ف EmployesController.

الكود لي غادي تزيد فل fichier هو :

                                                        
                                                            //
<?php 
	if(isset($_POST['id'])){
		$exitEmploye = new EmployesController();
		$exitEmploye->deleteEmploye();
	}
?>
                                                        
                                                    

4- إضافة الملف home.php

دائما ف dossier views غادي نزيد ملف جديد سميه home.php فيه غادي نعرض les employés  لي عندي كاملين لي كنسترجعهم بل fonction getAllEmployes لي  عندنا ف EmployesController.

عندي أيضا خاصية البحث ولي كتنفذ فيها ل fonction findEmployes لي كتاخذ قيمة البحث لي دخلنا فالحقل وكترجع les employés لي كيوافقوا قيمة البحث.

الكود لي غادي تزيد فل fichier هو :

                                                        
                                                            //
<?php 
	if(isset($_POST['find'])){
		$data = new EmployesController();
		$employes = $data->findEmployes();
	}else{
		$data = new EmployesController();
		$employes = $data->getAllEmployes();
	}
?>
<div class="container">
	<div class="row my-4">
		<div class="col-md-10 mx-auto">
			<?php include('./views/includes/alerts.php');?>
			<div class="card">
				<div class="card-body bg-light">
					<a href="<?php echo BASE_URL;?>add" class="btn btn-sm btn-primary mr-2 mb-2">
						<i class="fas fa-plus"></i>
					</a>
					<a href="<?php echo BASE_URL;?>" class="btn btn-sm btn-secondary mr-2 mb-2">
						<i class="fas fa-home"></i>
					</a>
					<a href="<?php echo BASE_URL;?>logout" title="Déconnexion" class="btn btn-link mr-2 mb-2">
						<i class="fas fa-user mr-2"> <?php echo $_SESSION['username'];?></i>
					</a>
					<form method="post" class="float-right mb-2 d-flex flex-row">
						<input type="text" class="form-control" name="search" placeholder="Recherche">
						<button class="btn btn-info btn-sm" name="find" type="submit"><i class="fas fa-search"></i></button>
					</form>
					<table class="table table-hover">
					  <thead>
					    <tr>
					      <th scope="col">Nom & Prénom</th>
					      <th scope="col">Matricule</th>
					      <th scope="col">Département</th>
					      <th scope="col">Poste</th>
					      <th scope="col">Date Embauche</th>
					      <th scope="col">Statut</th>
					      <th scope="col">Action</th>
					    </tr>
					  </thead>
					  <tbody>
					    <?php foreach($employes as $employe):?>
					    	<tr>
						      <th scope="row"><?php echo $employe['nom'].' '.$employe['prenom']; ?></th>
						      <td><?php echo $employe['matricule'];?></td>
						      <td><?php echo $employe['depart'];?></td>
						      <td><?php echo $employe['poste'];?></td>
						      <td><?php echo $employe['date_emb'];?></td>
						      <td>
						      	<?php echo $employe['statut']
						      			?
						      			'<span class="badge badge-success">Active</span>'
						      			:
						      			'<span class="badge badge-danger">Resilié</span>';
						      ;?></td>
						      <td class="d-flex flex-row">
						      	<form method="post" class="mr-1" action="update">
						      		<input type="hidden" name="id" value="<?php echo $employe['id'];?>">
						      		<button class="btn btn-sm btn-warning"><i class="fa fa-edit"></i></button>
						      	</form>
						      	<form method="post" class="mr-1" action="delete">
						      		<input type="hidden" name="id" value="<?php echo $employe['id'];?>">
						      		<button class="btn btn-sm btn-danger"><i class="fa fa-trash"></i></button>
						      	</form>
						      </td>
						    </tr>
					   	<?php endforeach;?>
					  </tbody>
					</table>
				</div>
			</div>
		</div>
	</div>
</div>
                                                        
                                                    

5- إضافة الملف .htaccess

كيف شفنا زدنا route ?page باش نسترجعوا les pages ديالنا أيضا خدامين ب index.php فل url فباش نحيدهم ونرجع ل url على هاد الشكل :

localhost/gestion-employes/home

غادي نزيدو fichier ف dossier ديال ل projet سميه htaccess. ولي غادي يكون الدور ديالو هو إعادة كتابة الروابط ديالنا.

الكود لي غادي تزيد فل fichier هو :

                                                        
                                                            //
Options All -Indexes

RewriteEngine On

RewriteBase /gestion-employes-chaine/

RewriteRule ^([-a-zA-Z0-9]+)$ index.php?page=$1