| 
									
										
										
										
											2023-12-16 06:04:34 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-16 16:37:45 +08:00
										 |  |  | defined('BASEPATH') or exit('No direct script access allowed'); | 
					
						
							| 
									
										
										
										
											2023-12-16 06:04:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  *   This adds the hrdlog username to the station profile as this is needed  | 
					
						
							|  |  |  |  *   for special callsigns | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-16 16:37:45 +08:00
										 |  |  | class Migration_hrdlog_username extends CI_Migration | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-12-16 06:04:34 +08:00
										 |  |  | 	public function up() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (!$this->db->field_exists('hrdlog_username', 'station_profile')) { | 
					
						
							|  |  |  | 			$fields = array( | 
					
						
							|  |  |  | 				'hrdlog_username VARCHAR(20) DEFAULT NULL AFTER hrdlog_code', | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			$this->dbforge->add_column('station_profile', $fields); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-12-16 16:37:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// SELECT all rows where hrdlog_code is not empty
 | 
					
						
							|  |  |  | 		$this->db->where("(hrdlog_code IS NOT NULL AND hrdlog_code != '')"); | 
					
						
							|  |  |  | 		$query = $this->db->get('station_profile'); | 
					
						
							|  |  |  | 		$rows = $query->result(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Iterate through all selected rows
 | 
					
						
							|  |  |  | 		foreach ($rows as $row) { | 
					
						
							|  |  |  | 			// Extract the username using the regex pattern
 | 
					
						
							|  |  |  | 			$regex = '/^((\d|[A-Z])+\/)?((\d|[A-Z]){3,})(\/(\d|[A-Z])+)?(\/(\d|[A-Z])+)?$/'; | 
					
						
							|  |  |  | 			preg_match($regex, $row->station_callsign, $matches); | 
					
						
							|  |  |  | 			$username = $matches[3]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Update the row with the extracted username
 | 
					
						
							|  |  |  | 			$this->db->where('station_id', $row->station_id); | 
					
						
							|  |  |  | 			$this->db->update('station_profile', array('hrdlog_username' => $username)); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-12-16 06:04:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public function down() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if ($this->db->field_exists('hrdlog_username', 'station_profile')) { | 
					
						
							|  |  |  | 			$this->dbforge->drop_column('station_profile', 'hrdlog_username'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-12-16 16:37:45 +08:00
										 |  |  | } |