From 451ec35a99bea542d40d918d93ff895ee477270c Mon Sep 17 00:00:00 2001
From: "DESKTOP-O569NC8\\User" <62160114@go.buu.ac.th>
Date: Mon, 26 Sep 2022 21:12:24 +0700
Subject: [PATCH] Insert update

---
 application/controllers/Cluster.php           |  26 +++
 application/models/cluster/Da_cluster.php     |  31 +++
 application/models/cluster/M_cluster.php      |  21 ++
 .../views/Cluster/v_cluster_manage.php        | 195 +++++++++++++++---
 .../views/Company/v_company_manage.php        |  12 +-
 .../Information/v_information_manage.php      |  12 +-
 application/views/Login/Login.php             |   1 -
 .../views/Project/v_project_manage.php        |  14 +-
 application/views/Tag/v_tag_manage.php        |   8 +-
 application/views/Team/v_team_manage.php      |  12 +-
 application/views/User/v_user_manage.php      |  12 +-
 11 files changed, 275 insertions(+), 69 deletions(-)
 create mode 100644 application/models/cluster/Da_cluster.php
 create mode 100644 application/models/cluster/M_cluster.php

diff --git a/application/controllers/Cluster.php b/application/controllers/Cluster.php
index b5e7221..f9ccce8 100644
--- a/application/controllers/Cluster.php
+++ b/application/controllers/Cluster.php
@@ -12,5 +12,31 @@ class Cluster extends ExhibitionController {
 		$this->output_admin('Cluster/v_cluster_manage');
 	}
 
+	public function get_cluster()
+	{
+		$this->load->model('cluster/M_cluster', 'cluster');
+		$data = $this->cluster->get_cluster()->result();
+		echo json_encode($data);
+	}
 
+	public function add_cluster()
+	{
+		$logo = $this->input->post('logo');
+		$cluster_name = $this->input->post('cluster_name');
+		$this->load->model('cluster/Da_cluster', 'cluster');
+		$add = $this->cluster->add_cluster($logo, $cluster_name);
+		$data['message'] = true;
+		echo json_encode($data);
+	}
+
+	public function edit_cluster()
+	{
+		$id = $this->input->post('id');
+		$logo = $this->input->post('logo');
+		$cluster_name = $this->input->post('cluster_name');
+		$this->load->model('cluster/Da_cluster', 'cluster');
+		$add = $this->cluster->edit_cluster($logo, $cluster_name, $id);
+		$data['message'] = true;
+		echo json_encode($data);
+	}
 }
\ No newline at end of file
diff --git a/application/models/cluster/Da_cluster.php b/application/models/cluster/Da_cluster.php
new file mode 100644
index 0000000..a018402
--- /dev/null
+++ b/application/models/cluster/Da_cluster.php
@@ -0,0 +1,31 @@
+<?php 
+
+defined('BASEPATH') or exit('No direct script access allowed');
+
+require_once dirname(__FILE__) . '/../ExhibitionModel.php';
+
+class Da_cluster extends ExhibitionModel
+{
+    public function __construct()
+	{
+		parent::__construct();
+	}
+
+    function add_cluster($logo, $cluster_name){
+        $sql = "INSERT INTO {$this->db_name}.cluster (
+            cluster_name, 
+            image
+        ) VALUES (?,?);";
+        $query = $this->db->query($sql,[$cluster_name,$logo]);
+        return $query;
+    }
+
+    function edit_cluster($logo, $cluster_name, $id){
+        $sql = "UPDATE {$this->db_name}.cluster 
+         SET cluster_name = ?, 
+            image = ?
+        WHERE cluster_id = ?;";
+        $query = $this->db->query($sql,[$cluster_name,$logo, $id]);
+        return $query;
+    }
+}
\ No newline at end of file
diff --git a/application/models/cluster/M_cluster.php b/application/models/cluster/M_cluster.php
new file mode 100644
index 0000000..d885643
--- /dev/null
+++ b/application/models/cluster/M_cluster.php
@@ -0,0 +1,21 @@
+<?php
+
+defined('BASEPATH') or exit('No direct script access allowed');
+
+require_once 'Da_cluster.php';
+
+class M_cluster extends Da_cluster
+{
+    public function __construct()
+	{
+		parent::__construct();
+	}
+
+    function get_cluster()
+    {
+        $sql = "SELECT * FROM {$this->db_name}.cluster ORDER BY cluster_id DESC";
+        $query = $this->db->query($sql);
+        return $query;
+    }//end get_cluster
+
+}
\ No newline at end of file
diff --git a/application/views/Cluster/v_cluster_manage.php b/application/views/Cluster/v_cluster_manage.php
index 3a1a83b..b6d069c 100644
--- a/application/views/Cluster/v_cluster_manage.php
+++ b/application/views/Cluster/v_cluster_manage.php
@@ -28,39 +28,10 @@
                                         <h5>Cluster Manage</h5>
                                     </div>
                                     <div class="card-block">
-                                        <table id="myTable" class="display">
-                                            <thead>
-                                                <tr>
-                                                    <th>No.</th>
-                                                    <th>Logo</th>
-                                                    <th>Cluster Number</th>
-                                                    <th>Manage</th>
-                                                    <th></th>
-                                                    <th></th>
-                                                
-                                                </tr>
-                                            </thead>
-                                            <tbody>
-                                                <tr>
-                                                    <td>1</td>
-                                                    <td></td>
-                                                    <td>0</td>
-                                                    <td><button type="button" class="btn btn-outline-primary" title="" data-toggle="tooltip" data-original-title="btn btn-outline-primary"><div class="i-block" data-clipboard-text="feather icon-eye" data-filter="icon-eye" data-toggle="tooltip" title="" data-original-title="icon-eye"><i class="feather icon-eye"></i></div></button></td>
-                                                    <td><button type="button" class="btn btn-outline-warning" title="" data-toggle="tooltip" data-original-title="btn btn-outline-warning"><div class="i-block" data-clipboard-text="feather icon-edit" data-filter="icon-edit" data-toggle="tooltip" title="" data-original-title="icon-edit"><i class="feather icon-edit"></i></div></button></td>
-                                                    <td><button type="button" class="btn btn-outline-danger" title="" data-toggle="tooltip" data-original-title="btn btn-outline-danger"><div class="i-block" data-clipboard-text="feather icon-trash-2" data-filter="icon-trash-2" data-toggle="tooltip" title="" data-original-title="icon-trash-2"><i class="feather icon-trash-2"></i></div></button></td>
+                                        <button type="button" class="btn btn-outline-primary" data-toggle="modal" data-target="#add-cluster-modal" ><div class="i-block"  data-toggle="tooltip" ><i class="feather icon-plus-circle"></i></div></button>
+                                        <div id='create_table'>
 
-                                                </tr>
-                                                <tr>
-                                                    <td>2</td>
-                                                    <td></td>
-                                                    <td>1</td>
-                                                    <td><button type="button" class="btn btn-outline-primary" title="" data-toggle="tooltip" data-original-title="btn btn-outline-primary"><div class="i-block" data-clipboard-text="feather icon-eye" data-filter="icon-eye" data-toggle="tooltip" title="" data-original-title="icon-eye"><i class="feather icon-eye"></i></div></button></td>
-                                                    <td><button type="button" class="btn btn-outline-warning" title="" data-toggle="tooltip" data-original-title="btn btn-outline-warning"><div class="i-block" data-clipboard-text="feather icon-edit" data-filter="icon-edit" data-toggle="tooltip" title="" data-original-title="icon-edit"><i class="feather icon-edit"></i></div></button></td>
-                                                    <td><button type="button" class="btn btn-outline-danger" title="" data-toggle="tooltip" data-original-title="btn btn-outline-danger"><div class="i-block" data-clipboard-text="feather icon-trash-2" data-filter="icon-trash-2" data-toggle="tooltip" title="" data-original-title="icon-trash-2"><i class="feather icon-trash-2"></i></div></button></td>
-
-                                                </tr>
-                                            </tbody>
-                                        </table>
+                                        </div>
                                     </div>
                                 </div>
                             </div>
@@ -72,11 +43,169 @@
         </div>
     </div>
 </div>
+
+<div class="modal fade" id="edit-cluster-modal" tabindex="-1" role="dialog" aria-hidden="true">
+  <div class="modal-dialog modal-dialog-centered" role="document">
+    <div class="modal-content">
+      <div class="modal-header">
+        <h5 class="modal-title" id="edit-cluster-modal">Edit Cluster</h5>
+        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+          <span aria-hidden="true">&times;</span>
+        </button>
+      </div>
+      <div class="modal-body">
+        <input class="form-control" type="hidden"  id="edit_id">
+        <div class="form-group">
+            <label for="email">Logo</label>
+            <input class="form-control" type="file" id="edit_logo">
+        </div>
+        <div class="form-group">
+            <label for="cluster_name">Cluster Name</label>
+            <input class="form-control" type="text" id="edit_name">
+        </div>  
+      </div>
+      <div class="modal-footer">
+        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
+        <button type="button" class="btn btn-primary" onclick="editCluster()">Save</button>
+      </div>
+    </div>
+  </div>
+</div>
+
+<div class="modal fade" id="add-cluster-modal" tabindex="-1" role="dialog" aria-hidden="true">
+  <div class="modal-dialog modal-dialog-centered" role="document">
+    <div class="modal-content">
+      <div class="modal-header">
+        <h5 class="modal-title" id="add-cluster-modal">Add Cluster</h5>
+        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+          <span aria-hidden="true">&times;</span>
+        </button>
+      </div>
+      <div class="modal-body">
+        <div class="form-group">
+            <label for="email">Logo</label>
+            <input class="form-control" type="file" id="add_logo" >
+        </div>
+        <div class="form-group">
+            <label for="cluster_name">Cluster Name</label>
+            <input class="form-control" type="text" id="add_cluster_name">
+        </div>  
+      </div>
+      <div class="modal-footer">
+        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
+        <button type="button" class="btn btn-primary" onclick="addCluster()">Save</button>
+      </div>
+    </div>
+  </div>
+</div>
+
 <script>
 
 $( document ).ready(function() {
-    $('#myTable').DataTable();
+    get_cluster();
 });
 
+function get_cluster(){
+    $.ajax({
+        type: 'post',
+        url: "<?php echo site_url() . '/Cluster/get_cluster'; ?>",
+        dataType: 'json',
+        success: function(data) {
+            create_table(data);
+        }
+    })
+}
+
+function create_table(data){
+    let html_code = '';
+    html_code +='<table id="myTable" class="table-hover">'
+    html_code +='<thead>'
+    html_code +='<tr>'
+    html_code +='<th style="width: 10%;">No.</th>'
+    html_code +='<th style="width: 20%;">Logo</th>'
+    html_code +='<th style="width: 55%;">Cluster Number</th>'
+    html_code +='<th style="width: 15%;">Manage</th>'
+    html_code +='</tr>'
+    html_code +='</thead>'
+    html_code +='<tbody>'
+    data.forEach((row, index) => {
+        html_code += '<tr>'
+        html_code += '<td>'+ (index + 1)+'</td>'
+        html_code += '<td>Logo</td>'
+        html_code += '<td>'+row.cluster_name+'</td>'
+        html_code += '<td>'
+        html_code +='<button type="button" class="btn btn-outline-warning" data-toggle="modal" data-target="#edit-cluster-modal" onclick="editModal('+ row.cluster_id +',\''+ row.cluster_name+'\')" ><div class="i-block" data-clipboard-text="feather icon-edit" data-filter="icon-edit" data-toggle="tooltip"><i class="feather icon-edit"></i></div></button>'
+        html_code +='<button type="button" class="btn btn-outline-danger" data-toggle="tooltip" onclick="deleteCluster(' + row.cluster_id + ')" ><div class="i-block" data-clipboard-text="feather icon-trash-2" data-filter="icon-trash-2" data-toggle="tooltip" title="" ><i class="feather icon-trash-2"></i></div></button>'
+        html_code += '</td>'  
+        html_code += '</tr>';
+    })
+    html_code +='</tbody>';
+    html_code +='</table>';
+    $('#create_table').html(html_code);
+    $('#myTable').DataTable();
+}
+
+function editModal(id, name){
+    $('#edit_id').val(id) 
+    $('#edit_name').val(name);
+}
+
+function addCluster(){
+    let logo = $('#add_logo').val();
+    let cluster_name = $('#add_cluster_name').val();
+    $.ajax({
+        type: 'POST',
+        url: "<?php echo site_url() . '/Cluster/add_cluster'; ?>",
+        data: {
+                'logo': logo,
+                'cluster_name': cluster_name,
+            },
+        dataType: 'json',
+        success: function(data) {
+            console.log(data)
+            if (data.message) {
+                alert('succes');
+                $('#add-cluster-modal .close').click();
+                get_cluster()
+            }
+            else {
+                alert('fail');
+            }
+        }
+    });
+}
+
+function editCluster(){
+    let id = $('#edit_id').val();
+    let logo = $('#edit_logo').val();
+    let cluster_name = $('#edit_name').val();
+    $.ajax({
+        type: 'POST',
+        url: "<?php echo site_url() . '/Cluster/edit_cluster'; ?>",
+        data: {
+                'id': id,
+                'logo': logo,
+                'cluster_name': cluster_name,
+            },
+        dataType: 'json',
+        success: function(data) {
+            console.log(data)
+            if (data.message) {
+                alert('succes');
+                $('#edit-cluster-modal .close').click();
+                get_cluster()
+            }
+            else {
+                alert('fail');
+            }
+            /* End Check if log in fail */
+        }
+    });
+}
+
+function deleteCluster(){
+
+}
+
 
 </script>
