Bug 24387: Rename "News" with "Additional contents"
[koha.git] / installer / data / mysql / atomicupdate / bug_24387.perl
1 $DBversion = 'XXX'; # will be replaced by the RM
2 if( CheckVersion( $DBversion ) ) {
3
4     if( TableExists('opac_news') ) {
5         $dbh->do(q|
6             ALTER TABLE opac_news RENAME additional_contents
7         |);
8     }
9
10     if ( foreign_key_exists('additional_contents', 'opac_news_branchcode_ibfk') ) {
11
12         $dbh->do(q|
13             ALTER TABLE additional_contents
14             DROP KEY borrowernumber_fk,
15             DROP KEY opac_news_branchcode_ibfk,
16             DROP FOREIGN KEY borrowernumber_fk,
17             DROP FOREIGN KEY opac_news_branchcode_ibfk
18         |);
19
20         $dbh->do(q|
21             ALTER TABLE additional_contents
22             ADD CONSTRAINT  additional_contents_borrowernumber_fk
23                 FOREIGN KEY (borrowernumber)
24                 REFERENCES borrowers (borrowernumber) ON DELETE SET NULL ON UPDATE CASCADE
25         |);
26
27         $dbh->do(q|
28             ALTER TABLE additional_contents
29             ADD CONSTRAINT  additional_contents_branchcode_ibfk
30                 FOREIGN KEY (branchcode)
31                 REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE
32         |);
33     }
34
35     $dbh->do(q|
36         UPDATE letter
37         SET content = REGEXP_REPLACE(content, '<<\\\\s*opac_news\.', '<<additional_contents.')
38     |);
39     $dbh->do(q|
40         UPDATE letter
41         SET content = REGEXP_REPLACE(content, '\\\\[%\\\\s*opac_news\.', '[% additional_contents.')
42     |);
43
44     $dbh->do(q|
45         UPDATE systempreferences
46         SET variable="AdditionalContentsEditor"
47         WHERE variable="NewsToolEditor"
48     |);
49
50     $dbh->do(q|
51         UPDATE permissions
52         SET code="edit_additional_contents"
53         WHERE code="edit_news"
54     |);
55
56     unless ( column_exists('additional_contents', 'category' ) ) {
57         $dbh->do(q|
58             ALTER TABLE additional_contents
59             ADD COLUMN `category` VARCHAR(20) NOT NULL COMMENT 'category for the additional content'
60             AFTER `idnew`
61         |);
62     }
63     unless ( column_exists('additional_contents', 'location' ) ) {
64         $dbh->do(q|
65             ALTER TABLE additional_contents
66             ADD COLUMN `location` VARCHAR(255) NOT NULL COMMENT 'location of the additional content'
67             AFTER `category`
68         |);
69     }
70
71     unless ( column_exists('additional_contents', 'code' ) ) {
72         $dbh->do(q|
73             ALTER TABLE additional_contents
74             ADD COLUMN `code` VARCHAR(100) NOT NULL COMMENT 'code to group content per lang'
75             AFTER `category`
76         |);
77     }
78
79     my $contents = $dbh->selectall_arrayref(q|SELECT * FROM additional_contents|, { Slice => {} });
80     for my $c ( @$contents ) {
81         my ( $category, $location, $new_lang );
82         if ( $c->{lang} eq '' ) {
83             $category = 'news';
84             $location = 'staff_and_opac';
85             $new_lang = 'default';
86         } elsif ( $c->{lang} eq 'koha' ) {
87             $category = 'news';
88             $location = 'staff_only';
89             $new_lang = 'default';
90         } elsif ( $c->{lang} eq 'slip' ) {
91             $category = 'news';
92             $location = 'slip';
93             $new_lang = 'default';
94         } elsif ( $c->{lang} =~ m|_| ) {
95             ( $location, $new_lang ) = split '_', $c->{lang};
96             $category = 'html_customizations'
97         } else {
98             $category = 'news';
99             $location = 'opac_only';
100             $new_lang = $c->{lang};
101         }
102
103         die "There is something wrong here, we didn't find a valid category for idnew=" . $c->{idnew} unless $category;
104
105         # Now this is getting weird
106         # We are adding an extra news with the same code when the lang is not "default" (/"en")
107
108         $new_lang = "default" if $new_lang eq 'en'; # Assume that "en" is "default"
109
110         my $sth_update = $dbh->prepare(q|
111             UPDATE additional_contents
112             SET category=?, location=?, lang=?
113             WHERE idnew=?
114         |);
115
116         my $parent_idnew;
117         if ( $new_lang ne 'default' ) {
118             $dbh->do(q|
119                 INSERT INTO additional_contents(category, code, location, branchcode, title, content, lang, published_on, updated_on, expirationdate, number, borrowernumber)
120                 VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
121             |, undef, $category, 'tmp_code', $location, $c->{branchcode}, $c->{title}, $c->{content}, 'default', $c->{published_on}, $c->{updated_on}, $c->{expirationdate}, $c->{number}, $c->{borrowernumber});
122
123             $parent_idnew = $dbh->last_insert_id(undef, undef, 'additional_contents', undef);
124         }
125         $sth_update->execute($category, $location, $new_lang, $c->{idnew});
126
127         my $idnew = $parent_idnew || $c->{idnew};
128         my $code = ( grep {$_ eq $location} qw( staff_and_opac staff_only opac_only slip ) ) ? "${location}_$idnew" : "News_$idnew";
129         $dbh->do(q|UPDATE additional_contents SET code=? WHERE idnew = ?|, undef, $code, $idnew) if $parent_idnew;
130         $dbh->do(q|UPDATE additional_contents SET code=? WHERE idnew = ?|, undef, $code, $idnew);
131     }
132
133     $dbh->do(q|
134         ALTER TABLE additional_contents
135         ADD UNIQUE KEY additional_contents_uniq (`category`,`code`,`branchcode`,`lang`)
136     |);
137
138     NewVersion( $DBversion, 24387, "Rename opac_news with additional_contents");
139 }