public function saveJosScienceResearch(JosScienceResearchOfUser $Jos_science_research_of_user)
 {
     $data = array('science_activity_id' => $Jos_science_research_of_user->getScienceActivityId(), 'user_id' => $Jos_science_research_of_user->getUserId(), 'time_from' => $Jos_science_research_of_user->getTimeFrom(), 'time_to' => $Jos_science_research_of_user->getTimeTo(), 'note' => $Jos_science_research_of_user->getNote());
     $value_id = (int) $Jos_science_research_of_user->getValueId();
     if ($value_id == 0) {
         $this->tableGateway->insert($data);
     } else {
         if ($this->getScienceResearchByArrayConditionAndArrayColumns(array('value_id' => $value_id), array('user_id'))) {
             $this->tableGateway->update($data, array('value_id' => $value_id));
         } else {
             return false;
         }
     }
     return true;
 }
 public function editHoatDongNghienCuuKhoaHocAction()
 {
     // điểm truy cập csdl
     $jos_users_table = $this->getServiceLocator()->get('Permission\\Model\\JosUsersTable');
     $jos_year_table = $this->getServiceLocator()->get('NamHoc\\Model\\JosYearTable');
     $jos_science_research_of_user_table = $this->getServiceLocator()->get('Application\\Model\\JosScienceResearchOfUserTable');
     $jos_science_activity_table = $this->getServiceLocator()->get('CongTacNghienCuu\\Model\\JosScienceActivityTable');
     $id_giang_vien = $this->params('id');
     $read = $this->getServiceLocator()->get('AuthService')->getStorage()->read();
     if (isset($read['username']) and $read['username']) {
         // kiểm tra user đang đăng nhập
         $user = $jos_users_table->getGiangVienByArrayConditionAndArrayColumns(array('username' => $read['username']));
         // kiểm tra user có quyền editAllProfile không
         $white_lists = $read['white_list'];
         $edit_all_profile = 0;
         foreach ($white_lists as $key => $white_list) {
             if ($white_list['action'] == 'editAllProfile') {
                 $edit_all_profile = 1;
             }
         }
         if ($user and isset($user[0]['id']) and $id_giang_vien == $user[0]['id']) {
             // nếu đã đăng nhập
             $edit_all_profile = 1;
         }
         // có quyền
         if ($edit_all_profile == 1) {
             $request = $this->getRequest();
             if ($request->isPost()) {
                 $post = $request->getPost();
                 $edit_ctnc_form = new EditCTNCForm();
                 $edit_ctnc_form->setData($post);
                 if ($edit_ctnc_form->isValid()) {
                     // lấy year_id default
                     $jos_year_table = $this->getServiceLocator()->get('NamHoc\\Model\\JosYearTable');
                     $year = $jos_year_table->getYearByArrayConditionAndArrayColumn(array('is_active' => 1));
                     if (!$year or !isset($year[0]['year_id'])) {
                         die('Lỗi, Không xác định được năm cần sửa');
                     }
                     $year_id = $year[0]['year_id'];
                     // nếu hoạt động nghiên cứu khoa học cần sửa hỏng tồn tại thì không sửa
                     $science_activity_exist = $jos_science_activity_table->getScienceActivityByArrayConditionAndArrayColumn(array('year_id' => $year_id, 'value_id' => $post['id_hoat_dong']), array('name'));
                     if ($science_activity_exist) {
                         // lấy dữ liệu lên
                         $science_research_exist = $jos_science_research_of_user_table->getScienceResearchAndScienceActivityByArrayConditionAndArrayColumns(array('t1.user_id' => $id_giang_vien, 't1.science_activity_id' => $post['id_hoat_dong'], 't2.year_id' => $year_id), array(), array());
                         // nếu post trang_thai==1 thì sửa hoặc thêm mới
                         if ($post['trang_thai'] == 1) {
                             $science_research_new = new JosScienceResearchOfUser();
                             $science_research_new->exchangeArray($post);
                             if ($science_research_exist and isset($science_research_exist[0]['value_id'])) {
                                 $science_research_new->setValueId($science_research_exist[0]['value_id']);
                             }
                             $science_research_new->setUserId($id_giang_vien);
                             $science_research_new->setScienceActivityId($post['id_hoat_dong']);
                             $science_research_new->setNote($post['ghi_chu']);
                             // xử lý lại ngày bắt đầu
                             $time_from = strtotime($post['time_from']);
                             $time_from = date('Y-m-d', $time_from);
                             $science_research_new->setTimeFrom($time_from);
                             // xử lý lại ngày kết thúc
                             $time_to = strtotime($post['time_to']);
                             $time_to = date('Y-m-d', $time_to);
                             $science_research_new->setTimeTo($time_to);
                             $jos_science_research_of_user_table->saveJosScienceResearch($science_research_new);
                         } else {
                             if ($science_research_exist and isset($science_research_exist[0]['value_id'])) {
                                 $jos_science_research_of_user_table->deleteScienceResearchById($science_research_exist[0]['value_id']);
                             }
                         }
                         $this->flashMessenger()->addSuccessMessage('Chúc mừng, cập nhật thành công!');
                         return $this->redirect()->toRoute('application/crud', array('action' => 'index', 'id' => $id_giang_vien));
                     }
                 }
             }
         }
     }
     if (isset($id_giang_vien)) {
         $this->flashMessenger()->addErrorMessage('Bạn không có quyền truy cập. Vui lòng kiểm tra lại!');
         return $this->redirect()->toRoute('application/crud', array('action' => 'index', 'id' => $id_giang_vien));
     }
     $this->flashMessenger()->addErrorMessage('Bạn không có quyền truy cập. Vui lòng kiểm tra lại!');
     return $this->redirect()->toRoute('application/crud', array('action' => 'index'));
 }