\ No newline at end of file
diff --git a/application/views/Company/v_company_manage.php b/application/views/Company/v_company_manage.php
index 24534af..8508aa3 100644
--- a/application/views/Company/v_company_manage.php
+++ b/application/views/Company/v_company_manage.php
@@ -31,12 +31,12 @@
                                         <table id="myTable" class="display">
                                             <thead>
                                                 <tr>
-                                                <th>No.</th>
-                                                    <th>Logo</th>
-                                                    <th>Company Name</th>
-                                                    <th>Manage</th>
-                                                    <th></th>
-                                                    <th></th>
+                                                    <th style="width: 5%;">No.</th>
+                                                    <th style="width: 30%;">Logo</th>
+                                                    <th style="width: 50%;">Company Name</th>
+                                                    <th style="width: 5%;"></th>
+                                                    <th style="width: 5%;">Manage</th>
+                                                    <th style="width: 5%;"></th>
                                                 
                                                 </tr>
                                             </thead>
diff --git a/application/views/Information/v_information_manage.php b/application/views/Information/v_information_manage.php
index b25b713..8056a79 100644
--- a/application/views/Information/v_information_manage.php
+++ b/application/views/Information/v_information_manage.php
@@ -31,11 +31,11 @@
                                         <table id="myTable" class="display">
                                             <thead>
                                                 <tr>
-                                                    <th>No.</th>
-                                                    <th>Information Name</th>
-                                                    <th>Manage</th>
-                                                    <th></th>
-                                                    <th></th>
+                                                    <th style="width: 10%;">No.</th>
+                                                    <th style="width: 75%;">Information Name</th>
+                                                    <th style="width: 5%; "></th>
+                                                    <th style="width: 5%;">Manage</th>
+                                                    <th style="width: 5%;"></th>
                                                 
                                                 </tr>
                                             </thead>
@@ -55,7 +55,7 @@
                                                     <td><button type="button" class="btn btn-outline-warning" title="" data-toggle="tooltip" data-original-title="btn btn-outline-warning"><div class="i-block" data-clipboard-text="feather icon-edit" data-filter="icon-edit" data-toggle="tooltip" title="" data-original-title="icon-edit"><i class="feather icon-edit"></i></div></button></td>
                                                     <td><button type="button" class="btn btn-outline-danger" title="" data-toggle="tooltip" data-original-title="btn btn-outline-danger"><div class="i-block" data-clipboard-text="feather icon-trash-2" data-filter="icon-trash-2" data-toggle="tooltip" title="" data-original-title="icon-trash-2"><i class="feather icon-trash-2"></i></div></button></td>
 
-                                                                                                     <
+                                                                                                     
 
                                                 </tr>
                                             </tbody>
