Start Point al 2026
This commit is contained in:
commit
8a76e1db3b
371 files changed
+160283
No files matched your search
@@ -0,0 +1,493 @@
|
||||
<?php namespace App\Controllers;
|
||||
|
||||
use App\Libraries\Crud;
|
||||
use App\Models\Lavoratori_Model;
|
||||
|
||||
|
||||
class Lavoratori extends CRUDController {
|
||||
|
||||
// Tab: Lavoratori = ['lvIDlavoratore', 'lvNome', 'lvCognome', 'lvMail', 'lvLevel', 'lvRimborsoKm', 'lvNickname', 'lvPassword', 'lvCreatedAt', 'lvUpdatedAt', 'lvDeletedAt'];
|
||||
|
||||
protected $crud;
|
||||
|
||||
function __construct() {
|
||||
|
||||
$params = [
|
||||
'table' => 'Lavoratori',
|
||||
'dev' => false,
|
||||
'fields' => $this->field_options(),
|
||||
'form_title_add' => 'Add Issue',
|
||||
'form_title_update' => 'Edit Issue',
|
||||
'form_submit' => 'Add',
|
||||
'table_title' => 'Pagina per la tabella Lavoratori',
|
||||
'form_submit_update' => 'Update',
|
||||
'base' => '',
|
||||
'UpdatedAt' => 'lvUpdatedAt',
|
||||
'DeletedAt' => 'lvDeletedAt',
|
||||
];
|
||||
|
||||
$this->crud = new Crud($params, service('request'));
|
||||
}// __construct
|
||||
|
||||
|
||||
protected function field_options() {
|
||||
|
||||
$fields = [];
|
||||
|
||||
$fields['lvIDlavoratore'] = ['label' => 'ID Lavoratore'];
|
||||
$fields['lvNome'] = ['label' => 'Nome', 'helper' => 'Nome del lavoratore (max 64 caratteri)', 'required' => true];
|
||||
$fields['lvCognome'] = ['label' => 'Cognome', 'helper' => 'Cognome del lavoratore (max 64 caratteri)', 'required' => true];
|
||||
$fields['lvMail'] = ['label' => 'email', 'helper' => 'Indirizzo mail del lavoratore (max 128 caratteri)', 'required' => true, 'unique' => [true, 'lvMail']];
|
||||
$fields['lvLevel'] = ['label' => 'Livello', 'helper' => 'Livello di accesso alla webapp: 0=Guest; 1=Oper; 2=Inven; 3=Admin; 4=Root', 'required' => true];
|
||||
$fields['lvRimborsoKm'] = ['label' => '€/Km', 'helper' => 'Rimborso chilometrico riconosciuto al lavoratore', 'required' => true];
|
||||
$fields['lvNickname'] = ['label' => 'Nickname', 'helper' => 'Nickname del lavoratore nella forma CSiCoCCN', 'required' => true, 'unique' => [true, 'lvNickname']];
|
||||
$fields['lvPassword'] = [
|
||||
'label' => 'Password',
|
||||
'required' => true,
|
||||
'only_add' => true,
|
||||
'type' => 'password',
|
||||
'confirm' => true,
|
||||
'password_hash' => true,
|
||||
'helper' => 'Password per il riconoscimento del lavoratore (min 8 caratteri)'
|
||||
];
|
||||
$fields['lvCreatedAt'] = ['label' => 'Created at', 'helper' => 'Data di creazione del record relativo nel database', 'only_edit'=> true];
|
||||
$fields['lvUpdatedAt'] = ['label' => 'Updated at', 'helper' => 'Timestamp dell\'ultimo aggiornamento del record nel database', 'type' => 'unset'];
|
||||
$fields['lvDeletedAt'] = ['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(['lvIDlavoratore', 'lvNickname', 'lvNome', 'lvCognome', 'lvMail', 'lvLevel', 'lvRimborsoKm', 'lvCreatedAt', 'lvUpdatedAt', 'lvDeletedAt']); }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// Auto CRUD terminato
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// funzione per reinizializzare un utente senza loggarsi, utile in caso di problemi per avere un nuovo amministratore e potersi collegare.
|
||||
// http://localhost/Lavoratori/reinizzializza/CSiCoAlm/4/password/127
|
||||
public function Reinizzializza($Nickname = 'CSiCoZZZ', $Level = '0', $Password = 'password', $CheckId = 0){
|
||||
if ($CheckId<>127) {
|
||||
// Se non siamo loggati
|
||||
session()->setFlashdata('danger', 'Reinizzializzazione del lavoratore non autorizzata.');
|
||||
return redirect()->to('/dashboard');
|
||||
}
|
||||
|
||||
// Ricavo l'ID del lavoratore indicato:
|
||||
$model = new Lavoratori_Model();
|
||||
$lav = $model->where('lvNickname', $Nickname)
|
||||
->first();
|
||||
if (!isset($lav)) {
|
||||
// Lavoratore non trovato
|
||||
session()->setFlashdata('danger', 'Lavoratore da reinizzializzare non trovato.');
|
||||
return redirect()->to('/dashboard');
|
||||
}
|
||||
|
||||
// Preparo i dati per l'aggiornamento
|
||||
$newData = [
|
||||
'lvIDlavoratore' => $lav->lvIDlavoratore,
|
||||
'lvLevel' => $Level,
|
||||
'lvPassword' => $Password,
|
||||
];
|
||||
|
||||
// Eseguo update del record
|
||||
if ($model->save($newData)) {
|
||||
session()->setFlashdata('success', 'Lavoratore reinizzializzato correttamente.');
|
||||
return redirect()->to('/dashboard');
|
||||
} else {
|
||||
session()->setFlashdata('warning', 'Reinizzializzazione non riuscita.');
|
||||
return redirect()->to('/dashboard');
|
||||
}
|
||||
} // function Reinizzializza
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
public function logout(){
|
||||
session()->destroy();
|
||||
return redirect()->to('/Lavoratori/login');
|
||||
}//function logout
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
public function login (){
|
||||
$data = [];
|
||||
helper(['tools','form']);
|
||||
|
||||
// Gestione POST data dal form
|
||||
if ($this->request->getMethod() == 'post') {
|
||||
|
||||
// Regole per validare i campi introdotti nel form:
|
||||
$rules = [
|
||||
'Nickname' => 'required|min_length[8]|max_length[8]',
|
||||
'Password' => 'required|min_length[8]|max_length[255]',
|
||||
];
|
||||
|
||||
// analisi della validazione
|
||||
if (! $this->validate($rules)) {
|
||||
// Validazione non passata, mostro errori sulla flash di warning
|
||||
$data['validation'] = $this->validator;
|
||||
session()->setFlashdata('warning', $data['validation']->listErrors());
|
||||
|
||||
} else {
|
||||
// validazione passata
|
||||
// ricavo il lavoratore dai dati inseriti
|
||||
$model = new Lavoratori_Model();
|
||||
$lav = $model->where('lvNickname', $this->request->getVar('Nickname'))
|
||||
->first();
|
||||
if (isset($lav) && (password_verify($this->request->getVar('Password'), $lav->lvPassword))) {
|
||||
// Verifico se lavoratore abilitato:
|
||||
if (password_verify($this->request->getVar('Password'), $lav->lvPassword)){
|
||||
// Utente riconosciuto, inizio la sessione di lavoro
|
||||
if (setUserSession($lav)){
|
||||
session()->setFlashdata('success', 'Utente abilitato correttamente');
|
||||
return redirect()->to('/dashboard');
|
||||
} else {
|
||||
session()->setFlashdata('warning', 'Errore durante la creazione della sessione di lavoro.<br> Riprovare e se l\'errore si ripete, contattare l\'amministratore.');
|
||||
}
|
||||
|
||||
} else {
|
||||
// utente non riconosciuto, ritorno al login
|
||||
session()->setFlashdata('warning', 'Errore, lavoratore non riconosciuto.<br> Riprovare e se l\'errore si ripete, contattare l\'amministratore.');
|
||||
}
|
||||
|
||||
} else {
|
||||
// Coppia nickname/password non corretta.
|
||||
session()->setFlashdata('warning', 'Errore, lavoratore non riconosciuto.<br> Riprovare e se l\'errore si ripete, contattare l\'amministratore.');
|
||||
}
|
||||
|
||||
}// analisi della validazione
|
||||
|
||||
}// Gestione POST data dal form
|
||||
|
||||
// Show login page
|
||||
echo view('pages/lavoratori/lavoratori_form_login', $data);
|
||||
}//function login
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
public function profile($id = null){
|
||||
$data = [];
|
||||
helper(['tools','form']);
|
||||
$model = new Lavoratori_Model();
|
||||
|
||||
// Verifico sessione e livello:
|
||||
if (!CheckLevel(0)) { return redirect()->to('/dashboard'); }
|
||||
|
||||
// Ricavo livello del lavoratore (solo amministratori o superiori, possono editare tutte del lavoratore)
|
||||
$enable = CheckLevel(3,false);
|
||||
|
||||
// Ricavo l'ID del lavoratore di cui editare il profilo:
|
||||
if (!isset($id)) {
|
||||
$id = session()->get('lavoratore')->lvIDlavoratore;
|
||||
}
|
||||
|
||||
// Ricavo il profilo del lavoratore da editare
|
||||
$lav = $model->where('lvIDlavoratore', $id)
|
||||
->first();
|
||||
|
||||
// Gestione POST
|
||||
if ($this->request->getMethod() == 'post') {
|
||||
// Regole per validare i campi introdotti nel form:
|
||||
//
|
||||
// L'utente normale può editare solo nome, cognome e quando vuole anche la password.
|
||||
// L'utente amministratore o superiore, può editare tutto dell'utente.
|
||||
$rules = [
|
||||
//'IDlavoratore' => '',
|
||||
'Nome' => 'trim|required|min_length[3]|max_length[64]',
|
||||
'Cognome' => 'trim|required|min_length[3]|max_length[64]',
|
||||
];
|
||||
if ($enable){
|
||||
$rules['Mail'] = 'required|min_length[6]|max_length[50]|valid_email';
|
||||
$rules['Nickname'] = 'trim|required|min_length[8]|max_length[8]';
|
||||
$rules['RimborsoKm'] = 'required';
|
||||
}
|
||||
if($this->request->getPost('NewPassword') != ''){
|
||||
$rules['NewPassword'] = 'required|min_length[8]|max_length[255]';
|
||||
$rules['ConfirmNewPassword'] = 'matches[NewPassword]';
|
||||
}
|
||||
|
||||
// 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
|
||||
$newData = [
|
||||
'lvIDlavoratore' => $lav->lvIDlavoratore,
|
||||
'lvNome' => $this->request->getVar('Nome'),
|
||||
'lvCognome' => $this->request->getVar('Cognome'),
|
||||
];
|
||||
if ($enable){
|
||||
$newData['lvLevel'] = $this->request->getVar('Livello');
|
||||
$newData['lvMail'] = $this->request->getVar('Mail');
|
||||
$newData['lvNickname'] = $this->request->getVar('Nickname');
|
||||
$newData['lvRimborsoKm'] = $this->request->getVar('RimborsoKm');
|
||||
}
|
||||
if($this->request->getPost('NewPassword') != ''){
|
||||
$newData['lvPassword'] = $this->request->getPost('NewPassword');
|
||||
}
|
||||
|
||||
if ($model->save($newData)) {
|
||||
session()->setFlashdata('success', 'Successfuly Updated');
|
||||
return redirect()->to('/dashboard');
|
||||
} else {
|
||||
session()->setFlashdata('danger', 'Errore durante l\'update del record nel DB.');
|
||||
}
|
||||
|
||||
}// analisi validazione
|
||||
|
||||
}// Gestione POST
|
||||
|
||||
// Rileggo i dati dal DB
|
||||
$data['lavoratore'] = $lav;
|
||||
$data['enable'] = $enable;
|
||||
// Show profile
|
||||
echo view('pages/lavoratori/lavoratori_form_profile', $data);
|
||||
} // function profile
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
public function delete($id = null){
|
||||
$data = [];
|
||||
helper(['tools','form']);
|
||||
|
||||
// Verifico sessione e livello:
|
||||
if (!CheckLevel(3,false)) { return redirect()->to('/Lavoratori/anagrafica'); }
|
||||
|
||||
// Verifico di non cancellare me stesso:
|
||||
if (session()->get('lavoratore')->lvIDlavoratore==$id) {
|
||||
session()->setFlashdata('warning', 'L\'utente non può cancellare se stesso.');
|
||||
return redirect()->to('/Lavoratori/anagrafica');
|
||||
}
|
||||
|
||||
// Elimino il record:
|
||||
$model = new Lavoratori_Model();
|
||||
if ($model->delete($id)) {
|
||||
session()->setFlashdata('success', 'Recors cancellato correttamente.');
|
||||
|
||||
} else {
|
||||
session()->setFlashdata('warning', 'Errore durante la cancellazione del record.');
|
||||
}
|
||||
|
||||
return redirect()->to('/Lavoratori/anagrafica');
|
||||
} // function delete
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
public function register(){
|
||||
$data = [];
|
||||
helper(['tools','form']);
|
||||
|
||||
// // Verifico sessione e livello:
|
||||
// if (CheckLevel(0,false)) { return redirect()->to('/dashboard'); } // non faccio il test così lo uso anche per creare nuovo utente dalla pagina anagrafica
|
||||
|
||||
// Gestione POST data dal form
|
||||
if ($this->request->getMethod() == 'post') {
|
||||
|
||||
// Regole per validare i campi introdotti nel form:
|
||||
$rules = [
|
||||
'Nome' => 'required|min_length[3]|max_length[64]',
|
||||
'Cognome' => 'required|min_length[3]|max_length[64]',
|
||||
'Password' => 'required|min_length[8]|max_length[255]',
|
||||
'PasswordConfirm' => 'matches[Password]',
|
||||
];
|
||||
|
||||
// analisi della validazione
|
||||
if (! $this->validate($rules)) {
|
||||
// Validazione non passata, mostro errori sulla flash di warning
|
||||
$data['validation'] = $this->validator;
|
||||
session()->setFlashdata('warning', $data['validation']->listErrors());
|
||||
|
||||
}else{
|
||||
// validazione passata
|
||||
$model = new Lavoratori_Model();
|
||||
$nom = $this->request->getVar('Nome');
|
||||
$cogn = $this->request->getVar('Cognome');
|
||||
|
||||
// Preparo i dati del nuovo record
|
||||
$newData = [
|
||||
'lvNome' => $nom,
|
||||
'lvCognome' => $cogn,
|
||||
'lvMail' => strtolower($nom) . "." . strtolower($cogn) . "@csico.it",
|
||||
'lvLevel' => 0,
|
||||
'lvRimborsoKm' => 0.0,
|
||||
'lvNickname' => 'CSiCo'.(substr(strtoupper($cogn), 0, 1)).(substr(strtolower($cogn), 1, 1)).(substr(strtolower($nom), 0, 1)),
|
||||
'lvPassword' => $this->request->getVar('Password'),
|
||||
];
|
||||
// Inserisco il nuovo record
|
||||
if ($model->save($newData)){
|
||||
session()->setFlashdata('success', 'Dati acquisiti, attendere la mail di conferma per l\'abilitazione completa');
|
||||
//return redirect()->to('/dashboard');
|
||||
return redirect()->to('/Lavoratori/anagrafica'); // uso register anche nell'anagrafica
|
||||
} else {
|
||||
session()->setFlashdata('danger', 'Errore durante l\'inserimento del record nel DB.');
|
||||
}
|
||||
|
||||
}// analisi validazione
|
||||
|
||||
}// Gestione POST
|
||||
|
||||
echo view('pages/lavoratori/lavoratori_form_register', $data);
|
||||
} // function register
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
public function anagrafica(){
|
||||
$data['tab'] = 'amministrazione';
|
||||
helper(['tools','form']);
|
||||
|
||||
// Verifico sessione e livello:
|
||||
if (!CheckLevel(1)) { return redirect()->to('/dashboard'); }
|
||||
|
||||
$model = new Lavoratori_Model();
|
||||
|
||||
// imposto l'ordinamento richiesto:
|
||||
CheckSort('anagrafica',true);
|
||||
$data['sortCol'] = session()->get('sort')['col'];
|
||||
$data['sortDir'] = session()->get('sort')['dir'];
|
||||
$model->orderBy($data['sortCol'], $data['sortDir']);
|
||||
|
||||
// // imposto l'ordinamento richiesto:
|
||||
// $order_field = session()->get('sort_lavoratori_field');
|
||||
// $order_direction = session()->get('sort_lavoratori_val');
|
||||
// $model->orderBy($order_field, $order_direction);
|
||||
|
||||
$data['dataset'] = $model->paginate(session()->get('pager_num'));
|
||||
$data['pager'] = $model->pager;
|
||||
|
||||
// Show anagrafica
|
||||
echo view('pages/lavoratori/lavoratori_anagrafica', $data);
|
||||
} // function anagrafica
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// Funzione per resettare la password dell'utente indicato con la standard password.
|
||||
// TODO Deve inviare mail di conferma all'utente.
|
||||
public function reset($id){
|
||||
$data = [];
|
||||
helper(['tools','form']);
|
||||
|
||||
// Verifico sessione e livello:
|
||||
if (!CheckLevel(0)) { return redirect()->to('/Lavoratori/login'); }
|
||||
|
||||
|
||||
// Se non abbiamo il livello amministrativo non posso resettare la password a meno che non sia la mia.
|
||||
$lav = session()->get('lavoratore');
|
||||
if ((CheckLevel(3, false)) || ($lav->lvIDlavoratore==$id)){
|
||||
//Resetto password con quella standard: password
|
||||
|
||||
// Preparo i dati per l'aggiornamento
|
||||
$newData = [
|
||||
'lvIDlavoratore' => $id,
|
||||
'lvPassword' => 'password',
|
||||
];
|
||||
|
||||
// Eseguo update del record
|
||||
$model = new Lavoratori_Model();
|
||||
if ($model->save($newData)) {
|
||||
session()->setFlashdata('success', 'Password resettata correttamente.');
|
||||
} else {
|
||||
session()->setFlashdata('warning', 'Reset password non riuscito.');
|
||||
}
|
||||
} else{
|
||||
// non resetto la password
|
||||
session()->setFlashdata('warning', 'Reset password non consentito.');
|
||||
}
|
||||
|
||||
return redirect()->to('/Lavoratori/anagrafica');
|
||||
} // function reset
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
public function export_csv(){
|
||||
helper(['tools']);
|
||||
|
||||
// Verifico sessione e livello:
|
||||
if (!CheckLevel(1)) {
|
||||
if (ENVIRONMENT === 'development') { session()->setFlashdata('warning', '/Lavoratori/export_csv -> Error code 001'); }
|
||||
return redirect()->to('/dashboard');
|
||||
}
|
||||
|
||||
// Ricavo le ore dell'utente:
|
||||
// Tab: Lavoratori = ['lvIDlavoratore', 'lvNome', 'lvCognome', 'lvMail', 'lvLevel', 'lvRimborsoKm', 'lvNickname', 'lvPassword', 'lvCreatedAt', 'lvUpdatedAt', 'lvDeletedAt'];
|
||||
$model = new Lavoratori_Model();
|
||||
|
||||
// imposto l'ordinamento richiesto:
|
||||
CheckSort('anagrafica',true);
|
||||
$sortCol = session()->get('sort')['col'];
|
||||
$sortDir = session()->get('sort')['dir'];
|
||||
$model->orderBy($sortCol, $sortDir);
|
||||
|
||||
// eseguo la query:
|
||||
$data['dataset'] = $model->asArray()
|
||||
->findAll();
|
||||
|
||||
// Imposto il nome del file da downloadare:
|
||||
$filepath = $_SERVER['MRK_FILE_PATH'];
|
||||
$filename = 'CSiCoDB_Lavoratori_il_' . date('Y-m-d_H-i-s') . '.csv';
|
||||
// file creation
|
||||
$file = fopen($filepath . $filename,"w");
|
||||
// Esport header
|
||||
fputcsv($file,['ID','Nome','Cognome','Mail','Level','RimborsoKm','Nickname','Hash Password','CreatedAt','UpdatedAt','DeletedAt']);
|
||||
foreach ($data['dataset'] as $line){
|
||||
fputcsv($file,$line);
|
||||
}
|
||||
// Chiudo il 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 Lavoratori
|
||||
Reference in new issue
Block a user