• Main Page
  • Modules
  • Classes
  • Files
  • File List

plugins/generic/translator/TranslatorHandler.inc.php

00001 <?php
00002 
00015 require_once('TranslatorAction.inc.php');
00016 import('classes.handler.Handler');
00017 
00018 // Import helper to be used while this plugin is not modernized with OMP UI.
00019 import('classes.plugins.LegacyPluginHelper');
00020 
00021 class TranslatorHandler extends Handler {
00022    var $plugin;
00023 
00027    function TranslatorHandler() {
00028       parent::Handler();
00029       $this->addCheck(new HandlerValidatorRoles($this, true, null, null, array(ROLE_ID_SITE_ADMIN)));
00030 
00031       $plugin =& Registry::get('plugin');
00032       $this->plugin =& $plugin;
00033    }
00034 
00035    function getEmailTemplateFilename($locale) {
00036       return 'locale/' . $locale . '/emailTemplates.xml';
00037    }
00038 
00039    function index() {
00040       $this->validate();
00041       $plugin =& $this->plugin;
00042       $this->setupTemplate(false);
00043 
00044       $rangeInfo = Handler::getRangeInfo('locales');
00045 
00046       $templateMgr =& TemplateManager::getManager();
00047       import('lib.pkp.classes.core.ArrayItemIterator');
00048       $templateMgr->assign('locales', new ArrayItemIterator(AppLocale::getAllLocales(), $rangeInfo->getPage(), $rangeInfo->getCount()));
00049       $templateMgr->assign('masterLocale', MASTER_LOCALE);
00050 
00051       // Test whether the tar binary is available for the export to work
00052       $tarBinary = Config::getVar('cli', 'tar');
00053       $templateMgr->assign('tarAvailable', !empty($tarBinary) && file_exists($tarBinary));
00054 
00055       return $templateMgr->fetchJson($plugin->getTemplatePath() . 'index.tpl');
00056    }
00057 
00058    function setupTemplate($subclass = true) {
00059       parent::setupTemplate();
00060       $templateMgr =& TemplateManager::getManager();
00061       AppLocale::requireComponents(LOCALE_COMPONENT_PKP_ADMIN, LOCALE_COMPONENT_PKP_MANAGER);
00062       $templateMgr->assign('helpTopicId', 'plugins.generic.TranslatorPlugin');
00063    }
00064 
00065    function edit($args) {
00066       $this->validate();
00067       $plugin =& $this->plugin;
00068       $this->setupTemplate();
00069 
00070       $locale = array_shift($args);
00071       $file = array_shift($args);
00072 
00073       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00074       $localeFiles = TranslatorAction::getLocaleFiles($locale);
00075       $miscFiles = TranslatorAction::getMiscLocaleFiles($locale);
00076       $emails = TranslatorAction::getEmailTemplates($locale);
00077 
00078       $templateMgr =& TemplateManager::getManager();
00079 
00080       $localeFilesRangeInfo = Handler::getRangeInfo('localeFiles');
00081       $miscFilesRangeInfo = Handler::getRangeInfo('miscFiles');
00082       $emailsRangeInfo = Handler::getRangeInfo('emails');
00083 
00084       import('lib.pkp.classes.core.ArrayItemIterator');
00085       $templateMgr->assign('localeFiles', new ArrayItemIterator($localeFiles, $localeFilesRangeInfo->getPage(), $localeFilesRangeInfo->getCount()));
00086       $templateMgr->assign('miscFiles', new ArrayItemIterator($miscFiles, $miscFilesRangeInfo->getPage(), $miscFilesRangeInfo->getCount()));
00087       $templateMgr->assign('emails', new ArrayItemIterator($emails, $emailsRangeInfo->getPage(), $emailsRangeInfo->getCount()));
00088 
00089       $templateMgr->assign('locale', $locale);
00090       $templateMgr->assign('masterLocale', MASTER_LOCALE);
00091 
00092       return $templateMgr->fetchJson($plugin->getTemplatePath() . 'locale.tpl');
00093    }
00094 
00095    function check($args) {
00096       $this->validate();
00097       $plugin =& $this->plugin;
00098       $this->setupTemplate();
00099 
00100       $locale = array_shift($args);
00101       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00102 
00103       $localeFiles = TranslatorAction::getLocaleFiles($locale);
00104       $unwriteableFiles = array();
00105       foreach ($localeFiles as $localeFile) {
00106          $filename = Core::getBaseDir() . DIRECTORY_SEPARATOR . $localeFile;
00107          if (file_exists($filename) && !is_writeable($filename)) {
00108             $unwriteableFiles[] = $localeFile;
00109          }
00110       }
00111 
00112       $templateMgr =& TemplateManager::getManager();
00113       $templateMgr->assign('locale', $locale);
00114       $templateMgr->assign('errors', TranslatorAction::testLocale($locale, MASTER_LOCALE));
00115       $templateMgr->assign('emailErrors', TranslatorAction::testEmails($locale, MASTER_LOCALE));
00116       $templateMgr->assign('localeFiles', TranslatorAction::getLocaleFiles($locale));
00117       if(!empty($unwriteableFiles)) {
00118          $templateMgr->assign('error', true);
00119          $templateMgr->assign('unwriteableFiles', $unwriteableFiles);
00120       }
00121       return $templateMgr->fetchJson($plugin->getTemplatePath() . 'errors.tpl');
00122    }
00123 
00128    function export($args) {
00129       $this->validate();
00130       $plugin =& $this->plugin;
00131       $this->setupTemplate();
00132 
00133       $locale = array_shift($args);
00134       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00135 
00136       TranslatorAction::export($locale);
00137    }
00138 
00139    function saveLocaleChanges($args) {
00140       $this->validate();
00141       $plugin =& $this->plugin;
00142       $this->setupTemplate();
00143 
00144       $locale = array_shift($args);
00145       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00146 
00147       $localeFiles = TranslatorAction::getLocaleFiles($locale);
00148 
00149       $changesByFile = array();
00150 
00151       // Arrange the list of changes to save into an array by file.
00152       $stack = Request::getUserVar('stack');
00153       while (!empty($stack)) {
00154          $filename = array_shift($stack);
00155          $key = array_shift($stack);
00156          $value = array_shift($stack);
00157          if (in_array($filename, $localeFiles)) {
00158             $changesByFile[$filename][$key] = $this->correctCr($value);
00159          }
00160       }
00161 
00162       // Save the changes file by file.
00163       import('lib.pkp.classes.file.EditableLocaleFile');
00164       foreach ($changesByFile as $filename => $changes) {
00165          $file = new EditableLocaleFile($locale, $filename);
00166          foreach ($changes as $key => $value) {
00167             if (empty($value)) continue;
00168             if (!$file->update($key, $value)) {
00169                $file->insert($key, $value);
00170             }
00171          }
00172          $file->write();
00173 
00174          unset($nodes);
00175          unset($dom);
00176          unset($file);
00177       }
00178 
00179       // Deal with key removals
00180       $deleteKeys = Request::getUserVar('deleteKey');
00181       if (!empty($deleteKeys)) {
00182          if (!is_array($deleteKeys)) $deleteKeys = array($deleteKeys);
00183          foreach ($deleteKeys as $deleteKey) { // FIXME Optimize!
00184             list($filename, $key) = explode('/', $deleteKey, 2);
00185             $filename = urldecode(urldecode($filename));
00186             if (!in_array($filename, $localeFiles)) continue;
00187             $file = new EditableLocaleFile($locale, $filename);
00188             $file->delete($key);
00189             $file->write();
00190             unset($file);
00191          }
00192       }
00193 
00194       // Deal with email removals
00195       import('lib.pkp.classes.file.EditableEmailFile');
00196       $deleteEmails = Request::getUserVar('deleteEmail');
00197       if (!empty($deleteEmails)) {
00198          $file = new EditableEmailFile($locale, $this->getEmailTemplateFilename($locale));
00199          foreach ($deleteEmails as $key) {
00200             $file->delete($key);
00201          }
00202          $file->write();
00203          unset($file);
00204       }
00205 
00206       return LegacyPluginHelper::redirect(Request::getUserVar('redirectUrl'));
00207    }
00208 
00209    function downloadLocaleFile($args) {
00210       $this->validate();
00211       $plugin =& $this->plugin;
00212       $this->setupTemplate();
00213 
00214       $locale = array_shift($args);
00215       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00216 
00217       $filename = urldecode(urldecode(array_shift($args)));
00218       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00219          return $this->edit(array($locale));
00220       }
00221 
00222       header('Content-Type: application/xml');
00223       header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
00224       header('Cache-Control: private');
00225       readfile($filename);
00226    }
00227 
00228    function editLocaleFile($args) {
00229       $this->validate();
00230       $plugin =& $this->plugin;
00231       $this->setupTemplate();
00232 
00233       $locale = array_shift($args);
00234       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00235 
00236       $filename = urldecode(urldecode(array_shift($args)));
00237       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00238          return $this->edit(array($locale));
00239       }
00240 
00241       $templateMgr =& TemplateManager::getManager();
00242       if(!is_writeable(Core::getBaseDir() . DIRECTORY_SEPARATOR . $filename)) {
00243          $templateMgr->assign('error', true);
00244       }
00245 
00246       import('lib.pkp.classes.file.EditableLocaleFile');
00247       $localeContentsRangeInfo = Handler::getRangeInfo('localeContents');
00248       $localeContents = EditableLocaleFile::load($filename);
00249 
00250       // Handle a search, if one was executed.
00251       $searchKey = Request::getUserVar('searchKey');
00252       $found = false;
00253       $index = 0;
00254       $pageIndex = 0;
00255       if (!empty($searchKey)) foreach ($localeContents as $key => $value) {
00256          if ($index % $localeContentsRangeInfo->getCount() == 0) $pageIndex++;
00257          if ($key == $searchKey) {
00258             $found = true;
00259             break;
00260          }
00261          $index++;
00262       }
00263 
00264       if ($found) {
00265          $localeContentsRangeInfo->setPage($pageIndex);
00266          $templateMgr->assign('searchKey', $searchKey);
00267       }
00268 
00269 
00270       $templateMgr->assign('filename', $filename);
00271       $templateMgr->assign('locale', $locale);
00272       import('lib.pkp.classes.core.ArrayItemIterator');
00273       $templateMgr->assign_by_ref('localeContents', new ArrayItemIterator($localeContents, $localeContentsRangeInfo->getPage(), $localeContentsRangeInfo->getCount()));
00274       $templateMgr->assign('referenceLocaleContents', EditableLocaleFile::load(TranslatorAction::determineReferenceFilename($locale, $filename)));
00275 
00276       return $templateMgr->fetchJson($plugin->getTemplatePath() . 'localeFile.tpl');
00277    }
00278 
00279    function editMiscFile($args) {
00280       $this->validate();
00281       $plugin =& $this->plugin;
00282       $this->setupTemplate();
00283 
00284       $locale = array_shift($args);
00285       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00286 
00287       $filename = urldecode(urldecode(array_shift($args)));
00288       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00289          return $this->edit(array($locale));
00290       }
00291       $referenceFilename = TranslatorAction::determineReferenceFilename($locale, $filename);
00292       $templateMgr =& TemplateManager::getManager();
00293 
00294       $templateMgr->assign('locale', $locale);
00295       $templateMgr->assign('filename', $filename);
00296       $templateMgr->assign('referenceContents', file_get_contents($referenceFilename));
00297       $templateMgr->assign('translationContents', file_exists($filename)?file_get_contents($filename):'');
00298       return $templateMgr->fetchJson($plugin->getTemplatePath() . 'editMiscFile.tpl');
00299    }
00300 
00301    function saveLocaleFile($args) {
00302       $this->validate();
00303       $plugin =& $this->plugin;
00304       $this->setupTemplate();
00305 
00306       $locale = array_shift($args);
00307       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00308 
00309       $filename = urldecode(urldecode(array_shift($args)));
00310       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00311          return $this->edit(array($locale));
00312       }
00313 
00314       import('lib.pkp.classes.file.EditableLocaleFile');
00315       $changes = Request::getUserVar('changes');
00316       $file = new EditableLocaleFile($locale, $filename);
00317 
00318       while (!empty($changes)) {
00319          $key = array_shift($changes);
00320          $value = $this->correctCr(array_shift($changes));
00321          if (!$file->update($key, $value)) {
00322             $file->insert($key, $value);
00323          }
00324       }
00325       $file->write();
00326       return LegacyPluginHelper::redirect(Request::getUserVar('redirectUrl'));
00327    }
00328 
00329    function deleteLocaleKey($args) {
00330       $this->validate();
00331       $plugin =& $this->plugin;
00332       $this->setupTemplate();
00333 
00334       $locale = array_shift($args);
00335       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00336 
00337       $filename = urldecode(urldecode(array_shift($args)));
00338       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00339          return $this->edit(array($locale));
00340       }
00341 
00342       $changes = Request::getUserVar('changes');
00343       import('lib.pkp.classes.file.EditableLocaleFile');
00344       $file = new EditableLocaleFile($locale, $filename);
00345 
00346       if ($file->delete(array_shift($args))) $file->write();
00347       return $this->editLocaleFile(array($locale, urlencode($filename)));
00348    }
00349 
00350    function saveMiscFile($args) {
00351       $this->validate();
00352       $plugin =& $this->plugin;
00353       $this->setupTemplate();
00354 
00355       $locale = array_shift($args);
00356       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00357 
00358       $filename = urldecode(urldecode(array_shift($args)));
00359       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00360          return $this->edit(array($locale));
00361       }
00362 
00363       $fp = fopen($filename, 'w+'); // FIXME error handling
00364       if ($fp) {
00365          $contents = $this->correctCr(Request::getUserVar('translationContents'));
00366          fwrite ($fp, $contents);
00367          fclose($fp);
00368       }
00369       return $this->edit(array($locale));
00370    }
00371 
00372    function editEmail($args) {
00373       $this->validate();
00374       $plugin =& $this->plugin;
00375       $this->setupTemplate();
00376 
00377       $locale = array_shift($args);
00378       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00379 
00380       $emails = TranslatorAction::getEmailTemplates($locale);
00381       $referenceEmails = TranslatorAction::getEmailTemplates(MASTER_LOCALE);
00382       $emailKey = array_shift($args);
00383 
00384       if (!in_array($emailKey, array_keys($referenceEmails)) && !in_array($emailKey, array_keys($emails))) return $this->index();
00385 
00386       $templateMgr =& TemplateManager::getManager();
00387       $templateMgr->assign('emailKey', $emailKey);
00388       $templateMgr->assign('locale', $locale);
00389       $templateMgr->assign('email', isset($emails[$emailKey])?$emails[$emailKey]:'');
00390       $templateMgr->assign('returnToCheck', Request::getUserVar('returnToCheck'));
00391       $templateMgr->assign('referenceEmail', isset($referenceEmails[$emailKey])?$referenceEmails[$emailKey]:'');
00392       return $templateMgr->fetchJson($plugin->getTemplatePath() . 'editEmail.tpl');
00393    }
00394 
00395    function createFile($args) {
00396       $this->validate();
00397       $plugin =& $this->plugin;
00398       $this->setupTemplate();
00399 
00400       $locale = array_shift($args);
00401       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00402 
00403       $filename = urldecode(urldecode(array_shift($args)));
00404       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00405          return $this->edit(array($locale));
00406       }
00407 
00408       import('lib.pkp.classes.file.FileManager');
00409       $fileManager = new FileManager();
00410       $fileManager->copyFile(TranslatorAction::determineReferenceFilename($locale, $filename), $filename);
00411       return LegacyPluginHelper::redirect(Request::getUserVar('redirectUrl'));
00412    }
00413 
00414    function deleteEmail($args) {
00415       $this->validate();
00416       $plugin =& $this->plugin;
00417       $this->setupTemplate();
00418 
00419       $locale = array_shift($args);
00420       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00421 
00422       $emails = TranslatorAction::getEmailTemplates($locale);
00423       $referenceEmails = TranslatorAction::getEmailTemplates(MASTER_LOCALE);
00424       $emailKey = array_shift($args);
00425 
00426       if (!in_array($emailKey, array_keys($emails))) return $this->index();
00427 
00428       import('lib.pkp.classes.file.EditableEmailFile');
00429       $file = new EditableEmailFile($locale, $this->getEmailTemplateFilename($locale));
00430 
00431       $subject = Request::getUserVar('subject');
00432       $body = Request::getUserVar('body');
00433       $description = Request::getUserVar('description');
00434       if ($file->delete($emailKey)) $file->write();
00435 
00436       return LegacyPluginHelper::redirect(Request::url(null, null, 'edit', $locale, null, 'emails'));
00437    }
00438 
00439    function saveEmail($args) {
00440       $this->validate();
00441       $plugin =& $this->plugin;
00442       $this->setupTemplate();
00443 
00444       $locale = array_shift($args);
00445       if (!AppLocale::isLocaleValid($locale)) return $this->index();
00446 
00447       $emails = TranslatorAction::getEmailTemplates($locale);
00448       $referenceEmails = TranslatorAction::getEmailTemplates(MASTER_LOCALE);
00449       $emailKey = array_shift($args);
00450       $targetFilename = str_replace(MASTER_LOCALE, $locale, $referenceEmails[$emailKey]['templateDataFile']); // FIXME: Ugly.
00451 
00452       if (!in_array($emailKey, array_keys($emails))) {
00453          // If it's not a reference or translation email, bail.
00454          if (!in_array($emailKey, array_keys($referenceEmails))) return $this->index();
00455 
00456          // If it's a reference email but not a translated one,
00457          // create a blank file. FIXME: This is ugly.
00458          if (!file_exists($targetFilename)) {
00459             $dir = dirname($targetFilename);
00460             if (!file_exists($dir)) mkdir($dir);
00461             file_put_contents($targetFilename, '<?xml version="1.0" encoding="UTF-8"?>
00462 <!DOCTYPE email_texts SYSTEM "../../../../../lib/pkp/dtd/emailTemplateData.dtd">
00463 <!--
00464   * emailTemplateData.xml
00465   *
00466   * Copyright (c) 2003-2012 John Willinsky
00467   * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
00468   *
00469   * Localized email templates XML file.
00470   -->
00471 <email_texts locale="' . $locale . '">
00472 </email_texts>');
00473          }
00474       }
00475 
00476       import('lib.pkp.classes.file.EditableEmailFile');
00477       $file = new EditableEmailFile($locale, $targetFilename);
00478 
00479       $subject = $this->correctCr(Request::getUserVar('subject'));
00480       $body = $this->correctCr(Request::getUserVar('body'));
00481       $description = $this->correctCr(Request::getUserVar('description'));
00482 
00483       if (!$file->update($emailKey, $subject, $body, $description))
00484          $file->insert($emailKey, $subject, $body, $description);
00485 
00486       $file->write();
00487       if (Request::getUserVar('returnToCheck')==1) return $this->check(array($locale));
00488       else return $this->edit(array($locale));
00489    }
00490 
00491    function correctCr($value) {
00492       return str_replace("\r\n", "\n", $value);
00493    }
00494 }
00495 
00496 ?>

Generated on Mon Sep 17 2012 13:58:56 for Open Monograph Press by  doxygen 1.7.1