We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45617
    • 24 Posts
    I have got a very complicated form and I’m thinking about sending this form to clients email as a pdf/csv due to lucidity. Client after filling this form will receive a pdf (or csv) on its email account.
    OK, theoretically:

    1. Create new table in database
    2. Create php form where names of fields are field in new table (formitbuilder) ?

    … then, I don’t know how to save – read – convert - send data from db ? (Sure, there are formsave, formit2db and back, but that’s not my case, isn’t it?)

    Or, should I somehow avoid to database? There’s no need to save data into db – just create file and send it away…

    Hit me, please, with some theory.
    Thank you. Peter
      • 44580
      • 189 Posts
      Maybe use something like this to create the pdf : http://itextpdf.com/functionality. The source data would have to be xml or database according to the documentation.
        • 45617
        • 24 Posts
        Hello Gissirob,

        thank you for quick reaction. Frankly, I don't mind the price - but learning / studying something brand new (strange) is unlucky for me ... Don't get me wrong, I'd prefer PHP developing... Thank you for your time. Peter.
        • Take at look at this thread! I am looking for a similar solution:

          http://forums.modx.com/thread/47539/attachments-with-formit/?page=5#dis-post-500649

          Cheers
            Benjamin Davis: American web designer living in Munich, Germany and a MODX Ambassador. I am also co-founder of SEDA.digital, a MODX Agency.
            • 40045
            • 534 Posts
            you could also use mxformbuilder, it makes 1. creating the form easy and 2. saves every submission of the form automatically into a database, from there it's quite easy to generate a csv file or whatever you like...

            If you decide to try it like this, i could post the csvexport snippet I'm using!
              • 45617
              • 24 Posts
              Hello Exside,

              actually I would be grateful for csvexport snippet ... if you are offering to share ... One more reason to try mxformbuilder !!

              Thank you again,

              Greetings, Peter.
                • 40045
                • 534 Posts
                Here you go:

                <?php
                $packagename = 'mxformbuilder';
                
                if ( $debug = isset($debug) ? $debug : false ) { $modx->setLogLevel(modX::LOG_LEVEL_INFO); }
                
                $mxfb = $modx->getService($packagename,'mxFormBuilder',$modx->getOption('mxformbuilder.core_path',null,$modx->getOption('core_path').'components/' . $packagename . '/').'model/' . $packagename . '/', array());
                
                if ($mxfb instanceof mxFormBuilder && isset($_GET['export']) && $modx->user->isAuthenticated('mgr') ) {
                
                	$query = $modx->newQuery('forms');
                	$query->where(array('form_id' => $formid));
                	$form = $modx->getObject('forms', $query);
                
                	$submissions = $form->getMany('Submissions');
                
                	$headers = array('Date');
                	$fieldsarr = array();
                
                	$filename = str_replace(' ', '_' , strtolower($form->get('name'))) . '.csv';
                	$file = fopen($modx->getOption('assets_path') . '/site/media/media.export/' . $filename, 'w+');
                
                
                	foreach ($submissions as $submission) {
                		$date = array('Datum' => strftime('%d.%m.%Y %H:%M:%S', $submission->get('date')));
                		$submission = $submission->toArray();
                		$vals = json_decode($submission['vals'], true);
                		$fields = $vals['data'][0];
                		// digg out the date from the object and add it to the fields for the export...
                		$fields = $date + $fields;
                
                		foreach($fields as $key => $value) {
                			if(intval($key)) {
                				unset($fields[$key]);
                			} else {
                				// this helps against the encoding issues in excel
                				if ( is_array($value) ) {
                					$fields[$key] = strip_tags($value[0]);
                				} else {
                					$fields[$key] = strip_tags(iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $value));
                				}
                
                				// create the csv headers
                				$cleankey = strip_tags(iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $key));
                				if ( !in_array($cleankey, $headers) ) {
                					$headers[] = $cleankey;
                				}
                			}
                		}
                
                		// kill the last field as it is the submit button
                		$fieldsarr[] = $fields;
                	}
                
                	// headers are generated while iterating over the fields, so we need to prepend them to the $fieldsarr here to have them in the csv
                	array_unshift($fieldsarr, $headers);
                	foreach ( $fieldsarr as $line ) {
                		fputcsv($file, $line);
                	}
                	fclose($file);
                
                	
                	return '<a href="/assets/site/media/media.export/' . $filename" class="download">Download data as CSV file</a>';
                    
                	
                } else {
                	//return 'Sorry, you have to log in!';
                }
                


                I then put it in the form.wrap template for mxfb as [[!mxfb.csvexport? &formid=`[[+mxfb.form.form_id]]`]], you need to provide the ID of the form somehow to the snippet. If you then add a ?export at the end of the url the form is displayed on, the snippet generates a link to the csv file and shows it before the form (when you're logged into the manager).
                  • 45617
                  • 24 Posts
                  You're awesome .... I don't really know how to THANK YOU !! .... I'm about to go to holiday when I come back I'll try your useful code!!

                  Thank you again !!

                  Peter
                    • 40045
                    • 534 Posts
                    NP, if you have problems, just reply here, got E-Mail notifications on!