Skip to content

Commit

Permalink
Added export
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-hopkins committed May 11, 2021
1 parent 42d406e commit e3c0b13
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lukehopkins/email-list",
"description": "A plugin for saving emails to a list",
"type": "craft-plugin",
"version": "1.0.4",
"version": "1.0.5",
"keywords": [
"craft",
"cms",
Expand Down
2 changes: 1 addition & 1 deletion src/EmailList.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ function (PluginEvent $event) {

Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
$event->rules['email-list/list'] = 'email-list/list/index';
$event->rules['email-list/export'] = 'email-list/list/export';
});


/**
* Logging in Craft involves using one of the following methods:
*
Expand Down
19 changes: 19 additions & 0 deletions src/controllers/ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,23 @@ public function actionSave()
EmailList::$plugin->email->saveEmail($model);
return $this->redirect(Craft::$app->request->getQueryParam('redirect'));
}

public function actionExport()
{
$this->requireCpRequest();
$data['emails'] = EmailList::$plugin->email->getEmails();

header('Content-type: application/csv');
header('Content-Disposition: attachment; filename=emails.csv');
header("Content-Transfer-Encoding: UTF-8");

$f = fopen('php://output', 'a');

foreach ($data['emails'] as $email) {
fputcsv($f, [$email->email]);
}

fclose($f);
die();
}
}
1 change: 1 addition & 0 deletions src/templates/list.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% set title = "Email List"|t('email-list') %}

{% block content %}
<div style="margin-bottom: 16px;"><a href="{{ url('email-list/export') }}" style="background: white; border: 1px solid #d7d7d7; text-align: center; display: inline-block; padding: 0.4em 0.6em; cursor: pointer; text-decoration: none;">Export</a></div>
<table id="menu-list" class="data fullwidth collapsible">
<thead>
<th scope="col">{{ "Email"|t('email-list') }}</th>
Expand Down

0 comments on commit e3c0b13

Please sign in to comment.