Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
damsfx committed Sep 24, 2021
0 parents commit c011985
Show file tree
Hide file tree
Showing 18 changed files with 1,113 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 OFFLINE Hounddd

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.
276 changes: 276 additions & 0 deletions Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
<?php namespace Hounddd\MallImportExport;

use App;
use Auth;
use Backend;
use BackendAuth;
use Config;
use Event;
use Lang;
use Yaml;
use Backend\Models\UserPreference;
use System\Classes\PluginBase;
use System\Models\PluginVersion;
use OFFLINE\Mall\Models\Currency;
use OFFLINE\Mall\Models\CustomerGroup;
use OFFLINE\Mall\Models\PriceCategory;

/**
* MallImportExport Plugin Information File
*/
class Plugin extends PluginBase
{
public $require = ['Offline.Mall'];

/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'hounddd.mallimportexport::lang.plugin.name',
'description' => 'hounddd.mallimportexport::lang.plugin.description',
'author' => 'Hounddd',
'icon' => 'icon-retweet',
'homepage' => 'https://github.com/Hounddd/wn-mallimportexport-plugin',
];
}

/**
* Boot method, called right before the request route.
*
* @return array
*/
public function boot()
{
$mallVersion = PluginVersion::getVersion('OFFLINE.Mall');

/**
* Extend OFFLINE.Mall plugin's menu or products toolbar
*/
if ($mallVersion >= '1.14.4') {
/**
* Extend products view toolbar
*/
Event::listen('offline.mall.extendProductsToolbar', function ($controller) {
$buttons = [];
$currentUser = BackendAuth::getUser();

if ($currentUser->hasAccess('hounddd.mallimportexport.*')) {
$buttons[] = '<span class="p-x"></span>';
}
if ($currentUser->hasPermission('hounddd.mallimportexport.import')) {
$buttons[] = '<a href="'. Backend::url('offline/mall/products/import') .'" '
.'class="btn btn-info oc-icon-cloud-download">'
.trans('hounddd.mallimportexport::lang.menus.import') .'</a>';
}
if ($currentUser->hasPermission('hounddd.mallimportexport.export')) {
$buttons[] = '<a href="'. Backend::url('offline/mall/products/export') .'" '
.'class="btn btn-info oc-icon-cloud-upload">'
.trans('hounddd.mallimportexport::lang.menus.export') .'</a>';
}
return implode('', $buttons);
});
} else {
/**
* Extend backend menus
*/
Event::listen('backend.menu.extendItems', function ($manager) {
$buttons = [];
$currentUser = BackendAuth::getUser();

if ($currentUser->hasPermission('hounddd.mallimportexport.import')
&& $currentUser->hasPermission('hounddd.mallimportexport.export')) {
$buttons['importexport'] = [
'label' => 'hounddd.mallimportexport::lang.menus.importexport',
'icon' => 'icon-retweet',
'code' => 'mall-importexport',
'owner' => 'Offline.Mall',
'url' => Backend::url('offline/mall/products/import'),
'permissions' => ['hounddd.mallimportexport.*'],
];
} else {
if ($currentUser->hasPermission('hounddd.mallimportexport.import')) {
$buttons['import'] = [
'label' => 'hounddd.mallimportexport::lang.menus.import',
'icon' => 'icon-cloud-upload',
'code' => 'mall-import',
'owner' => 'Offline.Mall',
'url' => Backend::url('offline/mall/products/import'),
'permissions' => ['hounddd.mallimportexport.import'],
];
}
if ($currentUser->hasPermission('hounddd.mallimportexport.export')) {
$buttons['export'] = [
'label' => 'hounddd.mallimportexport::lang.menus.export',
'icon' => 'icon-cloud-upload',
'code' => 'mall-export',
'owner' => 'Offline.Mall',
'url' => Backend::url('offline/mall/products/export'),
'permissions' => ['hounddd.mallimportexport.export'],
];
}
}

$manager->addSideMenuItems('Offline.Mall', 'mall-catalogue', $buttons);
});
}

/**
* Check for user permissions to access import or export routes
*/
Event::listen(
'backend.page.beforeDisplay',
function (
$backendController,
$action,
$params
) {
$currentUser = BackendAuth::getUser();
if ($currentUser->hasAccess('hounddd.mallimportexport.*')) {
if ($action == 'import' && !$currentUser->hasPermission('hounddd.mallimportexport.import')) {
return \Backend::redirect('offline/mall/products');
}
if ($action == 'export' && !$currentUser->hasPermission('hounddd.mallimportexport.export')) {
return \Backend::redirect('offline/mall/products');
}
}
}
);

/**
* Extend controllers
*/
\OFFLINE\Mall\Controllers\Products::extend(function ($controller) {

// Init some vars
list($author, $plugin) = explode('\\', strtolower(get_class()));
$config = $controller->makeConfig(sprintf('$/%s/%s/config/product_import_export.yaml', $author, $plugin));
$importList = $controller->makeConfig($config->import['list']);
$exportList = $controller->makeConfig($config->export['list']);

/*
|--------------------------------------------------------------------------
| Get default "price" translation according to user choice
|--------------------------------------------------------------------------
|
| Direct use of `Lang::get()` does not take into consideration the user
| preference choice and as we have to add a string to the column labels,
| it's not possible to use a translation key directly in the column label.
|
| It is therefore necessary to force the translation by specifying the
| language to use.
|
*/
$currentLocale = array_get(
UserPreference::forUser()->get('backend::backend.preferences'),
'locale',
App::getLocale()
);
$priceTrad = Lang::get(
sprintf('%s.%s::lang.columns.price', $author, $plugin),
[],
$currentLocale
);

// Implement behavior if not already implemented
if (!$controller->isClassExtendedWith('Backend.Behaviors.ImportExportController')) {
$controller->implement[] = 'Backend.Behaviors.ImportExportController';
}

// Add views
$partials_path = sprintf('$/%s/%s/partials', $author, $plugin);
$controller->addViewPath($partials_path);
$controller->vars['lang'] = $currentLocale;

// Add currencies price columns
$currencies = Currency::orderBy('is_default', 'DESC')->orderBy('sort_order', 'ASC')->get();
$currencies->each(function (Currency $currency) use (
$importList,
$exportList,
$priceTrad
) {
$importList->columns['price__' . $currency->code] = [
'label' => sprintf('%s %s', $priceTrad, $currency->symbol)
];
$exportList->columns['price__' . $currency->code] = [
'label' => sprintf('%s %s', $priceTrad, $currency->symbol)
];

// Add additional price categories
$additionalPriceCategories = PriceCategory::orderBy('sort_order', 'ASC')->get();
$additionalPriceCategories->each(function (PriceCategory $category) use (
$currency,
$importList,
$exportList
) {
$importList->columns['additional__'. $category->id .'__'.$currency->code] = [
'label' => $category->name.' '.$currency->symbol
];
$exportList->columns['additional__'. $category->id .'__'.$currency->code] = [
'label' => $category->name.' '.$currency->symbol
];
});

// Add custormer's group price columns
$customerGroups = CustomerGroup::orderBy('sort_order', 'ASC')->get();
$customerGroups->each(function (CustomerGroup $group) use (
$currency,
$importList,
$exportList,
$priceTrad
) {
$importList->columns['group__' . $group->id .'__'.$currency->code] = [
'label' => sprintf('%s %s', $priceTrad, strtolower($group->name).' '.$currency->symbol),
];
$exportList->columns['group__' . $group->id .'__'.$currency->code] = [
'label' => sprintf('%s %s', $priceTrad, strtolower($group->name).' '.$currency->symbol),
];
});
});

$config->import['list'] = $importList;
$config->export['list'] = $exportList;

// Set export file name from config
$fileName = $config->export['fileName'];
$newFileName = Config::get(sprintf('%s.%s::export.fileName', $author, $plugin, false));
if ($newFileName) {
$fileName = $newFileName;
}
// Append date to file name
if (Config::get(sprintf('%s.%s::export.appendDate', $author, $plugin, false))) {
$fileName .= (date(Config::get(sprintf('%s.%s::export.dateFormat', $author, $plugin), '_Y-m-d')));
}

$config->export['fileName'] = $fileName .'.csv';

// Define property if not already defined
if (!isset($controller->importExportConfig)) {
$controller->addDynamicProperty('importExportConfig', $config);
}
$controller->bodyClass = 'compact-container';
});
}

