|
| 1 | +<?php |
| 2 | + |
| 3 | +defined('BASEPATH') or exit('No direct script access allowed'); |
| 4 | + |
| 5 | +// Remove Dupes from eQSL-Table and add a unique idx |
| 6 | + |
| 7 | +class Migration_rm_eqsl_dbl extends CI_Migration |
| 8 | +{ |
| 9 | + |
| 10 | + public function up() |
| 11 | + { |
| 12 | + $dbltrbl = 1; |
| 13 | + while ($dbltrbl>0) { |
| 14 | + $sql = 'SELECT MIN(id) as id,qso_id FROM eQSL_images GROUP BY qso_id HAVING count(1)>1;'; |
| 15 | + $query = $this->db->query($sql); |
| 16 | + $dbltrbl = $query->num_rows(); |
| 17 | + |
| 18 | + if ($dbltrbl > 0) { |
| 19 | + $eqsl2del = []; |
| 20 | + |
| 21 | + foreach ($query->result() as $row) |
| 22 | + $eqsl2del[] = $row->id; |
| 23 | + |
| 24 | + foreach ($eqsl2del as $oneeqsl) { |
| 25 | + $res = $this->db->query("SELECT image_file FROM eQSL_images WHERE id=?", $oneeqsl)->result()[0]->image_file; |
| 26 | + |
| 27 | + if ($this->config->item('userdata')) { |
| 28 | + |
| 29 | + $userdata_dir = $this->config->item('userdata'); |
| 30 | + $qso_id = $this->get_qsoid_from_eqsl_filename($res) ?? ''; |
| 31 | + |
| 32 | + // we need to get the user ID which corresponds to that particular qso |
| 33 | + if (!empty($qso_id)) { |
| 34 | + $get_user_id = $this->get_user_id_from_qso($qso_id); |
| 35 | + |
| 36 | + // can be an deleted qso |
| 37 | + if (!empty($get_user_id)) { |
| 38 | + $user_id = $get_user_id; |
| 39 | + } else { |
| 40 | + $user_id = 'not_assigned'; |
| 41 | + } |
| 42 | + } else { |
| 43 | + $user_id = 'not_assigned'; |
| 44 | + } |
| 45 | + |
| 46 | + // target path with userdata dir |
| 47 | + $target_path = $userdata_dir . '/' . $user_id . '/eqsl_card'; |
| 48 | + |
| 49 | + // then remove the file |
| 50 | + if (!unlink($target_path . '/' . $res)) { |
| 51 | + log_message('error', "Mig 186: File: '" . $target_path . "/" . $res . "' could not be deleted. There is no file with this filename. This shouldn't be a problem."); |
| 52 | + } else { |
| 53 | + log_message('debug', "Mig 186: File: '" . $target_path . "/" . $res . "' was deleted because it was a dupe."); |
| 54 | + } |
| 55 | + } else { |
| 56 | + // if 'userdata' is disabled we can use the old paths |
| 57 | + if (!unlink('images/eqsl_card_images/' . $res)) { |
| 58 | + log_message('error', "Mig 186: File: 'images/eqsl_card_images/" . $res . "' could not be deleted. There is no file with this filename. This shouldn't be a problem."); |
| 59 | + } else { |
| 60 | + log_message('debug', "Mig 186: File: 'images/eqsl_card_images/" . $res . "' was deleted because it was a dupe."); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + foreach ($eqsl2del as $oneeqsl) { |
| 65 | + $this->db->query('delete from eQSL_images where id=?',$oneeqsl); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + $index = $this->db->query("SHOW INDEX FROM eQSL_images WHERE Key_name = 'qso_id_UNIQUE'")->num_rows(); |
| 71 | + if ($index == 0) { |
| 72 | + $this->db->query("ALTER TABLE `eQSL_images` ADD UNIQUE INDEX `qso_id_UNIQUE` (`qso_id` ASC);"); |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + public function down() |
| 78 | + { |
| 79 | + $index = $this->db->query("SHOW INDEX FROM eQSL_images WHERE Key_name = 'qso_id_UNIQUE'")->num_rows(); |
| 80 | + if ($index > 0) { |
| 81 | + $this->db->query("ALTER TABLE `eQSL_images` DROP INDEX `qso_id_UNIQUE`;"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + function get_qsoid_from_eqsl_filename($filename) |
| 86 | + { |
| 87 | + |
| 88 | + $sql = "SELECT qso_id FROM eQSL_images WHERE image_file = ?"; |
| 89 | + |
| 90 | + $result = $this->db->query($sql, $filename); |
| 91 | + |
| 92 | + $row = $result->row(); |
| 93 | + return $row->qso_id; |
| 94 | + } |
| 95 | + |
| 96 | + function get_user_id_from_qso($qso_id) |
| 97 | + { |
| 98 | + |
| 99 | + $clean_qsoid = $this->security->xss_clean($qso_id); |
| 100 | + |
| 101 | + $sql = 'SELECT station_profile.user_id |
| 102 | + FROM ' . $this->config->item('table_name') . ' |
| 103 | + INNER JOIN station_profile ON (' . $this->config->item('table_name') . '.station_id = station_profile.station_id) |
| 104 | + WHERE ' . $this->config->item('table_name') . '.COL_PRIMARY_KEY = ?'; |
| 105 | + |
| 106 | + $result = $this->db->query($sql, $clean_qsoid); |
| 107 | + $row = $result->row(); |
| 108 | + |
| 109 | + return $row->user_id; |
| 110 | + } |
| 111 | +} |
0 commit comments