| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Dxatlas extends CI_Controller { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-05 16:43:36 +08:00
										 |  |  | 	public function index()	{ | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 		$this->load->model('user_model'); | 
					
						
							| 
									
										
										
										
											2021-09-05 16:43:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if(!$this->user_model->authorize(99)) { $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); redirect('dashboard'); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 		$this->load->model('modes'); | 
					
						
							|  |  |  | 		$this->load->model('logbook_model'); | 
					
						
							| 
									
										
										
										
											2021-09-05 16:43:36 +08:00
										 |  |  | 		$this->load->model('stations'); | 
					
						
							| 
									
										
										
										
											2021-11-13 22:55:17 +08:00
										 |  |  | 		$this->load->model('bands'); | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 16:27:45 +08:00
										 |  |  | 		$data['station_profile'] = $this->stations->all_of_user();			// Used in the view for station location select
 | 
					
						
							| 
									
										
										
										
											2021-11-13 22:55:17 +08:00
										 |  |  | 		$data['worked_bands'] = $this->bands->get_worked_bands(); 	// Used in the view for band select
 | 
					
						
							| 
									
										
										
										
											2021-09-05 02:26:51 +08:00
										 |  |  | 		$data['modes'] = $this->modes->active(); 					// Used in the view for mode select
 | 
					
						
							|  |  |  | 		$data['dxcc'] = $this->logbook_model->fetchDxcc(); 			// Used in the view for dxcc select
 | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		$data['page_title'] = "DX Atlas Gridsquare Export"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$this->load->view('interface_assets/header', $data); | 
					
						
							|  |  |  | 		$this->load->view('dxatlas/index'); | 
					
						
							|  |  |  | 		$this->load->view('interface_assets/footer'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-05 20:56:30 +08:00
										 |  |  | 	public function export()  { | 
					
						
							| 
									
										
										
										
											2021-09-04 15:30:51 +08:00
										 |  |  | 		$this->load->model('dxatlas_model'); | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Parameters
 | 
					
						
							| 
									
										
										
										
											2021-09-05 16:43:36 +08:00
										 |  |  | 		$station_id = $this->security->xss_clean($this->input->post('station_profile')); | 
					
						
							|  |  |  | 		$band = $this->security->xss_clean($this->input->post('band')); | 
					
						
							|  |  |  | 		$mode = $this->security->xss_clean($this->input->post('mode')); | 
					
						
							|  |  |  | 		$dxcc = $this->security->xss_clean($this->input->post('dxcc_id')); | 
					
						
							|  |  |  | 		$cqz = $this->security->xss_clean($this->input->post('cqz')); | 
					
						
							|  |  |  | 		$propagation = $this->security->xss_clean($this->input->post('prop_mode')); | 
					
						
							|  |  |  | 		$fromdate = $this->security->xss_clean($this->input->post('fromdate')); | 
					
						
							|  |  |  | 		$todate = $this->security->xss_clean($this->input->post('todate')); | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Get QSOs with Valid QRAs
 | 
					
						
							| 
									
										
										
										
											2021-09-05 16:43:36 +08:00
										 |  |  | 		$grids = $this->dxatlas_model->get_gridsquares($station_id, $band, $mode, $dxcc, $cqz, $propagation, $fromdate, $todate); | 
					
						
							| 
									
										
										
										
											2021-09-05 02:26:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		$this->generateFiles($grids['worked'], $grids['confirmed'], $band); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function generateFiles($wkdArray, $cfmArray, $band) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$gridCfmArray = []; | 
					
						
							|  |  |  | 		$gridWkdArray = []; | 
					
						
							|  |  |  | 		$fieldCfmArray = []; | 
					
						
							|  |  |  | 		$fieldWkdArray = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		foreach ($cfmArray as $grid) { | 
					
						
							|  |  |  | 			$field = substr($grid, 0, 2); | 
					
						
							|  |  |  | 			if (!in_array($field, $fieldCfmArray)) { | 
					
						
							|  |  |  | 				$fieldCfmArray[] = $field; | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-09-05 02:26:51 +08:00
										 |  |  | 			$gridCfmArray[] = $grid; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-05 02:26:51 +08:00
										 |  |  | 		foreach ($wkdArray as $grid) { | 
					
						
							|  |  |  | 			$field = substr($grid, 0, 2); | 
					
						
							|  |  |  | 			if (!in_array($field, $fieldCfmArray)) { | 
					
						
							|  |  |  | 				if (!in_array($field, $fieldWkdArray)) { | 
					
						
							|  |  |  | 					$fieldWkdArray[] = $field; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (!in_array($grid, $gridCfmArray)) { | 
					
						
							|  |  |  | 				$gridWkdArray[] = $grid; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-05 02:26:51 +08:00
										 |  |  | 		$gridWkdString = ''; | 
					
						
							|  |  |  | 		$gridCfmString = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		asort($gridWkdArray); | 
					
						
							|  |  |  | 		asort($gridCfmArray); | 
					
						
							|  |  |  | 		asort($fieldWkdArray); | 
					
						
							|  |  |  | 		asort($fieldCfmArray); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		foreach ($fieldWkdArray as $fields) { | 
					
						
							|  |  |  | 			$gridWkdString .= $fields . "\r\n"; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-05 02:26:51 +08:00
										 |  |  | 		foreach ($gridWkdArray as $grids) { | 
					
						
							|  |  |  | 			$gridWkdString .= $grids . "\r\n"; | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-05 02:26:51 +08:00
										 |  |  | 		foreach ($fieldCfmArray as $fields) { | 
					
						
							|  |  |  | 			$gridCfmString .= $fields . "\r\n"; | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-09-05 02:26:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		foreach ($gridCfmArray as $grids) { | 
					
						
							|  |  |  | 			$gridCfmString .= $grids . "\r\n"; | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-05 02:26:51 +08:00
										 |  |  | 		$this->makeZip($gridWkdString, $gridCfmString, $band); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function makeZip($gridWkdString, $gridCfmString, $band) { | 
					
						
							| 
									
										
										
										
											2021-09-05 20:56:30 +08:00
										 |  |  | 		$zipFileName = 'dxatlas_gridsquares_'. $band . '.zip'; | 
					
						
							| 
									
										
										
										
											2021-09-05 02:26:51 +08:00
										 |  |  | 		// Prepare File
 | 
					
						
							|  |  |  | 		$file = tempnam("tmp", "zip"); | 
					
						
							|  |  |  | 		$zip = new ZipArchive(); | 
					
						
							|  |  |  | 		$zip->open($file, ZipArchive::OVERWRITE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Stuff with content
 | 
					
						
							|  |  |  | 		$zip->addFromString($band . '_grids.wkd', $gridWkdString); | 
					
						
							|  |  |  | 		$zip->addFromString($band . '_grids.cfm', $gridCfmString); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Close and send to users
 | 
					
						
							|  |  |  | 		$zip->close(); | 
					
						
							|  |  |  | 		$length = filesize($file); | 
					
						
							|  |  |  | 		header('Content-Type: application/zip'); | 
					
						
							|  |  |  | 		header('Content-Length: ' . $length); | 
					
						
							|  |  |  | 		header('Content-Disposition: attachment; filename="' . $zipFileName . '"'); | 
					
						
							|  |  |  | 		readfile($file); | 
					
						
							|  |  |  | 		unlink($file); | 
					
						
							| 
									
										
										
										
											2021-07-29 18:05:52 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |