From f2b0b5791283a06da0d5207fc231bb0affa6bb9e Mon Sep 17 00:00:00 2001
From: Nitipon094 <62160094@go.buu.ac.th>
Date: Wed, 8 Mar 2023 04:34:26 +0700
Subject: [PATCH] search

---
 html/exhi/application/controllers/General.php | 113 ++++++++----
 .../views/general/v_general_home.php          |  77 +++------
 .../application/views/general/v_project_2.php |   4 +
 .../application/views/general/v_project_4.php |  11 +-
 .../application/views/general/v_search.php    | 162 +++++++++++++++++-
 5 files changed, 271 insertions(+), 96 deletions(-)

diff --git a/html/exhi/application/controllers/General.php b/html/exhi/application/controllers/General.php
index 7a482c3..a18295c 100644
--- a/html/exhi/application/controllers/General.php
+++ b/html/exhi/application/controllers/General.php
@@ -113,38 +113,83 @@ class general extends Exhibition_Controller
 
     public function get_search()
     {
-        // $year = $_GET['year'];
-        // $this->load->model('general/M_general', 'num');
-        // $data['num'] = $this->num->get_num_page($year)->result();
-        // echo json_encode($data);
-        $this->load->view('general/v_search');
-    }
-
-	public function show_select($id){
-		$this->load->model('project/M_project','project');
-		$this->load->model('project_tag/M_project_tag','project_tag');
-		$this->load->model('detail/M_detail','detail');
-		$this->load->model('member/M_member','member');
-		$this->load->model('cluster/M_cluster','cluster');
-		$this->load->model('team/M_team','team');
-		$this->load->model('instructor/M_instructor','instructor');
-		$data['project'] =  $this->project->get_project_by_id($id)->row();
-		$select = '';
-		if($data['project']->cluster_id != 0){
-			$select = $this->cluster->get_cluster_by_id($data['project']->cluster_id)->row();
-		}else if($data['project']->team_id != 0){
-			$select  = $this->team->get_team_by_id($data['project']->team_id)->row();
-		}else{
-			$select  = $this->instructor->get_by_id($data['project']->instructor_id)->row();
-		}
-		$data['project']->select = $select->name;
-		$data['project_tag'] =  $this->project_tag->get_by_id($id)->result();
-		$data['detail'] = $this->detail->get_detail_by_project_id($id)->result();
-		$data['member'] = $this->member->get_member_by_project_id($id)->result();
-		// echo '<pre>';
-		// print_r($data);
-		// echo '</pre>';
-		
-		$this->output_frontend('general/v_general_select', $data);
-	}
+        $data['keyword'] = $this->input->post();
+        $this->load->view('general/v_search', $data);
+    }
+
+    public function get_search_data()
+    {
+        $year = $this->input->post('year_search');
+        if ($year == '') {
+            $year = 'YEAR(date)';
+        }
+        $grade = $this->input->post('grade');
+        if ($grade == '2') {
+            $grade = 'AND cluster_id != 0';
+        } else if ($grade == '3') {
+            $grade = 'AND team_id != 0';
+        } else if ($grade = 'cluster != 0' == '4') {
+            $grade = 'AND cluster_id = 0 AND team_id = 0';
+        } else {
+            $grade = '';
+        }
+        $cluster = $this->input->post('cluster');
+        if ($cluster == '') {
+            $cluster = 'cluster_id';
+        }
+        $team = $this->input->post('team');
+        if ($team == '') {
+            $team = 'team_id';
+        }
+        $company = $this->input->post('company');
+        if ($company == '') {
+            $company = 'company_id';
+        }
+        $pname = $this->input->post('pname');
+        if ($pname == '') {
+            $pname = 'name';
+        }
+        $tag = $this->input->post('tag');
+        if ($tag == '') {
+            $tag = 'project_tag.tag_id';
+        }
+
+        $this->load->model('general/M_general', 'search');
+        $data['search'] = $this->search->get_search($year, $grade, $cluster, $team, $company, $pname, $tag)->result();
+        $data['keyword'] = $this->input->post();
+        // echo "<pre>";
+        // echo print_r($this->input->post());
+        // echo "</pre>";
+
+        $this->load->view('general/v_search', $data);
+    }
+
+    public function show_select($id)
+    {
+        $this->load->model('project/M_project', 'project');
+        $this->load->model('project_tag/M_project_tag', 'project_tag');
+        $this->load->model('detail/M_detail', 'detail');
+        $this->load->model('member/M_member', 'member');
+        $this->load->model('cluster/M_cluster', 'cluster');
+        $this->load->model('team/M_team', 'team');
+        $this->load->model('instructor/M_instructor', 'instructor');
+        $data['project'] =  $this->project->get_project_by_id($id)->row();
+        $select = '';
+        if ($data['project']->cluster_id != 0) {
+            $select = $this->cluster->get_cluster_by_id($data['project']->cluster_id)->row();
+        } else if ($data['project']->team_id != 0) {
+            $select  = $this->team->get_team_by_id($data['project']->team_id)->row();
+        } else {
+            $select  = $this->instructor->get_by_id($data['project']->instructor_id)->row();
+        }
+        $data['project']->select = $select->name;
+        $data['project_tag'] =  $this->project_tag->get_by_id($id)->result();
+        $data['detail'] = $this->detail->get_detail_by_project_id($id)->result();
+        $data['member'] = $this->member->get_member_by_project_id($id)->result();
+        // echo '<pre>';
+        // print_r($data);
+        // echo '</pre>';
+
+        $this->output_frontend('general/v_general_select', $data);
+    }
 }
