Open Journal Systems  3.3.0
PKPReportGeneratorForm.inc.php
1 <?php
2 
17 import('lib.pkp.classes.form.Form');
18 
19 define('TIME_FILTER_OPTION_YESTERDAY', 0);
20 define('TIME_FILTER_OPTION_CURRENT_MONTH', 1);
21 define('TIME_FILTER_OPTION_RANGE_DAY', 2);
22 define('TIME_FILTER_OPTION_RANGE_MONTH', 3);
23 
24 abstract class PKPReportGeneratorForm extends Form {
25 
26  /* @var $_columns array */
27  private $_columns;
28 
29  /* @var $_columns array */
30  private $_optionalColumns;
31 
32  /* @var $_objects array */
33  private $_objects;
34 
35  /* @var $_fileTypes array */
36  private $_fileTypes;
37 
38  /* @var $_metricType string */
39  private $_metricType;
40 
41  /* @var $_defaultReportTemplates array */
42  private $_defaultReportTemplates;
43 
44  /* @var $_reportTemplateIndex int */
45  private $_reportTemplateIndex;
46 
61  function __construct($columns, $optionalColumns, $objects, $fileTypes, $metricType, $defaultReportTemplates, $reportTemplateIndex = null) {
62  parent::__construct('controllers/statistics/form/reportGeneratorForm.tpl');
63 
64  $this->_columns = $columns;
65  $this->_optionalColumns = $optionalColumns;
66  $this->_objects = $objects;
67  $this->_fileTypes = $fileTypes;
68  $this->_metricType = $metricType;
69  $this->_defaultReportTemplates = $defaultReportTemplates;
70  $this->_reportTemplateIndex = $reportTemplateIndex;
71 
72  $this->addCheck(new FormValidatorArray($this, 'columns', 'required', 'manager.statistics.reports.form.columnsRequired'));
73  $this->addCheck(new FormValidatorPost($this));
74  $this->addCheck(new FormValidatorCSRF($this));
75  }
76 
80  function fetch($request, $template = null, $display = false) {
81  $router = $request->getRouter();
82  $context = $router->getContext($request);
83  $columns = $this->_columns;
84  $statsHelper = new StatisticsHelper();
85 
86  $availableMetricTypeStrings = $statsHelper->getAllMetricTypeStrings();
87  if (count($availableMetricTypeStrings) > 1) {
88  $this->setData('metricTypeOptions', $availableMetricTypeStrings);
89  }
90 
91  $reportTemplateOptions = array();
92  $reportTemplates = $this->_defaultReportTemplates;
93  foreach($reportTemplates as $reportTemplate) {
94  $reportTemplateOptions[] = __($reportTemplate['nameLocaleKey']);
95  }
96 
97  if (!empty($reportTemplateOptions)) $this->setData('reportTemplateOptions', $reportTemplateOptions);
98 
99  $reportTemplateIndex = (int) $this->_reportTemplateIndex;
100  if (!is_null($reportTemplateIndex) && isset($reportTemplates[$reportTemplateIndex])) {
101  $reportTemplate = $reportTemplates[$reportTemplateIndex];
102  $reportColumns = $reportTemplate['columns'];
103  if (is_array($reportColumns)) {
104 
105  $this->setData('columns', $reportColumns);
106  $this->setData('reportTemplate', $reportTemplateIndex);
107  if (isset($reportTemplate['aggregationColumns'])) {
108  $aggreationColumns = $reportTemplate['aggregationColumns'];
109  if (is_array($aggreationColumns)) {
110 
111  $aggreationOptions = $selectedAggregationOptions = array();
112  foreach ($aggreationColumns as $column) {
113  $columnName = $statsHelper->getColumnNames($column);
114  if (!$columnName) continue;
115  $aggreationOptions[$column] = $columnName;
116  }
117  $this->setData('aggregationOptions', $aggreationOptions);
118  $this->setData('selectedAggregationOptions', array_intersect($aggreationColumns, $reportColumns));
119  }
120  }
121 
122  if (isset($reportTemplate['filter']) && is_array($reportTemplate['filter'])) {
123  foreach ($reportTemplate['filter'] as $dimension => $filter) {
124  switch ($dimension) {
125  case STATISTICS_DIMENSION_ASSOC_TYPE:
126  $this->setData('objectTypes', $filter);
127  break;
128  }
129  }
130  }
131  }
132  }
133 
134  $timeFilterSelectedOption = $request->getUserVar('timeFilterOption');
135  if (is_null($timeFilterSelectedOption)) {
136  $timeFilterSelectedOption = TIME_FILTER_OPTION_CURRENT_MONTH;
137  }
138  switch ($timeFilterSelectedOption) {
139  case TIME_FILTER_OPTION_YESTERDAY:
140  $this->setData('yesterday', true);
141  break;
142  case TIME_FILTER_OPTION_CURRENT_MONTH:
143  default:
144  $this->setData('currentMonth', true);
145  break;
146  case TIME_FILTER_OPTION_RANGE_DAY:
147  $this->setData('byDay', true);
148  break;
149  case TIME_FILTER_OPTION_RANGE_MONTH:
150  $this->setData('byMonth', true);
151  break;
152  }
153 
154  $startTime = $request->getUserDateVar('dateStart');
155  $endTime = $request->getUserDateVar('dateEnd');
156  if (!$startTime) $startTime = time();
157  if (!$endTime) $endTime = time();
158 
159  $this->setData('dateStart', $startTime);
160  $this->setData('dateEnd', $endTime);
161 
162  if (isset($columns[STATISTICS_DIMENSION_COUNTRY])) {
163  $geoLocationTool = $statsHelper->getGeoLocationTool();
164  if ($geoLocationTool) {
165  $countryCodes = $geoLocationTool->getAllCountryCodes();
166  if (!$countryCodes) $countryCodes = array();
167  $countryCodes = array_combine($countryCodes, $countryCodes);
168  $this->setData('countriesOptions', $countryCodes);
169  }
170 
171  $this->setData('showRegionInput', isset($columns[STATISTICS_DIMENSION_REGION]));
172  $this->setData('showCityInput', isset($columns[STATISTICS_DIMENSION_CITY]));
173  }
174 
175  $this->setData('showMonthInputs', isset($columns[STATISTICS_DIMENSION_MONTH]));
176  $this->setData('showDayInputs', isset($columns[STATISTICS_DIMENSION_DAY]));
177 
178  $orderColumns = $this->_columns;
179  $nonOrderableColumns = array(STATISTICS_DIMENSION_ASSOC_TYPE,
180  STATISTICS_DIMENSION_SUBMISSION_ID,
181  STATISTICS_DIMENSION_CONTEXT_ID,
182  STATISTICS_DIMENSION_REGION,
183  STATISTICS_DIMENSION_FILE_TYPE,
184  STATISTICS_DIMENSION_METRIC_TYPE
185  );
186 
187  foreach($nonOrderableColumns as $column) {
188  unset($orderColumns[$column]);
189  }
190 
191  $this->setData('metricType', $this->_metricType);
192  $this->setData('objectTypesOptions', $this->_objects);
193  if ($this->_fileTypes) {
194  $this->setData('fileTypesOptions', $this->_fileTypes);
195  }
196  $this->setData('fileAssocTypes', $this->getFileAssocTypes());
197  $this->setData('orderColumnsOptions', $orderColumns);
198  $this->setData('orderDirectionsOptions', array(
199  STATISTICS_ORDER_ASC => __('manager.statistics.reports.orderDir.asc'),
200  STATISTICS_ORDER_DESC => __('manager.statistics.reports.orderDir.desc')));
201 
202  $columnsOptions = $this->_columns;
203  // Reports will always include this column.
204  unset($columnsOptions[STATISTICS_METRIC]);
205  $this->setData('columnsOptions', $columnsOptions);
206  $this->setData('optionalColumns', $this->_optionalColumns);
207 
208  return parent::fetch($request, $template, $display);
209  }
210 
214  function readInputData() {
215  $this->readUserVars(array('columns', 'objectTypes', 'fileTypes', 'objectIds', 'issues',
216  'articles', 'timeFilterOption', 'countries', 'regions', 'cityNames',
217  'orderByColumn', 'orderByDirection'));
218  return parent::readInputData();
219  }
220 
224  function execute(...$functionArgs) {
225  parent::execute(...$functionArgs);
226  $request = Application::get()->getRequest();
227  $router = $request->getRouter(); /* @var $router PageRouter */
228  $context = $router->getContext($request);
229  $statsHelper = new StatisticsHelper();
230 
231  $columns = $this->getData('columns');
232  $filter = array();
233  if ($this->getData('objectTypes')) {
234  $filter[STATISTICS_DIMENSION_ASSOC_TYPE] = $this->getData('objectTypes');
235  }
236 
237  if ($this->getData('objectIds') && count($filter[STATISTICS_DIMENSION_ASSOC_TYPE] == 1)) {
238  $objectIds = explode(',', $this->getData('objectIds'));
239  $filter[STATISTICS_DIMENSION_ASSOC_ID] = $objectIds;
240  }
241 
242  if ($this->getData('fileTypes')) {
243  $filter[STATISTICS_DIMENSION_FILE_TYPE] = $this->getData('fileTypes');
244  }
245 
246  $filter[STATISTICS_DIMENSION_CONTEXT_ID] = $context->getId();
247 
248  if ($this->getData('issues')) {
249  $filter[STATISTICS_DIMENSION_ISSUE_ID] = $this->getData('issues');
250  }
251 
252  if ($this->getData('articles')) {
253  $filter[STATISTICS_DIMENSION_SUBMISSION_ID] = $this->getData('articles');
254  }
255 
256  // Get the time filter data, if any.
257  $startTime = $request->getUserDateVar('dateStart', 1, 1, 1, 23, 59, 59);
258  $endTime = $request->getUserDateVar('dateEnd', 1, 1, 1, 23, 59, 59);
259  if ($startTime && $endTime) {
260  $startYear = date('Y', $startTime);
261  $endYear = date('Y', $endTime);
262  $startMonth = date('m', $startTime);
263  $endMonth = date('m', $endTime);
264  $startDay = date('d', $startTime);
265  $endDay = date('d', $endTime);
266  }
267 
268  $timeFilterOption = $this->getData('timeFilterOption');
269  switch($timeFilterOption) {
270  case TIME_FILTER_OPTION_YESTERDAY:
271  $filter[STATISTICS_DIMENSION_DAY] = STATISTICS_YESTERDAY;
272  break;
273  case TIME_FILTER_OPTION_CURRENT_MONTH:
274  $filter[STATISTICS_DIMENSION_MONTH] = STATISTICS_CURRENT_MONTH;
275  break;
276  case TIME_FILTER_OPTION_RANGE_DAY:
277  case TIME_FILTER_OPTION_RANGE_MONTH:
278  $dimension = STATISTICS_DIMENSION_MONTH;
279  $startDate = $startYear . $startMonth;
280  $endDate = $endYear . $endMonth;
281 
282  if ($timeFilterOption == TIME_FILTER_OPTION_RANGE_DAY) {
283  $startDate .= $startDay;
284  $endDate .= $endDay;
285 
286  $dimension = STATISTICS_DIMENSION_DAY;
287  }
288 
289  if ($startDate == $endDate) {
290  $filter[$dimension] = $startDate;
291  } else {
292  $filter[$dimension]['from'] = $startDate;
293  $filter[$dimension]['to'] = $endDate;
294  }
295  break;
296  default:
297  break;
298  }
299 
300  if ($this->getData('countries')) {
301  $filter[STATISTICS_DIMENSION_COUNTRY] = $this->getData('countries');
302  }
303 
304  if ($this->getData('regions')) {
305  $filter[STATISTICS_DIMENSION_REGION] = $this->getData('regions');
306  }
307 
308  if ($this->getData('cityNames')) {
309  $cityNames = explode(',', $this->getData('cityNames'));
310  $filter[STATISTICS_DIMENSION_CITY] = $cityNames;
311  }
312 
313  $orderBy = array();
314  if ($this->getData('orderByColumn') && $this->getData('orderByDirection')) {
315  $orderByColumn = $this->getData('orderByColumn');
316  $orderByDirection = $this->getData('orderByDirection');
317 
318  $columnIndex = 0;
319 
320  foreach ($orderByColumn as $column) {
321  if ($column != '0' && !isset($orderBy[$column])) {
322  $orderByDir = $orderByDirection[$columnIndex];
323  if ($orderByDir == STATISTICS_ORDER_ASC || $orderByDir == STATISTICS_ORDER_DESC) {
324  $orderBy[$column] = $orderByDir;
325  }
326  }
327 
328  $columnIndex++;
329  }
330  }
331 
332  return $statsHelper->getReportUrl($request, $this->_metricType, $columns, $filter, $orderBy);
333  }
334 
335 
336  //
337  // Protected methods.
338  //
343  abstract function getFileAssocTypes();
344 }
345 
346 
FormValidatorArray
Form validation check that checks an array of fields.
Definition: FormValidatorArray.inc.php:18
Form\setData
setData($key, $value=null)
Definition: Form.inc.php:229
StatisticsHelper
Statistics helper class.
Definition: StatisticsHelper.inc.php:21
Form\readUserVars
readUserVars($vars)
Definition: Form.inc.php:378
PKPReportGeneratorForm\fetch
fetch($request, $template=null, $display=false)
Definition: PKPReportGeneratorForm.inc.php:101
Form\getData
getData($key)
Definition: Form.inc.php:220
FormValidatorPost
Form validation check to make sure the form is POSTed.
Definition: FormValidatorPost.inc.php:18
PKPReportGeneratorForm\__construct
__construct($columns, $optionalColumns, $objects, $fileTypes, $metricType, $defaultReportTemplates, $reportTemplateIndex=null)
Definition: PKPReportGeneratorForm.inc.php:82
PKPReportGeneratorForm\getFileAssocTypes
getFileAssocTypes()
PKPReportGeneratorForm\execute
execute(... $functionArgs)
Definition: PKPReportGeneratorForm.inc.php:245
PKPReportGeneratorForm\readInputData
readInputData()
Definition: PKPReportGeneratorForm.inc.php:235
PKPReportGeneratorForm
Base form class to generate custom statistics reports.
Definition: PKPReportGeneratorForm.inc.php:24
Form\addCheck
addCheck($formValidator)
Definition: Form.inc.php:395
FormValidatorCSRF
Form validation check to make sure the CSRF token is correct.
Definition: FormValidatorCSRF.inc.php:18
Form
Class defining basic operations for handling HTML forms.
Definition: Form.inc.php:47
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235