diff --git a/.craftplugin b/.craftplugin new file mode 100644 index 0000000..fba6059 --- /dev/null +++ b/.craftplugin @@ -0,0 +1 @@ +{"pluginName":"Beam","pluginDescription":"Generate CSVs and XLS files in your templates","pluginVersion":"2.0.0","pluginAuthorName":"Superbig","pluginVendorName":"superbig","pluginAuthorUrl":"https://superbig.co","pluginAuthorGithub":"sjelfull","codeComments":"","pluginComponents":["controllers","models","services","variables"],"consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a17970c --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ +# CRAFT ENVIRONMENT +.env.php +.env.sh +.env + +# COMPOSER +/vendor + +# BUILD FILES +/bower_components/* +/node_modules/* +/build/* +/yarn-error.log + +# MISC FILES +.cache +.DS_Store +.idea +.project +.settings +*.esproj +*.sublime-workspace +*.sublime-project +*.tmproj +*.tmproject +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +config.codekit3 +prepros-6.config diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..89b79aa --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +# Beam Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). + +## 2.0.0 - 2017-11-01 +### Added +- Initial release diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..197329e --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright (c) 2017 Superbig + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b070d68 --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# Beam plugin for Craft CMS 3.x + +Generate CSVs and XLS files in your templates + +![Screenshot](resources/img/plugin-logo.png) + +## Requirements + +This plugin requires Craft CMS 3.0.0-beta.23 or later. + +## Installation + +To install the plugin, follow these instructions. + +1. Open your terminal and go to your Craft project: + + cd /path/to/project + +2. Then tell Composer to load the plugin: + + composer require superbig/craft3-beam + +3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Beam. + +## Using Beam + +To generate an CSV: + +```twig +{% spaceless %} +{% set options = { + header: ['Email', 'Name'], + rows: [ + [ 'test@example.com', 'John Doe' ], + [ 'another+test@example.com', 'Jane Doe' ], + [ 'third+test@example.com', 'Trond Johansen' ], + ] +} %} +{{ craft.beam.csv(options) }} +{% endspaceless %} +``` + +To generate an XLSX: + +```twig +{% spaceless %} +{% set options = { + header: ['Email', 'Name'], + rows: [ + [ 'test@example.com', 'John Doe' ], + [ 'another+test@example.com', 'Jane Doe' ], + [ 'third+test@example.com', 'Trond Johansen' ], + ] +} %} +{{ craft.beam.xlsx(options) }} +{% endspaceless %} +``` + +Brought to you by [Superbig](https://superbig.co) diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..494b53a --- /dev/null +++ b/composer.json @@ -0,0 +1,52 @@ +{ + "name": "superbig/craft3-beam", + "description": "Generate CSVs and XLS files in your templates", + "type": "craft-plugin", + "version": "2.0.0", + "keywords": [ + "craft", + "cms", + "craftcms", + "craft-plugin", + "beam" + ], + "support": { + "docs": "https://github.com/sjelfull/craft3-beam/blob/master/README.md", + "issues": "https://github.com/sjelfull/craft3-beam/issues" + }, + "license": "MIT", + "authors": [ + { + "name": "Superbig", + "homepage": "https://superbig.co" + } + ], + "require": { + "craftcms/cms": "~3.0.0-beta.23", + "league/csv": "^8.2", + "mk-j/php_xlsxwriter": "^0.32.0" + }, + "repositories": [ + { + "type": "composer", + "url": "https://asset-packagist.org" + } + ], + "autoload": { + "psr-4": { + "superbig\\beam\\": "src/" + } + }, + "extra": { + "name": "Beam", + "handle": "beam", + "schemaVersion": "2.0.0", + "hasCpSettings": false, + "hasCpSection": false, + "changelogUrl": "https://raw.githubusercontent.com/sjelfull/craft3-beam/master/CHANGELOG.md", + "components": { + "beamService": "superbig\\beam\\services\\BeamService" + }, + "class": "superbig\\beam\\Beam" + } +} diff --git a/resources/img/plugin-logo.png b/resources/img/plugin-logo.png new file mode 100644 index 0000000..d5736f0 Binary files /dev/null and b/resources/img/plugin-logo.png differ diff --git a/src/Beam.php b/src/Beam.php new file mode 100644 index 0000000..70a6a57 --- /dev/null +++ b/src/Beam.php @@ -0,0 +1,88 @@ +sender; + $variable->set('beam', BeamVariable::class); + } + ); + + Event::on( + Plugins::class, + Plugins::EVENT_AFTER_INSTALL_PLUGIN, + function (PluginEvent $event) { + if ($event->plugin === $this) { + } + } + ); + + Craft::info( + Craft::t( + 'beam', + '{name} plugin loaded', + ['name' => $this->name] + ), + __METHOD__ + ); + } + + // Protected Methods + // ========================================================================= + +} diff --git a/src/icon.svg b/src/icon.svg new file mode 100644 index 0000000..e89836e --- /dev/null +++ b/src/icon.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/services/BeamService.php b/src/services/BeamService.php new file mode 100644 index 0000000..d41285c --- /dev/null +++ b/src/services/BeamService.php @@ -0,0 +1,100 @@ +insertOne($options['header']); + } + // Insert all the rows + $csv->insertAll($options['rows']); + + $csv->output($filename); + } + + /** + * @param array $options + * + * @return null + */ + public function xlsx ($options = []) + { + $tempPath = Craft::$app->path->getTempPath() . DIRECTORY_SEPARATOR . 'beam' . DIRECTORY_SEPARATOR; + + if ( empty($options['headers']) && empty($options['rows']) ) { + return null; + } + + if ( !file_exists($tempPath) && !is_dir($tempPath) ) { + FileHelper::createDirectory($tempPath); + } + + // Load the CSV document from a string + $writer = new XLSXWriter(); + $filename = !empty($options['filename']) ? $options['filename'] : 'output.xlsx'; + $sheetName = isset($options['sheetName']) ? $options['sheetName'] : 'Sheet'; + + if ( !empty($options['header']) ) { + $headers = []; + foreach ($options['header'] as $header) { + $headers[ $header ] = 'string'; + } + // Insert the headers + $writer->writeSheetHeader($sheetName, $headers); + } + + $filename = filter_var($filename, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); + + foreach ($options['rows'] as $row) { + $writer->writeSheetRow($sheetName, $row); + } + + $writer->writeToFile($tempPath . $filename); + + //$mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; + + Craft::$app->response->sendFile($tempPath . $filename, $filename); + } +} diff --git a/src/variables/BeamVariable.php b/src/variables/BeamVariable.php new file mode 100644 index 0000000..f41f03a --- /dev/null +++ b/src/variables/BeamVariable.php @@ -0,0 +1,46 @@ +beamService->csv($options); + } + + /** + * @param array $options + * + * @return null + */ + public function xlsx ($options = []) + { + return Beam::$plugin->beamService->xlsx($options); + } +}