diff --git a/html/exhi/application/views/general/v_general_home.php b/html/exhi/application/views/general/v_general_home.php
index f25b12e..e65a20e 100644
--- a/html/exhi/application/views/general/v_general_home.php
+++ b/html/exhi/application/views/general/v_general_home.php
@@ -164,7 +164,7 @@
     }
 
     ul.cloud[data-show-value] a::after {
-        content: " (" attr(data-weight) ")";
+        content: " ("attr(data-weight) ")";
         font-size: 1rem;
     }
 
@@ -235,7 +235,7 @@
 <div class="content-wrapper">
     <div class="content">
         <div class="container-fluid g-3">
-            <button class="button button-mid" data-toggle="modal" data-target="#advanceSearcheModal">Advance Search</button><br><br><br><br>
+            <button class="button button-mid" data-toggle="modal" data-target="#advanceSearcheModal">ค้นหาขั้นสูง</button><br><br><br><br>
         </div>
         <div class="year">
             <label for="year">ปี: </label>
@@ -266,7 +266,7 @@
             </div>
         </div>
         <div class="row justify-content-around">
-            <a href="<?php echo site_url() . '/General/show_project_2' ?>" class="col-md-4">
+            <a id="project_2" href="<?php echo site_url() . '/General/show_project_2' ?>" class="col-md-4">
                 <div class="card card-widget widget-user">
                     <div class="widget-user-header text-white" style="background: url('https://images.theconversation.com/files/496840/original/file-20221122-22-gpfi1q.jpg?ixlib=rb-1.1.0&rect=198%2C77%2C5328%2C2664&q=45&auto=format&w=668&h=324&fit=crop') center center;">
                     </div>
@@ -287,7 +287,7 @@
                     </div>
                 </div>
             </a>
-            <a href="<?php echo site_url() . '/General/show_project_3' ?>" class="col-md-4">
+            <a id="project_3" href="<?php echo site_url() . '/General/show_project_3' ?>" class="col-md-4">
                 <div class="card card-widget widget-user">
                     <div class="widget-user-header text-white" style="background: url('https://www.investopedia.com/thmb/SPOl62NtucSLHi9-XyGJxm-Wo68=/1500x0/filters:no_upscale():max_bytes(150000):strip_icc()/GettyImages-943067460-28883b8136b24330932cd4e2855c2508.jpg') center center;">
                     </div>
@@ -308,7 +308,7 @@
                     </div>
                 </div>
             </a>
-            <a href="<?php echo site_url() . '/General/show_project_4' ?>" class="col-md-4">
+            <a id="project_4" href="<?php echo site_url() . '/General/show_project_4' ?>" class="col-md-4">
                 <div class="card card-widget widget-user">
                     <div class="widget-user-header text-white" style="background: url('https://images.theconversation.com/files/496840/original/file-20221122-22-gpfi1q.jpg?ixlib=rb-1.1.0&rect=198%2C77%2C5328%2C2664&q=45&auto=format&w=668&h=324&fit=crop') center center;">
                     </div>
@@ -357,18 +357,18 @@
     <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
         <div class="modal-content">
             <div class="modal-header">
