BxDolCalendar.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. /**
  9. * Calendar for your module content
  10. *
  11. * Related classes:
  12. * BxBaseCalendar - calendar base representation
  13. * BxTemplCalendar - custom template representation
  14. *
  15. *
  16. *
  17. * To add calendar to your content you need to inherit this class and override the following methods:
  18. * \code
  19. * getEntriesNames ()
  20. * getData ()
  21. * getBaseUri ()
  22. * getBrowseUri ()
  23. * \endcode
  24. *
  25. * see each function documentation for more information.
  26. *
  27. *
  28. *
  29. * Example of usage:
  30. * After overriding this class and adding necessary methods use thje following code to disp;lay calendar:
  31. * This code is called from module class:
  32. *
  33. * \code
  34. * function actionCalendar ($iYear = '', $iMonth = '') {
  35. * bx_import ('Calendar', $this->_aModule);
  36. * $oCalendar = bx_instance ($this->_aModule['class_prefix'] . 'Calendar', array ($iYear, $iMonth, $this->_oDb, $this->_oTemplate, $this->_oConfig));
  37. * echo $oCalendar->display();
  38. * }
  39. * \endcode
  40. *
  41. *
  42. *
  43. * Memberships/ACL:
  44. * vote - ACTION_ID_VOTE
  45. *
  46. *
  47. *
  48. * Alerts:
  49. * this class don't risa any alert
  50. *
  51. */
  52. class BxDolCalendar extends BxDol {
  53. var $iYear, $iMonth, $iPrevYear, $iPrevMonth, $iNextYear, $iNextMonth;
  54. var $iFirstWeekDay, $iNumDaysInMonth, $sMonthName, $iWeekStart, $iWeekEnd;
  55. function BxDolCalendar ($iYear, $iMonth) {
  56. parent::BxDol();
  57. // input values
  58. $this->iYear = (int)$iYear ? (int)$iYear : date('Y');
  59. $this->iMonth = (int)$iMonth ? (int)$iMonth : date('m');
  60. $this->iDay = date('d');
  61. $this->sMonthName = strftime('%B', mktime(0, 0, 0, $this->iMonth+1, $iDay, $this->iYear));
  62. $this->iNumDaysInMonth = date('t', mktime(0, 0, 0, $this->iMonth+1, $iDay, $this->iYear));
  63. $this->iFirstWeekDay = (int)date('w', mktime(0, 0, 0, $this->iMonth, 1, $this->iYear));
  64. // previous month year, month
  65. $this->iPrevYear = $this->iYear;
  66. $this->iPrevMonth = $this->iMonth - 1;
  67. if ( $this->iPrevMonth <= 0 ) {
  68. $this->iPrevMonth = 12;
  69. $this->iPrevYear--;
  70. }
  71. // next month year, month
  72. $this->iNextYear = $this->iYear;
  73. $this->iNextMonth = $this->iMonth + 1;
  74. if ( $this->iNextMonth > 12 ) {
  75. $this->iNextMonth = 1;
  76. $this->iNextYear++;
  77. }
  78. // week config
  79. list ($this->iWeekStart, $this->iWeekEnd) = (getParam('sys_calendar_starts_sunday') == 'on') ? array (0, 7) : array (1, 8);
  80. if ($this->iFirstWeekDay < $this->iWeekStart)
  81. $this->iFirstWeekDay = $this->iWeekEnd-1;
  82. }
  83. function _getWeekNames ($isMiniMode = false)
  84. {
  85. $sPostfix = '';
  86. if ($isMiniMode)
  87. $sPostfix = '_mini';
  88. if(0 == $this->iWeekStart)
  89. $aWeek[] = array('name' => _t('_week_sun' . $sPostfix));
  90. $aWeek[] = array('name' => _t('_week_mon' . $sPostfix));
  91. $aWeek[] = array('name' => _t('_week_tue' . $sPostfix));
  92. $aWeek[] = array('name' => _t('_week_wed' . $sPostfix));
  93. $aWeek[] = array('name' => _t('_week_thu' . $sPostfix));
  94. $aWeek[] = array('name' => _t('_week_fri' . $sPostfix));
  95. $aWeek[] = array('name' => _t('_week_sat' . $sPostfix));
  96. if(8 == $this->iWeekEnd)
  97. $aWeek[] = array('name' => _t('_week_sun' . $sPostfix));
  98. return $aWeek;
  99. }
  100. function _getCalendarGrid (&$aCalendarGrid) {
  101. // fill calendar with events
  102. $aEvents = $this->getData();
  103. $aCalendar = array ();
  104. foreach ($aEvents as $a) {
  105. $aCalendar[$a['Day']] += 1;
  106. }
  107. // make calendar grid
  108. $aCalendarGrid = array ();
  109. $iCurrentDay = 0;
  110. for ($i = 0; $i < 6; $i++) {
  111. for ($j = $this->iWeekStart; $j < $this->iWeekEnd; $j++) {
  112. if ($this->iFirstWeekDay == $j || $i > 0 || $iCurrentDay > 0) {
  113. ++$iCurrentDay;
  114. if ($iCurrentDay > $this->iNumDaysInMonth) break 2;
  115. $aCalendarGrid[$i][$j]['day'] = $iCurrentDay;
  116. $aCalendarGrid[$i][$j]['today'] = ($this->iYear == date('Y') && $this->iMonth == date('m') && $iCurrentDay == $this->iDay);
  117. $aCalendarGrid[$i][$j]['num'] = isset($aCalendar[$iCurrentDay]) && $aCalendar[$iCurrentDay] > 0 ? $aCalendar[$iCurrentDay] : '';
  118. }
  119. }
  120. }
  121. }
  122. function _getCalendar () {
  123. $sBrowseUri = $this->getBrowseUri();
  124. list ($sEntriesSingle, $sEntriesMul) = $this->getEntriesNames ();
  125. $this->_getCalendarGrid($aCalendarGrid);
  126. $aRet = array ();
  127. for ($i = 0; $i < 6; $i++) {
  128. $aRow = array ('bx_repeat:cell');
  129. $isRowEmpty = true;
  130. for ($j = $this->iWeekStart; $j < $this->iWeekEnd; $j++) {
  131. $aCell = array ();
  132. if ($aCalendarGrid[$i][$j]['today']) {
  133. $aCell['class'] = 'sys_cal_cell sys_cal_today';
  134. $aCell['day'] = $aCalendarGrid[$i][$j]['day'];
  135. $aCell['bx_if:num'] = array ('condition' => $aCalendarGrid[$i][$j]['num'], 'content' => array(
  136. 'num' => $aCalendarGrid[$i][$j]['num'],
  137. 'href' => $sBrowseUri . $this->iYear . '/' . $this->iMonth . '/' . $aCell['day'],
  138. 'entries' => 1 == $aCalendarGrid[$i][$j]['num'] ? $sEntriesSingle : $sEntriesMul,
  139. ));
  140. $isRowEmpty = false;
  141. } elseif (isset($aCalendarGrid[$i][$j]['day'])) {
  142. $aCell['class'] = 'sys_cal_cell';
  143. $aCell['day'] = $aCalendarGrid[$i][$j]['day'];
  144. $aCell['bx_if:num'] = array ('condition' => $aCalendarGrid[$i][$j]['num'], 'content' => array(
  145. 'num' => $aCalendarGrid[$i][$j]['num'],
  146. 'href' => $sBrowseUri . $this->iYear . '/' . $this->iMonth . '/' . $aCell['day'],
  147. 'entries' => 1 == $aCalendarGrid[$i][$j]['num'] ? $sEntriesSingle : $sEntriesMul,
  148. ));
  149. $isRowEmpty = false;
  150. } else {
  151. $aCell['class'] = 'sys_cal_cell_blank';
  152. $aCell['day'] = '';
  153. $aCell['bx_if:num'] = array ('condition' => false, 'content' => array(
  154. 'num' => '',
  155. 'href' => '',
  156. 'entries' => '',
  157. ));
  158. }
  159. if ($aCell)
  160. $aRow['bx_repeat:cell'][] = $aCell;
  161. }
  162. if ($aRow['bx_repeat:cell'] && !$isRowEmpty) {
  163. $aRet[] = $aRow;
  164. }
  165. }
  166. return $aRet;
  167. }
  168. function getTitle () {
  169. return $this->sMonthName . ', ' . $this->iYear;
  170. }
  171. // override function below to implement your own calendar
  172. /**
  173. * return records for current month, there is mandatory field `Day` - a day for current row
  174. * use the following class variables to pass to your database query
  175. * $this->iYear, $this->iMonth, $this->iNextYear, $this->iNextMonth
  176. *
  177. * for example:
  178. * \code
  179. * return $db->getAll ("
  180. * SELECT *, DAYOFMONTH(FROM_UNIXTIME(`EventStart`)) AS `Day`
  181. * FROM `my_table`
  182. * WHERE `Date` >= UNIX_TIMESTAMP('{$this->iYear}-{$this->iMonth}-1') AND `Date` < UNIX_TIMESTAMP('{$this->iNextYear}-{$this->iNextMonth}-1') AND `Status` = 'approved'");
  183. * \endcode
  184. */
  185. function getData () {
  186. // override this func
  187. return array();
  188. }
  189. /**
  190. * deprecated
  191. *
  192. * return html for data unit for some day, it is:
  193. * - icon 32x32 with link if data have associated image, use $GLOBALS['oFunctions']->sysIcon() to return small icon
  194. * - data title with link if data have no associated image
  195. */
  196. function getUnit (&$aData) {
  197. // override this func
  198. return '';
  199. }
  200. /**
  201. * return base calendar url
  202. * year and month will be added to this url automatically
  203. * so if your base url is /m/some_module/calendar/, it will be transormed to
  204. * /m/some_module/calendar/YEAR/MONTH, like /m/some_module/calendar/2009/3
  205. */
  206. function getBaseUri () {
  207. // override this func
  208. return BX_DOL_URL_ROOT;
  209. }
  210. /**
  211. * return browse entries url
  212. * year and month and day will be added to this url automatically
  213. * so if your base url is /m/some_module/browse/calendar/, it will be transormed to
  214. * /m/some_module/browse/calendar/YEAR/MONTH/DAY, like /m/some_module/browse/calendar/2009/3/15
  215. */
  216. function getBrowseUri () {
  217. // override this func
  218. return '';
  219. }
  220. /**
  221. * return entries names in single and plural forms,
  222. * for example: ('event', 'events') or ('profile', 'profiles')
  223. */
  224. function getEntriesNames () {
  225. // override this func
  226. return array(_t('_item'), _t('_items'));
  227. }
  228. /**
  229. * return url for previous/next month
  230. */
  231. function getMonthUrl ($isNextMoths, $isMiniMode = false) {
  232. return $this->getBaseUri () . ($isNextMoths ? "{$this->iNextYear}/{$this->iNextMonth}" : "{$this->iPrevYear}/{$this->iPrevMonth}");
  233. }
  234. }