Open Monograph Press  3.3.0
CustomLocalePlugin.inc.php
1 <?php
2 
13 import('lib.pkp.classes.plugins.GenericPlugin');
14 
16 
20  function register($category, $path, $mainContextId = null) {
21  if (!parent::register($category, $path, $mainContextId)) return false;
22 
23  if ($this->getEnabled()) {
24  // Add custom locale data for already registered locale files.
25  $locale = AppLocale::getLocale();
26  $localeFiles = AppLocale::getLocaleFiles($locale);
27 
28  $request = Application::get()->getRequest();
29  $context = $request->getContext();
30 
31  import('lib.pkp.classes.file.ContextFileManager');
32  $contextFileManager = new ContextFileManager($context->getId());
33  $customLocalePathBase = $contextFileManager->getBasePath() . "customLocale/$locale/";
34 
35  import('lib.pkp.classes.file.FileManager');
36  $fileManager = new FileManager();
37  foreach ($localeFiles as $localeFile) {
38  $customLocalePath = $customLocalePathBase . $localeFile->getFilename();
39  if ($contextFileManager->fileExists($customLocalePath)) {
40  AppLocale::registerLocaleFile($locale, $customLocalePath, false);
41  }
42  }
43 
44  // Add custom locale data for all locale files registered after this plugin
45  HookRegistry::register('PKPLocale::registerLocaleFile', array($this, 'addCustomLocale'));
46  HookRegistry::register('LoadComponentHandler', array($this, 'setupGridHandler'));
47  HookRegistry::register('Template::Settings::website', array($this, 'callbackShowWebsiteSettingsTabs'));
48  HookRegistry::register('LoadHandler', array($this, 'handleLoadRequest'));
49 
50  $templateMgr = TemplateManager::getManager($request);
51  $templateMgr->addJavaScript(
52  'customLocale',
53  $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/customLocale.js',
54  ['contexts' => 'backend']
55  );
56  }
57 
58  return true;
59  }
60 
66  function setupGridHandler($hookName, $args) {
67  $component = $args[0];
68  if ($component == 'plugins.generic.customLocale.controllers.grid.CustomLocaleGridHandler') {
69  // Allow the custom locale grid handler to get the plugin object
70  import($component);
72  return true;
73  }
74  return false;
75  }
76 
82  function handleLoadRequest($hookName, $args) {
83  $request = $this->getRequest();
84  $templateMgr = TemplateManager::getManager($request);
85 
86  // get url path components
87  $page =& $args[0];
88  $op =& $args[1];
89  $tail = implode('/', $request->getRequestedArgs());
90 
91  if ($page=='management' && $op=='settings' && $tail=='printCustomLocaleChanges') {
92  $op = 'printCustomLocaleChanges';
93  define('HANDLER_CLASS', 'CustomLocaleHandler');
94  define('CUSTOMLOCALE_PLUGIN_NAME', $this->getName());
95  $this->import('CustomLocaleHandler');
96  }
97  return false;
98  }
99 
103  function getActions($request, $actionArgs) {
104  $dispatcher = $request->getDispatcher();
105  import('lib.pkp.classes.linkAction.request.RedirectAction');
106  return array_merge(
107  $this->getEnabled()?array(
108  new LinkAction(
109  'customize',
110  new RedirectAction($dispatcher->url(
111  $request, ROUTE_PAGE,
112  null, 'management', 'settings', 'website',
113  array('uid' => uniqid()), // Force reload
114  'customLocale' // Anchor for tab
115  )),
116  __('plugins.generic.customLocale.customize'),
117  null
118  ),
119  new LinkAction(
120  'printChanges',
121  new RedirectAction($dispatcher->url(
122  $request, ROUTE_PAGE,
123  null, 'management', 'settings', 'printCustomLocaleChanges',
124  array('uid' => uniqid()), // Force reload
125  null // Anchor for tab
126  )),
127  __('plugins.generic.customLocale.printChanges'),
128  null
129  ),
130  ):array(),
131  parent::getActions($request, $actionArgs)
132  );
133  }
134 
141  function addCustomLocale($hookName, $args) {
142  $locale =& $args[0];
143  $localeFilename =& $args[1];
144  $request =& Registry::get('request');
145  $context = $request->getContext();
146 
147  $contextFileManager = new ContextFileManager($context->getId());
148  $customLocalePath = $contextFileManager->getBasePath() . "customLocale/$locale/$localeFilename";
149 
150  if ($contextFileManager->fileExists($customLocalePath)) {
151  AppLocale::registerLocaleFile($locale, $customLocalePath, false);
152  }
153 
154  return true;
155  }
156 
161  function getDisplayName() {
162  return __('plugins.generic.customLocale.name');
163  }
164 
169  function getDescription() {
170  return __('plugins.generic.customLocale.description');
171  }
172 
179  function callbackShowWebsiteSettingsTabs($hookName, $args) {
180  $templateMgr = $args[1];
181  $output =& $args[2];
182 
183  $output .= $templateMgr->fetch($this->getTemplateResource('customLocaleTab.tpl'));
184 
185  // Permit other plugins to continue interacting with this hook
186  return false;
187  }
188 }
189 
$op
$op
Definition: lib/pkp/pages/help/index.php:18
CustomLocalePlugin\callbackShowWebsiteSettingsTabs
callbackShowWebsiteSettingsTabs($hookName, $args)
Definition: CustomLocalePlugin.inc.php:179
Plugin\getRequest
& getRequest()
Definition: Plugin.inc.php:828
CustomLocalePlugin\getActions
getActions($request, $actionArgs)
Definition: CustomLocalePlugin.inc.php:103
CustomLocalePlugin\getDescription
getDescription()
Definition: CustomLocalePlugin.inc.php:169
AppLocale\registerLocaleFile
static registerLocaleFile($locale, $filename, $addToTop=false)
Definition: env1/MockAppLocale.inc.php:48
CustomLocalePlugin\setupGridHandler
setupGridHandler($hookName, $args)
Definition: CustomLocalePlugin.inc.php:66
CustomLocalePlugin
Definition: CustomLocalePlugin.inc.php:15
ContextFileManager
Class defining operations for private context file management.
Definition: ContextFileManager.inc.php:19
CustomLocaleGridHandler\setPlugin
static setPlugin($plugin)
Definition: CustomLocaleGridHandler.inc.php:33
Plugin\getEnabled
getEnabled()
Definition: Plugin.inc.php:868
Registry\get
static & get($key, $createIfEmpty=false, $createWithDefault=null)
Definition: Registry.inc.php:35
LinkAction
Base class defining an action that can be performed by the user in the user interface.
Definition: LinkAction.inc.php:22
CustomLocalePlugin\getDisplayName
getDisplayName()
Definition: CustomLocalePlugin.inc.php:161
LazyLoadPlugin\getName
getName()
Definition: LazyLoadPlugin.inc.php:40
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
RedirectAction
This action request redirects to another page.
Definition: RedirectAction.inc.php:18
Plugin\getTemplateResource
getTemplateResource($template=null, $inCore=false)
Definition: Plugin.inc.php:349
Plugin\$request
$request
Definition: Plugin.inc.php:68
HookRegistry\register
static register($hookName, $callback, $hookSequence=HOOK_SEQUENCE_NORMAL)
Definition: HookRegistry.inc.php:70
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
CustomLocalePlugin\addCustomLocale
addCustomLocale($hookName, $args)
Definition: CustomLocalePlugin.inc.php:141
FileManager
Class defining basic operations for file management.
Definition: FileManager.inc.php:35
AppLocale\getLocale
static getLocale()
Definition: env1/MockAppLocale.inc.php:40
GenericPlugin
Abstract class for generic plugins.
Definition: GenericPlugin.inc.php:18
CustomLocalePlugin\handleLoadRequest
handleLoadRequest($hookName, $args)
Definition: CustomLocalePlugin.inc.php:82
PKPLocale\getLocaleFiles
static & getLocaleFiles($locale=null)
Definition: PKPLocale.inc.php:229