-
Notifications
You must be signed in to change notification settings - Fork 257
/
ImportExportFramework.pm
1210 lines (1091 loc) · 49.3 KB
/
ImportExportFramework.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package C4::ImportExportFramework;
# Copyright 2010-2011 MASmedios.com y Ministerio de Cultura
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use strict;
use warnings;
use XML::LibXML;
use XML::LibXML::XPathContext;
use Digest::MD5;
use POSIX qw( strftime );
use Text::CSV_XS;
use List::MoreUtils qw( indexes );
use C4::Context;
use Koha::Logger;
our (@ISA, @EXPORT_OK);
BEGIN {
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(
ExportFramework
ImportFramework
createODS
);
}
use constant XMLSTR => '<?xml version="1.0" encoding="UTF-8"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s27">
<Font x:Family="Swiss" ss:Color="#0000FF" ss:Bold="1"/>
</Style>
<Style ss:ID="s21">
<NumberFormat ss:Format="yyyy\-mm\-dd"/>
</Style>
<Style ss:ID="s22">
<NumberFormat ss:Format="yyyy\-mm\-dd\ hh:mm:ss"/>
</Style>
<Style ss:ID="s23">
<NumberFormat ss:Format="hh:mm:ss"/>
</Style>
</Styles>
</Workbook>
';
use constant ODSSTR => '<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.0">
<office:scripts/>
<office:font-face-decls/>
<office:automatic-styles/>
</office:document-content>';
use constant ODS_STYLES_STR => '<?xml version="1.0" encoding="UTF-8"?>
<office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" office:version="1.0">
<office:font-face-decls></office:font-face-decls>
<office:styles></office:styles>
<office:automatic-styles></office:automatic-styles>
<office:master-styles></office:master-styles>
</office:document-styles>';
use constant ODS_SETTINGS_STR => '<?xml version="1.0" encoding="UTF-8"?>
<office:document-settings xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0" xmlns:ooo="http://openoffice.org/2004/office" office:version="1.0"><office:settings>
<config:config-item-set config:name="ooo:view-settings">
<config:config-item config:name="VisibleAreaTop" config:type="int">0</config:config-item>
<config:config-item config:name="VisibleAreaLeft" config:type="int">0</config:config-item>
<config:config-item config:name="VisibleAreaWidth" config:type="int">2000</config:config-item>
<config:config-item config:name="VisibleAreaHeight" config:type="int">900</config:config-item>
<config:config-item-map-indexed config:name="Views"><config:config-item-map-entry>
<config:config-item config:name="ViewId" config:type="string">View1</config:config-item>
<config:config-item-map-named config:name="Tables">
<config:config-item-map-entry config:name="Sheet1"><config:config-item config:name="CursorPositionX" config:type="int">0</config:config-item><config:config-item config:name="CursorPositionY" config:type="int">1</config:config-item><config:config-item config:name="HorizontalSplitMode" config:type="short">0</config:config-item><config:config-item config:name="VerticalSplitMode" config:type="short">0</config:config-item><config:config-item config:name="HorizontalSplitPosition" config:type="int">0</config:config-item><config:config-item config:name="VerticalSplitPosition" config:type="int">0</config:config-item><config:config-item config:name="ActiveSplitRange" config:type="short">2</config:config-item><config:config-item config:name="PositionLeft" config:type="int">0</config:config-item><config:config-item config:name="PositionRight" config:type="int">0</config:config-item><config:config-item config:name="PositionTop" config:type="int">0</config:config-item><config:config-item config:name="PositionBottom" config:type="int">0</config:config-item>
</config:config-item-map-entry>
</config:config-item-map-named>
<config:config-item config:name="ActiveTable" config:type="string">Sheet1</config:config-item>
<config:config-item config:name="HorizontalScrollbarWidth" config:type="int">270</config:config-item>
<config:config-item config:name="ZoomType" config:type="short">0</config:config-item>
<config:config-item config:name="ZoomValue" config:type="int">100</config:config-item>
<config:config-item config:name="PageViewZoomValue" config:type="int">50</config:config-item>
<config:config-item config:name="ShowPageBreakPreview" config:type="boolean">false</config:config-item>
<config:config-item config:name="ShowZeroValues" config:type="boolean">true</config:config-item>
<config:config-item config:name="ShowNotes" config:type="boolean">true</config:config-item>
<config:config-item config:name="ShowGrid" config:type="boolean">true</config:config-item>
<config:config-item config:name="GridColor" config:type="long">12632256</config:config-item>
<config:config-item config:name="ShowPageBreaks" config:type="boolean">true</config:config-item>
<config:config-item config:name="HasColumnRowHeaders" config:type="boolean">true</config:config-item>
<config:config-item config:name="HasSheetTabs" config:type="boolean">true</config:config-item>
<config:config-item config:name="IsOutlineSymbolsSet" config:type="boolean">true</config:config-item>
<config:config-item config:name="IsSnapToRaster" config:type="boolean">false</config:config-item>
<config:config-item config:name="RasterIsVisible" config:type="boolean">false</config:config-item>
<config:config-item config:name="IsRasterAxisSynchronized" config:type="boolean">true</config:config-item></config:config-item-map-entry></config:config-item-map-indexed>
</config:config-item-set>
<config:config-item-set config:name="ooo:configuration-settings">
<config:config-item config:name="ShowZeroValues" config:type="boolean">true</config:config-item>
<config:config-item config:name="ShowNotes" config:type="boolean">true</config:config-item>
<config:config-item config:name="ShowGrid" config:type="boolean">true</config:config-item>
<config:config-item config:name="GridColor" config:type="long">12632256</config:config-item>
<config:config-item config:name="ShowPageBreaks" config:type="boolean">true</config:config-item>
<config:config-item config:name="LinkUpdateMode" config:type="short">3</config:config-item>
<config:config-item config:name="HasColumnRowHeaders" config:type="boolean">true</config:config-item>
<config:config-item config:name="HasSheetTabs" config:type="boolean">true</config:config-item>
<config:config-item config:name="IsOutlineSymbolsSet" config:type="boolean">true</config:config-item>
<config:config-item config:name="IsSnapToRaster" config:type="boolean">false</config:config-item>
<config:config-item config:name="RasterIsVisible" config:type="boolean">false</config:config-item>
<config:config-item config:name="IsRasterAxisSynchronized" config:type="boolean">true</config:config-item>
<config:config-item config:name="AutoCalculate" config:type="boolean">true</config:config-item>
<config:config-item config:name="PrinterName" config:type="string">Generic Printer</config:config-item>
<config:config-item config:name="ApplyUserData" config:type="boolean">true</config:config-item>
<config:config-item config:name="CharacterCompressionType" config:type="short">0</config:config-item>
<config:config-item config:name="SaveVersionOnClose" config:type="boolean">false</config:config-item>
<config:config-item config:name="UpdateFromTemplate" config:type="boolean">false</config:config-item>
<config:config-item config:name="AllowPrintJobCancel" config:type="boolean">true</config:config-item>
<config:config-item config:name="LoadReadonly" config:type="boolean">false</config:config-item>
</config:config-item-set>
</office:settings></office:document-settings>';
use constant ODS_MANIFEST_STR => '<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
<manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.spreadsheet" manifest:full-path="/"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/statusbar/"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/accelerator/"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/floater/"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/popupmenu/"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/progressbar/"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/menubar/"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/toolbar/"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/images/Bitmaps/"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/images/"/>
<manifest:file-entry manifest:media-type="application/vnd.sun.xml.ui.configuration" manifest:full-path="Configurations2/"/>
<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/>
<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="styles.xml"/>
<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="meta.xml"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Thumbnails/"/>
<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="settings.xml"/>
</manifest:manifest>';
=head1 NAME
C4::ImportExportFramework - Import/Export Framework to Excel-xml/ODS Module Functions
=head1 SYNOPSIS
use C4::ImportExportFramework;
=head1 DESCRIPTION
Module to Import/Export Framework to Excel-xml/ODS on intranet administration - MARC Frameworks section
Module to Import/Export Framework to Excel-xml/ODS on intranet administration - MARC Frameworks section
exporting the tables marc_tag_structure, marc_subfield_structure to excel-xml/ods or viceversa
Functions for handling import/export.
=head1 SUBROUTINES
=head2 ExportFramework
Export all information of a bibliographic or authority MARC framework to an Excel "xml" file, comma separated values "csv" or OpenDocument SpreadSheet "ods".
return :
succes
=cut
sub ExportFramework
{
my ($frameworkcode, $xmlStrRef, $mode, $frameworktype) = @_;
my $dbh = C4::Context->dbh;
if ($dbh) {
my $dom;
my $root;
my $elementSS;
if ($mode eq 'ods' || $mode eq 'excel') {
eval {
my $parser = XML::LibXML->new();
$dom = $parser->parse_string(($mode && $mode eq 'ods')?ODSSTR:XMLSTR);
if ($dom) {
$root = $dom->documentElement();
if ($mode && $mode eq 'ods') {
my $elementBody = $dom->createElement('office:body');
$root->appendChild($elementBody);
$elementSS = $dom->createElement('office:spreadsheet');
$elementBody->appendChild($elementSS);
}
}
};
if ($@) {
Koha::Logger->get->warn("Error ExportFramework $@");
return 0;
}
}
my $table = 'marc_tag_structure';
my $subtable = 'marc_subfield_structure';
if ($frameworktype eq "authority") {
$table = 'auth_tag_structure';
$subtable = 'auth_subfield_structure';
}
if (_export_table($table, $dbh, ($mode eq 'csv')?$xmlStrRef:$dom, ($mode eq 'ods')?$elementSS:$root, $frameworkcode, $mode, $frameworktype)) {
if (_export_table($subtable, $dbh, ($mode eq 'csv')?$xmlStrRef:$dom, ($mode eq 'ods')?$elementSS:$root, $frameworkcode, $mode, $frameworktype)) {
$$xmlStrRef = $dom->toString(1) if ($mode eq 'ods' || $mode eq 'excel');
return 1;
}
}
}
return 0;
}#ExportFramework
# Export all the data from a mysql table to an spreadsheet.
sub _export_table
{
my ($table, $dbh, $dom, $root, $frameworkcode, $mode, $frameworktype) = @_;
if ($mode eq 'csv') {
_export_table_csv($table, $dbh, $dom, $root, $frameworkcode, $frameworktype);
} elsif ($mode eq 'ods') {
_export_table_ods($table, $dbh, $dom, $root, $frameworkcode, $frameworktype);
} else {
_export_table_excel($table, $dbh, $dom, $root, $frameworkcode, $frameworktype);
}
}
# Export the mysql table to an csv file
sub _export_table_csv
{
my ($table, $dbh, $strCSV, $root, $frameworkcode, $frameworktype) = @_;
eval {
# First row with the name of the columns
my $query = 'SHOW COLUMNS FROM ' . $table;
my $sth = $dbh->prepare($query);
$sth->execute();
my @fields = ();
while (my $hashRef = $sth->fetchrow_hashref) {
$$strCSV .= '"' . $hashRef->{Field} . '",';
push @fields, $hashRef->{Field};
}
chop $$strCSV;
$$strCSV .= chr(10);
# Populate rows with the data from mysql
$query = 'SELECT * FROM ' . $table . ' WHERE frameworkcode=?';
if ($frameworktype eq "authority") {
$query = 'SELECT * FROM ' . $table . ' WHERE authtypecode=?';
}
$sth = $dbh->prepare($query);
$sth->execute($frameworkcode);
my $data;
while (my $hashRef = $sth->fetchrow_hashref) {
for my $field (@fields) {
my $value = $hashRef->{$field} // q||;
$value =~ s/[\r\n]//g;
$value =~ s/"/""/g;
$$strCSV .= '"' . $value . '",';
}
chop $$strCSV;
$$strCSV .= chr(10);
}
$$strCSV .= chr(10);
for (@fields) {
# Separator for change of table
$$strCSV .= '"#-#",';
}
chop $$strCSV;
$$strCSV .= chr(10);
$$strCSV .= chr(10);
};
if ($@) {
Koha::Logger->get->warn("Error _export_table_csv $@");
return 0;
}
return 1;
}#_export_table_csv
# Export the mysql table to an ods file
sub _export_table_ods
{
my ($table, $dbh, $dom, $root, $frameworkcode, $frameworktype) = @_;
eval {
my $elementTable = $dom->createElement('table:table');
$elementTable->setAttribute('table:name', $table);
$elementTable->setAttribute('table:print', 'false');
$root->appendChild($elementTable);
my $elementRow = $dom->createElement('table:table-row');
$elementTable->appendChild($elementRow);
my $elementCell;
my $elementData;
# First row with the name of the columns
my $query = 'SHOW COLUMNS FROM ' . $table;
my $sth = $dbh->prepare($query);
$sth->execute();
my @fields = ();
while (my $hashRef = $sth->fetchrow_hashref) {
$elementCell = $dom->createElement('table:table-cell');
$elementCell->setAttribute('office:value-type', 'string');
$elementCell->setAttribute('office:value', $hashRef->{Field});
$elementRow->appendChild($elementCell);
$elementData = $dom->createElement('text:p');
$elementCell->appendChild($elementData);
$elementData->appendTextNode($hashRef->{Field});
push @fields, {name => $hashRef->{Field}, type => ($hashRef->{Type} =~ /int/i)?'float':'string'};
}
# Populate rows with the data from mysql
$query = 'SELECT * FROM ' . $table . ' WHERE frameworkcode=?';
if ($frameworktype eq "authority") {
$query = 'SELECT * FROM ' . $table . ' WHERE authtypecode=?';
}
$sth = $dbh->prepare($query);
$sth->execute($frameworkcode);
my $data;
while (my $hashRef = $sth->fetchrow_hashref) {
$elementRow = $dom->createElement('table:table-row');
$elementTable->appendChild($elementRow);
for (@fields) {
$data = $hashRef->{$_->{name}};
if ($_->{type} eq 'float' && !defined($data)) {
$data = '0';
} elsif ($_->{type} eq 'string' && !defined($data)) {
$data = q{};
} elsif ($_->{type} eq 'string' && (!$data && $data ne '0')) {
$data = '#';
}
$data = _parseContent2Xml($data) if ($_->{type} eq 'string');
$elementCell = $dom->createElement('table:table-cell');
$elementCell->setAttribute('office:value-type', $_->{type});
$elementCell->setAttribute('office:value', $data);
$elementRow->appendChild($elementCell);
$elementData = $dom->createElement('text:p');
$elementCell->appendChild($elementData);
$elementData->appendTextNode($data);
}
}
};
if ($@) {
Koha::Logger->get->warn("Error _export_table_ods $@");
return 0;
}
return 1;
}#_export_table_ods
# Export the mysql table to an excel-xml (openoffice/libreoffice compatible) file
sub _export_table_excel
{
my ($table, $dbh, $dom, $root, $frameworkcode, $frameworktype) = @_;
eval {
my $elementWS = $dom->createElement('Worksheet');
$elementWS->setAttribute('ss:Name', $table);
$root->appendChild($elementWS);
my $elementTable = $dom->createElement('ss:Table');
$elementWS->appendChild($elementTable);
my $elementRow = $dom->createElement('ss:Row');
$elementTable->appendChild($elementRow);
# First row with the name of the columns
my $elementCell;
my $elementData;
my $query = 'SHOW COLUMNS FROM ' . $table;
my $sth = $dbh->prepare($query);
$sth->execute();
my @fields = ();
while (my $hashRef = $sth->fetchrow_hashref) {
$elementCell = $dom->createElement('ss:Cell');
$elementCell->setAttribute('ss:StyleID', 's27');
$elementRow->appendChild($elementCell);
$elementData = $dom->createElement('ss:Data');
$elementData->setAttribute('ss:Type', 'String');
$elementCell->appendChild($elementData);
$elementData->appendTextNode($hashRef->{Field});
push @fields, {name => $hashRef->{Field}, type => ($hashRef->{Type} =~ /int/i)?'Number':'String'};
}
# Populate rows with the data from mysql
$query = 'SELECT * FROM ' . $table . ' WHERE frameworkcode=?';
if ($frameworktype eq "authority") {
$query = 'SELECT * FROM ' . $table . ' WHERE authtypecode=?';
}
$sth = $dbh->prepare($query);
$sth->execute($frameworkcode);
my $data;
while (my $hashRef = $sth->fetchrow_hashref) {
$elementRow = $dom->createElement('ss:Row');
$elementTable->appendChild($elementRow);
for (@fields) {
$elementCell = $dom->createElement('ss:Cell');
$elementRow->appendChild($elementCell);
$elementData = $dom->createElement('ss:Data');
$elementData->setAttribute('ss:Type', $_->{type});
$elementCell->appendChild($elementData);
$data = $hashRef->{$_->{name}};
if ($_->{type} eq 'Number' && !defined($data)) {
$data = '0';
} elsif ($_->{type} eq 'String' && !defined($data)) {
$data = q{};
} elsif ($_->{type} eq 'String' && (!$data && $data ne '0')) {
$data = '#';
}
$elementData->appendTextNode(($_->{type} eq 'String')?_parseContent2Xml($data):$data);
}
}
};
if ($@) {
Koha::Logger->get->warn("Error _export_table_excel $@");
return 0;
}
return 1;
}#_export_table_excel
# Format chars problematics to a correct format for xml.
sub _parseContent2Xml
{
my $content = shift;
$content =~ s/\&(?![a-zA-Z#0-9]{1,4};)/&/g;
$content =~ s/</</g;
$content =~ s/>/>/g;
return $content;
}#_parseContent2Xml
# Get the tmp directory on the system
sub _getTmp
{
my $tmp = '/tmp';
if ($ENV{'TMP'} && -d $ENV{'TMP'}) {
$tmp = $ENV{'TMP'};
} elsif ($ENV{'TMPDIR'} && -d $ENV{'TMPDIR'}) {
$tmp = $ENV{'TMPDIR'};
} elsif ($ENV{'TEMP'} && -d $ENV{'TEMP'}) {
$tmp = $ENV{'TEMP'};
}
return $tmp;
}#_getTmp
# Create our tempdir directory for the ods process
sub _createTmpDir
{
my $tmp = shift;
my $tempdir = (-d $tmp)?$tmp . '/':'./';
$tempdir .= 'tmp_ods_' . Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().{}.$$));
eval {
mkdir $tempdir;
};
if ($@) {
return;
} else {
return $tempdir;
}
}#_createTmpDir
=head2 createODS
Creates a temporary directory to create the ods file and read it to store its content in a string.
return :
success
=cut
sub createODS
{
my ($strContent, $lang, $strODSRef) = @_;
my $tmp = _getTmp();
my $tempModule = 1;
my $tempdir;
eval {
require File::Temp;
import File::Temp qw/ tempfile tempdir /;
$tempdir = tempdir ( 'tmp_ods_' . $$ . '_XXXXXXXX', DIR => (-d $tmp)?$tmp:'.', CLEANUP => 1);
};
if ($@) {
$tempModule = 0;
$tempdir = _createTmpDir($tmp);
}
if ($tempdir) {
my $fh;
# populate tempdir directory with the ods elements
eval {
if (open($fh, '>', "$tempdir/content.xml")) {
print {$fh} $strContent;
close($fh);
}
if (open($fh, '>', "$tempdir/mimetype")) {
print {$fh} 'application/vnd.oasis.opendocument.spreadsheet';
close($fh);
}
if (open($fh, '>', "$tempdir/meta.xml")) {
print {$fh} _getMeta($lang);
close($fh);
}
if (open($fh, '>', "$tempdir/styles.xml")) {
print {$fh} ODS_STYLES_STR;
close($fh);
}
if (open($fh, '>', "$tempdir/settings.xml")) {
print {$fh} ODS_SETTINGS_STR;
close($fh);
}
mkdir($tempdir.'/META-INF/');
mkdir($tempdir.'/Configurations2/');
mkdir($tempdir.'/Configurations2/acceleator/');
mkdir($tempdir.'/Configurations2/images/');
mkdir($tempdir.'/Configurations2/popupmenu/');
mkdir($tempdir.'/Configurations2/statusbar/');
mkdir($tempdir.'/Configurations2/floater/');
mkdir($tempdir.'/Configurations2/menubar/');
mkdir($tempdir.'/Configurations2/progressbar/');
mkdir($tempdir.'/Configurations2/toolbar/');
if (open($fh, '>', "$tempdir/META-INF/manifest.xml")) {
print {$fh} ODS_MANIFEST_STR;
close($fh);
}
};
if ($@) {
Koha::Logger->get->warn("Error createODS $@");
} else {
# create ods file from tempdir directory
eval {
require Archive::Zip;
import Archive::Zip qw( :ERROR_CODES :CONSTANTS );
my $zip = Archive::Zip->new();
$zip->addTree( $tempdir, '' );
$zip->writeToFileNamed($tempdir . '/new.ods');
};
if ($@) {
my $cmd = qx(which zip 2>/dev/null || whereis zip);
chomp $cmd;
$cmd = 'zip' if (!$cmd || !-x $cmd);
system("cd $tempdir && $cmd -r new.ods ./");
}
my $ok = 0;
# read ods file and return as a string
if (-f "$tempdir/new.ods") {
if (open ($fh, '<', "$tempdir/new.ods")) {
binmode $fh;
my $buffer;
while (read ($fh, $buffer, 65536)) {
$$strODSRef .= $buffer;
}
close($fh);
$ok = 1;
}
}
# delete tempdir directory
if (!$tempModule && $tempdir) {
eval {
require File::Path;
import File::Temp qw/ rmtree /;
rmtree($tempdir);
};
if ($@) {
system("rm -rf $tempdir");
}
}
return 1 if ($ok);
}
}
return 0;
}#createODS
# return Meta content for ods file
sub _getMeta
{
my $lang = shift;
my $myDate = strftime ("%Y-%m-%dT%H:%M:%S", localtime(time()));
my $meta = '<?xml version="1.0" encoding="UTF-8"?>
<office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:ooo="http://openoffice.org/2004/office" office:version="1.0">
<office:meta>
<meta:generator>ods-php</meta:generator>
<meta:creation-date>' . $myDate . '</meta:creation-date>
<dc:date>' . $myDate . '</dc:date>
<dc:language>' . $lang . '</dc:language>
<meta:editing-cycles>2</meta:editing-cycles>
<meta:editing-duration>PT15S</meta:editing-duration>
<meta:user-defined meta:name="Info 1"/>
<meta:user-defined meta:name="Info 2"/>
<meta:user-defined meta:name="Info 3"/>
<meta:user-defined meta:name="Info 4"/>
</office:meta>
</office:document-meta>';
return $meta;
}#_getMeta
=head2 ImportFramework
Import all the information of a MARC or Authority Type Framework from a excel-xml/ods file.
return :
success
=cut
sub ImportFramework
{
my ($filename, $frameworkcode, $deleteFilename, $frameworktype) = @_;
my $tempdir;
my $ok = -1;
my $dbh = C4::Context->dbh;
$frameworktype ||= '';
if (-r $filename && $dbh) {
my $extension = '';
if ($filename =~ /\.(csv|ods|xml)$/i) {
$extension = lc($1);
} else {
unlink ($filename) if ($deleteFilename); # remove temporary file
return -1;
}
if ($extension eq 'ods') {
($tempdir, $filename) = _openODS($filename, $deleteFilename);
}
if ($filename) {
my $dom;
eval {
if ($extension eq 'ods' || $extension eq 'xml') {
# They have xml structure, so read it on a dom object
my $parser = XML::LibXML->new();
$dom = $parser->parse_file($filename);
if ($dom) {
my $root = $dom->documentElement();
}
} else {
# They are text files, so open it to read
open($dom, '<', $filename);
}
my $table = 'marc_tag_structure';
my $subtable = 'marc_subfield_structure';
if ($frameworktype eq "authority") {
$table = 'auth_tag_structure';
$subtable = 'auth_subfield_structure';
}
if ($dom) {
# Process both tables
my $numDeleted = 0;
my $numDeletedAux = 0;
if ($frameworktype eq "authority"){
if (($numDeletedAux = _import_table($dbh, $table, $frameworkcode, $dom, ['authtypecode', 'tagfield'], $extension, $frameworktype)) >= 0) {
$numDeleted += $numDeletedAux if ($numDeletedAux > 0);
if (($numDeletedAux = _import_table($dbh, $subtable, $frameworkcode, $dom, ['authtypecode', 'tagfield', 'tagsubfield'], $extension, $frameworktype)) >= 0) {
$numDeleted += $numDeletedAux if ($numDeletedAux > 0);
$ok = ($numDeleted > 0)?$numDeleted:0;
}
}
} else {
if (($numDeletedAux = _import_table($dbh, $table, $frameworkcode, $dom, ['frameworkcode', 'tagfield'], $extension, $frameworktype)) >= 0) {
$numDeleted += $numDeletedAux if ($numDeletedAux > 0);
if (($numDeletedAux = _import_table($dbh, $subtable, $frameworkcode, $dom, ['frameworkcode', 'tagfield', 'tagsubfield'], $extension, $frameworktype)) >= 0) {
$numDeleted += $numDeletedAux if ($numDeletedAux > 0);
$ok = ($numDeleted > 0)?$numDeleted:0;
}
}
}
} else {
Koha::Logger->get->warn("Error ImportFramework couldn't create dom");
}
};
if ($@) {
Koha::Logger->get->warn("Error ImportFramework $@");
} else {
if ($extension eq 'csv') {
close($dom) if ($dom);
}
}
}
unlink ($filename) if ($deleteFilename); # remove temporary file
} else {
Koha::Logger->get->warn("Error ImportFramework no conex to database or not readeable $filename");
}
if ($deleteFilename && $tempdir && -d $tempdir && -w $tempdir) {
eval {
require File::Path;
import File::Temp qw/ rmtree /;
rmtree($tempdir);
};
if ($@) {
system("rm -rf $tempdir");
}
}
return $ok;
}#ImportFramework
# Open (uncompress) ods file and return the content.xml file
sub _openODS
{
my ($filename, $deleteFilename) = @_;
my $tmp = _getTmp();
my $tempModule = 1;
my $tempdir;
eval {
require File::Temp;
import File::Temp qw/ tempfile tempdir /;
$tempdir = tempdir ( 'tmp_ods_' . $$ . '_XXXXXXXX', DIR => (-d $tmp)?$tmp:'.', CLEANUP => 1);
};
if ($@) {
$tempModule = 0;
$tempdir = _createTmpDir($tmp);
}
if ($tempdir) {
eval {
require Archive::Zip;
import Archive::Zip qw( :ERROR_CODES :CONSTANTS );
my $zip = Archive::Zip->new($filename);
foreach my $file ($zip->members) {
next if ($file->isDirectory);
(my $extractName = $file->fileName) =~ s{.*/}{};
next unless ($extractName eq 'content.xml');
$file->extractToFileNamed("$tempdir/$extractName");
}
};
if ($@) {
my $cmd = qx(which unzip 2>/dev/null || whereis unzip);
chomp $cmd;
$cmd = 'unzip' if (!$cmd || !-x $cmd);
system("$cmd $filename -d $tempdir");
}
if (-f "$tempdir/content.xml") {
unlink ($filename) if ($deleteFilename);
return ($tempdir, "$tempdir/content.xml");
}
}
unlink ($filename) if ($deleteFilename);
return ($tempdir, undef);
}#_openODS
# Check the table and columns corresponds with worksheet
sub _check_validity_worksheet
{
my ($dbh, $table, $nodeFields, $fieldsA, $format) = @_;
my $ret = 0;
eval {
my $query = 'DESCRIBE ' . $table;
my $sth = $dbh->prepare($query);
$sth->execute();
$sth->finish;
$query = 'SHOW COLUMNS FROM ' . $table;
$sth = $dbh->prepare($query);
$sth->execute();
my $fields = {};
while (my $hashRef = $sth->fetchrow_hashref) {
$fields->{$hashRef->{Field}} = $hashRef->{Field};
}
my @fields;
my $fieldsR;
if ($fieldsA) {
$fieldsR = $fieldsA;
} else {
$fieldsR = \@fields;
_getFields($nodeFields, $fieldsR, $format);
}
$ret = 1;
for (@$fieldsR) {
unless (exists($fields->{$_})) {
$ret = 0;
last;
}
}
};
return $ret;
}#_check_validity_worksheet
# Import the data from an excel-xml/ods to mysql tables.
sub _import_table
{
my ($dbh, $table, $frameworkcode, $dom, $PKArray, $format, $frameworktype) = @_;
my %fields2Delete;
my $query;
my @fields;
$frameworktype ||= '';
# Create hash with all elements defined by primary key to know which ones to delete after parsing the spreadsheet
eval {
@fields = @$PKArray;
shift @fields;
$query = 'SELECT ' . join(',', @fields) . ' FROM ' . $table . ' WHERE frameworkcode=?';
if ($frameworktype eq "authority") {
$query = 'SELECT ' . join(',', @fields) . ' FROM ' . $table . ' WHERE authtypecode=?';
}
my $sth = $dbh->prepare($query);
$sth->execute($frameworkcode);
my $field;
while (my $hashRef = $sth->fetchrow_hashref) {
$field = '';
map { $field .= $hashRef->{$_} . '_'; } @fields;
chop $field;
$fields2Delete{$field} = 1;
}
$sth->finish;
};
my $ok = 0;
if ($format eq 'csv') {
my @fieldsName = ();
eval {
my $query = 'SHOW COLUMNS FROM ' . $table;
my $sth = $dbh->prepare($query);
$sth->execute();
while (my $hashRef = $sth->fetchrow_hashref) {
push @fieldsName, $hashRef->{Field};
}
};
$ok = _import_table_csv($dbh, $table, $frameworkcode, $dom, $PKArray, \%fields2Delete, \@fieldsName, $frameworktype);
} elsif ($format eq 'ods') {
$ok = _import_table_ods($dbh, $table, $frameworkcode, $dom, $PKArray, \%fields2Delete, $frameworktype);
} else {
$ok = _import_table_excel($dbh, $table, $frameworkcode, $dom, $PKArray, \%fields2Delete, $frameworktype);
}
if ($ok) {
if (($ok = scalar(keys %fields2Delete)) > 0) {
$query = 'DELETE FROM ' . $table . ' WHERE ';
map {$query .= $_ . '=? AND ';} @$PKArray;
$query = substr($query, 0, -4);
my $sth = $dbh->prepare($query);
for (keys %fields2Delete) {
eval {
$sth->execute(($frameworkcode, split('_', $_)));
};
}
}
} else {
$ok = -1;
}
return $ok;
}#_import_table
# Insert/Update the row from the spreadsheet in the database
sub _processRow_DB
{
my ($dbh, $table, $fields, $dataStr, $updateStr, $dataFields, $dataFieldsHash, $PKArray, $fieldsPK, $fields2Delete) = @_;
my $ok = 0;
my $query;
$query = 'INSERT INTO ' . $table . ' (' . $fields . ') VALUES (' . $dataStr . ') ON DUPLICATE KEY UPDATE ' . $updateStr;
eval {
my $sth = $dbh->prepare($query);
$sth->execute((@$dataFields, @$dataFields));
};
if ($@) {
Koha::Logger->get->warn("Error _processRow_DB $@");
} else {
$ok = 1;
}
if ($ok) {
my $field = '';
map { $field .= $dataFieldsHash->{$_} . '_'; } @$fieldsPK;
chop $field;
delete $fields2Delete->{$field} if (exists($fields2Delete->{$field}));
}
return $ok;
}#_processRow_DB
# Process the rows of a worksheet and insert/update them in a mysql table.
sub _processRows_Table
{
my ($dbh, $frameworkcode, $nodeR, $table, $PKArray, $format, $fields2Delete, $frameworktype) = @_;
my $query;
my @fields = ();
my $fields = '';
my $dataStr = '';
my $updateStr = '';
my $j = 0;
my $ok = 0;
my @fieldsPK = @$PKArray;
shift @fieldsPK;
while ($nodeR) {
if ($nodeR->nodeType == 1 && (($format && $format eq 'ods' && $nodeR->nodeName =~ /(?:table:)?table-row/) || ($nodeR->nodeName =~ /(?:ss:)?Row/)) && $nodeR->hasChildNodes()) {
if ($j == 0) {
# Get name columns
_getFields($nodeR, \@fields, $format);
return 0 unless _check_validity_worksheet($dbh, $table, $nodeR, \@fields, $format);
$fields = join(',', @fields);
$dataStr = '';
map { $dataStr .= '?,';} @fields;
chop($dataStr) if ($dataStr);
$updateStr = '';
map { $updateStr .= $_ . '=?,';} @fields;
chop($updateStr) if ($updateStr);
} else {
# Get data from row
my ($dataFields, $dataFieldsR) = _getDataFields($frameworkcode, $nodeR, \@fields, $format, $frameworktype);
if (scalar(@fields) == scalar(@$dataFieldsR)) {
$ok = _processRow_DB($dbh, $table, $fields, $dataStr, $updateStr, $dataFieldsR, $dataFields, $PKArray, \@fieldsPK, $fields2Delete);
} else {
warn "$j don't match number of fields " . scalar(@fields) . ' vs ' . scalar(@$dataFieldsR) . "($dataStr)";
}
}
$j++;
}
$nodeR = $nodeR->nextSibling;
}
return 1;
}#_processRows_Table
# Import worksheet from the csv file to the mysql table
sub _import_table_csv
{
my ($dbh, $table, $frameworkcode, $dom, $PKArray, $fields2Delete, $fields, $frameworktype) = @_;
my $row = '';
my $partialRow = '';
my $numFields = @$fields;
my $fieldsNameRead = 0;
my @arrData;
my ($fieldsStr, $dataStr, $updateStr, @empty_indexes);
my @fieldsPK = @$PKArray;
shift @fieldsPK;
my $ok = 0;
my $pos = 0;
my $csv = Text::CSV_XS->new( { binary => 1, formula => "empty" } );
while ( my $row = $csv->getline($dom) ) {
my @fields = @$row;
@arrData = @fields;
next if scalar @arrData == grep { $_ eq '' } @arrData; # Emtpy lines
#$arrData[0] = substr($arrData[0], 1) if ($arrData[0] =~ /^"/);
#$arrData[$#arrData] =~ s/[\r\n]+$//;
#chop $arrData[$#arrData] if ($arrData[$#arrData] =~ /"$/);
if (@arrData) {
if ($arrData[0] eq '#-#' && $arrData[$#arrData] eq '#-#') {
# Change of table with separators #-#
return 1;
} elsif ($fieldsNameRead && $arrData[0] eq 'tagfield') {
# Change of table because we begin with field name with former field names read
seek($dom, $pos, 0);
return 1;
}
if (!$fieldsNameRead) {
# New table, we read the field names
$fieldsNameRead = 1;
$fields = [@arrData];
my $non_empty_fields = [ grep { $_ ne '' } @$fields ];
@empty_indexes = indexes { $_ eq '' } @$fields;
$fieldsStr = join(',', @$non_empty_fields);
$dataStr = '';
map { $dataStr .= '?,';} @$non_empty_fields;
chop($dataStr) if ($dataStr);
$updateStr = '';
map { $updateStr .= $_ . '=?,';} @$non_empty_fields;
chop($updateStr) if ($updateStr);
} else {
# Read data