Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 100 |
|
0.00% |
0 / 18 |
CRAP | |
0.00% |
0 / 1 |
MailTemplates | |
0.00% |
0 / 100 |
|
0.00% |
0 / 18 |
1122 | |
0.00% |
0 / 1 |
readEntity | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
readList | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
readListWithoutProvider | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
readListByProvider | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
readCustomizedListForProvider | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
readTemplate | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
readTemplateById | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
deleteTemplateById | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
updateTemplateContent | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
updateTemplateContentById | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
createCustomizationForProvider | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
updateEntity | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
56 | |||
readProperty | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
replaceProperty | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
deleteProperty | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
fetchData | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getSpecifiedValue | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
mergeMailTemplatesWithCustomizations | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb; |
4 | |
5 | use BO\Zmsentities\Mailtemplate as Mailtemplate; |
6 | |
7 | class MailTemplates extends Base |
8 | { |
9 | /** |
10 | * |
11 | * @return \BO\Zmsentities\Mailtemplate |
12 | */ |
13 | public function readEntity() |
14 | { |
15 | $query = Query\Mailtemplate::QUERY_SELECT; |
16 | $config = $this->fetchData($query); |
17 | return $config; |
18 | } |
19 | |
20 | |
21 | public function readList() |
22 | { |
23 | $query = new Query\Mailtemplate(Query\Base::SELECT); |
24 | $query->addEntityMapping(); |
25 | $logList = new \BO\Zmsentities\Collection\MailtemplateList($this->fetchList($query, new Mailtemplate())); |
26 | return $logList; |
27 | } |
28 | |
29 | public function readListWithoutProvider() |
30 | { |
31 | $query = new Query\Mailtemplate(Query\Base::SELECT); |
32 | $query->addEntityMapping(); |
33 | $query->addConditionWithoutProvider(); |
34 | $logList = new \BO\Zmsentities\Collection\MailtemplateList($this->fetchList($query, new Mailtemplate())); |
35 | return $logList; |
36 | } |
37 | |
38 | public function readListByProvider($providerId) |
39 | { |
40 | $query = new Query\Mailtemplate(Query\Base::SELECT); |
41 | $query->addEntityMapping(); |
42 | $query->addConditionProviderId($providerId); |
43 | $logList = new \BO\Zmsentities\Collection\MailtemplateList($this->fetchList($query, new Mailtemplate())); |
44 | return $logList; |
45 | } |
46 | |
47 | public function readCustomizedListForProvider($providerId) |
48 | { |
49 | $generalTemplates = $this->readListWithoutProvider(); |
50 | $customTemplates = $this->readListByProvider($providerId); |
51 | return $this->mergeMailTemplatesWithCustomizations($generalTemplates, $customTemplates); |
52 | } |
53 | |
54 | |
55 | public function readTemplate($templateName) |
56 | { |
57 | $query = new Query\Mailtemplate(Query\Base::SELECT); |
58 | $query->addEntityMapping() |
59 | ->addConditionName($templateName); |
60 | return $this->fetchOne($query, new Mailtemplate()); |
61 | } |
62 | |
63 | public function readTemplateById($templateId) |
64 | { |
65 | $query = new Query\Mailtemplate(Query\Base::SELECT); |
66 | $query->addEntityMapping() |
67 | ->addConditionId($templateId); |
68 | return $this->fetchOne($query, new Mailtemplate()); |
69 | } |
70 | |
71 | public function deleteTemplateById($templateId) |
72 | { |
73 | $query = new Query\Mailtemplate(Query\Base::DELETE); |
74 | $query->addConditionId($templateId); |
75 | return $this->deleteItem($query); |
76 | } |
77 | |
78 | public function updateTemplateContent($templateName, $templateContent) |
79 | { |
80 | $query = new Query\Mailtemplate(Query\Base::UPDATE); |
81 | $query->addConditionName($templateName); |
82 | $query->addTemplateContent($templateContent); |
83 | $this->writeItem($query); |
84 | return $this->readTemplate($templateName); |
85 | //return $this->readEntity($templateName, 1); |
86 | } |
87 | |
88 | public function updateTemplateContentById($templateId, $templateContent) |
89 | { |
90 | $query = new Query\Mailtemplate(Query\Base::UPDATE); |
91 | $query->addConditionId($templateId); |
92 | $query->addTemplateContent($templateContent); |
93 | $this->writeItem($query); |
94 | return $this->readTemplateById($templateId); |
95 | //return $this->readEntity($templateName, 1); |
96 | } |
97 | |
98 | |
99 | public function createCustomizationForProvider($providerId, $templateName, $templateContent) |
100 | { |
101 | $query = new Query\Mailtemplate(Query\Base::INSERT); |
102 | $query->addValues(array( |
103 | 'name' => $templateName, |
104 | 'value' => $templateContent, |
105 | 'provider' => $providerId, |
106 | 'changeTimestamp' => (new \DateTimeImmutable())->format('Y-m-d H:i:s') |
107 | )); |
108 | $this->writeItem($query); |
109 | return $this->readTemplate($templateName); |
110 | } |
111 | |
112 | public function updateEntity(MailTemplate $config) |
113 | { |
114 | $compareEntity = $this->readEntity(); |
115 | $result = false; |
116 | $query = new Query\Mailtemplate(Query\Base::REPLACE); |
117 | foreach ($config as $key => $item) { |
118 | if (is_array($item)) { |
119 | foreach ($item as $itemName => $itemValue) { |
120 | if ($itemValue && $compareEntity->getPreference($key, $itemName) != $itemValue) { |
121 | $query->addValues(array( |
122 | 'name' => $key . '__' . $itemName, |
123 | 'value' => $this->getSpecifiedValue($itemValue), |
124 | 'changeTimestamp' => (new \DateTimeImmutable())->format('Y-m-d H:i:s') |
125 | )); |
126 | $result = $this->writeItem($query); |
127 | } |
128 | } |
129 | } else { |
130 | $query->addValues(array( |
131 | 'name' => $key, |
132 | 'value' => $this->getSpecifiedValue($item), |
133 | 'changeTimestamp' => (new \DateTimeImmutable())->format('Y-m-d H:i:s') |
134 | )); |
135 | $result = $this->writeItem($query); |
136 | } |
137 | } |
138 | return ($result) ? $this->readEntity() : null; |
139 | } |
140 | |
141 | public function readProperty($property, $forUpdate = false) |
142 | { |
143 | $sql = Query\Mailtemplate::QUERY_SELECT_PROPERTY; |
144 | if ($forUpdate) { |
145 | $sql .= " FOR UPDATE"; |
146 | } |
147 | return $this->fetchValue($sql, [$property]); |
148 | } |
149 | |
150 | public function replaceProperty($property, $value) |
151 | { |
152 | return $this->perform(Query\Mailtemplate::QUERY_REPLACE_PROPERTY, [ |
153 | 'property' => $property, |
154 | 'value' => $value, |
155 | ]); |
156 | } |
157 | |
158 | /** |
159 | * remove config data |
160 | * |
161 | * |
162 | * @return Resource Status |
163 | */ |
164 | public function deleteProperty($property) |
165 | { |
166 | $query = new Query\Mailtemplate(Query\Base::DELETE); |
167 | $query->addConditionName($property); |
168 | return $this->deleteItem($query); |
169 | } |
170 | |
171 | protected function fetchData($querySql) |
172 | { |
173 | $splittedHash = array(); |
174 | $dataList = $this->getReader()->fetchAll($querySql); |
175 | foreach ($dataList as $data) { |
176 | $splittedHash[$data['name']] = $data['value']; |
177 | } |
178 | |
179 | return new Mailtemplate($splittedHash); |
180 | } |
181 | |
182 | protected function getSpecifiedValue($value) |
183 | { |
184 | if (is_bool($value)) { |
185 | return ($value) ? 1 : 0; |
186 | } |
187 | return trim($value); |
188 | } |
189 | |
190 | protected function mergeMailTemplatesWithCustomizations($generalTemplates, $customTemplates) |
191 | { |
192 | |
193 | $customTemplatesByName = []; |
194 | |
195 | if ($customTemplates) { |
196 | foreach ($customTemplates as $template) { |
197 | $customTemplatesByName[$template['name']] = $template; |
198 | } |
199 | } |
200 | |
201 | $mergedTemplates = []; |
202 | |
203 | if ($generalTemplates) { |
204 | foreach ($generalTemplates as $template) { |
205 | if (isset($customTemplatesByName[$template['name']])) { |
206 | $mergedTemplates[] = $customTemplatesByName[$template['name']]; |
207 | } else { |
208 | $mergedTemplates[] = $template; |
209 | } |
210 | } |
211 | } |
212 | |
213 | return $mergedTemplates; |
214 | } |
215 | } |