use Koha::Acquisition::Currencies;
use Koha::Biblio::Metadata;
use Koha::Biblio::Metadatas;
+use Koha::Holds;
+use Koha::ItemTypes;
use Koha::SearchEngine;
use Koha::Libraries;
# to get something
push @EXPORT, qw(
- GetBiblio
GetBiblioData
GetMarcBiblio
GetBiblioItemData
GetBiblioItemInfosOf
GetBiblioItemByBiblioNumber
- GetBiblioFromItemNumber
- GetBiblionumberFromItemnumber
&GetRecordValue
- &GetFieldMapping
- &SetFieldMapping
- &DeleteFieldMapping
&GetISBDView
&CountItemsIssued
&CountBiblioInOrders
- &GetSubscriptionsId
);
# To modify something
}
if ( C4::Context->preference("CataloguingLog") ) {
- my $newrecord = GetMarcBiblio($biblionumber);
+ my $newrecord = GetMarcBiblio({ biblionumber => $biblionumber });
logaction( "CATALOGUING", "MODIFY", $biblionumber, "biblio BEFORE=>" . $newrecord->as_formatted );
}
}
# We delete any existing holds
+ my $biblio = Koha::Biblios->find( $biblionumber );
+ my $holds = $biblio->holds;
require C4::Reserves;
- my $reserves = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber });
- foreach my $res ( @$reserves ) {
- C4::Reserves::CancelReserve({ reserve_id => $res->{'reserve_id'} });
+ while ( my $hold = $holds->next ) {
+ C4::Reserves::CancelReserve({ reserve_id => $hold->reserve_id }); # TODO Replace with $hold->cancel
}
# Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
return $error if $error;
}
+
# delete biblio from Koha tables and save in deletedbiblio
# must do this *after* _koha_delete_biblioitems, otherwise
# delete cascade will prevent deletedbiblioitems rows
return \@result;
}
-=head2 SetFieldMapping
-
- SetFieldMapping($framework, $field, $fieldcode, $subfieldcode);
-
-Set a Field to MARC mapping value, if it already exists we don't add a new one.
-
-=cut
-
-sub SetFieldMapping {
- my ( $framework, $field, $fieldcode, $subfieldcode ) = @_;
- my $dbh = C4::Context->dbh;
-
- my $sth = $dbh->prepare('SELECT * FROM fieldmapping WHERE fieldcode = ? AND subfieldcode = ? AND frameworkcode = ? AND field = ?');
- $sth->execute( $fieldcode, $subfieldcode, $framework, $field );
- if ( not $sth->fetchrow_hashref ) {
- my @args;
- $sth = $dbh->prepare('INSERT INTO fieldmapping (fieldcode, subfieldcode, frameworkcode, field) VALUES(?,?,?,?)');
-
- $sth->execute( $fieldcode, $subfieldcode, $framework, $field );
- }
-}
-
-=head2 DeleteFieldMapping
-
- DeleteFieldMapping($id);
-
-Delete a field mapping from an $id.
-
-=cut
-
-sub DeleteFieldMapping {
- my ($id) = @_;
- my $dbh = C4::Context->dbh;
-
- my $sth = $dbh->prepare('DELETE FROM fieldmapping WHERE id = ?');
- $sth->execute($id);
-}
-
-=head2 GetFieldMapping
-
- GetFieldMapping($frameworkcode);
-
-Get all field mappings for a specified frameworkcode
-
-=cut
-
-sub GetFieldMapping {
- my ($framework) = @_;
- my $dbh = C4::Context->dbh;
-
- my $sth = $dbh->prepare('SELECT * FROM fieldmapping where frameworkcode = ?');
- $sth->execute($framework);
-
- my @return;
- while ( my $row = $sth->fetchrow_hashref ) {
- push @return, $row;
- }
- return \@return;
-}
-
=head2 GetBiblioData
$data = &GetBiblioData($biblionumber);
return @results;
}
-=head2 GetBiblionumberFromItemnumber
-
-
-=cut
-
-sub GetBiblionumberFromItemnumber {
- my ($itemnumber) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare("Select biblionumber FROM items WHERE itemnumber = ?");
-
- $sth->execute($itemnumber);
- my ($result) = $sth->fetchrow;
- return ($result);
-}
-
-=head2 GetBiblioFromItemNumber
-
- $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
-
-Looks up the item with the given itemnumber. if undef, try the barcode.
-
-C<&itemnodata> returns a reference-to-hash whose keys are the fields
-from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
-database.
-
-=cut
-
-#'
-sub GetBiblioFromItemNumber {
- my ( $itemnumber, $barcode ) = @_;
- my $dbh = C4::Context->dbh;
- my $sth;
- if ($itemnumber) {
- $sth = $dbh->prepare(
- "SELECT * FROM items
- LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
- LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
- WHERE items.itemnumber = ?"
- );
- $sth->execute($itemnumber);
- } else {
- $sth = $dbh->prepare(
- "SELECT * FROM items
- LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
- LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
- WHERE items.barcode = ?"
- );
- $sth->execute($barcode);
- }
- my $data = $sth->fetchrow_hashref;
- $sth->finish;
- return ($data);
-}
-
=head2 GetISBDView
$isbd = &GetISBDView({
return $res;
}
-=head2 GetBiblio
-
- my $biblio = &GetBiblio($biblionumber);
-
-=cut
-
-sub GetBiblio {
- my ($biblionumber) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
- my $count = 0;
- my @results;
- $sth->execute($biblionumber);
- if ( my $data = $sth->fetchrow_hashref ) {
- return $data;
- }
- return;
-} # sub GetBiblio
-
=head2 GetBiblioItemInfosOf
GetBiblioItemInfosOf(@biblioitemnumbers);
=head2 GetMarcBiblio
- my $record = GetMarcBiblio($biblionumber, [$embeditems], [$opac]);
+ my $record = GetMarcBiblio({
+ biblionumber => $biblionumber,
+ embed_items => $embeditems,
+ opac => $opac });
Returns MARC::Record representing a biblio record, or C<undef> if the
biblionumber doesn't exist.
+Both embed_items and opac are optional.
+If embed_items is passed and is 1, items are embedded.
+If opac is passed and is 1, the record is filtered as needed.
+
=over 4
=item C<$biblionumber>
=cut
sub GetMarcBiblio {
- my $biblionumber = shift;
- my $embeditems = shift || 0;
- my $opac = shift || 0;
+ my ($params) = @_;
+
+ if (not defined $params) {
+ carp 'GetMarcBiblio called without parameters';
+ return;
+ }
+
+ my $biblionumber = $params->{biblionumber};
+ my $embeditems = $params->{embed_items} || 0;
+ my $opac = $params->{opac} || 0;
if (not defined $biblionumber) {
carp 'GetMarcBiblio called with undefined biblionumber';
=head2 MungeMarcPrice
Return the best guess at what the actual price is from a price field.
+
=cut
sub MungeMarcPrice {
#---- itemtypes
if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
- return getitemtypeinfo($value)->{translated_description};
+ my $itemtype = Koha::ItemTypes->find( $value );
+ return $itemtype ? $itemtype->translated_description : q||;
}
#---- "true" authorized value
}
my @urls = $field->subfield('u');
foreach my $url (@urls) {
+ $url =~ s/^\s+|\s+$//g; # trim
my $marcurl;
if ( $marcflavour eq 'MARC21' ) {
my $s3 = $field->subfield('3');
$marcflavour ||="MARC21";
require C4::Items;
- my $hostrecord = GetMarcBiblio($hostbiblionumber);
+ my $hostrecord = GetMarcBiblio({ biblionumber => $hostbiblionumber });
my $item = C4::Items::GetItem($hostitemnumber);
my $hostmarcfield;
);
if ( $op eq 'specialUpdate' ) {
unless ($record) {
- $record = GetMarcBiblio($biblionumber, 1);
+ $record = GetMarcBiblio({
+ biblionumber => $biblionumber,
+ embed_items => 1 });
}
my $records = [$record];
$indexer->update_index_background( [$biblionumber], [$record] );
my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
$sth->execute($biblionumber);
+ # FIXME There is a transaction in _koha_delete_biblio_metadata
+ # But actually all the following should be done inside a single transaction
if ( my $data = $sth->fetchrow_hashref ) {
# save the record in deletedbiblio
$bkup_sth->execute(@bind);
$bkup_sth->finish;
+ _koha_delete_biblio_metadata( $biblionumber );
+
# delete the biblio
my $sth2 = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
$sth2->execute($biblionumber);
return;
}
+=head2 _koha_delete_biblio_metadata
+
+ $error = _koha_delete_biblio_metadata($biblionumber);
+
+C<$biblionumber> - the biblionumber of the biblio metadata to be deleted
+
+=cut
+
+sub _koha_delete_biblio_metadata {
+ my ($biblionumber) = @_;
+
+ my $dbh = C4::Context->dbh;
+ my $schema = Koha::Database->new->schema;
+ $schema->txn_do(
+ sub {
+ $dbh->do( q|
+ INSERT INTO deletedbiblio_metadata (biblionumber, format, marcflavour, metadata)
+ SELECT biblionumber, format, marcflavour, metadata FROM biblio_metadata WHERE biblionumber=?
+ |, undef, $biblionumber );
+ $dbh->do( q|DELETE FROM biblio_metadata WHERE biblionumber=?|,
+ undef, $biblionumber );
+ }
+ );
+}
+
=head1 UNEXPORTED FUNCTIONS
=head2 ModBiblioMarc
return ($count);
}
-=head2 GetSubscriptionsId
-
- $subscriptions = &GetSubscriptionsId($biblionumber);
-
-This function return an array of subscriptionid with $biblionumber
-
-=cut
-
-sub GetSubscriptionsId {
- my ($biblionumber) = @_;
- my $dbh = C4::Context->dbh;
- my $query = "SELECT subscriptionid
- FROM subscription
- WHERE biblionumber=?";
- my $sth = $dbh->prepare($query);
- $sth->execute($biblionumber);
- my @subscriptions = $sth->fetchrow_array;
- return (@subscriptions);
-}
-
=head2 prepare_host_field
$marcfield = prepare_host_field( $hostbiblioitem, $marcflavour );
sub prepare_host_field {
my ( $hostbiblio, $marcflavour ) = @_;
$marcflavour ||= C4::Context->preference('marcflavour');
- my $host = GetMarcBiblio($hostbiblio);
+ my $host = GetMarcBiblio({ biblionumber => $hostbiblio });
# unfortunately as_string does not 'do the right thing'
# if field returns undef
my %sfd;
my ($biblionumber, $increase, $value) = @_;
my $totalissues;
- my $record = GetMarcBiblio($biblionumber);
+ my $record = GetMarcBiblio({ biblionumber => $biblionumber });
unless ($record) {
carp "UpdateTotalIssues could not get biblio record";
return;
}
- my $data = GetBiblioData($biblionumber);
- unless ($data) {
+ my $biblio = Koha::Biblios->find( $biblionumber );
+ unless ($biblio) {
carp "UpdateTotalIssues could not get datas of biblio";
return;
}
- my ($totalissuestag, $totalissuessubfield) = GetMarcFromKohaField('biblioitems.totalissues', $data->{'frameworkcode'});
+ my $biblioitem = $biblio->biblioitem;
+ my ($totalissuestag, $totalissuessubfield) = GetMarcFromKohaField('biblioitems.totalissues', $biblio->frameworkcode);
unless ($totalissuestag) {
return 1; # There is nothing to do
}
if (defined $value) {
$totalissues = $value;
} else {
- $totalissues = $data->{'totalissues'} + $increase;
+ $totalissues = $biblioitem->totalissues + $increase;
}
my $field = $record->field($totalissuestag);
$record->insert_grouped_field($field);
}
- return ModBiblio($record, $biblionumber, $data->{'frameworkcode'});
+ return ModBiblio($record, $biblionumber, $biblio->frameworkcode);
}
=head2 RemoveAllNsb