Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Bricolage: users

Programmatic Container Element Creation

 

 

Bricolage users RSS feed   Index | Next | Previous | View Threaded


smith_t at denison

Jul 28, 2008, 5:56 AM

Post #1 of 10 (320 views)
Permalink
Programmatic Container Element Creation

Happy Monday Group,

So as per my post last week I am working on creating a UID index page
for returned UIDs from the Oracal calendar. I'm having troubles
adding new container elements to the UID index, mainly because I've
never done something quite like this before. Here is my current code:

% # $uri == URI to store
% #Store the returned UID
% my $uidstory = Bric::Biz::Asset::Business::Story->lookup({uuid =>
'595FC2AC-58B9-11DD-800B-39D208E6C7E9'}); #UID INDEX STORY
% my $match = 0;
% foreach my $euid ($uidstory->get_elements()){
% #Check if UID is already stored
% if ($euid->has_name('event_uid')) {
% if($uid == $euid->get_data('uid')){$match++;}
% }}
% if($match == 0){
% #Store new UID and relate current story
% my $container = Bric::Biz::Element::Container->lookup({object =>
$uidstory, key_name => 'event_uid'});
% my $curuidstory = $uidstory->add_container($container);
% $curuidstory->get_fields('uid')->set_value($uid);
% $curuidstory->set_related_story($story);
% }

This is the error I am getting back:
Can't locate object method "get_field_types" via package
"Bric::Biz::Element::Container"

I assume this applies to this line:
% $curuidstory->get_fields('uid')->set_value($uid);

What I'm trying to do there is store the UID stored in the variable
$uid into the field 'uid' within my newly created container element.
Each container element only consists of a non-repeating UID text input
field and then a related story.

Thanks!
-Trevor


smith_t at denison

Jul 29, 2008, 6:20 AM

Post #2 of 10 (297 views)
Permalink
Re: Programmatic Container Element Creation [In reply to]

So on the opposite end of my problem listed above I am also trying to delete
the containers that reference UIDs to no longer present events. I am trying
to use this API call:

$container->delete_elements(\@subelements)

Like this:

% $element = $element->delete_elements({id => $e->get_id, type =>
'container'});
% $element = $element->save();

I am getting this error:

Not an ARRAY reference at
/usr/local/bricolage/lib/Bric/Biz/Element/Container.pm line 1351, <GEN1855>
line 549.

I understand it is supposed to be an array and i am passing a hash, but you
can't pass keys in an array...so what am I missing here? This is the link
to the API page:
http://www.bricolage.cc/docs/current/api/

Thanks ahead of time and for anyone who want to help me with the above
problem that would be great too!

-Trevor


lannings at who

Jul 29, 2008, 6:49 AM

Post #3 of 10 (297 views)
Permalink
Re: Programmatic Container Element Creation [In reply to]

On Tue, 29 Jul 2008, Trevor Smith wrote:
> So on the opposite end of my problem listed above I am also trying to delete
> the containers that reference UIDs to no longer present events. I am trying
> to use this API call:
>
> $container->delete_elements(\@subelements)

You forgot the rest of the doc:

Removes the specified subelements from the current element.
The arguments that can be passed via the array reference can be
either container or data element objects or hash references with
the following keys: [... 'type' and 'id' ...]

> Like this:
>
> % $element = $element->delete_elements({id => $e->get_id, type =>
> 'container'});
> % $element = $element->save();
>
> I am getting this error:
>
> Not an ARRAY reference at
> /usr/local/bricolage/lib/Bric/Biz/Element/Container.pm line 1351, <GEN1855>
> line 549.
>
> I understand it is supposed to be an array and i am passing a hash, but you
> can't pass keys in an array...so what am I missing here?

[.
{type => 'container', id => $id1},
{type => 'container', id => $id2},
# ....
]


smith_t at denison

Jul 29, 2008, 8:05 AM

Post #4 of 10 (297 views)
Permalink
Re: Programmatic Container Element Creation [In reply to]

So if I only want to delete one container element I should pass:

% $element = $element->delete_elements([{type => 'container' , id =>
$e->get_id}]);
% $element = $element->save();

And the brackets make it an array of hashes? Thanks for the clarification.

-Trevor


lannings at who

Jul 29, 2008, 8:27 AM

Post #5 of 10 (297 views)
Permalink
RE: Programmatic Container Element Creation [In reply to]

Actually now that I think about it, I don't see why you'd
do that. Just pass in $e itself:

$element->delete_elements([$e]);

Using the hashref is presumably for when you don't
have the object already (like if you used list_ids, I guess).

-----Original Message-----
From: smith.t.denison[at]gmail.com on behalf of Trevor Smith
Sent: Tue 7/29/2008 5:05 PM
To: users[at]lists.bricolage.cc; Lanning, Scott
Subject: Re: Programmatic Container Element Creation

So if I only want to delete one container element I should pass:

% $element = $element->delete_elements([{type => 'container' , id =>
$e->get_id}]);
% $element = $element->save();

And the brackets make it an array of hashes? Thanks for the clarification.

-Trevor


david at kineticode

Jul 29, 2008, 9:48 AM

Post #6 of 10 (295 views)
Permalink
Re: Programmatic Container Element Creation [In reply to]