-                <h5 class="modal-title" id="exampleModalLongTitle">Advance Search</h5>
+                <h5 class="modal-title" id="exampleModalLongTitle">ค้นหาขั้นสูง</h5>
                 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                     <span aria-hidden="true">&times;</span>
                 </button>
             </div>
             <div class="modal-body">
-                <form action="<?php echo site_url() . '/General/get_search' ?>">
+                <form method="POST" action="<?php echo site_url() . '/General/get_search' ?>">
                     <div class="row">
                         <div class="col-6">
-                            <label class="form-label" for="grad">ชั้นปี: </label>
-                            <select class="form-control" name="grad" id="">
-                                <option value="all">ทั้งหมด</option>
+                            <label class="form-label" for="grade">ชั้นปี: </label>
+                            <select class="form-control" name="grade" id="grade">
+                                <option value="">ทั้งหมด</option>
                                 <option value="2">2</option>
                                 <option value="3">3</option>
                                 <option value="4">4</option>
@@ -377,7 +377,7 @@
                         <div class="col-6">
                             <label class="form-label" for="year_search">ปี: </label>
                             <select class="form-control" name="year_search" id="year_search">
-                                <option value="all">ทั้งหมด</option>
+                                <option value="">ทั้งหมด</option>
                             </select>
                         </div>
                     </div><br>
@@ -385,7 +385,7 @@
                         <div class="col-6">
                             <label class="form-label" for="cluster">มกุล: </label>
                             <select class="form-control" name="cluster" id="">
-                                <option value="all">ทั้งหมด</option>
+                                <option value="">ทั้งหมด</option>
                                 <option value="0">0</option>
                                 <option value="1">1</option>
                                 <option value="2">2</option>
@@ -401,8 +401,8 @@
                         <div class="col-md-6" id="team">
                             <div class="form-group">
                                 <label class="form-label">ทีม: </label>
-                                <select class="form-control" id="team_select">
-                                    <option value="all">ทั้งหมด</option>
+                                <select class="form-control" id="team_select" name="team">
+                                    <option value="">ทั้งหมด</option>
                                     <?php foreach ($teams as $team) { ?>
                                         <option value="<?php echo $team->team_id ?>"><?php echo $team->name ?></option>
                                     <?php } ?>
@@ -410,52 +410,31 @@
                             </div>
                         </div>
                     </div>
-                    <!-- <div class="row p-t-20 center"> -->
-                    <!-- <div class="col-md-6"> -->
                     <div class="form-group has-success">
                         <label class="form-label">บริษัท: </label>
-                        <select class="form-control" id="company">
-                            <option value="all">ทั้งหมด</option>
+                        <select class="form-control" id="company" name="company">
+                            <option value="">ทั้งหมด</option>
                             <?php foreach ($companies as $company) { ?>
                                 <option value="<?php echo $company->company_id ?>"><?php echo $company->name ?></option>
                             <?php } ?>
                         </select>
                     </div>
-                    <!-- </div> -->
-                    <!-- <div class="col-md-6">
-                            <div class="form-group has-success">
-                                <label class="form-label">หมวดหมู่: </label>
-                                <select class="js-example-basic-multiple form-control" name="states[]" multiple="multiple" id="muti">
-                                    <?php //foreach ($tags as $tag) { 
-                                    ?>
-                                        <option value="<?php //echo $tag->tag_id 
-                                                        ?>"><?php //echo $tag->name 
-                                                            ?></option>
-                                    <?php //  } 
-                                    ?>
-                                </select>
-                            </div>
-                        </div> -->
-                    </label>
-
-                    <!-- </div> -->
                     <div class="form-group has-success">
                         <label class="form-label">ชื่อโปรเจกต์: </label>
                         <input name="pname" type="text" class="form-control">
                     </div>
                     <div class="form-group has-success">
                         <label class="form-label">หมวดหมู่: </label>
-                        <select class="js-example-basic-multiple form-control" name="states[]" multiple="multiple" id="muti">
+                        <select class="js-example-basic-multiple form-control" name="tag" multiple="multiple" id="muti">
                             <?php foreach ($tags as $tag) { ?>
                                 <option value="<?php echo $tag->tag_id ?>"><?php echo $tag->name ?></option>
                             <?php  } ?>
                         </select>
                     </div>
-
                     <hr>
                     <div class="row justify-content-between">
                         <a type="hidden"></a>
-                        <button type="submit" class="btn btn-primary">ค้นหา</button>
+                        <button class="btn btn-primary">ค้นหา</button>
                     </div>
                 </form>
             </div>
