Revision [3444]

This is an old revision of WikiTemplate made by JavaWoman on 2004-12-18 16:07:42.

 

Templates

I like to re-use and hate to redesign the wheel... Surely I'm not alone ;-)

My solution

I would like to build an action {{template from="OldPage" to="NewPage" editoption="true"}} that would duplicate the template page to a new page .
It would first check the existence of the template page, the non existence of the page to be created, the user right to read the template and the user right to create a page.
The editoption when true will create the new page and then open it for edition.

The code

Copy this code into an action named: template.php - place it in the actions folder.
<?php
/**
 * Duplicate a "template" page to a new named page
 *
 * Usage: {{template from="OldPage" to="NewPage" editoption="true"}}
 *
 * This action checks the existence of the template page, the non existence of the page to be created, the user
 * right to read the template and the user right to create a page.
 * The editoption when true will create the new page and then open it for edition.
 *
 *
 * @package         Actions
 * @subpackage        
 * @name              template
 *
 * @author            {@link http://wikka.jsnx.com/ChristianBarthelemy Christian Barthelemy}
 * @version           0.2
 * @since             Wikka 1.1.5.3
 *
 * @input             string  $from  optional: the template page to be duplicated
 *                            must be an existing page and must be authorized for reading to the current user  
 *                            defaulted to the current WikiPage
 *                              
 * @input             string  $to  mandatory: the page to be created
 *                            must be a non existing page and current user must be authorized to create
 *
 * @input             string  $note  optional: the note to be added to the page when created
 *                            default is "Templated from " followed by the name of the template page
 *
 * @input             boolean $editoption optional: if true, the newly created page will be opened for edition
 *                            default is false
 */


 // ***** CONSTANTS *****
define ('USER_NAME',$this->GetUserName());
define ('BASE_URL',$this->config['base_url']);
// ***** END CONSTANTS *****

 // ***** INPUT FORM *****

echo $this->FormOpen();
?>
<table class="template" border="1">
    <tr>
        <th align="left">WikiPage to be templated (empty means this page):</th>
        <td><input type="text" name="from" size="37" value="<?php echo $from ?>" /></td>
    </tr>
    <tr>
        <th align="left">WikiPage to be created:</th>
        <td><input type="text" name="to" size="37" value="<?php echo $to ?>" /></td>
    </tr>
    <tr>
        <th align="left">Added note to your edit:</th>
        <td><input type="text" name="note" size="37" value="<?php echo $note ?>" /></td>
    </tr>
    <tr>
        <th align="left">Do you want to edit the new page when created?</th>
        <td>
            <table border="1">
                <tr>
                    <td><input type="checkbox" name="editoption" value="Y" <?php echo $editoption == "true" ? "checked=\"checked\"" : "" ?> />Edit after creation</td>
                    <td><input type="submit" name="create" value="Template it now"></td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<?php
echo $this->FormClose();
 // ***** END INPUT FORM *****
 
// ***** GET PARAMETERS *****
$from = $_POST["from"];
$to = $_POST["to"];
$note = $_POST["note"];
$editoption = $_POST["editoption"];
// ***** END PARAMETERS *****

// ***** TEMPLATING PROCESS *****
If ($to<>"") {
    // The user must provide a WikiPage name to be created...
    if ($this->ExistsPage($to))
    {
        // and it should be a new one...
        $errormessage = "The destination page must not exist.";
        echo $errormessage;
        return;
    }
    // also the user has to be allowed to create this page...
    elseif ($this->HasAccess("write", $to)) {
        // The user should provide a WikiPage name to be duplicated...
        if (!$from)
        {
            // by default the system will duplicate the current page...
            $from = $this->GetPageTag();
        }
        else
        {
            // otherwise, the page to be dublicated must of course exit...
            if ($this->ExistsPage($from))
            {
                // and the user has to be allowed to read it...
                if (!$this->HasAccess("read", $from))
                {
                    $errormessage = "You are not authorized to use the template page.";
                    echo $errormessage;
                    return;
                }
            }
            else
            {
                // this is what happens if the user tries to duplicate a non existing page...
                $errormessage = "The template page does not exist.";
                echo $errormessage;
                return;
            }
        }
        // the user may provide a creation note otherwise...
        if (!$note)
        {
            // the system will create one by default...
            $note = "Templated from ".$from;
        }
       
        // ***** DERIVED VARIABLES *****
        $thepage=$this->LoadPage($from); // load the page that has to be duplicated...
        if ($thepage) $pagecontent = $thepage["body"]; // and get its content.
        // ***** END DERIVED VARIABLES *****
 
        // ***** OUTPUT SECTION *****
        $this->SavePage($to, $pagecontent, $note); // creates the page...
        echo "[[ ".$to." has been created from ".$from." and note was: ".$note." ]]"; // confirms what has been done
        if ($editoption) $this->redirect(BASE_URL.$to."/edit"); // drops the user to the new page for edition if he choosed so
        // ***** END OUTPUT SECTION *****

    } else
    {  
        // this is what happens if you try to create a page when you are not authorized.
        $errormessage = "You are not authorized to create the destination page.";
        echo $errormessage;
        return;
    }
}
// ***** END TEMPLATING PROCESS *****
?>


