Example #1
0
 public function saveKho(Kho $kho)
 {
     $data = array('ten_kho' => $kho->getTenKho(), 'dia_chi_kho' => $kho->getDiaChiKho());
     $id_kho = (int) $kho->getIdKho();
     if ($id_kho == 0) {
         $this->tableGateway->insert($data);
     } else {
         if ($this->getKhoByArrayConditionAndArrayColumn(array('id_kho' => $id_kho), array('ten_kho'))) {
             $this->tableGateway->update($data, array('id_kho' => $id_kho));
         } else {
             return false;
         }
     }
     return true;
 }
 public function suaChiNhanhAction()
 {
     $id = $this->params('id');
     $kho_table = $this->getServiceLocator()->get('Application\\Model\\KhoTable');
     $kho = $kho_table->getKhoByArrayConditionAndArrayColumn(array('id_kho' => $id), array());
     if (!$kho) {
         $this->flashMessenger()->addErrorMessage('Không tìm thấy chi nhánh cần sửa');
         return $this->redirect()->toRoute('chi_nhanh');
     }
     $form = $this->getServiceLocator()->get('Application\\Form\\SuaChiNhanhForm');
     $form->setData($kho[0]);
     $return = array('form' => $form, 'id' => $id);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post = $request->getPost();
         $form->setData($post);
         if ($form->isValid()) {
             $kho_ton_tai = $kho_table->getKhoByArrayConditionAndArrayColumn(array('dia_chi_kho' => $post['dia_chi_kho']), array('id_kho'));
             // nếu địa chỉ mới sửa chưa được sủ dụng, hoặc địa chỉ đó thuộc về kho cần sửa
             if (!$kho_ton_tai or $kho_ton_tai and $kho_ton_tai[0]['id_kho'] == $id) {
                 $kho_moi = new Kho();
                 $kho_moi->exchangeArray($post);
                 $kho_moi->setIdKho($id);
                 $kho_table->saveKho($kho_moi);
                 $this->flashMessenger()->addSuccessMessage('Chúc mừng, cập nhật chi nhánh mới thành công');
                 return $this->redirect()->toRoute('chi_nhanh');
             } else {
                 $this->flashMessenger()->addErrorMessage('Địa chỉ này đã tồn tại một chi nhánh khác');
                 return $this->redirect()->toRoute('chi_nhanh');
             }
         } else {
             die(var_dump($form->getMessages()));
             $return['form'] = $form;
             return $return;
         }
     } else {
         return $return;
     }
 }