/** * Imports mail templates from a YAML file. * * @param string $locale Locale code. * @param string $file Full path to the file that should be imported. */ public static function importTemplates($locale, $file) { // Get new mail templates $parser = new YamlParser(); $new_templates = $parser->parse(file_get_contents($file)); if (empty($new_templates)) { // Nothing to import. return; } foreach ($new_templates as $name => $template_info) { // Validate the template $is_valid_template = is_array($template_info) && array_key_exists('subject', $template_info) && array_key_exists('body', $template_info); if (!$is_valid_template) { throw new \RuntimeException(sprintf('An invalid mail template "%s" is found in "%s".', $name, $file)); } if (!Template::loadByName($name, $locale, true)) { // Import only templates that are not already in the database. $template = new Template($name, $locale); $template->subject = $template_info['subject']; $template->body = $template_info['body']; $template->save(); } } }
/** * Loads mail template. * * It's just a wrapper for {@link \Mibew\Mail\Template::loadByName()} * method which creates an empty template if it does not exist. * * @param string $name Machine name of the template * @param string $locale Locale code which should be used for template * loading. * @return \Mibew\Mail\Template Mail template. */ protected function loadMailTemplate($name, $locale) { $template = MailTemplate::loadByName($name, $locale); if (!$template) { // Create an empty template. It can be helpful in the case a new // template is added to the system. $template = new MailTemplate($name, $locale); $template->save(); } return $template; }