How to use it?

Once you have (re)designed a page that fits to be replicated many times (Task Lists, User Pages, Bug Description, Developement Description...) you just have to use the {{template}} action from any page: you may want to have a dedicated TemplatePage with the action and the instructions on it (I will propose one if this action can be installed on the Wikka server).
Then you must fill the name of the new WikiPage to be created, if you do not fill the name of the page that has to be duplicated the system will duplicate the current page, you may want to add a note, finally you decide if you just want to create the page or if you want to jump and edit it as soon as it has been copied.

To Do

My code needs probably to be reviewed by expert coder as I am not at all a developper.
Any ideas around this action more than welcome.

I get the following error (just copied the code to template.php and tried the syntax you gave):

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /.../actions/template.php on line 2

Parse error: parse error, unexpected T_STRING in /.../actions/template.php on line 2"
--NilsLindenberg


(After code change) Christian, OK, I see the intention now. And I like it a lot! Unfortunately, the code isn't quite correct - mind if I take a stab at clearing it up?
Nils, I don't have a clue how you can get that error; did you grab the code from the page source? --JavaWoman.

Yepp, I always copy from the source. --NilsLindenberg
Do you still have the same error with the new code? --ChristianBarthelemy
I have to try later, they don't trust me that much in the university, to allow ftp access to pages. And I haven't worked out how I should explain wikka as an essentail part of my studies ;-). I'll check later. --NilsLindenberg
1. Are you entering something in the form or as an action attribute that contains a \? There's not even a \ in the code that I can see. If so, try / as a path separator instead.
2. Wikka for studies - maybe this helps? Group as User: Flaming and the Design of Social Software - by Clay Shirky; I guess you're doing a field study? ;-) --JavaWoman
Ok, I tried the new code, but it hasn't changed anything. I tried simple opage names like NewPage, NewerPage etc. even adding {{template}} gives that error. --NilsLindenberg
Which Wikka version are you using? I do not see any reason for such an error. I replaced the template.php version from my server with a copy from this page to a new txt notepad file, renamed into template.php placed it in the action folder and it worked.

Update: Ok, I finally found out what I made wrong. My mistake: the txt files on this pc are connected with wordpad, not with notepad. I hadn't looked after that and so saved the files with useless renderings *bangs head against wall*. But after that, I found some points in your script:
  1. There was an ?> missing at the end
  1. The length for a page can be 75 chars, I changed your value to that just keep that in mind, cuase it means scrolling now for names >37
  1. if you have much output, more simple than putting out all with echo is to leave php. I replaced the input form above. You now get the variables shown in the fields.
--NilsLindenberg
2ndUpdate: works fine, thanx. --NilsLindenberg

Any one else tried it? --ChristianBarthelemy
I've been tweaking the code a bit but haven't actually tested anything yet; I'm afraid I have a few more urgent things on my list right now (such as finishing WikkaGeSHiIntegration with install/update). I will get back to this though, promise, since I think it's very useful! --JavaWoman

I like the idea too. A couple of general remarks:
This action (usefully) accepts a number of parameters, either predefined in the action or via the form; as far as I can tell you can not easily do that with a handler: URL parameters are possible but are much harder to handle for an end user (requiring knowledge of how to build a query via a URL). Also, such a handler would either have to operate on the source page or on the target page while an action like this can be embedded in any page. User input really is required: an action with a form is more flexible and more user-frienldy. --JavaWoman
Sounds nice but would be workable only in a small Wiki. OTH, if you limit the action to special-purpose Template pages this could be workable; or one could have an extra option to choose a template or any page with a dropdown only for templates and a text field for any other page. --JavaWoman
First, it would be possible to create a number of (read-only) templates (e.g., a Doumentation template). In general, though clone (but not export) might be a better term if you (can) use the action to copy any page and not just special-purpose template. --JavaWoman

You of course have my blessing - This code surely needs some revamping and I will be more than happy that we get something cleaner --ChristianBarthelemy

CategoryDevelopment
There are 2 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki