From 61166bb19c61f5bddb67bed48a4387e0d8aae906 Mon Sep 17 00:00:00 2001 From: Serhii Korneliuk Date: Fri, 17 May 2024 14:36:25 +0300 Subject: [PATCH] [ADD] Tree of subcategories. --- docs/_data/toc.yml | 2 + docs/pages/getting-started.md | 8 ++- docs/pages/integration.md | 93 +++++++++++++++++++++++++ src/Controllers/sCommerceController.php | 21 ++++++ src/sCommerce.php | 14 ++++ 5 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 docs/pages/integration.md diff --git a/docs/_data/toc.yml b/docs/_data/toc.yml index c749b04..471731d 100644 --- a/docs/_data/toc.yml +++ b/docs/_data/toc.yml @@ -7,3 +7,5 @@ url: "management" - title: "Attributes" url: "attributes" + - title: "Integration" + url: "integration" diff --git a/docs/pages/getting-started.md b/docs/pages/getting-started.md index 99786c8..cd8efce 100644 --- a/docs/pages/getting-started.md +++ b/docs/pages/getting-started.md @@ -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 diff --git a/docs/pages/integration.md b/docs/pages/integration.md new file mode 100644 index 0000000..0079190 --- /dev/null +++ b/docs/pages/integration.md @@ -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 +... +
+

{% raw %}{{$category->pagetitle}}{% endraw %}

+ {% raw %}{!!$category->content!!}{% endraw %} +
+@if($category->subcategories->count()) + @foreach($category->subcategories as $subcategory) +
+

{% raw %}{{$subcategory->pagetitle}}{% endraw %}

+

{% raw %}{{$subcategory->introtext}}{% endraw %}

+
+ @endforeach +@endif +``` \ No newline at end of file diff --git a/src/Controllers/sCommerceController.php b/src/Controllers/sCommerceController.php index 54e0583..bdf0411 100644 --- a/src/Controllers/sCommerceController.php +++ b/src/Controllers/sCommerceController.php @@ -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. * diff --git a/src/sCommerce.php b/src/sCommerce.php index df95b74..39ff153 100644 --- a/src/sCommerce.php +++ b/src/sCommerce.php @@ -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. *