forked from thelia/thelia
/
Lang.php
118 lines (94 loc) · 3.5 KB
/
Lang.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
<?php
namespace Thelia\Model;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Propel;
use Thelia\Core\Event\Lang\LangEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Base\Lang as BaseLang;
use Thelia\Model\Map\LangTableMap;
class Lang extends BaseLang
{
// Constants to define behavior when a request string does not exists in the current language
const STRICTLY_USE_REQUESTED_LANGUAGE = 0;
const REPLACE_BY_DEFAULT_LANGUAGE = 1;
use \Thelia\Model\Tools\ModelEventDispatcherTrait;
protected static $defaultLanguage;
/**
* Return the default language object, using a local variable to cache it.
*
* @throws \RuntimeException
*/
public static function getDefaultLanguage()
{
if (null === self::$defaultLanguage) {
self::$defaultLanguage = LangQuery::create()->findOneByByDefault(1);
if (null === self::$defaultLanguage) {
throw new \RuntimeException("No default language is defined. Please define one.");
}
}
return self::$defaultLanguage;
}
public function toggleDefault()
{
if ($this->getId() === null) {
throw new \RuntimeException("impossible to just uncheck default language, choose a new one");
}
if (!$this->getByDefault()) {
$con = Propel::getWriteConnection(LangTableMap::DATABASE_NAME);
$con->beginTransaction();
try {
LangQuery::create()
->filterByByDefault(1)
->update(array('ByDefault' => 0), $con);
$this
->setVisible(1)
->setActive(1)
->setByDefault(1)
->save($con);
$con->commit();
} catch (PropelException $e) {
$con->rollBack();
throw $e;
}
}
}
public function preInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_CREATELANG, new LangEvent($this));
return true;
}
public function postInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_CREATELANG, new LangEvent($this));
$this->dispatchEvent(TheliaEvents::LANG_FIX_MISSING_FLAG, new LangEvent($this));
}
public function preUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_UPDATELANG, new LangEvent($this));
return true;
}
public function postUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_UPDATELANG, new LangEvent($this));
$this->dispatchEvent(TheliaEvents::LANG_FIX_MISSING_FLAG, new LangEvent($this));
}
public function preDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_DELETELANG, new LangEvent($this));
return true;
}
public function postDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_DELETELANG, new LangEvent($this));
}
public function preSave(ConnectionInterface $con = null)
{
// If the date/time format is not specified, generate it.
$dateTimeFormat = $this->getDateTimeFormat();
if (empty($dateTimeFormat)) {
$this->setDatetimeFormat(sprintf("%s %s", $this->getDateFormat(), $this->getTimeFormat()));
}
return true;
}
}