Skip to content

Commit 6a62f73

Browse files
committed
"withParentTrait" and related
1 parent 4fdd301 commit 6a62f73

File tree

8 files changed

+184
-29
lines changed

8 files changed

+184
-29
lines changed

app/base/traits/WithParentTrait.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* SiteBase
4+
* PHP Version 7.0
5+
*
6+
* @category CMS / Framework
7+
* @package Degami\Sitebase
8+
* @author Mirko De Grandis <degami@github.com>
9+
* @license MIT https://opensource.org/licenses/mit-license.php
10+
* @link https://github.com/degami/sitebase
11+
*/
12+
13+
namespace App\Base\Traits;
14+
15+
use App\Site\Models\Taxonomy;
16+
17+
/**
18+
* Trait for elements with parent
19+
*/
20+
trait WithParentTrait
21+
{
22+
/**
23+
* gets parent object if any
24+
*
25+
* @return self|null
26+
*/
27+
public function getParentObj()
28+
{
29+
if ($this->parent_id == null) {
30+
return null;
31+
}
32+
33+
return $this->getContainer()->call([static::class, 'load'], ['id' => $this->parent_id]);
34+
}
35+
36+
/**
37+
* gets parent ids tree
38+
*
39+
* @return string
40+
*/
41+
public function getParentIds()
42+
{
43+
if ($this->parent_id == null) {
44+
return $this->id;
45+
}
46+
47+
return $this->getParentObj()->getParentIds() . '/' . $this->id;
48+
}
49+
}

