From a896b1ed5d127ff6a1c0ccd8df720262da0db236 Mon Sep 17 00:00:00 2001 From: JcarlosCabello1991 Date: Tue, 14 Jun 2022 17:55:54 +0200 Subject: [PATCH 1/2] dashboard and employee show data --- config/baseConstants.php | 20 +++++++++++ config/db.php | 6 ++++ controllers/employeeController.php | 50 ++++++++++++++++++++++++-- index.php | 24 ++++++++++++- models/employeeModel.php | 37 +++++++++++++++++++ models/helper/dbConnection.php | 19 ++++++++++ views/employee/employee.php | 35 ++++++++++++++++++ views/employee/employeeDashboard.php | 54 ++++++++++++++++++++++++++++ views/main/main.php | 14 +++++++- 9 files changed, 255 insertions(+), 4 deletions(-) create mode 100644 config/baseConstants.php create mode 100644 config/db.php create mode 100644 models/helper/dbConnection.php diff --git a/config/baseConstants.php b/config/baseConstants.php new file mode 100644 index 0000000..a12f0d5 --- /dev/null +++ b/config/baseConstants.php @@ -0,0 +1,20 @@ +prepare("SELECT e.id, e.name, e.email, g.name as 'gender', e.city, e.age, e.phone_number FROM employees e INNER JOIN genders g ON e.gender_id = g.id ORDER BY e.id ASC;"); + + try{ + $query->execute(); + $employees = $query->fetchAll(); + return $employees; + }catch(PDOException $e){ + return []; + } } function getById($id){ + $query = dbConnection()->prepare("SELECT e.id, e.name, e.email, g.name as 'gender', e.city, e.age, e.phone_number FROM employees e INNER JOIN genders g ON e.gender_id = g.id WHERE e.id=$id;"); + + try{ + $query->execute(); + $employee = $query->fetchAll(); + return $employee; + }catch(PDOException $e){ + return []; + } +} + +function setById($data){ + $query = dbConnection()->prepare("UPDATE employees SET name= ?,email= ?,gender_id=?, city= ?, age= ?, phone_number= ? WHERE id= ?;"); + $query->bindParam(1,$data[1]); + $query->bindParam(2,$data[2]); + $query->bindParam(3,$data[3]); + $query->bindParam(4,$data[4]); + $query->bindParam(5,$data[5]); + $query->bindParam(6,$data[6]); + $query->bindParam(7,$data[0]); + try{ + $query->execute(); + return [true]; + }catch(PDOException $e){ + return []; + } } \ No newline at end of file diff --git a/models/helper/dbConnection.php b/models/helper/dbConnection.php new file mode 100644 index 0000000..828f608 --- /dev/null +++ b/models/helper/dbConnection.php @@ -0,0 +1,19 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_EMULATE_PREPARES => FALSE, + ]; + + $pdo = new PDO($connection, USER, PASSWORD, $options); + + return $pdo; + }catch(PDOException $e){ + require_once(VIEWS . "error/error.php"); + } +} \ No newline at end of file diff --git a/views/employee/employee.php b/views/employee/employee.php index e69de29..0c5148d 100644 --- a/views/employee/employee.php +++ b/views/employee/employee.php @@ -0,0 +1,35 @@ + + + + + + + Document + + + +
+
" method="post"> + + " required> + " required> + + + + + " required> + " required> + " required> + + Cancel" + ?> +
+
+ + \ No newline at end of file diff --git a/views/employee/employeeDashboard.php b/views/employee/employeeDashboard.php index e69de29..ef1478e 100644 --- a/views/employee/employeeDashboard.php +++ b/views/employee/employeeDashboard.php @@ -0,0 +1,54 @@ + + + + + + + Document + + + + +

Employee Dashboard page!

+ +

