View source for Action: TemplateInsert

#||
||
Compatible with: !!(green)**R6.1**!!
Current version: 0.3
Credits: ((user:AhA AhA))
|{{toc numerate=1}} ||
||#

provide some templates to your users to give them a head start in creating their wiki pages
import a bundle of page templates

##~{{template.insert}}##

**~/action/template.insert.php**
%%(php)
<?php

if (!defined('IN_WACKO'))
{
	exit;
}

/*
**
* 1. create a template
* 2. embrace the template, as if you would cite it (usage: <[define your template]>)
* 3. place this action beneath <[your template]> {{template.insert}}
*
* Displays a form to create a new page
* It first validates the form, then takes the above content between <[  ]>
* and puts it into db and then directs you to the newly created page using the header() function;
*
*/

$tpl->href		= $this->href();

if (isset($_POST['page']) && $_POST['page'] == 'result')
{
	$new_tag = (string) ($_POST['newpage'] ?? '');

	$this->sanitize_page_tag($new_tag);
	$tpl->tag		= $new_tag;

	if (!$new_tag)
	{
		// a valid name must be entered
		$tpl->message =  '<p class="error">' . $this->_t('InvalidNameError') . '</p>';
	}

	if (!preg_match('/^([' . self::PATTERN['TAG_P'] . ']+)$/u', $new_tag))
	{
		$this->set_message($this->_t('InvalidWikiName'));
	}

	// check reserved word
	if ($result = $this->validate_reserved_words($new_tag))
	{
		$this->set_message(Ut::perc_replace(
			$this->_t('PageReservedWord'),
			'<code>' . $result . '</code>'));
	}
	// check for existing page
	else if ($this->load_page($new_tag))
	{
		$tpl->message = Ut::perc_replace(
			$this->_t('AlreadyExists'),
			'<strong>' . $this->compose_link_to_page($new_tag, '', '') . '</strong>');
	}
	else
	{
		// Select content of page, where action is implemented,
		// but only content between "~<[" instead of "~]>" every other expression would do
		preg_match_all("/<\[(.*)\]>/us", $this->page['body'], $match);

		// in order to control output from $match, takes first match: var_dump($match); exit;
		$body		= $match[1][0];
		$edit_note	= '';

		// puts queried content "into new page"
		$this->save_page($new_tag, $body, '', $edit_note, false, 0, 0, 0, $this->page['page_lang'], false, false);

		$this->http->redirect($this->href('', $new_tag));
	}
}

%%

**~/action/template/template.insert.tpl**
%%
[ === main === ]
	[ ' message ' ]
	<h3>[ ' _t: CreateNewPage ' ] using the above template:</h3>
	<br>
	<form action="[ ' href ' ]" method="post" name="add_template">
		[ ' csrf: add_template ' ]
		<input type="hidden" name="page" value="result">
		<label for="create_page">[ ' _t: CreateNewPage ' ]:</label><br>
		<input type="text" id="create_page" name="newpage" value="[ ' tag | e attr ' ]" size="60" maxlength="255">
		<button type="submit" id="submit_template">[ ' _t: CreateButton ' ]</button>
	</form>
%%


**Variante von ((user:AhA AhA))**
  * Nur angemeldete Benutzer dürfen Seiten hinzufügen
  * Erstellt wird automatisch eine Unterseite des angemeldeten Benutzers
  * Schreibrecht nur beim Benutzer
  * Exisiert die Seite bereits, erscheint eine Fehlermeldung
  * Der header ist global
  * Gibt man in der Seitenvorlage (zum Beispiel am Schluss) ::: ein, werden diese drei Doppelpunkte bei der Übergabe durch vier Doppelpunkte ersetzt. Wenn der Benutzer seine neue Seite erstmals editiert und wieder speichert, werden die vier Doppelpunkte durch den Benutzernamen ersetzt -> backlink!
  * Wenn kein Seitentitel eingegeben wird, bleibt die Aufforderung, man solle einen Seitentitel eingeben

