Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RequestConfig.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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\Common;
  11. /**
  12. * Contains base request properties for {@link Piwik\Plugin\ViewDataTable} instances. Manipulating
  13. * these properties will change the way a {@link Piwik\Plugin\ViewDataTable} loads report data.
  14. *
  15. * <a name="client-side-parameters-desc"></a>
  16. * **Client Side Parameters**
  17. *
  18. * Client side parameters are request properties that should be passed on to the browser so
  19. * client side JavaScript can use them. These properties will also be passed to the server with
  20. * every AJAX request made.
  21. *
  22. * Only affects ViewDataTables that output HTML.
  23. *
  24. * <a name="overridable-properties-desc"></a>
  25. * **Overridable Properties**
  26. *
  27. * Overridable properties are properties that can be set via the query string.
  28. * If a request has a query parameter that matches an overridable property, the property
  29. * will be set to the query parameter value.
  30. *
  31. * **Reusing base properties**
  32. *
  33. * Many of the properties in this class only have meaning for the {@link Piwik\Plugin\Visualization}
  34. * class, but can be set for other visualizations that extend {@link Piwik\Plugin\ViewDataTable}
  35. * directly.
  36. *
  37. * Visualizations that extend {@link Piwik\Plugin\ViewDataTable} directly and want to re-use these
  38. * properties must make sure the properties are used in the exact same way they are used in
  39. * {@link Piwik\Plugin\Visualization}.
  40. *
  41. * **Defining new request properties**
  42. *
  43. * If you are creating your own visualization and want to add new request properties for
  44. * it, extend this class and add your properties as fields.
  45. *
  46. * Properties are marked as client side parameters by calling the
  47. * {@link addPropertiesThatShouldBeAvailableClientSide()} method.
  48. *
  49. * Properties are marked as overridable by calling the
  50. * {@link addPropertiesThatCanBeOverwrittenByQueryParams()} method.
  51. *
  52. * ### Example
  53. *
  54. * **Defining new request properties**
  55. *
  56. * class MyCustomVizRequestConfig extends RequestConfig
  57. * {
  58. * /**
  59. * * My custom property. It is overridable.
  60. * *\/
  61. * public $my_custom_property = false;
  62. *
  63. * /**
  64. * * Another custom property. It is available client side.
  65. * *\/
  66. * public $another_custom_property = true;
  67. *
  68. * public function __construct()
  69. * {
  70. * parent::__construct();
  71. *
  72. * $this->addPropertiesThatShouldBeAvailableClientSide(array('another_custom_property'));
  73. * $this->addPropertiesThatCanBeOverwrittenByQueryParams(array('my_custom_property'));
  74. * }
  75. * }
  76. *
  77. * @api
  78. */
  79. class RequestConfig
  80. {
  81. /**
  82. * The list of request parameters that are 'Client Side Parameters'.
  83. */
  84. public $clientSideParameters = array(
  85. 'filter_excludelowpop',
  86. 'filter_excludelowpop_value',
  87. 'filter_pattern',
  88. 'filter_column',
  89. 'filter_offset',
  90. 'flat',
  91. 'expanded',
  92. 'pivotBy',
  93. 'pivotByColumn',
  94. 'pivotByColumnLimit'
  95. );
  96. /**
  97. * The list of ViewDataTable properties that can be overriden by query parameters.
  98. */
  99. public $overridableProperties = array(
  100. 'filter_sort_column',
  101. 'filter_sort_order',
  102. 'filter_limit',
  103. 'filter_offset',
  104. 'filter_pattern',
  105. 'filter_column',
  106. 'filter_excludelowpop',
  107. 'filter_excludelowpop_value',
  108. 'disable_generic_filters',
  109. 'disable_queued_filters',
  110. 'flat',
  111. 'expanded',
  112. 'pivotBy',
  113. 'pivotByColumn',
  114. 'pivotByColumnLimit'
  115. );
  116. /**
  117. * Controls which column to sort the DataTable by before truncating and displaying.
  118. *
  119. * Default value: If the report contains nb_uniq_visitors and nb_uniq_visitors is a
  120. * displayed column, then the default value is 'nb_uniq_visitors'.
  121. * Otherwise, it is 'nb_visits'.
  122. */
  123. public $filter_sort_column = false;
  124. /**
  125. * Controls the sort order. Either 'asc' or 'desc'.
  126. *
  127. * Default value: 'desc'
  128. */
  129. public $filter_sort_order = 'desc';
  130. /**
  131. * The number of items to truncate the data set to before rendering the DataTable view.
  132. *
  133. * Default value: false
  134. */
  135. public $filter_limit = false;
  136. /**
  137. * If set to true, the returned data will contain the flattened view of the table data set.
  138. * The children of all first level rows will be aggregated under one row.
  139. *
  140. * Default value: false
  141. */
  142. public $flat = false;
  143. /**
  144. * If set to true, the returned data will contain the first level results, as well as all sub-tables.
  145. *
  146. * Default value: false
  147. */
  148. public $expanded = false;
  149. /**
  150. * The number of items from the start of the data set that should be ignored.
  151. *
  152. * Default value: 0
  153. */
  154. public $filter_offset = 0;
  155. /**
  156. * A regex pattern to use to filter the DataTable before it is shown.
  157. *
  158. * @see also self::FILTER_PATTERN_COLUMN
  159. *
  160. * Default value: false
  161. */
  162. public $filter_pattern = false;
  163. /**
  164. * The column to apply a filter pattern to.
  165. *
  166. * @see also self::FILTER_PATTERN
  167. *
  168. * Default value: false
  169. */
  170. public $filter_column = false;
  171. /**
  172. * Stores the column name to filter when filtering out rows with low values.
  173. *
  174. * Default value: false
  175. */
  176. public $filter_excludelowpop = false;
  177. /**
  178. * Stores the value considered 'low' when filtering out rows w/ low values.
  179. *
  180. * Default value: false
  181. * @var \Closure|string
  182. */
  183. public $filter_excludelowpop_value = false;
  184. /**
  185. * An array property that contains query parameter name/value overrides for API requests made
  186. * by ViewDataTable.
  187. *
  188. * E.g. array('idSite' => ..., 'period' => 'month')
  189. *
  190. * Default value: array()
  191. */
  192. public $request_parameters_to_modify = array();
  193. /**
  194. * Whether to run generic filters on the DataTable before rendering or not.
  195. *
  196. * @see Piwik\API\DataTableGenericFilter
  197. *
  198. * Default value: false
  199. */
  200. public $disable_generic_filters = false;
  201. /**
  202. * Whether to run ViewDataTable's list of queued filters or not.
  203. *
  204. * _NOTE: Priority queued filters are always run._
  205. *
  206. * Default value: false
  207. */
  208. public $disable_queued_filters = false;
  209. /**
  210. * returns 'Plugin.apiMethodName' used for this ViewDataTable,
  211. * eg. 'Actions.getPageUrls'
  212. *
  213. * @var string
  214. */
  215. public $apiMethodToRequestDataTable = '';
  216. /**
  217. * If the current dataTable refers to a subDataTable (eg. keywordsBySearchEngineId for id=X) this variable is set to the Id
  218. *
  219. * @var bool|int
  220. */
  221. public $idSubtable = false;
  222. /**
  223. * Dimension ID to pivot by. See {@link Piwik\DataTable\Filter\PivotByDimension} for more info.
  224. *
  225. * @var string
  226. */
  227. public $pivotBy = false;
  228. /**
  229. * The column to display in a pivot table, eg, `'nb_visits'`. See {@link Piwik\DataTable\Filter\PivotByDimension}
  230. * for more info.
  231. *
  232. * @var string
  233. */
  234. public $pivotByColumn = false;
  235. /**
  236. * The maximum number of columns to display in a pivot table. See {@link Piwik\DataTable\Filter\PivotByDimension}
  237. * for more info.
  238. *
  239. * @var int
  240. */
  241. public $pivotByColumnLimit = false;
  242. public function getProperties()
  243. {
  244. return get_object_vars($this);
  245. }
  246. /**
  247. * Marks request properties as client side properties. [Read this](#client-side-properties-desc)
  248. * to learn more.
  249. *
  250. * @param array $propertyNames List of property names, eg, `array('disable_queued_filters', 'filter_column')`.
  251. */
  252. public function addPropertiesThatShouldBeAvailableClientSide(array $propertyNames)
  253. {
  254. foreach ($propertyNames as $propertyName) {
  255. $this->clientSideParameters[] = $propertyName;
  256. }
  257. }
  258. /**
  259. * Marks display properties as overridable. [Read this](#overridable-properties-desc) to
  260. * learn more.
  261. *
  262. * @param array $propertyNames List of property names, eg, `array('disable_queued_filters', 'filter_column')`.
  263. */
  264. public function addPropertiesThatCanBeOverwrittenByQueryParams(array $propertyNames)
  265. {
  266. foreach ($propertyNames as $propertyName) {
  267. $this->overridableProperties[] = $propertyName;
  268. }
  269. }
  270. public function setDefaultSort($columnsToDisplay, $hasNbUniqVisitors, $actualColumns)
  271. {
  272. // default sort order to visits/visitors data
  273. if ($hasNbUniqVisitors && in_array('nb_uniq_visitors', $columnsToDisplay)) {
  274. $this->filter_sort_column = 'nb_uniq_visitors';
  275. } else {
  276. $this->filter_sort_column = 'nb_visits';
  277. }
  278. // if the default sort column does not exist, sort by the first non-label column
  279. if (!in_array($this->filter_sort_column, $actualColumns)) {
  280. foreach ($actualColumns as $column) {
  281. if ($column != 'label') {
  282. $this->filter_sort_column = $column;
  283. break;
  284. }
  285. }
  286. }
  287. $this->filter_sort_order = 'desc';
  288. }
  289. /**
  290. * Returns `true` if queued filters have been disabled, `false` if otherwise.
  291. *
  292. * @return bool
  293. */
  294. public function areQueuedFiltersDisabled()
  295. {
  296. return isset($this->disable_queued_filters) && $this->disable_queued_filters;
  297. }
  298. /**
  299. * Returns `true` if generic filters have been disabled, `false` if otherwise.
  300. *
  301. * @return bool
  302. */
  303. public function areGenericFiltersDisabled()
  304. {
  305. // if disable_generic_filters query param is set to '1', generic filters are disabled
  306. if (Common::getRequestVar('disable_generic_filters', '0', 'string') == 1) {
  307. return true;
  308. }
  309. if (isset($this->disable_generic_filters) && true === $this->disable_generic_filters) {
  310. return true;
  311. }
  312. return false;
  313. }
  314. public function getApiModuleToRequest()
  315. {
  316. list($module, $method) = explode('.', $this->apiMethodToRequestDataTable);
  317. return $module;
  318. }
  319. public function getApiMethodToRequest()
  320. {
  321. list($module, $method) = explode('.', $this->apiMethodToRequestDataTable);
  322. return $method;
  323. }
  324. }