plugins/generic/translator/TranslatorHandler.inc.php

Go to the documentation of this file.
00001 <?php
00002 
00016 // $Id$
00017 
00018 
00019 require_once('TranslatorAction.inc.php');
00020 import('handler.Handler');
00021 
00022 class TranslatorHandler extends Handler {
00024    var $plugin;
00025 
00029    function TranslatorHandler() {
00030       parent::Handler();
00031 
00032       $this->addCheck(new HandlerValidatorRoles($this, true, null, null, array(ROLE_ID_SITE_ADMIN)));
00033       $this->plugin =& Registry::get('plugin');
00034    }
00035 
00036    function index() {
00037       $this->validate();
00038       $plugin =& $this->plugin;
00039       $this->setupTemplate(false);
00040 
00041       $rangeInfo = $this->getRangeInfo('locales');
00042 
00043       $templateMgr =& TemplateManager::getManager();
00044       import('core.ArrayItemIterator');
00045       $templateMgr->assign('locales', new ArrayItemIterator(AppLocale::getAllLocales(), $rangeInfo->getPage(), $rangeInfo->getCount()));
00046       $templateMgr->assign('masterLocale', MASTER_LOCALE);
00047 
00048       // Test whether the tar binary is available for the export to work
00049       $tarBinary = Config::getVar('cli', 'tar');
00050       $templateMgr->assign('tarAvailable', !empty($tarBinary) && file_exists($tarBinary));
00051 
00052       $templateMgr->display($plugin->getTemplatePath() . 'index.tpl');
00053    }
00054 
00055    function setupTemplate($subclass = true) {
00056       parent::setupTemplate();
00057       $templateMgr =& TemplateManager::getManager();
00058       AppLocale::requireComponents(array(LOCALE_COMPONENT_PKP_ADMIN, LOCALE_COMPONENT_PKP_MANAGER));
00059       $pageHierarchy = array(array(Request::url(null, null, 'user'), 'navigation.user'), array(Request::url(null, null, 'admin'), 'admin.siteAdmin'));
00060       if ($subclass) $pageHierarchy[] = array(Request::url(null, null, 'translate'), 'plugins.generic.translator.name');
00061       $templateMgr->assign('pageHierarchy', $pageHierarchy);
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)) Request::redirect(null, null, null, 'index');
00074       $localeFiles = TranslatorAction::getLocaleFiles($locale);
00075       $miscFiles = TranslatorAction::getMiscLocaleFiles($locale);
00076       $emails = TranslatorAction::getEmailTemplates($locale);
00077 
00078       $templateMgr =& TemplateManager::getManager();
00079 
00080       $localeFilesRangeInfo = $this->getRangeInfo('localeFiles');
00081       $miscFilesRangeInfo = $this->getRangeInfo('miscFiles');
00082       $emailsRangeInfo = $this->getRangeInfo('emails');
00083 
00084       import('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       $templateMgr->display($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)) Request::redirect(null, null, null, '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       $templateMgr->display($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)) Request::redirect(null, null, null, '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)) Request::redirect(null, null, null, '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('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('file.EditableEmailFile');
00196       $deleteEmails = Request::getUserVar('deleteEmail');
00197       if (!empty($deleteEmails)) {
00198          $file = new EditableEmailFile($locale, AppLocale::getEmailTemplateFilename($locale));
00199          foreach ($deleteEmails as $key) {
00200             $file->delete($key);
00201          }
00202          $file->write();
00203          unset($file);
00204       }
00205 
00206       Request::redirectUrl(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)) Request::redirect(null, null, null, 'index');
00216 
00217       $filename = urldecode(urldecode(array_shift($args)));
00218       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00219          Request::redirect(null, null, null, 'edit', $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)) Request::redirect(null, null, null, 'index');
00235 
00236       $filename = urldecode(urldecode(array_shift($args)));
00237       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00238          Request::redirect(null, null, null, 'edit', $locale);
00239       }
00240 
00241       $templateMgr =& TemplateManager::getManager();
00242       if(!is_writeable(Core::getBaseDir() . DIRECTORY_SEPARATOR . $filename)) {
00243          $templateMgr->assign('error', true);
00244       }
00245 
00246 
00247       import('file.EditableLocaleFile');
00248       $localeContentsRangeInfo = $this->getRangeInfo('localeContents');
00249       $localeContents = EditableLocaleFile::load($filename);
00250 
00251       // Handle a search, if one was executed.
00252       $searchKey = Request::getUserVar('searchKey');
00253       $found = false;
00254       $index = 0;
00255       $pageIndex = 0;
00256       if (!empty($searchKey)) foreach ($localeContents as $key => $value) {
00257          if ($index % $localeContentsRangeInfo->getCount() == 0) $pageIndex++;
00258          if ($key == $searchKey) {
00259             $found = true;
00260             break;
00261          }
00262          $index++;
00263       }
00264 
00265       if ($found) {
00266          $localeContentsRangeInfo->setPage($pageIndex);
00267          $templateMgr->assign('searchKey', $searchKey);
00268       }
00269 
00270 
00271       $templateMgr->assign('filename', $filename);
00272       $templateMgr->assign('locale', $locale);
00273       import('core.ArrayItemIterator');
00274       $templateMgr->assign_by_ref('localeContents', new ArrayItemIterator($localeContents, $localeContentsRangeInfo->getPage(), $localeContentsRangeInfo->getCount()));
00275       $templateMgr->assign('referenceLocaleContents', EditableLocaleFile::load(TranslatorAction::determineReferenceFilename($locale, $filename)));
00276 
00277       $templateMgr->display($plugin->getTemplatePath() . 'localeFile.tpl');
00278    }
00279 
00280    function editMiscFile($args) {
00281       $this->validate();
00282       $plugin =& $this->plugin;
00283       $this->setupTemplate();
00284 
00285       $locale = array_shift($args);
00286       if (!AppLocale::isLocaleValid($locale)) Request::redirect(null, null, null, 'index');
00287 
00288       $filename = urldecode(urldecode(array_shift($args)));
00289       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00290          Request::redirect(null, null, null, 'edit', $locale);
00291       }
00292       $referenceFilename = TranslatorAction::determineReferenceFilename($locale, $filename);
00293       $templateMgr =& TemplateManager::getManager();
00294 
00295       $templateMgr->assign('locale', $locale);
00296       $templateMgr->assign('filename', $filename);
00297       $templateMgr->assign('referenceContents', file_get_contents($referenceFilename));
00298       $templateMgr->assign('translationContents', file_exists($filename)?file_get_contents($filename):'');
00299       $templateMgr->display($plugin->getTemplatePath() . 'editMiscFile.tpl');
00300    }
00301 
00302    function saveLocaleFile($args) {
00303       $this->validate();
00304       $plugin =& $this->plugin;
00305       $this->setupTemplate();
00306 
00307       $locale = array_shift($args);
00308       if (!AppLocale::isLocaleValid($locale)) Request::redirect(null, null, null, 'index');
00309 
00310       $filename = urldecode(urldecode(array_shift($args)));
00311       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00312          Request::redirect(null, null, null, 'edit', $locale);
00313       }
00314 
00315       import('file.EditableLocaleFile');
00316       $changes = Request::getUserVar('changes');
00317       $file = new EditableLocaleFile($locale, $filename);
00318 
00319       while (!empty($changes)) {
00320          $key = array_shift($changes);
00321          $value = $this->correctCr(array_shift($changes));
00322          if (!$file->update($key, $value)) {
00323             $file->insert($key, $value);
00324          }
00325       }
00326       $file->write();
00327       Request::redirectUrl(Request::getUserVar('redirectUrl'));
00328    }
00329 
00330    function deleteLocaleKey($args) {
00331       $this->validate();
00332       $plugin =& $this->plugin;
00333       $this->setupTemplate();
00334 
00335       $locale = array_shift($args);
00336       if (!AppLocale::isLocaleValid($locale)) Request::redirect(null, null, null, 'index');
00337 
00338       $filename = urldecode(urldecode(array_shift($args)));
00339       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00340          Request::redirect(null, null, null, 'edit', $locale);
00341       }
00342 
00343       $changes = Request::getUserVar('changes');
00344       import('file.EditableLocaleFile');
00345       $file = new EditableLocaleFile($locale, $filename);
00346 
00347       if ($file->delete(array_shift($args))) $file->write();
00348       Request::redirect(null, null, null, 'editLocaleFile', array($locale, urlencode(urlencode($filename))));
00349    }
00350 
00351    function saveMiscFile($args) {
00352       $this->validate();
00353       $plugin =& $this->plugin;
00354       $this->setupTemplate();
00355 
00356       $locale = array_shift($args);
00357       if (!AppLocale::isLocaleValid($locale)) Request::redirect(null, null, null, 'index');
00358 
00359       $filename = urldecode(urldecode(array_shift($args)));
00360       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00361          Request::redirect(null, null, null, 'edit', $locale);
00362       }
00363 
00364       $fp = fopen($filename, 'w+'); // FIXME error handling
00365       if ($fp) {
00366          $contents = $this->correctCr(Request::getUserVar('translationContents'));
00367          fwrite ($fp, $contents);
00368          fclose($fp);
00369       }
00370       Request::redirect(null, null, null, 'edit', $locale);
00371    }
00372 
00373    function editEmail($args) {
00374       $this->validate();
00375       $plugin =& $this->plugin;
00376       $this->setupTemplate();
00377 
00378       $locale = array_shift($args);
00379       if (!AppLocale::isLocaleValid($locale)) Request::redirect(null, null, null, 'index');
00380 
00381       $emails = TranslatorAction::getEmailTemplates($locale);
00382       $referenceEmails = TranslatorAction::getEmailTemplates(MASTER_LOCALE);
00383       $emailKey = array_shift($args);
00384 
00385       if (!in_array($emailKey, array_keys($referenceEmails)) && !in_array($emailKey, array_keys($emails))) Request::redirect(null, null, null, 'index');
00386 
00387       $templateMgr =& TemplateManager::getManager();
00388       $templateMgr->assign('emailKey', $emailKey);
00389       $templateMgr->assign('locale', $locale);
00390       $templateMgr->assign('email', isset($emails[$emailKey])?$emails[$emailKey]:'');
00391       $templateMgr->assign('returnToCheck', Request::getUserVar('returnToCheck'));
00392       $templateMgr->assign('referenceEmail', isset($referenceEmails[$emailKey])?$referenceEmails[$emailKey]:'');
00393       $templateMgr->display($plugin->getTemplatePath() . 'editEmail.tpl');
00394    }
00395 
00396    function createFile($args) {
00397       $this->validate();
00398       $plugin =& $this->plugin;
00399       $this->setupTemplate();
00400 
00401       $locale = array_shift($args);
00402       if (!AppLocale::isLocaleValid($locale)) Request::redirect(null, null, null, 'index');
00403 
00404       $filename = urldecode(urldecode(array_shift($args)));
00405       if (!TranslatorAction::isLocaleFile($locale, $filename)) {
00406          Request::redirect(null, null, null, 'edit', $locale);
00407       }
00408 
00409       import('file.FileManager');
00410       FileManager::copyFile(TranslatorAction::determineReferenceFilename($locale, $filename), $filename);
00411       Request::redirectUrl(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)) Request::redirect(null, null, null, '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))) Request::redirect(null, null, null, 'index');
00427 
00428       import('file.EditableEmailFile');
00429       $file = new EditableEmailFile($locale, AppLocale::getEmailTemplateFilename($locale));
00430 
00431       $subject = Request::getUserVar('subject');
00432       $body = Request::getUserVar('body');
00433       $description = Request::getUserVar('description');
00434 
00435       if ($file->delete($emailKey)) $file->write();
00436       Request::redirect(null, 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)) Request::redirect(null, null, null, '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))) Request::redirect(null, null, '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) 2000-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   * $Id$
00472   -->
00473 <email_texts locale="' . $locale . '">
00474 </email_texts>');
00475          }
00476       }
00477 
00478       import('file.EditableEmailFile');
00479       $file = new EditableEmailFile($locale, $targetFilename);
00480 
00481       $subject = $this->correctCr(Request::getUserVar('subject'));
00482       $body = $this->correctCr(Request::getUserVar('body'));
00483       $description = $this->correctCr(Request::getUserVar('description'));
00484 
00485       if (!$file->update($emailKey, $subject, $body, $description))
00486          $file->insert($emailKey, $subject, $body, $description);
00487 
00488       $file->write();
00489       if (Request::getUserVar('returnToCheck')==1) Request::redirect(null, null, null, 'check', $locale);
00490       else Request::redirect(null, null, null, 'edit', $locale);
00491    }
00492 
00493    function correctCr($value) {
00494       return str_replace("\r\n", "\n", $value);
00495    }
00496 }
00497 
00498 ?>

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