On Thu, 21 Dec 2006, Adeola Awoyemi wrote:
> Is there a way to search for Contributors in Bricolage?
I have been looking
> but have not found one yet. I want to search for a
contributor based on the
> data I have (fname, lname) and get their id from that.
>
> Any help/arrows would be greatly appreciated.
The contributor stuff is hard to work with.
Below are some snippets from a script written by Mark
Jaroski and modified
by me. There might be some application-specific things that
won't
apply for you (like "Country Office", or
"ola", country ISO3 codes, etc.),
though I tried to only include the relevant parts (mostly I
just checked
that there weren't things like usernames and passwords .
This was written for Bricolage 1.8, but probably will work
for later
versions (?).
What's here isn't necessarily the best way to do things...
Your mileage may vary. No guarantees. May destroy your
system. Etc.
use strict;
use warnings;
use Encode;
BEGIN {
die "Set BRICOLAGE_ROOT environment variable to a
valid directoryn"
unless exists $ENV{'BRICOLAGE_ROOT'} and -d
"$ENV{'BRICOLAGE_ROOT'}/lib";
use lib "$ENV{'BRICOLAGE_ROOT'}/lib";
}
use Bric::Biz::Contact;
use Bric::Biz::Person;
use Bric::Util::Grp;
use Bric::Util::Grp::Parts::Member::Contrib;
.....
my ($CO_CONTRIB_GRP) = Bric::Util::Grp->list({ name =>
'Country Office', all =>
1 });
.....
# this gets all contribs whose lname matches a certain
pattern
sub get_co_contribs {
my %contributors =
map {
my $code = lc $_->get_person->get_lname;
$code =~ s/^s+//; $code =~ s/s+$//;
$code => $_;
}
grep { $_->get_person->get_lname =~
/^s*[a-zA-Z]s*$/ } # ISO3 code
(3 letters)
Bric::Util::Grp::Parts::Member::Contrib->list({
grp => $CO_CONTRIB_GRP });
return %contributors;
}
# this creates a contributor
sub create_country_contrib {
my $code = shift;
my $contrib =
Bric::Util::Grp::Parts::Member::Contrib->new({
object_package => 'Bric::Biz::Person',
grp => $CO_CONTRIB_GRP,
active => 1
});
my $p = Bric::Biz::Person->new({ lname => lc
$code });
$p->save();
$contrib->set_obj_id($p->get_id());
$contrib->activate();
$contrib->save();
return $contrib;
}
# This updates the contacts for a particular contributor
sub update_contacts {
my ($contrib, $ola_data) = _;
my $person = $contrib->get_person();
# Delete all ola_* contacts
my contacts = grep { $_->get_type =~ /^ola_/ }
$person->get_contacts();
$person->del_contacts( contacts);
$person->save();
# Recreate new contacts from OLA data
foreach my $col ( COLUMNS) {
unless (exists $ola_data->{$col} and defined
$ola_data->{$col}
and $ola_data->{$col} =~ /S/ and
$ola_data->{$col} ne 'NULL')
{
next;
}
next if $col =~ /country_code/;
$person->new_contact("ola_$col",
decode('iso-8859-1',
$ola_data->{$col}));
}
$person->activate();
$person->save();
}
# This is updating the contribs associated to certain
stories
sub update_country_covers {
my ($country_covers, $co_contribs) = _;
# Note: $country_covers and $co_contribs hash keys are
different
# ($country_covers is URLs, while $co_contribs is ISO3
codes)
foreach my $url (sort keys %$country_covers) {
my $cc = $country_covers->{$url};
my $iso3 = url2iso3($cc);
next if $c and uc($iso3) ne uc($c);
print "$urln";
# Find existing Country Office contribs from the
country cover
my old_co_contribs =
grep { $_->get_grp->get_name eq 'Country
Office' }
$cc->get_contributors();
if (exists $co_contribs->{$iso3} and defined
$co_contribs->{$iso3}) {
# Get the ID for comparing with each existing
one
my $new_c = $co_contribs->{$iso3};
my $new_cid = $new_c->get_id();
my %dont_update = ();
# Wipe out existing C.O. contribs from this
country cover
# (XXX: change this only if you know what
you're doing)
foreach my $c ( old_co_contribs) {
my $cid = $c->get_id;
if ($cid == $new_cid) {
$dont_update{$cid}++;
next;
}
# print "DEL contrib: '$cid'n";
$cc->delete_contributors([$cid]);
$cc->save(); # probably
unnecessary (done below)
}
# Add the appropriate contrib to this country
cover
unless (exists $dont_update{$new_cid}) {
# print "ADD contrib: ",
$new_c->get_person->get_lname,
# ' (', $new_c->get_id, ')', $/;
$cc->add_contributor($new_c);
$cc->save();
}
} else {
# print "SKIP: no contrib for $url
!n";
}
}
}
|