@@ -466,7 +445,10 @@
     $(document).ready(function() {
         get_project_year();
         $("#muti").select2().select2()
-
+        var year = document.getElementById("year").value;
+        $('#project_2').attr('href', '<?php echo site_url() . "/General/show_project_2" ?>?y=' + year);
+        $('#project_3').attr('href', '<?php echo site_url() . "/General/show_project_3" ?>?y=' + year);
+        $('#project_4').attr('href', '<?php echo site_url() . "/General/show_project_4" ?>?y=' + year);
     });
 
     function get_project_year() {
@@ -488,6 +470,9 @@
 
     function get_num_project() {
         var year = document.getElementById("year").value;
+        $('#project_2').attr('href', '<?php echo site_url() . "/General/show_project_2" ?>?y=' + year);
+        $('#project_3').attr('href', '<?php echo site_url() . "/General/show_project_3" ?>?y=' + year);
+        $('#project_4').attr('href', '<?php echo site_url() . "/General/show_project_4" ?>?y=' + year);
         $.ajax({
             type: 'get',
             url: "<?php echo site_url() . '/General/get_num_project'; ?>",
@@ -524,18 +509,6 @@
         })
     }
 
-    function search() {
-        // var year = document.getElementById("year").value;
-        $.ajax({
-            type: 'post',
-            url: "<?php echo site_url() . '/General/get_search'; ?>",
-            data: {
-                // 'year': year
-            },
-            // dataType: 'json',
-        })
-    }
-
     function create_num_product(data, year) {
         let thai_year = parseInt(year) + 543;
         let selectDiv_2 = document.querySelector('#num_project_2')
diff --git a/html/exhi/application/views/general/v_project_2.php b/html/exhi/application/views/general/v_project_2.php
index 58a991a..ee6d5ee 100644
--- a/html/exhi/application/views/general/v_project_2.php
+++ b/html/exhi/application/views/general/v_project_2.php
@@ -35,6 +35,7 @@
         text-align: justify;
     } */
 </style>
+<?php $y = $_GET['y']; ?>
 <div class="content-wrapper h-100">
     <div class="content-header">
         <div class="container-fluid g-3">
@@ -99,6 +100,9 @@
         data.forEach((row, index) => {
             option = document.createElement("option")
             option.setAttribute("value", row.year)
+            if (row.year == <?php echo $y ?>) {
+                option.setAttribute("selected", "selected")
+            }
             const number = parseInt(row.year) + 543
             option.text = number
             selectList.appendChild(option)
diff --git a/html/exhi/application/views/general/v_project_4.php b/html/exhi/application/views/general/v_project_4.php
index aab3da0..bd9cb1d 100644
--- a/html/exhi/application/views/general/v_project_4.php
+++ b/html/exhi/application/views/general/v_project_4.php
@@ -47,17 +47,10 @@
             <br><br>
         </div>
         <div id="project" class="row"></div>
-        <!-- <div id="select-page"> -->
         <nav aria-label="Page navigation">
             <ul class="pagination" id="select-page">
-                <!-- <li><button class="page-link" id="select-page">Previous</button></li>
-                <li><button class="page-link">1</button></li>
-                <li><button class="page-link">2</button></li>
-                <li><button class="page-link">3</button></li>
-                <li><button class="page-link" id="next-page">Next</button></li> -->
             </ul>
         </nav>
-        <!-- </div> -->
     </div>
 </div>
 <div id="data-container"></div>
@@ -87,8 +80,8 @@
                     h2 = document.createElement("h2")
                     h2.setAttribute("class", "notfound")
                     const text = document.createTextNode('- ไม่พบข้อมูล -')
-                    h5.appendChild(text)
-                    selectList.appendChild(h5)
+                    h2.appendChild(text)
+                    selectList.appendChild(h)
                 }
 
             }
diff --git a/html/exhi/application/views/general/v_search.php b/html/exhi/application/views/general/v_search.php
index b72d075..b3552d2 100644
--- a/html/exhi/application/views/general/v_search.php
+++ b/html/exhi/application/views/general/v_search.php
@@ -1 +1,161 @@
-<h1>-Advance Search-</h1>
\ No newline at end of file
+<style>
+    a,
+    a:hover {
+        color: black;
+    }
+
+    .content {
+        max-width: 1500px;
+        max-height: auto;
+        margin: auto;
+        background: white;
+        padding: 10px;
+    }
+
+    .notfound {
+        text-align: center;
+        font-weight: bold;
+    }
+
+    .description-block {
+        text-align: left;
+    }
+</style>
+<pre>
+    <?php
+    echo print_r($keyword);
+    ?>
+</pre>
+<div class="content-wrapper h-100">
+    <div class="content-header">
+        <div class="container-fluid g-3">
+            Home/ <a href="<?php echo site_url() . '/General/show_general_home/' ?>">Dashboard</a> /ค้นหาขั้นสูง
+        </div>
+    </div>
+    <div class="content">
+        <div class="mb-3"></div>
+        <div class="card-container">
+            <div>
+                <label for="limit">จำนวนต่อหน้า: </label>
+                <select name="limit" id="limit" onchange="load_num_project(), reset_page(), load_data()">
+                    <option value="10">10</option>
+                    <option value="20">20</option>
+                    <option value="50">50</option>
+                    <option value="100">100</option>
+                </select>
+            </div>
+            <br><br>
+        </div>
+        <div id="project" class="row"></div>
+        <nav aria-label="Page navigation">
+            <ul class="pagination" id="select-page">
+            </ul>
+        </nav>
+    </div>
+</div>
+<div id="data-container"></div>
+<script>
+    $(document).ready(function() {
+
+    });
+
+
+    let current_page = 1;
+
+    function load_data() {
+        var year = document.getElementById("year").value;
+        var limit = document.getElementById("limit").value;
+        $.ajax({
+            type: "get",
+            url: "<?php echo site_url() . '/General/get_data_pagination' ?>",
+            data: {
+                'year': year,
+                'page': current_page,
+                'limit': limit
+            },
+            dataType: "json",
+            success: function(data) {
+                create_project(data.pagination)
+            }
+        });
+    }
+
+    function load_num_project() {
+        var limit = document.getElementById("limit").value;
+        $.ajax({
+            type: "get",
+            url: "<?php echo site_url() . '/General/get_num_page' ?>",
+            data: {
+
+            },
+            dataType: "json",
+            success: function(data) {
+                console.log(data)
+                let num = Math.ceil(data.num[0].num_project / limit);
+                create_page_select(num);
+            }
+        });
+    }
+
+    function reset_page() {
+        current_page = 1;
+    }
+
+    function set_page(page) {
+        current_page = page;
+        console.log('page' + page);
+        load_data();
+    }
+
+    function previous_page() {
+        if (current_page > 1) {
+            current_page--;
+            console.log('page' + current_page);
+            load_data();
+        }
+    }
+
+    function next_page(maxPage) {
+        if (current_page < maxPage) {
+            current_page++;
+            console.log('page' + current_page);
+            load_data();
+        }
+    }
+
+
+    function create_project(data) {
+        let html = '';
+        data.forEach((row, index) => {
+            html += `<a href="<?php echo site_url() . '/General/show_project_4' ?>" class="col-md-2">`;
+            html += `<div class="card card-widget widget-user">`;
+            html += `<div class="widget-user-header text-white" style="background: url('` + row.image_cover + `') center center;">`;
+            html += `</div>`;
+            html += `<div class="widget-user-image">`;
+            html += `<img class="img-circle" src="` + row.image_logo + `" alt="User Avatar">`;
+            html += `</div>`;
+            html += `<div class="card-body">`;
+            html += `<h5 class="description-header"><b>` + row.name + `</b></h5><br>`;
+            html += `<div class="description-block">`;
+            html += `<h5 class="description-header">` + row.first_name + ' ' + row.last_name + `</h5>`;
+            html += `<h5 class="description-header">` + row.student_id + `</h5>`;
+            html += `</div>`;
+            html += `</div>`;
+            html += `</div>`;
+            html += `</a>`;
+        })
+        document.getElementById('project').innerHTML = html;
+    }
+
+    function create_page_select(data) {
+        let html = '';
+        html += `<li><button class="page-link" onclick="previous_page()">ก่อนหน้า</button></li>`;
+        for (i = 0; i < data; i++) {
+            let page = i + 1;
+            console.log('i' + i);
+            html += `<li><button class="page-link" onclick="set_page(` + page + `)">` + page + `</button></li>`;
+        }
+        html += `<li><button class="page-link" onclick="next_page(` + data + `)">ถัดไป</button></li>`;
+        document.getElementById('select-page').innerHTML = html;
+    }
+</script>
\ No newline at end of file
-- 
GitLab