Topic: [Snippet] eForm2db 0.1BETA - Manipulate DB records via eForm and MODx DBAPI  (Read 72572 times)

Pages: [1] 2 3 ... 10   Go Down

#1: 19-Oct-2006, 07:21 PM

Coding Team

pixelchutes
Posts: 889

WWW
 
   Capture, store and process validated form submission with E’s ...eForm + eForm2db Wink



         eForm2db
         Version: 0.1 [Beta 1]
         Author: pixelchutes

         Requirements:
            eForm 1.4+

         Use:
  • Takes validated eForm submittal data for use with the MODx DBAPI extender class.
    Reference: /manager/includes/extenders/dbapi.mysql.class.inc.php
  • Easily process database records referencing your form submission data:
  • INSERT new records to a table of your choice
  • UPDATE existing records if you'd prefer
  • DELETE records based on form submittal criteria
  • Form a query and return its record set as XML using $modx->db->getXML
  • Query a table's MetaData using $modx->db->getTableMetaData, followed by an update query on only the form fields that are named after table columns!
  • and much more!

The eForm2db snippet:
Code:
<?php
function 
eForm2db( &$fields )
{
/*---------------------------------------------------------------
eForm2db 
Version: 0.1 [Beta 1]
Author: pixelchutes
---------------------------------------------------------------
Requirements:
eForm 1.4+
---------------------------------------------------------------
Use:
- Takes validated eForm submittal data for use with the MODx DBAPI extender class.
- Easily process database records referencing your form submission data:   
  + INSERT new records to a table of your choice   
  + UPDATE existing records if you'd prefer   
  + DELETE records based on form submittal criteria   
  + Form a query and return its record set as XML using $modx->db->getXML   
  + Query a table's MetaData using $modx->db->getTableMetaData, followed by an update query
    on only the form fields that are named after table columns!     
  + and much more! 
---------------------------------------------------------------*/

// Bring needed resources into scope
global $modx$table_prefix;

// Init our array
$dbTable = array(); // key = DB Column; Value = Insert/Update value

// Insert field/value pairs to insert/update in our table
$dbTable[name] = $fields[first_name].' '.$fields[last_name]; // Merge two form fields together
$dbTable[datetime] = date'YmdHis'strtotime$fields[postdate] ) ); // Massage the postdate timestamp to be MySQL insert friendly


// INSERT - $dbQuery = $modx->db->insert( $dbTable, $table_prefix . 'insertTableName' );
// UPDATE - $dbQuery = $modx->db->update( $dbTable, $table_prefix . 'updateTableName' );
// DELETE - $dbQuery = $modx->db->delete( $table_prefix . 'deleteFromTableName', 'some_field = 1 AND name=\''.$dbTable[name].'\'', '' );
// etc...

// Run the db insert query
$dbQuery $modx->db->insert$dbTable$table_prefix 'insertTableName' );

return true;
}
// Return empty string
return '';
?>


Implementation:

Add the snippet call before your eForm snippet call:
Code:
[[eForm2db]]

Utilize eForm's "callback functions" in your eForm snippet call:
Code:
[!eForm? &noemail=`true` &formid=`contact` &eFormOnBeforeMailSent=`eForm2db` &tpl=`contactform` &thankyou=`contactthanks` !]

NOTES:
- &noemail param is optional. Here, I have used this call solely as a db capture form. Since no email is sent, there is no need to define things like: to, from, report, subject, etc. Simply disable mailing, and use the callback function to process your form's validated fields!

- This is my first attempt at giving back to this community. MODx is definitely the next level. While I realize eForm2db is not modular in any way-shape-form, it is indeed its direction. What do you think? Would this make a better Module? Plugin? etc... What direction should we take this to allow modular, customizable processing of eForm submittal?

