17 import(
'lib.pkp.classes.form.Form');
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);
30 private $_optionalColumns;
42 private $_defaultReportTemplates;
45 private $_reportTemplateIndex;
61 function __construct($columns, $optionalColumns, $objects, $fileTypes, $metricType, $defaultReportTemplates, $reportTemplateIndex =
null) {
62 parent::__construct(
'controllers/statistics/form/reportGeneratorForm.tpl');
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;
80 function fetch($request, $template =
null, $display =
false) {
81 $router = $request->getRouter();
82 $context = $router->getContext($request);
83 $columns = $this->_columns;
86 $availableMetricTypeStrings = $statsHelper->getAllMetricTypeStrings();
87 if (count($availableMetricTypeStrings) > 1) {
88 $this->
setData(
'metricTypeOptions', $availableMetricTypeStrings);
91 $reportTemplateOptions = array();
92 $reportTemplates = $this->_defaultReportTemplates;
93 foreach($reportTemplates as $reportTemplate) {
94 $reportTemplateOptions[] = __($reportTemplate[
'nameLocaleKey']);
97 if (!empty($reportTemplateOptions)) $this->
setData(
'reportTemplateOptions', $reportTemplateOptions);
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)) {
105 $this->
setData(
'columns', $reportColumns);
106 $this->
setData(
'reportTemplate', $reportTemplateIndex);
107 if (isset($reportTemplate[
'aggregationColumns'])) {
108 $aggreationColumns = $reportTemplate[
'aggregationColumns'];
109 if (is_array($aggreationColumns)) {
111 $aggreationOptions = $selectedAggregationOptions = array();
112 foreach ($aggreationColumns as $column) {
113 $columnName = $statsHelper->getColumnNames($column);
114 if (!$columnName)
continue;
115 $aggreationOptions[$column] = $columnName;
117 $this->
setData(
'aggregationOptions', $aggreationOptions);
118 $this->
setData(
'selectedAggregationOptions', array_intersect($aggreationColumns, $reportColumns));
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);
134 $timeFilterSelectedOption = $request->getUserVar(
'timeFilterOption');
135 if (is_null($timeFilterSelectedOption)) {
136 $timeFilterSelectedOption = TIME_FILTER_OPTION_CURRENT_MONTH;
138 switch ($timeFilterSelectedOption) {
139 case TIME_FILTER_OPTION_YESTERDAY:
140 $this->
setData(
'yesterday',
true);
142 case TIME_FILTER_OPTION_CURRENT_MONTH:
144 $this->
setData(
'currentMonth',
true);
146 case TIME_FILTER_OPTION_RANGE_DAY:
149 case TIME_FILTER_OPTION_RANGE_MONTH:
150 $this->
setData(
'byMonth',
true);
154 $startTime = $request->getUserDateVar(
'dateStart');
155 $endTime = $request->getUserDateVar(
'dateEnd');
156 if (!$startTime) $startTime = time();
157 if (!$endTime) $endTime = time();
159 $this->
setData(
'dateStart', $startTime);
160 $this->
setData(
'dateEnd', $endTime);
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);
171 $this->
setData(
'showRegionInput', isset($columns[STATISTICS_DIMENSION_REGION]));
172 $this->
setData(
'showCityInput', isset($columns[STATISTICS_DIMENSION_CITY]));
175 $this->
setData(
'showMonthInputs', isset($columns[STATISTICS_DIMENSION_MONTH]));
176 $this->
setData(
'showDayInputs', isset($columns[STATISTICS_DIMENSION_DAY]));
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
187 foreach($nonOrderableColumns as $column) {
188 unset($orderColumns[$column]);
191 $this->
setData(
'metricType', $this->_metricType);
192 $this->
setData(
'objectTypesOptions', $this->_objects);
193 if ($this->_fileTypes) {
194 $this->
setData(
'fileTypesOptions', $this->_fileTypes);
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')));
202 $columnsOptions = $this->_columns;
204 unset($columnsOptions[STATISTICS_METRIC]);
205 $this->
setData(
'columnsOptions', $columnsOptions);
206 $this->
setData(
'optionalColumns', $this->_optionalColumns);
208 return parent::fetch($request, $template, $display);
215 $this->
readUserVars(array(
'columns',
'objectTypes',
'fileTypes',
'objectIds',
'issues',
216 'articles',
'timeFilterOption',
'countries',
'regions',
'cityNames',
217 'orderByColumn',
'orderByDirection'));
218 return parent::readInputData();
224 function execute(...$functionArgs) {
225 parent::execute(...$functionArgs);
227 $router = $request->getRouter();
228 $context = $router->getContext($request);
231 $columns = $this->
getData(
'columns');
233 if ($this->
getData(
'objectTypes')) {
234 $filter[STATISTICS_DIMENSION_ASSOC_TYPE] = $this->
getData(
'objectTypes');
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;
242 if ($this->
getData(
'fileTypes')) {
243 $filter[STATISTICS_DIMENSION_FILE_TYPE] = $this->
getData(
'fileTypes');
246 $filter[STATISTICS_DIMENSION_CONTEXT_ID] = $context->getId();
248 if ($this->
getData(
'issues')) {
249 $filter[STATISTICS_DIMENSION_ISSUE_ID] = $this->
getData(
'issues');
252 if ($this->
getData(
'articles')) {
253 $filter[STATISTICS_DIMENSION_SUBMISSION_ID] = $this->
getData(
'articles');
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);
268 $timeFilterOption = $this->
getData(
'timeFilterOption');
269 switch($timeFilterOption) {
270 case TIME_FILTER_OPTION_YESTERDAY:
271 $filter[STATISTICS_DIMENSION_DAY] = STATISTICS_YESTERDAY;
273 case TIME_FILTER_OPTION_CURRENT_MONTH:
274 $filter[STATISTICS_DIMENSION_MONTH] = STATISTICS_CURRENT_MONTH;
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;
282 if ($timeFilterOption == TIME_FILTER_OPTION_RANGE_DAY) {
283 $startDate .= $startDay;
286 $dimension = STATISTICS_DIMENSION_DAY;
289 if ($startDate == $endDate) {
290 $filter[$dimension] = $startDate;
292 $filter[$dimension][
'from'] = $startDate;
293 $filter[$dimension][
'to'] = $endDate;
300 if ($this->
getData(
'countries')) {
301 $filter[STATISTICS_DIMENSION_COUNTRY] = $this->
getData(
'countries');
304 if ($this->
getData(
'regions')) {
305 $filter[STATISTICS_DIMENSION_REGION] = $this->
getData(
'regions');
308 if ($this->
getData(
'cityNames')) {
309 $cityNames = explode(
',', $this->
getData(
'cityNames'));
310 $filter[STATISTICS_DIMENSION_CITY] = $cityNames;
314 if ($this->
getData(
'orderByColumn') && $this->
getData(
'orderByDirection')) {
315 $orderByColumn = $this->
getData(
'orderByColumn');
316 $orderByDirection = $this->
getData(
'orderByDirection');
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;
332 return $statsHelper->getReportUrl($request, $this->_metricType, $columns, $filter, $orderBy);