From e2df2cae6fe85c5a61057c1c98dc8870a3a0a0f0 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Wed, 12 Nov 2014 15:07:49 +1100 Subject: [PATCH 1/2] better form markup --- form.html | 162 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 112 insertions(+), 50 deletions(-) diff --git a/form.html b/form.html index 387d15c..e3d7559 100644 --- a/form.html +++ b/form.html @@ -27,14 +27,16 @@

Please correct the following errors

* Denotes a required field

{% for question in form.getQuestions() %} -
- - {% switch question.type %} + {% switch question.type %} + + + {% case 'PlainText' %} +
+ - {% case 'PlainText' %} Please correct the following errors id="{{ question.handle }}" name="questions[{{ question.handle }}]" value="{{ submission is defined ? submission[question.handle] }}"> +
+ + + {% case 'MultilineText' %} +
+ - {% case 'MultilineText' %} - - {% case 'Dropdown' %} - - - {% case 'RadioButtons' %} -
    - {% for option in question.options %} -
  • - - -
  • - {% endfor %} -
- - {% case 'Checkboxes' %} +
+ + + {% case 'Dropdown' %} + + + + + + {% case 'RadioButtons' %} + + + + {% case 'Checkboxes' %} +
    {% for option in question.options %}
  • + {% set selected = submission is defined and submission[question.handle] + ? submission[question.handle] == option.value + : option.default %} - + value="{{ option.value }}" + {{ selected ? 'checked' }}> +
  • {% endfor %}
+
+ + + {% case 'Number' %} +
+ - {% case 'Number' %} Please correct the following errors id="{{ question.handle }}" name="questions[{{ question.handle }}]" value="{{ submission is defined ? submission[question.handle] }}"> +
+ + + {% case 'Email' %} +
+ - {% case 'Email' %} Please correct the following errors id="{{ question.handle }}" name="questions[{{ question.handle }}]" value="{{ submission is defined ? submission[question.handle] }}"> +
+ + + {% case 'Tel' %} +
+ - {% case 'Tel' %} Please correct the following errors id="{{ question.handle }}" name="questions[{{ question.handle }}]" value="{{ submission is defined ? submission[question.handle] }}"> +
+ + + {% case 'Url' %} +
+ - {% case 'Url' %} Please correct the following errors id="{{ question.handle }}" name="questions[{{ question.handle }}]" value="{{ submission is defined ? submission[question.handle] }}"> +
+ + + {% case 'Date' %} +
+ - {% case 'Date' %} Please correct the following errors id="{{ question.handle }}" name="questions[{{ question.handle }}][date]" value="{{ submission is defined ? submission[question.handle] }}"> +
{% endswitch %} -
{% endfor %}
@@ -184,4 +246,4 @@

Please correct the following errors

- \ No newline at end of file + From 8e469c3cfe4604bab7718884affcd23681d01cfe Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Thu, 13 Nov 2014 17:07:39 +1100 Subject: [PATCH 2/2] fix crash when sending emails, handle checkboxes field correctly, provide html version of email --- form.html | 15 ++++++++-- formerly/models/Formerly_SubmissionModel.php | 28 ++++++++++++++++++- .../services/Formerly_SubmissionsService.php | 25 +++++++++++++++-- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/form.html b/form.html index e3d7559..bd10300 100644 --- a/form.html +++ b/form.html @@ -45,6 +45,7 @@

Please correct the following errors

data-val="true" {% endif %} class="text" + placeholder="{{ question.name | upper }}" id="{{ question.handle }}" name="questions[{{ question.handle }}]" value="{{ submission is defined ? submission[question.handle] }}"> @@ -65,6 +66,7 @@

Please correct the following errors

data-val="true" {% endif %} class="text" + placeholder="{{ question.name | upper }}" cols="20" rows="2" id="{{ question.handle }}" name="questions[{{ question.handle }}]">{{ submission is defined ? submission[question.handle] }} @@ -120,11 +122,14 @@

Please correct the following errors

