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