diff --git a/application/views/Login/Login.php b/application/views/Login/Login.php
index 8ed5272..bf72e6a 100644
--- a/application/views/Login/Login.php
+++ b/application/views/Login/Login.php
@@ -44,7 +44,6 @@
                 else {
 					alert('fail')
                 }
-                /* End Check if log in fail */
             }
         });
 
diff --git a/application/views/Project/v_project_manage.php b/application/views/Project/v_project_manage.php
index b27b78d..15611cf 100644
--- a/application/views/Project/v_project_manage.php
+++ b/application/views/Project/v_project_manage.php
@@ -31,13 +31,13 @@
                                         <table id="myTable" class="display">
                                             <thead>
                                                 <tr>
-                                                    <th>No.</th>
-                                                    <th>Student ID</th>
-                                                    <th>Student Name</th>
-                                                    <th>Project Name</th>
-                                                    <th>Manage</th>
-                                                    <th></th>
-                                                    <th></th>
+                                                    <th style="width: 5%;">No.</th>
+                                                    <th style="width: 15%;">Student ID</th>
+                                                    <th style="width: 30%;">Student Name</th>
+                                                    <th style="width: 40%;">Project Name</th>
+                                                    <th style="width: 5%;"></th>
+                                                    <th style="width: 5%;">Manage</th>
+                                                    <th style="width: 5%;"></th>
                                                 
                                                 </tr>
                                             </thead>
diff --git a/application/views/Tag/v_tag_manage.php b/application/views/Tag/v_tag_manage.php
index 6e905ec..98720a3 100644
--- a/application/views/Tag/v_tag_manage.php
+++ b/application/views/Tag/v_tag_manage.php
@@ -31,10 +31,10 @@
                                         <table id="myTable" class="display">
                                             <thead>
                                                 <tr>
-                                                    <th>No</th>
-                                                    <th>Tag Name</th>
-                                                    <th>Status</th>
-                                                    <th>Manage</th>
+                                                    <th style="width: 5%;">No</th>
+                                                    <th style="width: 55%;">Tag Name</th>
+                                                    <th style="width: 30%;">Status</th>
+                                                    <th style="width: 10%; text-align: center">Manage</th>
                                                 </tr>
                                             </thead>
                                             <tbody>
diff --git a/application/views/Team/v_team_manage.php b/application/views/Team/v_team_manage.php
index c668f50..4437855 100644
--- a/application/views/Team/v_team_manage.php
+++ b/application/views/Team/v_team_manage.php
@@ -31,12 +31,12 @@
                                         <table id="myTable" class="display">
                                             <thead>
                                                <tr>
-                                                    <th>No.</th>
-                                                    <th>Logo</th>
-                                                    <th>Team Number</th>
-                                                    <th>Manage</th>
-                                                    <th></th>
-                                                    <th></th>
+                                                    <th style="width: 5%;">No.</th>
+                                                    <th style="width: 30%;">Logo</th>
+                                                    <th style="width: 50%;">Team Number</th>
+                                                    <th style="width: 5%;"></th>
+                                                    <th style="width: 5%;">Manage</th>
+                                                    <th style="width: 5%;"></th>
                                                 
                                                 </tr>
                                             </thead>
diff --git a/application/views/User/v_user_manage.php b/application/views/User/v_user_manage.php
index f07d679..14e1ccc 100644
--- a/application/views/User/v_user_manage.php
+++ b/application/views/User/v_user_manage.php
@@ -31,12 +31,12 @@
                                         <table id="myTable" class="display">
                                             <thead>
                                                 <tr>
-                                                    <th>No.</th>
-                                                    <th>Student ID</th>
-                                                    <th>Student Name</th>
-                                                    <th>Manage</th>
-                                                    <th></th>
-                                                    <th></th>
+                                                    <th style="width: 5%;">No.</th>
+                                                    <th style="width: 15%;">Student ID</th>
+                                                    <th style="width: 65%;">Student Name</th>
+                                                    <th style="width: 5%;"></th>
+                                                    <th style="width: 5%;">Manage</th>
+                                                    <th style="width: 5%;"></th>
                                                 
                                                 </tr>
                                             </thead>
-- 
GitLab