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