MT2938 : Adds a permission for editing items
[koha.git] / installer / data / mysql / updatedatabase30.pl
1 #!/usr/bin/perl
2
3
4 # Database Updater
5 # This script checks for required updates to the database.
6
7 # Part of the Koha Library Software www.koha.org
8 # Licensed under the GPL.
9
10 # Bugs/ToDo:
11 # - Would also be a good idea to offer to do a backup at this time...
12
13 # NOTE:  If you do something more than once in here, make it table driven.
14
15 # NOTE: Please keep the version in kohaversion.pl up-to-date!
16
17 use strict;
18 # use warnings;
19
20 # CPAN modules
21 use DBI;
22 use Getopt::Long;
23 # Koha modules
24 use C4::Context;
25 use C4::Installer;
26
27 use MARC::Record;
28 use MARC::File::XML ( BinaryEncoding => 'utf8' );
29  
30 # FIXME - The user might be installing a new database, so can't rely
31 # on /etc/koha.conf anyway.
32
33 my $debug = 0;
34
35 my (
36     $sth, $sti,
37     $query,
38     %existingtables,    # tables already in database
39     %types,
40     $table,
41     $column,
42     $type, $null, $key, $default, $extra,
43     $prefitem,          # preference item in systempreferences table
44 );
45
46 my $silent;
47 GetOptions(
48     's' =>\$silent
49     );
50 my $dbh = C4::Context->dbh;
51 $|=1; # flushes output
52
53 =item
54
55     Deal with virtualshelves
56
57 =cut
58
59 my $DBversion = '3.00.01.000';
60 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
61     print "Upgrade to $DBversion done (start of 3.0.1)\n";
62     SetVersion ($DBversion);
63 }
64
65 $DBversion = '3.00.01.001';
66 if ( C4::Context->preference('Version') < TransformToNum($DBversion) ) {
67     # use statistics where available
68     $dbh->do("
69         ALTER TABLE statistics ADD KEY  tmp_stats (type, itemnumber, borrowernumber)
70     ");
71     $dbh->do("
72         UPDATE issues iss
73         SET issuedate = (
74             SELECT max(datetime)
75             FROM statistics 
76             WHERE type = 'issue'
77             AND itemnumber = iss.itemnumber
78             AND borrowernumber = iss.borrowernumber
79         )
80         WHERE issuedate IS NULL;
81     ");  
82     $dbh->do("ALTER TABLE statistics DROP KEY tmp_stats");
83
84     # default to last renewal date
85     $dbh->do("
86         UPDATE issues
87         SET issuedate = lastreneweddate
88         WHERE issuedate IS NULL
89         and lastreneweddate IS NOT NULL
90     ");
91
92     my $num_bad_issuedates = $dbh->selectrow_array("SELECT COUNT(*) FROM issues WHERE issuedate IS NULL");
93     if ($num_bad_issuedates > 0) {
94         print STDERR "After the upgrade to $DBversion, there are still $num_bad_issuedates loan(s) with a NULL (blank) loan date. ",
95                      "Please check the issues table in your database.";
96     }
97     print "Upgrade to $DBversion done (bug 2582: set null issues.issuedate to lastreneweddate)";
98     SetVersion($DBversion);
99 }
100
101 $DBversion = "3.00.01.002";
102 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
103     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('AllowRenewalLimitOverride', '0', 'if ON, allows renewal limits to be overridden on the circulation screen',NULL,'YesNo')");
104     print "Upgrade to $DBversion done (add new syspref)\n";
105     SetVersion ($DBversion);
106 }
107
108 $DBversion = "3.00.01.003";
109 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
110         my $search=$dbh->selectall_arrayref("select * from systempreferences where variable='dontmerge'");
111         if (@$search){
112                 my $search=$dbh->selectall_arrayref("select * from systempreferences where variable='MergeAuthoritiesOnUpdate'");
113                 if (@$search){
114                 $dbh->do("DELETE FROM systempreferences set variable='dontmerge'");
115                 }
116                 else {
117                 $dbh->do("UPDATE systempreferences set variable='MergeAuthoritiesOnUpdate' ,value=1-value*1 WHERE variable='dontmerge'");
118                 }
119         }
120         else {
121         $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('MergeAuthoritiesOnUpdate', '1', 'if ON, Updating authorities will automatically updates biblios',NULL,'YesNo')");
122         }
123     print "Upgrade to $DBversion done (add new syspref MergeAuthoritiesOnUpdate)\n";
124     SetVersion ($DBversion);
125 }
126
127 $DBversion = "3.00.01.004";
128 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
129   if (lc(C4::Context->preference('marcflavour')) eq "unimarc"){
130     $dbh->do("INSERT IGNORE INTO `marc_tag_structure` (`tagfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `authorised_value`, `frameworkcode`) VALUES ('099', 'Informations locales', '', 0, 0, '', '');");
131     $dbh->do("INSERT IGNORE INTO `marc_tag_structure` (`frameworkcode`,`tagfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `authorised_value`) SELECT DISTINCT(frameworkcode),'099', 'Informations locales', '', 0, 0, '' from biblio_framework");
132     $dbh->do(<<ENDOFSQL);
133 INSERT IGNORE INTO marc_subfield_structure (`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value`, `authtypecode`, `value_builder`, `isurl`, `hidden`, `seealso`, `link`, `defaultvalue`,frameworkcode )
134 VALUES ('099', 'c', 'date creation notice (koha)', '', 0, 0, 'biblio.datecreated', -1, '', '', '', NULL, 0, '', '', NULL, ''),
135 ('099', 'd', 'date modification notice (koha)', '', 0, 0, 'biblio.timestamp', -1, '', '', '', NULL, 0, '', '', NULL, ''),
136 ('995', '2', 'Perdu', '', 0, 0, 'items.itemlost', 10, '', '', '', NULL, 1, '', NULL, NULL, '');
137 ENDOFSQL
138     $dbh->do(<<ENDOFSQL1);
139 INSERT IGNORE INTO marc_subfield_structure (`frameworkcode`,`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value`, `authtypecode`, `value_builder`, `isurl`, `hidden`, seealso, link, defaultvalue )
140 SELECT DISTINCT(frameworkcode), '099', 'c', 'date creation notice (koha)', '', 0, 0, 'biblio.datecreated', -1, '', '', '', NULL, 0, '', '', NULL from biblio_framework;
141 ENDOFSQL1
142     $dbh->do(<<ENDOFSQL2);
143 INSERT IGNORE INTO marc_subfield_structure (`frameworkcode`,`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value`, `authtypecode`, `value_builder`, `isurl`, `hidden`, seealso, link, defaultvalue )
144 SELECT DISTINCT(frameworkcode), '099', 'd', 'date modification notice (koha)', '', 0, 0, 'biblio.timestamp', -1, '', '', '', NULL, 0, '', '', NULL from biblio_framework;
145 ENDOFSQL2
146     $dbh->do(<<ENDOFSQL3);
147 INSERT IGNORE INTO marc_subfield_structure (`frameworkcode`,`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value`, `authtypecode`, `value_builder`, `isurl`, `hidden`, seealso, link, defaultvalue )
148 SELECT DISTINCT(frameworkcode), '995', '2', 'Perdu', '', 0, 0, 'items.itemlost', 10, '', '', '', NULL, 1, '', NULL, NULL from biblio_framework;
149 ENDOFSQL3
150       print "Upgrade to $DBversion done (updates MARC framework structure)\n";
151     }
152     SetVersion ($DBversion);
153 }
154
155 $DBversion = "3.00.01.005";
156 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
157    $dbh->do(<<ENDOFNOTFORLOANOVERRIDE);
158 INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('AllowNotForLoanOverride', '0', 'if ON, enables the librarian to choose when they want to check out a notForLoan regular item',NULL,'YesNo')
159 ENDOFNOTFORLOANOVERRIDE
160       print "Upgrade to $DBversion done (Adding AllowNotForLoanOverride System preference)\n";
161 }
162
163 $DBversion = "3.00.01.005";
164 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
165     $dbh->do("ALTER TABLE issuingrules ADD COLUMN `finedays` int(11) default NULL AFTER `fine`");
166     print "Upgrade to $DBversion done (Adding a field in issuingrules table)\n";
167     SetVersion ($DBversion);
168 }
169
170 $DBversion = "3.00.01.006";
171 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
172     $dbh->do("ALTER TABLE `aqbudget` CHANGE `aqbudgetid` `aqbudgetid` INT( 11 ) NOT NULL AUTO_INCREMENT");
173     print "Upgrade to $DBversion done (Change the field)\n";
174     SetVersion ($DBversion);
175 }
176
177 $DBversion = "3.00.01.007";
178 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
179     $dbh->do(<<ENDOFRENEWAL);
180 INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('RenewalPeriodBase', 'date_due', 'Set whether the renewal date should be counted from the date_due or from the moment the Patron asks for renewal ','date_due|now','Choice');
181 ENDOFRENEWAL
182     print "Upgrade to $DBversion done (Change the field)\n";
183     SetVersion ($DBversion);
184 }
185 $DBversion = "3.00.02.001";
186 #01.00.005';
187 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
188     $dbh->do("
189         INSERT INTO `letter` (module, code, name, title, content)
190         VALUES('reserves', 'HOLD', 'Hold Available for Pickup', 'Hold Available for Pickup at <<branches.branchname>>', 'Dear <<borrowers.firstname>> <<borrowers.surname>>,\r\n\r\nYou have a hold available for pickup as of <<reserves.waitingdate>>:\r\n\r\nTitle: <<biblio.title>>\r\nAuthor: <<biblio.author>>\r\nCopy: <<items.copynumber>>\r\nLocation: <<branches.branchname>>\r\n<<branches.branchaddress1>>\r\n<<branches.branchaddress2>>\r\n<<branches.branchaddress3>>')
191     ");
192     $dbh->do("INSERT INTO `message_attributes` (message_attribute_id, message_name, takes_days) values(4, 'Hold Filled', 0)");
193     $dbh->do("INSERT INTO `message_transports` (message_attribute_id, message_transport_type, is_digest, letter_module, letter_code) values(4, 'sms', 0, 'reserves', 'HOLD')");
194     $dbh->do("INSERT INTO `message_transports` (message_attribute_id, message_transport_type, is_digest, letter_module, letter_code) values(4, 'email', 0, 'reserves', 'HOLD')");
195     print "Upgrade to $DBversion done (Add letter for holds notifications)\n";
196     SetVersion ($DBversion);
197 }
198
199 $DBversion = "3.00.02.002";
200 #$DBversion = '3.01.00.006';
201 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
202     $dbh->do("ALTER TABLE `biblioitems` ADD KEY issn (issn)");
203     print "Upgrade to $DBversion done (add index on biblioitems.issn)\n";
204     SetVersion ($DBversion);
205 }
206
207 $DBversion = "3.00.02.003";
208 #$DBversion = "3.01.00.007";
209 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
210     $dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='intranetmainUserblock'");
211     $dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='intranetuserjs'");
212     $dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='opacheader'");
213     $dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='OpacMainUserBlock'");
214     $dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='OpacNav'");
215     $dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='opacuserjs'");
216     $dbh->do("UPDATE `systempreferences` SET options='30|10', type='Textarea' WHERE variable='OAI-PMH:Set'");
217     $dbh->do("UPDATE `systempreferences` SET options='50' WHERE variable='intranetstylesheet'");
218     $dbh->do("UPDATE `systempreferences` SET options='50' WHERE variable='intranetcolorstylesheet'");
219     $dbh->do("UPDATE `systempreferences` SET options='10' WHERE variable='globalDueDate'");
220     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='numSearchResults'");
221     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='OPACnumSearchResults'");
222     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='ReservesMaxPickupDelay'");
223     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='TransfersMaxDaysWarning'");
224     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='StaticHoldsQueueWeight'");
225     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='holdCancelLength'");
226     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='XISBNDailyLimit'");
227     $dbh->do("UPDATE `systempreferences` SET type='Float' WHERE variable='gist'");
228     $dbh->do("UPDATE `systempreferences` SET type='Free' WHERE variable='BakerTaylorUsername'");
229     $dbh->do("UPDATE `systempreferences` SET type='Free' WHERE variable='BakerTaylorPassword'");
230     $dbh->do("UPDATE `systempreferences` SET type='Textarea', options='70|10' WHERE variable='ISBD'");
231     $dbh->do("UPDATE `systempreferences` SET type='Textarea', options='70|10', explanation='Enter a specific hash for NoZebra indexes. Enter : \\\'indexname\\\' => \\\'100a,245a,500*\\\',\\\'index2\\\' => \\\'...\\\'' WHERE variable='NoZebraIndexes'");
232     print "Upgrade to $DBversion done (fix display of many sysprefs)\n";
233     SetVersion ($DBversion);
234 }
235
236
237 $DBversion = "3.00.02.004";
238 #$DBversion = "3.01.00.009";
239 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
240     $dbh->do("INSERT INTO permissions (module_bit, code, description) VALUES ( 1, 'circulate_remaining_permissions', 'Remaining circulation permissions')");
241     $dbh->do("INSERT INTO permissions (module_bit, code, description) VALUES ( 1, 'override_renewals', 'Override blocked renewals')");
242     print "Upgrade to $DBversion done (added subpermissions for circulate permission)\n";
243 }
244
245 $DBversion = "3.00.02.005";
246 #$DBversion = '3.01.00.010';
247 if ( C4::Context->preference('Version') < TransformToNum($DBversion) ) {
248     $dbh->do("ALTER TABLE `borrower_attributes` MODIFY COLUMN `attribute` VARCHAR(64) DEFAULT NULL");
249     $dbh->do("ALTER TABLE `borrower_attributes` MODIFY COLUMN `password` VARCHAR(64) DEFAULT NULL");
250     print "Upgrade to $DBversion done (bug 2687: increase length of borrower attribute fields)\n";
251     SetVersion ($DBversion);
252 }
253
254 $DBversion = "3.00.02.006";
255 #$DBversion = '3.01.00.011';
256 if ( C4::Context->preference('Version') < TransformToNum($DBversion) ) {
257
258     # Yes, the old value was ^M terminated.
259     my $bad_value = "function prepareEmailPopup(){\r\n  if (!document.getElementById) return false;\r\n  if (!document.getElementById('reserveemail')) return false;\r\n  rsvlink = document.getElementById('reserveemail');\r\n  rsvlink.onclick = function() {\r\n      doReservePopup();\r\n      return false;\r\n  }\r\n}\r\n\r\nfunction doReservePopup(){\r\n}\r\n\r\nfunction prepareReserveList(){\r\n}\r\n\r\naddLoadEvent(prepareEmailPopup);\r\naddLoadEvent(prepareReserveList);";
260
261     my $intranetuserjs = C4::Context->preference('intranetuserjs');
262     if ($intranetuserjs  and  $intranetuserjs eq $bad_value) {
263         my $sql = <<'END_SQL';
264 UPDATE systempreferences
265 SET value = ''
266 WHERE variable = 'intranetuserjs'
267 END_SQL
268         $dbh->do($sql);
269     }
270     print "Upgrade to $DBversion done (removed bogus intranetuserjs syspref)\n";
271     SetVersion($DBversion);
272 }
273
274
275 $DBversion = "3.00.02.007";
276 #$DBversion = '3.01.00.015';
277 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
278     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('OPACAmazonCoverImages', '0', 'Display cover images on OPAC from Amazon Web Services','','YesNo')");
279
280     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('AmazonCoverImages', '0', 'Display Cover Images in Staff Client from Amazon Web Services','','YesNo')");
281
282     $dbh->do("UPDATE systempreferences SET variable='AmazonEnabled' WHERE variable = 'AmazonContent'");
283
284     $dbh->do("UPDATE systempreferences SET variable='OPACAmazonEnabled' WHERE variable = 'OPACAmazonContent'");
285
286     print "Upgrade to $DBversion done (added Syndetics Enhanced Content system preferences)\n";
287     SetVersion ($DBversion);
288 }
289
290
291
292 $DBversion = "3.00.02.008";
293 #$DBversion = "3.01.00.018";
294 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
295     $dbh->do("ALTER TABLE deletedborrowers ADD `smsalertnumber` varchar(50) default NULL");
296     print "Upgrade to $DBversion done (added deletedborrowers.smsalertnumber, missed in 3.00.00.091)\n";
297     SetVersion ($DBversion);
298 }
299
300
301 $DBversion = "3.00.02.009";
302 #$DBversion = '3.01.00.023';
303 if ( C4::Context->preference('Version') < TransformToNum($DBversion) ) {
304     $dbh->do("ALTER TABLE biblioitems        MODIFY COLUMN isbn VARCHAR(30) DEFAULT NULL");
305     $dbh->do("ALTER TABLE deletedbiblioitems MODIFY COLUMN isbn VARCHAR(30) DEFAULT NULL");
306     $dbh->do("ALTER TABLE import_biblios     MODIFY COLUMN isbn VARCHAR(30) DEFAULT NULL");
307     $dbh->do("ALTER TABLE suggestions        MODIFY COLUMN isbn VARCHAR(30) DEFAULT NULL");
308     print "Upgrade to $DBversion done (bug 2765: increase width of isbn column in several tables)\n";
309     SetVersion ($DBversion);
310 }
311
312 $DBversion = '3.00.02.010';
313 #$DBversion = '3.01.00.027';
314 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
315     $dbh->do("ALTER TABLE zebraqueue CHANGE `biblio_auth_number` `biblio_auth_number` bigint(20) unsigned NOT NULL default 0");
316     print "Upgrade to $DBversion done (Increased size of zebraqueue biblio_auth_number to address bug 3148.)\n";
317     SetVersion ($DBversion);
318 }
319
320 #$DBversion = '3.01.00.028';
321 $DBversion = '3.00.02.011';
322 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
323     my $enable_reviews = C4::Context->preference('AmazonEnabled') ? '1' : '0';
324     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('AmazonReviews', '$enable_reviews', 'Display Amazon reviews on staff interface','','YesNo')");
325     print "Upgrade to $DBversion done (added AmazonReviews)\n";
326     SetVersion ($DBversion);
327 }
328
329 #$DBversion = '3.01.00.029';
330 $DBversion = '3.00.02.012';
331 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
332     $dbh->do(q( UPDATE language_rfc4646_to_iso639
333                 SET iso639_2_code = 'spa'
334                 WHERE rfc4646_subtag = 'es'
335                 AND   iso639_2_code = 'rus' )
336             );
337     print "Upgrade to $DBversion done (fixed bug 2599: using Spanish search limit retrieves Russian results)\n";
338     SetVersion ($DBversion);
339 }
340
341 $DBversion = '3.00.03.001';
342 if (C4::Context->preference('Version') < TransformToNum($DBversion)){
343     $dbh->do("
344         INSERT INTO `letter` (module, code, name, title, content)         VALUES('reserves', 'HOLDPLACED', 'Hold Placed on Item', 'Hold Placed on Item','An hold has been placed on the following item : <<title>> (<<biblionumber>>) by the user <<firstname>> <<surname>> (<<cardnumber>>).');
345     ");
346 }
347
348 $DBversion = '3.00.04.001';
349 if ( C4::Context->preference('Version') < TransformToNum($DBversion) ) {
350     $dbh->do("
351         CREATE TABLE hold_fill_targets (
352             `borrowernumber` int(11) NOT NULL,
353             `biblionumber` int(11) NOT NULL,
354             `itemnumber` int(11) NOT NULL,
355             `source_branchcode`  varchar(10) default NULL,
356             `item_level_request` tinyint(4) NOT NULL default 0,
357             PRIMARY KEY `itemnumber` (`itemnumber`),
358             KEY `bib_branch` (`biblionumber`, `source_branchcode`),
359             CONSTRAINT `hold_fill_targets_ibfk_1` FOREIGN KEY (`borrowernumber`) 
360                 REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE,
361             CONSTRAINT `hold_fill_targets_ibfk_2` FOREIGN KEY (`biblionumber`) 
362                 REFERENCES `biblio` (`biblionumber`) ON DELETE CASCADE ON UPDATE CASCADE,
363             CONSTRAINT `hold_fill_targets_ibfk_3` FOREIGN KEY (`itemnumber`) 
364                 REFERENCES `items` (`itemnumber`) ON DELETE CASCADE ON UPDATE CASCADE,
365             CONSTRAINT `hold_fill_targets_ibfk_4` FOREIGN KEY (`source_branchcode`) 
366                 REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE
367         ) ENGINE=InnoDB DEFAULT CHARSET=utf8
368     ");
369     $dbh->do("
370         ALTER TABLE tmp_holdsqueue
371             ADD item_level_request tinyint(4) NOT NULL default 0
372     ");
373
374     print "Upgrade to $DBversion done (add hold_fill_targets table and a column to tmp_holdsqueue)\n";
375     SetVersion($DBversion);
376 }
377
378 $DBversion = '3.00.04.002';
379 if ( C4::Context->preference('Version') < TransformToNum($DBversion) ) {
380     # use statistics where available
381     $dbh->do("
382         ALTER TABLE statistics ADD KEY  tmp_stats (type, itemnumber, borrowernumber)
383     ");
384     $dbh->do("
385         UPDATE issues iss
386         SET issuedate = (
387             SELECT max(datetime)
388             FROM statistics 
389             WHERE type = 'issue'
390             AND itemnumber = iss.itemnumber
391             AND borrowernumber = iss.borrowernumber
392         )
393         WHERE issuedate IS NULL;
394     ");  
395     $dbh->do("ALTER TABLE statistics DROP KEY tmp_stats");
396
397     # default to last renewal date
398     $dbh->do("
399         UPDATE issues
400         SET issuedate = lastreneweddate
401         WHERE issuedate IS NULL
402         and lastreneweddate IS NOT NULL
403     ");
404
405     my $num_bad_issuedates = $dbh->selectrow_array("SELECT COUNT(*) FROM issues WHERE issuedate IS NULL");
406     if ($num_bad_issuedates > 0) {
407         print STDERR "After the upgrade to $DBversion, there are still $num_bad_issuedates loan(s) with a NULL (blank) loan date. ",
408                      "Please check the issues table in your database.";
409     }
410     print "Upgrade to $DBversion done (bug 2582: set null issues.issuedate to lastreneweddate)\n";
411     SetVersion($DBversion);
412 }
413
414 $DBversion = "3.00.04.003";
415 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
416     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('AllowRenewalLimitOverride', '0', 'if ON, allows renewal limits to be overridden on the circulation screen',NULL,'YesNo')");
417     print "Upgrade to $DBversion done (add new syspref)\n";
418     SetVersion ($DBversion);
419 }
420
421 $DBversion = '3.00.04.004';
422 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
423     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('OPACDisplayRequestPriority','0','Show patrons the priority level on holds in the OPAC','','YesNo')");
424     print "Upgrade to $DBversion done (added OPACDisplayRequestPriority system preference)\n";
425     SetVersion ($DBversion);
426 }
427
428 $DBversion = '3.00.04.005';
429 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
430     $dbh->do("
431         INSERT INTO `letter` (module, code, name, title, content)
432         VALUES('reserves', 'HOLD', 'Hold Available for Pickup', 'Hold Available for Pickup at <<branches.branchname>>', 'Dear <<borrowers.firstname>> <<borrowers.surname>>,\r\n\r\nYou have a hold available for pickup as of <<reserves.waitingdate>>:\r\n\r\nTitle: <<biblio.title>>\r\nAuthor: <<biblio.author>>\r\nCopy: <<items.copynumber>>\r\nLocation: <<branches.branchname>>\r\n<<branches.branchaddress1>>\r\n<<branches.branchaddress2>>\r\n<<branches.branchaddress3>>')
433     ");
434     $dbh->do("INSERT INTO `message_attributes` (message_attribute_id, message_name, takes_days) values(4, 'Hold Filled', 0)");
435     $dbh->do("INSERT INTO `message_transports` (message_attribute_id, message_transport_type, is_digest, letter_module, letter_code) values(4, 'sms', 0, 'reserves', 'HOLD')");
436     $dbh->do("INSERT INTO `message_transports` (message_attribute_id, message_transport_type, is_digest, letter_module, letter_code) values(4, 'email', 0, 'reserves', 'HOLD')");
437     print "Upgrade to $DBversion done (Add letter for holds notifications)\n";
438     SetVersion ($DBversion);
439 }
440
441 $DBversion = '3.00.04.006';
442 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
443     $dbh->do("ALTER TABLE `biblioitems` ADD KEY issn (issn)");
444     print "Upgrade to $DBversion done (add index on biblioitems.issn)\n";
445     SetVersion ($DBversion);
446 }
447
448 $DBversion = "3.00.04.007";
449 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
450     $dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='intranetmainUserblock'");
451     $dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='intranetuserjs'");
452     $dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='opacheader'");
453     $dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='OpacMainUserBlock'");
454     $dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='OpacNav'");
455     $dbh->do("UPDATE `systempreferences` SET options='70|10' WHERE variable='opacuserjs'");
456     $dbh->do("UPDATE `systempreferences` SET options='30|10', type='Textarea' WHERE variable='OAI-PMH:Set'");
457     $dbh->do("UPDATE `systempreferences` SET options='50' WHERE variable='intranetstylesheet'");
458     $dbh->do("UPDATE `systempreferences` SET options='50' WHERE variable='intranetcolorstylesheet'");
459     $dbh->do("UPDATE `systempreferences` SET options='10' WHERE variable='globalDueDate'");
460     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='numSearchResults'");
461     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='OPACnumSearchResults'");
462     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='ReservesMaxPickupDelay'");
463     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='TransfersMaxDaysWarning'");
464     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='StaticHoldsQueueWeight'");
465     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='holdCancelLength'");
466     $dbh->do("UPDATE `systempreferences` SET type='Integer' WHERE variable='XISBNDailyLimit'");
467     $dbh->do("UPDATE `systempreferences` SET type='Float' WHERE variable='gist'");
468     $dbh->do("UPDATE `systempreferences` SET type='Free' WHERE variable='BakerTaylorUsername'");
469     $dbh->do("UPDATE `systempreferences` SET type='Free' WHERE variable='BakerTaylorPassword'");
470     $dbh->do("UPDATE `systempreferences` SET type='Textarea', options='70|10' WHERE variable='ISBD'");
471     $dbh->do("UPDATE `systempreferences` SET type='Textarea', options='70|10', explanation='Enter a specific hash for NoZebra indexes. Enter : \\\'indexname\\\' => \\\'100a,245a,500*\\\',\\\'index2\\\' => \\\'...\\\'' WHERE variable='NoZebraIndexes'");
472     print "Upgrade to $DBversion done (fix display of many sysprefs)\n";
473     SetVersion ($DBversion);
474 }
475
476
477 $DBversion = '3.00.04.008';
478 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
479
480     $dbh->do("CREATE TABLE branch_transfer_limits (
481                           limitId int(8) NOT NULL auto_increment,
482                           toBranch varchar(4) NOT NULL,
483                           fromBranch varchar(4) NOT NULL,
484                           itemtype varchar(4) NOT NULL,
485                           PRIMARY KEY  (limitId)
486                           ) ENGINE=InnoDB DEFAULT CHARSET=utf8"
487                         );
488
489     $dbh->do("INSERT INTO `systempreferences` ( `variable` , `value` , `options` , `explanation` , `type` ) VALUES ( 'UseBranchTransferLimits', '0', '', 'If ON, Koha will will use the rules defined in branch_transfer_limits to decide if an item transfer should be allowed.', 'YesNo')");
490
491     print "Upgrade to $DBversion done (added branch_transfer_limits table and UseBranchTransferLimits system preference)\n";
492     SetVersion ($DBversion);
493 }
494
495 $DBversion = "3.00.04.009";
496 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
497     $dbh->do("ALTER TABLE permissions MODIFY `code` varchar(64) DEFAULT NULL");
498     $dbh->do("ALTER TABLE user_permissions MODIFY `code` varchar(64) DEFAULT NULL");
499     $dbh->do("INSERT INTO permissions (module_bit, code, description) VALUES ( 1, 'circulate_remaining_permissions', 'Remaining circulation permissions')");
500     $dbh->do("INSERT INTO permissions (module_bit, code, description) VALUES ( 1, 'override_renewals', 'Override blocked renewals')");
501     print "Upgrade to $DBversion done (added subpermissions for circulate permission)\n";
502     SetVersion ($DBversion);
503 }
504
505 $DBversion = '3.00.04.010';
506 if ( C4::Context->preference('Version') < TransformToNum($DBversion) ) {
507     $dbh->do("ALTER TABLE `borrower_attributes` MODIFY COLUMN `attribute` VARCHAR(64) DEFAULT NULL");
508     $dbh->do("ALTER TABLE `borrower_attributes` MODIFY COLUMN `password` VARCHAR(64) DEFAULT NULL");
509     print "Upgrade to $DBversion done (bug 2687: increase length of borrower attribute fields)\n";
510     SetVersion($DBversion);
511 }
512
513 $DBversion = '3.00.04.011';
514 if ( C4::Context->preference('Version') < TransformToNum($DBversion) ) {
515     $dbh->do("ALTER TABLE biblioitems        MODIFY COLUMN isbn VARCHAR(30) DEFAULT NULL");
516     $dbh->do("ALTER TABLE deletedbiblioitems MODIFY COLUMN isbn VARCHAR(30) DEFAULT NULL");
517     $dbh->do("ALTER TABLE import_biblios     MODIFY COLUMN isbn VARCHAR(30) DEFAULT NULL");
518     $dbh->do("ALTER TABLE suggestions        MODIFY COLUMN isbn VARCHAR(30) DEFAULT NULL");
519     print "Upgrade to $DBversion done (bug 2765: increase width of isbn column in several tables)\n";
520     SetVersion ($DBversion);
521 }
522
523 $DBversion = '3.00.04.012';
524 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
525     $dbh->do("INSERT INTO `systempreferences` ( `variable` , `value` , `options` , `explanation` , `type` ) VALUES ( 'ceilingDueDate', '', '', 'If set, date due will not be past this date.  Enter date according to the dateformat System Preference', 'free')");
526
527     print "Upgrade to $DBversion done (added ceilingDueDate system preference)\n";
528     SetVersion ($DBversion);
529 }
530
531 $DBversion = '3.00.04.013';
532 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
533     $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('AWSPrivateKey','','See:  http://aws.amazon.com.  Note that this is required after 2009/08/15 in order to retrieve any enhanced content other than book covers from Amazon.','','free')");
534     SetVersion ($DBversion);
535     print "Upgrade to $DBversion done (added AWSPrivateKey syspref - note that if you use enhanced content from Amazon, this should be set right away.)";
536 }
537
538 $DBversion = '3.00.04.014';
539 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
540     $dbh->do("ALTER TABLE zebraqueue CHANGE `biblio_auth_number` `biblio_auth_number` bigint(20) unsigned NOT NULL default 0");
541     print "Upgrade to $DBversion done (Increased size of zebraqueue biblio_auth_number to address bug 3148.)\n";
542     SetVersion ($DBversion);
543 }
544
545
546 $DBversion = "3.00.04.015";
547 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
548     $dbh->do(<<ENDOFRENEWAL);
549 INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('RenewalPeriodBase', 'now', 'Set whether the renewal date should be counted from the date_due or from the moment the Patron asks for renewal ','date_due|now','Choice');
550 ENDOFRENEWAL
551     print "Upgrade to $DBversion done (Change the field)\n";
552     SetVersion ($DBversion);
553 }
554
555 #$DBversion = "3.01.00.040";
556 $DBversion = "3.00.04.016";
557 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
558     $dbh->do("
559         CREATE TABLE `fieldmapping` (
560           `id` int(11) NOT NULL auto_increment,
561           `field` varchar(255) NOT NULL,
562           `frameworkcode` char(4) NOT NULL default '',
563           `fieldcode` char(3) NOT NULL,
564           `subfieldcode` char(1) NOT NULL,
565           PRIMARY KEY  (`id`)
566         ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
567              ");
568     SetVersion ($DBversion);
569 }
570 $DBversion = "3.00.04.017";
571 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
572     $dbh->do("
573         INSERT INTO `permissions` (`module_bit`, `code`, `description`) VALUES
574         (15, 'check_expiration', 'Check the expiration of a serial'),
575         (15, 'claim_serials', 'Claim missing serials'),
576         (15, 'create_subscription', 'Create a new subscription'),
577         (15, 'delete_subscription', 'Delete an existing subscription'),
578         (15, 'edit_subscription', 'Edit an existing subscription'),
579         (15, 'receive_serials', 'Serials receiving'),
580         (15, 'renew_subscription', 'Renew a subscription'),
581         (15, 'routing', 'Routing');
582                  ");
583     SetVersion ($DBversion);
584 }
585
586 #$DBversion = '3.01.00.036';
587 $DBversion = "3.00.04.018";
588 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
589     $dbh->do("UPDATE systempreferences SET explanation = 'Choose the default detail view in the staff interface; choose between normal, labeled_marc, marc or isbd'
590               WHERE variable = 'IntranetBiblioDefaultView'
591               AND   explanation = 'IntranetBiblioDefaultView'");
592     $dbh->do("UPDATE systempreferences SET type = 'Choice', options = 'normal|marc|isbd|labeled_marc'
593               WHERE variable = 'IntranetBiblioDefaultView'");
594     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type)VALUES('viewISBD','1','Allow display of ISBD view of bibiographic records','','YesNo')");
595     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type)VALUES('viewLabeledMARC','0','Allow display of labeled MARC view of bibiographic records','','YesNo')");
596     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type)VALUES('viewMARC','1','Allow display of MARC view of bibiographic records','','YesNo')");
597     print "Upgrade to $DBversion done (new viewISBD, viewLabeledMARC, viewMARC sysprefs and tweak IntranetBiblioDefaultView)\n";
598     SetVersion ($DBversion);
599 }  
600
601 $DBversion = "3.00.04.019";
602 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
603     my $authdisplayhierarchy = C4::Context->preference('AuthDisplayHierarchy');
604     if ($authdisplayhierarchy < 1){
605        $dbh->do("INSERT IGNORE INTO systempreferences (variable,value,explanation,options,type)VALUES('AuthDisplayHierarchy','0','To display authorities in a hierarchy way. Put ON only if you have a thesaurus. Default is OFF','','YesNo')");
606     };
607
608     my $opacamazon = C4::Context->preference('OPACAmazonCoverImages');
609     if ($opacamazon < 1){
610        $dbh->do("INSERT IGNORE INTO systempreferences (variable,value,explanation,options,type)VALUES('OPACAmazonCoverImages','0','Display cover images on OPAC from Amazon Web Services. Default is OFF','','YesNo')");
611     };
612
613     print "Upgrade to $DBversion done (new AuthDisplayHierarchy, )\n";
614     SetVersion ($DBversion);
615 }  
616
617 $DBversion = "3.00.04.020";
618 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
619     if (lc(C4::Context->preference('marcflavour')) eq "unimarc"){
620         $dbh->do(<<OPACISBD);
621 INSERT IGNORE INTO systempreferences (variable,explanation,options,type,value)
622 VALUES('OPACISBD','OPAC ISBD View','90|20', 'Textarea',
623 '#200|<h2>Titre : |{200a}{. 200c}{ : 200e}{200d}{. 200h}{. 200i}|</h2>\r\n#500|<label class="ipt">Autres titres : </label>|{500a}{. 500i}{. 500h}{. 500m}{. 500q}{. 500k}<br/>|\r\n#517|<label class="ipt"> </label>|{517a}{ : 517e}{. 517h}{, 517i}<br/>|\r\n#541|<label class="ipt"> </label>|{541a}{ : 541e}<br/>|\r\n#200||<label class="ipt">Auteurs : </label><br/>|\r\n#700||<a href="opac-search.pl?op=do_search&marclist=7009&operator==&type=intranet&value={7009}"> <img border="0" src="/opac-tmpl/css/en/images/filefind.png" height="15" title="Chercher sur l''auteur"></a>{700c}{ 700b}{ 700a}{ 700d}{ (700f)}{. 7004}<br/>|\r\n#701||<a href="opac-search.pl?op=do_search&marclist=7009&operator==&type=intranet&value={7019}"> <img border="0" src="/opac-tmpl/css/en/images/filefind.png" height="15" title="Chercher sur l''auteur"></a>{701c}{ 701b}{ 701a}{ 701d}{ (701f)}{. 7014}<br/>|\r\n#702||<a href="opac-search.pl?op=do_search&marclist=7009&operator==&type=intranet&value={7029}"> <img border="0" src="/opac-tmpl/css/en/images/filefind.png" height="15" title="Chercher sur l''auteur"></a>{702c}{ 702b}{ 702a}{ 702d}{ (702f)}{. 7024}<br/>|\r\n#710||<a href="opac-search.pl?op=do_search&marclist=7109&operator==&type=intranet&value={7109}"> <img border="0" src="/opac-tmpl/css/en/images/filefind.png" height="15" title="Chercher sur l''auteur"></a>{710a}{ (710c)}{. 710b}{ : 710d}{ ; 710f}{ ; 710e}<br/>|\r\n#711||<a href="opac-search.pl?op=do_search&marclist=7109&operator==&type=intranet&value={7119}"> <img border="0" src="/opac-tmpl/css/en/images/filefind.png" height="15" title="Chercher sur l''auteur"></a>{711a}{ (711c)}{. 711b}{ : 711d}{ ; 711f}{ ; 711e}<br/>|\r\n#712||<a href="opac-search.pl?op=do_search&marclist=7109&operator==&type=intranet&value={7129}"> <img border="0" src="/opac-tmpl/css/en/images/filefind.png" height="15" title="Chercher sur l''auteur"></a>{712a}{ (712c)}{. 712b}{ : 712d}{ ; 712f}{ ; 712e}<br/>|\r\n#210|<label class="ipt">Lieu d''édition : </label>|{ 210a}<br/>|\r\n#210|<label class="ipt">Editeur : </label>|{ 210c}<br/>|\r\n#210|<label class="ipt">Date d''édition : </label>|{ 210d}<br/>|\r\n#215|<label class="ipt">Description : </label>|{215a}{ : 215c}{ ; 215d}{ + 215e}|<br/>\r\n#225|<label class="ipt">Collection :</label>|<a href="opac-search.pl?op=do_search&marclist=225a&operator==&type=intranet&value={225a}"> <img border="0" src="/opac-tmpl/css/en/images/filefind.png" height="15" title="Chercher sur {225a}"></a>{ (225a}{ = 225d}{ : 225e}{. 225h}{. 225i}{ / 225f}{, 225x}{ ; 225v}|)<br/>\r\n#200||<label class="ipt">Sujets : </label><br/>|\r\n#600||<a href="opac-search.pl?op=do_search&marclist=6009&operator==&type=intranet&value={6009}"><img border="0" src="/opac-tmpl/css/en/images/filefind.png" height="15" title="Search on {6009}"></a>{ 600c}{ 600b}{ 600a}{ 600d}{ (600f)} {-- 600x }{-- 600z }{-- 600y}<br />|\r\n#604||<a href="opac-search.pl?op=do_search&marclist=6049&operator==&type=intranet&value={6049}"><img border="0" src="/opac-tmpl/css/en/images/filefind.png" height="15" title="Search on {6049}"></a>{ 604a}{. 604t}<br />|\r\n#601||<a href="opac-search.pl?op=do_search&marclist=6019&operator==&type=intranet&value={6019}"><img border="0" src="/opac-tmpl/css/en/images/filefind.png" height="15" title="Search on {6019}"></a>{ 601a}{ (601c)}{. 601b}{ : 601d} { ; 601f}{ ; 601e}{ -- 601x }{-- 601z }{-- 601y}<br />|\r\n#605||<a href="opac-search.pl?op=do_search&marclist=6059&operator==&type=intranet&value={6059}"><img border="0" src="/opac-tmpl/css/en/images/filefind.png" height="15" title="Search on {6059}"></a>{ 605a}{. 605i}{. 605h}{. 605k}{. 605m}{. 605q} {-- 605x }{-- 605z }{-- 605y }{-- 605l}<br />|\r\n#606||<a href="opac-search.pl?op=do_search&marclist=6069&operator==&type=intranet&value={6069}"><img border="0" src="/opac-tmpl/css/en/images/filefind.png" height="15" title="Search on {6069}">xx</a>{ 606a}{-- 606x }{-- 606z }{606y }<br />|\r\n#607||<a href="opac-search.pl?op=do_search&marclist=6079&operator==&type=intranet&value={6079}"><img border="0" src="/opac-tmpl/css/en/images/filefind.png" height="15" title="Search on {6079}"></a>{ 607a}{-- 607x}{-- 607z}{-- 607y}<br />|\r\n#010|<label class="ipt">ISBN : </label>|{010a}|<br/>\r\n#011|<label class="ipt">ISSN : </label>|{011a}|<br/>\r\n#200||<label class="ipt">Notes : </label>|<br/>\r\n#300||{300a}|<br/>\r\n#320||{320a}|<br/>\r\n#327||{327a}|<br/>\r\n#328||{328a}|<br/>\r\n#200||<br/><h2>Exemplaires</h2>|\r\n#200|<table>|<th>Localisation</th><th>Cote</th>|\r\n#995||<tr><td>{995e}&nbsp;&nbsp;</td><td> {995k}</td></tr>|\r\n#200|||</table>')
624 OPACISBD
625     }else{
626         $dbh->do(<<OPACISBDEN);
627 INSERT IGNORE INTO `systempreferences` (variable,value,explanation,options,type) 
628 VALUES('OPACISBD','#100||{ 100a }{ 100b }{ 100c }{ 100d }{ 110a }{ 110b }{ 110c }{ 110d }{ 110e }{ 110f }{ 110g }{ 130a }{ 130d }{ 130f }{ 130g }{ 130h }{ 130k }{ 130l }{ 130m }{ 130n }{ 130o }{ 130p }{ 130r }{ 130s }{ 130t }|<br/><br/>\r\n#245||{ 245a }{ 245b }{245f }{ 245g }{ 245k }{ 245n }{ 245p }{ 245s }{ 245h }|\r\n#246||{ : 246i }{ 246a }{ 246b }{ 246f }{ 246g }{ 246n }{ 246p }{ 246h }|\r\n#242||{ = 242a }{ 242b }{ 242n }{ 242p }{ 242h }|\r\n#245||{ 245c }|\r\n#242||{ = 242c }|\r\n#250| - |{ 250a }{ 250b }|\r\n#254|, |{ 254a }|\r\n#255|, |{ 255a }{ 255b }{ 255c }{ 255d }{ 255e }{ 255f }{ 255g }|\r\n#256|, |{ 256a }|\r\n#257|, |{ 257a }|\r\n#258|, |{ 258a }{ 258b }|\r\n#260| - |{ 260a }{ 260b }{ 260c }|\r\n#300| - |{ 300a }{ 300b }{ 300c }{ 300d }{ 300e }{ 300f }{ 300g }|\r\n#306| - |{ 306a }|\r\n#307| - |{ 307a }{ 307b }|\r\n#310| - |{ 310a }{ 310b }|\r\n#321| - |{ 321a }{ 321b }|\r\n#340| - |{ 3403 }{ 340a }{ 340b }{ 340c }{ 340d }{ 340e }{ 340f }{ 340h }{ 340i }|\r\n#342| - |{ 342a }{ 342b }{ 342c }{ 342d }{ 342e }{ 342f }{ 342g }{ 342h }{ 342i }{ 342j }{ 342k }{ 342l }{ 342m }{ 342n }{ 342o }{ 342p }{ 342q }{ 342r }{ 342s }{ 342t }{ 342u }{ 342v }{ 342w }|\r\n#343| - |{ 343a }{ 343b }{ 343c }{ 343d }{ 343e }{ 343f }{ 343g }{ 343h }{ 343i }|\r\n#351| - |{ 3513 }{ 351a }{ 351b }{ 351c }|\r\n#352| - |{ 352a }{ 352b }{ 352c }{ 352d }{ 352e }{ 352f }{ 352g }{ 352i }{ 352q }|\r\n#362| - |{ 362a }{ 351z }|\r\n#440| - |{ 440a }{ 440n }{ 440p }{ 440v }{ 440x }|.\r\n#490| - |{ 490a }{ 490v }{ 490x }|.\r\n#800| - |{ 800a }{ 800b }{ 800c }{ 800d }{ 800e }{ 800f }{ 800g }{ 800h }{ 800j }{ 800k }{ 800l }{ 800m }{ 800n }{ 800o }{ 800p }{ 800q }{ 800r }{ 800s }{ 800t }{ 800u }{ 800v }|.\r\n#810| - |{ 810a }{ 810b }{ 810c }{ 810d }{ 810e }{ 810f }{ 810g }{ 810h }{ 810k }{ 810l }{ 810m }{ 810n }{ 810o }{ 810p }{ 810r }{ 810s }{ 810t }{ 810u }{ 810v }|.\r\n#811| - |{ 811a }{ 811c }{ 811d }{ 811e }{ 811f }{ 811g }{ 811h }{ 811k }{ 811l }{ 811n }{ 811p }{ 811q }{ 811s }{ 811t }{ 811u }{ 811v }|.\r\n#830| - |{ 830a }{ 830d }{ 830f }{ 830g }{ 830h }{ 830k }{ 830l }{ 830m }{ 830n }{ 830o }{ 830p }{ 830r }{ 830s }{ 830t }{ 830v }|.\r\n#500|<br/><br/>|{ 5003 }{ 500a }|\r\n#501|<br/><br/>|{ 501a }|\r\n#502|<br/><br/>|{ 502a }|\r\n#504|<br/><br/>|{ 504a }|\r\n#505|<br/><br/>|{ 505a }{ 505t }{ 505r }{ 505g }{ 505u }|\r\n#506|<br/><br/>|{ 5063 }{ 506a }{ 506b }{ 506c }{ 506d }{ 506u }|\r\n#507|<br/><br/>|{ 507a }{ 507b }|\r\n#508|<br/><br/>|{ 508a }{ 508a }|\r\n#510|<br/><br/>|{ 5103 }{ 510a }{ 510x }{ 510c }{ 510b }|\r\n#511|<br/><br/>|{ 511a }|\r\n#513|<br/><br/>|{ 513a }{513b }|\r\n#514|<br/><br/>|{ 514z }{ 514a }{ 514b }{ 514c }{ 514d }{ 514e }{ 514f }{ 514g }{ 514h }{ 514i }{ 514j }{ 514k }{ 514m }{ 514u }|\r\n#515|<br/><br/>|{ 515a }|\r\n#516|<br/><br/>|{ 516a }|\r\n#518|<br/><br/>|{ 5183 }{ 518a }|\r\n#520|<br/><br/>|{ 5203 }{ 520a }{ 520b }{ 520u }|\r\n#521|<br/><br/>|{ 5213 }{ 521a }{ 521b }|\r\n#522|<br/><br/>|{ 522a }|\r\n#524|<br/><br/>|{ 524a }|\r\n#525|<br/><br/>|{ 525a }|\r\n#526|<br/><br/>|{\\n510i }{\\n510a }{ 510b }{ 510c }{ 510d }{\\n510x }|\r\n#530|<br/><br/>|{\\n5063 }{\\n506a }{ 506b }{ 506c }{ 506d }{\\n506u }|\r\n#533|<br/><br/>|{\\n5333 }{\\n533a }{\\n533b }{\\n533c }{\\n533d }{\\n533e }{\\n533f }{\\n533m }{\\n533n }|\r\n#534|<br/><br/>|{\\n533p }{\\n533a }{\\n533b }{\\n533c }{\\n533d }{\\n533e }{\\n533f }{\\n533m }{\\n533n }{\\n533t }{\\n533x }{\\n533z }|\r\n#535|<br/><br/>|{\\n5353 }{\\n535a }{\\n535b }{\\n535c }{\\n535d }|\r\n#538|<br/><br/>|{\\n5383 }{\\n538a }{\\n538i }{\\n538u }|\r\n#540|<br/><br/>|{\\n5403 }{\\n540a }{ 540b }{ 540c }{ 540d }{\\n520u }|\r\n#544|<br/><br/>|{\\n5443 }{\\n544a }{\\n544b }{\\n544c }{\\n544d }{\\n544e }{\\n544n }|\r\n#545|<br/><br/>|{\\n545a }{ 545b }{\\n545u }|\r\n#546|<br/><br/>|{\\n5463 }{\\n546a }{ 546b }|\r\n#547|<br/><br/>|{\\n547a }|\r\n#550|<br/><br/>|{ 550a }|\r\n#552|<br/><br/>|{ 552z }{ 552a }{ 552b }{ 552c }{ 552d }{ 552e }{ 552f }{ 552g }{ 552h }{ 552i }{ 552j }{ 552k }{ 552l }{ 552m }{ 552n }{ 562o }{ 552p }{ 552u }|\r\n#555|<br/><br/>|{ 5553 }{ 555a }{ 555b }{ 555c }{ 555d }{ 555u }|\r\n#556|<br/><br/>|{ 556a }{ 506z }|\r\n#563|<br/><br/>|{ 5633 }{ 563a }{ 563u }|\r\n#565|<br/><br/>|{ 5653 }{ 565a }{ 565b }{ 565c }{ 565d }{ 565e }|\r\n#567|<br/><br/>|{ 567a }|\r\n#580|<br/><br/>|{ 580a }|\r\n#581|<br/><br/>|{ 5633 }{ 581a }{ 581z }|\r\n#584|<br/><br/>|{ 5843 }{ 584a }{ 584b }|\r\n#585|<br/><br/>|{ 5853 }{ 585a }|\r\n#586|<br/><br/>|{ 5863 }{ 586a }|\r\n#020|<br/><br/><label>ISBN: </label>|{ 020a }{ 020c }|\r\n#022|<br/><br/><label>ISSN: </label>|{ 022a }|\r\n#222| = |{ 222a }{ 222b }|\r\n#210| = |{ 210a }{ 210b }|\r\n#024|<br/><br/><label>Standard No.: </label>|{ 024a }{ 024c }{ 024d }{ 0242 }|\r\n#027|<br/><br/><label>Standard Tech. Report. No.: </label>|{ 027a }|\r\n#028|<br/><br/><label>Publisher. No.: </label>|{ 028a }{ 028b }|\r\n#013|<br/><br/><label>Patent No.: </label>|{ 013a }{ 013b }{ 013c }{ 013d }{ 013e }{ 013f }|\r\n#030|<br/><br/><label>CODEN: </label>|{ 030a }|\r\n#037|<br/><br/><label>Source: </label>|{ 037a }{ 037b }{ 037c }{ 037f }{ 037g }{ 037n }|\r\n#010|<br/><br/><label>LCCN: </label>|{ 010a }|\r\n#015|<br/><br/><label>Nat. Bib. No.: </label>|{ 015a }{ 0152 }|\r\n#016|<br/><br/><label>Nat. Bib. Agency Control No.: </label>|{ 016a }{ 0162 }|\r\n#600|<br/><br/><label>Subjects--Personal Names: </label>|{\\n6003 }{\\n600a}{ 600b }{ 600c }{ 600d }{ 600e }{ 600f }{ 600g }{ 600h }{--600k}{ 600l }{ 600m }{ 600n }{ 600o }{--600p}{ 600r }{ 600s }{ 600t }{ 600u }{--600x}{--600z}{--600y}{--600v}|\r\n#610|<br/><br/><label>Subjects--Corporate Names: </label>|{\\n6103 }{\\n610a}{ 610b }{ 610c }{ 610d }{ 610e }{ 610f }{ 610g }{ 610h }{--610k}{ 610l }{ 610m }{ 610n }{ 610o }{--610p}{ 610r }{ 610s }{ 610t }{ 610u }{--610x}{--610z}{--610y}{--610v}|\r\n#611|<br/><br/><label>Subjects--Meeting Names: </label>|{\\n6113 }{\\n611a}{ 611b }{ 611c }{ 611d }{ 611e }{ 611f }{ 611g }{ 611h }{--611k}{ 611l }{ 611m }{ 611n }{ 611o }{--611p}{ 611r }{ 611s }{ 611t }{ 611u }{--611x}{--611z}{--611y}{--611v}|\r\n#630|<br/><br/><label>Subjects--Uniform Titles: </label>|{\\n630a}{ 630b }{ 630c }{ 630d }{ 630e }{ 630f }{ 630g }{ 630h }{--630k }{ 630l }{ 630m }{ 630n }{ 630o }{--630p}{ 630r }{ 630s }{ 630t }{--630x}{--630z}{--630y}{--630v}|\r\n#648|<br/><br/><label>Subjects--Chronological Terms: </label>|{\\n6483 }{\\n648a }{--648x}{--648z}{--648y}{--648v}|\r\n#650|<br/><br/><label>Subjects--Topical Terms: </label>|{\\n6503 }{\\n650a}{ 650b }{ 650c }{ 650d }{ 650e }{--650x}{--650z}{--650y}{--650v}|\r\n#651|<br/><br/><label>Subjects--Geographic Terms: </label>|{\\n6513 }{\\n651a}{ 651b }{ 651c }{ 651d }{ 651e }{--651x}{--651z}{--651y}{--651v}|\r\n#653|<br/><br/><label>Subjects--Index Terms: </label>|{ 653a }|\r\n#654|<br/><br/><label>Subjects--Facted Index Terms: </label>|{\\n6543 }{\\n654a}{--654b}{--654x}{--654z}{--654y}{--654v}|\r\n#655|<br/><br/><label>Index Terms--Genre/Form: </label>|{\\n6553 }{\\n655a}{--655b}{--655x }{--655z}{--655y}{--655v}|\r\n#656|<br/><br/><label>Index Terms--Occupation: </label>|{\\n6563 }{\\n656a}{--656k}{--656x}{--656z}{--656y}{--656v}|\r\n#657|<br/><br/><label>Index Terms--Function: </label>|{\\n6573 }{\\n657a}{--657x}{--657z}{--657y}{--657v}|\r\n#658|<br/><br/><label>Index Terms--Curriculum Objective: </label>|{\\n658a}{--658b}{--658c}{--658d}{--658v}|\r\n#050|<br/><br/><label>LC Class. No.: </label>|{ 050a }{ / 050b }|\r\n#082|<br/><br/><label>Dewey Class. No.: </label>|{ 082a }{ / 082b }|\r\n#080|<br/><br/><label>Universal Decimal Class. No.: </label>|{ 080a }{ 080x }{ / 080b }|\r\n#070|<br/><br/><label>National Agricultural Library Call No.: </label>|{ 070a }{ / 070b }|\r\n#060|<br/><br/><label>National Library of Medicine Call No.: </label>|{ 060a }{ / 060b }|\r\n#074|<br/><br/><label>GPO Item No.: </label>|{ 074a }|\r\n#086|<br/><br/><label>Gov. Doc. Class. No.: </label>|{ 086a }|\r\n#088|<br/><br/><label>Report. No.: </label>|{ 088a }|','ISBD','70|10','Textarea');
629 OPACISBDEN
630     }    
631     print "Upgrade to $DBversion done (new OPACISBD syspref, )\n";
632     SetVersion ($DBversion);
633 }  
634
635 $DBversion = "3.00.05.001";
636 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
637     $dbh->do(<<DEFAULTBRANCHRULES);
638     CREATE TABLE `default_branch_item_rules` (
639       `itemtype` varchar(10) NOT NULL,
640       `holdallowed` tinyint(1) default NULL,
641       PRIMARY KEY  (`itemtype`),
642       CONSTRAINT `default_branch_item_rules_ibfk_1` FOREIGN KEY (`itemtype`) REFERENCES `itemtypes` (`itemtype`)
643         ON DELETE CASCADE ON UPDATE CASCADE
644     ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
645 DEFAULTBRANCHRULES
646 }
647
648 $DBversion = "3.00.05.001";
649 #$DBversion = "3.01.00.012";
650 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
651     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('AllowHoldPolicyOverride', '0', 'Allow staff to override hold policies when placing holds',NULL,'YesNo')");
652     $dbh->do("
653         CREATE TABLE `branch_item_rules` (
654           `branchcode` varchar(10) NOT NULL,
655           `itemtype` varchar(10) NOT NULL,
656           `holdallowed` tinyint(1) default NULL,
657           PRIMARY KEY  (`itemtype`,`branchcode`),
658           KEY `branch_item_rules_ibfk_2` (`branchcode`),
659           CONSTRAINT `branch_item_rules_ibfk_1` FOREIGN KEY (`itemtype`) REFERENCES `itemtypes` (`itemtype`) ON DELETE CASCADE ON UPDATE CASCADE,
660           CONSTRAINT `branch_item_rules_ibfk_2` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE
661         ) ENGINE=InnoDB DEFAULT CHARSET=utf8
662     ");
663     $dbh->do("
664         ALTER TABLE default_branch_circ_rules
665             ADD COLUMN holdallowed tinyint(1) NULL
666     ");
667     $dbh->do("
668         ALTER TABLE default_circ_rules
669             ADD COLUMN holdallowed tinyint(1) NULL
670     ");
671     print "Upgrade to $DBversion done (Add tables and system preferences for holds policies)\n";
672     SetVersion ($DBversion);
673 }
674
675 $DBversion = "3.00.05.002";
676 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
677     $dbh->do("UPDATE systempreferences SET options = 'Calendar|Days|Datedue' WHERE variable = 'useDaysMode'");
678     
679     print "Upgrade to $DBversion done (upgrade useDaysMode syspref)\n";
680     SetVersion ($DBversion);
681 }
682
683 $DBversion = "3.00.05.003";
684 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
685     $dbh->do("UPDATE language_rfc4646_to_iso639 SET iso639_2_code = 'por' WHERE rfc4646_subtag='pt' ");
686     print "Upgrade to $DBversion done (corrected ISO 639-2 language code for Portuguese)\n";
687     
688     SetVersion ($DBversion);
689 }
690
691
692 $DBversion = "3.00.06.001";
693 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
694     my $value = $dbh->selectrow_array("SELECT value FROM systempreferences WHERE variable = 'HomeOrHoldingBranch'");
695     $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('HomeOrHoldingBranchReturn','$value','Used by Circulation to determine which branch of an item to check checking-in items','holdingbranch|homebranch','Choice');");
696     print "Upgrade to $DBversion done (Add HomeOrHoldingBranchReturn system preference)\n";
697     SetVersion ($DBversion);
698 }
699
700 $DBversion = "3.00.06.002";
701 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
702     $dbh->do("ALTER TABLE issues CHANGE COLUMN `itemnumber` `itemnumber` int(11) UNIQUE DEFAULT NULL;");
703     $dbh->do("ALTER TABLE serialitems ADD CONSTRAINT `serialitems_sfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE CASCADE ON UPDATE CASCADE;");
704     print "Upgrade to $DBversion done (Improve serialitems table security)\n";
705     SetVersion ($DBversion);
706 }
707
708 $DBversion = "3.00.06.003";
709 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
710     $dbh->do("UPDATE systempreferences set value='../koha-tmpl/opac-tmpl/prog/en/xslt/".C4::Context->preference('marcflavour')."slim2OPACDetail.xsl',type='Free' where variable='XSLTDetailsDisplay' AND value=1;");
711     $dbh->do("UPDATE systempreferences set value='../koha-tmpl/opac-tmpl/prog/en/xslt/".C4::Context->preference('marcflavour')."slim2OPACResults.xsl',type='Free' where variable='XSLTResultsDisplay' AND value=1;");
712     print "Upgrade to $DBversion done (Improve XSLT)\n";
713     SetVersion ($DBversion);
714 }
715
716 $DBversion = "3.00.06.004";
717 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
718     $dbh->do("INSERT INTO systempreferences  (variable,value,explanation,options,type) VALUES('IndependentBranchPatron','0','If ON, librarian patron search can only be done on patron of same library as librarian',NULL,'YesNo');");
719     print "Upgrade to $DBversion done (Add IndependentBranchPatron system preference to be able to limit patron search to librarian's Library)\n";
720     SetVersion ($DBversion);
721 }
722
723 $DBversion = '3.00.06.005';
724 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
725     $dbh->do(qq{INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('OPACviewISBD','1','Allow display of ISBD view of bibiographic records in OPAC','','YesNo');});
726     $dbh->do(qq{INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('OPACviewMARC','1','Allow display of MARC view of bibiographic records in OPAC','','YesNo');});
727
728     print "Upgrade to $DBversion done (Added OPAC ISBD and MARC view sysprefs)\n";
729     SetVersion ($DBversion);
730 }
731
732 $DBversion = '3.00.06.006';
733 if (C4::Context->preference('Version') < TransformToNum($DBversion)){
734     $dbh->do("
735         INSERT INTO `letter` (module, code, name, title, content)         VALUES('reserves', 'STAFFHOLDPLACED', 'Hold Placed on Item (from staff)', 'Hold Placed on Item (from staff)','An hold has been placed on the following item from the intranet : <<title>> (<<biblionumber>>) for the user <<firstname>> <<surname>> (<<cardnumber>>).');
736     ");
737     print "Upgrade to $DBversion done (Added notice for hold from staff)\n";
738     SetVersion ($DBversion);
739 }
740
741 $DBversion = '3.00.06.007';
742 if (C4::Context->preference('Version') < TransformToNum($DBversion)){
743     $dbh->do("INSERT INTO `permissions` (`module_bit` , `code` , `description`) VALUES ('9', 'edit_items', 'Edit items');");
744     print "Upgrade to $DBversion done (Added 'Edit Items' permission)\n";
745     SetVersion ($DBversion);
746 }
747
748
749
750
751 =item DropAllForeignKeys($table)
752
753   Drop all foreign keys of the table $table
754
755 =cut
756
757 sub DropAllForeignKeys {
758     my ($table) = @_;
759     # get the table description
760     my $sth = $dbh->prepare("SHOW CREATE TABLE $table");
761     $sth->execute;
762     my $vsc_structure = $sth->fetchrow;
763     # split on CONSTRAINT keyword
764     my @fks = split /CONSTRAINT /,$vsc_structure;
765     # parse each entry
766     foreach (@fks) {
767         # isolate what is before FOREIGN KEY, if there is something, it's a foreign key to drop
768         $_ = /(.*) FOREIGN KEY.*/;
769         my $id = $1;
770         if ($id) {
771             # we have found 1 foreign, drop it
772             $dbh->do("ALTER TABLE $table DROP FOREIGN KEY $id");
773             $id="";
774         }
775     }
776 }
777
778
779 =item TransformToNum
780
781   Transform the Koha version from a 4 parts string
782   to a number, with just 1 .
783
784 =cut
785
786 sub TransformToNum {
787     my $version = shift;
788     # remove the 3 last . to have a Perl number
789     $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
790     return $version;
791 }
792
793 =item SetVersion
794
795     set the DBversion in the systempreferences
796
797 =cut
798
799 sub SetVersion {
800     my $kohaversion = TransformToNum(shift);
801     if (C4::Context->preference('Version')) {
802       my $finish=$dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'");
803       $finish->execute($kohaversion);
804     } else {
805       my $finish=$dbh->prepare("INSERT into systempreferences (variable,value,explanation) values ('Version',?,'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller')");
806       $finish->execute($kohaversion);
807     }
808 }
809 exit;
810