Skip to content

Commit

Permalink
Finished up CSV export. Tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marty Wallace committed Nov 13, 2014
1 parent 2c16641 commit aec0968
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 29 deletions.
3 changes: 2 additions & 1 deletion formerly/FormerlyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function registerCpRoutes()
'formerly/forms/(?P<formId>\d+)' => array('action' => 'formerly/forms/editForm'),
'formerly' => array('action' => 'formerly/submissions/index'),
'formerly/(?P<formHandle>{handle})/(?P<submissionId>\d+)' => array('action' => 'formerly/submissions/viewSubmission'),
'formerly/export' => array('action' => 'formerly/export/index')
'formerly/export' => array('action' => 'formerly/export/index'),
'formerly/export/csv' => array('action' => 'formerly/export/csv')
);
}
}
54 changes: 54 additions & 0 deletions formerly/controllers/Formerly_ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,58 @@ public function actionIndex()
));
}


public function actionCsv()
{
$formId = craft()->request->getPost('form');
$form = craft()->formerly_forms->getFormById($formId);

$criteria = craft()->elements->getCriteria('Formerly_Submission');
$criteria->formId = $formId;

$data = array();

foreach($criteria->find() as $submission)
{
$row = array(
'Id' => $submission->id,
'Time' => $submission->dateCreated->format('d/m/Y H:i:s')
);

foreach($form->getQuestions() as $question)
{
$columnName = str_replace($form->handle . '_', '', $question->handle);
$columnName = ucwords($columnName);

$row[$columnName] = $submission->{$question->handle};
}

$data[] = $row;
}

if(count($data) > 0)
{
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . ($form->handle . '_submissions.csv'));
header('Content-Transfer-Encoding: binary');

$stream = fopen('php://output', 'w');

// Write column names first.
fputcsv($stream, array_keys($data[0]));

foreach ($data as $row)
{
fputcsv($stream, $row);
}

fclose($stream);
}
else
{
header('Content-type: text/plain');
echo 'There are no submissions.';
}
}

}
16 changes: 4 additions & 12 deletions formerly/models/Formerly_SubmissionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,12 @@ public function __toString()

public function getSummary()
{
$summary = '';

$questions = $this->getForm()->getQuestions();
for ($i = 0; $i < count($questions); ++$i)
$summary = array();
foreach ($this->getForm()->getQuestions() as $question)
{
$question = $questions[$i];

$summary .= $question->name . "\n: " . $this[$question->handle];
if ($i != count($questions) - 1)
{
$summary .= "\n\n";
}
$summary[] = $question->name . ":\n" . $this->{$question->handle};
}

return $summary;
return implode("\n\n", $summary);
}
}
34 changes: 18 additions & 16 deletions formerly/templates/export/_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,27 @@

{% set content %}
{% if forms | length %}
<div class="field">
<div class="heading">
<label for="form">Form</label>
<div class="instructions">
<p>Select the form whose data you want to export</p>
<form action="{{ url('formerly/export/csv') }}" method="post" target="_blank">
<div class="field">
<div class="heading">
<label for="form">Form</label>
<div class="instructions">
<p>Select the form whose data you want to export</p>
</div>
</div>
<div class="select">
<select name="form">
{% for form in forms %}
<option value="{{ form.id }}">{{ form.name }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="select">
<select name="form">
{% for form in forms %}
<option value="{{ form.id }}">{{ form.name }}</option>
{% endfor %}
</select>
</div>
</div>

<div class="button last">
<input type="submit" class="btn submit" value="Export CSV" />
</div>
<div class="button last">
<input type="submit" class="btn submit" value="Export CSV" />
</div>
</form>
{% else %}
<p>There are no forms.</p>
{% endif %}
Expand Down

0 comments on commit aec0968

Please sign in to comment.