Config.php
1 year ago
Factory.php
1 year ago
Manager.php
1 year ago
Request.php
1 year ago
RequestConfig.php
1 year ago
Request.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\ViewDataTable; |
| 10 | |
| 11 | use Piwik\API\Request as ApiRequest; |
| 12 | use Piwik\Common; |
| 13 | class Request |
| 14 | { |
| 15 | /** |
| 16 | * @var null|\Piwik\ViewDataTable\RequestConfig |
| 17 | */ |
| 18 | public $requestConfig; |
| 19 | public function __construct($requestConfig) |
| 20 | { |
| 21 | $this->requestConfig = $requestConfig; |
| 22 | } |
| 23 | /** |
| 24 | * Function called by the ViewDataTable objects in order to fetch data from the API. |
| 25 | * The function init() must have been called before, so that the object knows which API module and action to call. |
| 26 | * It builds the API request string and uses Request to call the API. |
| 27 | * The requested DataTable object is stored in $this->dataTable. |
| 28 | * |
| 29 | * @param array $forcedParams Optional parameters which will be used to overwrite the request parameters |
| 30 | */ |
| 31 | public function loadDataTableFromAPI($forcedParams = []) |
| 32 | { |
| 33 | // we build the request (URL) to call the API |
| 34 | $requestArray = $this->getRequestArray(); |
| 35 | $requestArray = array_merge($requestArray, $forcedParams); |
| 36 | // we make the request to the API |
| 37 | $request = new ApiRequest($requestArray); |
| 38 | // and get the DataTable structure |
| 39 | $dataTable = $request->process(); |
| 40 | return $dataTable; |
| 41 | } |
| 42 | /** |
| 43 | * @return array URL to call the API, eg. "method=Referrers.getKeywords&period=day&date=yesterday"... |
| 44 | */ |
| 45 | public function getRequestArray() |
| 46 | { |
| 47 | // we prepare the array to give to the API Request |
| 48 | // we setup the method and format variable |
| 49 | // - we request the method to call to get this specific DataTable |
| 50 | // - the format = original specifies that we want to get the original DataTable structure itself, not rendered |
| 51 | $requestArray = array('method' => $this->requestConfig->apiMethodToRequestDataTable, 'format' => 'original'); |
| 52 | $toSetEventually = array_merge(array('filter_limit', 'keep_totals_row', 'keep_summary_row', 'filter_sort_column', 'filter_sort_order', 'filter_excludelowpop', 'filter_excludelowpop_value', 'filter_column', 'filter_pattern', 'flat', 'totals', 'expanded', 'pivotBy', 'pivotByColumn', 'pivotByColumnLimit'), $this->requestConfig->getExtraParametersToSet()); |
| 53 | foreach ($toSetEventually as $varToSet) { |
| 54 | $value = $this->getDefaultOrCurrent($varToSet); |
| 55 | if (\false !== $value) { |
| 56 | $requestArray[$varToSet] = $value; |
| 57 | } |
| 58 | } |
| 59 | $segment = ApiRequest::getRawSegmentFromRequest(); |
| 60 | if (!empty($segment)) { |
| 61 | $requestArray['segment'] = $segment; |
| 62 | } |
| 63 | if (ApiRequest::shouldLoadExpanded()) { |
| 64 | $requestArray['expanded'] = 1; |
| 65 | } |
| 66 | $requestArray = array_merge($requestArray, $this->requestConfig->request_parameters_to_modify); |
| 67 | if (!empty($requestArray['filter_limit']) && $requestArray['filter_limit'] === 0) { |
| 68 | unset($requestArray['filter_limit']); |
| 69 | } |
| 70 | if ($this->requestConfig->disable_generic_filters) { |
| 71 | $requestArray['disable_generic_filters'] = '1'; |
| 72 | } |
| 73 | if ($this->requestConfig->disable_queued_filters) { |
| 74 | $requestArray['disable_queued_filters'] = 1; |
| 75 | } |
| 76 | if (!empty($requestArray['compareSegments'])) { |
| 77 | $requestArray['compareSegments'] = Common::unsanitizeInputValues($requestArray['compareSegments']); |
| 78 | } |
| 79 | return $requestArray; |
| 80 | } |
| 81 | /** |
| 82 | * Returns, for a given parameter, the value of this parameter in the REQUEST array. |
| 83 | * If not set, returns the default value for this parameter @see getDefault() |
| 84 | * |
| 85 | * @param string $nameVar |
| 86 | * @return string|mixed Value of this parameter |
| 87 | */ |
| 88 | protected function getDefaultOrCurrent($nameVar) |
| 89 | { |
| 90 | if (isset($_GET[$nameVar])) { |
| 91 | return Common::sanitizeInputValue($_GET[$nameVar]); |
| 92 | } |
| 93 | return $this->getDefault($nameVar); |
| 94 | } |
| 95 | /** |
| 96 | * Returns the default value for a given parameter. |
| 97 | * For example, these default values can be set using the disable* methods. |
| 98 | * |
| 99 | * @param string $nameVar |
| 100 | * @return mixed |
| 101 | */ |
| 102 | protected function getDefault($nameVar) |
| 103 | { |
| 104 | if (isset($this->requestConfig->{$nameVar})) { |
| 105 | return $this->requestConfig->{$nameVar}; |
| 106 | } |
| 107 | return \false; |
| 108 | } |
| 109 | } |
| 110 |