public function updateAction() { // Disable view $this->view->disable(); // Get and process POST data if ($this->request->isPost() && !empty($data = $this->request->getPost("data")) && in_array($action = $this->request->getPost("action"), array("Add", "Update"))) { if (!isset($data["kue_name"]) || empty($kue_name = ucfirst(htmlspecialchars(trim($data["kue_name"]))))) { $this->flash->error("Nama kuesioner belum diisi. Mohon isi nama kuesioner dan ulangi update kembali!"); return; } if (!isset($data["krit_ids"]) || count($krit_ids = array_map("trim", $data["krit_ids"])) < 1) { $this->flash->error("Belum ada kriteria yang dipilih untuk kuesioner ini. " . "Mohon masukkan minimal 1 kriteria dan ulangi update kembali!"); return; } //Get selected respondents id $chosen_responden = isset($data["chosen_responden"]) ? $data["chosen_responden"] : array(); if ($action == "Add") { //Kuesioner dupe check if (!empty(KuesionerHead::findFirst("name = '" . $kue_name . "' AND active = true"))) { $this->flash->error("Kuesioner dengan nama <strong>" . $kue_name . "</strong> sudah ada di database! " . "Mohon gunakan nama lain dan ulangi proses update kembali."); return; } $kuesioner_head = new KuesionerHead(); $kuesioner_head->setName($kue_name); if (!$kuesioner_head->save()) { $this->flash->error("Fatal Error! Could not create kuesioner head record."); foreach ($kuesioner_head->getMessages() as $err) { $this->flash->error($err); } return; } $kuesioner_chain = new KuesionerChain(); $kuesioner_chain->setIdKuesioner($kuesioner_head->getIdKuesioner()); for ($i = 0; $i < count($krit_ids);) { $kuesioner_chain->setIdKueKrit(null)->setIdKriteria($krit_ids[$i++])->setSequenceNo($i); if (!$kuesioner_chain->save()) { $this->flash->error("Fatal Error! Could not create kuesioner chain records."); foreach ($kuesioner_chain->getMessages() as $err) { $this->flash->error($err); } return; } } if (!$this->mergeKuesionerAccess($kuesioner_head->getIdKuesioner(), $chosen_responden)) { return; } //It's possible to do direct update after add kuesioner, so store kuesioner id at session $this->session->set("#pending_id_kuesioner", $kuesioner_head->getIdKuesioner()); } else { if (empty($kue_id = (int) $this->request->getPost("kue_id", "int"))) { $this->flash->error("Damn! No kuesioner id received!"); return; } else { if ($kue_id == -1 && empty($kue_id = $this->session->get("#pending_id_kuesioner"))) { $this->flash->error("Damn! No kuesioner id stored!"); return; } } $kuesioner_head = KuesionerHead::findFirst("id_kuesioner = " . $kue_id . " AND active = true"); if (empty($kuesioner_head)) { $this->flash->error("Unable to retrieve kuesioner head record using the specified id!"); return; } if ($kuesioner_head->getStatus() == 'L') { //Kuesioner status already locked if ($this->mergeKuesionerAccess($kue_id, $chosen_responden)) { $this->flash->warning("Kuesioner ini telah diisi oleh responden! " . "Hanya hak akses responden saja yang berhasil diupdate ke database."); } return; } //Additional check if kuesioner name changed if ($kue_name != $kuesioner_head->getName()) { if (!empty(KuesionerHead::findFirst("id_kuesioner != " . $kue_id . " AND name = '" . $kue_name . "' AND active = true"))) { $this->flash->error("Kuesioner dengan nama <strong>" . $kue_name . "</strong> sudah ada di database. " . "Mohon gunakan nama yang lain!"); return; } } $kuesioner_head->setName($kue_name); //First, mark all kuesioner kriteria items to inactive $result = $this->modelsManager->executeQuery("UPDATE KuesionerChain SET active = false" . " WHERE id_kuesioner = :id_kue:", array("id_kue" => $kue_id)); if (!$result->success()) { $this->flash->error("Unable to perform update for this kuesioner."); foreach ($result->getMessages() as $message) { $this->flash->error($message->getMessage()); } return; } for ($i = 0; $i < count($krit_ids); $i++) { $kuesioner_chain = KuesionerChain::findFirst(array("id_kuesioner = :kue_id: AND id_kriteria = :krit_id: AND sequence_no = :seq_no:", "bind" => array("kue_id" => $kue_id, "krit_id" => $krit_ids[$i], "seq_no" => $i + 1))); if (empty($kuesioner_chain)) { $kuesioner_chain = new KuesionerChain(); $kuesioner_chain->setIdKuesioner($kue_id)->setIdKriteria($krit_ids[$i])->setSequenceNo($i + 1); } else { $kuesioner_chain->setActive(true); } if (!$kuesioner_chain->save()) { $this->flash->error("Fatal Error! Could not save changes to kuesioner chain records."); foreach ($kuesioner_chain->getMessages() as $err) { $this->flash->error($err); } return; } } //Finally, save kuesioner head record if (!$kuesioner_head->save()) { $this->flash->error("Fatal Error! Error occured while saving kuesioner head record."); foreach ($kuesioner_head->getMessages() as $err) { $this->flash->error($err); } return; } if (!$this->mergeKuesionerAccess($kue_id, $chosen_responden)) { return; } } $this->flash->success("Data kuesioner telah berhasil disimpan ke dalam database."); } }