List Info

Thread: Re: Editing Templates from Within - Templates part 2




Re: Editing Templates from Within - Templates part 2
user name
2008-02-20 10:58:27
Just to cut to the chase (since this may be what you are
most  
needing), here is the code from the Greg/Bret
lineup_manipulator.mc  
subelement template (if added to a story it will allow the
story  
editor to manipulate the order and related stories shown on
the Home  
story, but from within a related story not the Home story).

<%perl>

# collect objects
my $user;
if (!$story->get_user__id) {
   $user = Bric::App::Session->get_user_id;
} else {
   $user = $story->get_user__id;
}

my homepage =
Bric::Biz::Asset::Business::Story->list({
   active => 1,
   category_uri => '/',
   element_key_name => 'category_hub',
   Limit => 1
});
my category_home =
Bric::Biz::Asset::Business::Story->list({
   active => 1,
   category_uri =>
$story->get_primary_category->get_uri,
   element_key_name => 'category_hub',
   Limit => 1
});

# find which lineups need shuffling
foreach my $e ($element->get_elements) {
   # skip if possible
   next if ($e->get_value eq 'checked_out' ||
$e->get_value eq  
'do_not_touch' || $e->get_value eq 'empty');

   # are we dealing with a category page or the home page?
   my $page;
   my $lineup_name = $e->get_key_name.'_lineup';
   my lineup;
   if ($lineup_name =~ m/^home_/) {
     $page = $homepage[0];
     $lineup_name =~ s/home_//;
     lineup =
$homepage[0]->get_elements($lineup_name);
   } elsif ($lineup_name =~ m/^category_/) {
     $page = $category_home[0];
     $lineup_name =~ s/^category_//;
     lineup =
$category_home[0]->get_elements($lineup_name);
#    $m->out("looking for lineup
".$lineup_name." in ".$page- 
 >get_title."<br>");
#    foreach my $thingie
($category_home[0]->get_elements) {
#      $m->out("ELEMENT
".$thingie->get_key_name."<br>");
#    }
   } else {
     next;
   }

   if ($#lineup < 0) {
#    $m->out("no lineup found for
".ucfirst($lineup_name)."!<br>");
     next;
   }

   # ensure the page isn't already checked out
   if ($page->get_checked_out) {
     $burner->throw_error("Cannot modify the lineup
(".$page- 
 >get_title.") because it is checked out!");
   }

   # loop and collect lineup items
   my current_lineup;
   my $current_position;
   foreach my $item ($lineup[0]->get_elements) {
     push(current_lineup, $item->get_related_story_id);
     if ($item->get_related_story_id ==
$story->get_id) {
       $current_position = $item->get_object_order;
     }
   }

# $m->out("old: current_lineup
<br>");

   # make the new lineup
   if ($current_position && $e->get_value >=
$current_position) {
     splice(current_lineup, $e->get_value, 0,
$story->get_id);
   } else {
     splice(current_lineup, $e->get_value-1, 0,
$story->get_id);
   }

# $m->out("mid: current_lineup
<br>");

   if ($current_position && $e->get_value <
$current_position) {
     splice(current_lineup, $current_position, 1);
   } elsif ($current_position && $e->get_value
>= $current_position) {
     splice(current_lineup, $current_position-1, 1);
   }

# $m->out("new: current_lineup
<br>");

   # loop again and do the actual work
   my $current_count = 0;
   my $element_type_id;
   foreach my $item ($lineup[0]->get_elements) {
     if ($item->get_related_story_id !=
$current_lineup[($item- 
 >get_object_order-1)]) {
      
$item->set_related_story_id($current_lineup[($item- 
 >get_object_order-1)]);
       $item->save;
       $element_type_id = $item->get_element_type_id;
     }
     $current_count++;
   }

   # maybe we need to add a new lineup item
   if (($#current_lineup+1) > $current_count) {
     my $index = $#current_lineup;
# $m->out("ADDING!
".$current_lineup[$index]."<br>");
     my $new_item = Bric::Biz::Element::Container->new({
       object_type => 'story',
       object_instance_id => $current_lineup[$index],
       place => ($index+1),
       parent_id => $lineup[0]->get_id,
       element_type_id => $element_type_id,
       active => 1
     });
    
$new_item->set_related_story_id($current_lineup[$index]);

     $lineup[0]->add_element($new_item);
   }

# $m->out("final: current_lineup
<br>");

   # save and check the page back in
   $page->set_workflow_id(101);
   $page->save;

   my $publish_desk = Bric::Biz::Workflow::Parts:esk->
lookup({ name  
=> 'Publish' });
   $publish_desk->accept({ asset => $page });
   $publish_desk->save;

}
</%perl>



Now you also have to have a code_select widget set up in the
 
lineup_manipulator element type - which contains the
following code:

my %collection;
my homepage =
Bric::Biz::Asset::Business::Story->list({
   active => 1,
   category_uri => '/',
   element_key_name => 'category_hub',
   Limit => 1
});

if ($homepage[0]) {  #Home page found!

   if ($homepage[0]->get_checked_out) {  #Somebody else
is using the  
home page.
     my $user = Bric::Biz::Person::User->lookup({ id
=> $homepage[0]- 
 >get_user__id });
     %collection = (
       'checked_out' => 'Home page is checked out to ' .
$user- 
 >get_fname . ' ' . $user->get_lname . '. Sorry.'
     );

   } else {  #Home page is not in use, so look for lineup
inside it.

     my $lineup =
$homepage[0]->get_container('east_lineup');

     if ($lineup) {  #Lineup container exists
       my entries = $lineup->get_elements;

       $collection{'do_not_touch'} = 'Do not touch';
       foreach my $entry(entries) {
         $collection{ $entry->get_object_order } =
sprintf('%02d', 
$entry->get_object_order) . ' - ' .
$entry->get_related_story- 
 >get_title;
       }

       if (!entries) {  #The lineup container exists but is
empty.  
Do not fill.
         %collection = (
           'empty' => 'Lineup contains no stories. Please
edit it  
manually.'
         );
       }

     } else {  #Lineup container does not exist
       %collection = (
         'absent' => 'Lineup does not exist. (Maybe it
was deleted?)  
Please add and edit it manually.'
       );
     }
   }


} else {  #Home page story not found. Time for panic.
   %collection = (
     'no_homepage' => 'Home page not found. (Maybe it was
deleted?)  
Please contact an administrator.'
   );
}
return %collection;



