View | Details | Raw Unified | Return to bug 6679
Collapse All | Expand All

(-)a/C4/Tags.pm (-24 / +23 lines)
Lines 113-125 sub get_count_by_tag_status { Link Here
113
}
113
}
114
114
115
sub remove_tag {
115
sub remove_tag {
116
	my $tag_id  = shift or return undef;
116
	my $tag_id  = shift or return;
117
	my $user_id = (@_) ? shift : undef;
117
	my $user_id = (@_) ? shift : undef;
118
	my $rows = (defined $user_id) ?
118
	my $rows = (defined $user_id) ?
119
			get_tag_rows({tag_id=>$tag_id, borrowernumber=>$user_id}) :
119
			get_tag_rows({tag_id=>$tag_id, borrowernumber=>$user_id}) :
120
			get_tag_rows({tag_id=>$tag_id}) ;
120
			get_tag_rows({tag_id=>$tag_id}) ;
121
	$rows or return 0;
121
	$rows or return 0;
122
	(scalar(@$rows) == 1) or return undef;	# should never happen (duplicate ids)
122
	(scalar(@$rows) == 1) or return;	# should never happen (duplicate ids)
123
	my $row = shift(@$rows);
123
	my $row = shift(@$rows);
124
	($tag_id == $row->{tag_id}) or return 0;
124
	($tag_id == $row->{tag_id}) or return 0;
125
	my $tags = get_tags({term=>$row->{term}, biblionumber=>$row->{biblionumber}});
125
	my $tags = get_tags({term=>$row->{term}, biblionumber=>$row->{biblionumber}});
Lines 141-165 sub remove_tag { Link Here
141
}
141
}
142
142
143
sub delete_tag_index {
143
sub delete_tag_index {
144
	(@_) or return undef;
144
	(@_) or return;
145
	my $sth = C4::Context->dbh->prepare("DELETE FROM tags_index WHERE term = ? AND biblionumber = ? LIMIT 1");
145
	my $sth = C4::Context->dbh->prepare("DELETE FROM tags_index WHERE term = ? AND biblionumber = ? LIMIT 1");
146
	$sth->execute(@_);
146
	$sth->execute(@_);
147
	return $sth->rows || 0;
147
	return $sth->rows || 0;
148
}
148
}
149
sub delete_tag_approval {
149
sub delete_tag_approval {
150
	(@_) or return undef;
150
	(@_) or return;
151
	my $sth = C4::Context->dbh->prepare("DELETE FROM tags_approval WHERE term = ? LIMIT 1");
151
	my $sth = C4::Context->dbh->prepare("DELETE FROM tags_approval WHERE term = ? LIMIT 1");
152
	$sth->execute(shift);
152
	$sth->execute(shift);
153
	return $sth->rows || 0;
153
	return $sth->rows || 0;
154
}
154
}
155
sub delete_tag_row_by_id {
155
sub delete_tag_row_by_id {
156
	(@_) or return undef;
156
	(@_) or return;
157
	my $sth = C4::Context->dbh->prepare("DELETE FROM tags_all WHERE tag_id = ? LIMIT 1");
157
	my $sth = C4::Context->dbh->prepare("DELETE FROM tags_all WHERE tag_id = ? LIMIT 1");
158
	$sth->execute(shift);
158
	$sth->execute(shift);
159
	return $sth->rows || 0;
159
	return $sth->rows || 0;
160
}
160
}
161
sub delete_tag_rows_by_ids {
161
sub delete_tag_rows_by_ids {
162
	(@_) or return undef;
162
	(@_) or return;
163
	my $i=0;
163
	my $i=0;
164
	foreach(@_) {
164
	foreach(@_) {
165
		$i += delete_tag_row_by_id($_);
165
		$i += delete_tag_row_by_id($_);
Lines 355-361 sub get_approval_rows { # i.e., from tags_approval Link Here
355
}
355
}
356
356
357
sub is_approved {
357
sub is_approved {
358
	my $term = shift or return undef;
358
	my $term = shift or return;
359
	my $sth = C4::Context->dbh->prepare("SELECT approved FROM tags_approval WHERE term = ?");
359
	my $sth = C4::Context->dbh->prepare("SELECT approved FROM tags_approval WHERE term = ?");
360
	$sth->execute($term);
360
	$sth->execute($term);
361
	unless ($sth->rows) {
361
	unless ($sth->rows) {
Lines 366-372 sub is_approved { Link Here
366
}
366
}
367
367
368
sub get_tag_index {
368
sub get_tag_index {
369
	my $term = shift or return undef;
369
	my $term = shift or return;
370
	my $sth;
370
	my $sth;
371
	if (@_) {
371
	if (@_) {
372
		$sth = C4::Context->dbh->prepare("SELECT * FROM tags_index WHERE term = ? AND biblionumber = ?");
372
		$sth = C4::Context->dbh->prepare("SELECT * FROM tags_index WHERE term = ? AND biblionumber = ?");
Lines 380-386 sub get_tag_index { Link Here
380
380
381
sub whitelist {
381
sub whitelist {
382
	my $operator = shift;
382
	my $operator = shift;
383
	defined $operator or return undef; # have to test defined to allow =0 (kohaadmin)
383
	defined $operator or return; # have to test defined to allow =0 (kohaadmin)
384
	if ($ext_dict) {
384
	if ($ext_dict) {
385
		foreach (@_) {
385
		foreach (@_) {
386
			spellcheck($_) or next;
386
			spellcheck($_) or next;
Lines 402-408 sub whitelist { Link Here
402
# a term mistakenly, you can still reverse it. But there is no going back to "neutral".
402
# a term mistakenly, you can still reverse it. But there is no going back to "neutral".
403
sub blacklist {
403
sub blacklist {
404
	my $operator = shift;
404
	my $operator = shift;
405
	defined $operator or return undef; # have to test defined to allow =0 (kohaadmin)
405
	defined $operator or return; # have to test defined to allow =0 (kohaadmin)
406
	foreach (@_) {
406
	foreach (@_) {
407
		my $aref = get_approval_rows({term=>$_});
407
		my $aref = get_approval_rows({term=>$_});
408
		if ($aref and scalar @$aref) {
408
		if ($aref and scalar @$aref) {
Lines 415-428 sub blacklist { Link Here
415
}
415
}
416
sub add_filter {
416
sub add_filter {
417
	my $operator = shift;
417
	my $operator = shift;
418
	defined $operator or return undef; # have to test defined to allow =0 (kohaadmin)
418
	defined $operator or return; # have to test defined to allow =0 (kohaadmin)
419
	my $query = "INSERT INTO tags_blacklist (regexp,y,z) VALUES (?,?,?)";
419
	my $query = "INSERT INTO tags_blacklist (regexp,y,z) VALUES (?,?,?)";
420
	# my $sth = C4::Context->dbh->prepare($query);
420
	# my $sth = C4::Context->dbh->prepare($query);
421
	return scalar @_;
421
	return scalar @_;
422
}
422
}
423
sub remove_filter {
423
sub remove_filter {
424
	my $operator = shift;
424
	my $operator = shift;
425
	defined $operator or return undef; # have to test defined to allow =0 (kohaadmin)
425
	defined $operator or return; # have to test defined to allow =0 (kohaadmin)
426
	my $query = "REMOVE FROM tags_blacklist WHERE blacklist_id = ?";
426
	my $query = "REMOVE FROM tags_blacklist WHERE blacklist_id = ?";
427
	# my $sth = C4::Context->dbh->prepare($query);
427
	# my $sth = C4::Context->dbh->prepare($query);
428
	# $sth->execute($term);
428
	# $sth->execute($term);
Lines 431-437 sub remove_filter { Link Here
431
431
432
sub add_tag_approval {	# or disapproval
432
sub add_tag_approval {	# or disapproval
433
	$debug and warn "add_tag_approval(" . join(", ",map {defined($_) ? $_ : 'UNDEF'} @_) . ")";
433
	$debug and warn "add_tag_approval(" . join(", ",map {defined($_) ? $_ : 'UNDEF'} @_) . ")";
434
	my $term = shift or return undef;
434
	my $term = shift or return;
435
	my $query = "SELECT * FROM tags_approval WHERE term = ?";
435
	my $query = "SELECT * FROM tags_approval WHERE term = ?";
436
	my $sth = C4::Context->dbh->prepare($query);
436
	my $sth = C4::Context->dbh->prepare($query);
437
	$sth->execute($term);
437
	$sth->execute($term);
Lines 456-463 sub add_tag_approval { # or disapproval Link Here
456
456
457
sub mod_tag_approval {
457
sub mod_tag_approval {
458
	my $operator = shift;
458
	my $operator = shift;
459
	defined $operator or return undef; # have to test defined to allow =0 (kohaadmin)
459
	defined $operator or return; # have to test defined to allow =0 (kohaadmin)
460
	my $term     = shift or return undef;
460
	my $term     = shift or return;
461
	my $approval = (scalar @_ ? shift : 1);	# default is to approve
461
	my $approval = (scalar @_ ? shift : 1);	# default is to approve
462
	my $query = "UPDATE tags_approval SET approved_by=?, approved=?, date_approved=NOW() WHERE term = ?";
462
	my $query = "UPDATE tags_approval SET approved_by=?, approved=?, date_approved=NOW() WHERE term = ?";
463
	$debug and print STDERR "mod_tag_approval query: $query\nmod_tag_approval args: ($operator,$approval,$term)\n";
463
	$debug and print STDERR "mod_tag_approval query: $query\nmod_tag_approval args: ($operator,$approval,$term)\n";
Lines 466-473 sub mod_tag_approval { Link Here
466
}
466
}
467
467
468
sub add_tag_index {
468
sub add_tag_index {
469
	my $term         = shift or return undef;
469
	my $term         = shift or return;
470
	my $biblionumber = shift or return undef;
470
	my $biblionumber = shift or return;
471
	my $query = "SELECT * FROM tags_index WHERE term = ? AND biblionumber = ?";
471
	my $query = "SELECT * FROM tags_index WHERE term = ? AND biblionumber = ?";
472
	my $sth = C4::Context->dbh->prepare($query);
472
	my $sth = C4::Context->dbh->prepare($query);
473
	$sth->execute($term,$biblionumber);
473
	$sth->execute($term,$biblionumber);
Lines 480-486 sub add_tag_index { Link Here
480
}
480
}
481
481
482
sub get_tag {		# by tag_id
482
sub get_tag {		# by tag_id
483
	(@_) or return undef;
483
	(@_) or return;
484
	my $sth = C4::Context->dbh->prepare("$select_all WHERE tag_id = ?");
484
	my $sth = C4::Context->dbh->prepare("$select_all WHERE tag_id = ?");
485
	$sth->execute(shift);
485
	$sth->execute(shift);
486
	return $sth->fetchrow_hashref;
486
	return $sth->fetchrow_hashref;
Lines 501-507 sub rectify_weights { Link Here
501
	} else {
501
	} else {
502
		$sth->execute();
502
		$sth->execute();
503
	}
503
	}
504
	my $results = $sth->fetchall_arrayref({}) or return undef;
504
	my $results = $sth->fetchall_arrayref({}) or return;
505
	my %tally = ();
505
	my %tally = ();
506
	foreach (@$results) {
506
	foreach (@$results) {
507
		_set_weight($_->{count},$_->{term},$_->{biblionumber});
507
		_set_weight($_->{count},$_->{term},$_->{biblionumber});
Lines 553-564 sub _set_weight { Link Here
553
}
553
}
554
554
555
sub add_tag {	# biblionumber,term,[borrowernumber,approvernumber]
555
sub add_tag {	# biblionumber,term,[borrowernumber,approvernumber]
556
	my $biblionumber = shift or return undef;
556
	my $biblionumber = shift or return;
557
	my $term         = shift or return undef;
557
	my $term         = shift or return;
558
	my $borrowernumber = (@_) ? shift : 0;		# the user, default to kohaadmin
558
	my $borrowernumber = (@_) ? shift : 0;		# the user, default to kohaadmin
559
	$term =~ s/^\s+//;
559
	$term =~ s/^\s+//;
560
	$term =~ s/\s+$//;
560
	$term =~ s/\s+$//;
561
	($term) or return undef;	# must be more than whitespace
561
	($term) or return;	# must be more than whitespace
562
	my $rows = get_tag_rows({biblionumber=>$biblionumber, borrowernumber=>$borrowernumber, term=>$term, limit=>1});
562
	my $rows = get_tag_rows({biblionumber=>$biblionumber, borrowernumber=>$borrowernumber, term=>$term, limit=>1});
563
	my $query = "INSERT INTO tags_all
563
	my $query = "INSERT INTO tags_all
564
	(borrowernumber,biblionumber,term,date_created)
564
	(borrowernumber,biblionumber,term,date_created)
Lines 567-573 sub add_tag { # biblionumber,term,[borrowernumber,approvernumber] Link Here
567
							"add_tag query args: ($borrowernumber,$biblionumber,$term)\n";
567
							"add_tag query args: ($borrowernumber,$biblionumber,$term)\n";
568
	if (scalar @$rows) {
568
	if (scalar @$rows) {
569
		$debug and carp "Duplicate tag detected.  Tag not added.";	
569
		$debug and carp "Duplicate tag detected.  Tag not added.";	
570
		return undef;
570
		return;
571
	}
571
	}
572
	# add to tags_all regardless of approaval
572
	# add to tags_all regardless of approaval
573
	my $sth = C4::Context->dbh->prepare($query);
573
	my $sth = C4::Context->dbh->prepare($query);
574
- 

Return to bug 6679