Skip to content

Commit

Permalink
[ADD] Tree of subcategories.
Browse files Browse the repository at this point in the history
  • Loading branch information
Seiger committed May 17, 2024
1 parent ce1ef27 commit 61166bb
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/_data/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
url: "management"
- title: "Attributes"
url: "attributes"
- title: "Integration"
url: "integration"
8 changes: 7 additions & 1 deletion docs/pages/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ administrator panel **Admin Panel -> Modules -> Commerce**.
You can also fix quick access to the module through the main menu of the Admin Panel.
This can be done on the configuration tab (only available for the administrator role).

[Management tabs]({{ site.baseurl }}/management/){: .btn .btn-sky}
[Management tabs]({{site.baseurl}}/management/){: .btn .btn-sky}

## Integration

Integrate sCommerce capabilities into your code using the examples provided on the Integration page.

[Integration]({{site.baseurl}}/integration/){: .btn .btn-sky}

## Extra

Expand Down
93 changes: 93 additions & 0 deletions docs/pages/integration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
layout: page
title: Integration
description: Integration sCommerce methods to You code
permalink: /integration/
---

Integrate sCommerce capabilities into your code using the examples below.

## Tree of subcategories

In order to get the desired category together with subcategories, use the method for recursive
construction of the category tree `sCommerce::getTreeActiveCategories(evo()->documentIdentifier, 10)`.

This method will return the searched category along with the recursively constructed subcategories.
Nesting depth depends on the `$dept` parameter (default 10).

```php
EvolutionCMS\Models\SiteContent {#1902 ▼
#connection: "default"
#table: "site_content"
...
#attributes: array:37 [▼
"id" => "50"
"type" => "document"
...
"subcategories" => EvolutionCMS\Extensions\Collection {#1889 ▼
#items: array:4 [▼
0 => EvolutionCMS\Models\SiteContent {#1888 ▼
#connection: "default"
#table: "site_content"
...
#attributes: array:37 [▶]
#original: array:36 [▶]
...
}
1 => EvolutionCMS\Models\SiteContent {#1886 ▶}
2 => EvolutionCMS\Models\SiteContent {#1884 ▶}
3 => EvolutionCMS\Models\SiteContent {#1882 ▶}
]
#escapeWhenCastingToString: false
}
]
#original: array:36 [▶]
...
}
```

### Call the getTreeActiveCategories() method

```php
namespace EvolutionCMS\Main\Controllers;

use Seiger\sCommerce\Facades\sCommerce;

class CategoryController extends BaseController
{
public function render()
{
parent::render();
...
$this->data['category'] = sCommerce::getTreeActiveCategories(evo()->documentIdentifier, 10);
...
}
}
```

### Arguments the getTreeActiveCategories() method

From the example above:

`evo()->documentIdentifier` - The ID of the category for which you want to retrieve data. Number type integer.
`10` - Nesting depth to get subcategories. Number type integer. Default 10.

### Applying the result of the getTreeActiveCategories() call

See below for an example of using the result of the getTreeActiveCategories() method call in the Blade template.

```php
...
<section>
<h1>{% raw %}{{$category->pagetitle}}{% endraw %}</h1>
{% raw %}{!!$category->content!!}{% endraw %}
</section>
@if($category->subcategories->count())
@foreach($category->subcategories as $subcategory)
<section>
<h2>{% raw %}{{$subcategory->pagetitle}}{% endraw %}</h2>
<p>{% raw %}{{$subcategory->introtext}}{% endraw %}</p>
</section>
@endforeach
@endif
```
21 changes: 21 additions & 0 deletions src/Controllers/sCommerceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,27 @@ public function categoryParentsIds(int $category): array
return $this->getParentsIds($category);
}

/**
* Retrieves and assigns the subcategories of a given category recursively.
*
* @param object $category A reference to the category object.
* @return object The modified category object with the "subcategories" property assigned.
*/
public function listSubCategories(object &$category, int $dept): object
{
if ($category->hasChildren() && $dept) {
$children = $category->children()->active()->get();
$children->map(function ($item) use ($dept) {
return $this->listSubCategories($item, $dept--);
});
$category->subcategories = $children;
} else {
$category->subcategories = SiteContent::whereId(0)->get();
}

return $category;
}

/**
* Initializes and returns a rich text editor for the specified elements.
*
Expand Down
14 changes: 14 additions & 0 deletions src/sCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ public function getProductByAlias(string $alias): object
return sProduct::whereAlias($alias)->first() ?? new sProduct();
}

/**
* Retrieves the active subcategories of a given category.
*
* @param int $category The id of the category whose subcategories need to be retrieved.
* @param int $dept The depth level up to which the subcategories should be retrieved. Default value is 10.
* @return object The list of active subcategories of the given category.
*/
public function getTreeActiveCategories(int $category, int $dept = 10): object
{
$sCommerceController = new sCommerceController();
$object = SiteContent::find($category);
return $sCommerceController->listSubCategories($object, $dept);
}

/**
* Retrieves the products belonging to a specific category.
*
Expand Down

0 comments on commit 61166bb

Please sign in to comment.