plugins/generic/translator/TranslatorAction.inc.php

Go to the documentation of this file.
00001 <?php
00002 
00015 //$Id$
00016 
00017 class TranslatorAction {
00022    function export($locale) {
00023       // Construct the tar command
00024       $tarBinary = Config::getVar('cli', 'tar');
00025       if (empty($tarBinary) || !file_exists($tarBinary)) {
00026          // We can use fatalError() here as we already have a user
00027          // friendly way of dealing with the missing tar on the
00028          // index page.
00029          fatalError('The tar binary must be configured in config.inc.php\'s cli section to use the export function of this plugin!');
00030       }
00031       $command = $tarBinary.' cz';
00032 
00033       $localeFilesList = TranslatorAction::getLocaleFiles($locale);
00034       $localeFilesList = array_merge($localeFilesList, TranslatorAction::getMiscLocaleFiles($locale));
00035       $emailTemplateDao =& DAORegistry::getDAO('EmailTemplateDAO');
00036       $localeFilesList[] = $emailTemplateDao->getMainEmailTemplateDataFilename($locale);
00037       foreach (array_values(TranslatorAction::getEmailFileMap($locale)) as $emailFile) {
00038       }
00039 
00040       // Include locale files (main file and plugin files)
00041       foreach ($localeFilesList as $file) {
00042          if (file_exists($file)) $command .= ' ' . escapeshellarg($file);
00043       }
00044 
00045       header('Content-Type: application/x-gtar');
00046       header("Content-Disposition: attachment; filename=\"$locale.tar.gz\"");
00047       header('Cache-Control: private'); // Workarounds for IE weirdness
00048       passthru($command);
00049    }
00050 
00051    function getLocaleFiles($locale) {
00052       if (!AppLocale::isLocaleValid($locale)) return null;
00053 
00054       $localeFiles = AppLocale::getFilenameComponentMap($locale);
00055       $plugins =& PluginRegistry::loadAllPlugins();
00056       foreach (array_keys($plugins) as $key) {
00057          $plugin =& $plugins[$key];
00058          $localeFile = $plugin->getLocaleFilename($locale);
00059          if (!empty($localeFile)) $localeFiles[] = $localeFile;
00060          unset($plugin);
00061       }
00062       return $localeFiles;
00063    }
00064 
00065    function getMiscLocaleFiles($locale) {
00066       $countryDao =& DAORegistry::getDAO('CountryDAO');
00067       $currencyDao =& DAORegistry::getDAO('CurrencyDAO');
00068       return array(
00069          $countryDao->getFilename($locale),
00070          $currencyDao->getCurrencyFilename($locale)
00071       );
00072    }
00073 
00074    function getEmailFileMap($locale) {
00075       $emailTemplateDao =& DAORegistry::getDAO('EmailTemplateDAO');
00076       $files = array($emailTemplateDao->getMainEmailTemplatesFilename() => $emailTemplateDao->getMainEmailTemplateDataFilename($locale));
00077       $categories = PluginRegistry::getCategories();
00078       foreach ($categories as $category) {
00079          $plugins =& PluginRegistry::loadCategory($category, true);
00080          if (is_array($plugins)) foreach (array_keys($plugins) as $name) {
00081             $plugin =& $plugins[$name];
00082             $templatesFile = $plugin->getInstallEmailTemplatesFile();
00083             if ($templatesFile) {
00084                $files[$templatesFile] = str_replace('{$installedLocale}', $locale, $plugin->getInstallEmailTemplateDataFile());
00085             }
00086             unset($plugin);
00087          }
00088          unset($plugins);
00089       }
00090       return $files;
00091    }
00092 
00093    function getEmailTemplates($locale) {
00094       $files = TranslatorAction::getEmailFileMap($locale);
00095       $returner = array();
00096       foreach ($files as $templateFile => $templateDataFile) {
00097          $xmlParser = new XMLParser();
00098          $data = null;
00099          if (file_exists($templateDataFile)) $data =& $xmlParser->parse($templateDataFile, array('email'));
00100          if ($data) foreach ($data->getChildren() as $emailNode) {
00101             $returner[$emailNode->getAttribute('key')] = array(
00102                'subject' => $emailNode->getChildValue('subject'),
00103                'body' => $emailNode->getChildValue('body'),
00104                'description' => $emailNode->getChildValue('description'),
00105                'templateFile' => $templateFile,
00106                'templateDataFile' => $templateDataFile
00107             );
00108          }
00109          unset($xmlParser, $data);
00110       }
00111       return $returner;
00112    }
00113 
00114    function isLocaleFile($locale, $filename) {
00115       if (in_array($filename, TranslatorAction::getLocaleFiles($locale))) return true;
00116       if (in_array($filename, TranslatorAction::getMiscLocaleFiles($locale))) return true;
00117       $emailTemplateDao =& DAORegistry::getDAO('EmailTemplateDAO');
00118       if ($filename == $emailTemplateDao->getMainEmailTemplateDataFilename($locale)) return true;
00119       return false;
00120    }
00121 
00122    function determineReferenceFilename($locale, $filename) {
00123       // FIXME: This is ugly.
00124       return str_replace($locale, MASTER_LOCALE, $filename);
00125    }
00126 
00134    function testLocale($locale, $referenceLocale) {
00135       $localeFileNames = AppLocale::getFilenameComponentMap($locale);
00136 
00137       $errors = array();
00138       foreach ($localeFileNames as $localeFileName) {
00139          $referenceLocaleFileName = str_replace($locale, $referenceLocale, $localeFileName);
00140          $localeFile = new LocaleFile($locale, $localeFileName);
00141          $referenceLocaleFile = new LocaleFile($referenceLocale, $referenceLocaleFileName);
00142          $errors = array_merge_recursive($errors, $localeFile->testLocale($referenceLocaleFile));
00143          unset($localeFile);
00144          unset($referenceLocaleFile);
00145       }
00146 
00147       $plugins =& PluginRegistry::loadAllPlugins();
00148       foreach (array_keys($plugins) as $key) {
00149          $plugin =& $plugins[$key];
00150          $referenceLocaleFilename = $plugin->getLocaleFilename($referenceLocale);
00151          if ($referenceLocaleFilename) {
00152             $localeFile = new LocaleFile($locale, $plugin->getLocaleFilename($locale));
00153             $referenceLocaleFile = new LocaleFile($referenceLocale, $referenceLocaleFilename);
00154             $errors = array_merge_recursive($errors, $localeFile->testLocale($referenceLocaleFile));
00155             unset($localeFile);
00156             unset($referenceLocaleFile);
00157          }
00158          unset($plugin);
00159       }
00160       return $errors;
00161    }
00162 
00170    function testEmails($locale, $referenceLocale) {
00171       $errors = array(
00172       );
00173 
00174       $emails = TranslatorAction::getEmailTemplates($locale);
00175       $referenceEmails = TranslatorAction::getEmailTemplates($referenceLocale);
00176 
00177       // Pass 1: For all translated emails, check that they match
00178       // against reference translations.
00179       foreach ($emails as $emailKey => $email) {
00180          // Check if a matching reference email was found.
00181          if (!isset($referenceEmails[$emailKey])) {
00182             $errors[EMAIL_ERROR_EXTRA_EMAIL][] = array(
00183                'key' => $emailKey
00184             );
00185             continue;
00186          }
00187 
00188          // We've successfully found a matching reference email.
00189          // Compare it against the translation.
00190          $bodyParams = AppLocale::getParameterNames($email['body']);
00191          $referenceBodyParams = AppLocale::getParameterNames($referenceEmails[$emailKey]['body']);
00192          $diff = array_diff($bodyParams, $referenceBodyParams);
00193          if (!empty($diff)) {
00194             $errors[EMAIL_ERROR_DIFFERING_PARAMS][] = array(
00195                'key' => $emailKey,
00196                'mismatch' => $diff
00197             );
00198          }
00199 
00200          $subjectParams = AppLocale::getParameterNames($email['subject']);
00201          $referenceSubjectParams = AppLocale::getParameterNames($referenceEmails[$emailKey]['subject']);
00202 
00203          $diff = array_diff($subjectParams, $referenceSubjectParams);
00204          if (!empty($diff)) {
00205             $errors[EMAIL_ERROR_DIFFERING_PARAMS][] = array(
00206                'key' => $emailKey,
00207                'mismatch' => $diff
00208             );
00209          }
00210 
00211          $matchedReferenceEmails[] = $emailKey;
00212 
00213          unset($email);
00214          unset($referenceEmail);
00215       }
00216 
00217       // Pass 2: Make sure that there are no missing translations.
00218       foreach ($referenceEmails as $emailKey => $email) {
00219          // Extract the fields from the email to be tested.
00220          if (!isset($emails[$emailKey])) {
00221             $errors[EMAIL_ERROR_MISSING_EMAIL][] = array(
00222                'key' => $emailKey
00223             );
00224          }
00225       }
00226 
00227       return $errors;
00228    }
00229 }
00230 
00231 ?>

Generated on 25 Jul 2013 for Open Conference Systems by  doxygen 1.4.7