public function removeDevice() { $dev_id = $_GET['dev_id']; $device = Device::fromDatabaseById($dev_id); if ($device) { if ($device->delete()) { return 'true'; } } return 'false'; }
public function execute() { global $gvPath; // Trim data $this->dev_ip_address = trim($this->dev_ip_address); $this->dev_desk_number = trim($this->dev_desk_number); // Data validation if ($this->dev_ip_address === '' || $this->dev_desk_number === '') { $this->message = "Errore: tutti i campi sono obbligatori."; return true; } // dev_desk_number should contain... numbers if (preg_match('/^(0|[1-9][0-9]*)$/', $this->dev_desk_number) !== 1) { $this->message = "Errore: il numero dello sportello non è valido."; return true; } // Check ip_address if (!filter_var($this->dev_ip_address, FILTER_VALIDATE_IP)) { $this->message = "Errore: l'indirizzo IP non è valido."; return true; } // Check if desk number really exists if ((int) $this->dev_desk_number !== 0) { $desk = Desk::fromDatabaseByNumber($this->dev_desk_number); if (!$desk) { $this->message = "Errore: lo sportello specificato non esiste."; return true; } unset($desk); } // Check tdCode exists and active if ($this->dev_td_code) { $td = TopicalDomain::fromDatabaseByCode($this->dev_td_code); if (!$td || !$td->getActive()) { $this->message = "Errore: l'area tematica selezionata non è disponibile."; return true; } } // Check ip is not taken $device = Device::fromDatabaseByIpAddress($this->dev_ip_address); $desk = Desk::fromDatabaseByIpAddress($this->dev_ip_address); if ($desk || $device && ($this->dev_id === 0 || $this->dev_id !== (int) $device->getId())) { $this->message = "Errore: l'indirizzo IP è gia stato assegnato."; return true; } unset($device); if ($this->dev_id === 0) { $device = Device::newRecord(); } else { $device = Device::fromDatabaseById($this->dev_id); } $device->setIpAddress($this->dev_ip_address); $device->setDeskNumber($this->dev_desk_number); $device->setTdCode($this->dev_td_code); if ($device->save()) { gfSetDelayedMsg('Operazione effettuata correttamente', 'Ok'); $redirect = new RedirectOutput("{$gvPath}/application/adminDeviceList"); return $redirect; } else { $this->message = "Impossibile salvare le modifiche. Ritentare in seguito."; return true; } }