| 
									
										
										
										
											2011-06-17 20:52:00 +08:00
										 |  |  | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Search extends CI_Controller { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 06:12:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->load->helper(array('form', 'url')); | 
					
						
							| 
									
										
										
										
											2021-02-10 01:52:23 +08:00
										 |  |  |         if($this->optionslib->get_option('global_search') != "true") { | 
					
						
							| 
									
										
										
										
											2019-09-12 00:35:31 +08:00
										 |  |  |             $this->load->model('user_model'); | 
					
						
							|  |  |  |             if(!$this->user_model->authorize(2)) { $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); redirect('dashboard'); } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-09-05 06:12:45 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-17 20:52:00 +08:00
										 |  |  | 	public function index() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-11-05 01:32:03 +08:00
										 |  |  | 		$data['page_title'] = "Search"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 18:52:11 +08:00
										 |  |  |         $this->load->view('interface_assets/header', $data); | 
					
						
							| 
									
										
										
										
											2011-06-17 20:52:00 +08:00
										 |  |  | 		$this->load->view('search/main'); | 
					
						
							| 
									
										
										
										
											2019-05-14 18:52:11 +08:00
										 |  |  |         $this->load->view('interface_assets/footer'); | 
					
						
							| 
									
										
										
										
											2011-06-17 20:52:00 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-09-05 06:12:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Filter is for advanced searching and filtering of the logbook
 | 
					
						
							|  |  |  |     public function filter() { | 
					
						
							|  |  |  |         $data['page_title'] = "Search & Filter Logbook"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->load->library('form_validation'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 06:42:11 +08:00
										 |  |  |         $this->load->model('Search_filter'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $data['get_table_names'] = $this->Search_filter->get_table_columns(); | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 		$data['stored_queries'] = $this->Search_filter->get_stored_queries(); | 
					
						
							| 
									
										
										
										
											2019-09-05 06:42:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         //print_r($this->Search_filter->get_table_columns());
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 06:12:45 +08:00
										 |  |  |         if ($this->form_validation->run() == FALSE) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             $this->load->view('interface_assets/header', $data); | 
					
						
							|  |  |  |             $this->load->view('search/filter'); | 
					
						
							|  |  |  |             $this->load->view('interface_assets/footer'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             $this->load->view('interface_assets/header', $data); | 
					
						
							|  |  |  |             $this->load->view('search/filter'); | 
					
						
							|  |  |  |             $this->load->view('interface_assets/footer'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-09-12 07:53:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function json_result() { | 
					
						
							|  |  |  |           if(isset($_POST['search'])) { | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 			  $result = $this->fetchQueryResult($_POST['search'], false); | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 			  echo json_encode($result->result_array()); | 
					
						
							| 
									
										
										
										
											2021-09-26 00:08:31 +08:00
										 |  |  | 		  } | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 	function get_stored_queries() { | 
					
						
							|  |  |  | 		$this->load->model('Search_filter'); | 
					
						
							|  |  |  | 		$data['result'] = $this->Search_filter->get_stored_queries(); | 
					
						
							|  |  |  | 		$this->load->view('search/stored_queries', $data); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function search_result() { | 
					
						
							|  |  |  | 		if(isset($_POST['search'])) { | 
					
						
							|  |  |  | 			$data['results'] = $this->fetchQueryResult($_POST['search'], false); | 
					
						
							|  |  |  | 			$this->load->view('search/search_result_ajax', $data); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 	function export_to_adif() { | 
					
						
							|  |  |  | 		if(isset($_POST['search'])) { | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 			$data['qsos'] = $this->fetchQueryResult($_POST['search'], false); | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 			$this->load->view('adif/data/exportall', $data); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 	function export_stored_query_to_adif() { | 
					
						
							|  |  |  | 		$this->db->where('id', xss_clean($this->input->post('id'))); | 
					
						
							|  |  |  | 		$sql = $this->db->get('queries')->result(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$data['qsos'] = $this->db->query($sql[0]->query); | 
					
						
							|  |  |  | 		$this->load->view('adif/data/exportall', $data); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function run_query() { | 
					
						
							|  |  |  | 		$this->db->where('id', xss_clean($this->input->post('id'))); | 
					
						
							|  |  |  | 		$sql = $this->db->get('queries')->result(); | 
					
						
							| 
									
										
										
										
											2021-09-26 01:49:28 +08:00
										 |  |  | 		$sql = $sql[0]->query; | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-07 22:08:28 +08:00
										 |  |  | 		if (stristr($sql, 'select') && !stristr($sql, 'delete') && !stristr($sql, 'update')) { | 
					
						
							| 
									
										
										
										
											2021-09-26 01:49:28 +08:00
										 |  |  | 			$data['results'] = $this->db->query($sql); | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-26 01:49:28 +08:00
										 |  |  | 			$this->load->view('search/search_result_ajax', $data); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function save_query() { | 
					
						
							|  |  |  | 		if(isset($_POST['search'])) { | 
					
						
							|  |  |  | 			$query = $this->fetchQueryResult($_POST['search'], true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			$data = array( | 
					
						
							|  |  |  | 				'userid' => xss_clean($this->session->userdata('user_id')), | 
					
						
							|  |  |  | 				'query' => $query, | 
					
						
							|  |  |  | 				'description' => xss_clean($_POST['description']) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			$this->db->insert('queries', $data); | 
					
						
							| 
									
										
										
										
											2021-09-26 00:08:31 +08:00
										 |  |  | 			$last_id = $this->db->insert_id(); | 
					
						
							|  |  |  | 			header('Content-Type: application/json'); | 
					
						
							|  |  |  | 			echo json_encode(array('id' => $last_id, 'description' => xss_clean($_POST['description']))); | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function delete_query() { | 
					
						
							|  |  |  | 		$id = xss_clean($this->input->post('id')); | 
					
						
							|  |  |  | 		$this->load->model('search_filter'); | 
					
						
							|  |  |  | 		$this->search_filter->delete_query($id); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-04 02:06:08 +08:00
										 |  |  | 	function save_edited_query() { | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 		$data = array( | 
					
						
							| 
									
										
										
										
											2021-10-04 02:06:08 +08:00
										 |  |  | 			'description' => xss_clean($this->input->post('description')), | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$this->db->where('id', xss_clean($this->input->post('id'))); | 
					
						
							| 
									
										
										
										
											2021-10-04 02:06:08 +08:00
										 |  |  | 		$this->db->where('userid', $this->session->userdata['user_id']); | 
					
						
							|  |  |  | 		$this->db->update('queries', $data); | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 	function buildWhere(array $object, string $condition = null): void | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		/* | 
					
						
							|  |  |  | 		 * The $object is one of the following: | 
					
						
							|  |  |  | 		 * - a group, with 'condition' and 'rules' keys | 
					
						
							|  |  |  | 		 * - a condition, that is either 'AND' or 'OR' depending on the parent group setting | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		$objectIsGroup = isset($object['condition']); | 
					
						
							|  |  |  | 		if ($objectIsGroup) { | 
					
						
							|  |  |  | 			if ($condition === null || $condition === 'AND') { | 
					
						
							|  |  |  | 				$this->db->group_start(); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				$this->db->or_group_start(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			foreach ($object['rules'] as $rule) { | 
					
						
							|  |  |  | 				/* | 
					
						
							|  |  |  | 				 * Now iterate over the children, that are either groups or conditions | 
					
						
							|  |  |  | 				 */ | 
					
						
							|  |  |  | 				$this->buildWhere($rule, $object['condition']); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			$this->db->group_end(); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			$object['field'] = $this->config->item('table_name') . '.' . $object['field']; | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 			if ($object['operator'] == "equal") { | 
					
						
							|  |  |  | 				if ($condition == "AND") { | 
					
						
							|  |  |  | 					$this->db->where($object['field'], $object['value']); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					$this->db->or_where($object['field'], $object['value']); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 			if ($object['operator'] == "not_equal") { | 
					
						
							|  |  |  | 				if ($condition == "AND") { | 
					
						
							|  |  |  | 					$this->db->where($object['field'] . ' !=', $object['value']); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					$this->db->or_where($object['field'] . ' !=', $object['value']); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 			if ($object['operator'] == "begins_with") { | 
					
						
							|  |  |  | 				if ($condition == "AND") { | 
					
						
							|  |  |  | 					$this->db->where($object['field'] . ' like ', $object['value'] . "%"); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					$this->db->or_where($object['field'] . ' like ', $object['value'] . "%"); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 			if ($object['operator'] == "contains") { | 
					
						
							|  |  |  | 				if ($condition == "AND") { | 
					
						
							|  |  |  | 					$this->db->where($object['field'] . ' like ', "%" . $object['value'] . "%"); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					$this->db->or_where($object['field'] . ' like ', "%" . $object['value'] . "%"); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 			if ($object['operator'] == "ends_with") { | 
					
						
							|  |  |  | 				if ($condition == "AND") { | 
					
						
							|  |  |  | 					$this->db->where($object['field'] . ' like ', "%" . $object['value']); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					$this->db->or_where($object['field'] . ' like ', "%" . $object['value']); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if ($object['operator'] == "is_empty") { | 
					
						
							|  |  |  | 				if ($condition == "AND") { | 
					
						
							| 
									
										
										
										
											2022-01-24 03:52:23 +08:00
										 |  |  | 					$this->db->where($object['field'], ''); | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2022-01-24 03:52:23 +08:00
										 |  |  | 					$this->db->or_where($object['field'], ''); | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 			if ($object['operator'] == "is_not_empty") { | 
					
						
							|  |  |  | 				if ($condition == "AND") { | 
					
						
							| 
									
										
										
										
											2022-01-24 03:52:23 +08:00
										 |  |  | 					$this->db->where($object['field'] . ' !=', ''); | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2022-01-24 03:52:23 +08:00
										 |  |  | 					$this->db->or_where($object['field'] . ' !=', ''); | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 			if ($object['operator'] == "is_null") { | 
					
						
							|  |  |  | 				if ($condition == "AND") { | 
					
						
							| 
									
										
										
										
											2022-01-24 03:52:23 +08:00
										 |  |  | 					$this->db->where($object['field'] . ' IS NULL'); | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2022-01-24 03:52:23 +08:00
										 |  |  | 					$this->db->or_where($object['field'] . ' IS NULL'); | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if ($object['operator'] == "is_not_null") { | 
					
						
							|  |  |  | 				if ($condition == "AND") { | 
					
						
							| 
									
										
										
										
											2022-01-24 03:52:23 +08:00
										 |  |  | 					$this->db->where($object['field'] . ' IS NOT NULL'); | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2022-01-24 03:52:23 +08:00
										 |  |  | 					$this->db->or_where($object['field'] . ' IS NOT NULL'); | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if ($object['operator'] == "less") { | 
					
						
							|  |  |  | 				if ($condition == "AND") { | 
					
						
							|  |  |  | 					$this->db->where($object['field'] . ' <', $object['value']); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					$this->db->or_where($object['field'] . ' <', $object['value']); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if ($object['operator'] == "less_or_equal") { | 
					
						
							|  |  |  | 				if ($condition == "AND") { | 
					
						
							|  |  |  | 					$this->db->where($object['field'] . ' <=', $object['value']); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					$this->db->or_where($object['field'] . ' <=', $object['value']); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if ($object['operator'] == "greater") { | 
					
						
							|  |  |  | 				if ($condition == "AND") { | 
					
						
							|  |  |  | 					$this->db->where($object['field'] . ' >', $object['value']); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					$this->db->or_where($object['field'] . ' >', $object['value']); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if ($object['operator'] == "greater_or_equal") { | 
					
						
							|  |  |  | 				if ($condition == "AND") { | 
					
						
							|  |  |  | 					$this->db->where($object['field'] . ' >=', $object['value']); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					$this->db->or_where($object['field'] . ' >=', $object['value']); | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function fetchQueryResult($json, $returnquery) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$search_items = json_decode($json, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$this->buildWhere($search_items); | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 		$this->db->order_by('COL_TIME_ON', 'DESC'); | 
					
						
							|  |  |  | 		$this->db->join('station_profile', 'station_profile.station_id = '.$this->config->item('table_name').'.station_id'); | 
					
						
							| 
									
										
										
										
											2021-09-25 23:40:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if ($returnquery) { | 
					
						
							|  |  |  | 			$query = $this->db->get_compiled_select($this->config->item('table_name')); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			$query = $this->db->get($this->config->item('table_name')); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-09-24 02:18:47 +08:00
										 |  |  | 		return $query; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-12-21 03:15:22 +08:00
										 |  |  | } |