forked from thelia/thelia
/
Module.php
237 lines (201 loc) · 6.38 KB
/
Module.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
namespace Thelia\Model;
use Propel\Runtime\Connection\ConnectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Thelia\Core\Template\TemplateDefinition;
use Thelia\Model\Base\Module as BaseModule;
use Thelia\Model\Tools\ModelEventDispatcherTrait;
use Thelia\Model\Tools\PositionManagementTrait;
use Thelia\Module\BaseModuleInterface;
class Module extends BaseModule
{
use ModelEventDispatcherTrait;
use PositionManagementTrait;
const ADMIN_INCLUDES_DIRECTORY_NAME = "AdminIncludes";
public function postSave(ConnectionInterface $con = null)
{
ModuleQuery::resetActivated();
}
public function getTranslationDomain()
{
return strtolower($this->getCode());
}
public function getAdminIncludesTranslationDomain()
{
return $this->getTranslationDomain().'.ai';
}
public function getAbsoluteBackOfficeTemplatePath($subdir)
{
return sprintf("%s".DS."%s".DS."%s",
$this->getAbsoluteTemplateBasePath(),
TemplateDefinition::BACK_OFFICE_SUBDIR,
$subdir
);
}
public function getAbsoluteBackOfficeI18nTemplatePath($subdir)
{
return sprintf("%s".DS."%s".DS."%s",
$this->getAbsoluteI18nPath(),
TemplateDefinition::BACK_OFFICE_SUBDIR,
$subdir
);
}
public function getBackOfficeTemplateTranslationDomain($templateName)
{
return $this->getTranslationDomain(). '.bo.' . $templateName;
}
public function getAbsoluteFrontOfficeTemplatePath($subdir)
{
return sprintf("%s".DS."%s".DS."%s",
$this->getAbsoluteTemplateBasePath(),
TemplateDefinition::FRONT_OFFICE_SUBDIR,
$subdir
);
}
public function getAbsoluteFrontOfficeI18nTemplatePath($subdir)
{
return sprintf("%s".DS."%s".DS."%s",
$this->getAbsoluteI18nPath(),
TemplateDefinition::FRONT_OFFICE_SUBDIR,
$subdir
);
}
public function getFrontOfficeTemplateTranslationDomain($templateName)
{
return $this->getTranslationDomain(). '.fo.' . $templateName;
}
/**
* @return the module's base directory path, relative to THELIA_MODULE_DIR
*/
public function getBaseDir()
{
return ucfirst($this->getCode());
}
/**
* @return the module's base directory path, relative to THELIA_MODULE_DIR
*/
public function getAbsoluteBaseDir()
{
return THELIA_MODULE_DIR . $this->getBaseDir();
}
/**
* @return the module's config directory path, relative to THELIA_MODULE_DIR
*/
public function getConfigPath()
{
return $this->getBaseDir() . DS . "Config";
}
/**
* @return the module's config absolute directory path
*/
public function getAbsoluteConfigPath()
{
return THELIA_MODULE_DIR . $this->getConfigPath();
}
/**
* @return the module's i18N directory path, relative to THELIA_MODULE_DIR
*/
public function getI18nPath()
{
return $this->getBaseDir() . DS . "I18n";
}
/**
* @return the module's i18N absolute directory path
*/
public function getAbsoluteI18nPath()
{
return THELIA_MODULE_DIR . $this->getI18nPath();
}
/**
* @return the module's AdminIncludes absolute directory path
*/
public function getAbsoluteAdminIncludesPath()
{
return $this->getAbsoluteBaseDir() . DS . self::ADMIN_INCLUDES_DIRECTORY_NAME;
}
/**
* @return the module's AdminIncludes i18N absolute directory path
*/
public function getAbsoluteAdminIncludesI18nPath()
{
return THELIA_MODULE_DIR . $this->getI18nPath() . DS . self::ADMIN_INCLUDES_DIRECTORY_NAME;
}
/**
* Return the absolute path to the module's template directory
*
* @return string a path
*/
public function getAbsoluteTemplateBasePath()
{
return $this->getAbsoluteBaseDir() . DS . 'templates';
}
/**
* Return the absolute path to one of the module's template directories
*
* @param int $templateSubdirName the name of the, probably one of TemplateDefinition::xxx_SUBDIR constants
* @return string a path
*/
public function getAbsoluteTemplateDirectoryPath($templateSubdirName)
{
return $this->getAbsoluteTemplateBasePath() .DS. $templateSubdirName;
}
/**
* @return true if this module is a delivery module
*/
public function isDeliveryModule()
{
$moduleReflection = new \ReflectionClass($this->getFullNamespace());
return $moduleReflection->implementsInterface("Thelia\Module\DeliveryModuleInterface");
}
/**
* @return true if this module is a payment module
*/
public function isPayementModule()
{
$moduleReflection = new \ReflectionClass($this->getFullNamespace());
return $moduleReflection->implementsInterface("Thelia\Module\PaymentModuleInterface");
}
/**
* @return bool true if the module image has been deployed, false otherwise.
*/
public function isModuleImageDeployed()
{
return ModuleImageQuery::create()->filterByModuleId($this->getId())->count() > 0;
}
/**
* @param ContainerInterface $container the Thelia container
* @return BaseModuleInterface a module instance
* @throws \InvalidArgumentException if the module could not be found in the container/
*/
public function getModuleInstance(ContainerInterface $container)
{
$instance = $container->get(sprintf('module.%s', $this->getCode()));
if ($instance == null) {
throw new \InvalidArgumentException(sprintf('Undefined module in container: "%s"', $this->getCode()));
}
return $instance;
}
/**
* @return BaseModule a new module instance.
*/
public function createInstance()
{
$moduleClass = new \ReflectionClass($this->getFullNamespace());
return $moduleClass->newInstance();
}
/**
* Calculate next position relative to module type
*/
protected function addCriteriaToPositionQuery($query)
{
$query->filterByType($this->getType());
}
/**
* {@inheritDoc}
*/
public function preInsert(ConnectionInterface $con = null)
{
$this->setPosition($this->getNextPosition());
return true;
}
}