Skip to content

Commit

Permalink
Added components to be able to display products
Browse files Browse the repository at this point in the history
  • Loading branch information
ljb0904 committed Dec 30, 2019
1 parent 077ce58 commit d96b4ba
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 5 deletions.
7 changes: 4 additions & 3 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ public function boot()
*/
public function registerComponents()
{
return []; // Remove this line to activate

return [
'Lbaig\Catalog\Components\MyComponent' => 'myComponent',
'Lbaig\Catalog\Components\Breadcrumbs' => 'Breadcrumbs',
'Lbaig\Catalog\Components\CategoryItem' => 'CategoryItem',
'Lbaig\Catalog\Components\CategoryList' => 'CategoryList',
'Lbaig\Catalog\Components\ProductList' => 'ProductList'
];
}

Expand Down
36 changes: 36 additions & 0 deletions components/Breadcrumbs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php namespace Lbaig\Catalog\Components;

use Cms\Classes\ComponentBase;
use Lbaig\Catalog\Models\Category;

class Breadcrumbs extends ComponentBase
{
public function componentDetails()
{
return [
'name' => 'Breadcrumbs Component',
'description' => 'No description provided yet...'
];
}

public function defineProperties()
{
return [];
}

public function get(Category $category = null)
{
if ($category === null) {
return [];
}

$result = [$category];
while ($category->parent !== null) {
$result[] = $category->parent;
$category = $category->parent;
}
\Log::info($result);

return array_reverse($result);
}
}
35 changes: 35 additions & 0 deletions components/CategoryItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php namespace Lbaig\Catalog\Components;

use Cms\Classes\ComponentBase;
use Lbaig\Catalog\Models\Category;

class CategoryItem extends ComponentBase
{
public function componentDetails()
{
return [
'name' => 'CategoryItem Component',
'description' => 'No description provided yet...'
];
}

public function defineProperties()
{
return [
'slug' => [
'title' => 'Category',
'description' => 'Category of Products',
'default' => '*',
'type' => 'dropdown',
]
];
}


public function get()
{
$slug = $this->property('slug');

return Category::where('slug', $slug)->first();
}
}
29 changes: 29 additions & 0 deletions components/CategoryList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php namespace Lbaig\Catalog\Components;

use Cms\Classes\ComponentBase;
use Lbaig\Catalog\Models\Category;


class CategoryList extends ComponentBase
{
public function componentDetails()
{
return [
'name' => 'CategoryList Component',
'description' => 'No description provided yet...'
];
}

public function defineProperties()
{
return [];
}

public function getRootCategories()
{
$categories = Category::active()
->whereNull('parent_id')
->get();
return $categories;
}
}
37 changes: 37 additions & 0 deletions components/ProductList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php namespace Lbaig\Catalog\Components;

use Cms\Classes\ComponentBase;
use Lbaig\Catalog\Models\Category;
use Lbaig\Catalog\Models\Product;

class ProductList extends ComponentBase
{
public function componentDetails()
{
return [
'name' => 'ProductList Component',
'description' => 'No description provided yet...'
];
}

public function defineProperties()
{
return [];
}

public function get(Category $category)
{
// select all subcategories as well as category
$subcategory_ids = Category::active()
->where('nest_left', '>=', $category->nest_left)
->where('nest_right', '<=', $category->nest_right)
->pluck('id');

// select all products that belong to this category or lower
$products = Product::active()
->whereIn('category_id', $subcategory_ids)
->get();

return $products;
}
}
3 changes: 3 additions & 0 deletions components/breadcrumbs/default.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>This is the default markup for component Breadcrumbs</p>

<small>You can delete this file if you want</small>
3 changes: 3 additions & 0 deletions components/categoryitem/default.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>This is the default markup for component CategoryItem</p>

<small>You can delete this file if you want</small>
3 changes: 3 additions & 0 deletions components/categorylist/default.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>This is the default markup for component CategoryList</p>

<small>You can delete this file if you want</small>
3 changes: 3 additions & 0 deletions components/productlist/default.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>This is the default markup for component ProductList</p>

<small>You can delete this file if you want</small>
4 changes: 3 additions & 1 deletion controllers/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ class Categories extends Controller
{
public $implement = [
'Backend.Behaviors.FormController',
'Backend.Behaviors.ListController'
'Backend.Behaviors.ListController',
'Backend.behaviors.ReorderController'
];

public $formConfig = 'config_form.yaml';
public $listConfig = 'config_list.yaml';
public $reorderConfig = 'config_reorder.yaml';

public function __construct()
{
Expand Down
6 changes: 6 additions & 0 deletions controllers/categories/_list_toolbar.htm
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@
data-stripe-load-indicator>
Delete selected
</button>

<a
href="<?= Backend::url('lbaig/catalog/categories/reorder') ?>"
class="btn btn-default oc-icon-sitemap">
<?= e(trans('lbaig.catalog::lang.categories.reorder')) ?>
</a>
</div>
5 changes: 5 additions & 0 deletions controllers/categories/_reorder_toolbar.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div data-control="toolbar">
<a href="<?= Backend::url('lbaig/catalog/categories') ?>" class="btn btn-primary oc-icon-caret-left">
<?= e(trans('lbaig.catalog::lang.categories.return_to_categories')) ?>
</a>
</div>
17 changes: 17 additions & 0 deletions controllers/categories/config_reorder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ===================================
# Reorder Behavior Config
# ===================================

# Reorder Title
title: lbaig.catalog::lang.categories.reorder

# Attribute name
nameFrom: name

# Model Class name
modelClass: Lbaig\Catalog\Models\Category

# Toolbar widget configuration
toolbar:
# Partial for toolbar buttons
buttons: reorder_toolbar
1 change: 1 addition & 0 deletions controllers/categories/reorder.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?= $this->reorderRender() ?>
12 changes: 12 additions & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [
'plugin' => [
'name' => 'Catalog',
'description' => 'Custom catalog platform',
],
'categories' => [
'reorder' => 'Reorder',
'return_to_categories' => 'Return to the category list',
],
];
8 changes: 7 additions & 1 deletion models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class Category extends Model
public $hasMany = [
'Lbaig\Catalog\Models\Product'
];
public $belongsTo = [];
public $belongsTo = [
'parent' => 'Lbaig\Catalog\Models\Category'
];
public $belongsToMany = [];
public $morphTo = [];
public $morphOne = [];
Expand All @@ -84,4 +86,8 @@ class Category extends Model
];
public $attachMany = [];

public function scopeActive($query)
{
$query->where('active', true);
}
}
6 changes: 6 additions & 0 deletions models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,10 @@ class Product extends Model
'preview_image' => 'System\Models\File'
];
public $attachMany = [];


public function scopeActive($query)
{
$query->where('active', true);
}
}
4 changes: 4 additions & 0 deletions models/category/columns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ columns:
name:
label: Name
type: text
parent:
lable: Category
relation: parent
select: name

0 comments on commit d96b4ba

Please sign in to comment.