public function update() { $isLogged = $this->protection->isUserLogged(); $data['status'] = null; $data['object'] = array(); $data['errors'] = array(); if ($isLogged) { $validationRules = array(array('field' => 'id', 'label' => 'ID', 'rules' => 'required|is_natural_no_zero')); $this->form_validation->set_rules($validationRules); $this->form_validation->set_message('required', 'O campo %s não pode ser vazio.'); $this->form_validation->set_message('is_natural_no_zero', 'O campo %s deve ser um inteiro positivo.'); if ($this->form_validation->run()) { $id = $this->input->post('id'); $broker = new Broker($id, null, null, null); if ($this->input->post('ip_address')) { $broker->setIpAddress($this->input->post('ip_address')); } if ($this->input->post('port')) { $broker->setPort($this->input->post('port')); } if ($this->input->post('topic')) { $broker->setTopic($this->input->post('topic')); } $data['status'] = true; $this->load->model('model_brokers'); $data["object"] = $this->model_brokers->update($broker); } else { $data["status"] = false; foreach ($validationRules as $field) { $error = form_error($field['field']); if ($error != "") { $data["errors"][] = $error; } } } } else { $data['status'] = false; $data['errors'][] = $this->apphelper->getErrMsgs()['login']; } //prepare the answer $clientAccepts = $this->apphelper->getAcceptHeader(); switch ($clientAccepts[0]) { case "text/html": $this->apphelper->loadDefaultViewData($data); if ($isLogged) { $viewName = 'html/view_brokers.php'; $this->load->model('model_user'); $email = $this->session->userdata('email'); $data['sessionUser'] = $this->model_user->getUserByEmail($email); } else { $viewName = 'html/view_login'; $this->apphelper->loadLoginInputs($data); } break; case "application/json": $viewName = 'json_render.php'; $data['jsonData']['status'] =& $data['status']; $data['jsonData']['object'] =& $data['object']; $data['jsonData']['errors'] =& $data['errors']; break; default: $viewName = 'text_render.php'; $data['text'] = $this->apphelper->getErrMsgs()['unknowMIME']; break; } //sends the answer $this->load->view($viewName, $data); }