| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\API; |
|
|
| use Exception; |
| use Piwik\Archive\DataTableFactory; |
| use Piwik\Container\StaticContainer; |
| use Piwik\DataTable\Row; |
| use Piwik\DataTable; |
| use Piwik\Period\Range; |
| use Piwik\Plugins\API\API; |
| use Piwik\Url; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| abstract class DataTableManipulator |
| { |
| protected $apiModule; |
| protected $apiMethod; |
| protected $request; |
| protected $apiMethodForSubtable; |
|
|
| |
| |
| |
| |
| |
| public function __construct($apiModule = false, $apiMethod = false, $request = array()) |
| { |
| $this->apiModule = $apiModule; |
| $this->apiMethod = $apiMethod; |
| $this->request = $request; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected function manipulate($dataTable) |
| { |
| if ($dataTable instanceof DataTable\Map) { |
| return $this->manipulateDataTableMap($dataTable); |
| } elseif ($dataTable instanceof DataTable) { |
| return $this->manipulateDataTable($dataTable); |
| } else { |
| return $dataTable; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| protected function manipulateDataTableMap($dataTable) |
| { |
| $result = $dataTable->getEmptyClone(); |
| foreach ($dataTable->getDataTables() as $tableLabel => $childTable) { |
| $newTable = $this->manipulate($childTable); |
| $result->addTable($newTable, $tableLabel); |
| } |
| return $result; |
| } |
|
|
| |
| |
| |
| |
| abstract protected function manipulateDataTable($dataTable); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected function loadSubtable($dataTable, $row) |
| { |
| if (!($this->apiModule && $this->apiMethod && count($this->request))) { |
| return null; |
| } |
|
|
| $request = $this->request; |
|
|
| $idSubTable = $row->getIdSubDataTable(); |
| if ($idSubTable === null) { |
| return null; |
| } |
|
|
| $request['idSubtable'] = $idSubTable; |
| if ($dataTable) { |
| $period = $dataTable->getMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX); |
| if ($period instanceof Range) { |
| $request['date'] = $period->getDateStart() . ',' . $period->getDateEnd(); |
| } else { |
| $request['date'] = $period->getDateStart()->toString(); |
| } |
| } |
|
|
| $method = $this->getApiMethodForSubtable($request); |
| return $this->callApiAndReturnDataTable($this->apiModule, $method, $request); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| abstract protected function manipulateSubtableRequest($request); |
|
|
| |
| |
| |
| |
| |
| |
| protected function getApiMethodForSubtable($request) |
| { |
| if (!$this->apiMethodForSubtable) { |
| if (!empty($request['idSite'])) { |
| $idSite = $request['idSite']; |
| } else { |
| $idSite = 'all'; |
| } |
|
|
| $apiParameters = array(); |
| $entityNames = StaticContainer::get('entities.idNames'); |
| foreach ($entityNames as $idName) { |
| if (!empty($request[$idName])) { |
| $apiParameters[$idName] = $request[$idName]; |
| } |
| } |
|
|
| $meta = API::getInstance()->getMetadata($idSite, $this->apiModule, $this->apiMethod, $apiParameters); |
|
|
| if (empty($meta) && array_key_exists('idGoal', $apiParameters)) { |
| unset($apiParameters['idGoal']); |
| $meta = API::getInstance()->getMetadata($idSite, $this->apiModule, $this->apiMethod, $apiParameters); |
| } |
|
|
| if (empty($meta)) { |
| throw new Exception(sprintf( |
| "The DataTable cannot be manipulated: Metadata for report %s.%s could not be found. You can define the metadata in a hook, see example at: %s", |
| $this->apiModule, |
| $this->apiMethod, |
| Url::addCampaignParametersToMatomoLink('https://developer.matomo.org/api-reference/events#apigetreportmetadata') |
| )); |
| } |
|
|
| if (isset($meta[0]['actionToLoadSubTables'])) { |
| $this->apiMethodForSubtable = $meta[0]['actionToLoadSubTables']; |
| } else { |
| $this->apiMethodForSubtable = $this->apiMethod; |
| } |
| } |
| return $this->apiMethodForSubtable; |
| } |
|
|
| protected function callApiAndReturnDataTable($apiModule, $method, $request) |
| { |
| $class = Request::getClassNameAPI($apiModule); |
|
|
| $request = $this->manipulateSubtableRequest($request); |
| $request['serialize'] = 0; |
| $request['expanded'] = 0; |
| $request['format'] = 'original'; |
| $request['format_metrics'] = 0; |
| $request['compare'] = 0; |
|
|
| |
| |
| |
| unset($request['filter_pattern_recursive']); |
|
|
| $dataTable = Proxy::getInstance()->call($class, $method, $request); |
| $response = new ResponseBuilder($format = 'original', $request); |
| $response->disableSendHeader(); |
| $dataTable = $response->getResponse($dataTable, $apiModule, $method); |
|
|
| |
| if ($dataTable instanceof DataTable\DataTableInterface) { |
| $dataTable->filter(function (DataTable $table) use ($apiModule, $method) { |
| $table->setMetadata('apiModule', $apiModule); |
| $table->setMetadata('apiMethod', $method); |
| }); |
| } |
|
|
| return $dataTable; |
| } |
| } |
|
|