/**
* Registers any back-end permissions used by this plugin.
*
* @return array
*/
public function registerPermissions()
{
return [
'hounddd.mallimportexport.import' => [
'tab' => 'Mall Import/Export',
'label' => 'hounddd.mallimportexport::lang.permissions.import'
],
'hounddd.mallimportexport.export' => [
'tab' => 'Mall Import/Export',
'label' => 'hounddd.mallimportexport::lang.permissions.export'
],
];
}
}
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Mall Import/Export

This plugin offers the possibility to import or export data into the [OFFLINE.Mall](https://github.com/OFFLINE-GmbH/oc-mall-plugin) plugin.

***
**Please note that it does NOT allow you to add products but to update existing products**.

***

## What you can import or export

- publication status
- stocks
- authorisation for sales out of stock
- weight
- prices, additional prices and group prices, in all currencies.


## How it works
The plugin uses the CSV file [import/export features of WinterCMS](https://wintercms.com/docs/backend/import-export).

With default options, exporting data will give you a CSV file named `Products_export_2021-09-23.csv`.

### Columns naming
The plugin use currencies symbols to append same to the column name.
- The prices columns will be named "Price $", "Price €", ...
- The customers prices columns will be named "Price *pricename* $", "Price *pricename* €", ...
<br />ex for a "Vip" customer price : "Price vip $", "Price vip €", ...
- The defined price categories use their own names "*Price category name* $", "*Price category name* €", ...
<br />ex for a "Old price" price category : "Old price $", "Old price €", ...

You can define your own filename, date or column's names from [config](#mallimportexportconfig).

### Integration
If the version of your OFFLINE.Mall plugin includes event hooks for toolbars (>= 1.14.4), you will find the import and export buttons at the top of the product list, otherwise an entry will be added to the side menu of the plugin.
Buttons are available according to the user rights.

## Installation
*Let assume you're in the root of your wintercms installation*

### Using composer
Just run this command
```bash
composer require hounddd/wn-mallimportexport
```

### Cloning repo
Clone this repo into your winter plugins folder.

```bash
cd plugins
mkdir hounddd && cd hounddd
git clone https://github.com/Hounddd/wn-mallimportexport mallimportexport
```
## <a name="mallimportexportconfig"></a>Configuring
### Import/export behaviors
You can configure plugin behaviors with a file `\config\hounddd\mallimportexport\config.php`

```php
<?php

return [
'import' => [
],

'export' => [
'fileName' => 'Export_produits', // New export filename, default "Products_export"
'appendDate' => true, // Append date to filename, default true
'dateFormat' => '_Y-m-d-z', // How to format append date, default '_Y-m-d'
]
];
```
See [PHP date format](https://www.php.net/manual/datetime.format.php) for dateFormat accepted values.
Remember that this is for the name of a file, respect the conventions to avoid trouble.

### Csv file column's names
You can change the column names to match your needs, simply add a `\lang\XX\hounddd\mallimportexport\lang.php` (xhere XX is your locale) to your site.
```php
<?php
return [

'columns' => [
'allow_out_of_stock_purchases' => 'Vente hors stock',
'price' => 'Prix en',
'published' => 'Publié',
'stock' => 'Stock',
'user_defined_id' => 'Référence',
'weight' => 'Poids en g',
],

];

```

***
Make awesome sites with [WinterCMS](https://wintercms.com)!
Binary file added assets/images/en-export-csv-utf8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/fr-export-csv-utf8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c011985

Please sign in to comment.