Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests ExportModel with custom format options #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion controllers/Locations.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class Locations extends Controller
public $implement = [
\Backend\Behaviors\FormController::class,
\Backend\Behaviors\ListController::class,
\Backend\Behaviors\RelationController::class
\Backend\Behaviors\RelationController::class,
\Backend\Behaviors\ImportExportController::class,
];

public $formConfig = 'config_form.yaml';
public $listConfig = 'config_list.yaml';
public $relationConfig = 'config_relation.yaml';
public $importExportConfig = 'config_import_export.yaml';

public function __construct()
{
Expand Down
5 changes: 5 additions & 0 deletions controllers/locations/_list_toolbar.htm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
data-stripe-load-indicator>
Delete selected
</button>
<a
href="<?= Backend::url('october/test/locations/export') ?>"
class="btn btn-secondary oc-icon-file-text">
Export Locations
</a>
<a
href="<?= Backend::url('october/test/countries') ?>"
class="btn btn-default oc-icon-database">
Expand Down
18 changes: 18 additions & 0 deletions controllers/locations/config_import_export.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ===================================
# Import/Export Behavior Config
# ===================================

export:
#useList: false
fileName: locations.csv
title: 'Export Locations'
modelClass: October\Test\Models\LocationExport
list: $/october/test/models/locationexport/columns.yaml
redirect: october/test/locations

defaultFormatOptions:
fileFormat: csv_custom
delimiter: ','
enclosure: '"'
escape: '\'
encoding: 'utf-8'
24 changes: 24 additions & 0 deletions controllers/locations/export.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php Block::put('breadcrumb'); ?>
<ul>
<li><a href="<?= Backend::url('october/test/locations'); ?>">Locations</a></li>
<li><?= e($this->pageTitle); ?></li>
</ul>
<?php Block::endPut(); ?>

<?= Form::open(['class' => 'layout mw-750']); ?>
<div class="layout-row">
<?= $this->exportRender(); ?>
</div>
<div class="form-buttons">
<div class="loading-indicator-container">
<button
type="submit"
data-control="popup"
data-handler="onExportLoadForm"
data-keyboard="false"
class="btn btn-primary">
Export
</button>
</div>
</div>
<?= Form::close(); ?>
17 changes: 17 additions & 0 deletions models/LocationExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php namespace October\Test\Models;

use Backend\Models\ExportModel;

class LocationExport extends ExportModel
{
public function exportData($columns, $sessionKey = null)
{
$locations = Location::all();

$locations->each(function($location) use ($columns) {
$location->addVisible($columns);
});

return $locations->toArray();
}
}
9 changes: 9 additions & 0 deletions models/locationexport/columns.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ===================================
# List Column Definitions
# ===================================

columns:
is_enabled: Enabled
name: Address
city_name: City
country_name: Country