Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
ReportGeneratorHandler.inc.php
1 <?php
2 
16 import('classes.handler.Handler');
17 import('lib.pkp.classes.core.JSONMessage');
18 import('classes.statistics.StatisticsHelper');
19 
25  parent::Handler();
26  $this->addRoleAssignment(
27  ROLE_ID_MANAGER,
28  array('fetchReportGenerator', 'saveReportGenerator', 'fetchArticlesInfo', 'fetchRegions'));
29  }
30 
36  function fetchReportGenerator($args, $request) {
37  $this->setupTemplate($request);
38  $reportGeneratorForm = $this->_getReportGeneratorForm($request);
39  $reportGeneratorForm->initData($request);
40 
41  $formContent = $reportGeneratorForm->fetch($request);
42 
43  $json = new JSONMessage(true);
44  if ($request->getUserVar('refreshForm')) {
45  $json->setEvent('refreshForm', $formContent);
46  } else {
47  $json->setContent($formContent);
48  }
49 
50  return $json->getString();
51  }
52 
58  function saveReportGenerator($args, $request) {
59  $this->setupTemplate($request);
60 
61  $reportGeneratorForm = $this->_getReportGeneratorForm($request);
62  $reportGeneratorForm->readInputData();
63  $json = new JSONMessage(true);
64  if ($reportGeneratorForm->validate()) {
65  $reportUrl = $reportGeneratorForm->execute($request);
66  $json->setAdditionalAttributes(array('reportUrl' => $reportUrl));
67  } else {
68  $json->setStatus(false);
69  }
70 
71  return $json->getString();
72  }
73 
81  function fetchArticlesInfo($args, $request) {
82  $this->validate();
83 
84  $issueId = (int) $request->getUserVar('issueId');
85  import('lib.pkp.classes.core.JSONMessage');
86  $json = new JSONMessage();
87 
88  if (!$issueId) {
89  $json->setStatus(false);
90  } else {
91  $articleDao = DAORegistry::getDAO('PublishedArticleDAO'); /* @var $articleDao PublishedArticleDAO */
92  $articles = $articleDao->getPublishedArticles($issueId);
93  $articlesInfo = array();
94  foreach ($articles as $article) {
95  $articlesInfo[] = array('id' => $article->getId(), 'title' => $article->getLocalizedTitle());
96  }
97 
98  $json->setContent($articlesInfo);
99  }
100 
101  return $json->getString();
102  }
103 
111  function fetchRegions($args, $request) {
112  $this->validate();
113 
114  $countryId = (string) $request->getUserVar('countryId');
115  import('lib.pkp.classes.core.JSONMessage');
116  $json = new JSONMessage(false);
117 
118  if ($countryId) {
119  $statsHelper = new StatisticsHelper();
120  $geoLocationTool = $statsHelper->getGeoLocationTool();
121  if ($geoLocationTool) {
122  $regions = $geoLocationTool->getRegions($countryId);
123  if (!empty($regions)) {
124  $regionsData = array();
125  foreach ($regions as $id => $name) {
126  $regionsData[] = array('id' => $id, 'name' => $name);
127  }
128  $json->setStatus(true);
129  $json->setContent($regionsData);
130  }
131  }
132  }
133 
134  return $json->getString();
135  }
136 
140  function setupTemplate($request) {
141  parent::setupTemplate($request);
142  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_MANAGER, LOCALE_COMPONENT_PKP_SUBMISSION,
143  LOCALE_COMPONENT_APP_MANAGER, LOCALE_COMPONENT_APP_SUBMISSION);
144  }
145 
146 
147  //
148  // Private helper methods.
149  //
154  function &_getReportGeneratorForm($request) {
155  $router = $request->getRouter();
156  $context = $router->getContext($request);
157 
158  $metricType = $request->getUserVar('metricType');
159  if (!$metricType) {
160  $metricType = $context->getDefaultMetricType();
161  }
162 
163  $statsHelper = new StatisticsHelper();
164  $reportPlugin = $statsHelper->getReportPluginByMetricType($metricType);
165  if (!is_scalar($metricType) || !$reportPlugin) {
166  fatalError('Invalid metric type.');
167  }
168 
169  $columns = $reportPlugin->getColumns($metricType);
170  $columns = array_flip(array_intersect(array_flip($statsHelper->getColumnNames()), $columns));
171 
172  $objects = $reportPlugin->getObjectTypes($metricType);
173  $objects = array_flip(array_intersect(array_flip($statsHelper->getObjectTypeString()), $objects));
174 
175  $defaultReportTemplates = $reportPlugin->getDefaultReportTemplates($metricType);
176 
177  // If the report plugin doesn't works with the file type column,
178  // don't load file types.
179  if (isset($columns[STATISTICS_DIMENSION_FILE_TYPE])) {
180  $fileTypes = $statsHelper->getFileTypeString();
181  } else {
182  $fileTypes = null;
183  }
184 
185  // Metric type will be presented in header, remove if any.
186  if (isset($columns[STATISTICS_DIMENSION_METRIC_TYPE])) unset($columns[STATISTICS_DIMENSION_METRIC_TYPE]);
187 
188  $reportTemplate = $request->getUserVar('reportTemplate');
189 
190  import('controllers.statistics.form.ReportGeneratorForm');
191  $reportGeneratorForm = new ReportGeneratorForm($columns,
192  $objects, $fileTypes, $metricType, $defaultReportTemplates, $reportTemplate);
193 
194  return $reportGeneratorForm;
195  }
196 }
197 
198 ?>
static & getDAO($name, $dbconn=null)
static requireComponents()
Form to generate custom statistics reports.
addRoleAssignment($roleIds, $operations)
Handle requests for report generator functions.
Class to represent a JSON (Javascript Object Notation) message.
validate($requiredContexts=null, $request=null)
Base request handler application class.
Definition: Handler.inc.php:20
Statistics helper class.