Skip to content

Commit

Permalink
Add export link option
Browse files Browse the repository at this point in the history
  • Loading branch information
damsfx committed Oct 8, 2021
1 parent 9a93e4e commit 4e46a19
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 42 deletions.
1 change: 1 addition & 0 deletions config/product_import_export.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export:
title: 'hounddd.mallimportexport::lang.export.title'
modelClass: Hounddd\MallImportExport\Models\ProductExport
list: $/hounddd/mallimportexport/models/product/export.yaml
form: $/hounddd/mallimportexport/models/product/export_fields.yaml
redirect: offline/mall/products
fileName: 'products_export.csv'

Expand Down
2 changes: 2 additions & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
'stock' => 'Stock',
'user_defined_id' => 'Reference',
'weight' => 'Weight (g)',
'link' => 'Link to product',
],

'ux' => [
'export_button' => 'Export data',
'import_button' => 'Update data',
'export_links' => 'Export link to product',
'return_list' => 'Back to the product list',
],
];
2 changes: 2 additions & 0 deletions lang/fr/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
'stock' => 'Stock',
'user_defined_id' => 'Référence',
'weight' => 'Poids (g)',
'link' => 'Lien vers le produit',
],

'ux' => [
'export_button' => 'Exporter les données',
'import_button' => 'Mettre à jour les données',
'export_links' => 'Exporter un lien vers le produit',
'return_list' => 'Retourner à la liste des produits',
],

Expand Down
145 changes: 103 additions & 42 deletions models/ProductExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

use Config;
use Str;

use Cms\Classes\Page;
use OFFLINE\Mall\Models\Currency;
use OFFLINE\Mall\Models\GeneralSettings;
use OFFLINE\Mall\Models\Product;
use OFFLINE\Mall\Models\Price;
use OFFLINE\Mall\Models\ProductPrice;
Expand All @@ -13,15 +14,59 @@ class ProductExport extends \Backend\Models\ExportModel
{
public $requiredPermissions = ['hounddd.mallimportexport.export'];

public $currencies;

public $defaultCurrency;
public $fillable = [
'link'
];

/**
* List of available currencies
*
* @var collection
*/
protected $currencies;

/**
* Default currency
*
* @var Currency
*/
protected $defaultCurrency;

/**
* Product page identifier
*
* @var string
*/
protected $productPage;

/**
* Product page
*
* @var Page
*/
protected $cmsPage;

private $exportLink = false;

public function __construct()
{
parent::__construct();
$this->currencies = Currency::orderBy('is_default', 'DESC')->orderBy('sort_order', 'ASC')->get();
$this->defaultCurrency = Currency::where('is_default', true)->first();

$this->productPage = GeneralSettings::get('product_page', 'product');
$this->cmsPage = Page::loadCached(config('cms.activeTheme'), $this->productPage);
}

/**
* Used to override column definitions at export time.
*/
protected function exportExtendColumns($columns)
{
if ($this->exportLink) {
$columns['link'] = 'hounddd.mallimportexport::lang.columns.link';
}
return $columns;
}

public function exportData($columns, $sessionKey = null)
Expand Down Expand Up @@ -50,9 +95,18 @@ public function exportData($columns, $sessionKey = null)
});

$products = $products->map(function ($product) {
return $this->getOtherPrices($product);
})
->each(function ($product) use ($columns) {
return $this->addOtherPrices($product);
});

if ($this->link) {
$this->exportLink = true;
$columns[] = 'link';
$products = $products->map(function ($product) {
return $this->addLink($product);
});
}

$products = $products->each(function ($product) use ($columns) {
$product->addVisible($columns);
});

Expand All @@ -64,14 +118,17 @@ public function exportData($columns, $sessionKey = null)
return $data->toArray();
}



private function getOtherPrices($product)
/**
* Add properties for each prices
*
* @param Product $product
* @return Product
*/
protected function addOtherPrices($product)
{
foreach ($product->prices as $key => $price) {
$product['price__'. $price->currency->code] = $this->formatedPriceNoSymbol($price->toArray());
}

foreach ($product->additional_prices as $key => $price) {
$name = 'additional__'. $price->price_category_id .'__'. $price->currency->code;
$product[$name] = $this->formatedPriceNoSymbol($price->toArray());
Expand All @@ -83,10 +140,44 @@ private function getOtherPrices($product)
return $product;
}

/**
* Add link property to product's page
*
* @param Product $product
* @return Product
*/
protected function addLink($product)
{
if ($this->cmsPage) {
$pageUrl = $this->cmsPage->url($this->productPage, ['slug' => $product->slug]);
$product['link'] = $pageUrl;
}

return $product;
}

/**
* Return formated price without currency symbol
*
* @param array $price
* @return string
*/
protected function formatedPriceNoSymbol($price)
{
return trim(str_replace(
$price['currency']['symbol'],
'',
$price['price_formatted']
));
}

private function encodeArrays($item)
/**
* Encore array values to json
*
* @param mixed $item
* @return mixed
*/
protected function encodeArrays($item)
{
if (is_array($item)) {
foreach ($item as $key => $value) {
Expand All @@ -97,34 +188,4 @@ private function encodeArrays($item)
}
return $item;
}


private function getPrices($prices)
{
if (count($prices) == 1) {
$price = $prices[0];
$prices = $this->formatedPriceNoSymbol($price);
} else {
foreach ($prices as $key => $price) {
if (is_array($price)) {
$prices[$key] = [
$price['currency']['code'] => $this->formatedPriceNoSymbol($price),
];
return $prices;
}
}
}
return $prices;
}


private function formatedPriceNoSymbol($price)
{
return trim(str_replace(
$price['currency']['symbol'],
'',
$price['price_formatted']
));
// return number_format($price['price'] / 100, $price['currency']['decimals']);
}
}
10 changes: 10 additions & 0 deletions models/product/export_fields.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ===================================
# Form Field Definitions
# ===================================

fields:

link:
label: 'hounddd.mallimportexport::lang.ux.export_links'
type: checkbox
default: true

0 comments on commit 4e46a19

Please sign in to comment.