app/base/traits/WithWebsiteTrait.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
*/
2020
trait WithWebsiteTrait
2121
{
22+
/** @var Website */
23+
protected $websiteModel = null;
24+
2225
/**
2326
* gets website
2427
*
@@ -28,6 +31,12 @@ public function getWebsite()
2831
{
2932
$this->checkLoaded();
3033

31-
return $this->getContainer()->make(Website::class, ['dbrow' => $this->website()->fetch()]);
34+
if ($this->websiteModel == null) {
35+
$this->websiteModel = $this->getContainer()->make(Website::class, ['dbrow' => $this->website()->fetch()]);
36+
}
37+
38+
return $this->websiteModel;
3239
}
40+
41+
3342
}

app/site/controllers/Admin/Menus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ protected function saveLevel($menu_name, $level, $parent, $position = 0)
326326
if ($id != null && $menu_elem instanceof Menu) {
327327
$menu_elem->parent_id = $parent_id;
328328
$menu_elem->position = $position;
329-
$menu_elem->breadcrumb = $menu_elem->getParentIds();
329+
// $menu_elem->breadcrumb = $menu_elem->getParentIds();
330330
$menu_elem->save();
331331
}
332332
}

app/site/controllers/Admin/Taxonomy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function getFormDefinition(FAPI\Form $form, &$form_state)
112112
$term_content = $term->content;
113113
$term_template_name = $term->template_name;
114114
$term_parent = $term->parent_id;
115-
$term_position = $term->postion;
115+
$term_position = $term->position;
116116
}
117117

118118
$form->addField('title', [
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* SiteBase
4+
* PHP Version 7.0
5+
*
6+
* @category CMS / Framework
7+
* @package Degami\Sitebase
8+
* @author Mirko De Grandis <degami@github.com>
9+
* @license MIT https://opensource.org/licenses/mit-license.php
10+
* @link https://github.com/degami/sitebase
11+
*/
12+
13+
namespace App\Site\Migrations;
14+
15+
use \App\Base\Abstracts\Migrations\DBMigration;
16+
use Degami\SqlSchema\Exceptions\DuplicateException;
17+
use \Degami\SqlSchema\Table;
18+
19+
/**
20+
* "add level column" to menu table migration
21+
*/
22+
class AddLevelToMenuTableMigration extends DBMigration
23+
{
24+
/**
25+
* @var string table name
26+
*/
27+
protected $tableName = 'menu';
28+
29+
/**
30+
* {@inheritdocs}
31+
*
32+
* @return string
33+
*/
34+
public function getName()
35+
{
36+
return '06_' . parent::getName();
37+
}
38+
39+
/**
40+
* {@inheritdocs}
41+
*
42+
* @param Table $table
43+
* @return Table
44+
* @throws DuplicateException
45+
*/
46+
public function addDBTableDefinition(Table $table)
47+
{
48+
$table
49+
->addColumn('level', 'INT', null, ['UNSIGNED']);
50+
51+
return $table;
52+
}
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* SiteBase
4+
* PHP Version 7.0
5+
*
6+
* @category CMS / Framework
7+
* @package Degami\Sitebase
8+
* @author Mirko De Grandis <degami@github.com>
9+
* @license MIT https://opensource.org/licenses/mit-license.php
10+
* @link https://github.com/degami/sitebase
11+
*/
12+
13+
namespace App\Site\Migrations;
14+
15+
use \App\Base\Abstracts\Migrations\DBMigration;
16+
use Degami\SqlSchema\Exceptions\DuplicateException;
17+
use \Degami\SqlSchema\Table;
18+
19+
/**
20+
* "add path and leve columns" to taxonomy table migration
21+
*/
22+
class AddPathAndLevelToTaxonomyTableMigration extends DBMigration
23+
{
24+
/**
25+
* @var string table name
26+
*/
27+
protected $tableName = 'taxonomy';
28+
29+
/**
30+
* {@inheritdocs}
31+
*
32+
* @return string
33+
*/
34+
public function getName()
35+
{
36+
return '05_' . parent::getName();
37+
}
38+
39+
/**
40+
* {@inheritdocs}
41+
*
42+
* @param Table $table
43+
* @return Table
44+
* @throws DuplicateException
45+
*/
46+
public function addDBTableDefinition(Table $table)
47+
{
48+
$table
49+
->addColumn('path', 'VARCHAR', [1024])
50+
->addColumn('level', 'INT', null, ['UNSIGNED']);
51+
52+
return $table;
53+
}
54+
}

app/site/models/Menu.php

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace App\Site\Models;
1414

1515
use \App\Base\Abstracts\Models\ModelWithChildren;
16+
use App\Base\Traits\WithParentTrait;
1617
use \App\Base\Traits\WithWebsiteTrait;
1718
use DateTime;
1819
use Degami\Basics\Exceptions\BasicException;
@@ -36,7 +37,7 @@
3637
*/
3738
class Menu extends ModelWithChildren
3839
{
39-
use WithWebsiteTrait;
40+
use WithWebsiteTrait, WithParentTrait;
4041

4142
/**
4243
* gets link URL
@@ -79,31 +80,10 @@ function ($el) use ($container) {
7980
);
8081
}
8182

82-
/**
83-
* gets parent object if any
84-
*
85-
* @return self|null
86-
*/
87-
public function getParentObj()
88-
{
89-
if ($this->parent_id == null) {
90-
return null;
91-
}
92-
93-
return $this->getContainer()->call([Menu::class, 'load'], ['id' => $this->parent_id]);
94-
}
95-
96-
/**
97-
* gets parent ids tree
98-
*
99-
* @return string
100-
*/
101-
public function getParentIds()
83+
public function prePersist()
10284
{
103-
if ($this->parent_id == null) {
104-
return $this->id;
105-
}
106-
107-
return $this->getParentObj()->getParentIds() . '/' . $this->id;
85+
$this->breadcrumb = $this->getParentIds();
86+
$this->level = max(count(explode("/", $this->breadcrumb))-1, 0);
87+
return parent::prePersist();
10888
}
10989
}

app/site/models/Taxonomy.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace App\Site\Models;
1414

1515
use \App\Base\Abstracts\Models\FrontendModelWithChildren;
16+
use App\Base\Traits\WithParentTrait;
1617
use DateTime;
1718
use Exception;
1819

@@ -33,6 +34,8 @@
3334
*/
3435
class Taxonomy extends FrontendModelWithChildren
3536
{
37+
use WithParentTrait;
38+
3639
/**
3740
* @var array taxonomy pages
3841
*/
@@ -69,4 +72,11 @@ function ($el) {
6972
}
7073
return $this->pages;
7174
}
75+
76+
public function prePersist()
77+
{
78+
$this->path = $this->getParentIds();
79+
$this->level = max(count(explode("/", $this->path))-1, 0);
80+
return parent::prePersist();
81+
}
7282
}

0 commit comments

Comments
 (0)