CalendarComponent.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. App::uses('Component', 'Controller');
  3. /**
  4. * Calendar Component
  5. *
  6. * inspired by http://www.flipflops.org/2007/09/21/a-simple-php-calendar-function/
  7. *
  8. * @author Mark Scherer
  9. * @copyright 2012 Mark Scherer
  10. * @license http://opensource.org/licenses/mit-license.php MIT
  11. *
  12. */
  13. class CalendarComponent extends Component {
  14. public $Controller = null;
  15. public $monthList = [
  16. 'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'];
  17. public $dayList = [
  18. 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'
  19. ];
  20. public $year = null;
  21. public $month = null;
  22. public $day = null;
  23. /**
  24. * Startup controller
  25. *
  26. * @param object $Controller Controller instance
  27. * @return void
  28. */
  29. public function startup(Controller $Controller) {
  30. $this->Controller = $Controller;
  31. }
  32. /**
  33. * @return bool Success
  34. */
  35. public function ensureCalendarConsistency($year, $month, $span = 10) {
  36. if (!is_numeric($month)) {
  37. $monthKeys = array_keys($this->monthList, $month);
  38. $month = array_shift($monthKeys);
  39. if ($month === null) {
  40. $month = -1;
  41. }
  42. }
  43. if (!$year || !$month) {
  44. $year = date('Y');
  45. $month = date('n');
  46. $item = null;
  47. }
  48. $year = (int)$year;
  49. $month = (int)$month;
  50. $current = date('Y');
  51. if (empty($month) || $year < $current - $span || $year > $current + $span) {
  52. $this->Controller->Flash->error(__d('tools', 'invalid date'));
  53. $this->Controller->redirect(['action' => 'index']);
  54. }
  55. $this->year = $year;
  56. $this->month = $month;
  57. if (empty($this->Controller->request->params['pass'])) {
  58. return true;
  59. }
  60. if ($month < 1 || $month > 12) {
  61. $this->Controller->Flash->error(__d('tools', 'invalid date'));
  62. $this->Controller->redirect(['action' => 'index']);
  63. }
  64. return true;
  65. }
  66. public function year() {
  67. return $this->year;
  68. }
  69. public function month($asString = false) {
  70. return $this->month;
  71. }
  72. /**
  73. * Month as integer value 1..12 or 0 on error
  74. * february => 2
  75. */
  76. public function retrieveMonth($string) {
  77. if (empty($string)) {
  78. return 0;
  79. }
  80. $string = mb_strtolower($string);
  81. if (in_array($string, $this->monthList)) {
  82. $keys = array_keys($this->monthList, $string);
  83. return $keys[0] + 1;
  84. }
  85. return 0;
  86. }
  87. /**
  88. * Day as integer value 1..31 or 0 on error
  89. * february => 2
  90. */
  91. public function retrieveDay($string, $month = null) {
  92. if (empty($string)) {
  93. return 0;
  94. }
  95. $string = (int)$string;
  96. if ($string < 1 || $string > 31) {
  97. return 0;
  98. }
  99. // check on month days!
  100. return $string;
  101. return 0;
  102. }
  103. public function months() {
  104. return $this->monthList;
  105. }
  106. public function days() {
  107. return $this->dayList;
  108. }
  109. /**
  110. * Converts integer to x-digit string
  111. * 1 => 01, 12 => 12
  112. */
  113. public function asString($number, $digits = 2) {
  114. $number = (string)$number;
  115. $count = mb_strlen($number);
  116. while ($count < $digits) {
  117. $number = '0' . $number;
  118. $count++;
  119. }
  120. return $number;
  121. }
  122. }