New documents with eForm

on 15-Nov-2009 | Comments ( 27 ) 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: 18
Comment
custom content form
Reply #27 on : Mon December 14, 2009, 11: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: 9
Comment
Re: New documents with eForm
Reply #26 on : Mon December 14, 2009, 12: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, 12:38:12 by cipa  
john
Posts: 18
Comment
thx
Reply #25 on : Tue December 15, 2009, 10: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: 9
Comment
Re: New documents with eForm
Reply #24 on : Tue December 15, 2009, 11: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: 18
Comment
thx again
Reply #23 on : Tue December 15, 2009, 20: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: 18
Comment
combining this with web user register
Reply #22 on : Fri January 01, 2010, 14: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: 18
Comment
upload files
Reply #21 on : Wed January 06, 2010, 17: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: 18
Comment
Template
Reply #20 on : Thu April 22, 2010, 05:33:22
how do i use $values['template'] to change the template of the new document. please help..

tnx
Vajira
cipa
Posts: 9
Comment
Re: New documents with eForm
Reply #19 on : Thu April 22, 2010, 05:35:09
Replace 4 in $values['template'] = 4 with the id of your template.
Marc
Posts: 18
Comment
Same thing but Chunks instead of Resources?
Reply #18 on : Mon June 14, 2010, 04: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: 9
Comment
Re: New documents with eForm
Reply #17 on : Mon June 14, 2010, 07: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.
Steve
Posts: 18
Comment
Some questions
Reply #16 on : Tue October 12, 2010, 07:10:29
Hi, I'm using this code for a site I'm building where the user will be able to post ads. I have it working like the example plus with my own TV's, but there's 2 more things I'd like to be able to do:

1) Instead of immediately creating the document after clicking the 'submit' button then showing the thank you message, I would like to first show a page that displays a preview of the content, then from that page is where they confirm and submit the document. Any ideas on how to do this?

2) The goal of the site is for it to be an e-commerce site where users pay to place ads. I am considering using Foxycart software. Not sure if you have experience with Foxycart but I'd like to set it up so the document is not created until the user has completed the checkout process?

Thanks for the great articles. Hope you or anyone else can provide some help.

Steve
cipa
Posts: 9
Comment
Re: New documents with eForm
Reply #15 on : Wed October 13, 2010, 13:41:04
1. You can use javascript. Add a Preview button, hide the form and use the values in the form to generate some html; replace Preview button with Submit button

2. Unless FoxyCart provides some sort of callback or API that you can check I don't see a way to do what you want. Maybe you'll have more luck on their forum
Steve
Posts: 18
Comment
Re: New documents with eForm
Reply #14 on : Thu October 21, 2010, 05:43:43
Hi cipa

I don't suppose you could provide some code examples for my first question? I'm a bit lost when it comes to Javascript...

Thanks,
Steve
Anonymous
Posts: 18
Comment
Re: New documents with eForm
Reply #13 on : Thu October 21, 2010, 05:53:09
This should get you started.
http://papermashup.com/create-a-dynamic-form-preview/

or just google "javascript form preview". If you use a js framework add the name in the search.
Stuart
Posts: 18
Comment
Error with this script
Reply #12 on : Sat January 01, 2011, 21:37:50
Hi Guys,

When I try ti implement this im getting the following error. Any ideas why? Tried everything even upgraded Modx to 1.0.4


Fatal error: Call to undefined function createNewResource() in /nfs/c05/h01/mnt/82451/domains/exampleDomain.com/html/assets/snippets/eform/eform.inc.php on line 378
Stuart
Posts: 18
Comment
Alteration to this script
Reply #11 on : Sun January 02, 2011, 13:32:44
Hi guys,

Trying to make a basic alteration to this scrip so it has a parameter to define the post folder when you call the snippet!

Any assistance appreciated.

http://modxcms.com/forums/index.php?topic=59261.new;topicseen#new
Natalia
Posts: 18
Comment
alias
Reply #10 on : Fri May 20, 2011, 03:30:31
Script works great. Thanks.

Any ideas how can I generate alias form my new document?
cipa
Posts: 9
Comment
Re: New documents with eForm
Reply #9 on : Fri May 20, 2011, 07:12:40
$values['alias'] = 'Your alias'; should work
Mike
Posts: 18
Comment
Re: New documents with eform
Reply #8 on : Tue November 22, 2011, 08:31:24
I like your site - most helpful.

I am hoping to use this with webusers so that they can create new documents. I have been able to create new documents with the webuser's id in the "createdby" field. I now want to let the webuser update / delete the documents they have entered.

Do you have any scripts for this or can you let me know the best way to do this?

Thanks
cipa
Posts: 9
Comment
Re: New documents with eForm
Reply #7 on : Tue November 22, 2011, 08:51:48
It is possible but it is complex. I don't have any scripts that could help you.
1. I would use WebLogin to login the user and redirect him to a page.
2. the redirect page should have a list of documents he has access to (can edit, delete etc)
3. When a user clicks to edit a document, you open an edit page with eForm on it. The eForm fields will be prepopulated with the values in the modx resource(content, pagetitle etc)
4. on save update the modx resource and clear cache

I heard NewsPublisher can do this. Maybe you can have a look at it.
Last Edit: November 22, 2011, 08:53:13 by cipa  
Mike
Posts: 18
Comment
Re: New documents with eform
Reply #6 on : Tue November 22, 2011, 09:08:17
Thanks for the speedy reply, I shall explore further.
You mentioned NewsPublisher could help - will PubKit work as well?

Thanks
cipa
Posts: 9
Comment
Re: New documents with eForm
Reply #5 on : Tue November 22, 2011, 09:13:24
I am not familiar with any of those
Brett
Posts: 18
Comment
Quick Question
Reply #4 on : Fri March 23, 2012, 05:00:20
Great snippet. I've combined this with TinyMCE on the front-end, but the only problem is when it gets inserted into the db, it strips the HTML formatting.

Is there any way around this that you know of?
cipa
Posts: 9
Comment
Re: New documents with eForm
Reply #3 on : Fri March 23, 2012, 11:52:30
Try &allowhtml=`1` in the eForm call
Thanks
Anonymous
Posts: 18
Comment
Re: New documents with eForm
Reply #2 on : Fri May 04, 2012, 03:48:45
rthrt
Steve
Posts: 18
Comment
REvo
Reply #1 on : Fri March 22, 2013, 23:41:55
Will this work with Revo?

Quick modx Evolution Tags

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

Quick modx Revolution Tags

  • [[snippet]]
  • [[$chunk]]
  • [[+placeholder]]
  • [[*resourceField/TV]]
  • [[~link]]
  • [^timing^]
  • [[++systemSetting]]
  • [[%languageStringKey]]
  • all tags can be called un-cached like: [[! snippet]]

Timing Tags (Evo and Revo)

  • [^qt^] - Query time
  • [^q^] - Query count
  • [^p^] - Parse time
  • [^t^] - Total time
  • [^s^] - Source
© modxRULES! 2009-2014