From 0d5c7a6605dc6aa3f112dd86c261fe03d45d9bd7 Mon Sep 17 00:00:00 2001 From: Andrew Haine Date: Thu, 19 Apr 2018 11:46:47 +0100 Subject: [PATCH] [SS3] Add return values when capturing forms Provides some useful information which can be used in the controller --- .editorconfig | 14 +++ README.md | 9 ++ code/CapturedFormExtension.php | 162 +++++++++++++++++++++------------ 3 files changed, 126 insertions(+), 59 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..eb0fd14 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/README.md b/README.md index aecba4a..6ab0d22 100644 --- a/README.md +++ b/README.md @@ -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/ + +``` + ### Options When calling the captureForm() there are a few optional parameters which will enhance how submission objects are displayed in the CMS. diff --git a/code/CapturedFormExtension.php b/code/CapturedFormExtension.php index 1326e6b..faa6d12 100644 --- a/code/CapturedFormExtension.php +++ b/code/CapturedFormExtension.php @@ -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; + } }