New documents with eForm

on 15-Nov-2009 | Comments ( 11 ) Tags: Evolution

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

  • Required fields are marked with *.

If you have trouble reading the code, click on the code itself to generate a new random code.
 
john
Posts: 7
Comment
custom content form
Reply #11 on : Mon December 14, 2009, 12:01:21
how do i create custom content that have more than just one database field? when i learn drupal, there's a tutor about creating biography type of content that have year field, summary field, works field and some other field, can i do that in modx? can i add custom db tables and rows in modx? can you write some tutor on how to do that? thx
cipa
Posts: 4
Comment
Re: New documents with eForm
Reply #10 on : Mon December 14, 2009, 13:37:49
Hi John,

1. Create TVs for your document
2. Add some code after line 25

$customValues = array();
$customValues['tmplvarid'] = 5;//ID of the TV
$customValues['contentid'] = $newId; //The ID of the DOCUMENT
$customValues['value'] = "YOUR VALUE";
$modx->db->insert( $customValues, $modx->getFullTableName('site_tmplvar_contentvalues') );

The value you want to update/enter can come from the database , form etc
Last Edit: December 14, 2009, 13:38:12 by cipa  
john
Posts: 7
Comment
thx
Reply #9 on : Tue December 15, 2009, 11:38:36
nice... thank's for the tutor.
... still comparing drupal and modx, in drupal when you create a module that need some new tables from database, you can create an install routine and uninstall routine so if you activate/load the module those tables are created automaticly and if you deactivate/unload the module, those tables are deleted automaticly... from your previous tutor it seem's that if i need new tables for snippet or plugin in modx, i need to create it manually, and if i deactivate my plugin, i have to delete the table manually, is it correct? or modx have similar way to create tables (not adding data to tables)?
cipa
Posts: 4
Comment
Re: New documents with eForm
Reply #8 on : Tue December 15, 2009, 12:05:14
Hi John,

I'm using this to create/update documents from frontend using eForm.

In modx a document uses Template Variables that you need to create yourself or or modify the code above to create them for you.

I think your case is different. I recommend posting your needs on the forums. I'm sure you'll find ex-Drupal users that will give you a better solution that the one posted here. There might be a module/plugin that does it for you.

I'm glad you enjoyed the post.
john
Posts: 7
Comment
thx again
Reply #7 on : Tue December 15, 2009, 21:11:08
thank's again for your reply... i'm already registering myself in modx forum but i still haven't received approval from forum admin, that's why i'm asking around in blogs..
frank los
Posts: 7
Comment
combining this with web user register
Reply #6 on : Fri January 01, 2010, 15:27:06
I am wondering if you advise me in the thing I want to acomplish described in this thread
http://modxcms.com/forums/index.php?topic=43793.new;topicseen#new

Thanks, Frank
maerrry
Posts: 7
Comment
upload files
Reply #5 on : Wed January 06, 2010, 18:24:10
thanks for tutorial :)

I wonder if is there a way to allow users upload their files. May be you advise what to write here:

$customValues['value'] = "YOUR VALUE";
Vajira
Posts: 7
Comment
Template
Reply #4 on : Thu April 22, 2010, 06:33:22
how do i use $values['template'] to change the template of the new document. please help..

tnx
Vajira
cipa
Posts: 4
Comment
Re: New documents with eForm
Reply #3 on : Thu April 22, 2010, 06:35:09
Replace 4 in $values['template'] = 4 with the id of your template.
Marc
Posts: 7
Comment
Same thing but Chunks instead of Resources?
Reply #2 on : Mon June 14, 2010, 05:45:40
Can the same apply to adding data to a chunk rather than creating new resources?

I have a chunk that contains gig data for a band web site. I have a form to allow user to update gig listing but not sure how to proceed. Just want items added to chunk not new pages?

Thanks
cipa
Posts: 4
Comment
Re: New documents with eForm
Reply #1 on : Mon June 14, 2010, 08:18:26
Adding data to chunks it's a lot easier. Just get familiar to the modx DB structure and use a custom sql to update the value.

Quick modx Evolution Tags

  • cached [[snippet]] or uncached [!snippet!]
  • {{chunk}}
  • [+placeholder+]
  • [*resourceField/TV*]
  • [^timing^]
  • [~link~]

Quick modx Revolution Tags

  • cached [[snippet]] or uncached [[!snippet]]
  • cached [[$chunk]] or uncached [[!$chunk]]
  • [[+placeholder]]
  • cached [[*resourceField/TV]] or uncached [[!*resourceField/TV]]
  • no timing tag - must investigate
  • [[~link]]
  • [[++systemSetting]]
  • [[%languageStringKey]]
© modxRULES! 2009-2010