| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Plugins\CustomDimensions; |
|
|
| use Piwik\Common; |
| use Piwik\Archive; |
| use Piwik\DataTable; |
| use Piwik\Filesystem; |
| use Piwik\Piwik; |
| use Piwik\Plugins\CustomDimensions\Dao\Configuration; |
| use Piwik\Plugins\CustomDimensions\Dao\LogTable; |
| use Piwik\Plugins\CustomDimensions\Dimension\Active; |
| use Piwik\Plugins\CustomDimensions\Dimension\CaseSensitive; |
| use Piwik\Plugins\CustomDimensions\Dimension\Description; |
| use Piwik\Plugins\CustomDimensions\Dimension\Dimension; |
| use Piwik\Plugins\CustomDimensions\Dimension\Extraction; |
| use Piwik\Plugins\CustomDimensions\Dimension\Extractions; |
| use Piwik\Plugins\CustomDimensions\Dimension\Index; |
| use Piwik\Plugins\CustomDimensions\Dimension\Name; |
| use Piwik\Plugins\CustomDimensions\Dimension\Scope; |
| use Piwik\Tracker\Cache; |
|
|
| |
| |
| |
| |
| |
| class API extends \Piwik\Plugin\API |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function getCustomDimension(int $idDimension, int $idSite, string $period, string $date, $segment = false, bool $expanded = false, bool $flat = false, $idSubtable = false) |
| { |
| Piwik::checkUserHasViewAccess($idSite); |
|
|
| $dimension = new Dimension($idDimension, $idSite); |
| $dimension->checkActive(); |
|
|
| $record = Archiver::buildRecordNameForCustomDimensionId($idDimension); |
|
|
| $dataTable = Archive::createDataTableFromArchive($record, $idSite, $period, $date, $segment ?: '', $expanded, $flat, $idSubtable); |
|
|
| if (!empty($idSubtable) && $dataTable->getRowsCount()) { |
| $parentTable = Archive::createDataTableFromArchive($record, $idSite, $period, $date, $segment ?: ''); |
| $row = $parentTable->getRowFromIdSubDataTable($idSubtable); |
| if ($row) { |
| $parentValue = $row->getColumn('label'); |
| $dataTable->filter('Piwik\Plugins\CustomDimensions\DataTable\Filter\AddSubtableSegmentMetadata', array($idDimension, $parentValue)); |
| } |
| } else { |
| $dataTable->filter('Piwik\Plugins\CustomDimensions\DataTable\Filter\AddSegmentMetadata', array($idDimension)); |
| } |
|
|
| $dataTable->filter('Piwik\Plugins\CustomDimensions\DataTable\Filter\RemoveUserIfNeeded', array($idSite, $period, $date)); |
|
|
| return $dataTable; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function configureNewCustomDimension(int $idSite, string $name, string $scope, $active, $extractions = [], $caseSensitive = true, string $description = '') |
| { |
| Piwik::checkUserHasWriteAccess($idSite); |
|
|
| $this->checkCustomDimensionConfig($name, $active, $extractions, $caseSensitive, $description); |
|
|
| $scopeCheck = new Scope($scope); |
| $scopeCheck->check(); |
|
|
| $extractions = $this->unsanitizeExtractions($extractions); |
| $this->checkExtractionsAreSupportedForScope($scope, $extractions); |
|
|
| $index = new Index(); |
| $index = $index->getNextIndex($idSite, $scope); |
|
|
| $configuration = $this->getConfiguration(); |
| $idDimension = $configuration->configureNewDimension($idSite, $name, $scope, $index, $active, $extractions, $caseSensitive, $description); |
|
|
| Cache::deleteCacheWebsiteAttributes($idSite); |
| Cache::clearCacheGeneral(); |
| Filesystem::deleteAllCacheOnUpdate(); |
|
|
| return $idDimension; |
| } |
|
|
| |
| |
| |
| |
| private function unsanitizeExtractions($extractions) |
| { |
| if (!empty($extractions) && is_array($extractions)) { |
| foreach ($extractions as $index => $extraction) { |
| if (!empty($extraction['pattern']) && is_string($extraction['pattern'])) { |
| $extractions[$index]['pattern'] = Common::unsanitizeInputValue($extraction['pattern']); |
| } |
| } |
| } |
|
|
| return $extractions; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function configureExistingCustomDimension(int $idDimension, int $idSite, string $name, $active, $extractions = [], $caseSensitive = null, ?string $description = null): void |
| { |
| Piwik::checkUserHasWriteAccess($idSite); |
|
|
| $dimension = new Dimension($idDimension, $idSite); |
| $dimension->checkExists(); |
|
|
| if (!isset($caseSensitive)) { |
| $caseSensitive = $dimension->getCaseSensitive(); |
| } |
|
|
| if (!isset($description)) { |
| $description = $dimension->getDescription(); |
| } |
|
|
| $extractions = $this->unsanitizeExtractions($extractions); |
| $this->checkCustomDimensionConfig($name, $active, $extractions, $caseSensitive, $description); |
| $this->checkExtractionsAreSupportedForScope($dimension->getScope(), $extractions); |
|
|
| $this->getConfiguration()->configureExistingDimension($idDimension, $idSite, $name, $active, $extractions, $caseSensitive, $description); |
|
|
| Cache::deleteCacheWebsiteAttributes($idSite); |
| Cache::clearCacheGeneral(); |
| } |
|
|
| |
| |
| |
| |
| private function checkExtractionsAreSupportedForScope($scope, $extractions): void |
| { |
| if (!CustomDimensions::doesScopeSupportExtractions($scope) && !empty($extractions)) { |
| throw new \Exception("Extractions can be used only in scope 'action'"); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function getConfiguredCustomDimensions(int $idSite) |
| { |
| Piwik::checkUserHasViewAccess($idSite); |
|
|
| return $this->getConfiguration()->getCustomDimensionsForSite($idSite); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function getConfiguredCustomDimensionsHavingScope(int $idSite, $scope) |
| { |
| $result = $this->getConfiguredCustomDimensions($idSite); |
| $result = array_filter($result, function ($row) use ($scope) { |
| return $row['scope'] == $scope; |
| }); |
| return array_values($result); |
| } |
|
|
| |
| |
| |
| |
| |
| private function checkCustomDimensionConfig(string $name, $active, $extractions, $caseSensitive, string $description = ''): void |
| { |
| |
| |
| |
| |
|
|
| $name = new Name($name); |
| $name->check(); |
|
|
| $active = new Active($active); |
| $active->check(); |
|
|
| $extractions = new Extractions($extractions); |
| $extractions->check(); |
|
|
| $caseSensitive = new CaseSensitive($caseSensitive); |
| $caseSensitive->check(); |
|
|
| $description = new Description($description); |
| $description->check(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function getAvailableScopes(int $idSite): array |
| { |
| Piwik::checkUserHasViewAccess($idSite); |
|
|
| $scopes = []; |
| foreach (CustomDimensions::getPublicScopes() as $scope) { |
| $configs = $this->getConfiguredCustomDimensionsHavingScope($idSite, $scope); |
| $indexes = $this->getTracking($scope)->getInstalledIndexes(); |
|
|
| $scopes[] = [ |
| 'value' => $scope, |
| 'name' => Piwik::translate('General_TrackingScope' . ucfirst($scope)), |
| 'numSlotsAvailable' => count($indexes), |
| 'numSlotsUsed' => count($configs), |
| 'numSlotsLeft' => count($indexes) - count($configs), |
| 'supportsExtractions' => CustomDimensions::doesScopeSupportExtractions($scope), |
| ]; |
| } |
|
|
| return $scopes; |
| } |
|
|
| |
| |
| |
| |
| |
| public function getAvailableExtractionDimensions(): array |
| { |
| Piwik::checkUserHasSomeWriteAccess(); |
|
|
| $supported = Extraction::getSupportedDimensions(); |
|
|
| $dimensions = []; |
| foreach ($supported as $value => $dimension) { |
| $dimensions[] = ['value' => $value, 'name' => $dimension]; |
| } |
|
|
| return $dimensions; |
| } |
|
|
| private function getTracking(string $scope): LogTable |
| { |
| return new LogTable($scope); |
| } |
|
|
| private function getConfiguration(): Configuration |
| { |
| return new Configuration(); |
| } |
| } |
|
|