+ + + + + + + + + + + + + + + $employee) { + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + } + ?> + +
IDNameEmailGenderCityAgePhone NumberActions
" . $employee["id"] . "" . $employee["name"] . "" . $employee["email"] . "" . $employee["gender"] . "" . $employee["city"] . "" . $employee["age"] . "" . $employee["phone_number"] . " + Edit + Delete +
+ Create + Back + + + \ No newline at end of file diff --git a/views/main/main.php b/views/main/main.php index fe1b309..da15f1a 100644 --- a/views/main/main.php +++ b/views/main/main.php @@ -1 +1,13 @@ - \ No newline at end of file + + + + + + + + Document + + + Employees + + \ No newline at end of file From c34173dfeabacf24ba537933090839aac70e05f0 Mon Sep 17 00:00:00 2001 From: JcarlosCabello1991 Date: Wed, 15 Jun 2022 16:35:58 +0200 Subject: [PATCH 2/2] hobbies --- assets/css/style.css | 17 +++++ controllers/employeeController.php | 51 ++++++++++++- controllers/travelController.php | 49 ++++++++++++ models/employeeModel.php | 110 +++++++++++++++++++++++++-- models/travelModel.php | 83 ++++++++++++++++++++ views/employee/employee.php | 36 ++++++--- views/employee/employeeDashboard.php | 9 ++- views/main/main.php | 4 +- views/travel/travel.php | 35 +++++++++ views/travel/travelDashboard.php | 53 +++++++++++++ 10 files changed, 425 insertions(+), 22 deletions(-) diff --git a/assets/css/style.css b/assets/css/style.css index e69de29..6dc0752 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -0,0 +1,17 @@ +.tg-0lax .img-size{ + height: 3rem; + width: 3rem; +} +.logo{ + height: 3rem; + width: 10rem; +} +.navBar{ + display: flex; + justify-content: space-between; + flex-direction: row; +} + +h4{ + margin-top: 1rem; +} \ No newline at end of file diff --git a/controllers/employeeController.php b/controllers/employeeController.php index 211b61e..6738bd0 100644 --- a/controllers/employeeController.php +++ b/controllers/employeeController.php @@ -54,17 +54,21 @@ function getEmployee($request) error("This employee doesnt exists"); } } - +// name, last_name, email, gender_id, avatar, age, phone_number, city, street_address, state, postal_code function setEmployee($request){ //Get the new data employee $newData = array(); array_push($newData, $request["id"]); array_push($newData, $_POST["name"]); + array_push($newData, $_POST["last_name"]); array_push($newData, $request["email"]); array_push($newData, $request["gender_id"]); - array_push($newData, $request["city"]); array_push($newData, $request["age"]); array_push($newData, $request["phone_number"]); + array_push($newData, $request["city"]); + array_push($newData, $_POST["street_address"]); + array_push($newData, $_POST["state"]); + array_push($newData, $_POST["postal_code"]); //Invoque the function to set the employee data $employee = setById($newData); @@ -72,6 +76,49 @@ function setEmployee($request){ getAllEmployees(); } +function deleteEmployee($request){ + delete($request["id"]); + + //Set the ID and id gender of each employee to show the in order in dashboard + $numOfEmployees = getNumEmployees(); + setIdEmployees($numOfEmployees); + setIdGender($numOfEmployees); + + //Show the dashboard + getAllEmployees(); +} + +function createEmployee(){ + require_once VIEWS . "/employee/employee.php"; +} + +function create($request){ + $newData = array(); + array_push($newData, $request["id"]); + array_push($newData, $_POST["name"]); + array_push($newData, $_POST["last_name"]); + array_push($newData, $request["email"]); + array_push($newData, $request["gender_id"]); + array_push($newData, $request["age"]); + array_push($newData, $request["phone_number"]); + array_push($newData, $request["city"]); + array_push($newData, $request["street_address"]); + array_push($newData, $request["state"]); + array_push($newData, $request["postal_code"]); + + //Invoque the function to set the employee data + $employee = createNewEmployee($newData); + $msg ="data setted"; + + //Set the ID and id gender of each employee to show the in order in dashboard + $numOfEmployees = getNumEmployees(); + setIdEmployees($numOfEmployees); + setIdGender($numOfEmployees); + + //Show the dashboard + getAllEmployees(); +} + /** * This function includes the error view with a message */ diff --git a/controllers/travelController.php b/controllers/travelController.php index e69de29..af8b6c0 100644 --- a/controllers/travelController.php +++ b/controllers/travelController.php @@ -0,0 +1,49 @@ +prepare("SELECT e.id, e.name, e.email, g.name as 'gender', e.city, e.age, e.phone_number FROM employees e INNER JOIN genders g ON e.gender_id = g.id ORDER BY e.id ASC;"); + $query = dbConnection()->prepare("SELECT e.id, e.name, e.email, g.name as 'gender', e.avatar, e.city, e.age, e.phone_number FROM employees e INNER JOIN genders g ON e.gender_id = g.id ORDER BY e.id ASC;"); try{ $query->execute(); @@ -14,8 +15,9 @@ function get(){ } } +//Function to get the employee by id and show his data on employee.php function getById($id){ - $query = dbConnection()->prepare("SELECT e.id, e.name, e.email, g.name as 'gender', e.city, e.age, e.phone_number FROM employees e INNER JOIN genders g ON e.gender_id = g.id WHERE e.id=$id;"); + $query = dbConnection()->prepare("SELECT e.id, e.name, e.last_name, e.email, g.name as 'gender',e.age,e.phone_number, e.city, e.street_address, e.state, e.postal_code FROM employees e INNER JOIN genders g ON e.gender_id = g.id WHERE e.id=$id;"); try{ $query->execute(); @@ -26,15 +28,109 @@ function getById($id){ } } +//Function to set an employee to update their data function setById($data){ - $query = dbConnection()->prepare("UPDATE employees SET name= ?,email= ?,gender_id=?, city= ?, age= ?, phone_number= ? WHERE id= ?;"); - $query->bindParam(1,$data[1]); - $query->bindParam(2,$data[2]); + $query = dbConnection()->prepare("UPDATE employees SET name= ?,last_name=?,email= ?,gender_id=?, age= ?,phone_number= ?, city= ?,street_address=?,state=?,postal_code=? WHERE id= ?;"); + + $query->bindParam(1, $data[1]); + $query->bindParam(2, $data[2]); $query->bindParam(3,$data[3]); $query->bindParam(4,$data[4]); $query->bindParam(5,$data[5]); - $query->bindParam(6,$data[6]); - $query->bindParam(7,$data[0]); + $query->bindParam(6, $data[6]); + $query->bindParam(7, $data[7]); + $query->bindParam(8, $data[8]); + $query->bindParam(9,$data[9]); + $query->bindParam(10,$data[10]); + $query->bindParam(11,$data[0]); + + try{ + $query->execute(); + return [true]; + }catch(PDOException $e){ + return []; + } +} + +//function that gets a list of the employees ids +function getNumEmployees(){ + $query = dbConnection()->prepare("SELECT e.id FROM employees e ORDER BY e.id ASC;"); + try{ + $query->execute(); + $numEmployees = $query->fetchAll(); + return $numEmployees; + }catch(PDOException $e){ + return []; + } +} + +//Set the employees ID to show them in dashboard in order +function setIdEmployees($n){ + for($i=0; $iprepare("UPDATE employees SET id=? WHERE id=?;"); + $index = $i+1; + $index1 = $n[$i][0]; + $query->bindParam(1, $index); + $query->bindParam(2, $index1); + try{ + $query->execute(); + if($i == (count($n)-1)){ + return[true]; + } + }catch(PDOException $e){ + return []; + } + } +} + +//Set the employees ID gender to show them in dashboard in order +function setIdGender($n){ + for($i=0; $iprepare("UPDATE genders SET id=? WHERE id=?;"); + $index = $i+1; + $index1 = $n[$i][0]; + $query->bindParam(1, $index); + $query->bindParam(2, $index1); + try{ + $query->execute(); + if($i == count($n)-1){ + return[true]; + } + }catch(PDOException $e){ + return []; + } + } +} + +//function to delete the user who has the variable $id +function delete($id){ + $query = dbConnection()->prepare("DELETE FROM employees WHERE id=?;"); + $query->bindParam(1, $id); + try{ + $query->execute(); + return [true]; + }catch(PDOException $e){ + return []; + } +} +//Function to create a new employee with the values the user inserts on the employee.php form +function createNewEmployee($data){ + $urlAvatar="https://m.media-amazon.com/images/M/MV5BMzI5NDIzNTQ1Nl5BMl5BanBnXkFtZTgwMjQ0Mzc1MTE@._V1_UY256_CR4,0,172,256_AL_.jpg"; + + $query=dbConnection()->prepare("INSERT INTO employees (name,last_name, email, gender_id,avatar, age,phone_number,city,street_address,state,postal_code) VALUES (?, ?, ?, ?, ?, ?,?,?,?,?,?);"); + + $query->bindParam(1, $data[1]); + $query->bindParam(2, $data[2]); + $query->bindParam(3,$data[3]); + $query->bindParam(4,$data[4]); + $query->bindParam(5,$urlAvatar); + $query->bindParam(6,$data[5]); + $query->bindParam(7, $data[6]); + $query->bindParam(8, $data[7]); + $query->bindParam(9, $data[8]); + $query->bindParam(10,$data[9]); + $query->bindParam(11,$data[10]); + try{ $query->execute(); return [true]; diff --git a/models/travelModel.php b/models/travelModel.php index e69de29..2eef388 100644 --- a/models/travelModel.php +++ b/models/travelModel.php @@ -0,0 +1,83 @@ +prepare("SELECT e.id, e.name, e.last_name, h.name as 'hobbieName', h.type FROM employees e LEFT JOIN hobbies h ON e.id=h.id ORDER BY e.id ASC;"); + try{ + $query->execute(); + $hobbiesEmployees = $query->fetchAll(); + return $hobbiesEmployees; + }catch(PDOException $e){ + return []; + } +} + +//Function to get Hobbies to show them on travel.php +function getHobbiesEmployee($id){ + + $index = $id["id"]; + $query=dbConnection()->prepare("SELECT e.id, e.name, e.last_name, h.name as 'hobbieName', h.type FROM employees e LEFT JOIN hobbies h ON e.id=h.id WHERE e.id = ?;"); + $query->bindParam(1, $index); + try{ + $query->execute(); + $hobbiesEmployee = $query->fetchAll(); + return $hobbiesEmployee; + }catch(PDOException $e){ + return []; + } +} + +//Function to create or updates hobbies +function setHobbieEmployee($hobbieEmployee){ + $index= $hobbieEmployee[0]; + $index1=$hobbieEmployee[3]; + $index2=$hobbieEmployee[4]; + + $nHobbies = getNumEmployeesHobbies(); + $flag = false;//Create a new hobbie + + for($i = 0; $i < count($nHobbies); $i++){ + if(in_array($index,$nHobbies[$i])){ + $flag = true;//Update + $i = count($nHobbies); + } + } + + if(!$flag){ + $query=dbConnection()->prepare("INSERT INTO hobbies (id, name, type) VALUES (?, ?, ?);"); + $query->bindParam(1, $index); + $query->bindParam(2, $index1); + $query->bindParam(3, $index2); + try{ + $query->execute(); + $hobbiesEmployee = $query->fetchAll(); + return $hobbiesEmployee; + }catch(PDOException $e){ + return []; + } + }else{ + $query=dbConnection()->prepare("UPDATE hobbies SET name=?, type=? WHERE id=?;"); + $query->bindParam(3, $index); + $query->bindParam(1, $index1); + $query->bindParam(2, $index2); + try{ + $query->execute(); + $hobbiesEmployee = $query->fetchAll(); + return $hobbiesEmployee; + }catch(PDOException $e){ + return []; + } + } +} + +function getNumEmployeesHobbies(){ + $query=dbConnection()->prepare("SELECT id FROM hobbies;"); + try{ + $query->execute(); + $num = $query->fetchAll(); + return $num; + }catch(PDOException $e){ + return []; + } +} \ No newline at end of file diff --git a/views/employee/employee.php b/views/employee/employee.php index 0c5148d..1bd6648 100644 --- a/views/employee/employee.php +++ b/views/employee/employee.php @@ -8,22 +8,36 @@ +

