Open Journal Systems  3.3.0
PKPToolsHandler.inc.php
1 <?php
2 
16 // Import the base ManagementHandler.
17 import('lib.pkp.pages.management.ManagementHandler');
18 
19 define('IMPORTEXPORT_PLUGIN_CATEGORY', 'importexport');
20 
22 
24  var $_isBackendPage = true;
25 
29  function __construct() {
30  parent::__construct();
31  $this->addRoleAssignment(
32  ROLE_ID_MANAGER,
33  array('tools', 'statistics', 'importexport', 'permissions')
34  );
35  }
36 
37 
38  //
39  // Public handler methods.
40  //
41  function setupTemplate($request) {
42  parent::setupTemplate($request);
43  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_MANAGER, LOCALE_COMPONENT_APP_SUBMISSION);
44  }
45 
51  function tools($args, $request) {
52  $path = array_shift($args);
53  switch ($path) {
54  case '':
55  case 'index':
56  $this->index($args, $request);
57  break;
58  case 'statistics':
59  $this->statistics($args, $request);
60  break;
61  case 'report':
62  $this->report($args, $request);
63  break;
64  case 'reportGenerator':
65  $this->reportGenerator($args, $request);
66  break;
67  case 'generateReport':
68  $this->generateReport($args, $request);
69  break;
70  case 'saveStatisticsSettings':
71  return $this->saveStatisticsSettings($args, $request);
72  case 'permissions':
73  $this->permissions($args, $request);
74  break;
75  case 'resetPermissions':
76  $this->resetPermissions($args, $request);
77  break;
78  default: assert(false);
79  }
80  }
81 
87  function index($args, $request) {
88  $templateMgr = TemplateManager::getManager($request);
89  $this->setupTemplate($request);
90  $templateMgr->assign('pageTitle', __('navigation.tools'));
91  $templateMgr->display('management/tools/index.tpl');
92  }
93 
99  function importexport($args, $request) {
100  $this->setupTemplate($request, true);
101 
102  PluginRegistry::loadCategory(IMPORTEXPORT_PLUGIN_CATEGORY);
103  $templateMgr = TemplateManager::getManager($request);
104 
105  if (array_shift($args) === 'plugin') {
106  $pluginName = array_shift($args);
107  $plugin = PluginRegistry::getPlugin(IMPORTEXPORT_PLUGIN_CATEGORY, $pluginName);
108  if ($plugin) return $plugin->display($args, $request);
109  }
110  $templateMgr->assign('plugins', PluginRegistry::getPlugins(IMPORTEXPORT_PLUGIN_CATEGORY));
111  return $templateMgr->fetchJson('management/tools/importexport.tpl');
112  }
113 
119  function statistics($args, $request) {
120  $this->setupTemplate($request);
121  $context = $request->getContext();
122 
123  $templateMgr = TemplateManager::getManager($request);
124 
126  $templateMgr->assign('appSettings', $this->hasAppStatsSettings());
127  $templateMgr->assign('contextObjectName', __($application->getNameKey()));
128 
129  $reportPlugins = PluginRegistry::loadCategory('reports');
130  $templateMgr->assign('reportPlugins', $reportPlugins);
131 
132  $templateMgr->assign('defaultMetricType', $context->getData('defaultMetricType'));
133  $availableMetricTypes = $context->getMetricTypes(true);
134  $templateMgr->assign('availableMetricTypes', $availableMetricTypes);
135  if (count($availableMetricTypes) > 1) {
136  $templateMgr->assign('showMetricTypeSelector', true);
137  }
138 
139  return $templateMgr->fetchJson('management/tools/statistics.tpl');
140  }
141 
148  function report($args, $request) {
149  $this->setupTemplate($request);
150 
151  $pluginName = $request->getUserVar('pluginName');
152  $reportPlugins = PluginRegistry::loadCategory('reports');
153 
154  if ($pluginName == '' || !isset($reportPlugins[$pluginName])) {
155  $request->redirect(null, null, 'management', 'statistics');
156  }
157 
158  $plugin = $reportPlugins[$pluginName];
159  $plugin->display($args, $request);
160  }
161 
167  function reportGenerator($args, $request) {
168  $this->setupTemplate($request);
169 
170  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_SUBMISSION, LOCALE_COMPONENT_APP_EDITOR);
171 
172  $templateMgr = TemplateManager::getManager();
173  $templateMgr->assign([
174  'breadcrumbs' => [
175  [
176  'id' => 'tools',
177  'name' => __('navigation.tools'),
178  'url' => $request->getRouter()->url($request, null, 'management', 'tools'),
179  ],
180  [
181  'id' => 'reportGenerator',
182  'name' => __('manager.statistics.reports')
183  ],
184  ],
185  'pageTitle', __('manager.statistics.reports'),
186  ]);
187  $templateMgr->display('management/tools/reportGenerator.tpl');
188  }
189 
190 
197  function generateReport($args, $request) {
198  $this->setupTemplate($request);
199  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_SUBMISSION);
200 
201  $router = $request->getRouter();
202  $context = $router->getContext($request);
203  import('classes.statistics.StatisticsHelper');
204  $statsHelper = new StatisticsHelper();
205 
206  $metricType = $request->getUserVar('metricType');
207  if (is_null($metricType)) {
208  $metricType = $context->getDefaultMetricType();
209  }
210 
211  // Generates only one metric type report at a time.
212  if (is_array($metricType)) $metricType = current($metricType);
213  if (!is_scalar($metricType)) $metricType = null;
214 
215  $reportPlugin = $statsHelper->getReportPluginByMetricType($metricType);
216  if (!$reportPlugin || is_null($metricType)) {
217  $request->redirect(null, null, 'tools', 'statistics');
218  }
219 
220  $columns = $request->getUserVar('columns');
221  $filters = (array) json_decode($request->getUserVar('filters'));
222  if (!$filters) $filters = $request->getUserVar('filters');
223 
224  $orderBy = $request->getUserVar('orderBy');
225  if ($orderBy) {
226  $orderBy = (array) json_decode($orderBy);
227  if (!$orderBy) $orderBy = $request->getUserVar('orderBy');
228  } else {
229  $orderBy = array();
230  }
231 
232  $metrics = $reportPlugin->getMetrics($metricType, $columns, $filters, $orderBy);
233 
234  $allColumnNames = $statsHelper->getColumnNames();
235  $columnOrder = array_keys($allColumnNames);
236  $columnNames = array();
237 
238  foreach ($columnOrder as $column) {
239  if (in_array($column, $columns)) {
240  $columnNames[$column] = $allColumnNames[$column];
241  }
242 
243  if ($column == STATISTICS_DIMENSION_ASSOC_TYPE && in_array(STATISTICS_DIMENSION_ASSOC_ID, $columns)) {
244  $columnNames['common.title'] = __('common.title');
245  }
246  }
247 
248  // Make sure the metric column will always be present.
249  if (!in_array(STATISTICS_METRIC, $columnNames)) $columnNames[STATISTICS_METRIC] = $allColumnNames[STATISTICS_METRIC];
250 
251  header('content-type: text/comma-separated-values');
252  header('content-disposition: attachment; filename=statistics-' . date('Ymd') . '.csv');
253  $fp = fopen('php://output', 'wt');
254  //Add BOM (byte order mark) to fix UTF-8 in Excel
255  fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF));
256  fputcsv($fp, array($reportPlugin->getDisplayName()));
257  fputcsv($fp, array($reportPlugin->getDescription()));
258  fputcsv($fp, array(__('common.metric') . ': ' . $metricType));
259  fputcsv($fp, array(__('manager.statistics.reports.reportUrl') . ': ' . $request->getCompleteUrl()));
260  fputcsv($fp, array(''));
261 
262  // Just for better displaying.
263  $columnNames = array_merge(array(''), $columnNames);
264 
265  fputcsv($fp, $columnNames);
266  foreach ($metrics as $record) {
267  $row = array();
268  foreach ($columnNames as $key => $name) {
269  if (empty($name)) {
270  // Column just for better displaying.
271  $row[] = '';
272  continue;
273  }
274 
275  // Give a chance for subclasses to set the row values.
276  if ($returner = $this->getReportRowValue($key, $record)) {
277  $row[] = $returner;
278  continue;
279  }
280 
281  switch ($key) {
282  case 'common.title':
283  $assocId = $record[STATISTICS_DIMENSION_ASSOC_ID];
284  $assocType = $record[STATISTICS_DIMENSION_ASSOC_TYPE];
285  $row[] = $this->getObjectTitle($assocId, $assocType);
286  break;
287  case STATISTICS_DIMENSION_ASSOC_TYPE:
288  $assocType = $record[STATISTICS_DIMENSION_ASSOC_TYPE];
289  $row[] = $statsHelper->getObjectTypeString($assocType);
290  break;
291  case STATISTICS_DIMENSION_CONTEXT_ID:
292  $assocId = $record[STATISTICS_DIMENSION_CONTEXT_ID];
293  $assocType = Application::getContextAssocType();
294  $row[] = $this->getObjectTitle($assocId, $assocType);
295  break;
296  case STATISTICS_DIMENSION_SUBMISSION_ID:
297  if (isset($record[STATISTICS_DIMENSION_SUBMISSION_ID])) {
298  $assocId = $record[STATISTICS_DIMENSION_SUBMISSION_ID];
299  $assocType = ASSOC_TYPE_SUBMISSION;
300  $row[] = $this->getObjectTitle($assocId, $assocType);
301  } else {
302  $row[] = '';
303  }
304  break;
305  case STATISTICS_DIMENSION_REGION:
306  if (isset($record[STATISTICS_DIMENSION_REGION]) && isset($record[STATISTICS_DIMENSION_COUNTRY])) {
307  $geoLocationTool = $statsHelper->getGeoLocationTool();
308  if ($geoLocationTool) {
309  $regions = $geoLocationTool->getRegions($record[STATISTICS_DIMENSION_COUNTRY]);
310  $regionId = $record[STATISTICS_DIMENSION_REGION];
311  if (strlen($regionId) == 1) $regionId = '0' . $regionId;
312  if (isset($regions[$regionId])) {
313  $row[] = $regions[$regionId];
314  break;
315  }
316  }
317  }
318  $row[] = '';
319  break;
320  case STATISTICS_DIMENSION_PKP_SECTION_ID:
321  $sectionId = null;
322  if (isset($record[STATISTICS_DIMENSION_PKP_SECTION_ID])) {
323  $sectionId = $record[STATISTICS_DIMENSION_PKP_SECTION_ID];
324  }
325  if ($sectionId) {
326  $row[] = $this->getObjectTitle($sectionId, ASSOC_TYPE_SECTION);
327  } else {
328  $row[] = '';
329  }
330  break;
331  case STATISTICS_DIMENSION_FILE_TYPE:
332  if ($record[$key]) {
333  $row[] = $statsHelper->getFileTypeString($record[$key]);
334  } else {
335  $row[] = '';
336  }
337  break;
338  default:
339  $row[] = $record[$key];
340  break;
341  }
342  }
343  fputcsv($fp, $row);
344  }
345  fclose($fp);
346  }
347 
354  function saveStatisticsSettings($args, $request) {
355  $router = $request->getRouter();
356  $context = $router->getContext($request);
357 
358  $defaultMetricType = $request->getUserVar('defaultMetricType');
359  $context->updateSetting('defaultMetricType', $defaultMetricType);
360 
361  $notificationManager = new NotificationManager();
362  $user = $request->getUser();
363  $notificationManager->createTrivialNotification($user->getId());
364 
365  return new JSONMessage();
366  }
367 
368 
369  //
370  // Protected methods.
371  //
379  protected function getReportRowValue($key, $record) {
380  return null;
381  }
382 
390  protected function getObjectTitle($assocId, $assocType) {
391  switch ($assocType) {
393  $contextDao = Application::getContextDAO(); /* @var $contextDao ContextDAO */
394  $context = $contextDao->getById($assocId);
395  if (!$context) break;
396  return $context->getLocalizedName();
397  case ASSOC_TYPE_SUBMISSION:
398  $submissionDao = DAORegistry::getDAO('SubmissionDAO'); /* @var $submissionDao SubmissionDAO */
399  $submission = $submissionDao->getById($assocId, null, true);
400  if (!$submission) break;
401  return $submission->getLocalizedTitle();
402  case ASSOC_TYPE_SECTION:
403  $sectionDao = Application::getSectionDAO();
404  $section = $sectionDao->getById($assocId);
405  if (!$section) break;
406  return $section->getLocalizedTitle();
407  case ASSOC_TYPE_SUBMISSION_FILE:
408  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO'); /* @var $submissionFileDao SubmissionFileDAO */
409  $submissionFile = $submissionFileDao->getLatestRevision($assocId);
410  if (!$submissionFile) break;
411  return $submissionFile->getFileLabel();
412  }
413 
414  return __('manager.statistics.reports.objectNotFound');
415  }
416 
422  protected function hasAppStatsSettings() {
423  return false;
424  }
425 
431  function permissions($args, $request) {
432  $this->setupTemplate($request);
433 
434  $templateMgr = TemplateManager::getManager($request);
435 
436  return $templateMgr->fetchJson('management/tools/permissions.tpl');
437  }
438 
444  function resetPermissions($args, $request) {
445  if (!$request->checkCSRF()) return new JSONMessage(false);
446 
447  $context = $request->getContext();
448  if (!$context) {
449  return;
450  }
451 
452  $submissionDao = DAORegistry::getDAO('SubmissionDAO'); /* @var $submissionDao SubmissionDAO */
453  $submissionDao->resetPermissions($context->getId());
454 
455  $user = $request->getUser();
456  NotificationManager::createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('manager.setup.resetPermissions.success')));
457 
458  // This is an ugly hack to force the PageHandler to return JSON, so this
459  // method can communicate properly with the AjaxFormHandler. Returning a
460  // JSONMessage, or JSONMessage::toString(), doesn't seem to do it.
461  echo json_encode(true);
462  die;
463  }
464 
465 }
466 
467 
PKPHandler\addRoleAssignment
addRoleAssignment($roleIds, $operations)
Definition: PKPHandler.inc.php:213
Application\getContextDAO
static getContextDAO()
Definition: Application.inc.php:137
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
PluginRegistry\getPlugins
static & getPlugins($category=null)
Definition: PluginRegistry.inc.php:30
$application
$application
Definition: index.php:65
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
StatisticsHelper
Statistics helper class.
Definition: StatisticsHelper.inc.php:21
PKPToolsHandler\resetPermissions
resetPermissions($args, $request)
Definition: PKPToolsHandler.inc.php:444
PKPToolsHandler\$_isBackendPage
$_isBackendPage
Definition: PKPToolsHandler.inc.php:24
PKPToolsHandler\getReportRowValue
getReportRowValue($key, $record)
Definition: PKPToolsHandler.inc.php:379
PluginRegistry\loadCategory
static loadCategory($category, $enabledOnly=false, $mainContextId=null)
Definition: PluginRegistry.inc.php:103
PKPToolsHandler\setupTemplate
setupTemplate($request)
Definition: PKPToolsHandler.inc.php:41
PKPNotificationOperationManager\createTrivialNotification
createTrivialNotification($userId, $notificationType=NOTIFICATION_TYPE_SUCCESS, $params=null)
Definition: PKPNotificationOperationManager.inc.php:193
Application\getContextAssocType
static getContextAssocType()
Definition: Application.inc.php:199
PKPToolsHandler\hasAppStatsSettings
hasAppStatsSettings()
Definition: PKPToolsHandler.inc.php:422
JSONMessage
Class to represent a JSON (Javascript Object Notation) message.
Definition: JSONMessage.inc.php:18
PKPToolsHandler\tools
tools($args, $request)
Definition: PKPToolsHandler.inc.php:51
PKPToolsHandler\index
index($args, $request)
Definition: PKPToolsHandler.inc.php:87
PKPToolsHandler\report
report($args, $request)
Definition: PKPToolsHandler.inc.php:148
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
PKPToolsHandler\generateReport
generateReport($args, $request)
Definition: PKPToolsHandler.inc.php:197
PKPToolsHandler\permissions
permissions($args, $request)
Definition: PKPToolsHandler.inc.php:431
PKPToolsHandler
Handle requests for Tool pages.
Definition: PKPToolsHandler.inc.php:21
PluginRegistry\getPlugin
static getPlugin($category, $name)
Definition: PluginRegistry.inc.php:85
PKPToolsHandler\statistics
statistics($args, $request)
Definition: PKPToolsHandler.inc.php:119
NotificationManager
Definition: NotificationManager.inc.php:19
PKPToolsHandler\saveStatisticsSettings
saveStatisticsSettings($args, $request)
Definition: PKPToolsHandler.inc.php:354
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
PKPToolsHandler\__construct
__construct()
Definition: PKPToolsHandler.inc.php:29
Application\getSectionDAO
static getSectionDAO()
Definition: Application.inc.php:154
PKPToolsHandler\reportGenerator
reportGenerator($args, $request)
Definition: PKPToolsHandler.inc.php:167
ManagementHandler
Base class for all management page handlers.
Definition: ManagementHandler.inc.php:19
PKPToolsHandler\getObjectTitle
getObjectTitle($assocId, $assocType)
Definition: PKPToolsHandler.inc.php:390
PKPToolsHandler\importexport
importexport($args, $request)
Definition: PKPToolsHandler.inc.php:99