Revision [3641]

This is an old revision of CloneHandler made by ChristianBarthelemy on 2004-12-24 18:25:04.

 

Cloning an existing WikiPage

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

Two solutions

Either an action CloneAction or this CloneHandler.
When the action was first published, DarTar explained that a handler would make more sense. It certainly is the right way to do so here is the handler.

Dependancy

This relies of the ExistsPage function developed by JavaWoman and part of release 1.1.6.0. You can find this version at WikkaDevelopment.

The code

Copy this code into a handler named: clone.php - place it in the handlers/page folder.
<div class="page">
<?php
/**
 * Clone a "template" page to a new named page
 *
 * Usage: type in /clone at the end of the URL you want to clone
 *
 * This handler checks 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         Handlers
 * @subpackage        
 * @name              clone
 *
 * @author            {@link http://wikka.jsnx.com/ChristianBarthelemy Christian Barthelemy}
 * @version           0.2
 * @since             Wikka 1.1.6.x
 *                      
 * @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 *****

if ($_POST)
{
    // ***** GET PARAMETERS *****
    $from = $this->GetPageTag();
    $to = $_POST["to"];
    $note = $_POST["note"];
    $editoption = $_POST["editoption"];
    // ***** END PARAMETERS *****

    // ***** CLONING 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.";
            $this->SetMessage($errormessage);
            $this->Redirect($this->Href());
        }
        // 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)
            {
                // the page to be duplicated 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.";
                        $this->SetMessage($errormessage);
                        $this->Redirect($this->Href());
                    }
                }
                else
                {
                    // this is what happens if the user tries to duplicate a non existing page...
                    $errormessage = "The template page does not exist.";
                    $this->SetMessage($errormessage);
                    $this->Redirect($this->Href());
                }
            }
            // 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...
            $message=$to." has been created from ".$from." and note was: ".$note;
            if ($editoption)
            {
                $this->redirect(BASE_URL.$to."/edit"); // drops the user to the new page for edition if he choosed so...
            }
            else
            {
                $this->SetMessage($message);
                $this->Redirect($this->Href()); // ...or back where he comes from.
            }
            // ***** 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.";
            $this->SetMessage($errormessage);
            $this->Redirect($this->Href());
        }
    }
    // ***** END TEMPLATING PROCESS *****
}
else
{
    // ***** INPUT FORM *****
    print $this->FormOpen("clone");
    echo '<table class="clone" border="1">';
    echo '  <tr>';
    echo '      <th align="left">WikiPage to be created:</th>';
    echo '          <td><input type="text" name="to" size="37">'.$to.'</td>';
    echo '  </tr>';
    echo '  <tr>';
    echo '      <th align="left">Added note to your edit:</th>';
    echo '          <td><input type="text" name="note" size="37">'.$note.'</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="checkbox" name="editoption" value="false">Edit after creation'.$editoption.'</td>';
    echo '                  <td><input type="submit" name="create" value="Clone it now"></td>';
    echo '              </tr>';
    echo '          </table>';
    echo '          </td>';
    echo '  </tr>';
    echo '</table>';
    print $this->FormClose();
    // ***** END INPUT FORM *****
}
?>
</div>


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 type in /clone at the end of its URL.
Then you must fill the name of the new WikiPage to be created, 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 handler more than welcome.

All discussions on this page still are at WikiTemplate. We definitely need a PageNameChange handler ;-)
See MovePages - not implemented, it has its problems; but the wish/requirement isn't new ;-) --JavaWoman


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