More on the DBAPI:
http://www.modxcms.com/dbapi.html
« Last Edit: 30-Aug-2007, 05:20 PM by pixelchutes »
Mike Reid - www.pixelchutes.com
MODx Team Member / Contributor
[Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
________________________________
Where every pixel matters.

#2: 19-Oct-2006, 08:05 PM


kudolink
Posts: 243

MODx Entusiast!

WWW
 Shocked

As soon as I start believing this is true I'll have other words.

Sincerely this is a post to have this thread in my "Show new replies to your posts" page. But this is very, very very interesting, truly.
kudo
www.kudolink.com - webdesign (surprised?)

  proudly uses 

#3: 19-Oct-2006, 10:19 PM

Marketing & Design Team

davidm
MODx evangelist
Posts: 7,073

The best way to predict the future is to invent it

WWW
Indeed !

Really this really would take eForm to the next level... Not qualified to give you input as to which direction you should take, but it's really impressive and I am eager to see it in action...

Bookmarking this thread also Smiley
« Last Edit: 19-Oct-2006, 10:22 PM by davidm »
.: nodeo.net : Pour un web libre, moderne et ouvert ! :: david-molliere.net : Suivez en "live" mes expérimentations et billets sur les CMS et autres applications web :.

*** Forums modxcms.fr Participez à l'élaboration du site MODx francophone ! ***

! Nouveau !  En live, ne manquez pas les news de modxcms.fr sur Twitter   ! Nouveau !

MODx est l'outil idéal pour les developpeurs et webdesigners qui cherchent un framework de gestion de contenu hautement flexible et performant, tout en étant simple d'accès pour les utilisateurs finaux.

Config : Apache 2.2.8 - MySQL 5.0.67 - PHP 5.2.8 | Debian 4.0 (Etch)

Réalisations sous MODx : | pargade-notaires.fr | soleil.info | gican.asso.fr | michelez-notaires.com | amadom.gerondicap.com | jocelyne-violet.net

#4: 19-Oct-2006, 10:29 PM

Coding Team

sottwell
Posts: 10,530

WWW
Thank you! I've been meaning to dig into eForm to figure out how to use those two events, now I see how easy it is!

Now all we need is another event in eForm, onBeforeFormLoad, where we can grab data from a database to populate the form fields with for profile editing!
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html

#5: 19-Oct-2006, 10:52 PM

Coding Team

pixelchutes
Posts: 889

WWW
@kudo/david/susan-- Thank you all for your interest, as well as your contributions!

I'd also like to give a hand out to the developer(s) behind the DBAPI. Once I realized this piece was already in place, I couldn't believe how easy it really was.

As you can see, a quick array and you're inserting/deleting/updating in no time! No connection logic, etc. Wink

Ya know, I like the onBeforeFormLoad idea...it actually would be quite easy. Thanks, Toby!


« Last Edit: 17-Jan-2007, 05:13 PM by pixelchutes »
Mike Reid - www.pixelchutes.com
MODx Team Member / Contributor
[Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
________________________________
Where every pixel matters.

#6: 19-Oct-2006, 11:03 PM

Coding Team

sottwell
Posts: 10,530

WWW
Quote
Ya know, I like the onBeforeFormLoad idea...it actually would be quite easy.
However, I think TobyL's intentions may have been to keep [ eForm <-> "DB Stuff" ] separate to reduce "bloated code"? Especially since his callback functions allow for it the way I've illustrated here.

Indeed, and I agree with the idea of keeping eForm itself clean. That's why I was thinking another event, so the database (or whatever... this could be used to load the $fields array with SESSION values, even) results could get loaded into the $fields array, but the developer would make his own snippet to do that, just as you have done.
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html

#7: 19-Oct-2006, 11:19 PM

Coding Team

pixelchutes
Posts: 889

WWW
Indeed, and I agree with the idea of keeping eForm itself clean. That's why I was thinking another event, so the database (or whatever... this could be used to load the $fields array with SESSION values, even) results could get loaded into the $fields array, but the developer would make his own snippet to do that, just as you have done.

I believe the form is not fully parsed until after it's first submission...wait, that can't be true since the parser DOES remove all eform="" tags...

In that case, we might need to create db2eForm Cheesy
Mike Reid - www.pixelchutes.com
MODx Team Member / Contributor
[Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
________________________________
Where every pixel matters.

#8: 19-Oct-2006, 11:25 PM

Coding Team

sottwell
Posts: 10,530

WWW
Exactly! But we need to be able to get that data before the form is parsed, so the data will get inserted as the form is being parsed. That means another event, so we can (with our db2eForm snippet) go get the data and tell eForm to insert it.

I did something similar, getting data from the SESSION and loading it into the $fields array, but I hacked eForm to do it. Much better to have an event to work with.
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html

#9: 19-Oct-2006, 11:44 PM

Coding Team

pixelchutes
Posts: 889

WWW
Shocked

As soon as I start believing this is true I'll have other words.

Sincerely this is a post to have this thread in my "Show new replies to your posts" page. But this is very, very very interesting, truly.

BTW, believe it! I was up and running in 10 minutes and was inserting and deleting db records Smiley
Your imagination is really the limit with this one. Just a nudge in the right direction... Wink
Mike Reid - www.pixelchutes.com
MODx Team Member / Contributor
[Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
________________________________
Where every pixel matters.

#10: 20-Oct-2006, 08:57 AM

Emeritus
xwisdom
Posts: 1,732

Very nice pixelchutes! Man you're light years ahead Smiley

Some DB features are on the to-do list for version 3.0 but it's nice to see that you have gotten a good head start with the built-in events

Here's one way I'm planning on adding support for data columns (or content fields) in 2.0 that you might find interesting:

New efield="column:datatype:default" attribute
Quote
<input name="name" type="text" eform="Your name:string-50:1" efield="fname:string:" emessage="You must enter a name" />

Note here that the -50 represents the length of the field. More on this in time to come.
xWisdom
www.xwisdomhtml.com
The fear of the Lord is the beginning of wisdom:
MODx Co-Founder - Create and do more with less.

#11: 20-Oct-2006, 09:24 AM

Coding Team

pixelchutes
Posts: 889

WWW
New efield="column:datatype:default" attribute
Quote
<input name="name" type="text" eform="Your name:string-50:1" efield="fname:string:" emessage="You must enter a name" />

Note here that the -50 represents the length of the field. More on this in time to come.

Thank you! This sounds like some very cool stuff. I am anticipating seeing/using the new methods. Can't you just feel the buzz?
Mike Reid - www.pixelchutes.com
MODx Team Member / Contributor
[Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
________________________________
Where every pixel matters.

#12: 20-Oct-2006, 11:42 AM

Coding Team

pixelchutes
Posts: 889

WWW
Query a table's MetaData using $modx->db->getTableMetaData, followed by an update query on only the form fields that are named after table columns!




In this quick example, I grab the columns from our `leads` table, and for any form field submitted that matches a column name in the table, a definition is defined for that column, using the validated, submitted form value.
(NOTE: I am not passing {PREFIX} because I don't use one here)

Code:
// Grab our table's columns
$tblColumns = array_keys( $modx->db->getTableMetaData( 'leads' ) );
// Loop through each column, and insert it if present in the form
foreach( $tblColumns as $tblCol )
if( isset( $fields[ $tblCol ] ) ) $dbTable[ $tblCol ] = $fields[ $tblCol ]

However, my contact form asks for both Best Time to Call and whether in AM or PM. I also need to convert the time they submitted the form to be MySQL-insert-friendly.

So, I handle any custom processing immediately after:

Code:
// Custom processing (e.g. date values, field joining, PHP logic, etc)
// Insert field/value pairs to insert/update in our table
$dbTable[datetime] = date( 'YmdHis', strtotime( $fields[postdate] ) );
$dbTable[call_time] = $fields[time] . ' ' . $fields[amorpm];

And then, simply invoke the insert SQL (as in my original example):

Code:
$dbQuery = $modx->db->insert( $dbTable, 'leads' );

And here is the result...eForm records right in the db!

« Last Edit: 20-Oct-2006, 11:49 AM by pixelchutes »
Mike Reid - www.pixelchutes.com
MODx Team Member / Contributor
[Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
________________________________
Where every pixel matters.

#13: 22-Oct-2006, 01:22 AM

Coding Team

TobyL
Posts: 1,024

@pixelchutes

I was really hoping this would happen... This is truly great stuff.

Quote
However, I think TobyL's intentions may have been to keep [ eForm <-> "DB Stuff" ] separate to reduce "bloated code"? Especially since his callback functions allow for it the way I've illustrated here.

Yes, that was indeed my intention but an extra event wouldn't ga amiss I think. Once I've cleaned up the niggly bits you and others have pointed out in eForm I'll have a look at other possible events.

« Last Edit: 22-Oct-2006, 01:28 AM by TobyL »

#14: 22-Oct-2006, 06:44 AM

Coding Team

TobyL
Posts: 1,024

Oh and I just noticed this one:

Quote
I believe the form is not fully parsed until after it's first submission...wait, that can't be true since the parser DOES remove all eform="" tags...
In that case, we might need to create db2eForm Cheesy

The form template is indeed not fully parsed until after the first form submission (would be pretty useless as it's meant for parsing data for validation at the moment).  It does merge any placeholders for which it finds a value in the $fields array. The eform pseudo-attributes are simply removed with a regular expression.

#15: 22-Oct-2006, 02:04 PM

Coding Team

pixelchutes
Posts: 889

WWW
@TobyL--

Thank you for clarifying this. Definitely points out that an event such as onBeforeFormLoad Would be pretty useful.

What if you built the parser to capture from <form id="eFormID" ... to </form> and maybe pass that as an argument to be referenced in the user function. They could setPlaceholders from db, etc, whatever they'd like really, then return the modified string-HTML. Is this the idea?


Mike Reid - www.pixelchutes.com
MODx Team Member / Contributor
[Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
________________________________
Where every pixel matters.

#16: 22-Oct-2006, 07:47 PM

Coding Team

TobyL
Posts: 1,024

Thank you for clarifying this. Definitely points out that an event such as onBeforeFormLoad Would be pretty useful.

What if you built the parser to capture from <form id="eFormID" ... to </form> and maybe pass that as an argument to be referenced in the user function. They could setPlaceholders from db, etc, whatever they'd like really, then return the modified string-HTML. Is this the idea?

Working on it...  See this post.

#17: 23-Oct-2006, 10:55 AM

Coding Team

pixelchutes
Posts: 889

WWW
Working on it...  See this post.

Woo hoo! This is getting good! In the post you reference, you mentioned correction of "some small bug fixes" ...mind sharing high-level, is it anything to be concerned about?
« Last Edit: 23-Oct-2006, 11:33 AM by pixelchutes »
Mike Reid - www.pixelchutes.com
MODx Team Member / Contributor
[Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
________________________________
Where every pixel matters.

#18: 23-Oct-2006, 11:27 AM

Coding Team

TobyL
Posts: 1,024

Woo hoo! This is getting good! in the post you reference, you mentioned correction of "some small bug fixes" ...mind sharing high-level, is it anything to be concerned about?
# Fixed: Missing language file produces fatal error. (Will now generate a debug message if debug is on)
# Fixed: Missing 'replyto' in AddAddressToMailer function.
# Fixed: Erroneous closing slash (/) being added in <option ..> tags
# Fixed: Missing whitespace before 'selected' and 'checked' attributes

#19: 29-Oct-2006, 11:40 PM

TheBear
Posts: 16

I'd love to see this thing in action! Is there an example out there?

#20: 30-Oct-2006, 10:23 AM

Coding Team

pixelchutes
Posts: 889

WWW
I'd love to see this thing in action! Is there an example out there?

@TheBear,

Unfortunately, my only active example is on my local testing machine. However, if you have MODx installed, with the most recent eForm snippet, you can follow my example in the very first post to customize mapping of validated form data into the DB.

Another post in this same thread shows a screen shot of captured data in a MySQL GUI.
Mike Reid - www.pixelchutes.com
MODx Team Member / Contributor
[Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
________________________________
Where every pixel matters.
Pages: [1] 2 3 ... 10   Go Up
0 Members and 1 Guest are viewing this topic.