New documents with eForm
Sometimes you might need to allow your web users to create new documents using the front-end of your website. Adding values to Template Variables will be cover in a future tutorial.
1.eForm chunks
For simplicity the only fields filled via eForm will be the title and the content.
a. The eForm template
Create a new chunk and name it eFormTpl
[+validationmessage+]
<form method="post" action="[~[*id*]~]" enctype="multipart/form-data">
<input type="hidden" name="formid" value="newResourceForm" />
Title :<br/><input type="text" name="title" size="40" maxlength="40" eform="Title::1" />
<br/><br/>
Content:<br/>
<textarea cols="40" rows="3" name="content" eform="Content::1"></textarea>
<br/><br/>
<input type="submit" name="submit" value="Submit">
</form>
b. The eForm report template
Create a new chunk and name it eFormTplReport. This will be the template of the email you'll receive once the document is created.
<p>The following resource has been posted [+title+]</p>
c. The eForm thanks template
Create a new chunk and name it eFormTplThanks. This will be the thank you message the user will see after submit.
<p>Thank You! Your document has been created</p>
2. Snippet to handle the resource creation
Create a new snippet createNewResource and modify it to your needs.
<?php
function createNewResource( &$fields ){
global $modx;
$values = array();
$values['pagetitle'] = $modx->db->escape($fields['title']);
$values['longtitle'] = '';
$values['description'] = '';
$values['alias'] = '';
$values['published'] = 1; //0 - if you want the document to be created unpublished
$values['parent'] = 0; //id of the container for the the new document
$values['introtext'] = '';
$values['content'] = $modx->db->escape($fields['content']);
$values['template'] = 4; //the template of your new document
$values['searchable'] = 1;
$values['cacheable'] = 1;
$values['createdby'] = 1; //this is not the web user ID. Change it to an existing manager id
$values['createdon '] = time();
$values['editedby'] = 1; //this is not the web user ID. Change it to an existing manager id
$values['editedon '] = time();
//you can find more document properties by browsing the modx_site_content table
//insert the new document. In case you need the id of the new document use the $newId variable
$newId = $modx->db->insert( $values, $modx->getFullTableName('site_content') );
//clear the cache. in case you use Ditto to display the new document
clearCache();
return true;
}
function clearCache() {
include_once "manager/processors/cache_sync.class.processor.php";
$sync = new synccache();
$sync->setCachepath("assets/cache/");
$sync->setReport(false);
$sync->emptyCache();
}
?>
3. The eForm and snippet call
Now it's time to add the snippet call and eForm call to your document. I reccomend using a minitexarea to paste the bellow calls. If you are pasting it into the Content area do not forget to wrap everything in a div.
[!createNewResource!] [!eForm? &formid=`newResourceForm` &tpl=`eFormTpl` &report=`eFormTplReport` &to=`YOUR EMAIL ADDRESS!` &thankyou=`eFormTplThanks` &subject=`New resource` &eFormOnBeforeMailSent=`createNewResource`!]
The snippet has to be called first, as it holds the function eForm will call before the email/notification is sent.
Thoughts and ideas are welcomed.
Write a comment
Posts: 7
Reply #11 on : Mon December 14, 2009, 12:01:21
Posts: 4
Reply #10 on : Mon December 14, 2009, 13:37:49
Posts: 7
Reply #9 on : Tue December 15, 2009, 11:38:36
Posts: 4
Reply #8 on : Tue December 15, 2009, 12:05:14
Posts: 7
Reply #7 on : Tue December 15, 2009, 21:11:08
Posts: 7
Reply #6 on : Fri January 01, 2010, 15:27:06
Posts: 7
Reply #5 on : Wed January 06, 2010, 18:24:10
Posts: 7
Reply #4 on : Thu April 22, 2010, 06:33:22
Posts: 4
Reply #3 on : Thu April 22, 2010, 06:35:09
Posts: 7
Reply #2 on : Mon June 14, 2010, 05:45:40
Posts: 4
Reply #1 on : Mon June 14, 2010, 08:18:26