Templates

This idea is now implemented (as a handler, not as an action) as of version 1.1.6.0; see CloneHandlerInfo.
Last edited by JavaWoman:
Replaces old-style internal links with new pipe-split links.
Fri, 20 May 2016 07:38 UTC [diff]


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.

Solved error & minor code changes
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


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
  2. 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
  3. 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 --DarTar

Difference action/handler
Should this really be an action? Why not a handler? --DarTar
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
I thought about this when I first defined what I expected, and concluded that an action was more appropriate for the reasons explained by JavaWoman. --ChristianBarthelemy
To me 'cloning a page' is really something that should be taken care of by a handler. Here's why:
  • I don't see the difficulty of creating handlers containing forms. See for instance the /acls handler: you can have user-input and URL parameters (both GET and POST) easily dealt with a handler.
  • Moreover, the reason why I see this functionality better implemented as a handler than as an action is that it really looks like an operation that users are likely to perform on any page. Instead of: 1) opening the page, 2) adding an action, 3) save the page, and start cloning the page, you just need to point to the 'clone' handler for this page. The clone handler will then print a form asking you the name of the target page (the source being by default the current page - as the default selected item of a form menu).
  • Pointing to the 'clone' handler for the current page can then be easily done by adding a footer link, much as the current "Edit page". I'm more and more convinced that these kinds of operations on pages (which are coherent with the definition of a handler) should one day be all added in the footer menu: different footer links pointing to different handlers will then allow the user to edit the current page, show source of current page, modify the ACLs for the current page, display the referrers of the current page, clone the current page, delete the current page, send the current page by email, create a PDF of the current page etc.
  • As a general rule, what handlers are displayed in the footer menu should be a configurable option (either system-wide or a user preference).
Doesn't this sound more reasonable than using an action? -- DarTar

Got your point and the logic behind. I do not know enough about the handlers and did not realized that you can use forms within. I am far from being a coder, anyway I will try to figure out how to make it a handler. I think that an expert advice explaining when to use handler or action would be nice to be found at ActionOrHandler--ChristianBarthelemy
Christian, take a look at HandlerInfo to see the distinction between actions and handlers. Actions are meant in general to 'produce automatically generated content within a page'. Handlers perform operations on a page as a whole -- DarTar.
I knew about the general distinction between actions and (page) handlers, but like Christian I hadn't realised that a handler can display a form, too. I thought of action because a form is obvously needed, and that would need to be displayed "on" a page. Given that a handler can display a form to request parameter input, I agree this would be better implemented as a handler.
[Note to self: learn more about handlers!] --JavaWoman

My distinction between handler and action is the following:an action can be included in a page. It adds code, it is a plugin. A handler is not put on a page, nor does it add code to it, but it does something with the page. --NilsLindenberg

Dropdown-list with page-names
What about adding a menu to allow the user to choose the source page from one of the existing pages? This might avoid checking for page existence --DarTar
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
Is there any problem with long drop-down menus? In any case the source won't be longer than the source of PageIndex. I find interesting on the other hand the idea of having some preferred pages listed at the top of the menu (maybe just checking a Category will do the job)-- DarTar
"Another idea would be to predefine the pages that may be duplicated by tagging them somehow. The system would then only provide in a dropdown list such pages. --ChristianBarthelemy"



better name then template?
I think that the concept of template is associated in general to something quite different from what you are proposing here. What about using a name suggesting page exporting,duplicating or cloning? --DarTar
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
"I agree, the name of template only fits my personnal purpose and "clone" is a more appropriate naming for this action. Hence a new page: CloneAction. --ChristianBarthelemy"


Development
(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? --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

Contribution
I dont know if this is proper , (putting up ChristianBarthelemy 's code with my edits but here it is ...
this is has a drop-down menu to select the page you want to copy ...
( also removed the amazing documentation/comments so that it wont be heavy in the eye when posting on this page.
Edit/Remove at will
code removed I stayed behind and didnt look at the development train ...
Well, do add a bit to show what your idea for the dropdown was... --JavaWoman
$res = $this->LoadAll("select tag from ".$this->config['table_prefix']."pages where latest='Y'");

foreach($res as $i => $arr){
    if (!($arr['tag'] == $this->GetPageTag()))
        $select_input .= '<option value="'.$arr['tag'].'">'.$arr['tag'].'</option>\n\t';
}

$select_input = $this->ReturnSafeHTML(
                "<select name=\"from\">".
                "\n\t<option value=\"".$this->GetPageTag()."\">".$this->GetPageTag()."</option>".
                "\t\n$select_input</select>"
                );
This is the way I created the dropdown formfield ... maybe it will be usefull at some point .. --GeorgePetsagourakis

Pardon me if this is not a proper way to do things.. I am a bit new in here --GeorgePetsagourakis
"This is a wiki so it has to be a collaborative environment, as I pointed out, my original code surely needs to be improved. --ChristianBarthelemy."

George, nothing in principle against including alternative code - but your code seems to be based on Christian's original code which was already much improved (by Nils) as can be seen earlier on this page. Consequently your code has the same problems Christian's original had. Why not just show how the code above could be changed with your extension? (And note my remark above: "Sounds nice but would be workable only in a small Wiki.") --JavaWoman


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