From a6d5fbdc3edb133ed7568bd0e3b8535a89a7c2fd Mon Sep 17 00:00:00 2001
From: Woraprat <73052317+aeworaprat@users.noreply.github.com>
Date: Sun, 12 Mar 2023 23:24:21 +0700
Subject: [PATCH] update check not delete

---
 html/exhi/application/controllers/Company.php | 13 ++++++++++--
 .../application/controllers/Instructor.php    | 13 ++++++++++--
 html/exhi/application/controllers/Tag.php     | 11 ++++++++--
 html/exhi/application/controllers/User.php    | 20 +++++++++++++++++--
 .../application/models/member/M_member.php    |  6 ++++++
 .../models/project_tag/M_project_tag.php      |  7 +++++++
 .../views/company/v_company_manage.php        |  2 +-
 .../application/views/user/v_user_manage.php  |  2 +-
 8 files changed, 64 insertions(+), 10 deletions(-)

diff --git a/html/exhi/application/controllers/Company.php b/html/exhi/application/controllers/Company.php
index 9b4bea9..568cfb5 100644
--- a/html/exhi/application/controllers/Company.php
+++ b/html/exhi/application/controllers/Company.php
@@ -19,10 +19,19 @@ class Company extends Exhibition_Controller {
 	}
 
 	public function delete_company(){
+		$data['message'] = true;
 		$id = $this->input->post('id');
 		$this->load->model('company/M_company','company');
-		$this->company->delete_company($id);
-		$data['message'] = true ;
+		$this->load->model('project/M_project','project');
+		$projects = $this->project->get_all()->result();
+		foreach($projects as $project){
+			if($project->company_id == $id){
+				$data['message'] = false;
+			}
+		}
+		if($data['message'] != false){
+			$this->company->delete_company($id);
+		}
         echo json_encode($data);
 	}
 
diff --git a/html/exhi/application/controllers/Instructor.php b/html/exhi/application/controllers/Instructor.php
index 8078f01..51b3634 100644
--- a/html/exhi/application/controllers/Instructor.php
+++ b/html/exhi/application/controllers/Instructor.php
@@ -19,10 +19,19 @@ class Instructor extends Exhibition_Controller {
 	}
 
 	public function delete_instructor(){
+		$data['message'] = true ;
 		$id = $this->input->post('id');
 		$this->load->model('instructor/M_instructor','instructor');
-		$this->instructor->delete_instructor($id);
-		$data['message'] = true ;
+		$this->load->model('project/M_project','project');
+		$projects = $this->project->get_all()->result();
+		foreach($projects as $project){
+			if($project->instructor_id == $id){
+				$data['message'] = false;
+			}
+		}
+		if($data['message'] != false){
+			$this->instructor->delete_instructor($id);
+		}
         echo json_encode($data);
 	}
 
diff --git a/html/exhi/application/controllers/Tag.php b/html/exhi/application/controllers/Tag.php
index 4d268e8..91803b4 100644
--- a/html/exhi/application/controllers/Tag.php
+++ b/html/exhi/application/controllers/Tag.php
@@ -19,10 +19,17 @@ class Tag extends Exhibition_Controller {
 	}
 
 	public function delete_tag(){
+		$data['message'] = true;
 		$id = $this->input->post('id');
 		$this->load->model('tag/M_tag','tag');
-		$this->tag->delete_tag($id);
-		$data['message'] = true ;
+		$this->load->model('project_tag/M_project_tag','project_tag');
+		$project_tag = $this->project_tag->get_project_tag_by_tag_id($id)->result();
+		if(count($project_tag) > 0){
+			$data['message'] = false;
+		}
+		if($data['message'] != false){
+			$this->tag->delete_tag($id);
+		}
         echo json_encode($data);
 	}
 
diff --git a/html/exhi/application/controllers/User.php b/html/exhi/application/controllers/User.php
index d9ee93a..b6c7a50 100644
--- a/html/exhi/application/controllers/User.php
+++ b/html/exhi/application/controllers/User.php
@@ -93,10 +93,26 @@ class User extends Exhibition_Controller {
 
 
 	public function delete_user(){
+		$data['message'] = true ;
 		$id = $this->input->post('id');
 		$this->load->model('user/M_user','user');
-		$this->user->delete_user($id);
-		$data['message'] = true ;
+		$this->load->model('project/M_project','project');
+		$this->load->model('member/M_member','member');
+		$projects = $this->project->get_all()->result();
+		foreach($projects as $project){
+			if($project->user_id == $id){
+				$data['message'] = false;
+			}
+		}
+		$members = $this->member->get_all()->result();
+		foreach($members as $member){
+			if($member->user_id == $id){
+				$data['message'] = false;
+			}
+		}
+		if($data['message'] != false){
+			$this->user->delete_user($id);
+		}
         echo json_encode($data);
 	}
 
diff --git a/html/exhi/application/models/member/M_member.php b/html/exhi/application/models/member/M_member.php
index 8325ac7..4a06218 100644
--- a/html/exhi/application/models/member/M_member.php
+++ b/html/exhi/application/models/member/M_member.php
@@ -11,6 +11,12 @@ class M_member extends Da_member
 		parent::__construct();
 	}
 
+	public function get_all(){
+		$sql = "SELECT * FROM {$this->db_name}.member";
+        $query = $this->db->query($sql);
+        return $query;
+	}
+
 	public function get_member_by_project_id($id){
 		$sql = "SELECT * FROM {$this->db_name}.member
 		Where project_id = $id";
diff --git a/html/exhi/application/models/project_tag/M_project_tag.php b/html/exhi/application/models/project_tag/M_project_tag.php
index 7520edf..2403cc4 100644
--- a/html/exhi/application/models/project_tag/M_project_tag.php
+++ b/html/exhi/application/models/project_tag/M_project_tag.php
@@ -18,6 +18,13 @@ class M_project_tag extends Da_project_tag
         return $query;
 	}
 
+	public function get_project_tag_by_tag_id($id){
+		$sql = "SELECT * FROM {$this->db_name}.project_tag
+		Where tag_id = $id";
+        $query = $this->db->query($sql);
+        return $query;
+	}
+
 	public function get_by_id($id){
 		$sql = "SELECT * FROM {$this->db_name}.project_tag
 		join tag on project_tag.tag_id = tag.tag_id
diff --git a/html/exhi/application/views/company/v_company_manage.php b/html/exhi/application/views/company/v_company_manage.php
index 823b67c..3de14e5 100644
--- a/html/exhi/application/views/company/v_company_manage.php
+++ b/html/exhi/application/views/company/v_company_manage.php
@@ -345,7 +345,7 @@ function delete_company(id) {
                     } 
                     else {
                         Toast.fire({
-                            icon: 'success',
+                            icon: 'error',
                             title: 'ลบข้อมูลไม่สำเร็จ',
                         });
                     } 
diff --git a/html/exhi/application/views/user/v_user_manage.php b/html/exhi/application/views/user/v_user_manage.php
index 158dff3..ef1edec 100644
--- a/html/exhi/application/views/user/v_user_manage.php
+++ b/html/exhi/application/views/user/v_user_manage.php
@@ -161,7 +161,7 @@ function delete_user(id) {
                     } 
                     else {
                         Toast.fire({
-                            icon: 'success',
+                            icon: 'error',
                             title: 'ลบข้อมูลไม่สำเร็จ',
                         });
                     } 
-- 
GitLab