Created
July 29, 2023 21:55
-
-
Save vihugarcia/8c90d21ea7a351af582161923238e7ce to your computer and use it in GitHub Desktop.
Chapter 11 EmployeeController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\controllers; | |
use SimpleMVC\core\Controller as Controller; | |
use App\models\Employee as Employee; | |
use SimpleMVC\core\View as View; | |
use SimpleMVC\widgets\DataTable as DataTable; | |
use SimpleMVC\core\db as db; | |
class EmployeeController extends Controller { | |
private $employee; | |
private $view; | |
private $db; | |
public function __construct(Employee $employee, View $view) | |
{ | |
$this->employee = $employee; | |
$this->view = $view; | |
} | |
public function index() | |
{ | |
$dataTable = new DataTable(['firstname', 'lastname', 'type', 'id'], 'id', 'employee'); | |
$this->view->setAction("index"); | |
$this->view->set('grid', $dataTable); | |
$scripts = <<<scripts | |
$( document ).ready(function() { | |
table = $('#data-table').DataTable({ | |
"ajax":{ | |
url :"/employee/data", // json datasource | |
type: "post" // type of method, GET/POST/DELETE | |
}, | |
columns: [ | |
{"data": 0},{"data": 1},{"data": 2}, { | |
data: null, | |
orderable: false, | |
searchable: false, | |
className: "center", | |
render: function (data, type, full, meta) { | |
return '<a href="/employee/view/'+data[3]+'" title="View">View</a>' + ' ' + | |
'<a href="/employee/edit/'+data[3]+'" title="Edit">Edit</a>' + ' ' + | |
'<a href="javascript: void(0);" title="Delete" onclick="del('+data[3]+')">Delete</a>'; | |
} | |
} | |
] | |
}); | |
}); | |
scripts; | |
return $this->view->render(true, $scripts); | |
} | |
public function data() | |
{ | |
$params = $_REQUEST; | |
$where = ""; | |
$columns = array( | |
0 =>'firstname', | |
1 =>'lastname', | |
2 =>'type', | |
3 =>'id' | |
); | |
$data = []; | |
$queryTot = $queryRec = "SELECT firstname, lastname, type, id FROM employees"; | |
// check search value exist | |
if( !empty($params['search']['value']) ) { | |
$where .=" WHERE "; | |
$where .=" ( firstname LIKE '".$params['search']['value']."%' "; | |
$where .=" OR lastname LIKE '".$params['search']['value']."%' "; | |
$where .=" OR type LIKE '".$params['search']['value']."%' )"; | |
$queryTot .= $where; | |
$queryRec .= $where; | |
} | |
if( !empty($params['order'][0]) ) { | |
$queryRec .= " ORDER BY ". $columns[$params['order'][0]['column']]." ".$params['order'][0]['dir']." LIMIT ".$params['start']." ,".$params['length']." "; | |
} | |
$this->db = new db(CONFIG); | |
$employees = $this->db->query($queryRec); | |
foreach ($employees as $employee) { | |
foreach ($employee as $key => $value) { | |
$item[] = $value; | |
} | |
$data[] = $item; | |
$item = []; | |
} | |
$totalRecords = count($this->db->query($queryTot)); | |
$draw = ( !empty($params['draw']) ) ? intval( $params['draw'] ) : false; | |
$json_data = array( | |
"draw" => $draw, | |
"recordsTotal" => intval( $totalRecords ), | |
"recordsFiltered" => intval($totalRecords), | |
"data" => $data // total data array | |
); | |
echo json_encode($json_data); // send data as json format | |
exit; | |
} | |
public function view($id) | |
{ | |
$employee = $this->employee->loadModel($id); | |
if ($employee) { | |
$this->view->setAction('view'); | |
$this->view->set('employee', $employee); | |
$this->view->render(); | |
} | |
} | |
public function add() | |
{ | |
if ($_SERVER["REQUEST_METHOD"] == 'POST') { | |
$data = [ | |
'firstname' => $_POST["firstname"], | |
'lastname' => $_POST["lastname"], | |
'type' => $_POST["type"] | |
]; | |
$this->employee->load($data); | |
$this->employee->save(); | |
header('Location: ' . SITE_BASE . 'employee/index/'); | |
exit; | |
} | |
$this->view->setAction('add'); | |
$this->view->set('employee', $this->employee); | |
$this->view->render(); | |
} | |
public function edit($id) | |
{ | |
if ($_SERVER["REQUEST_METHOD"] == 'POST') { | |
$data = [ | |
'firstname' => $_POST["firstname"], | |
'lastname' => $_POST["lastname"], | |
'type' => $_POST["type"] | |
]; | |
$this->employee->load($data); | |
$this->employee->save($id); | |
header('Location: ' . SITE_BASE . 'employee/index/'); | |
exit; | |
} | |
$this->employee->loadModel($id); | |
$this->view->setAction('edit'); | |
$this->view->set('employee', $this->employee); | |
$this->view->render(); | |
} | |
public function delete() | |
{ | |
if (!isset($_REQUEST["id"])) { | |
echo "error"; | |
} else { | |
if ($this->employee->del("id", $_REQUEST["id"])) { | |
echo "success"; | |
} else { | |
echo "error"; | |
} | |
} | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment