Documentation

ImportExportController extends ControllerBehavior
in package

Adds features for importing and exporting data.

This behavior is implemented in the controller like so:

public $implement = [
    'Backend.Behaviors.ImportExportController',
];

public $importExportConfig = 'config_import_export.yaml';

The $importExportConfig property makes reference to the configuration values as either a YAML file, located in the controller view directory, or directly as a PHP array.

Tags
author

Alexey Bobkov, Samuel Georges

Table of Contents

$assetPath  : string
$exportColumns  : array<string|int, mixed>
$exportModel  : Model
$extendableStaticCalledClass  : string
$importColumns  : array<string|int, mixed>
$importModel  : Model
$layout  : string
$suppressLayout  : bool
$vars  : array<string|int, mixed>
$actions  : array<string|int, mixed>
$assets  : array<string|int, mixed>
$config  : array<string|int, mixed>
$configPath  : string
$controller  : Controller
$exportFileName  : string
$exportFormatFormWidget  : WidgetBase
$exportOptionsFormWidget  : WidgetBase
$extensionCallbacks  : array<string|int, mixed>
$extensionHidden  : mixed
$importOptionsFormWidget  : WidgetBase
$importUploadFormWidget  : WidgetBase
$layoutPath  : string
$requiredConfig  : array<string|int, mixed>
$requiredProperties  : array<string|int, mixed>
$viewPath  : string|array<string|int, mixed>
__construct()  : mixed
Behavior constructor
addCss()  : void
Adds StyleSheet asset to the asset list. Call $this->makeAssets() in a view to output corresponding markup.
addJs()  : void
Adds JavaScript asset to the asset list. Call $this->makeAssets() in a view to output corresponding markup.
addRss()  : void
Adds an RSS link asset to the asset list. Call $this->makeAssets() in a view to output corresponding markup.
addViewPath()  : void
Prepends a path on the available view path locations.
combineAssets()  : string
Run the provided assets through the Asset Combiner
download()  : mixed
export()  : mixed
exportFromList()  : void
Outputs the list results as a CSV export.
exportGetModel()  : mixed
exportRender()  : mixed
extend()  : mixed
extensionApplyInitCallbacks()  : mixed
extensionExtendCallback()  : void
Helper method for `::extend()` static method
extensionIsHiddenField()  : mixed
extensionIsHiddenMethod()  : mixed
flushAssets()  : void
Disables the use, and subequent broadcast, of assets. This is useful to call during an AJAX request to speed things up. This method works by specifically targeting the hasAssetsDefined method.
getAssetPath()  : string
Locates a file based on it's definition. If the file starts with a forward slash, it will be returned in context of the application public path, otherwise it will be returned in context of the asset path.
getAssetPaths()  : array<string|int, mixed>
Returns an array of all registered asset paths.
getCalledExtensionClass()  : mixed
getConfig()  : string
Safe accessor for configuration values.
getConfigPath()  : string
Locates a file based on it's definition. If the file starts with the ~ symbol it will be returned in context of the application base path, otherwise it will be returned in context of the config path.
getViewPath()  : string
Locates a file based on its definition. The file name can be prefixed with a symbol (~|$) to return in context of the application or plugin base path, otherwise it will be returned in context of this object view path.
getViewPaths()  : array<string|int, mixed>
Returns the active view path locations.
guessConfigPath()  : string
Guess the package path for the called class.
guessConfigPathFrom()  : string
Guess the package path from a specified class.
guessViewPath()  : string
Guess the package path for the called class.
guessViewPathFrom()  : string
Guess the package path from a specified class.
hasAssetsDefined()  : bool
Returns true if assets any have been added.
import()  : mixed
importExportMakePartial()  : string
Controller accessor for making partials within this behavior.
importGetModel()  : mixed
importIsColumnRequired()  : mixed
importRender()  : mixed
makeAssets()  : string
Outputs `<link>` and `<script>` tags to load assets previously added with addJs and addCss method calls
makeConfig()  : array<string|int, mixed>|stdClass
Reads the contents of the supplied file and applies it to this object.
makeConfigFromArray()  : stdClass
Makes a config object from an array, making the first level keys properties of a new object.
makeFileContents()  : string
Includes a file path using output buffering.
makeFormWidget()  : FormWidgetBase
Makes a form widget object with the supplied form field and widget configuration.
makeLayout()  : mixed
Render a layout.
makeLayoutPartial()  : string
Renders a layout partial
makePartial()  : mixed
Render a partial file contents located in the views folder.
makeView()  : string
Loads a view with the name specified. Applies layout if its name is provided by the parent object.
makeViewContent()  : string
Renders supplied contents inside a layout.
makeWidget()  : mixed|WidgetBase
Makes a widget object with the supplied configuration file.
mergeConfig()  : stdClass
Merges two configuration sources, either prepared or not, and returns them as a single configuration object.
onExport()  : mixed
onExportLoadForm()  : mixed
onImport()  : mixed
onImportLoadColumnSampleForm()  : mixed
onImportLoadForm()  : mixed
prepareExportVars()  : void
Prepares the view data.
prepareImportVars()  : void
Prepares the view data.
resetSession()  : void
Resets all session data related to this widget.
setConfig()  : mixed
Sets the configuration values
addAsset()  : mixed
Adds the provided asset to the internal asset collections
checkPermissionsForType()  : View
Checks to see if the import/export is controlled by permissions and if the logged in user has permissions.
checkRequiredImportColumns()  : mixed
checkUseListExportMode()  : mixed
controllerMethodExists()  : bool
Returns true in case if a specified method exists in the extended controller.
createCsvReader()  : Reader
Create a new CSV reader with options selected by the user
extensionHideField()  : mixed
extensionHideMethod()  : mixed
getAssetEntryBuildPath()  : string
Internal helper, attaches a build code to an asset path
getAssetScheme()  : string
Internal helper, get asset scheme
getExportColumns()  : mixed
getFormatOptionsFromPost()  : array<string|int, mixed>
Returns the file format options from postback. This method can be used to define presets.
getImportDbColumns()  : mixed
getImportFileColumns()  : mixed
getImportFilePath()  : mixed
getImportSourceIndexOffset()  : int
Get the index offset to add to the reported row number in status messages
getLocalPath()  : mixed
getModelForType()  : mixed
getRedirectUrlForType()  : mixed
getSession()  : string
Retrieves a widget related key/value pair from session data.
handleViewException()  : void
Handle a view exception.
hideAction()  : mixed
Protects a public method from being available as an controller action.
makeExportFormatFormWidget()  : mixed
makeExportOptionsFormWidget()  : mixed
makeImportOptionsFormWidget()  : mixed
makeImportUploadFormWidget()  : mixed
makeListColumns()  : mixed
makeOptionsFormWidgetForType()  : mixed
makeSessionId()  : string
Returns a unique session identifier for this widget and controller action.
processExportColumnsFromPost()  : mixed
putSession()  : void
Saves a widget related key/value pair in to session data.
removeDuplicates()  : void
Removes duplicate assets from the entire collection.

Properties

$assetPath

public string $assetPath

Specifies a path to the asset directory.

$extendableStaticCalledClass

public static string $extendableStaticCalledClass = null

The calling class when using a static method.

$layout

public string $layout

Layout to use for the view.

$suppressLayout

public bool $suppressLayout = false

Prevents the use of a layout.

$vars

public array<string|int, mixed> $vars = []

A list of variables to pass to the page.

$actions

protected array<string|int, mixed> $actions = ['import', 'export', 'download']

Visible actions in context of the controller

$assets

protected array<string|int, mixed> $assets = ['js' => [], 'css' => [], 'rss' => []]

Collection of assets to display in the layout.

$configPath

protected string $configPath

Specifies a path to the config directory.

$exportFileName

protected string $exportFileName = 'export.csv'

File name used for export output.

$extensionCallbacks

protected static array<string|int, mixed> $extensionCallbacks = []

Used to extend the constructor of an extension class. Eg:

BehaviorClass::extend(function($obj) { })

$extensionHidden

protected mixed $extensionHidden = ['fields' => [], 'methods' => ['extensionIsHiddenField', 'extensionIsHiddenMethod']]

$layoutPath

protected string $layoutPath

Specifies a path to the layout directory.

$requiredConfig

protected array<string|int, mixed> $requiredConfig = []

Configuration values that must exist when applying the primary config file.

$requiredProperties

protected array<string|int, mixed> $requiredProperties = ['importExportConfig']

Properties that must exist in the controller using this behavior.

Tags
inheritDoc

$viewPath

protected string|array<string|int, mixed> $viewPath

Specifies a path to the views directory.

Methods

addCss()

Adds StyleSheet asset to the asset list. Call $this->makeAssets() in a view to output corresponding markup.

public addCss(string $name[, array<string|int, mixed> $attributes = [] ]) : void
Parameters
$name : string

Specifies a path (URL) to the script.

$attributes : array<string|int, mixed> = []

Adds extra HTML attributes to the asset link.

Return values
void

addJs()

Adds JavaScript asset to the asset list. Call $this->makeAssets() in a view to output corresponding markup.

public addJs(string $name[, array<string|int, mixed> $attributes = [] ]) : void
Parameters
$name : string

Specifies a path (URL) to the script.

$attributes : array<string|int, mixed> = []

Adds extra HTML attributes to the asset link.

Return values
void

addRss()

Adds an RSS link asset to the asset list. Call $this->makeAssets() in a view to output corresponding markup.

public addRss(string $name[, array<string|int, mixed> $attributes = [] ]) : void
Parameters
$name : string

Specifies a path (URL) to the RSS channel

$attributes : array<string|int, mixed> = []

Adds extra HTML attributes to the asset link.

Return values
void

addViewPath()

Prepends a path on the available view path locations.

public addViewPath(string|array<string|int, mixed> $path) : void
Parameters
$path : string|array<string|int, mixed>
Return values
void

combineAssets()

Run the provided assets through the Asset Combiner

public combineAssets(array<string|int, mixed> $assets[, string $localPath = '' ]) : string
Parameters
$assets : array<string|int, mixed>

Collection of assets

$localPath : string = ''

Prefix all assets with this path (optional)

Return values
string

download()

public download(mixed $name[, mixed $outputName = null ]) : mixed
Parameters
$name : mixed
$outputName : mixed = null
Return values
mixed

exportFromList()

Outputs the list results as a CSV export.

public exportFromList([string $definition = null ][, array<string|int, mixed> $options = [] ]) : void
Parameters
$definition : string = null
$options : array<string|int, mixed> = []
Return values
void

extend()

public static extend(callable $callback) : mixed
Parameters
$callback : callable
Return values
mixed

extensionApplyInitCallbacks()

public extensionApplyInitCallbacks() : mixed
Return values
mixed

extensionExtendCallback()

Helper method for `::extend()` static method

public static extensionExtendCallback(callable $callback) : void
Parameters
$callback : callable
Return values
void

extensionIsHiddenField()

public extensionIsHiddenField(mixed $name) : mixed
Parameters
$name : mixed
Return values
mixed

extensionIsHiddenMethod()

public extensionIsHiddenMethod(mixed $name) : mixed
Parameters
$name : mixed
Return values
mixed

flushAssets()

Disables the use, and subequent broadcast, of assets. This is useful to call during an AJAX request to speed things up. This method works by specifically targeting the hasAssetsDefined method.

public flushAssets() : void
Return values
void

getAssetPath()

Locates a file based on it's definition. If the file starts with a forward slash, it will be returned in context of the application public path, otherwise it will be returned in context of the asset path.

public getAssetPath(string $fileName[, string $assetPath = null ]) : string
Parameters
$fileName : string

File to load.

$assetPath : string = null

Explicitly define an asset path.

Return values
string

Relative path to the asset file.

getAssetPaths()

Returns an array of all registered asset paths.

public getAssetPaths() : array<string|int, mixed>
Return values
array<string|int, mixed>

getCalledExtensionClass()

public static getCalledExtensionClass() : mixed
Return values
mixed

getConfig()

Safe accessor for configuration values.

public getConfig([string $name = null ][, mixed $default = null ]) : string
Parameters
$name : string = null

Config name, supports array names like "field[key]"

$default : mixed = null

Default value if nothing is found

Return values
string

getConfigPath()

Locates a file based on it's definition. If the file starts with the ~ symbol it will be returned in context of the application base path, otherwise it will be returned in context of the config path.

public getConfigPath(string $fileName[, mixed $configPath = null ]) : string
Parameters
$fileName : string

File to load.

$configPath : mixed = null

Explicitly define a config path.

Return values
string

Full path to the config file.

getViewPath()

Locates a file based on its definition. The file name can be prefixed with a symbol (~|$) to return in context of the application or plugin base path, otherwise it will be returned in context of this object view path.

public getViewPath(string $fileName[, mixed $viewPath = null ]) : string
Parameters
$fileName : string

File to load.

$viewPath : mixed = null

Explicitly define a view path.

Return values
string

Full path to the view file.

getViewPaths()

Returns the active view path locations.

public getViewPaths() : array<string|int, mixed>
Return values
array<string|int, mixed>

guessConfigPath()

Guess the package path for the called class.

public guessConfigPath([string $suffix = '' ]) : string
Parameters
$suffix : string = ''

An extra path to attach to the end

Return values
string

guessConfigPathFrom()

Guess the package path from a specified class.

public guessConfigPathFrom(string $class[, string $suffix = '' ]) : string
Parameters
$class : string

Class to guess path from.

$suffix : string = ''

An extra path to attach to the end

Return values
string

guessViewPath()

Guess the package path for the called class.

public guessViewPath([string $suffix = '' ][, bool $isPublic = false ]) : string
Parameters
$suffix : string = ''

An extra path to attach to the end

$isPublic : bool = false

Returns public path instead of an absolute one

Return values
string

guessViewPathFrom()

Guess the package path from a specified class.

public guessViewPathFrom(string $class[, string $suffix = '' ][, bool $isPublic = false ]) : string
Parameters
$class : string

Class to guess path from.

$suffix : string = ''

An extra path to attach to the end

$isPublic : bool = false

Returns public path instead of an absolute one

Return values
string

hasAssetsDefined()

Returns true if assets any have been added.

public hasAssetsDefined() : bool
Return values
bool

importExportMakePartial()

Controller accessor for making partials within this behavior.

public importExportMakePartial(string $partial[, array<string|int, mixed> $params = [] ]) : string
Parameters
$partial : string
$params : array<string|int, mixed> = []
Return values
string

Partial contents

importIsColumnRequired()

public importIsColumnRequired(mixed $columnName) : mixed
Parameters
$columnName : mixed
Return values
mixed

makeAssets()

Outputs `<link>` and `<script>` tags to load assets previously added with addJs and addCss method calls

public makeAssets([string $type = null ]) : string
Parameters
$type : string = null

Return an asset collection of a given type (css, rss, js) or null for all.

Return values
string

makeConfig()

Reads the contents of the supplied file and applies it to this object.

public makeConfig([array<string|int, mixed> $configFile = [] ][, array<string|int, mixed> $requiredConfig = [] ]) : array<string|int, mixed>|stdClass
Parameters
$configFile : array<string|int, mixed> = []
$requiredConfig : array<string|int, mixed> = []
Return values
array<string|int, mixed>|stdClass

makeConfigFromArray()

Makes a config object from an array, making the first level keys properties of a new object.

public makeConfigFromArray([array<string|int, mixed> $configArray = [] ]) : stdClass
Parameters
$configArray : array<string|int, mixed> = []

Config array.

Return values
stdClass

The config object

makeFileContents()

Includes a file path using output buffering.

public makeFileContents(string $filePath[, array<string|int, mixed> $extraParams = [] ]) : string

Ensures that vars are available.

Parameters
$filePath : string

Absolute path to the view file.

$extraParams : array<string|int, mixed> = []

Parameters that should be available to the view.

Return values
string

makeFormWidget()

Makes a form widget object with the supplied form field and widget configuration.

public makeFormWidget(string $class[, mixed $fieldConfig = [] ][, array<string|int, mixed> $widgetConfig = [] ]) : FormWidgetBase
Parameters
$class : string

Widget class name

$fieldConfig : mixed = []

A field name, an array of config or a FormField object.

$widgetConfig : array<string|int, mixed> = []

An array of config.

Return values
FormWidgetBase

The widget object

makeLayout()

Render a layout.

public makeLayout([string $name = null ][, array<string|int, mixed> $params = [] ][, bool $throwException = true ]) : mixed
Parameters
$name : string = null

Specifies the layout name. If this parameter is omitted, the $layout property will be used.

$params : array<string|int, mixed> = []

Parameter variables to pass to the view.

$throwException : bool = true

Throw an exception if the layout is not found

Return values
mixed

The layout contents, or false.

makeLayoutPartial()

Renders a layout partial

public makeLayoutPartial(string $partial[, array<string|int, mixed> $params = [] ]) : string
Parameters
$partial : string

The view to load.

$params : array<string|int, mixed> = []

Parameter variables to pass to the view.

Return values
string

The layout partial contents

makePartial()

Render a partial file contents located in the views folder.

public makePartial(string $partial[, array<string|int, mixed> $params = [] ][, bool $throwException = true ]) : mixed
Parameters
$partial : string

The view to load.

$params : array<string|int, mixed> = []

Parameter variables to pass to the view.

$throwException : bool = true

Throw an exception if the partial is not found.

Return values
mixed

Partial contents or false if not throwing an exception.

makeView()

Loads a view with the name specified. Applies layout if its name is provided by the parent object.

public makeView(string $view) : string

The view file must be situated in the views directory, and has the extension "htm".

Parameters
$view : string

Specifies the view name, without extension. Eg: "index".

Return values
string

makeViewContent()

Renders supplied contents inside a layout.

public makeViewContent(string $contents[, string $layout = null ]) : string
Parameters
$contents : string

The inner contents as a string.

$layout : string = null

Specifies the layout name.

Return values
string

makeWidget()

Makes a widget object with the supplied configuration file.

public makeWidget(string $class[, array<string|int, mixed> $widgetConfig = [] ]) : mixed|WidgetBase
Parameters
$class : string

Widget class name

$widgetConfig : array<string|int, mixed> = []

An array of config.

Return values
mixed|WidgetBase

The widget object

mergeConfig()

Merges two configuration sources, either prepared or not, and returns them as a single configuration object.

public mergeConfig(mixed $configA, mixed $configB) : stdClass
Parameters
$configA : mixed
$configB : mixed
Return values
stdClass

The config object

onImportLoadColumnSampleForm()

public onImportLoadColumnSampleForm() : mixed
Return values
mixed

prepareExportVars()

Prepares the view data.

public prepareExportVars() : void
Return values
void

prepareImportVars()

Prepares the view data.

public prepareImportVars() : void
Return values
void

resetSession()

Resets all session data related to this widget.

public resetSession() : void
Return values
void

setConfig()

Sets the configuration values

public setConfig(mixed $config[, array<string|int, mixed> $required = [] ]) : mixed
Parameters
$config : mixed

Config object or array

$required : array<string|int, mixed> = []

Required config items

Return values
mixed

addAsset()

Adds the provided asset to the internal asset collections

protected addAsset(string $type, string $path, array<string|int, mixed> $attributes) : mixed
Parameters
$type : string

The type of the asset: 'js' || 'css' || 'rss'

$path : string

The path to the asset

$attributes : array<string|int, mixed>

The attributes for the asset

Return values
mixed

checkPermissionsForType()

Checks to see if the import/export is controlled by permissions and if the logged in user has permissions.

protected checkPermissionsForType(mixed $type) : View
Parameters
$type : mixed
Return values
View

checkRequiredImportColumns()

protected checkRequiredImportColumns() : mixed
Return values
mixed

controllerMethodExists()

Returns true in case if a specified method exists in the extended controller.

protected controllerMethodExists(string $methodName) : bool
Parameters
$methodName : string

Specifies the method name

Return values
bool

createCsvReader()

Create a new CSV reader with options selected by the user

protected createCsvReader(string $path) : Reader
Parameters
$path : string
Return values
Reader

extensionHideField()

protected extensionHideField(mixed $name) : mixed
Parameters
$name : mixed
Return values
mixed

extensionHideMethod()

protected extensionHideMethod(mixed $name) : mixed
Parameters
$name : mixed
Return values
mixed

getAssetEntryBuildPath()

Internal helper, attaches a build code to an asset path

protected getAssetEntryBuildPath(array<string|int, mixed> $asset) : string
Parameters
$asset : array<string|int, mixed>

Stored asset array

Return values
string

getAssetScheme()

Internal helper, get asset scheme

protected getAssetScheme(string $asset) : string
Parameters
$asset : string

Specifies a path (URL) to the asset.

Return values
string

getFormatOptionsFromPost()

Returns the file format options from postback. This method can be used to define presets.

protected getFormatOptionsFromPost() : array<string|int, mixed>
Return values
array<string|int, mixed>

getImportSourceIndexOffset()

Get the index offset to add to the reported row number in status messages

protected getImportSourceIndexOffset(bool $firstRowTitles) : int
Parameters
$firstRowTitles : bool

Whether or not the first row contains column titles

Return values
int

$offset

getLocalPath()

protected getLocalPath(string $relativePath) : mixed
Parameters
$relativePath : string
Return values
mixed

getModelForType()

protected getModelForType(mixed $type) : mixed
Parameters
$type : mixed
Return values
mixed

getRedirectUrlForType()

protected getRedirectUrlForType(mixed $type) : mixed
Parameters
$type : mixed
Return values
mixed

getSession()

Retrieves a widget related key/value pair from session data.

protected getSession([string $key = null ][, string $default = null ]) : string
Parameters
$key : string = null

Unique key for the data store.

$default : string = null

A default value to use when value is not found.

Return values
string

handleViewException()

Handle a view exception.

protected handleViewException(Exception $e, int $obLevel) : void
Parameters
$e : Exception
$obLevel : int
Return values
void

hideAction()

Protects a public method from being available as an controller action.

protected hideAction(mixed $methodName) : mixed

These methods could be defined in a controller to override a behavior default action. Such methods should be defined as public, to allow the behavior object to access it. By default public methods of a controller are considered as actions. To prevent this occurrence, methods should be hidden by using this method.

Parameters
$methodName : mixed

Specifies a method name.

Return values
mixed

makeExportFormatFormWidget()

protected makeExportFormatFormWidget() : mixed
Return values
mixed

makeExportOptionsFormWidget()

protected makeExportOptionsFormWidget() : mixed
Return values
mixed

makeImportOptionsFormWidget()

protected makeImportOptionsFormWidget() : mixed
Return values
mixed

makeImportUploadFormWidget()

protected makeImportUploadFormWidget() : mixed
Return values
mixed

makeListColumns()

protected makeListColumns(mixed $config) : mixed
Parameters
$config : mixed
Return values
mixed

makeOptionsFormWidgetForType()

protected makeOptionsFormWidgetForType(mixed $type) : mixed
Parameters
$type : mixed
Return values
mixed

makeSessionId()

Returns a unique session identifier for this widget and controller action.

protected makeSessionId() : string
Return values
string

processExportColumnsFromPost()

protected processExportColumnsFromPost() : mixed
Return values
mixed

putSession()

Saves a widget related key/value pair in to session data.

protected putSession(string $key, string $value) : void
Parameters
$key : string

Unique key for the data store.

$value : string

The value to store.

Return values
void

removeDuplicates()

Removes duplicate assets from the entire collection.

protected removeDuplicates() : void
Return values
void

Search results