-
" method="post"> + " method="post"> - " required> - " required> + + + - + + + + + + + + Document + -

Employee Dashboard page!

+ @@ -17,6 +22,7 @@ + @@ -31,6 +37,7 @@ $employee) { echo ""; + echo ""; echo ""; echo ""; echo ""; diff --git a/views/main/main.php b/views/main/main.php index da15f1a..7534fda 100644 --- a/views/main/main.php +++ b/views/main/main.php @@ -5,9 +5,11 @@ + Document - Employees + Employees + Hobbies \ No newline at end of file diff --git a/views/travel/travel.php b/views/travel/travel.php index e69de29..fb952a7 100644 --- a/views/travel/travel.php +++ b/views/travel/travel.php @@ -0,0 +1,35 @@ + + + + + + + Document + + + +
+ " method="post"> + + + + + + + + Cancel" + ?> + +
+ + \ No newline at end of file diff --git a/views/travel/travelDashboard.php b/views/travel/travelDashboard.php index e69de29..40211d1 100644 --- a/views/travel/travelDashboard.php +++ b/views/travel/travelDashboard.php @@ -0,0 +1,53 @@ + + + + + + + Document + + + + + + + +

+

AVATAR ID Name Email
" . $employee["id"] . "" . $employee["name"] . "" . $employee["email"] . "
+ + + + + + + + + + + + $employee) { + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + } + ?> + +
IDNameLast NameHobbyTypeACTIONS
" . $employee["id"] . "" . $employee["name"] . "" . $employee["last_name"] . "" . $employee["hobbieName"] . "" . $employee["type"] . " + Edit +
+ Back + + + \ No newline at end of file