diff --git a/composer.json b/composer.json index f70a9f8..b54e762 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/EmailList.php b/src/EmailList.php index 31ab66b..6def7f6 100644 --- a/src/EmailList.php +++ b/src/EmailList.php @@ -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: * diff --git a/src/controllers/ListController.php b/src/controllers/ListController.php index 8b28a2c..d9f000e 100644 --- a/src/controllers/ListController.php +++ b/src/controllers/ListController.php @@ -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(); + } } diff --git a/src/templates/list.twig b/src/templates/list.twig index 654406a..815c1c3 100644 --- a/src/templates/list.twig +++ b/src/templates/list.twig @@ -2,6 +2,7 @@ {% set title = "Email List"|t('email-list') %} {% block content %} +
Export