Last active
June 27, 2023 14:57
-
-
Save NatemcM/6a286295f83864e15a6189e7ecddf788 to your computer and use it in GitHub Desktop.
Formit csv export
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Edited GIST based on: https://community.modx.com/t/better-way-to-access-formit-form-data/6828 | |
/** | |
* Snippet to get formit forms by name | |
* usage: | |
* [[!getFormitForm? &name=`Name of form`]] | |
*/ | |
/** | |
If you want to use this with MODX 3 replace lines 35 to 51 with | |
use Sterc\FormIt\Model\FormItForm; | |
// Load formit class | |
$formit = $modx->getService('formit', 'FormIt', $modx->getOption('formit.core_path', null, $modx->getOption('core_path') . 'components/formit/') . 'model/formit/'); | |
// Load PDO Tools | |
$pdo = $modx->getService('pdoTools'); | |
// set snippet props | |
$formName = $modx->getOption('name', $scriptProperties, null); | |
// xpdo query to get forms | |
$q = $modx->newQuery(FormItForm::class); | |
$q->where([ | |
'form' => $formName, | |
]); | |
$total = $modx->getCount(FormItForm::class, $q); | |
$forms = $modx->getCollection(FormItForm::class, $q); | |
**/ | |
// Load formit class | |
$formit = $modx->getService('formit', 'FormIt', $modx->getOption('formit.core_path', null, $modx->getOption('core_path') . 'components/formit/') . 'model/formit/'); | |
// Load PDO Tools | |
$pdo = $modx->getService('pdoTools'); | |
// set snippet props | |
$formName = $modx->getOption('name', $scriptProperties, null); | |
// xpdo query to get forms | |
$q = $modx->newQuery('FormItForm'); | |
$q->where([ | |
'form' => $formName, | |
]); | |
$total = $modx->getCount('FormItForm', $q); | |
$forms = $modx->getCollection('FormItForm', $q); | |
if (!empty($forms)) { | |
// Set our inner tpl var | |
$dataOutput = ''; | |
// Loop through each object and tpl up | |
foreach ($forms as $form) { | |
$fields = $form->toArray(); | |
// Check if data is encrypted | |
if ($form->get('encrypted')) { | |
// Form is encrypted, use the objects decrypt method to decyrpt | |
$fields['values'] = $form->decrypt($form->get('values')); | |
} | |
// $fields['values'] contains the JSON array to access pass to json_decode() ie; | |
$values = json_decode($fields['values'], true); | |
$fields['fields'] = ''; | |
// then access like: $values['firstName'] | |
foreach ($values as $key => $value) { | |
$fields['fields'] .= "<strong>$key:</strong> $value<br>"; | |
} | |
// Format the date string | |
$fields['date'] = date('Y-m-d', $fields['date']); | |
// Tpl up our form | |
$dataOutput .= $pdo->getChunk( | |
"@INLINE <tr> | |
<td>[[+id]]</td> | |
<td>[[+form]]</td> | |
<td>[[+ip]]</td> | |
<td>[[+date]]</td> | |
<td>[[+hash]]</td> | |
<td>[[+fields]]</td> | |
</tr>", | |
$fields | |
); | |
} | |
// Set wrapper tpl | |
$output = $pdo->getChunk("@INLINE <table width='100%'> | |
<thead> | |
<tr> | |
<td>id</td> | |
<td>form</td> | |
<td>ip</td> | |
<td>date</td> | |
<td>hash</td> | |
<td>values</td> | |
</tr> | |
</thead> | |
<tbody> | |
[[+wrapper]] | |
</tbody> | |
</table> | |
<br> | |
<strong>Total: [[+total]]</strong>", [ | |
'wrapper' => $dataOutput, | |
'total' => $total | |
]); | |
} | |
return $output; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment