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