CalendarComponent.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 MIT
  11. *
  12. * 2012-02-08 ms
  13. */
  14. class CalendarComponent extends Component {
  15. public $Controller = null;
  16. public $monthList = array(
  17. 'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
  18. public $dayList = array(
  19. 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'
  20. );
  21. public $year = null;
  22. public $month = null;
  23. public $day = null;
  24. /**
  25. * Startup controller
  26. *
  27. * @param object $Controller Controller instance
  28. * @access public
  29. * @return void
  30. */
  31. public function startup(Controller $Controller) {
  32. $this->Controller = $Controller;
  33. }
  34. /**
  35. * @return bool $success
  36. */
  37. public function ensureCalendarConsistency($year, $month, $span = 10) {
  38. if (!is_numeric($month)) {
  39. $monthKeys = array_keys($this->monthList, $month);
  40. $month = array_shift($monthKeys);
  41. if ($month === null) {
  42. $month = -1;
  43. }
  44. }
  45. if (!$year || !$month) {
  46. $year = date('Y');
  47. $month = date('n');
  48. $item = null;
  49. }
  50. $year = (int)$year;
  51. $month = (int)$month;
  52. $current = date('Y');
  53. if (empty($month) || $year < $current - $span || $year > $current + $span) {
  54. $this->Controller->Common->flashMessage(__('invalid date'), 'error');
  55. $this->Controller->redirect(array('action' => 'index'));
  56. }
  57. $this->year = $year;
  58. $this->month = $month;
  59. if (empty($this->Controller->request->params['pass'])) {
  60. return true;
  61. }
  62. if ($month < 1 || $month > 12) {
  63. $this->Controller->Common->flashMessage(__('invalid date'), 'error');
  64. $this->Controller->redirect(array('action' => 'index'));
  65. }
  66. return true;
  67. }
  68. public function year() {
  69. return $this->year;
  70. }
  71. public function month($asString = false) {
  72. return $this->month;
  73. }
  74. /**
  75. * month as integer value 1..12 or 0 on error
  76. * february => 2
  77. * 2009-12-26 ms
  78. */
  79. public function retrieveMonth($string) {
  80. if (empty($string)) {
  81. return 0;
  82. }
  83. $string = mb_strtolower($string);
  84. if (in_array($string, $this->monthList)) {
  85. $keys = array_keys($this->monthList, $string);
  86. return $keys[0] + 1;
  87. }
  88. return 0;
  89. }
  90. /**
  91. * day as integer value 1..31 or 0 on error
  92. * february => 2
  93. * 2009-12-26 ms
  94. */
  95. public function retrieveDay($string, $month = null) {
  96. if (empty($string)) {
  97. return 0;
  98. }
  99. $string = (int)$string;
  100. if ($string < 1 || $string > 31) {
  101. return 0;
  102. }
  103. # check on month days!
  104. return $string;
  105. return 0;
  106. }
  107. public function months() {
  108. return $this->monthList;
  109. }
  110. public function days() {
  111. return $this->dayList;
  112. }
  113. /**
  114. * converts integer to x-digit string
  115. * 1 => 01, 12 => 12
  116. * 2009-12-26 ms
  117. */
  118. public function asString($number, $digits = 2) {
  119. $number = (string )$number;
  120. $count = mb_strlen($number);
  121. while ($count < $digits) {
  122. $number = '0' . $number;
  123. $count++;
  124. }
  125. return $number;
  126. }
  127. }