1: <?php
2: 3: 4:
5: class Horde_Block_Sunrise extends Horde_Core_Block
6: {
7: 8:
9: public function __construct($app, $params = array())
10: {
11: parent::__construct($app, $params);
12:
13: $this->_name = _("Sunrise/Sunset");
14: }
15:
16: 17:
18: protected function _params()
19: {
20: $params = array(
21: 'location' => array(
22: 'type' => 'mlenum',
23: 'name' => _("Location"),
24: 'default' => '51.517:-0.117'
25: )
26: );
27:
28: global $coordinates;
29: if (!is_array($coordinates)) {
30: include 'Horde/Nls/Coordinates.php';
31: if (!is_array($coordinates)) {
32: $coordinates = array();
33: }
34: }
35: $params['location']['values'] = $coordinates;
36:
37: return $params;
38: }
39:
40: 41:
42: protected function _content()
43: {
44: if (empty($this->_params['location'])) {
45: throw new Horde_Exception(_("No location is set."));
46: }
47:
48:
49: $GLOBALS['registry']->setTimeZone();
50:
51: list($lat, $long) = explode(':', $this->_params['location']);
52: $rise = $this->_calculateSunset(time(), $lat, $long, false, floor(date('Z') / 3600));
53: $set = $this->_calculateSunset(time(), $lat, $long, true, floor(date('Z') / 3600));
54:
55: $location = '';
56: global $coordinates;
57: if (!is_array($coordinates)) {
58: require 'Horde/Nls/Coordinates.php';
59: }
60: foreach ($coordinates as $country) {
61: if (array_key_exists($this->_params['location'], $country)) {
62: $location = $country[$this->_params['location']];
63: break;
64: }
65: }
66:
67: return '<table width="100%" height="100%" cellspacing="0"><tr>' .
68: '<td colspan="2" class="control"><strong>' . $location . '</strong></td></tr><tr height="100%">' .
69: '<td width="50%" align="center">' .
70: Horde::img('block/sunrise/sunrise.png', _("Sun Rise")) .
71: '<br />' . $rise . '</td>' .
72: '<td width="50%" align="center">' .
73: Horde::img('block/sunrise/sunset.png', _("Sun Set")) .
74: '<br />' . $set . '</td>' . '</tr></table>';
75: }
76:
77: 78: 79:
80: private function _calculateSunset($date, $latitude, $longitude, $sunset = true, $timezone)
81: {
82: $yday = date('z', $date);
83: $mon = date('n', $date);
84: $mday = date('j', $date);
85: $year = date('Y', $date);
86:
87: if ($timezone == '13') {
88: $timezone = '-11';
89: $mday++;
90: $yday++;
91: }
92:
93: $A = 1.5708;
94: $B = 3.14159;
95: $C = 4.71239;
96: $D = 6.28319;
97: $E = 0.0174533 * $latitude;
98: $F = 0.0174533 * $longitude;
99: $G = 0.261799 * $timezone;
100:
101:
102:
103:
104:
105: $R = -.0145439;
106:
107: if ($sunset) {
108: $J = $C;
109: } else {
110: $J = $A;
111: }
112:
113: $K = $yday + (($J - $F) / $D);
114: $L = ($K * .017202) - .0574039;
115: $M = $L + .0334405 * sin($L);
116: $M += 4.93289 + (3.49066E-04) * sin(2 * $L);
117: while ($M < 0) {
118: $M = ($M + $D);
119: }
120: while ($M >= $D) {
121: $M = ($M - $D);
122: }
123:
124: if (($M / $A) - intval($M / $A) == 0) {
125: $M += 4.84814E-06;
126: }
127:
128: $P = sin($M) / cos($M);
129: $P = atan2(.91746 * $P, 1);
130:
131:
132: if ($M > $C) {
133: $P += $D;
134: } elseif ($M > $A) {
135: $P += $B;
136: }
137:
138: $Q = .39782 * sin($M);
139: $Q = $Q / sqrt(-$Q * $Q + 1);
140: $Q = atan2($Q, 1);
141:
142: $S = $R - (sin($Q) * sin($E));
143: $S = $S / (cos($Q) * cos($E));
144:
145: if (abs($S) > 1) {
146: return 'none';
147: }
148:
149: $S = $S / sqrt(-$S * $S + 1);
150: $S = $A - atan2($S, 1);
151:
152: if (!$sunset) {
153: $S = $D - $S ;
154: }
155:
156: $T = $S + $P - 0.0172028 * $K - 1.73364;
157: $U = $T - $F;
158: $V = $U + $G;
159:
160:
161: while ($V < 0) {
162: $V = ($V + $D);
163: }
164: while ($V >= $D) {
165: $V = ($V - $D);
166: }
167: $V = $V * 3.81972;
168:
169: $hour = intval($V);
170: $V -= $hour;
171: $min = intval($V * 60);
172: $V -= $min / 60;
173: $sec = intval($V * 3600);
174:
175: return strftime('%X', mktime($hour, $min, $sec, $mon, $mday, $year));
176: }
177:
178: }
179: