public function saveJosTeaching(JosTeaching $jos_teaching)
 {
     $data = array('subject_id' => $jos_teaching->getSubjectId(), 'user_id' => $jos_teaching->getUserId(), 'lesson_number' => $jos_teaching->getLessonNumber(), 'qualifications' => $jos_teaching->getQualifications(), 'edu_system' => $jos_teaching->getEduSystem(), 'note' => $jos_teaching->getNote());
     $value_id = (int) $jos_teaching->getValueId();
     if ($value_id == 0) {
         $this->tableGateway->insert($data);
     } else {
         if ($this->getTeachingByArrayConditionAndArrayColumns(array('value_id' => $value_id), array('user_id'))) {
             $this->tableGateway->update($data, array('value_id' => $value_id));
         } else {
             return false;
         }
     }
     return true;
 }
 public function editTeachingAction()
 {
     $id = $this->params('id');
     $request = $this->getRequest();
     if ($request->isPost()) {
         // đ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_teaching_table = $this->getServiceLocator()->get('Application\\Model\\JosTeachingTable');
         $service_config = $this->getServiceLocator()->get('config');
         $function_class = new FunctionClass();
         // post
         $post = $request->getPost();
         // nếu đã đăng nhập
         $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;
                 }
             }
             // nếu id cần sửa bằng với user_id đang đăng nhập hoặc có quyền editAllProfile
             if ($user and $user[0]['id'] == $id or $edit_all_profile == 1) {
                 $teaching_edit_form = new EditCongTacGiangDayForm($this->getServiceLocator());
                 $teaching_edit_form->setData($post);
                 if ($teaching_edit_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'];
                     // kiểm tra dữ liệu post đã tồn tại chưa nếu tồn tại rồi thì sửa
                     $teaching_exist = $jos_teaching_table->getTeachingAndSubjectByArrayConditionAndArrayColumns(array('t1.user_id' => $id, 't1.subject_id' => $post['ten_mon'], 't2.year_id' => $year_id), array(), array());
                     if ((!$teaching_exist or $teaching_exist and $teaching_exist[0]['value_id'] == $post['id_cong_tac']) and ($edit_all_profile == 1 or $teaching_exist[0]['user_id'] == $id)) {
                         // truy cập vào config lấy ra danh sách config bậc học
                         $bac_hocs = array();
                         $bac_hoc_source_model = array();
                         $bac_hoc_source_model['source_model']['application'] = 'bac_hoc';
                         $bac_hocs = $function_class->selectElementArray(array('array_element' => $bac_hoc_source_model, 'array' => $service_config));
                         $bac_hoc = '';
                         $co_bac_hoc = 0;
                         foreach ($bac_hocs as $key => $bh) {
                             if (isset($post['bac_hoc_' . $key]) and $post['bac_hoc_' . $key] == 1) {
                                 $co_bac_hoc++;
                                 if ($co_bac_hoc > 1) {
                                     $bac_hoc .= ', ';
                                 }
                                 $bac_hoc .= $bh;
                             }
                         }
                         // truy cập vào config lấy ra danh sách config hệ đào tạo
                         $he_dao_taos = array();
                         $he_dao_tao_source_model = array();
                         $he_dao_tao_source_model['source_model']['application'] = 'he_dao_tao';
                         $he_dao_taos = $function_class->selectElementArray(array('array_element' => $he_dao_tao_source_model, 'array' => $service_config));
                         $he_dao_tao = '';
                         $co_he_dao_tao = 0;
                         foreach ($he_dao_taos as $key => $hdt) {
                             if (isset($post['he_dao_tao_' . $key]) and $post['he_dao_tao_' . $key] == 1) {
                                 $co_he_dao_tao++;
                                 if ($co_he_dao_tao > 1) {
                                     $he_dao_tao .= ', ';
                                 }
                                 $he_dao_tao .= $hdt;
                             }
                         }
                         $teaching_new = new JosTeaching();
                         $teaching_new->setValueId($post['id_cong_tac']);
                         $teaching_new->setSubjectId($post['ten_mon']);
                         $teaching_new->setUserId($id);
                         $teaching_new->setLessonNumber($post['so_tiet']);
                         $teaching_new->setQualifications($bac_hoc);
                         $teaching_new->setEduSystem($he_dao_tao);
                         $teaching_new->setNote($post['ghi_chu']);
                         $jos_teaching_table->saveJosTeaching($teaching_new);
                         $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));
                     }
                 }
             }
         }
     }
     if (isset($id)) {
         $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));
     }
     // không có quyền sửa
     $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'));
 }