예제 #1
0
 public function execute()
 {
     global $gvMinPasswordLength, $gvPath;
     $op_password = gfPostVar('op_password', '');
     $op_password_repete = gfPostVar('op_password_repete', '');
     // Trim data
     $this->op_code = trim($this->op_code);
     $this->op_name = trim($this->op_name);
     $this->op_surname = trim($this->op_surname);
     // Data validation
     if ($this->op_code === '' || $this->op_name === '' || $this->op_surname === '') {
         $this->message = "Errore: tutti i campi sono obbligatori.";
         return true;
     }
     if ($this->op_id === 0 && $op_password === '') {
         $this->message = "Errore: il campo password è obbligatorio.";
         return true;
     }
     if ($op_password && strlen($op_password) < $gvMinPasswordLength) {
         $this->message = "Errore: la password deve contenere almeno " . "{$gvMinPasswordLength} caratteri.";
         return true;
     }
     if ($op_password !== $op_password_repete) {
         $this->message = "Errore: le password non coincidono.";
         return true;
     }
     // Allow only letters and digits in op_code
     if (preg_match('/^[0-9a-z]+$/i', $this->op_code) !== 1) {
         $this->message = "Errore: il codice operatore non è valido.";
         return true;
     }
     // Check name
     if (preg_match('/^[a-z \'àèéìòù]+$/i', $this->op_name) !== 1) {
         $this->message = "Errore: il nome contiene caratteri non validi.";
         return true;
     }
     // Check surname
     if (preg_match('/^[a-z \'àèéìòù]+$/i', $this->op_surname) !== 1) {
         $this->message = "Errore: il cognome contiene caratteri non validi.";
         return true;
     }
     // Check if code is taken for new operator
     $op = Operator::fromDatabaseByCode($this->op_code);
     if ($op && ($this->op_id === 0 || $this->op_id !== (int) $op->getId())) {
         $this->message = "Errore: il codice operatore non è disponibile.";
         return true;
     }
     unset($op);
     // Check operator is offline (only when edit)
     if ($this->op_id !== 0) {
         $operator = Operator::fromDatabaseById($this->op_id);
         if (!$operator) {
             $this->message = "Errore interno: il record non è presente.";
             return true;
         }
         if ($operator->isOnline()) {
             $this->message = "L'operatore è online, impossibile modificarlo.";
             return true;
         }
     }
     if ($this->op_id === 0) {
         $op = Operator::newRecord();
         $op->setCode($this->op_code);
         $op->setName($this->op_name);
         $op->setSurname($this->op_surname);
         $op->setPassword($op_password);
     } else {
         $op = Operator::fromDatabaseById($this->op_id);
         $op->setCode($this->op_code);
         $op->setName($this->op_name);
         $op->setSurname($this->op_surname);
         if ($op_password) {
             $op->setPassword($op_password);
         }
     }
     if ($op->save()) {
         gfSetDelayedMsg('Operazione effettuata correttamente', 'Ok');
         $redirect = new RedirectOutput("{$gvPath}/application/adminOperatorList");
         return $redirect;
     } else {
         $this->message = "Impossibile salvare le modifiche. Ritentare in seguito.";
         return true;
     }
 }