Changeset 930


Ignore:
Timestamp:
Mar 11, 2007, 12:51:17 PM (15 years ago)
Author:
kake
Message:

Allow Guide admins to control the content of autocreated nodes (#47).

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/CUSTOMISATION

    r713 r930  
    3333   Will be included just below the main wiki content in node.tt.
    3434
     35 - custom_autocreate_content.tt
     36   You can control the text that gets put into the content field of any
     37   autocreated category/locale nodes by putting it in this template.
     38   The following TT variables will be available to the template:
     39     - [% index_type %]
     40       e.g. "Category" - note that the first letter is capitalised
     41     - [% index_value %]
     42       e.g. "Vegan-friendly" - capitalisation just as typed in the edit form
     43     - [% node_name %]
     44       e.g. "Category Vegan-Friendly" - the full, "official" name of the
     45       autocreated node
     46
    3547Requests for further custom inserts are welcomed.
    3648
  • trunk/Changes

    r928 r930  
    1010        Make sure to always pass the config object into the templates.
    1111        Add some extra test utilities to OpenGuides::Test
     12        Allow Guide admins to control the content of autocreated nodes (#47).
    1213        Write tests for and fix:
    1314          #48 (Edit conflict page erroneously converts lat/lon to os_x, os_y).
  • trunk/lib/OpenGuides.pm

    r928 r930  
    12801280identifies if any of its Categories or Locales are missing, and creates them.
    12811281
     1282Guide admins can control the text that gets put into the content field of the
     1283autocreated node by putting it in custom_autocreate_content.tt in their custom
     1284templates directory.  The following TT variables will be available to the
     1285template:
     1286
     1287=over
     1288
     1289=item * index_type (e.g. C<Category>)
     1290
     1291=item * index_value (e.g. C<Vegan-friendly>)
     1292
     1293=item * node_name (e.g. C<Category Vegan-Friendly>)
     1294
     1295=back
     1296
     1297(Note capitalisation - index_value is what they typed in to the form, and
     1298node_name is the fully free-upper-ed name of the autocreated node.)
     1299
    12821300For nodes not requiring moderation, should be called on writing the node
    12831301For nodes requiring moderation, should only be called on moderation
    1284 =cut
     1302
     1303=cut
     1304
    12851305sub _autoCreateCategoryLocale {
    12861306    my ($self, %args) = @_;
     
    12911311
    12921312    # Check to make sure all the indexable nodes are created
     1313    my $config = $self->config;
     1314    my $template_path = $config->template_path;
     1315    my $custom_template_path = $config->custom_template_path || "";
     1316    my $tt = Template->new( { INCLUDE_PATH =>
     1317                                  "$custom_template_path:$template_path" } );
     1318
    12931319    foreach my $type (qw(Category Locale)) {
    12941320        my $lctype = lc($type);
     
    13001326            unless ( $wiki->node_exists($node) ) {
    13011327                my $category = $type eq "Category" ? "Category" : "Locales";
     1328                # Try to get the autocreated content from a custom template;
     1329                # if we fail, use some default text.
     1330                my $blurb;
     1331                my %tt_vars = (
     1332                                index_type  => $type,
     1333                                index_value => $index,
     1334                                node_name   => $node,
     1335                              );
     1336                my $ok = $tt->process( "custom_autocreate_content.tt",
     1337                                       \%tt_vars, \$blurb );
     1338                if ( !$ok ) {
     1339                    $blurb = "\@INDEX_LINK [[$node]]";
     1340                }
    13021341                $wiki->write_node(
    13031342                                     $node,
    1304                                      "\@INDEX_LINK [[$node]]",
     1343                                     $blurb,
    13051344                                     undef,
    13061345                                     {
  • trunk/t/19_autocreate.t

    r927 r930  
    11use strict;
     2use Cwd;
    23use OpenGuides;
    34use OpenGuides::Test;
     
    89    plan skip_all => "DBD::SQLite not installed - no database to test with";
    910} else {
    10     plan tests => 1;
     11    plan tests => 4;
    1112}
    1213
    1314my $config = OpenGuides::Test->make_basic_config;
     15$config->custom_template_path( cwd . "/t/templates/" );
    1416my $guide = OpenGuides->new( config => $config );
    1517my $wiki = $guide->wiki;
     
    2123}
    2224
     25# Write a custom template to autofill content in autocreated nodes.
     26eval {
     27    unlink cwd . "/t/templates/custom_autocreate_content.tt";
     28};
     29open( FILE, ">", cwd . "/t/templates/custom_autocreate_content.tt" )
     30  or die $!;
     31print FILE <<EOF;
     32Auto-generated list of places in
     33[% IF index_type == "Category" %]this category[% ELSE %][% index_value %][% END %]:
     34\@INDEX_LIST [[[% node_name %]]]
     35EOF
     36close FILE or die $!;
     37
    2338# Check that autocapitalisation works correctly in categories with hyphens.
    2439OpenGuides::Test->write_data(
     
    2641                              node  => "Vivat Bacchus",
    2742                              categories => "Restaurants\r\nVegan-friendly",
     43                              locales => "Farringdon",
    2844                            );
    2945
    3046ok( $wiki->node_exists( "Category Vegan-Friendly" ),
    3147    "Categories with hyphens in are auto-created correctly." );
     48
     49# Check that the custom autocreate template was picked up.
     50my $content = $wiki->retrieve_node( "Category Vegan-Friendly" );
     51$content =~ s/\s+$//s;
     52$content =~ s/\s+/ /gs;
     53is( $content, "Auto-generated list of places in this category: "
     54              . "\@INDEX_LIST [[Category Vegan-Friendly]]",
     55    "Custom autocreate template works properly for categories" );
     56
     57$content = $wiki->retrieve_node( "Locale Farringdon" );
     58$content =~ s/\s+$//s;
     59$content =~ s/\s+/ /gs;
     60is( $content, "Auto-generated list of places in Farringdon: "
     61              . "\@INDEX_LIST [[Locale Farringdon]]",
     62    "...and locales" );
     63
     64# Now make sure that we have a fallback if there's no autocreate template.
     65unlink cwd . "/t/templates/custom_autocreate_content.tt";
     66
     67OpenGuides::Test->write_data(
     68                              guide => $guide,
     69                              node  => "Bleeding Heart",
     70                              categories => "Pubs",
     71                            );
     72$content = $wiki->retrieve_node( "Category Pubs" );
     73$content =~ s/\s+$//s;
     74$content =~ s/\s+/ /gs;
     75is( $content, "\@INDEX_LINK [[Category Pubs]]",
     76    "Default content is picked up if autocreate template doesn't exist" );
Note: See TracChangeset for help on using the changeset viewer.