PageRenderTime 219ms CodeModel.GetById 132ms RepoModel.GetById 0ms app.codeStats 0ms

/core/ViewDataTable/Request.php

https://github.com/CodeYellowBV/piwik
PHP | 139 lines | 78 code | 21 blank | 40 comment | 7 complexity | 7ab66ed7d32cda8811cba5d47cdcfdd5 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  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. );
  68. foreach ($toSetEventually as $varToSet) {
  69. $value = $this->getDefaultOrCurrent($varToSet);
  70. if (false !== $value) {
  71. $requestArray[$varToSet] = $value;
  72. }
  73. }
  74. $segment = ApiRequest::getRawSegmentFromRequest();
  75. if (!empty($segment)) {
  76. $requestArray['segment'] = $segment;
  77. }
  78. if (ApiRequest::shouldLoadExpanded()) {
  79. $requestArray['expanded'] = 1;
  80. }
  81. $requestArray = array_merge($requestArray, $this->requestConfig->request_parameters_to_modify);
  82. if (!empty($requestArray['filter_limit'])
  83. && $requestArray['filter_limit'] === 0
  84. ) {
  85. unset($requestArray['filter_limit']);
  86. }
  87. return $requestArray;
  88. }
  89. /**
  90. * Returns, for a given parameter, the value of this parameter in the REQUEST array.
  91. * If not set, returns the default value for this parameter @see getDefault()
  92. *
  93. * @param string $nameVar
  94. * @return string|mixed Value of this parameter
  95. */
  96. protected function getDefaultOrCurrent($nameVar)
  97. {
  98. if (isset($_GET[$nameVar])) {
  99. return Common::sanitizeInputValue($_GET[$nameVar]);
  100. }
  101. $default = $this->getDefault($nameVar);
  102. return $default;
  103. }
  104. /**
  105. * Returns the default value for a given parameter.
  106. * For example, these default values can be set using the disable* methods.
  107. *
  108. * @param string $nameVar
  109. * @return mixed
  110. */
  111. protected function getDefault($nameVar)
  112. {
  113. if (isset($this->requestConfig->$nameVar)) {
  114. return $this->requestConfig->$nameVar;
  115. }
  116. return false;
  117. }
  118. }