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