Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
sjelfull committed Nov 1, 2017
0 parents commit 5f7dac2
Show file tree
Hide file tree
Showing 11 changed files with 402 additions and 0 deletions.
1 change: 1 addition & 0 deletions .craftplugin
Original file line number Diff line number Diff line change
@@ -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"}
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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: [
[ '[email protected]', 'John Doe' ],
[ '[email protected]', 'Jane Doe' ],
[ '[email protected]', 'Trond Johansen' ],
]
} %}
{{ craft.beam.csv(options) }}
{% endspaceless %}
```

To generate an XLSX:

```twig
{% spaceless %}
{% set options = {
header: ['Email', 'Name'],
rows: [
[ '[email protected]', 'John Doe' ],
[ '[email protected]', 'Jane Doe' ],
[ '[email protected]', 'Trond Johansen' ],
]
} %}
{{ craft.beam.xlsx(options) }}
{% endspaceless %}
```

Brought to you by [Superbig](https://superbig.co)
52 changes: 52 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Binary file added resources/img/plugin-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
88 changes: 88 additions & 0 deletions src/Beam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Beam plugin for Craft CMS 3.x
*
* Generate CSVs and XLS files in your templates
*
* @link https://superbig.co
* @copyright Copyright (c) 2017 Superbig
*/

namespace superbig\beam;

use superbig\beam\services\BeamService as BeamServiceService;
use superbig\beam\variables\BeamVariable;

use Craft;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\events\PluginEvent;
use craft\web\UrlManager;
use craft\web\twig\variables\CraftVariable;
use craft\events\RegisterUrlRulesEvent;

use yii\base\Event;

/**
* Class Beam
*
* @author Superbig
* @package Beam
* @since 2.0.0
*
* @property BeamServiceService $beamService
*/
class Beam extends Plugin
{
// Static Properties
// =========================================================================

/**
* @var Beam
*/
public static $plugin;

// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function init()
{
parent::init();
self::$plugin = $this;

Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function (Event $event) {
/** @var CraftVariable $variable */
$variable = $event->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
// =========================================================================

}
6 changes: 6 additions & 0 deletions src/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
100 changes: 100 additions & 0 deletions src/services/BeamService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Beam plugin for Craft CMS 3.x
*
* Generate CSVs and XLS files in your templates
*
* @link https://superbig.co
* @copyright Copyright (c) 2017 Superbig
*/

namespace superbig\beam\services;

use craft\helpers\FileHelper;
use craft\helpers\Path;
use superbig\beam\Beam;

use Craft;
use craft\base\Component;
use League\Csv\Writer;
use League\Csv\Reader;
use XLSXWriter;
use yii\web\Response;

/**
* @author Superbig
* @package Beam
* @since 2.0.0
*/
class BeamService extends Component
{
// Public Methods
// =========================================================================

/**
* @param array $options
*
* @return null
*/
public function csv ($options = [])
{
if ( empty($options['headers']) && empty($options['rows']) ) {
return null;
}
// Load the CSV document from a string
$csv = Writer::createFromString('');
$filename = !empty($options['filename']) ? $options['filename'] : 'output.csv';
if ( !empty($options['header']) ) {
// Insert the headers
$csv->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);
}
}
Loading

0 comments on commit 5f7dac2

Please sign in to comment.