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

(-)a/misc/devel/create_superlibrarian.pl (-13 / +68 lines)
Lines 18-29 Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
use Getopt::Long qw( GetOptions );
22
use Getopt::Long qw( GetOptions );
22
use Pod::Usage qw( pod2usage );
23
use Pod::Usage   qw( pod2usage );
24
use Try::Tiny    qw(catch try);
23
25
24
use Koha::Script;
26
use Koha::Database;
27
use Koha::Exceptions::Object;
28
use Koha::Libraries;
29
use Koha::Patron::Categories;
25
use Koha::Patrons;
30
use Koha::Patrons;
26
31
32
use Koha::Script;
33
27
my ( $help, $surname, $userid, $password, $branchcode, $categorycode, $cardnumber );
34
my ( $help, $surname, $userid, $password, $branchcode, $categorycode, $cardnumber );
28
GetOptions(
35
GetOptions(
29
    'help|?'         => \$help,
36
    'help|?'         => \$help,
Lines 41-56 pod2usage("branchcode is mandatory") unless $branchcode; Link Here
41
pod2usage("categorycode is mandatory") unless $categorycode;
48
pod2usage("categorycode is mandatory") unless $categorycode;
42
pod2usage("cardnumber is mandatory")   unless $cardnumber;
49
pod2usage("cardnumber is mandatory")   unless $cardnumber;
43
50
44
my $patron = Koha::Patron->new({
51
try {
45
    surname      => $surname,
52
    Koha::Database->new->schema->txn_do(
46
    userid       => $userid,
53
        sub {
47
    cardnumber   => $cardnumber,
54
            Koha::Exceptions::Object::FKConstraint->throw(
48
    branchcode   => $branchcode,
55
                error     => 'Broken FK constraint',
49
    categorycode => $categorycode,
56
                broken_fk => 'branchcode'
50
    flags        => 1,
57
            ) unless Koha::Libraries->find($branchcode);
51
})->store;
58
52
59
            Koha::Exceptions::Object::DuplicateID->throw( duplicate_id => 'userid' )
53
$patron->set_password({ password => $password, skip_validation => 1 });
60
                if Koha::Patrons->find( { userid => $userid } );
61
62
            Koha::Exceptions::Object::DuplicateID->throw( duplicate_id => 'cardnumber' )
63
                if Koha::Patrons->find( { cardnumber => $cardnumber } );
64
65
            my $patron = Koha::Patron->new(
66
                {
67
                    surname      => $surname,
68
                    userid       => $userid,
69
                    cardnumber   => $cardnumber,
70
                    branchcode   => $branchcode,
71
                    categorycode => $categorycode,
72
                    flags        => 1,               # superlibrarian
73
                }
74
            )->store;
75
76
            # password is set on a separate step (store would set the hashed password)
77
            $patron->set_password( { password => $password, skip_validation => 1 } );
78
        }
79
    );
80
} catch {
81
    if ( ref($_) eq 'Koha::Exceptions::Object::FKConstraint' ) {
82
83
        my $value =
84
              $_->broken_fk eq 'branchcode'   ? $branchcode
85
            : $_->broken_fk eq 'categorycode' ? $categorycode
86
            :                                   'ERROR';
87
88
        my @valid_values =
89
              $_->broken_fk eq 'branchcode'   ? Koha::Libraries->new->get_column('branchcode')
90
            : $_->broken_fk eq 'categorycode' ? Koha::Patron::Categories->new->get_column('categorycode')
91
            :                                   ('UNEXPECTED');
92
93
        printf STDERR "ERROR: '%s' is not valid for the '%s' field\n", $value, $_->broken_fk;
94
        printf STDERR "Possible values are: " . join( ', ', @valid_values ) . "\n";
95
96
    } elsif ( ref($_) eq 'Koha::Exceptions::Object::DuplicateID' ) {
97
98
        my $value =
99
              $_->duplicate_id eq 'cardnumber' ? $cardnumber
100
            : $_->duplicate_id eq 'userid'     ? $userid
101
            :                                    'ERROR';
102
103
        printf STDERR "Field '%s' must be unique. Value '%s' is used already.\n", $_->duplicate_id, $value;
104
    } else {
105
        print STDERR "Uncaught exception: $_\n";
106
    }
107
108
    exit 1;
109
};
54
110
55
=head1 NAME
111
=head1 NAME
56
112
57
- 

Return to bug 36309