|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Donatas Navidonskis <[email protected]> |
|
5
|
|
|
* @since 2017 |
|
6
|
|
|
* @class LangModule |
|
7
|
|
|
* |
|
8
|
|
|
* @property string Name |
|
9
|
|
|
* |
|
10
|
|
|
* @method DataList Entities |
|
11
|
|
|
*/ |
|
12
|
|
|
class LangModule extends DataObject { |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
* @config |
|
17
|
|
|
*/ |
|
18
|
|
|
private static $db = [ |
|
|
|
|
|
|
19
|
|
|
'Name' => 'Varchar', |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
* @config |
|
25
|
|
|
*/ |
|
26
|
|
|
private static $has_many = [ |
|
|
|
|
|
|
27
|
|
|
'Entities' => 'LangEntity', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Find module by name or create a new one if not existing. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $name |
|
34
|
|
|
* |
|
35
|
|
|
* @return LangModule |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function findOrCreate($name) { |
|
38
|
|
|
$module = static::get()->filter('Name', strtolower($name)); |
|
39
|
|
|
|
|
40
|
|
|
if ($module->exists()) { |
|
41
|
|
|
return $module->first(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$module = static::create(['Name' => $name]); |
|
45
|
|
|
$module->write(); |
|
46
|
|
|
|
|
47
|
|
|
return $module; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function onBeforeWrite() { |
|
51
|
|
|
$this->Name = strtolower($this->Name); |
|
52
|
|
|
|
|
53
|
|
|
parent::onBeforeWrite(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public static function getByName($name) { |
|
57
|
|
|
if(($module = static::get()->filter('Name', $name)->first()) instanceof LangModule) { |
|
58
|
|
|
return $module; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function mergeOrAddEntity($namespace, $value, $title = '', $merge = false) { |
|
65
|
|
|
// try to check is existing entity already |
|
66
|
|
|
$entity = LangEntity::get()->filter('Namespace', $namespace)->first(); |
|
67
|
|
|
|
|
68
|
|
|
if ($entity instanceof LangEntity) { |
|
69
|
|
|
if ($merge) { // if we can overwrite value |
|
70
|
|
|
$entity->Value = $value; |
|
71
|
|
|
|
|
72
|
|
|
if (! empty($title)) { |
|
73
|
|
|
$entity->Title = $title; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$entity->write(); |
|
77
|
|
|
|
|
78
|
|
|
if (! ($this->Entities()->byID($entity->ID) instanceof LangEntity)) { |
|
|
|
|
|
|
79
|
|
|
$entity->ModuleID = $this->ID; |
|
80
|
|
|
$entity->write(); |
|
81
|
|
|
|
|
82
|
|
|
$this->Entities()->add($entity); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
return $entity; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} else { // entity not existing |
|
88
|
|
|
$entity = LangEntity::create([ |
|
89
|
|
|
'Namespace' => $namespace, |
|
90
|
|
|
'Value' => $value, |
|
91
|
|
|
'Title' => $title, |
|
92
|
|
|
'ModuleID' => $this->ID, |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
|
|
if ($entity->write()) { |
|
96
|
|
|
$this->Entities()->add($entity); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
return $entity; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.