Skip to content

Commit

Permalink
Support import/export v4
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Aug 26, 2024
1 parent eaf8eb4 commit 8a036b4
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 52 deletions.
1 change: 0 additions & 1 deletion resources/models/menuexport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
'minimum_qty' => 'lang:igniter.cart::default.menus.label_minimum_qty',
'categories' => 'lang:igniter.cart::default.menus.label_category',
'menu_status' => 'lang:admin::lang.label_status',
'mealtimes' => 'lang:igniter.cart::default.menus.label_mealtime',
],
];
1 change: 0 additions & 1 deletion resources/models/menuimport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
'minimum_qty' => 'lang:igniter.cart::default.menus.label_minimum_qty',
'categories' => 'lang:igniter.cart::default.menus.label_category',
'menu_status' => 'lang:admin::lang.label_status',
'mealtimes' => 'lang:igniter.cart::default.menus.label_mealtime',
],
'fields' => [
'update_existing' => [
Expand Down
28 changes: 0 additions & 28 deletions src/Models/MenuExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,32 @@

namespace Igniter\Cart\Models;

use Igniter\Flame\Database\Attach\HasMedia;
use IgniterLabs\ImportExport\Models\ExportModel;

class MenuExport extends ExportModel
{
use HasMedia;

protected $table = 'menus';

protected $primaryKey = 'menu_id';

public $relation = [
'belongsToMany' => [
'menu_categories' => [\Igniter\Cart\Models\Category::class, 'table' => 'menu_categories', 'foreignKey' => 'menu_id'],
'menu_mealtimes' => [\Igniter\Cart\Models\Mealtime::class, 'table' => 'menu_mealtimes', 'foreignKey' => 'menu_id'],
],
];

public $mediable = ['thumb'];

/**
* The accessors to append to the model's array form.
* @var array
*/
protected $appends = [
'categories',
'thumb_url',
'mealtimes',
];

public function exportData($columns)
{
return self::make()->with([
'menu_mealtimes',
'menu_categories',
'media',
])->get()->toArray();
}

Expand All @@ -49,22 +39,4 @@ public function getCategoriesAttribute()

return $this->encodeArrayValue($this->menu_categories->pluck('name')->all());
}

public function getThumbUrlAttribute()
{
if (!$this->hasMedia('thumb')) {
return '';
}

return $this->getFirstMedia('thumb')->getPath();
}

public function getMealtimesAttribute()
{
if (!$this->menu_mealtimes) {
return '';
}

return $this->encodeArrayValue($this->menu_mealtimes->pluck('mealtime_name')->all());
}
}
23 changes: 1 addition & 22 deletions src/Models/MenuImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class MenuImport extends ImportModel

protected $primaryKey = 'menu_id';

protected $mealtimeNameCache = [];

protected $categoryNameCache = [];

public function importData($results)
Expand All @@ -29,15 +27,11 @@ public function importData($results)
$menuItem = $this->findDuplicateMenuItem($data) ?: $menuItem;
}

$except = ['menu_id', 'categories', 'mealtimes'];
$except = ['menu_id', 'categories'];
foreach (array_except($data, $except) as $attribute => $value) {
$menuItem->{$attribute} = $value ?: null;
}

if ($mealtime = $this->findMealtimeFromName($data)) {
$menuItem->mealtime_id = $mealtime->mealtime_id;
}

$menuExists = $menuItem->exists;
$menuItem->save();

Expand All @@ -64,21 +58,6 @@ protected function findDuplicateMenuItem($data)
return $menuItem->first();
}

protected function findMealtimeFromName($data)
{
if (!$name = array_get($data, 'mealtimes')) {
return null;
}

if (isset($this->mealtimeNameCache[$name])) {
return $this->mealtimeNameCache[$name];
}

$mealtime = Mealtime::where('mealtime_name', $name)->first();

return $this->mealtimeNameCache[$name] = $mealtime;
}

protected function getCategoryIdsForMenuItem($data)
{
$ids = [];
Expand Down

0 comments on commit 8a036b4

Please sign in to comment.