Conditions | 54 |
Paths | > 20000 |
Total Lines | 277 |
Code Lines | 158 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
38 | public function run($request) |
||
39 | { |
||
40 | $subsiteSupport = SubsiteHelper::usesSubsite(); |
||
41 | $fluentSupport = FluentHelper::usesFluent(); |
||
42 | |||
43 | echo 'Run with ?clear=1 to clear empty database before running the task<br/>'; |
||
44 | echo 'Run with ?overwrite=soft|hard to overwrite templates that exists in the cms. Soft will replace template if not modified by the user, hard will replace template even if modified by user.<br/>'; |
||
45 | echo 'Run with ?templates=xxx,yyy to specify which template should be imported<br/>'; |
||
46 | if ($subsiteSupport) { |
||
47 | echo 'Run with ?subsite=all|subsiteID to create email templates in all subsites (including main site) or only in the chosen subsite (if a subsite is active, it will be used by default).<br/>'; |
||
48 | } |
||
49 | if ($fluentSupport) { |
||
50 | echo 'Run with ?locales=fr,en to choose which locale to import.<br/>'; |
||
51 | } |
||
52 | echo '<strong>Remember to flush the templates/translations if needed</strong><br/>'; |
||
53 | echo '<hr/>'; |
||
54 | |||
55 | $overwrite = $request->getVar('overwrite'); |
||
56 | $clear = $request->getVar('clear'); |
||
57 | $templatesToImport = $request->getVar('templates'); |
||
58 | $importToSubsite = $request->getVar('subsite'); |
||
59 | $chosenLocales = $request->getVar('locales'); |
||
60 | |||
61 | // Normalize argument |
||
62 | if ($overwrite && $overwrite != 'soft' && $overwrite != 'hard') { |
||
63 | $overwrite = 'soft'; |
||
64 | } |
||
65 | |||
66 | // Select which subsite to import emails to |
||
67 | $subsites = []; |
||
68 | if ($subsiteSupport) { |
||
69 | if ($importToSubsite == 'all') { |
||
70 | $subsites = SubsiteHelper::listSubsites(); |
||
71 | } elseif (is_numeric($importToSubsite)) { |
||
72 | $subsites = SubsiteHelper::listSubsites(); |
||
73 | $subsiteTitle = 'Subsite #' . $importToSubsite; |
||
74 | foreach ($subsites as $subsite) { |
||
75 | if ($subsite->ID == $importToSubsite) { |
||
76 | $subsiteTitle = $subsite->Title; |
||
77 | } |
||
78 | } |
||
79 | $subsites = [ |
||
80 | $importToSubsite => $subsiteTitle |
||
81 | ]; |
||
82 | } |
||
83 | if (SubsiteHelper::currentSubsiteID()) { |
||
84 | DB::alteration_message("Importing to current subsite. Run from main site to import other subsites at once.", "created"); |
||
85 | $subsites = []; |
||
86 | } |
||
87 | if (!empty($subsites)) { |
||
88 | DB::alteration_message("Importing to subsites : " . implode(',', array_values($subsites)), "created"); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | if ($templatesToImport) { |
||
93 | $templatesToImport = explode(',', $templatesToImport); |
||
94 | } |
||
95 | |||
96 | // Do we clear our templates? |
||
97 | if ($clear == 1) { |
||
98 | DB::alteration_message("Clear all email templates", "created"); |
||
99 | $emailTemplates = EmailTemplate::get(); |
||
100 | foreach ($emailTemplates as $emailTemplate) { |
||
101 | $emailTemplate->delete(); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | $emailTemplateSingl = singleton(EmailTemplate::class); |
||
106 | |||
107 | $locales = null; |
||
108 | if ($fluentSupport && class_exists('TractorCow\Fluent\Model\Locale')) { |
||
109 | if (FluentHelper::isClassTranslated(EmailTemplate::class)) { |
||
110 | $locales = Locale::get()->column('Locale'); |
||
111 | |||
112 | // We collect only one locale, restrict the list |
||
113 | if ($chosenLocales) { |
||
114 | $arr = explode(',', $chosenLocales); |
||
115 | $locales = []; |
||
116 | foreach ($arr as $a) { |
||
117 | $a = FluentHelper::get_locale_from_lang($a); |
||
118 | $locales[] = $a; |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
124 | $defaultLocale = FluentHelper::get_locale(); |
||
125 | |||
126 | $templates = $this->collectTemplates(); |
||
127 | |||
128 | // don't throw errors |
||
129 | Config::modify()->set(i18n::class, 'missing_default_warning', false); |
||
130 | |||
131 | foreach ($templates as $filePath) { |
||
132 | $isOverwritten = false; |
||
133 | |||
134 | $fileName = basename($filePath, '.ss'); |
||
135 | |||
136 | // Remove base path |
||
137 | $relativeFilePath = str_replace(Director::baseFolder(), '', $filePath); |
||
138 | $relativeFilePathParts = explode('/', trim($relativeFilePath, '/')); |
||
139 | |||
140 | // Group by module |
||
141 | $moduleName = array_shift($relativeFilePathParts); |
||
142 | if ($moduleName == 'vendor') { |
||
143 | $moduleVendor = array_shift($relativeFilePathParts); |
||
144 | // get module name |
||
145 | $moduleName = $moduleVendor . '/' . array_shift($relativeFilePathParts); |
||
146 | } |
||
147 | |||
148 | // remove /templates part |
||
149 | array_shift($relativeFilePathParts); |
||
150 | $templateName = str_replace('.ss', '', implode('/', $relativeFilePathParts)); |
||
151 | |||
152 | $templateTitle = basename($templateName); |
||
153 | |||
154 | // Create the email code (basically, the template name without "Email" at the end) |
||
155 | $code = preg_replace('/Email$/', '', $fileName); |
||
156 | |||
157 | if (!empty($templatesToImport) && !in_array($code, $templatesToImport)) { |
||
158 | DB::alteration_message("Template with code <b>$code</b> was ignored.", "repaired"); |
||
159 | continue; |
||
160 | } |
||
161 | |||
162 | $whereCode = [ |
||
163 | 'Code' => $code |
||
164 | ]; |
||
165 | $emailTemplate = EmailTemplate::get()->filter($whereCode)->first(); |
||
166 | |||
167 | // Check if it has been modified or not |
||
168 | $templateModified = false; |
||
169 | if ($emailTemplate) { |
||
170 | $templateModified = $emailTemplate->Created != $emailTemplate->LastEdited; |
||
171 | } |
||
172 | |||
173 | if (!$overwrite && $emailTemplate) { |
||
174 | DB::alteration_message("Template with code <b>$code</b> already exists. Choose overwrite if you want to import again.", "repaired"); |
||
175 | continue; |
||
176 | } |
||
177 | if ($overwrite == 'soft' && $templateModified) { |
||
178 | DB::alteration_message("Template with code <b>$code</b> has been modified by the user. Choose overwrite=hard to change.", "repaired"); |
||
179 | continue; |
||
180 | } |
||
181 | |||
182 | // Create a default title from code |
||
183 | $title = preg_split('/(?=[A-Z])/', $code); |
||
184 | $title = implode(' ', $title); |
||
185 | |||
186 | // Get content of the email |
||
187 | $content = file_get_contents($filePath); |
||
188 | |||
189 | // Analyze content to find incompatibilities |
||
190 | $errors = self::checkContentForErrors($content); |
||
191 | if (!empty($errors)) { |
||
192 | echo "<div style='color:red'>Invalid syntax was found in '$relativeFilePath'. Please fix these errors before importing the template<ul>"; |
||
193 | foreach ($errors as $error) { |
||
194 | echo '<li>' . $error . '</li>'; |
||
195 | } |
||
196 | echo '</ul></div>'; |
||
197 | continue; |
||
198 | } |
||
199 | |||
200 | // Parse language |
||
201 | $module = ModuleLoader::getModule($moduleName); |
||
202 | $collector = new i18nTextCollector; |
||
203 | $entities = $collector->collectFromTemplate($content, $fileName, $module); |
||
204 | |||
205 | /* |
||
206 | array:1 [▼ |
||
207 | "MyEmail.SUBJECT" => "My subject" |
||
208 | ] |
||
209 | */ |
||
210 | |||
211 | $translationTable = []; |
||
212 | foreach ($entities as $entity => $data) { |
||
213 | if ($locales) { |
||
214 | foreach ($locales as $locale) { |
||
215 | i18n::set_locale($locale); |
||
216 | if (!isset($translationTable[$entity])) { |
||
217 | $translationTable[$entity] = []; |
||
218 | } |
||
219 | $translationTable[$entity][$locale] = i18n::_t($entity, $data); |
||
220 | } |
||
221 | i18n::set_locale($defaultLocale); |
||
222 | } else { |
||
223 | $translationTable[$entity] = [$defaultLocale => i18n::_t($entity, $data)]; |
||
224 | } |
||
225 | } |
||
226 | |||
227 | $contentLocale = []; |
||
228 | // May be null |
||
229 | if ($locales) { |
||
230 | foreach ($locales as $locale) { |
||
231 | $contentLocale[$locale] = $content; |
||
232 | } |
||
233 | } |
||
234 | if (!isset($contentLocale[$defaultLocale])) { |
||
235 | $contentLocale[$defaultLocale] = $content; |
||
236 | } |
||
237 | |||
238 | // Now we use our translation table to manually replace _t calls into file content |
||
239 | foreach ($translationTable as $entity => $translationData) { |
||
240 | // use the double escape notation |
||
241 | $escapedEntity = str_replace('\\', '\\\\\\\\', $entity); |
||
242 | // fix dot notation |
||
243 | $escapedEntity = str_replace('.', '\.', $escapedEntity); |
||
244 | $baseTranslation = null; |
||
245 | |||
246 | foreach ($translationData as $locale => $translation) { |
||
247 | if (!$baseTranslation && $translation) { |
||
248 | $baseTranslation = $translation; |
||
249 | } |
||
250 | if (!$translation) { |
||
251 | $translation = $baseTranslation; |
||
252 | } |
||
253 | // This regex should match old and new style |
||
254 | $count = 0; |
||
255 | $contentLocale[$locale] = preg_replace("/<%(t | _t\(')" . $escapedEntity . "( |').*?%>/ums", $translation, $contentLocale[$locale], -1, $count); |
||
256 | if (!$count) { |
||
257 | throw new Exception("Failed to replace $escapedEntity with translation $translation"); |
||
258 | } |
||
259 | } |
||
260 | } |
||
261 | |||
262 | // Create a template if necassery or mark as overwritten |
||
263 | if (!$emailTemplate) { |
||
264 | $emailTemplate = new EmailTemplate; |
||
265 | } else { |
||
266 | $isOverwritten = true; |
||
267 | } |
||
268 | |||
269 | // Other properties |
||
270 | $emailTemplate->Code = $code; |
||
271 | $emailTemplate->Category = $moduleName; |
||
272 | if (SubsiteHelper::currentSubsiteID() && !$emailTemplate->SubsiteID) { |
||
273 | $emailTemplate->SubsiteID = SubsiteHelper::currentSubsiteID(); |
||
274 | } |
||
275 | // Write to main site or current subsite |
||
276 | $emailTemplate->write(); |
||
277 | |||
278 | // Apply content to email after write to ensure we can localize properly |
||
279 | $this->assignContent($emailTemplate, $contentLocale[$defaultLocale]); |
||
280 | |||
281 | if (!empty($locales)) { |
||
282 | foreach ($locales as $locale) { |
||
283 | $this->assignContent($emailTemplate, $contentLocale[$locale], $locale); |
||
284 | } |
||
285 | } |
||
286 | |||
287 | // Reset date to allow tracking user edition (for soft/hard overwrite) |
||
288 | $this->resetLastEditedDate($emailTemplate->ID); |
||
289 | |||
290 | // Loop through subsites |
||
291 | if (!empty($importToSubsite)) { |
||
292 | SubsiteHelper::disableFilter(); |
||
293 | foreach ($subsites as $subsiteID => $subsiteTitle) { |
||
294 | $whereCode['SubsiteID'] = $subsiteID; |
||
295 | |||
296 | $subsiteEmailTemplate = EmailTemplate::get()->filter($whereCode)->first(); |
||
297 | |||
298 | $emailTemplateCopy = $emailTemplate; |
||
299 | $emailTemplateCopy->SubsiteID = $subsiteID; |
||
300 | if ($subsiteEmailTemplate) { |
||
301 | $emailTemplateCopy->ID = $subsiteEmailTemplate->ID; |
||
302 | } else { |
||
303 | $emailTemplateCopy->ID = 0; // New |
||
304 | } |
||
305 | $emailTemplateCopy->write(); |
||
306 | |||
307 | $this->resetLastEditedDate($emailTemplateCopy->ID); |
||
308 | } |
||
309 | } |
||
310 | |||
311 | if ($isOverwritten) { |
||
312 | DB::alteration_message("Overwrote <b>{$emailTemplate->Code}</b>", "created"); |
||
313 | } else { |
||
314 | DB::alteration_message("Imported <b>{$emailTemplate->Code}</b>", "created"); |
||
315 | } |
||
457 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths