forked from thelia/thelia
/
Folder.php
132 lines (105 loc) · 3.2 KB
/
Folder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Thelia\Model;
use Thelia\Core\Event\Folder\FolderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Files\FileModelParentInterface;
use Thelia\Model\Base\Folder as BaseFolder;
use Propel\Runtime\Connection\ConnectionInterface;
class Folder extends BaseFolder implements FileModelParentInterface
{
use \Thelia\Model\Tools\ModelEventDispatcherTrait;
use \Thelia\Model\Tools\PositionManagementTrait;
use \Thelia\Model\Tools\UrlRewritingTrait;
/**
* {@inheritDoc}
*/
public function getRewrittenUrlViewName()
{
return 'folder';
}
/**
* @return int number of contents for the folder
*/
public function countChild()
{
return FolderQuery::countChild($this->getId());
}
/**
*
* count all products for current category and sub categories
*
* @return int
*/
public function countAllContents()
{
$children = FolderQuery::findAllChild($this->getId());
array_push($children, $this);
$contentsCount = 0;
foreach ($children as $child) {
$contentsCount += ContentQuery::create()
->filterByFolder($child)
->count();
}
return $contentsCount;
}
/**
* Get the root folder
* @param int $folderId
* @return mixed
*/
public function getRoot($folderId)
{
$folder = FolderQuery::create()->findPk($folderId);
if (0 !== $folder->getParent()) {
$parentFolder = FolderQuery::create()->findPk($folder->getParent());
if (null !== $parentFolder) {
$folderId = $this->getRoot($parentFolder->getId());
}
}
return $folderId;
}
/**
* Calculate next position relative to our parent
*/
protected function addCriteriaToPositionQuery($query)
{
$query->filterByParent($this->getParent());
}
/**
* {@inheritDoc}
*/
public function preInsert(ConnectionInterface $con = null)
{
$this->setPosition($this->getNextPosition());
$this->dispatchEvent(TheliaEvents::BEFORE_CREATEFOLDER, new FolderEvent($this));
return true;
}
public function postInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_CREATEFOLDER, new FolderEvent($this));
}
public function preUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_UPDATEFOLDER, new FolderEvent($this));
return true;
}
public function postUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_UPDATEFOLDER, new FolderEvent($this));
}
public function preDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_DELETEFOLDER, new FolderEvent($this));
$this->reorderBeforeDelete(
array(
"parent" => $this->getParent(),
)
);
return true;
}
public function postDelete(ConnectionInterface $con = null)
{
$this->markRewrittenUrlObsolete();
$this->dispatchEvent(TheliaEvents::AFTER_DELETEFOLDER, new FolderEvent($this));
}
}