Skip to content

Commit

Permalink
fix crash when sending emails, handle checkboxes field correctly, pro…
Browse files Browse the repository at this point in the history
…vide html version of email
  • Loading branch information
Robert Anderson committed Nov 13, 2014
1 parent e2df2ca commit 8e469c3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
15 changes: 12 additions & 3 deletions form.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ <h3>Please correct the following errors</h3>
data-val="true"
{% endif %}
class="text"
placeholder="{{ question.name | upper }}"
id="{{ question.handle }}"
name="questions[{{ question.handle }}]"
value="{{ submission is defined ? submission[question.handle] }}">
Expand All @@ -65,6 +66,7 @@ <h3>Please correct the following errors</h3>
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] }}</textarea>
Expand Down Expand Up @@ -120,11 +122,14 @@ <h3>Please correct the following errors</h3>
{% for option in question.options %}
<li>
{% 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 %}
<input type="checkbox" name="questions[{{ question.handle }}][]" value="">
{% endif %}
<input type="checkbox"
id="{{ question.handle ~ loop.index }}"
name="questions[{{ question.handle }}]"
name="questions[{{ question.handle }}][]"
value="{{ option.value }}"
{{ selected ? 'checked' }}>
<label for="{{ question.handle ~ loop.index }}">{{ option.label }}</label>
Expand All @@ -148,6 +153,7 @@ <h3>Please correct the following errors</h3>
data-val="true"
{% endif %}
class="text"
placeholder="{{ question.name | upper }}"
id="{{ question.handle }}"
name="questions[{{ question.handle }}]"
value="{{ submission is defined ? submission[question.handle] }}">
Expand All @@ -168,6 +174,7 @@ <h3>Please correct the following errors</h3>
data-val="true"
{% endif %}
class="text"
placeholder="{{ question.name | upper }}"
id="{{ question.handle }}"
name="questions[{{ question.handle }}]"
value="{{ submission is defined ? submission[question.handle] }}">
Expand All @@ -188,6 +195,7 @@ <h3>Please correct the following errors</h3>
data-val="true"
{% endif %}
class="text"
placeholder="{{ question.name | upper }}"
id="{{ question.handle }}"
name="questions[{{ question.handle }}]"
value="{{ submission is defined ? submission[question.handle] }}">
Expand All @@ -208,6 +216,7 @@ <h3>Please correct the following errors</h3>
data-val="true"
{% endif %}
class="text"
placeholder="{{ question.name | upper }}"
id="{{ question.handle }}"
name="questions[{{ question.handle }}]"
value="{{ submission is defined ? submission[question.handle] }}">
Expand All @@ -229,7 +238,7 @@ <h3>Please correct the following errors</h3>
{% 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] }}">
Expand Down
28 changes: 27 additions & 1 deletion formerly/models/Formerly_SubmissionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
25 changes: 23 additions & 2 deletions formerly/services/Formerly_SubmissionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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))
Expand All @@ -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);
}
}

0 comments on commit 8e469c3

Please sign in to comment.