PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/core/DataTable/Filter/BeautifyTimeRangeLabels.php

https://github.com/CodeYellowBV/piwik
PHP | 121 lines | 38 code | 7 blank | 76 comment | 9 complexity | f6b22f97d677b38e0e23f43a8ddcbdf8 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\DataTable\Filter;
  10. use Piwik\DataTable;
  11. /**
  12. * A {@link DataTable} filter that replaces range labels whose values are in seconds with
  13. * prettier, human-friendlier versions.
  14. *
  15. * This filter customizes the behavior of the {@link BeautifyRangeLabels} filter
  16. * so range values that are less than one minute are displayed in seconds but
  17. * other ranges are displayed in minutes.
  18. *
  19. * **Basic usage**
  20. *
  21. * $dataTable->filter('BeautifyTimeRangeLabels', array("%1$s-%2$s min", "1 min", "%s min"));
  22. *
  23. * @api
  24. */
  25. class BeautifyTimeRangeLabels extends BeautifyRangeLabels
  26. {
  27. /**
  28. * A format string used to create pretty range labels when the range's
  29. * lower bound is between 0 and 60.
  30. *
  31. * This format string must take two numeric parameters, one for each
  32. * range bound.
  33. */
  34. protected $labelSecondsPlural;
  35. /**
  36. * Constructor.
  37. *
  38. * @param DataTable $table The DataTable this filter will run over.
  39. * @param string $labelSecondsPlural A string to use when beautifying range labels
  40. * whose lower bound is between 0 and 60. Must be
  41. * a format string that takes two numeric params.
  42. * @param string $labelMinutesSingular A string to use when replacing a range that
  43. * equals 60-60 (or 1 minute - 1 minute).
  44. * @param string $labelMinutesPlural A string to use when replacing a range that
  45. * spans multiple minutes. This must be a
  46. * format string that takes one string parameter.
  47. */
  48. public function __construct($table, $labelSecondsPlural, $labelMinutesSingular, $labelMinutesPlural)
  49. {
  50. parent::__construct($table, $labelMinutesSingular, $labelMinutesPlural);
  51. $this->labelSecondsPlural = $labelSecondsPlural;
  52. }
  53. /**
  54. * Beautifies and returns a range label whose range spans over one unit, ie
  55. * 1-1, 2-2 or 3-3.
  56. *
  57. * If the lower bound of the range is less than 60 the pretty range label
  58. * will be in seconds. Otherwise, it will be in minutes.
  59. *
  60. * @param string $oldLabel The original label value.
  61. * @param int $lowerBound The lower bound of the range.
  62. * @return string The pretty range label.
  63. */
  64. public function getSingleUnitLabel($oldLabel, $lowerBound)
  65. {
  66. if ($lowerBound < 60) {
  67. return sprintf($this->labelSecondsPlural, $lowerBound, $lowerBound);
  68. } else if ($lowerBound == 60) {
  69. return $this->labelSingular;
  70. } else {
  71. return sprintf($this->labelPlural, ceil($lowerBound / 60));
  72. }
  73. }
  74. /**
  75. * Beautifies and returns a range label whose range is bounded and spans over
  76. * more than one unit, ie 1-5, 5-10 but NOT 11+.
  77. *
  78. * If the lower bound of the range is less than 60 the pretty range label
  79. * will be in seconds. Otherwise, it will be in minutes.
  80. *
  81. * @param string $oldLabel The original label value.
  82. * @param int $lowerBound The lower bound of the range.
  83. * @param int $upperBound The upper bound of the range.
  84. * @return string The pretty range label.
  85. */
  86. public function getRangeLabel($oldLabel, $lowerBound, $upperBound)
  87. {
  88. if ($lowerBound < 60) {
  89. return sprintf($this->labelSecondsPlural, $lowerBound, $upperBound);
  90. } else {
  91. return sprintf($this->labelPlural, ceil($lowerBound / 60) . "-" . ceil($upperBound / 60));
  92. }
  93. }
  94. /**
  95. * Beautifies and returns a range label whose range is unbounded, ie
  96. * 5+, 10+, etc.
  97. *
  98. * If the lower bound of the range is less than 60 the pretty range label
  99. * will be in seconds. Otherwise, it will be in minutes.
  100. *
  101. * @param string $oldLabel The original label value.
  102. * @param int $lowerBound The lower bound of the range.
  103. * @return string The pretty range label.
  104. */
  105. public function getUnboundedLabel($oldLabel, $lowerBound)
  106. {
  107. if ($lowerBound < 60) {
  108. return sprintf($this->labelSecondsPlural, $lowerBound);
  109. } else {
  110. // since we're using minutes, we use floor so 1801s+ will be 30m+ and not 31m+
  111. return sprintf($this->labelPlural, "" . floor($lowerBound / 60) . urlencode('+'));
  112. }
  113. }
  114. }