3 # Copyright 2009 Foundations Bible College.
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 my $sth = C4
::Context
->dbh;
24 # NOTE: As long as we die on error *before* the DROP TABLE instructions are executed, the script may simply be rerun after addressing whatever errors occur; If we get past the data conversion without error, the DROPs and ALTERs could be executed manually if need be.
26 # Turn off key checks for duration of script...
28 SET UNIQUE_CHECKS = 0;
29 ") or die "DB ERROR: " . $sth->errstr . "\n";
32 SET FOREIGN_KEY_CHECKS = 0;
33 ") or die "DB ERROR: " . $sth->errstr . "\n";
35 # Create new tables with temporary names...
37 DROP TABLE IF EXISTS labels_batches_tmp;");
39 CREATE TABLE `labels_batches_tmp` (
40 `label_id` int(11) NOT NULL auto_increment,
41 `batch_id` int(10) NOT NULL default '1',
42 `item_number` int(11) NOT NULL default '0',
43 `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
44 `branch_code` varchar(10) NOT NULL default 'NB',
45 PRIMARY KEY USING BTREE (`label_id`),
46 KEY `branch_fk_constraint` (`branch_code`),
47 KEY `item_fk_constraint` (`item_number`),
48 FOREIGN KEY (`branch_code`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE,
49 FOREIGN KEY (`item_number`) REFERENCES `items` (`itemnumber`) ON DELETE CASCADE
50 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
51 ") or die "DB ERROR: " . $sth->errstr . "\n";
54 DROP TABLE IF EXISTS labels_layouts_tmp;");
56 CREATE TABLE `labels_layouts_tmp` (
57 `layout_id` int(4) NOT NULL auto_increment,
58 `barcode_type` char(100) NOT NULL default 'CODE39',
59 `printing_type` char(32) NOT NULL default 'BAR',
60 `layout_name` char(20) NOT NULL default 'DEFAULT',
61 `guidebox` int(1) default '0',
62 `font` char(10) character set utf8 collate utf8_unicode_ci NOT NULL default 'TR',
63 `font_size` int(4) NOT NULL default '10',
64 `callnum_split` int(1) default '0',
65 `text_justify` char(1) character set utf8 collate utf8_unicode_ci NOT NULL default 'L',
66 `format_string` varchar(210) NOT NULL default 'barcode',
67 PRIMARY KEY USING BTREE (`layout_id`)
68 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
69 ") or die "DB ERROR: " . $sth->errstr . "\n";
72 DROP TABLE IF EXISTS labels_templates_tmp;");
74 CREATE TABLE `labels_templates_tmp` (
75 `template_id` int(4) NOT NULL auto_increment,
76 `profile_id` int(4) default NULL,
77 `template_code` char(100) NOT NULL default 'DEFAULT TEMPLATE',
78 `template_desc` char(100) NOT NULL default 'Default description',
79 `page_width` float NOT NULL default '0',
80 `page_height` float NOT NULL default '0',
81 `label_width` float NOT NULL default '0',
82 `label_height` float NOT NULL default '0',
83 `top_text_margin` float NOT NULL default '0',
84 `left_text_margin` float NOT NULL default '0',
85 `top_margin` float NOT NULL default '0',
86 `left_margin` float NOT NULL default '0',
87 `cols` int(2) NOT NULL default '0',
88 `rows` int(2) NOT NULL default '0',
89 `col_gap` float NOT NULL default '0',
90 `row_gap` float NOT NULL default '0',
91 `units` char(20) NOT NULL default 'POINT',
92 PRIMARY KEY (`template_id`),
93 KEY `template_profile_fk_constraint` (`profile_id`)
94 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
95 ") or die "DB ERROR: " . $sth->errstr . "\n";
98 DROP TABLE IF EXISTS printers_profile_tmp;");
100 CREATE TABLE `printers_profile_tmp` (
101 `profile_id` int(4) NOT NULL auto_increment,
102 `printer_name` varchar(40) NOT NULL default 'Default Printer',
103 `template_id` int(4) NOT NULL default '0',
104 `paper_bin` varchar(20) NOT NULL default 'Bypass',
105 `offset_horz` float NOT NULL default '0',
106 `offset_vert` float NOT NULL default '0',
107 `creep_horz` float NOT NULL default '0',
108 `creep_vert` float NOT NULL default '0',
109 `units` char(20) NOT NULL default 'POINT',
110 PRIMARY KEY (`profile_id`),
111 UNIQUE KEY `printername` (`printer_name`,`template_id`,`paper_bin`)
112 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
113 ") or die "DB ERROR: " . $sth->errstr . "\n";
115 # Migrate data from existing tables to new tables...
117 $sth->do("INSERT INTO `labels_batches_tmp` (label_id, batch_id, item_number) SELECT labelid, batch_id, itemnumber FROM labels;") or die "DB ERROR: " . $sth->errstr . "\n";
118 # Since the new label creator keys batches on branch code we must add a branch code during the conversion; the simplest solution appears to be to grab the top branch code from the branches table...
119 $sth->do("UPDATE `labels_batches_tmp` SET branch_code=(SELECT branchcode FROM branches LIMIT 0,1);") or die "DB ERROR: " . $sth->errstr . "\n";
122 $sth->do("INSERT INTO `labels_layouts_tmp` (layout_id, barcode_type, printing_type, layout_name, guidebox, callnum_split, text_justify, format_string) SELECT lc.id, lc.barcodetype, lc.printingtype, lc.layoutname, lc.guidebox, lc.callnum_split, lc.text_justify, lc.formatstring FROM labels_conf AS lc;") or die "DB ERROR: " . $sth->errstr . "\n";
124 $sth->do("INSERT INTO `labels_templates_tmp` (template_id, template_code, template_desc, page_width, page_height, label_width, label_height, top_margin, left_margin, cols, rows, col_gap, row_gap, units) SELECT lt.tmpl_id, lt.tmpl_code, lt.tmpl_desc, lt.page_width, lt.page_height, lt.label_width, lt.label_height, lt.topmargin, lt.leftmargin, lt.cols, lt.rows, lt.colgap, lt.rowgap, lt.units FROM labels_templates AS lt;") or die "DB ERROR: " . $sth->errstr . "\n";
126 $sth->do("INSERT INTO `printers_profile_tmp` (profile_id, printer_name, template_id, paper_bin, offset_horz, offset_vert, creep_horz, creep_vert, units) SELECT prof_id, printername, tmpl_id, paper_bin, offset_horz, offset_vert, creep_horz, creep_vert, unit FROM printers_profile;") or die "DB ERROR: " . $sth->errstr . "\n";
129 my $sth1 = C4
::Context
->dbh->prepare("SELECT layout_id, format_string FROM labels_layouts_tmp;");
130 #$sth1->{'TraceLevel'} = 3;
131 $sth1->execute or die "DB ERROR: " . $sth1->errstr . "\n";
132 while (my $layout = $sth1->fetchrow_hashref()) {
133 if (!$layout->{'format_string'}) {
134 my $sth2 = C4
::Context
->dbh->prepare("SELECT id, title, subtitle, itemtype, barcode, dewey, classification, subclass, itemcallnumber, author, issn, isbn, ccode FROM labels_conf WHERE id = " . $layout->{'layout_id'});
135 $sth2->execute or die "DB ERROR: " . $sth2->errstr . "\n";
136 my $record = $sth2->fetchrow_hashref();
137 my @label_fields = ();
139 foreach (keys(%$record)) {
140 next RECORD
if $record->{$_} eq '' or $_ eq 'id';
141 $label_fields[$record->{$_}] = $_;
144 my $format_string = join (",", @label_fields);
145 # my $format_string = s/^,//i;
146 $sth->do("UPDATE `labels_layouts_tmp` SET format_string=\'$format_string\' WHERE layout_id = " . $record->{'id'}) or die "DB ERROR: " . $sth->errstr . "\n";
150 my $sth3 = C4
::Context
->dbh->prepare("SELECT template_id FROM labels_templates_tmp;");
151 $sth3->execute or die "DB ERROR: " . $sth3->errstr . "\n";
153 while (my $template = $sth3->fetchrow_hashref()) {
154 my $sth4 = C4
::Context
->dbh->prepare("SELECT profile_id FROM printers_profile_tmp WHERE template_id = " . $template->{'template_id'});
155 $sth4->execute or die "DB ERROR: " . $sth4->errstr . "\n";
156 my $profile_id = $sth4->fetchrow_hashref();
157 next RECORD
if $profile_id->{'profile_id'} eq '';
158 $sth->do("UPDATE `labels_templates_tmp` SET profile_id=\'" . $profile_id->{'profile_id'} . "\' WHERE template_id = " . $template->{'template_id'}) or die "DB ERROR: " . $sth->errstr . "\n";
161 # Drop old tables....
163 $sth->do("DROP TABLE IF EXISTS labels;") or die "DB ERROR: " . $sth->errstr . "\n";
164 $sth->do("DROP TABLE IF EXISTS labels_batches;") or die "DB ERROR: " . $sth->errstr . "\n";
165 $sth->do("DROP TABLE IF EXISTS labels_conf;") or die "DB ERROR: " . $sth->errstr . "\n";
166 $sth->do("DROP TABLE IF EXISTS labels_layouts;") or die "DB ERROR: " . $sth->errstr . "\n";
167 $sth->do("DROP TABLE IF EXISTS labels_profile;") or die "DB ERROR: " . $sth->errstr . "\n";
168 $sth->do("DROP TABLE IF EXISTS labels_templates;") or die "DB ERROR: " . $sth->errstr . "\n";
169 $sth->do("DROP TABLE IF EXISTS printers_profile;") or die "DB ERROR: " . $sth->errstr . "\n";
171 # Rename temporary tables to permenant names...
173 $sth->do("ALTER TABLE labels_batches_tmp RENAME TO labels_batches;") or die "DB ERROR: " . $sth->errstr . "\n";
174 $sth->do("ALTER TABLE labels_layouts_tmp RENAME TO labels_layouts;") or die "DB ERROR: " . $sth->errstr . "\n";
175 $sth->do("ALTER TABLE labels_templates_tmp RENAME TO labels_templates;") or die "DB ERROR: " . $sth->errstr . "\n";
176 $sth->do("ALTER TABLE printers_profile_tmp RENAME TO printers_profile;") or die "DB ERROR: " . $sth->errstr . "\n";
179 # Re-enable key checks...
181 SET UNIQUE_CHECKS = 1;
182 ") or die "DB ERROR: " . $sth->errstr . "\n";
185 SET FOREIGN_KEY_CHECKS = 1;
186 ") or die "DB ERROR: " . $sth->errstr . "\n";