Skip to content

Commit

Permalink
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 committed Apr 19, 2018
1 parent c112e75 commit f671e1d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 31 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public function doMyForm($data, $form) {

// Other processing
}
```
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
Expand Down
105 changes: 74 additions & 31 deletions src/Extension/FormCaptureExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SSFormCapture\Extension;
use SilverStripe\Core\Extension;
use SilverStripe\Control\Director;
use SSFormCapture\Admin\MyAdmin as FormCaptureAdmin;
use SSFormCapture\Model\CapturedFormSubmission;
use SSFormCapture\Model\CapturedField;

Expand Down Expand Up @@ -42,51 +44,92 @@ public function captureForm($dataName = 'Form Submission', $excludedFields = [],

// 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 = in_array($field->Name, $inDetails) ? '1' : '0';
$showInDetails = in_array($field->Name, $inDetails) ? '1' : '0';

// Add to this statement if any future type-based value conversions are required
switch ($field->Type()) {
$capturedField = $this->create_captured_field($field, $showInDetails);

case 'checkbox':
$capturedField->SubmissionID = $submission->ID;

$val->Value = $field->dataValue() === 1 ? 'Yes' : 'No';
$capturedField->write();
}

// Return an ID for this submission
return [
'ID' => $submission->ID,
'Link' => $this->get_submission_link($submission->ID)
];
}

/**
* 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();

$field->performReadonlyTransformation();
$val->Name = $field->Name;
$val->Title = $field->Title() ?: $field->Name;
$val->IsInDetails = $showInDetails;

// Add to this statement if any future type-based value conversions are required
switch ($field->Type()) {

break;
case 'checkbox':

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

// Relevent values
$groupedSrc = $field->getSourceAsArray();
$selected = $field->dataValue();
break;

// 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 there's a match return the key holding the value
$catForVal = $key;
case 'groupeddropdown dropdown':

// 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) {
if(is_array($option) && array_search($selected, $option)) {
// 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;
break;

default:
default:

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

break;
}
break;
}

$val->write();
}
}
return $val;
}

/**
* 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::class)->Link();
$editorLink = $base . 'SSFormCapture-Model-CapturedFormSubmission/EditForm/field/SSFormCapture-Model-CapturedFormSubmission/item/';
return $editorLink . $id;
}
}

0 comments on commit f671e1d

Please sign in to comment.