forked from thelia-modules/PopIn
/
PopIn.php
151 lines (129 loc) · 4.4 KB
/
PopIn.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
<?php
namespace PopIn;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Propel;
use Thelia\Core\Template\TemplateDefinition;
use Thelia\Core\Translation\Translator;
use Thelia\Install\Database;
use Thelia\Model\Config;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Folder;
use Thelia\Model\FolderQuery;
use Thelia\Model\Lang;
use Thelia\Model\LangQuery;
use Thelia\Module\BaseModule;
/**
* PopIn module.
*/
class PopIn extends BaseModule
{
const MESSAGE_DOMAIN = "popin";
const MESSAGE_DOMAIN_BO = "popin.bo.default";
const MESSAGE_DOMAIN_FO = "popin.fo.default";
const ROUTER = "router.popin";
const CONF_KEY_IMAGE_FOLDER_ID = "popin.image_folder_id";
public function postActivation(ConnectionInterface $con = null)
{
$database = new Database($con);
$database->insertSql(null, [__DIR__ . "/Config/create.sql", __DIR__ . "/Config/insert.sql"]);
$this->loadBackOfficeTranslationResources();
$this->createPopInImageFolder();
}
public function getHooks()
{
return [
[
"type" => TemplateDefinition::FRONT_OFFICE,
"code" => "pop-in.content",
"title" => [
"en_US" => "Pop-in content",
"fr_FR" => "Contenu de la pop-in",
],
"block" => false,
"active" => true,
]
];
}
/**
* Load the back-office translations into the translator.
*/
protected function loadBackOfficeTranslationResources()
{
/** @var Lang $lang */
foreach (LangQuery::create()->find() as $lang) {
Translator::getInstance()->addResource(
"php",
__DIR__ . "/I18n/backOffice/default/" . $lang->getLocale() . ".php",
$lang->getLocale(),
static::MESSAGE_DOMAIN_BO
);
}
}
/**
* Create and save the id of the pop-in images folder if necessary.
*
* @throws \Exception
* @throws PropelException
*/
protected function createPopInImageFolder()
{
$imageFolderIdConfig = ConfigQuery::create()->findOneByName(static::CONF_KEY_IMAGE_FOLDER_ID);
if (null !== $imageFolderIdConfig && null !== FolderQuery::create()->findPk($imageFolderIdConfig->getValue())) {
// we already have and know our images folder
return;
}
Propel::getConnection()->beginTransaction();
try {
// create the folder
$folder = new Folder();
$folder->setVisible(true);
/** @var Lang $lang */
foreach (LangQuery::create()->find() as $lang) {
$localizedTitle = Translator::getInstance()->trans(
"Pop-in images",
[],
static::MESSAGE_DOMAIN_BO,
$lang->getLocale(),
false
);
if ($localizedTitle == "") {
continue;
}
$folder
->setLocale($lang->getLocale())
->setTitle($localizedTitle);
}
$folder->save();
// save the folder id in configuration
if ($folder->getId() !== null) {
$config = new Config();
$config
->setName(static::CONF_KEY_IMAGE_FOLDER_ID)
->setValue($folder->getId())
->setHidden(false);
/** @var Lang $lang */
foreach (LangQuery::create()->find() as $lang) {
$localizedTitle = Translator::getInstance()->trans(
"Pop-in images folder id",
[],
static::MESSAGE_DOMAIN_BO,
$lang->getLocale(),
false
);
if ($localizedTitle == "") {
continue;
}
$config
->setLocale($lang->getLocale())
->setTitle($localizedTitle);
}
$config->save();
}
} catch (\Exception $e) {
Propel::getConnection()->rollBack();
throw $e;
}
Propel::getConnection()->commit();
}
}