{% for option in question.options %}
  • {% set selected = submission is defined and submission[question.handle] - ? submission[question.handle] == option.value + ? option.value in submission[question.handle] : option.default %} + {% if not selected %} + + {% endif %} @@ -148,6 +153,7 @@

    Please correct the following errors

    data-val="true" {% endif %} class="text" + placeholder="{{ question.name | upper }}" id="{{ question.handle }}" name="questions[{{ question.handle }}]" value="{{ submission is defined ? submission[question.handle] }}"> @@ -168,6 +174,7 @@

    Please correct the following errors

    data-val="true" {% endif %} class="text" + placeholder="{{ question.name | upper }}" id="{{ question.handle }}" name="questions[{{ question.handle }}]" value="{{ submission is defined ? submission[question.handle] }}"> @@ -188,6 +195,7 @@

    Please correct the following errors

    data-val="true" {% endif %} class="text" + placeholder="{{ question.name | upper }}" id="{{ question.handle }}" name="questions[{{ question.handle }}]" value="{{ submission is defined ? submission[question.handle] }}"> @@ -208,6 +216,7 @@

    Please correct the following errors

    data-val="true" {% endif %} class="text" + placeholder="{{ question.name | upper }}" id="{{ question.handle }}" name="questions[{{ question.handle }}]" value="{{ submission is defined ? submission[question.handle] }}"> @@ -229,7 +238,7 @@

    Please correct the following errors

    {% endif %} class="text" pattern="\d{2}\/\d{2}\/\d{4}" - placeholder="DD/MM/YYYY" + placeholder="{{ question.name | upper }} (DD/MM/YYYY)" id="{{ question.handle }}" name="questions[{{ question.handle }}][date]" value="{{ submission is defined ? submission[question.handle] }}"> diff --git a/formerly/models/Formerly_SubmissionModel.php b/formerly/models/Formerly_SubmissionModel.php index df85a64..1bca545 100644 --- a/formerly/models/Formerly_SubmissionModel.php +++ b/formerly/models/Formerly_SubmissionModel.php @@ -55,11 +55,37 @@ public function getSummary() $summary = ''; $questions = $this->getForm()->getQuestions(); + for ($i = 0; $i < count($questions); ++$i) { $question = $questions[$i]; - $summary .= $question->name . "\n: " . $this[$question->handle]; + $name = $question->name; + $value = $this[$question->handle]; + + $summary .= $name . ":\n"; + + if ($value instanceof MultiOptionsFieldData) + { + $options = $value->getOptions(); + + for ($j = 0; $j < count($options); ++$j) + { + $option = $options[$j]; + + $summary .= $option->label . ': ' . ($option->selected ? 'yes' : 'no'); + + if ($j != count($options) - 1) + { + $summary .= "\n"; + } + } + } + else + { + $summary .= $value; + } + if ($i != count($questions) - 1) { $summary .= "\n\n"; diff --git a/formerly/services/Formerly_SubmissionsService.php b/formerly/services/Formerly_SubmissionsService.php index 0f8dea0..9599516 100644 --- a/formerly/services/Formerly_SubmissionsService.php +++ b/formerly/services/Formerly_SubmissionsService.php @@ -10,9 +10,18 @@ public function getSubmissionById($submissionId) public function postSubmission(Formerly_SubmissionModel $submission) { + $this->onBeforePost(new Event($this, array( + 'submission' => $submission + ))); + if ($this->saveSubmission($submission)) { $this->sendSubmissionEmails($submission); + + $this->onPost(new Event($this, array( + 'submission' => $submission + ))); + return true; } @@ -90,11 +99,13 @@ public function sendSubmissionEmails(Formerly_SubmissionModel $submission) if (!empty($emailDef['body'])) { - $email->body = $this->_renderSubmissionTemplate($emailDef['body'], $submission); + $email->body = $this->_renderSubmissionTemplate($emailDef['body'], $submission); + $email->htmlBody = nl2br($email->body); } else { - $email->body = $submission->getSummary(); + $email->body = $submission->getSummary(); + $email->htmlBody = nl2br($email->body); } if (!empty($email->body)) @@ -114,4 +125,14 @@ private function _renderSubmissionTemplate($template, Formerly_SubmissionModel $ return craft()->templates->renderObjectTemplate($formattedTemplate, $submission); } + + public function onBeforePost(Event $event) + { + $this->raiseEvent('onBeforePost', $event); + } + + public function onPost(Event $event) + { + $this->raiseEvent('onPost', $event); + } }