legacy code, incompatible with WackoWiki 6.x
%%(php) <?php 
if ($user = $this->GetUser()) {
/*
**
* 1. create a template(Mustervorlage)
* 2. embrace the template, as if you would cite it (usage: <[define your template]>)
* 3. place this action beneath <[your template]> {{template.insert}}
*
* Displays a form to create a new page
* It first validates the form, then takes the above content between <[  ]>
* and puts it into db and then directs you to the newly created page using the header() function;
*
* for function GetTranslation to work properly you need to put
* "CreateButton"=>"Create", ("CreateButton"=>"Translationtoyourlanguage",)
* "NewPage"=>"<b>Your PageName: </b>", ("CreateNewPage"=>"<b>Translation: </b>",)
* in /lang/wakka."yourlanguage".php file
*/
$path = $this->GetPagePath();
$path = strip_tags($path);
$seitentitel = $_POST["newpage"];
$path = str_replace( "?", "", $path);
$form = $this->FormOpen().
            
            "\n<input type=\"hidden\" name=\"page\" value=\"result\">".
            "\n<hr />".$this->GetTranslation("CreateNewPage")."Die Unterseite <b>".$this->GetUserName()."</b>"."/<input name=\"newpage\" value=\"".$_POST["newpage"]."\" type=\"text\" size=\"20\"/>".
        "<input type=\"submit\" value=\"".$this->GetTranslation("CreateButton")."\" <hr />".

        
$this->FormClose();

if (!$seitentitel) {
    
        // a valid name must be entered
echo $this->GetTranslation("CreateNewPage")."Füge eine Unterseite hinzu.<br />Wähle ein selbsterklärendes Wort ohne Umlaute und Sonderzeichen, z.B. <b>Musik</b>, <b>Hobby</b>, <b>Tiere</b> oder <b>Autos</b>";  
        echo $form;          
    }
    else
        

if ($_POST["page"]=="result") {
    
    $newpage = $this->GetUserName()."/".$_POST["newpage"];
    
     if (!$newpage) {
    
        // a valid name must be entered
        echo "<p class=\"error\">Bitte den Seitennamen eingeben</p>";    
        echo $form;    
    } else 
    
    if ($existingPage = $this->LoadPage($this->GetUserName()."/".$_POST["newpage"]))
    {
      echo "Eine Seite mit diesem Namen existiert bereits!<br />Bitte einen neuen Seitentitel eingeben:";
      echo $form;
      }
      
      else
    {
        
        // Select content of page, where action is implemented,
        
           $tag = $this->getPageTag();
         $sql = $this->LoadSingle("select body from ".$this->config["table_prefix"]."pages"." where tag='".quote($tag)."'");
         $sql = str_replace( ":::", "::::", $sql);
        //but only content between "~<[" instead of "~]>" every other expression would do
        $in=$sql["body"];
    preg_match_all("/<\[(.*)\]>/s",$in,$treffer);
        
    // in order to control output from $treffer: var_dump($treffer); exit;
         $body = $treffer[1][0];
        
// current user is owner; if user is logged in! otherwise, no owner.

       $write_acl = $this->GetUserName();
       $owner = $this->GetUserName();
       
//puts queried content "into new page"

       $this->Query("insert into ".$this->config["table_prefix"]."pages set ".
         ($comment_on ? "comment_on = '".quote($comment_on)."', " : "").
         ($comment_on ? "super_comment_on = '".quote($this->NpjTranslit($comment_on))."', " : "").
         "time = now(), ".
         "owner = '".quote($owner)."', ".
         "user = '".quote($owner)."', ".
         "latest = 'Y', ".
         "supertag = '".quote($this->NpjTranslit($newpage))."', ".
         "body = '".quote($body)."', ".
         "body_r = '".quote($body_r)."', ".
         "body_toc = '".quote($body_toc)."', ".
         "lang = '".quote($lang)."', ".
         "tag = '".quote($newpage)."'");
         
$this->SaveAcl($newpage, "write", ($comment_on ? "" : $write_acl));

// here the script is not quite ready, validation not complete, when accidentally overwriting an existing page
/* Redirect browser */

header('Location:' . $this->config["base_url"] . $newpage);

/* Make sure that code below does not get executed when we redirect. */
exit;

    }    
} else {
    echo $form;
}
}
else
echo "<p class=\"error\">Bitte einloggen! Nur angemeldete Benutzer / Benutzerinnen können Seiten hinzufügen."

?>  %%

===Documentation===
for users available for copy and paste
((http://www.eonywiki.de/Mustervorlagen/E/EinstellenVonPublikationen demo))

===How to===
====Localization (optional)====
put this at the end of your language file 
%%
'CreateButton' => 'Create',
'CreateNewPage' => '<b>Create NewPage: </b>', 
%%
**lang/custom.en.php**
%%
//hacks
'YourPhrase' => 'your translation here',
%%

**[xyz] proposed translations**
[de] ...
[fr]
[ru]

===ToDo===
  * acl, user check

===Changelog===
  * 0.3 - fixed the action to work with 6.0 and the template engine

===See also===
  * ((/Dev/Release/R6.1/TemplateBoxes Template Boxes))

{{backlinks}}