On Jul 28, 2008, at 05:56, Trevor Smith wrote:

> This is the error I am getting back:
> Can't locate object method "get_field_types" via package
> "Bric::Biz::Element::Container"

Please always include a stack trace when you post stuff like this,
otherwise we have no idea where the error occurred or what triggered it.

> I assume this applies to this line:
> % $curuidstory->get_fields('uid')->set_value($uid);
>
> What I'm trying to do there is store the UID stored in the variable
> $uid into the field 'uid' within my newly created container element.
> Each container element only consists of a non-repeating UID text input
> field and then a related story.

I rewrote things so that I could better understand what you were
doing. You're pretty close, I think; I added code to add the UID field
if it doesn't already exist. If it's a required field, it should
exist, but who knows? Anyway, give this a try; I'm assuming that this
is a utility template, hence the `return`. Be sure to use List::Util
in PERL_LOADER in bricolage.conf. I've not tested this code, of
course, so watch for syntax errors!

<%perl>;
# $uri == URI to store
# Store the returned UID
use List::Util 'first'; # Put this in PERL_LOADER.
my $uidstory = Bric::Biz::Asset::Business::Story->lookup({
uuid => '595FC2AC-58B9-11DD-800B-39D208E6C7E9',
}); # UID INDEX STORY

return if first {
$uid = $_->get_value('uid')
} $uidstory->get_elements('event_uid');

# Add an "event_uid" container element to the UID story.
my $elem = $uidstory->get_element;
my $etype = Bric::Biz::ElementType->lookup({ key_name =>
'event_uid' });
my $euid = $uidstory->add_container($etype);

# Make sure that the element has a "uri" field.
my $uid_field = $euid->get_field('uid');
unless ($uid_field) {
my $et = $uidstory->get_element_type;
my $ftype = $et->get_field_types('uid');
$elem->add_field($ftype);
$uid_field = $euid->get_field('uid');
}

# Set the UID and relate the current story.
$uid_field->set_vaule($uid);
$elem->set_related_story($story);
$elem->save;
</%perl>

Best,

David


smith_t at denison

Jul 29, 2008, 11:07 AM

Post #7 of 10 (297 views)
Permalink
Re: Programmatic Container Element Creation [In reply to]

Wow, thanks David that was super helpful. My code is now creating the
container elements and filling them with the currectly filled UID field,
however it is still not relating an event. This is how I adapted the code
you sent me below. This actually is in the "events.mc" template and not in
a utility template, fyi.
#Store the returned UID
my $match = 0;
foreach my $euid ($uidstory->get_elements()){
#Check if UID is already stored
if ($euid->has_name('event_uid')) {
if($uid == $euid->get_data('uid')){$match++;}
}
}
if($match == 0){
### Store New UID and Relate Current Story ###
$uidstory->get_elements('event_uid');
# Add an "event_uid" container element to the UID story
my $elem = $uidstory->get_element;
my $etype = Bric::Biz::ElementType->lookup({ key_name => 'event_uid'
});
my $euid = $uidstory->add_container($etype);
# Make sure that the element has a "uid" field
my $uid_field = $euid->get_field('uid');
unless ($uid_field) {
my $et = $uidstory->get_element_type;
my $ftype = $et->get_field_types('uid');
$elem->add_field($ftype);
$uid_field = $euid->get_field('uid');
}
# Set the UID and relate the current story
$uid_field->set_value($uid);
$elem->set_related_story($story);
$elem->save;
}

Could it to be how I am saving the element? I've tried several
combination. Thanks so much!

-Trevor


david at kineticode

Jul 29, 2008, 11:13 AM

Post #8 of 10 (297 views)
Permalink
Re: Programmatic Container Element Creation [In reply to]

On Jul 29, 2008, at 11:07, Trevor Smith wrote:

> # Set the UID and relate the current story
> $uid_field->set_value($uid);
> $elem->set_related_story($story);
> $elem->save;
> }
>
> Could it to be how I am saving the element? I've tried several
> combination. Thanks so much!

I think you also need to call $uid_story->save;

HTH,

David


smith_t at denison

Jul 29, 2008, 11:17 AM

Post #9 of 10 (297 views)
Permalink
Re: Programmatic Container Element Creation [In reply to]

Thanks, but I tried that and no soup... Any other thoughts?

NTT,

Trevor

On Tue, Jul 29, 2008 at 2:13 PM, David E. Wheeler <david[at]kineticode.com>wrote:

> On Jul 29, 2008, at 11:07, Trevor Smith wrote:
>
> # Set the UID and relate the current story
>> $uid_field->set_value($uid);
>> $elem->set_related_story($story);
>> $elem->save;
>> }
>>
>> Could it to be how I am saving the element? I've tried several
>> combination. Thanks so much!
>>
>
> I think you also need to call $uid_story->save;
>
> HTH,
>
> David
>


david at kineticode

Jul 29, 2008, 11:36 AM

Post #10 of 10 (296 views)
Permalink
Re: Programmatic Container Element Creation [In reply to]

On Jul 29, 2008, at 11:17, Trevor Smith wrote:

> Thanks, but I tried that and no soup... Any other thoughts?

No, not really. Look at ContainerProf.pm for clues.

Best,

David

Bricolage users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.