Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Request.php 4.1KB

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