This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollection.pm
1763 lines (1214 loc) · 47.5 KB
/
Collection.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 Collection;
=head1 NAME
Collection
=head1 DESCRIPTION
This class encapsulates database operations of several types
1. Modify collection metadata
2. Create new collection (check CollectionSet add new coll vs Collection responsiblities)
3. adds or delete items from collections
4. read item data for one item?
5. provide sorted lists of item data.
Should this class only deal with managing lists of items?
Should modification of collection metadata and creation of new
collection be moved to CollectionSet class?
Modification of collection metadata (this corresponds to editcoll action)
change status (public/private)
change description
change collection name
Tagging and annotation of items may go to a separate class in next phase
Tagging operations on a list of items goes where?
=head1 SYNOPSIS
my $co = new Collection;
my $item_list_ref = $co->list_items($coll_id);
my $item_list = $co->list_items( [sortkey],[start_rec_num, num_records_per_page]);
foreach my $item (@{$item_list})
{
my $title = $item_list->{'Display_Title'};
my $author = $item_list->{ 'Display_Author'}
}
record number counting starts at 1
$item_list is reference to array of hashes where keys are database
field names and values are the values for that row
The following operations assume $user_id is available in
$self->get_user_id;
Actions on groups of items in collection is Collection responsible for
making sure user owns collection?
$co->copy_items($coll_id,$item_array_ref);
$co->delete_items($coll_id,$item_array_ref);
Actions on collections
my $coll_id = $co->get_coll_id_from_coll_name($coll_id);
$co->edit_description($coll_id, $description);
$co->edit_collection_name($coll_id, $new_coll_name);
$co->change_status($coll_id, [public|private]);
=head1 TODO:
=over 4
=item need to index sort fields in MySQL to make limit work efficiently!
=item see notes for CollectionSet
=back
=head1 METHODS
=cut
my $DEBUG = undef;
#$DEBUG = "true";
BEGIN
{
# enable strict under development
if ( $ENV{'HT_DEV'} )
{
require "strict.pm";
strict::import();
}
}
use CollectionUser;
# Perl MBooks modules
use Utils;
use DbUtils;
use CollectionSet;
use Debug::DUtils;
sub new
{
my $class = shift;
my $self = {};
bless $self, $class;
$self->_initialize( @_ );
return $self;
}
# ---------------------------------------------------------------------
=item _initialize
Description
=cut
# ---------------------------------------------------------------------
sub _initialize {
my $self = shift;
$self->{'dbh'} = shift;
my $config = shift;
$self->{'user_id'} = CollectionUser->new(shift);
$self->{'config'} = $config;
my $use_test_tables = DEBUG('usetesttbl') || $config->get('use_test_tables');
if ($use_test_tables) {
$self->{'coll_table_name'} = $config->get('test_coll_table_name');
$self->{'coll_item_table_name'} = $config->get('test_coll_item_table_name');
$self->{'item_table_name'} = $config->get('test_item_table_name');
}
else {
$self->{'coll_table_name'} = $config->get('coll_table_name');
$self->{'coll_item_table_name'} = $config->get('coll_item_table_name');
$self->{'item_table_name'} = $config->get('item_table_name');
}
my @item_display_fields = $config->get('item_table_display_field_names');
$self->{'item_display_fields_arr_ref'} = \@item_display_fields;
my @sort_fields = $config->get('item_table_sort_field_names');
$self->{'item_sort_fields_arr_ref'} = \@sort_fields;
}
# ---------------------------------------------------------------------
=item get_config
Description
=cut
# ---------------------------------------------------------------------
sub get_config
{
my $self = shift;
return $self->{'config'}
}
# ---------------------------------------------------------------------
=item get_dbh
Description
=cut
# ---------------------------------------------------------------------
sub get_dbh
{
my $self = shift;
return $self->{'dbh'};
}
# ---------------------------------------------------------------------
=item get_user_id
Description
=cut
# ---------------------------------------------------------------------
sub get_user_id
{
my $self = shift;
return $self->{'user_id'};
}
sub get_user
{
my $self = shift;
return $self->{'user_id'};
}
# ---------------------------------------------------------------------
=item get_coll_table_name
Description
=cut
# ---------------------------------------------------------------------
sub get_coll_table_name
{
my $self = shift;
return $self->{coll_table_name};
}
# ---------------------------------------------------------------------
=item get_coll_item_table_name
Description
=cut
# ---------------------------------------------------------------------
sub get_coll_item_table_name
{
my $self = shift;
return $self->{coll_item_table_name};
}
# ---------------------------------------------------------------------
=item get_item_table_name
Description
=cut
# ---------------------------------------------------------------------
sub get_item_table_name
{
my $self = shift;
return $self->{item_table_name};
}
# ---------------------------------------------------------------------
=item get_item_display_fields_arr
Description
=cut
# ---------------------------------------------------------------------
sub get_item_display_fields_arr
{
my $self = shift;
return @{$self->{'item_display_fields_arr_ref'}};
}
# ---------------------------------------------------------------------
=item get_item_sort_fields_arr_ref
Description
=cut
# ---------------------------------------------------------------------
sub get_item_sort_fields_arr_ref
{
my $self = shift;
return $self->{'item_sort_fields_arr_ref'};
}
# ---------------------------------------------------------------------
=item coll_owned_by_user
Description
=cut
# ---------------------------------------------------------------------
sub coll_owned_by_user
{
my $self = shift;
my $coll_id = shift;
my $user = CollectionUser->new(shift);
my ( $owner_names, $owner_expr ) = CollectionSet->_get_owner_expr($user);
# Utils::trim_spaces(\$username);
my $dbh = $self->get_dbh();
my $coll_table_name = $self->get_coll_table_name;
my $statement = qq{SELECT owner FROM $coll_table_name WHERE MColl_id = ? AND owner IN ( $owner_expr )};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $coll_id, @$owner_names);
my @ary = $sth->fetchrow_array;
return scalar @ary == 1;
# my $owner = $ary[0];
# DEBUG('dbcoll', qq{username = $username collection $coll_id owned by $owner"});
# # When owner is an email address, compare case in-sensitively to avoid stuff like
# # Mary.Smith@some.edu vs. Mary.smith@some.edu (both legit)
# my ($test_username, $test_owner) = ($username, $owner);
# if ($test_owner =~ m,@,) {
# ($test_username, $test_owner) = (lc($username), lc($owner));
# }
# return ($test_username eq $test_owner);
}
# ---------------------------------------------------------------------
=item get_coll_owner_display_name
Returns owner_name (as opposed ot owner which is the unique persistent
id for the user) for a given coll_id
=cut
# ---------------------------------------------------------------------
sub get_coll_owner_display_name
{
my $self = shift;
my $coll_id = shift;
my $dbh = $self->get_dbh();
my $coll_table_name = $self->get_coll_table_name;
my $statement = qq{SELECT owner_name FROM $coll_table_name WHERE MColl_id=?};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $coll_id);
my @ary = $sth->fetchrow_array;
my $owner_display_name = $ary[0];
return $owner_display_name;
}
# ---------------------------------------------------------------------
=item get_coll_owner
Returns owner (as opposed to owner_display_name) which is the unique persistent
id for the user) for a given coll_id
=cut
# ---------------------------------------------------------------------
sub get_coll_owner {
my $self = shift;
my $coll_id = shift;
my $dbh = $self->get_dbh();
my $coll_table_name = $self->get_coll_table_name;
my $statement = qq{SELECT owner FROM $coll_table_name WHERE MColl_id=?};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $coll_id);
my @ary = $sth->fetchrow_array;
my $owner = $ary[0];
return $owner;
}
# ---------------------------------------------------------------------
=item create_or_update_item_metadata
This adds an item to the item metadata table. Caller is responsible
for also adding it to a collection
These need to be worked out assuming we separate extern_id from
item_id
=cut
# ---------------------------------------------------------------------
sub create_or_update_item_metadata {
my $self = shift;
my $metadata_ref = shift;
my $dbh = $self->get_dbh;
my $item_table_name = $self->get_item_table_name;
my $id = $metadata_ref->{'extern_item_id'};
DbUtils::begin_work($dbh);
eval {
if ($self->item_exists($id)) {
# item already in table so just update the metadata
DbUtils::update_row_by_key($dbh, $item_table_name, $metadata_ref, 'extern_item_id', $id);
}
else {
# item not in item_metadata table so create new item and
# return extern_item_id do sql insert. Generate a new unique item_id.
### DbUtils::insert_new_row($dbh, $item_table_name, $metadata_ref);
DbUtils::insert_or_update_row($dbh, $item_table_name, $metadata_ref, 'extern_item_id');
}
DbUtils::commit($dbh);
};
if ( my $err = $@ ) {
eval { $dbh->rollback; };
ASSERT(0, qq{Problem with create_or_update_item_metadata: $err});
}
return $id;
}
# ---------------------------------------------------------------------
=item _field_is_valid
Description
=cut
# ---------------------------------------------------------------------
sub _field_is_valid
{
my $self = shift;
my $fieldname = shift;
my $value = shift;
my $item_table_name = $self->get_item_table_name;
my $dbh = $self->get_dbh;
}
# ---------------------------------------------------------------------
=item copy_items
copy_items($coll_id,\@ids)
This only adds existing items to an existing collection
=cut
# ---------------------------------------------------------------------
sub copy_items {
my $self = shift;
my $coll_id = shift;
my $id_arr_ref = shift;
my $force_ownership = shift;
my $dbh = $self->get_dbh;
my $coll_item_table_name = $self->get_coll_item_table_name;
my $row_array_ref = [];
my $col_names_array_ref = ['extern_item_id','MColl_ID'];
# my $user_id = $self->get_user_id;
my $user = $self->get_user;
unless ($force_ownership) {
ASSERT($self->coll_owned_by_user($coll_id, $user),
qq{Collection $coll_id not owned by user $user});
}
foreach my $id (@$id_arr_ref) {
if ($self->item_exists($id)) {
push (@$row_array_ref, [$id, $coll_id]);
}
else {
ASSERT (0, qq{item id $id does not exist in item table});
}
}
DbUtils::begin_work($dbh);
eval {
# Add the items
DbUtils::insert_one_or_more_rows($dbh, $coll_item_table_name, $col_names_array_ref, $row_array_ref);
# Add count to collections table
$self->update_item_count($coll_id);
DbUtils::commit($dbh);
};
if ( my $err = $@ ) {
eval { $dbh->rollback; };
ASSERT(0, qq{Problem with copy_items: $err});
}
}
# ---------------------------------------------------------------------
=item delete_items
delete_items($coll_id,\@ids)
Only removes the relationship between collection and items does not
affect item metadata checks that user is owner of collection
=cut
# ---------------------------------------------------------------------
sub delete_items {
my $self = shift;
my $coll_id = shift;
my $id_arr_ref = shift;
my $force_ownership = shift;
my $dbh = $self->get_dbh();
my $coll_item_table_name = $self->get_coll_item_table_name;
unless ($force_ownership) {
my $user = $self->get_user;
ASSERT($self->coll_owned_by_user($coll_id, $user),
qq{Can not delete items: Collection $coll_id not owned by user $user});
}
my $id_string = $self->arr_ref2SQL_in_string($id_arr_ref);
DbUtils::begin_work($dbh);
eval {
my $statement = qq{DELETE FROM $coll_item_table_name WHERE extern_item_id IN $id_string AND MColl_ID=?};
DbUtils::prep_n_execute ($dbh, $statement, @$id_arr_ref, $coll_id);
# update item count int collection table!
$self->update_item_count($coll_id);
DbUtils::commit($dbh);
};
if ( my $err = $@ ) {
eval { $dbh->rollback; };
ASSERT(0, qq{Problem with delete_items: $err});
}
}
#----------------------------------------------------------------------
=item delete_coll(coll_id)
Description
=cut
#----------------------------------------------------------------------
sub delete_coll {
my $self = shift;
my $coll_id = shift;
my $force_ownership = shift;
my $dbh = $self->get_dbh();
my $coll_table_name = $self->get_coll_table_name;
my $coll_item_table_name = $self->get_coll_item_table_name;
my $user = $self->get_user;
unless ($force_ownership) {
ASSERT($self->coll_owned_by_user($coll_id, $user),
qq{Collection $coll_id not owned by user $user});
}
DbUtils::begin_work($dbh);
eval {
DbUtils::del_row_by_key($dbh, $coll_table_name, 'MColl_ID', $coll_id);
# DbUtils doesn't return a status so should we write our own?
# return $status;
DbUtils::del_one_or_more_rows_by_key($dbh, $coll_item_table_name, 'MColl_ID', $coll_id);
DbUtils::commit($dbh);
};
if ( my $err = $@ ) {
eval { $dbh->rollback; };
ASSERT(0, qq{Problem with delete_coll: $err});
}
}
#----------------------------------------------------------------------
=item list_items
record number counting starts at 1.
returns reference to array of hashrefs where keys are database field
names and values are the values for that row
if optional slice arguments (start_rec_num, num_records_per_slice) are
supplied it only returns the first num_records_per_slice starting with
start_rec_num. Otherwise it returns all matching rows.
=cut
#----------------------------------------------------------------------
sub list_items {
my $self = shift;
my ($coll_id, $sort_key, $direction, $slice_start, $recs_per_slice, $rights_ref, $id_arr_ref) = @_;
ASSERT($sort_key, qq{no sort key supplied});
ASSERT($direction, qq{no direction supplied});
my $item_table = $self->get_item_table_name;
my $coll_table = $self->get_coll_table_name;
my $coll_item_table = $self->get_coll_item_table_name;
my @metadata_fields = ( $self->get_item_display_fields_arr, (qw/rights extern_item_id/) );
my $item_sort_fields_arr_ref = $self->get_item_sort_fields_arr_ref;
# undefined $slice_start and for $recs_per_slice implies no LIMIT
# clause below
DEBUG('dbcoll', qq{slice start is $slice_start at $recs_per_slice records per slice});
my $sort_key_in_sort_fields = grep(/$sort_key/, @$item_sort_fields_arr_ref);
ASSERT($sort_key_in_sort_fields, qq{Collection::list_items $sort_key not in item_sort_fields});
# SELECT FROM
@metadata_fields = map { "a." . $_ } @metadata_fields;
my $fields = join(",", @metadata_fields);
my $SELECT = qq{SELECT $item_table.extern_item_id FROM $item_table, $coll_item_table };
# WHERE
my $WHERE = qq{WHERE $item_table.extern_item_id=$coll_item_table.extern_item_id AND $coll_item_table.MColl_ID=?};
if (defined $id_arr_ref) {
my $IN_clause = $self->arr_ref2SQL_in_string($id_arr_ref);
$WHERE .= qq{ AND $item_table.extern_item_id IN $IN_clause };
}
# AND IN $rights_ref
if (defined $rights_ref->[0]) {
my $IN_clause = $self->arr_ref2SQL_in_string([ sort { $a <=> $b } @$rights_ref ]);
$WHERE .= qq{ AND $item_table.rights IN $IN_clause };
}
# ORDER BY
$direction = ($direction eq 'a') ? 'ASC' : 'DESC';
$WHERE .= qq{ ORDER BY $sort_key $direction};
# LIMIT
my $LIMIT = '';
my $offset = $slice_start - 1; # MySQL limit counts records from 0
if ($offset >= 0) {
$LIMIT = qq{ LIMIT $offset, $recs_per_slice};
}
# # NOTE: this odd construct is for efficiency
#### add time() to bust MySQL cache
#### $fields = time() . ", $fields";
my $statement = qq{SELECT $fields FROM ( $SELECT $WHERE $LIMIT ) o JOIN $item_table a ON a.extern_item_id = o.extern_item_id ORDER BY $sort_key $direction};
DEBUG('dbcoll', qq{list_items sql=$statement});
my $dbh = $self->get_dbh();
my $sth = DbUtils::prep_n_execute($dbh, $statement, $coll_id, @$id_arr_ref, @$rights_ref);
my $arr_ref = $sth->fetchall_arrayref({});
return $arr_ref;
}
# ---------------------------------------------------------------------
=item arr_ref2SQL_in_string
Description
=cut
# ---------------------------------------------------------------------
sub arr_ref2SQL_in_string {
my $self = shift;
my $id_arr_ref = shift;
my $s = '(' . join(',', map {'?'} @$id_arr_ref) . ')';
return $s;
}
#======================================================================
#
# Methods for acting on collection metadata rather than lists of items
#
# Should these be moved to a true collection object and the list mgt
# functions be in a listMgr object?
# ---------------------------------------------------------------------
=item get_coll_record
Description
Fetches the row for $coll_id and caches the result.
=cut
# ---------------------------------------------------------------------
sub get_coll_record {
my $self = shift;
my $coll_id = shift;
unless ( defined $self->{_collection_collid_record}->{$coll_id} ) {
my $dbh = $self->get_dbh();
my $coll_table_name = $self->get_coll_table_name;
my $statement = qq{SELECT * from $coll_table_name WHERE MColl_ID=?};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $coll_id);
$self->{_collection_collid_record}->{$coll_id} = $sth->fetchrow_hashref;
}
return $self->{_collection_collid_record}->{$coll_id} || {};
}
# ----------------------------------------------------------------------
# shared_status is 1 if public 0 if private
# ---------------------------------------------------------------------
=item get_shared_status
Description
=cut
# ---------------------------------------------------------------------
sub get_shared_status {
my $self = shift;
my $coll_id = shift;
my $status_string = "";
my $status = $self->get_coll_record($coll_id)->{shared};
# instead of returning 1 or 0 return strings
if ($status == 0) {
$status_string = 'private';
}
elsif ($status == 1) {
$status_string = 'public'
}
elsif ($status == -1) {
$status_string = 'draft';
}
else {
ASSERT(0, qq{get_shared_status returned $status. It should be one or zero});
}
return $status_string;
}
# ---------------------------------------------------------------------
=item get_description
Description
=cut
# ---------------------------------------------------------------------
sub get_description {
my $self = shift;
my $coll_id = shift;
return $self->get_coll_record($coll_id)->{description};
}
sub get_contributor_name {
my $self = shift;
my $coll_id = shift;
return $self->get_coll_record($coll_id)->{contributor_name};
}
# ---------------------------------------------------------------------
=item get_coll_name
Description
=cut
# ---------------------------------------------------------------------
sub get_coll_name {
my $self = shift;
my $coll_id = shift;
return $self->get_coll_record($coll_id)->{collname};
}
sub get_coll_featured {
my $self = shift;
my $coll_id = shift;
return $self->get_coll_record($coll_id)->{featured};
}
sub get_coll_branding {
my $self = shift;
my $coll_id = shift;
return $self->get_coll_record($coll_id)->{branding};
}
sub get_coll_contact_info {
my $self = shift;
my $coll_id = shift;
return $self->get_coll_record($coll_id)->{contact_info};
}
sub get_coll_contact_link {
my $self = shift;
my $coll_id = shift;
return $self->get_coll_record($coll_id)->{contact_link};
}
# ---------------------------------------------------------------------
=item _edit_metadata
Description
=cut
# ---------------------------------------------------------------------
sub _edit_metadata {
my $self = shift;
my $coll_id = shift;
my $field = shift;
my $value = shift;
my $max_length = shift;
my $user = $self->get_user;
my $dbh = $self->get_dbh();
my $coll_table_name = $self->get_coll_table_name;
my $coll_name = $self->get_coll_name($coll_id);
ASSERT($self->coll_owned_by_user($coll_id, $user),
qq{Can not edit this collection: Collection $coll_name id = $coll_id not owned by user $user});
if (defined($max_length)) {
if (length($value) > $max_length) {
$value = substr($value, 0, $max_length);
}
}
if ( $value ) {
# sanitize the input
require XML::LibXML;
require HTML::Entities;
my $html_parser = XML::LibXML->new();
eval {
$value = HTML::Entities::decode_entities($value);
my $html_dom = $html_parser->parse_html_string("<div>$value</div>", { recover => 2 });
$value = HTML::Entities::encode_entities($html_dom->textContent, '<>&"');
};
if ( my $err = $@ ) {
ASSERT($err, qq{Value could be not sanitized $coll_id : $value : $user});
}
}
DbUtils::begin_work($dbh);
eval {
# Add the items
my $statement = qq{UPDATE $coll_table_name SET $field=? WHERE MColl_ID=?};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $value, $coll_id);
# clear the cache
delete $self->{_collection_collid_record}->{$coll_id};
DbUtils::commit($dbh);
};
if ( my $err = $@ ) {
eval { $dbh->rollback; };
ASSERT(0, qq{Problem with _edit_metadata : $field : $err});
}
}
#----------------------------------------------------------------------
=item edit_status
saves public/private shared status to database
$co->edit_status($coll_id,$status)
$status is string "private" or "public"
=cut
#----------------------------------------------------------------------
sub edit_status {
my $self = shift;
my $coll_id = shift;
my $status = shift;
# default of 1 is public
my $value = 1;
ASSERT(scalar($status =~ m,^(public|private|draft)$,),
qq{argument to edit_status must be either 'public' or 'private'});
if ($status =~ /^private$/i) {
$value = 0;
} elsif ($status =~ /^draft$/i) {
$value = -1;
}
$self->_edit_metadata($coll_id, 'shared', $value);
}
#----------------------------------------------------------------------
=item edit_description
$co->edit_description($coll_id, $desc)
Saves description to datbase client is responsible for making sure
$desc is less than 150 characters.
=cut
#----------------------------------------------------------------------
sub edit_description {
my $self = shift;
my $coll_id = shift;
my $description = shift;
$self-> _edit_metadata($coll_id, 'description', $description, 255);
}
sub edit_contributor_name {
my $self = shift;
my $coll_id = shift;
my $contributor_name = shift;
$self->_edit_metadata( $coll_id, 'contributor_name', $contributor_name,
255 );
}
sub edit_contact_info {
my $self = shift;
my $coll_id = shift;
my $contact_info = shift;
$self-> _edit_metadata($coll_id, 'contact_info', $contact_info, 255);
}
#----------------------------------------------------------------------
=item edit_coll_name
$co->edit_coll_name($coll_id, $name)
Replaces existing coll_name with $name client is responsible for
making sure coll_name is unique for this user
=cut
#----------------------------------------------------------------------
sub edit_coll_name {
my $self = shift;
my $coll_id = shift;
my $coll_name = shift;
$self->_edit_metadata($coll_id, 'collname', $coll_name, 100);
}
sub transfer_collection {
my $self = shift;
my $coll_id = shift;
my $current_owner = shift;
my $owner = shift;
my $owner_name = shift;
my $dbh = $self->get_dbh();
my $coll_table_name = $self->get_coll_table_name;
my $current_expr = join(',', map { '?' } @$current_owner);
my $statement = <<SQL;
UPDATE $coll_table_name
SET owner = ?, owner_name = ?
WHERE MColl_ID = ? AND owner IN ( $current_expr )
SQL
DbUtils::begin_work($dbh);