Revision [3324]

This is an old revision of WikiTemplate made by JavaWoman on 2004-12-16 16:35:26.

 

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.1
 * @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 *****

echo '<form action="" method="get">';
$result .= "<input type=\"hidden\" name=\"wakka\" value=\"".$this->MiniHref()."\">\n";
echo $result;
//echo  $this->FormClose();
echo '<table class="template" border="1">';
echo '  <tr>';
echo '      <th align=left>WikiPage to be templated (empty means this page):</th>';
echo '          <td><input type="text" name="from" size="37"></td>';
echo '  </tr>';
echo '  <tr>';
echo '      <th align=left>WikiPage to be created:</th>';
echo '          <td><input type="text" name="to" size="37"></td>';
echo '  </tr>';
echo '  <tr>';
echo '      <th align=left>Added note to your edit:</th>';
echo '          <td><input type="text" name="note" size="37"></td>';
echo '  </tr>';
echo '  <tr>';
echo '      <th align=left>Do you want to edit the new page when created?</th>';
echo '          <td>';
echo '          <table border="1">';
echo '              <tr>';
echo '                  <td><input type="radio" name="editoption" value="false" checked>No</td>';
echo '                  <td><input type="radio" name="editoption" value="true">Yes</td>';
echo '                  <td><input type="submit" name="create" value="Template it now"></td>';
echo '              </tr>';
echo '          </table>';
echo '          </td>';
echo '  </tr>';
echo '</table>';
echo '</form>';

$to = $_GET['to'];
If ($to<>"") {
    if ($this->ExistsPage($to))
    {
        $errormessage = "The destination page must not exist.";
        echo $errormessage;
        return;
    }
    elseif ($this->HasAccess("write", $to)) {

        // get and interpret parameters
        // overrride defaults with parameters provided (accept only valid values)
        $uFrom = $_GET['from'];
        if ($uFrom)
        {
            if ($this->ExistsPage($uFrom))
            {
                if ($this->HasAccess("read", $uFrom))
                {
                    $from = $uFrom;
                }
                else
                {
                    $errormessage = "You are not authorized to use the template page.";
                    echo $errormessage;
                    return;
                }
            }
            else
            {
                $errormessage = "The template page does not exist.";
                echo $errormessage;
                return;
            }
        }
        else
        {
            $from = $this->GetPageTag();
        }

        $uNote = $_GET['note'];
        if (uNote) $note = $uNote;
        else
        {
            $note = "Templated from ".$from;
        }

        $uEditoption = $_GET['editoption'];
        if (uEditoption != "") $editoption = $uEditoption;
        // ***** END (ACTION) PARAMETERS *****

        // ***** DERIVED VARIABLES *****
        $thepage=$this->LoadPage($from);
        if ($thepage) $pagecontent = $thepage["body"];
        // ***** END DERIVED VARIABLES *****
 
        // ***** OUTPUT SECTION *****
        $this->SavePage($to, $pagecontent, $note);
        echo "[[".$to." has been created from ".$from." and note was: ".$note." ]]";
        if ($editoption) $this->redirect(BASE_URL.$to."/edit");
 
        // ***** END OUTPUT SECTION *****

    } else
    {  
        $errormessage = "You are not authorized to create the destination page.";
        echo $errormessage;
        return;
    }
}
   
/**
    function IsWikiName($text) { return preg_match("/^[A-Z,ÄÖÜ][a-z,ßäöü]+[A-Z,0-9,ÄÖÜ][A-Z,a-z,0-9,ÄÖÜ,ßäöü]*$/", $text); }
 */


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


Poking through the code ... you are (also) grabbing the $to variable from a URL parameter; I don't undertand what the intention of that is or how to use it - can you give an example? --JavaWoman
Scratch that - I see you're using method GET in the form (POST is better and more secure); but I don't see you picking up the parameters from the action itself, or am I missing something again? --JavaWoman

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