| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph; |
|
|
| use Piwik\API\Request as ApiRequest; |
| use Piwik\Common; |
| use Piwik\Container\StaticContainer; |
| use Piwik\Period\Factory; |
| use Piwik\Period\Range; |
| use Piwik\Plugins\CoreVisualizations\JqplotDataGenerator; |
| use Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph; |
| use Piwik\Plugins\CoreVisualizations\Visualizations\EvolutionPeriodSelector; |
| use Piwik\Site; |
|
|
| |
| |
| |
| |
| |
| class Evolution extends JqplotGraph |
| { |
| public const ID = 'graphEvolution'; |
| public const SERIES_COLOR_COUNT = 8; |
|
|
| public static function getDefaultConfig() |
| { |
| return new Evolution\Config(); |
| } |
|
|
| public function beforeRender() |
| { |
| parent::beforeRender(); |
|
|
| $this->checkRequestIsOnlyForMultiplePeriods(); |
|
|
| $this->config->show_flatten_table = false; |
| $this->config->datatable_js_type = 'JqplotEvolutionGraphDataTable'; |
| } |
|
|
| public function beforeLoadDataTable() |
| { |
| $isComparingDatesOrPeriods = $this->isComparingDatesOrPeriods(); |
|
|
| if (!$this->isComparing() || !$isComparingDatesOrPeriods) { |
| $this->calculateEvolutionDateRange(); |
| } |
|
|
| parent::beforeLoadDataTable(); |
|
|
| |
| $this->requestConfig->request_parameters_to_modify['fetch_archive_state'] = true; |
|
|
| |
| |
| |
| $period = Common::getRequestVar('period', false); |
| $selector = StaticContainer::get(EvolutionPeriodSelector::class); |
|
|
| if ($period === 'range') { |
| $date = Common::getRequestVar('date', false); |
| $requestingPeriod = Factory::build($period, $date); |
|
|
| |
| |
| $this->requestConfig->request_parameters_to_modify['period'] = $selector->getHighestPeriodInCommon($requestingPeriod, []); |
| $this->requestConfig->request_parameters_to_modify['date'] = $requestingPeriod->getRangeString(); |
| } |
|
|
| $this->config->custom_parameters['columns'] = $this->config->columns_to_display; |
|
|
| if ($this->isComparing() && $isComparingDatesOrPeriods) { |
| $this->config->show_limit_control = false; |
| $this->config->show_periods = false; |
|
|
| $requestArray = $this->request->getRequestArray(); |
| $requestArray = ApiRequest::getRequestArrayFromString($requestArray); |
|
|
| $requestingPeriod = Factory::build($requestArray['period'], $requestArray['date']); |
|
|
| $comparisonPeriods = []; |
| if (!empty($requestArray['comparePeriods'])) { |
| $comparisonPeriods = $selector->getComparisonPeriodObjects($requestArray['comparePeriods'], $requestArray['compareDates']); |
| } |
|
|
| $this->requestConfig->request_parameters_to_modify = $selector->setDatePeriods( |
| $this->requestConfig->request_parameters_to_modify, |
| $requestingPeriod, |
| $comparisonPeriods, |
| true |
| ); |
| } |
| } |
|
|
| private function isComparingDatesOrPeriods(): bool |
| { |
| $comparePeriods = Common::getRequestVar('comparePeriods', [], 'array'); |
| $compareDates = Common::getRequestVar('compareDates', [], 'array'); |
|
|
| return !empty($comparePeriods) || !empty($compareDates); |
| } |
|
|
| public function afterAllFiltersAreApplied() |
| { |
| parent::afterAllFiltersAreApplied(); |
|
|
| if (false === $this->config->x_axis_step_size) { |
| $rowCount = $this->dataTable->getRowsCount(); |
|
|
| $this->config->x_axis_step_size = $this->getDefaultXAxisStepSize($rowCount); |
| } |
| } |
|
|
| protected function makeDataGenerator($properties) |
| { |
| return JqplotDataGenerator::factory('evolution', $properties, $this); |
| } |
|
|
| |
| |
| |
| |
| private function calculateEvolutionDateRange() |
| { |
| $period = Common::getRequestVar('period'); |
| $idSite = Common::getRequestVar('idSite'); |
| $timezone = Site::getTimezoneFor($idSite); |
|
|
| $lastNParamName = self::getLastNParamName($period); |
| $defaultLastN = $this->config->custom_parameters[$lastNParamName] ?? self::getDefaultLastN($period); |
| $originalDate = Common::getRequestVar('date', 'last' . $defaultLastN, 'string'); |
|
|
| if ('range' != $period) { |
| |
| if (Range::parseDateRange($originalDate)) { |
| |
|
|
| |
| $oPeriod = new Range($period, $originalDate, $timezone); |
| $lastN = count($oPeriod->getSubperiods()); |
| } else { |
| |
| list($newDate, $lastN) = self::getDateRangeAndLastN($period, $originalDate, $defaultLastN); |
| $this->requestConfig->request_parameters_to_modify['date'] = $newDate; |
| $this->config->custom_parameters['dateUsedInGraph'] = $newDate; |
| } |
|
|
| $this->config->custom_parameters[$lastNParamName] = $lastN; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function getDateRangeAndLastN($period, $endDate, $defaultLastN = null, ?int $idSite = null) |
| { |
| if ($defaultLastN === null) { |
| $defaultLastN = self::getDefaultLastN($period); |
| } |
|
|
| $lastNParamName = self::getLastNParamName($period); |
| $lastN = Common::getRequestVar($lastNParamName, $defaultLastN, 'int'); |
|
|
| $idSite = $idSite ?? Common::getRequestVar('idSite'); |
| $site = new Site($idSite); |
|
|
| $dateRange = Range::getRelativeToEndDate($period, 'last' . $lastN, $endDate, $site); |
|
|
| return array($dateRange, $lastN); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static function getDefaultLastN($period) |
| { |
| switch ($period) { |
| case 'week': |
| return 26; |
| case 'month': |
| return 24; |
| case 'year': |
| return 5; |
| case 'day': |
| default: |
| return 30; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public static function getLastNParamName($period) |
| { |
| return "evolution_{$period}_last_n"; |
| } |
|
|
| public function getDefaultXAxisStepSize($countGraphElements) |
| { |
| |
| if ($countGraphElements <= 7) { |
| return 1; |
| } |
|
|
| $periodLabel = Common::getRequestVar('period'); |
|
|
| switch ($periodLabel) { |
| case 'day': |
| case 'range': |
| $steps = 5; |
| break; |
| case 'week': |
| $steps = 4; |
| break; |
| case 'month': |
| $steps = 5; |
| break; |
| case 'year': |
| $steps = 5; |
| break; |
| default: |
| $steps = 5; |
| break; |
| } |
|
|
| $paddedCount = $countGraphElements + 2; |
|
|
| return ceil($paddedCount / $steps); |
| } |
|
|
| public function supportsComparison() |
| { |
| return true; |
| } |
|
|
| protected function ensureValidColumnsToDisplay() |
| { |
| parent::ensureValidColumnsToDisplay(); |
|
|
| $columnsToDisplay = $this->config->columns_to_display; |
|
|
| |
| $this->config->columns_to_display = $columnsToDisplay ? : array('nb_visits'); |
| } |
| } |
|
|