Skip to content

Instantly share code, notes, and snippets.

@vihugarcia
Last active July 29, 2023 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vihugarcia/fda671b4285014a26db9e2ec5f0caaeb to your computer and use it in GitHub Desktop.
Save vihugarcia/fda671b4285014a26db9e2ec5f0caaeb to your computer and use it in GitHub Desktop.
Chapter 09 ClientController.php
<?php
namespace App\controllers;
use SimpleMVC\core\Controller as Controller;
use App\models\Client as Client;
use SimpleMVC\core\View as View;
use SimpleMVC\widgets\DataTable as DataTable;
use SimpleMVC\core\db as db;
class ClientController extends Controller {
private $client;
private $view;
private $db;
public function __construct(Client $client, View $view)
{
$this->client = $client;
$this->view = $view;
}
public function index()
{
$dataTable = new DataTable(['firstname', 'lastname', 'email', 'id'], 'id', 'client');
$this->view->setAction("index");
$this->view->set('grid', $dataTable);
$scripts = <<<scripts
$( document ).ready(function() {
table = $('#data-table').DataTable({
"ajax":{
url :"/client/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="/client/view/'+data[3]+'" title="View">View</a>' + ' ' +
'<a href="/client/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 =>'email',
3 =>'id'
);
$data = [];
$queryTot = $queryRec = "SELECT firstname, lastname, email, id FROM clients";
// 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 email 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);
$clients = $this->db->query($queryRec);
foreach ($clients as $client) {
foreach ($client 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)
{
$client = $this->client->loadModel($id);
if ($client) {
$this->view->setAction('view');
$this->view->set('client', $client);
$this->view->render();
}
}
public function add()
{
if ($_SERVER["REQUEST_METHOD"] == 'POST') {
$data = [
'firstname' => $_POST["firstname"],
'lastname' => $_POST["lastname"],
'email' => $_POST["email"]
];
$this->client->load($data);
$this->client->save();
header('Location: ' . SITE_BASE . 'client/index/');
exit;
}
$this->view->setAction('add');
$this->view->set('client', $this->client);
$this->view->render();
}
public function edit($id)
{
if ($_SERVER["REQUEST_METHOD"] == 'POST') {
$data = [
'firstname' => $_POST["firstname"],
'lastname' => $_POST["lastname"],
'email' => $_POST["email"]
];
$this->client->load($data);
$this->client->save($id);
header('Location: ' . SITE_BASE . 'client/index/');
exit;
}
$this->client->loadModel($id);
$this->view->setAction('edit');
$this->view->set('client', $this->client);
$this->view->render();
}
public function delete()
{
if (!isset($_REQUEST["id"])) {
echo "error";
} else {
if ($this->client->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