---

So please let me know if you need some kind of in between
details.  
I'm not quite sure at what level it would be best to write
this  
tutorial for new and intermediate bricolage users. So
questions or  
clarifications will help me determine that.

Dawn

Re: Editing Templates from Within - Templates part 2
user name
2008-02-20 12:11:52
Greetings!

The code you have there is similar to what I was
implementing, In  
theory I know how to implement these relationships (Ashlee
Caul  
already answered to my other question).  The only problem is
that I'm  
having trouble checking out the related stories and don't
know exactly  
why.  The checkout method just throws a "Must be
checked out to users"  
exception that's not documented and doesn't make sense to
me, and in  
my particular case it's not feasible to ask content editors
to  
checkout the game spaces to which they wish to relate before
editing  
stories because that would be nearly the same as forcing
them to  
create relationships in the game spaces (which is what they
already do  
now and want to avoid).

Thanks for looking into it;
   -- jps

Quoting Dawn Buie <dawnthetyee.ca>:

> Just to cut to the chase (since this may be what you
are most needing),
> here is the code from the Greg/Bret
lineup_manipulator.mc subelement
> template (if added to a story it will allow the story
editor to
> manipulate the order and related stories shown on the
Home story, but
> from within a related story not the Home story).

------------------------------------------------------------
----
This message was sent using IMP, the Internet Messaging
Program.



[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )