Skip to content

Commit

Permalink
[SS3] Add return values when capturing forms
Browse files Browse the repository at this point in the history
Provides some useful information which can be used in the controller
  • Loading branch information
Andrew Haine authored and AndrewHaine committed Apr 19, 2018
1 parent 97f9747 commit 0d5c7a6
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 59 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# For more information about the properties used in this file,
# please see the EditorConfig documentation:
# http://editorconfig.org

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[{*.yml,package.json}]
indent_size = 2
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public function doMyForm($data, $form) {
}
```

When capturing a form some useful information is returned which can be used in the controller. For example a link is returned to the submission area in the CMS.
```php
$capturedSubmission = $form->captureForm();

echo($capturedSubmission['Link']);
// http://your-site.com/admin/<Link to exact submission>

```

### Options
When calling the captureForm() there are a few optional parameters which will enhance how submission objects are displayed in the CMS.

Expand Down
162 changes: 103 additions & 59 deletions code/CapturedFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,129 @@

class CapturedFormExtension extends Extension
{
/**
* Add a method to Form which will capture data when invoked
* @param string $dataName Am optional name for the submission
* @param mixed $excludedFields An array or string of fields to be ignored when saving the submission
* @param mixed $inDetails Fields to be included in the 'details' column in the admin area
* @return null
*/
public function captureForm($dataName = 'Form Submission', $excludedFields = [], $inDetails = []) {

/**
* Add a method to Form which will capture data when invoked
* @param string $dataName Am optional name for the submission
* @param mixed $excludedFields An array or string of fields to be ignored when saving the submission
* @param mixed $inDetails Fields to be included in the 'details' column in the admin area
* @return null
*/
public function captureForm($dataName = 'Form Submission', $excludedFields = [], $inDetails = [])
{

$form = $this->owner;

// Create a blank form submission and write to database so that we have an ID to work with
$submission = CapturedFormSubmission::create();
$submission->Type = $dataName;
$submission->write();
// Create a blank form submission and write to database so that we have an ID to work with
$submission = CapturedFormSubmission::create();
$submission->Type = $dataName;
$submission->write();

// Grab all the fields
$fieldsToWrite = $form->fields->dataFields();

// Allow the excluded fields and details fields to be a single string
$excludedFields = is_array($excludedFields) ? $excludedFields : [$excludedFields];
$inDetails = is_array($inDetails) ? $inDetails : [$inDetails];

// Ignore SecurityID by default
array_push($excludedFields, 'SecurityID');

// Remove any unwanted fields from the fields array
foreach($excludedFields as $excludedField) {
if($form->fields->dataFieldByName($excludedField)) {
unset($fieldsToWrite[$excludedField]);
};
}

// For every wanted field create a Captured Field object and write it to this submission
foreach($fieldsToWrite as $field) {

$showInDetails = in_array($field->Name, $inDetails) ? '1' : '0';

$capturedField = $this->create_captured_field($field, $showInDetails);

$capturedField->SubmissionID = $submission->ID;

// Grab all the fields
$fieldsToWrite = $form->fields->dataFields();
$capturedField->write();

// Allow the excluded fields and details fields to be a single string
$excludedFields = is_array($excludedFields) ? $excludedFields : [$excludedFields];
$inDetails = is_array($inDetails) ? $inDetails : [$inDetails];
}

// Ignore SecurityID by default
array_push($excludedFields, 'SecurityID');
// Return an ID for this submission
return [
'ID' => $submission->ID,
'Link' => $this->get_submission_link($submission->ID)
];
}

// Remove any unwanted fields from the fields array
foreach($excludedFields as $excludedField) {
if($form->fields->dataFieldByName($excludedField)) {
unset($fieldsToWrite[$excludedField]);
};
}
/**
* Method what returns a captured field constructed from
* a given value
*
* @param FormField $field The field to transform and write to the db
* @param boolean $showIndetails Controls whether the current field should show in the submission 'Details'
*
* @return CapturedField The final field for the submission
*/
private function create_captured_field($field, $showInDetails = false)
{
$val = CapturedField::create();

// For every wanted field create a Captured Field object and write it to this submission
foreach($fieldsToWrite as $field) {
$val = CapturedField::create();
$val->SubmissionID = $submission->ID;
$field->performReadonlyTransformation();
$val->Name = $field->Name;
$val->Title = $field->Title() ?: $field->Name;
$val->IsInDetails = $showInDetails;

$field->performReadonlyTransformation();
$val->Name = $field->Name;
$val->Title = $field->Title() ?: $field->Name;
$val->IsInDetails = in_array($field->Name, $inDetails) ? '1' : '0';
// Add to this statement if any future type-based value conversions are required
switch ($field->Type()) {
case 'checkbox':

// Add to this statement if any future type-based value conversions are required
switch ($field->Type()) {
case 'checkbox':
$val->Value = $field->dataValue() === 1 ? 'Yes' : 'No';

$val->Value = $field->dataValue() === 1 ? 'Yes' : 'No';
break;
case 'groupeddropdown dropdown':

break;
case 'groupeddropdown dropdown':
// Relevent values
$groupedSrc = $field->getSourceAsArray();
$selected = $field->dataValue();

// Relevent values
$groupedSrc = $field->getSourceAsArray();
$selected = $field->dataValue();
// Loop through all source keys, if we find an array search it for the field value
foreach ($groupedSrc as $key => $option) {

// Loop through all source keys, if we find an array search it for the field value
foreach ($groupedSrc as $key => $option) {
if(is_array($option) && array_search($selected, $option)) {

if(is_array($option) && array_search($selected, $option)) {
// If there's a match return the key holding the value
$catForVal = $key;

// If there's a match return the key holding the value
$catForVal = $key;
}
}

}
}
// Formatted value for CMS Display
$val->Value = $catForVal ? '[' . $catForVal .'] ' . $selected : $selected;

// Formatted value for CMS Display
$val->Value = $catForVal ? '[' . $catForVal .'] ' . $selected : $selected;
break;
default:

break;
default:
$val->Value = $field->dataValue();

$val->Value = $field->dataValue();
break;
}

break;
}
return $val;
}

$val->write();
}
}
/**
* Return a link which can be used externally for linking
* to a specific submission object in the CMS
*
* @param int $id The ID of the linked submission
*
* @return string
*/
private function get_submission_link($id)
{
$base = Director::AbsoluteBaseURL() . singleton('FormCaptureAdmin')->Link();
$editorLink = $base . 'CapturedFormSubmission/EditForm/field/CapturedFormSubmission/item/';
return $editorLink . $id;
}
}

0 comments on commit 0d5c7a6

Please sign in to comment.