352 lines
12 KiB
PHP
352 lines
12 KiB
PHP
<?php namespace App\Controllers;
|
|
|
|
use App\Libraries\Crud;
|
|
use App\Models\CSiCoCars_Model;
|
|
use App\Models\CSiCoCarEvents_Model;
|
|
|
|
|
|
class CSiCoCars extends CRUDController {
|
|
|
|
// Tab: CSiCoCars = ['caIDcar', 'caTarga', 'caDescrizione', 'caDataAssicurazione', 'caTipoGomme', 'caCreatedAt', 'caUpdatedAt', 'caStato', 'caDeletedAt'];
|
|
|
|
protected $crud;
|
|
|
|
function __construct() {
|
|
|
|
$params = [
|
|
'table' => 'CSiCoCars',
|
|
'dev' => false,
|
|
'fields' => $this->field_options(),
|
|
'form_title_add' => 'Add CSiCoCar',
|
|
'form_title_update' => 'Edit CSiCoCar',
|
|
'form_submit' => 'Add',
|
|
'table_title' => 'Pagina per la tabella CSiCoCars',
|
|
'form_submit_update' => 'Update',
|
|
'base' => '',
|
|
'UpdatedAt' => 'caUpdatedAt',
|
|
'DeletedAt' => 'caDeletedAt',
|
|
];
|
|
|
|
$this->crud = new Crud($params, service('request'));
|
|
}// __construct
|
|
|
|
protected function field_options() {
|
|
|
|
$fields = [];
|
|
$fields['caIDcar'] = ['label' => 'ID', 'helper' => 'Identificativo univoco del record nel database'];
|
|
$fields['caTarga'] = ['label' => 'Targa', 'helper' => 'Targa dell\'auto (max 10 caratteri)', 'required' => true];
|
|
$fields['caDescrizione'] = ['label' => 'Descrizione', 'helper' => 'Descrizione esaustiva della CSiCoCar', 'required' => true];
|
|
$fields['caDataAssicurazione'] = ['label' => 'Scadenza Assicurazione', 'helper' => 'Data di scadenza dell\'assicurazione', 'required' => true];
|
|
$fields['caTipoGomme'] = ['label' => 'Tipo Gomme', 'helper' => 'Tipologia delle gomme in uso', 'required' => true];
|
|
$fields['caStato'] = ['label' => 'Stato', 'helper' => 'Stato di disponibilità della CSiCoCar', 'required' => true];
|
|
$fields['caCreatedAt'] = ['label' => 'Created At', 'helper' => 'Data di creazione del record relativo nel database', 'only_edit'=> true];
|
|
$fields['caUpdatedAt'] = ['label' => 'Updated At', 'helper' => 'Timestamp dell\'ultimo aggiornamento del record nel database', 'type' => 'unset'];
|
|
$fields['caDeletedAt'] = ['label' => 'Deleted At', 'helper' => 'Data di cancellazione del record relativo nel database', 'type' => 'unset'];
|
|
|
|
return $fields;
|
|
}// field_options
|
|
|
|
|
|
public function index() { return $this->CRUD_view(['caIDcar', 'caTarga', 'caDescrizione', 'caDataAssicurazione', 'caTipoGomme', 'caStato', 'caCreatedAt', 'caUpdatedAt', 'caDeletedAt']); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
// Auto CRUD terminato
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function select($IDcar = null) {
|
|
helper(['tools','form']);
|
|
$data['tab'] = 'cars';
|
|
$model = new CSiCoCars_Model();
|
|
|
|
// Verifico sessione e livello:
|
|
if (!CheckLevel(1)) {
|
|
if (ENVIRONMENT === 'development') { session()->setFlashdata('warning', '/CSiCoCars/select -> Error code 001'); }
|
|
return redirect()->to('/dashboard');
|
|
}
|
|
|
|
// memorizzo la selezione
|
|
session()->set('filter_car', $IDcar);
|
|
|
|
// visualizzo gli eventi della carta selezionata:
|
|
return redirect()->to('/CSiCoCarEvents/show');
|
|
}//function select
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function add() {
|
|
helper(['tools','form']);
|
|
|
|
// Verifico sessione e livello:
|
|
if (!CheckLevel(1)) {
|
|
if (ENVIRONMENT === 'development') { session()->setFlashdata('warning', '/CSiCoCars/add -> Error code 001'); }
|
|
return redirect()->to('/dashboard');
|
|
}
|
|
|
|
// Ricavo livello del lavoratore (solo amministratori o superiori, possono editare tutti i campi)
|
|
$data['enable'] = $enable = CheckLevel(3, false);
|
|
|
|
// Solo amministratori possono inserire una nuova sim:
|
|
if (!$enable){
|
|
session()->setFlashdata('warning', 'Non si hanno i permessi per inserire una nuova auto aziendale.');
|
|
return redirect()->to('/dashboard/cars');
|
|
}
|
|
|
|
// preparo i dati
|
|
$data['tab'] = 'cars';
|
|
$data['add'] = true;
|
|
$data['title'] = 'Dettagli nuova Auto';
|
|
|
|
// Gestione POST
|
|
if ($this->request->getMethod() == 'post') {
|
|
// Regole per validare i campi introdotti nel form:
|
|
$rules = [
|
|
'Targa' => 'trim|required|min_length[7]|max_length[9]',
|
|
'DataAssicurazione' => 'trim|required',
|
|
'TipoGomme' => 'trim|required',
|
|
'Descrizione' => 'trim|required|min_length[8]',
|
|
//'Stato' => 'trim|required',
|
|
];
|
|
|
|
// analisi della validazione
|
|
if (! $this->validate($rules)) {
|
|
$data['validation'] = $this->validator;
|
|
session()->setFlashdata('warning', $data['validation']->listErrors());
|
|
|
|
}else{
|
|
// validazione passata
|
|
// Preparo i dati per l'inserimento
|
|
// Tab: CSiCoCars = ['caIDcar', 'caTarga', 'caDescrizione', 'caDataAssicurazione', 'caTipoGomme', 'caCreatedAt', 'caUpdatedAt', 'caStato', 'caDeletedAt'];
|
|
$newData = [
|
|
'caTarga' => $this->request->getVar('Targa'),
|
|
'caDataAssicurazione' => $this->request->getVar('DataAssicurazione'),
|
|
'caTipoGomme' => $this->request->getVar('TipoGomme'),
|
|
'caDescrizione' => $this->request->getVar('Descrizione'),
|
|
'caStato' => 'Disponibile',
|
|
];
|
|
|
|
$model = new CSiCoCars_Model();
|
|
if ($model->save($newData)) {
|
|
session()->setFlashdata('success', 'Record inserito con successo sul DB');
|
|
if(isset($_POST['crea_continua'])) {
|
|
// è stato premuto il secondo pulsante, quindi inserisco un secondo valore:
|
|
//return redirect()->to('/dashboard/orari');
|
|
} else {
|
|
return redirect()->to('/dashboard/cars');
|
|
}
|
|
} else {
|
|
session()->setFlashdata('danger', 'Errore durante l\'inserimento del record nel DB.');
|
|
}
|
|
|
|
}// analisi validazione
|
|
|
|
}// Gestione POST
|
|
|
|
// Show form
|
|
echo view('pages/form/CSiCoCars_form_add_edit', $data);
|
|
}//function add
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function edit($IDcar = null) {
|
|
helper(['tools','form']);
|
|
|
|
// Verifico dato introdotto:
|
|
if ($IDcar==null) {
|
|
if (ENVIRONMENT === 'development') { session()->setFlashdata('warning', '/CSiCoCars/edit -> Error code 001'); }
|
|
return redirect()->to('/dashboard');
|
|
}
|
|
|
|
// Verifico sessione e livello:
|
|
if (!CheckLevel(1)) {
|
|
if (ENVIRONMENT === 'development') { session()->setFlashdata('warning', '/CSiCoCars/edit -> Error code 002'); }
|
|
return redirect()->to('/dashboard');
|
|
}
|
|
|
|
// Ricavo livello del lavoratore (solo amministratori o superiori, possono editare tutti i campi)
|
|
$data['enable'] = $enable = CheckLevel(3, false);
|
|
|
|
// Solo amministratori possono editare una carta:
|
|
if (!$enable){
|
|
session()->setFlashdata('warning', 'Non si hanno i permessi per editare i dati di una carta di credito aziendale.');
|
|
return redirect()->to('/dashboard/cars');
|
|
}
|
|
|
|
// preparo i dati
|
|
$data['tab'] = 'cars';
|
|
$data['add'] = false;
|
|
$data['title'] = 'Dettagli dell\'auto aziendale';
|
|
|
|
// Ricavo il profilo dell'orario da editare
|
|
// Tab: CSiCoCars = ['caIDcar', 'caTarga', 'caDescrizione', 'caDataAssicurazione', 'caTipoGomme', 'caCreatedAt', 'caUpdatedAt', 'caStato', 'caDeletedAt'];
|
|
$model = new CSiCoCars_Model();
|
|
$data['car'] = $model->where('caIDcar', $IDcar)
|
|
->first();
|
|
|
|
// Verifico che esista il record dell'orario:
|
|
if (null==$data['car']) {
|
|
if (ENVIRONMENT === 'development') { session()->setFlashdata('warning', '/CSiCoCars/edit -> Error code 003'); }
|
|
return redirect()->to('/dashboard');
|
|
}
|
|
|
|
// Gestione POST
|
|
if ($this->request->getMethod() == 'post') {
|
|
// Regole per validare i campi introdotti nel form:
|
|
$rules = [
|
|
'Targa' => 'trim|required|min_length[7]|max_length[9]',
|
|
'DataAssicurazione' => 'trim|required',
|
|
'TipoGomme' => 'trim|required',
|
|
'Descrizione' => 'trim|required|min_length[8]',
|
|
'Stato' => 'trim|required',
|
|
];
|
|
|
|
// analisi della validazione
|
|
if (! $this->validate($rules)) {
|
|
$data['validation'] = $this->validator;
|
|
session()->setFlashdata('warning', $data['validation']->listErrors());
|
|
|
|
}else{
|
|
// validazione passata
|
|
// Preparo i dati per l'aggiornamento
|
|
// Tab: CSiCoCars = ['caIDcar', 'caTarga', 'caDescrizione', 'caDataAssicurazione', 'caTipoGomme', 'caCreatedAt', 'caUpdatedAt', 'caStato', 'caDeletedAt'];
|
|
$newData = [
|
|
'caIDcar' => $this->request->getVar('IDcar'),
|
|
'caTarga' => $this->request->getVar('Targa'),
|
|
'caDataAssicurazione' => $this->request->getVar('DataAssicurazione'),
|
|
'caTipoGomme' => $this->request->getVar('TipoGomme'),
|
|
'caDescrizione' => $this->request->getVar('Descrizione'),
|
|
'caStato' => $this->request->getVar('Stato'),
|
|
];
|
|
|
|
|
|
|
|
$model = new CSiCoCars_Model();
|
|
if ($model->save($newData)) {
|
|
session()->setFlashdata('success', 'Successfuly Updated');
|
|
return redirect()->to('/dashboard/cars');
|
|
} else {
|
|
session()->setFlashdata('danger', 'Errore durante l\'update del record nel DB.');
|
|
}
|
|
|
|
}// analisi validazione
|
|
|
|
}// Gestione POST
|
|
|
|
|
|
// Show form
|
|
echo view('pages/form/CSiCoCars_form_add_edit', $data);
|
|
}//function select
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function delete($IDcar = null){
|
|
helper(['tools']);
|
|
$data = [];
|
|
$model = new CSiCoCars_Model();
|
|
|
|
// Verifico sessione e livello:
|
|
if (!CheckLevel(3)) {
|
|
session()->setFlashdata('warning', 'Non si hanno i permessi per cancellare un\'auto aziendale.');
|
|
return redirect()->to('/dashboard/cars');
|
|
}
|
|
|
|
// Verifico dato introdotto:
|
|
if ($IDcar==null) {
|
|
if (ENVIRONMENT === 'development') { session()->setFlashdata('warning', '/CSiCoCars/delete -> Error code 001'); }
|
|
return redirect()->to('/dashboard');
|
|
}
|
|
|
|
// Elimino il record:
|
|
if ($model->delete($IDcar)) {
|
|
session()->setFlashdata('success', 'Recors cancellato correttamente.');
|
|
|
|
} else {
|
|
$data['validation'] = $this->validator;
|
|
session()->setFlashdata('warning', 'Errore durante la cancellazione del record.');
|
|
}
|
|
|
|
// Show profile
|
|
return redirect()->to('/dashboard/cars');
|
|
} // function delete($id)
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function export_csv(){
|
|
helper(['tools','form']);
|
|
|
|
// Verifico sessione e livello:
|
|
if (!CheckLevel(1)) {
|
|
if (ENVIRONMENT === 'development') { session()->setFlashdata('warning', '/CSiCoCars/export_csv -> Error code 001'); }
|
|
return redirect()->to('/Lavoratori/login');
|
|
}
|
|
|
|
// Ricavo le ore dell'utente:
|
|
// Tab: CSiCoCars = ['caIDcar', 'caTarga', 'caDescrizione', 'caDataAssicurazione', 'caTipoGomme', 'caCreatedAt', 'caUpdatedAt', 'caStato', 'caDeletedAt'];
|
|
$model = new CSiCoCars_Model();
|
|
|
|
// Costruisco la query per selezionare le ore da esportare:
|
|
// - applico eventuale filtro per lo stato:
|
|
|
|
// imposto l'ordinamento richiesto:
|
|
CheckSort('csicocars',true);
|
|
$data['sortCol'] = session()->get('sort')['col'];
|
|
$data['sortDir'] = session()->get('sort')['dir'];
|
|
$model->orderBy($data['sortCol'], $data['sortDir']);
|
|
|
|
// eseguo la query:
|
|
$data['dataset'] = $model->asArray()
|
|
->findAll();
|
|
|
|
// Imposto il nome del file da downloadare:
|
|
$filepath = $_SERVER['MRK_FILE_PATH'];
|
|
$filename = 'CSiCoDB_CSiCoCars_il_' . date('Y-m-d_H-i-s') . '.csv';
|
|
// file creation
|
|
$file = fopen($filepath . $filename,"w");
|
|
// Esport header
|
|
fputcsv($file,['ID Car', 'Targa', 'Descrizione', 'Data Assicurazione', 'Tipo Gomme', 'CreatedAt', 'UpdatedAt', 'Stato']);
|
|
|
|
foreach ($data['dataset'] as $line){
|
|
fputcsv($file,$line);
|
|
}
|
|
// Chiudo il file
|
|
fclose($file);
|
|
// download
|
|
header("Content-Description: File Transfer");
|
|
header("Content-Disposition: attachment; filename=".$filename);
|
|
header("Content-Type: application/csv; ");
|
|
readfile($filepath . $filename);
|
|
// deleting file
|
|
unlink($filepath . $filename);
|
|
exit();
|
|
} // function export_csv
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
}//class CSiCoCars
|