Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

AutoGroupPages

Summary: How to create a number of pages for a new group automatically
Version: 2006-08-05
Prerequisites: pmwiki 2.1
Status: stable
Maintainer:
Categories: Editing, Administration

Questions answered by this recipe

How can I have a number of pages created automatically for a new group?

Description

Here's a brief recipe that will create HomePage, GroupHeader, and GroupFooter automatically if they don't exist when a page is created in a group:

function AutoGroupPages($pagename, &$page, &$new) {
     global $IsPagePosted, $GroupPagesFmt;
     if (!$IsPagePosted) return;
     SDV($AutoGroupPagesFmt, array(
       '{$Group}.HomePage' => 'Templates.HomePage',
       '{$Group}.GroupHeader' => 'Templates.GroupHeader',
       '{$Group}.GroupFooter' => 'Templates.GroupFooter'));

     foreach($AutoGroupPagesFmt as $n => $t) {
       $n = FmtPageName($n, $pagename);
       $t = FmtPageName($t, $pagename);
       if (!PageExists($n) && $n != $pagename) {
         WritePage($n, ReadPage($t));
       }
     }
   }

   $EditFunctions[] = 'AutoGroupPages';

The last line adds the function to the $EditFunctions array, which results in it getting executed whenever a page edit takes place, meaning that by editing a new page for a new group and saving it, the additional pages specified in the $AutoGroupPagesFmt array will be created too.

$AutoGroupPagesFmt can be defined with different pages prior of including the function.

Notes

The function is declared and used in newgroupbox.phpΔ. If you wish to use NewGroupBox and also have the function available generally to be used when editing a new page for a new group, then you should only add to config.php:

$EditFunctions[] = 'AutoGroupPages';

Automatic creation of Uploads Directory

If you need to have a group uploads directory created automatically, when a first page in a new group is created, you can add the following to local/config.php:

function AutoCreateUploadDirectory($pagename, &$page, &$new) {
 global $IsPagePosted, $UploadFileFmt;

 if (!$IsPagePosted) return;
 $uploaddir = FmtPageName($UploadFileFmt, $pagename);
 mkdirp($uploaddir);
}

$EditFunctions[] = 'AutoCreateUploadDirectory';

Note that normally PmWiki manages fine to create uploads directories when the need (i.e. upload) arises.

Release Notes

  • 2006-08-05: first release as published by Pm on the pmwiki-user list.

If the recipe has multiple releases, then release notes can be placed here. Note that it's often easier for people to work with "release dates" instead of "version numbers".

  • 2007-05-18: Fixed problem with clobbering pagename. That is, if you created an initial group page that was the name of one of the pages created by AutoGroupPages(), then it would overwrite the page. BenWilson May 18, 2007, at 09:47 AM

Comments

The syntax on this page for creating the pages looks like this:

{$Group}.GroupHeader' => 'Templates.GroupHeader'

And on the NewGroupBox recipe page like this:

'Templates.GroupHeader' => '{$Group}.GroupHeader',

Which way is it really? LasseS

They are different! If you use NewPageBox you won't need AutoGroupPages. It is built in. But I reversed the page - template pairing, since it looked more natural to write Template => Page. Well maybe I should not have changed this, as it may be confusing! HansB

See Also

Contributors

Pm

Edit - History - Print - Recent Changes - Search
Page last modified on March 05, 2008, at 02:13 AM