/plugins/calendar/branches/0.6-0.x/calendar.plugin.php

https://github.com/somefool/habari-extras · PHP · 198 lines · 127 code · 16 blank · 55 comment · 32 complexity · 308c8d2d60d0bfb8ec5587bbdf919f41 MD5 · raw file

  1. <?php
  2. /**
  3. * Calendar
  4. *
  5. * @package calendar
  6. * @version $Id$
  7. * @author ayunyan <ayu@commun.jp>
  8. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  9. * @link http://ayu.commun.jp/habari-calendar
  10. */
  11. class Calendar extends Plugin
  12. {
  13. private $week_starts = array(6 => 'Saturday', 0 => 'Sunday', 1 => 'Monday');
  14. /**
  15. * plugin information
  16. *
  17. * @access public
  18. * @retrun void
  19. */
  20. public function info()
  21. {
  22. return array(
  23. 'name' => 'Calendar',
  24. 'version' => '0.01-alpha',
  25. 'url' => 'http://ayu.commun.jp/habari-calendar',
  26. 'author' => 'ayunyan',
  27. 'authorurl' => 'http://ayu.commun.jp/',
  28. 'license' => 'Apache License 2.0',
  29. 'description' => 'Displays calendar on your blog.',
  30. 'guid' => '4f97ad6b-c0a5-11dd-aff6-001b210f913f'
  31. );
  32. }
  33. /**
  34. * action: plugin_activation
  35. *
  36. * @access public
  37. * @param string $file
  38. * @return void
  39. */
  40. public function action_plugin_activation($file)
  41. {
  42. if (Plugins::id_from_file($file) != Plugins::id_from_file(__FILE__)) return;
  43. Options::set('calendar__week_start', 0);
  44. }
  45. /**
  46. * action: init
  47. *
  48. * @access public
  49. * @return void
  50. */
  51. public function action_init()
  52. {
  53. $this->load_text_domain('calendar');
  54. $this->add_template('calendar', dirname(__FILE__) . '/templates/calendar.php');
  55. }
  56. /**
  57. * action: update_check
  58. *
  59. * @access public
  60. * @return void
  61. */
  62. public function action_update_check()
  63. {
  64. Update::add($this->info->name, $this->info->guid, $this->info->version);
  65. }
  66. /**
  67. * action: plugin_ui
  68. *
  69. * @access public
  70. * @param string $plugin_id
  71. * @param string $action
  72. * @return void
  73. */
  74. public function action_plugin_ui($plugin_id, $action)
  75. {
  76. if ($plugin_id != $this->plugin_id()) return;
  77. if ($action == _t('Configure')) {
  78. $form = new FormUI(strtolower(get_class($this)));
  79. $form->append('select', 'week_start', 'calendar__week_start', _t('Week starts on: ', 'calendar'), $this->week_starts);
  80. $form->append('submit', 'save', _t('Save'));
  81. $form->out();
  82. }
  83. }
  84. /**
  85. * filter: plugin_config
  86. *
  87. * @access public
  88. * @return array
  89. */
  90. public function filter_plugin_config($actions, $plugin_id)
  91. {
  92. if ($plugin_id == $this->plugin_id()) {
  93. $actions[] = _t('Configure');
  94. }
  95. return $actions;
  96. }
  97. /**
  98. * theme: show_calendar
  99. *
  100. * @access public
  101. * @param object $theme
  102. * @return string
  103. */
  104. public function theme_show_calendar($theme)
  105. {
  106. $handler_vars = Controller::get_handler_vars();
  107. if (Controller::get_action() == 'display_date' && isset($handler_vars['month'])) {
  108. $year = (int)$handler_vars['year'];
  109. $month = (int)$handler_vars['month'];
  110. } else {
  111. $year = date('Y');
  112. $month = date('m');
  113. }
  114. $next_year = $year;
  115. $prev_year = $year;
  116. $next_month = $month + 1;
  117. $prev_month = $month - 1;
  118. if ($next_month == 13) {
  119. $next_month = 1;
  120. $next_year++;
  121. }
  122. if ($prev_month == 0) {
  123. $prev_month = 12;
  124. $prev_year--;
  125. }
  126. $start_time = mktime(0, 0, 0, $month, 1, $year);
  127. $end_time = mktime(0, 0, 0, $next_month, 1, $next_year);
  128. $t_posts_day = DB::get_column('SELECT pubdate FROM {posts} WHERE pubdate >= ? AND pubdate < ? AND status = ?', array($start_time, $end_time, Post::status('published')));
  129. $posts_day = array();
  130. @reset($t_posts_day);
  131. while (list(, $pubdate) = @each($t_posts_day)) {
  132. $posts_day[] = (int)date("j", $pubdate);
  133. }
  134. $month_start_week = date("w", $start_time);
  135. $month_days = date("t", $start_time);
  136. $week_days = array(_t('Sun', 'calendar'), _t('Mon', 'calendar'), _t('Tue', 'calendar'), _t('Wed', 'calendar'), _t('Thu', 'calendar'), _t('Fri', 'calendar'), _t('Sat', 'calendar'));
  137. $week_start = Options::get('calendar__week_start');
  138. $calendar = array();
  139. $day = 0;
  140. while (1) {
  141. $week = array();
  142. for ($i = 0; $i < 7; $i++) {
  143. $wday = ($i + $week_start) % 7;
  144. if (count($calendar) == 0) {
  145. $week[] = array('label' => $week_days[($i + $week_start) % 7]);
  146. continue;
  147. } elseif ($day == 0) {
  148. if ($wday == $month_start_week) {
  149. $day = 1;
  150. if (array_search($day, $posts_day) !== false) {
  151. $week[] = array('label' => $day, 'url' => URL::get('display_entries_by_date', array('year' => $year, 'month' => sprintf('%02d', $month), 'day' => sprintf('%02d', $day))));
  152. } else {
  153. $week[] = array('label' => $day);
  154. }
  155. } else {
  156. $week[] = array('label' => null);
  157. continue;
  158. }
  159. } elseif ($day >= $month_days) {
  160. $week[] = array('label' => null);
  161. continue;
  162. } else {
  163. $day++;
  164. if (array_search($day, $posts_day) !== false) {
  165. $week[] = array('label' => $day, 'url' => URL::get('display_entries_by_date', array('year' => $year, 'month' => sprintf('%02d', $month), 'day' => sprintf('%02d', $day))));
  166. } else {
  167. $week[] = array('label' => $day);
  168. }
  169. }
  170. }
  171. $calendar[] = $week;
  172. if ($day == $month_days) break;
  173. }
  174. $theme->year = $year;
  175. $theme->month = $month;
  176. $theme->prev_month_url = URL::get('display_entries_by_date', array('year' => $prev_year, 'month' => sprintf('%02d', $prev_month)));
  177. $theme->next_month_url = URL::get('display_entries_by_date', array('year' => $next_year, 'month' => sprintf('%02d', $next_month)));
  178. $theme->calendar = $calendar;
  179. return $theme->fetch('calendar');
  180. }
  181. }
  182. ?>