30 require_once SQ_FUDGE_PATH.
'/general/datetime.inc';
31 require_once SQ_FUDGE_PATH.
'/db_extras/db_extras.inc';
48 function getSingleEventQueryBase($root_ids, $event_type_code=
'calendar_event_single', $include_exceptions=TRUE, &$bind_vars=Array())
53 if ($include_exceptions) {
58 $sql =
'SELECT a.assetid, a.type_code, a.name, t.treeid, cd.*'.
"\n";
59 if (!$GLOBALS[
'SQ_SYSTEM']->userSystemAdmin() && !$GLOBALS[
'SQ_SYSTEM']->userRoot()) {
60 $sql .=
', p.granted as granted';
63 $sql .=
'WHERE a.type_code IN ( '.implode(
',', $type_codes).
') '.
"\n";
66 $sql .=
' AND '.Calendar_Common::_getTreeClause($root_ids);
69 if (!$GLOBALS[
'SQ_SYSTEM']->userSystemAdmin() && !$GLOBALS[
'SQ_SYSTEM']->userRoot()) {
70 $userids = $GLOBALS[
'SQ_SYSTEM']->am->getParents($GLOBALS[
'SQ_SYSTEM']->user->id,
'user_group', FALSE);
71 $userids[$GLOBALS[
'SQ_SYSTEM']->user->id] = 1;
72 $userids[$GLOBALS[
'SQ_SYSTEM']->am->getSystemAssetid(
'public_user')] = 1;
74 foreach ($userids as $userid => $type) {
75 $bind_vars[$userid] =
'user'.$user_count++;
77 $userid_cond =
' AND p.userid IN (:'.implode(
',:', $bind_vars).
')';
107 list($year,$month,$day) = sscanf(substr($iso,0,10),
'%4d-%2d-%2d');
108 if (!$month) $month = 12;
110 if (($view ==
'period') && ($length == 0)) {
115 $wdom_list = weekday_dates_in_month($now[
'wday'], $month, $year);
121 $sql =
'SELECT a.assetid, a.type_code, a.name, t.treeid, cd.*'.
"\n";
122 if (!$GLOBALS[
'SQ_SYSTEM']->userSystemAdmin() && !$GLOBALS[
'SQ_SYSTEM']->userRoot()) {
123 $sql .=
', p.granted as granted';
125 $sql .=
"\nFROM ".Calendar_Common::_getTableList().
" \nWHERE (a.type_code IN (".implode(
',', $type_codes).
")) \n\tAND (a.status >= ".
MatrixDAL::quote(SQ_STATUS_LIVE).
")\n";
126 $bind_vars = Array();
127 if (!$GLOBALS[
'SQ_SYSTEM']->userSystemAdmin() && !$GLOBALS[
'SQ_SYSTEM']->userRoot()) {
128 $userids = $GLOBALS[
'SQ_SYSTEM']->am->getParents($GLOBALS[
'SQ_SYSTEM']->user->id,
'user_group', FALSE);
129 $userids[$GLOBALS[
'SQ_SYSTEM']->user->id] = 1;
130 $userids[$GLOBALS[
'SQ_SYSTEM']->am->getSystemAssetid(
'public_user')] = 1;
132 foreach ($userids as $userid => $type) {
133 $bind_vars[$userid] =
'user'.$user_count++;
135 $userid_cond =
' AND p.userid IN (:'.implode(
',:', $bind_vars).
')';
140 $sql_freqs = Array();
144 $last_day = $now[
'ds'];
145 $now[
'month_length'] = days_in_month($month, $year);
146 $now[
'month_start_wday'] = date(
'w', strtotime(sprintf(
'%04d-%02d-%02d', $year, $month, 1)));
147 $now[
'month_end_wday'] = date(
'w', strtotime(sprintf(
'%04d-%02d-%02d', $year, $month, $now[
'month_length'])));
148 $prev_month_length = date(
't', strtotime(
'-1 month', $now[
'ts']));
149 $prev_month_start_wday = date(
'w', strtotime(sprintf(
'%04d-%02d-%02d', $year, $month, 1).
' -1 month'));
150 $prev_month_end_wday = date(
'w', strtotime(sprintf(
'%04d-%02d-%02d', $year, $month, 1).
' -1 day'));
152 if (($now[
'wday'] != 0) && ($now[
'wday'] != 6)) {
153 $sql_freqs[] =
'frequency = '.MatrixDAL::quote(
'DWD');
155 $sql_freqs[] =
'frequency = '.MatrixDAL::quote(
'DWE');
158 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'DED').
' AND mod(('.
MatrixDAL::quote($now[
'ds']).
' - start_date_ds), period) BETWEEN 0 AND COALESCE(end_date_ds - start_date_ds,0))';
160 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'WEW').
' AND mod(('.
MatrixDAL::quote($now[
'ds']).
' - start_date_ds), (period*7)) BETWEEN 0 AND COALESCE(end_date_ds - start_date_ds,0))';
163 (SUBSTR(frequency, 1, 1) = '.MatrixDAL::quote(
'M').
') AND (
165 /* there is an instance that starts in our month */
168 /* our view date is between the start and end of the instance */
169 ((frequency = '.
MatrixDAL::quote(
'MFN').
') AND (('.
MatrixDAL::quote($now[
'mday']).
' - start_date_mday) BETWEEN 0 AND (COALESCE((end_date_ds - start_date_ds), 0))))
170 OR ((frequency = '.
MatrixDAL::quote(
'MRN').
') AND ((start_date_rmday - '.
MatrixDAL::quote($now[
'rmday']).
') BETWEEN 0 AND (COALESCE((end_date_ds - start_date_ds), 0))))
171 OR ((frequency = '.
MatrixDAL::quote(
'MFW').
') AND (('.
MatrixDAL::quote($now[
'mday']).
' - (((start_date_wdom - 1) * 7) + MOD(start_date_wday + 7 - '.$now[
'month_start_wday'].
', 7) + 1)) BETWEEN 0 AND (COALESCE((end_date_ds - start_date_ds), 0))))
172 OR ((frequency = '.
MatrixDAL::quote(
'MRW').
') AND ((((start_date_rwdom - 1) * 7) + MOD('.($now[
'month_end_wday'] + 7).
' - start_date_wday, 7) + 1 - '.$now[
'rmday'].
') BETWEEN 0 AND (COALESCE((end_date_ds - start_date_ds), 0))))
175 /* An instance that started in a previous month doesnt end til a subsequent month */
176 (MOD('.$now[
'ms'].
' - start_date_ms, period) < MOD('.$now[
'ms'].
' - end_date_ms, period))
178 /* An instance ends this month */
179 ((start_date_ms != end_date_ms) AND (MOD('.$now[
'ms'].
' - end_date_ms, period) = 0))
181 /* its end is PROBABLY after our view date - we can only be ~90% sure here */
183 AND ((end_date_ds - start_date_ds + 1) >= ROUND(('.
MatrixDAL::quote($now[
'ts']).
' - '.
Calendar_Common::_ts2Sql(
'FLOOR((('.$now[
'year'].
' * 12) + '.($now[
'mon']-1).
' - (end_date_ms - start_date_ms)) / 12)',
'MOD('.$now[
'mon'].
' + 10 - MOD('.($now[
'ms']-1).
' - start_date_ms, period), 12) + 1',
'start_date_mday').
') / 86400)))
185 AND ((end_date_ds - start_date_ds + 1) >= ROUND(('.
MatrixDAL::quote($now[
'ts']).
' - '.
Calendar_Common::_ts2Sql(
'FLOOR((('.$now[
'year'].
' * 12) + '.($now[
'mon']-1).
' - (end_date_ms - start_date_ms)) / 12)',
'MOD('.$now[
'mon'].
' + 10 - MOD('.($now[
'ms']-1).
' - start_date_ms, period), 12) + 1',
'((start_date_mday + 3) - (FLOOR((start_date_mday + 3) / 31) * MOD(start_date_mday + 3, 31)))').
') / 86400)))
187 AND ((end_date_ds - start_date_ds + 1) >= ROUND(('.
MatrixDAL::quote($now[
'ts']).
' - '.
Calendar_Common::_ts2Sql(
'FLOOR((('.$now[
'year'].
' * 12) + '.($now[
'mon']-1).
' - (end_date_ms - start_date_ms)) / 12)',
'MOD('.$now[
'mon'].
' + 10 - MOD('.($now[
'ms']-1).
' - start_date_ms, period), 12) + 1',
'(7 * start_date_wdom) - (FLOOR((7 * start_date_wdom) / 31) * MOD(7 * start_date_wdom, 31))').
') / 86400)))
189 AND ((end_date_ds - start_date_ds + 1) >= ROUND(('.
MatrixDAL::quote($now[
'ts']).
' - '.
Calendar_Common::_ts2Sql(
'FLOOR((('.$now[
'year'].
' * 12) + '.($now[
'mon']-1).
' - (end_date_ms - start_date_ms)) / 12)',
'MOD('.$now[
'mon'].
' + 10 - MOD('.($now[
'ms']-1).
' - start_date_ms, period), 12) + 1',
'(31 - ((start_date_rwdom - 1) * 7))').
') / 86400)))
192 /* An instance that started last month has run into our month
193 even though its original instance was contained in only one month
194 and it ends on/after our view date */
195 (MOD('.$now[
'ms'].
' - start_date_ms, period) = 1)
198 AND ((start_date_mday + (end_date_ds - start_date_ds)) - '.$prev_month_length.
' >= '.$now[
'mday'].
'))
200 AND (((MOD(7 + '.$prev_month_start_wday.
' - start_date_wday, 7) + 1) + ((start_date_wdom - 1) * 7) + (end_date_ds - start_date_ds)) - '.$prev_month_length.
' >= '.$now[
'mday'].
'))
202 AND ((('.$prev_month_length.
' - MOD(7 + '.$prev_month_end_wday.
' - start_date_wday, 7)) + (end_date_ds - start_date_ds)) - '.$prev_month_length.
' >= '.$now[
'mday'].
'))
212 $days_in_month = days_in_month($month,$year);
214 $req_next_week = ($day % 7 != 1);
215 $req_rev_next_week = (($days_in_month - $day + 1) % 7 != 1);
216 $req_next_month = ($day + 6 > $days_in_month);
218 $last_day = $now[
'ds'] + 6;
220 $sql_freqs[] =
'frequency = '.MatrixDAL::quote(
'DWD');
221 $sql_freqs[] =
'frequency = '.MatrixDAL::quote(
'DWE');
225 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'DED').
' AND ((FLOOR(('.
MatrixDAL::quote($now[
'ds']-1).
' - start_date_ds) / (period*1.0)) < FLOOR(('.
MatrixDAL::quote($now[
'ds']+6).
' - start_date_ds) / (period*1.0))) or mod(('.
MatrixDAL::quote($now[
'ds']).
' - start_date_ds), period) BETWEEN 0 AND COALESCE(end_date_ds - start_date_ds,0)))';
227 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'WEW').
' AND ((FLOOR(('.
MatrixDAL::quote($now[
'ds']-1).
' - start_date_ds)/(period*7.0)) < FLOOR(('.
MatrixDAL::quote($now[
'ds']+6).
' - start_date_ds)/(period*7.0))) OR MOD(('.
MatrixDAL::quote($now[
'ds']).
' - start_date_ds), period) BETWEEN 0 AND COALESCE(end_date_ds - start_date_ds,0)))';
229 $sql_freqs_m = Array();
232 $sql_freqs_m[] =
'(frequency = '.MatrixDAL::quote(
'MFN').
' AND start_date_mday BETWEEN '.
MatrixDAL::quote(intval($day)).
' AND '.
MatrixDAL::quote(min($day+6, $days_in_month)).
')';
235 $sql_freqs_m[] =
'(frequency = '.MatrixDAL::quote(
'MFW').
' AND start_date_wdom IN ( '.
MatrixDAL::quote($now[
'wdom']).($req_next_week ?
','.MatrixDAL::quote($now[
'wdom']+1) :
'').
'))';
238 $sql_freqs_m[] =
'(frequency = '.MatrixDAL::quote(
'MRN').
' AND start_date_rmday BETWEEN '.
MatrixDAL::quote(max($days_in_month - $day - 5, 1)).
' AND '.
MatrixDAL::quote(intval($days_in_month - $day + 1)).
')';
241 $sql_freqs_m[] =
'(frequency = '.MatrixDAL::quote(
'MRW').
' AND start_date_rwdom IN ( '.
MatrixDAL::quote($now[
'rwdom']).($req_rev_next_week ?
','.MatrixDAL::quote($now[
'rwdom']-1) :
'').
'))';
243 $sql_freqs[] =
'(('.implode(
"\n\t".
' OR ', $sql_freqs_m).
') AND (MOD('.
MatrixDAL::quote($now[
'ms']).
' - start_date_ms, period) = 0))';
246 if ($req_next_month) {
249 $new_year = $year + 1;
251 $new_month = $month + 1;
255 $req_rev_next_week = (($day + 28) > days_in_month($new_month, $new_year));
256 $wdom_list = weekday_dates_in_month($now[
'wday'], $new_month, $new_year);
257 $days_in_new_month = days_in_month($new_month,$new_year);
260 $sql_freqs_m = Array();
262 $sql_freqs_m[] =
'(frequency = '.MatrixDAL::quote(
'MFN').
' AND start_date_mday BETWEEN '.
MatrixDAL::quote(1).
' AND '.
MatrixDAL::quote($day+6-$days_in_month).
')';
266 $sql_freqs_m[] =
'(frequency = '.MatrixDAL::quote(
'MFW').
' AND start_date_wdom = 1)';
269 $sql_freqs_m[] =
'(frequency = '.MatrixDAL::quote(
'MRN').
' AND start_date_rmday BETWEEN '.
MatrixDAL::quote($days_in_month * 2 - $day - 5).
' AND '.
MatrixDAL::quote(intval($days_in_month)).
')';
272 $sql_freqs_m[] =
'(frequency = '.MatrixDAL::quote(
'MRW').
' AND start_date_rwdom IN ( '.
MatrixDAL::quote(count($wdom_list)).($req_rev_next_week ?
','.MatrixDAL::quote(count($wdom_list)-1) :
'').
'))';
274 $sql_freqs[] =
'(('.implode(
"\n\t".
' OR ', $sql_freqs_m).
') AND (MOD('.
MatrixDAL::quote($now[
'ms']+1).
' - start_date_ms, period) = 0))';
277 $sql_freqs[] =
'(frequency IN ('.MatrixDAL::quote(
'MFN').
', '.
MatrixDAL::quote(
'MRN').
') AND (end_date_ds - start_date_ds >= '.$day.
'))';
279 $sql_freqs[] =
'(frequency IN ('.MatrixDAL::quote(
'MFW').
', '.
MatrixDAL::quote(
'MRW').
') AND (end_date_ds - start_date_ds >= 1 + ((start_date_rwdom-1) * 7)))';
284 $last_day = $now[
'ds'] + days_in_month($month,$year) - 1;
286 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'DWD').
')';
287 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'DWE').
')';
289 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'DED').
' AND (FLOOR(('.
MatrixDAL::quote($now[
'ds']-1).
' - start_date_ds)/period) < FLOOR(('.
MatrixDAL::quote($last_day).
' - start_date_ds)/period)))';
291 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'WEW').
' AND period = 1)';
293 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'WEW').
' AND (floor(('.
MatrixDAL::quote($now[
'ds']-1).
' - start_date_ds)/(period*7)) < FLOOR('.
MatrixDAL::quote($last_day).
' - start_date_ds)/(period*7)))';
296 $sql_freqs[] =
'(frequency LIKE '.MatrixDAL::quote(
'M%').
' AND (MOD(('.
MatrixDAL::quote($now[
'ms']).
' - start_date_ms), period) = 0))';
298 $iso = $year.
'-'.sprintf(
'%02d', $month).
'-01';
303 $last_day = $now[
'ds']+364+is_leap_year($year);
305 $sql_freqs[] =
'frequency = '.MatrixDAL::quote(
'DWD');
306 $sql_freqs[] =
'frequency = '.MatrixDAL::quote(
'DWE');
308 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'DED').
' AND (FLOOR(('.
MatrixDAL::quote($now[
'ds']-1).
' - start_date_ds)/period) < FLOOR(('.
MatrixDAL::quote($last_day).
' - start_date_ds)/period)))';
310 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'WEW').
' AND period = 1)';
312 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'WEW').
' AND (FLOOR(('.
MatrixDAL::quote($now[
'ds']-1).
' - start_date_ds)/(period*7)) < FLOOR(('.
MatrixDAL::quote($last_day).
' - start_date_ds)/(period*7))))';
317 $sql_freqs[] =
'(frequency LIKE '.MatrixDAL::quote(
'M%').
' AND (FLOOR(('.($year * 12).
' - start_date_ms) / period) < FLOOR(('.(($year + 1) * 12).
' - start_date_ms) / period)))';
321 $last_date = date(
'Y-m-d', strtotime(
"$iso 00:00:00 +$length days") - strtotime(
'1970-01-01 00:00:00 Z'));
323 $last_day = $last_date_components[
'ds'];
327 (start_date_ds BETWEEN '.$now[
'ds'].
' AND '.$last_date_components[
'ds'].
')
329 (end_date_ds BETWEEN '.$now[
'ds'].
' AND '.$last_date_components[
'ds'].
')
334 (frequency = '.MatrixDAL::quote(
'DED').
')
337 (period <= '.$length.
')
339 (FLOOR(('.$now[
'ds'].
' - start_date_ds) / period) < FLOOR(('.$last_date_components[
'ds'].
' - start_date_ds) / period))
341 (floor(('.$now[
'ds'].
' - end_date_ds) / period) < FLOOR(('.$last_date_components[
'ds'].
' - end_date_ds) / period))
344 if (($length > 2) || !in_array($now[
'wday'], Array(0, 6)) || !in_array($last_date_components[
'wday'], Array(0, 6))) {
346 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'DWD').
')';
348 if (($length > 5) || in_array($now[
'wday'], Array(0, 6)) || in_array($last_date_components[
'wday'], Array(0, 6)) || ($last_date_components[
'wday'] < $now[
'wday'])) {
350 $sql_freqs[] =
'(frequency = '.MatrixDAL::quote(
'DWE').
')';
355 (frequency = '.MatrixDAL::quote(
'WEW').
')
358 ((period * 7) <= '.$length.
')
360 (FLOOR(('.($now[
'ds']-1).
' - start_date_ds) / (period * 7)) < FLOOR(('.$last_date_components[
'ds'].
' - start_date_ds) / (period * 7)))
362 (FLOOR(('.($now[
'ds']-1).
' - end_date_ds) / (period * 7)) < FLOOR(('.$last_date_components[
'ds'].
' - end_date_ds) / (period * 7)))
367 $monthly_conditions = Array();
368 if ($last_date_components[
'ms'] == $now[
'ms']) {
372 $monthly_conditions[] =
'((frequency = '.MatrixDAL::quote(
'MFN').
') AND (((MOD('.$now[
'ms'].
' - start_date_ms, period) = 0) AND (start_date_mday BETWEEN '.$now[
'mday'].
' AND '.$last_date_components[
'mday'].
')) OR ((MOD('.$now[
'ms'].
' - end_date_ms, period) = 0) AND (end_date_mday BETWEEN '.$now[
'mday'].
' AND '.$last_date_components[
'mday'].
'))))';
373 $monthly_conditions[] =
'((frequency = '.MatrixDAL::quote(
'MRN').
') AND ((start_date_rwdom BETWEEN '.$last_date_components[
'rwdom'].
' AND '.$now[
'rwdom'].
') OR (end_date_rwdom BETWEEN '.$last_date_components[
'rwdom'].
' AND '.$now[
'rwdom'].
')))';
376 $mfw_conditions = Array();
377 if ($last_date_components[
'wdom'] == $now[
'wdom']) {
381 $mfw_conditions[] =
'((MOD('.$now[
'ms'].
' - start_date_ms, period) = 0)
382 AND (start_date_wdom = '.$now[
'wdom'].
')
383 AND (start_date_mday - (7 * (start_date_wdom - 1)) BETWEEN '.($now[
'mday'] - (7 * ($now[
'wdom'] - 1))).
' AND '.($last_date_components[
'mday'] - (7 * ($last_date_components[
'wdom'] - 1))).
'))';
384 $mfw_conditions[] =
'((MOD('.$now[
'ms'].
' - end_date_ms, period) = 0)
385 AND (end_date_wdom = '.$now[
'wdom'].
')
386 AND (end_date_mday - (7 * (end_date_wdom - 1)) BETWEEN '.($now[
'mday'] - (7 * ($now[
'wdom'] - 1))).
' AND '.($last_date_components[
'mday'] - (7 * ($last_date_components[
'wdom'] - 1))).
'))';
392 $mfw_conditions[] =
'(((start_date_wdom = '.$now[
'wdom'].
') AND (start_date_mday - ((start_date_wdom - 1) * 7) >= '.($now[
'mday'] - (($now[
'wdom'] - 1) * 7)).
')) OR (((end_date_wdom = '.$now[
'wdom'].
') AND (end_date_mday - ((end_date_wdom - 1) * 7) >= '.($now[
'mday'] - (($now[
'wdom'] - 1) * 7)).
'))))';
395 $mfw_conditions[] =
'(((start_date_wdom = '.$last_date_components[
'wdom'].
') AND (start_date_mday - ((start_date_wdom - 1) * 7) <= '.($last_date_components[
'mday'] - (($last_date_components[
'wdom'] - 1) * 7)).
')) OR (((end_date_wdom = '.$last_date_components[
'wdom'].
') AND (end_date_mday - ((end_date_wdom - 1) * 7) <= '.($last_date_components[
'mday'] - (($last_date_components[
'wdom'] - 1) * 7)).
'))))';
398 $mfw_conditions[] =
'((start_date_wdom <= '.$now[
'wdom'].
') AND (end_date_wdom >= '.$last_date_components[
'wdom'].
') AND (start_date_mday - ((start_date_wdom - 1) * 7) < '.($now[
'mday'] - (($now[
'wdom'] - 1) * 7)).
') AND (end_date_mday - ((end_date_wdom - 1) * 7) > '.($last_date_components[
'mday'] - (($last_date_components[
'wdom'] - 1) * 7)).
'))';
400 if ($last_date_components[
'wdom'] - $now[
'wdom'] != 1) {
403 $mfw_conditions[] =
'((frequency = '.MatrixDAL::quote(
'MFW').
') AND ((start_date_wdom BETWEEN '.($now[
'wdom'] + 1).
' AND '.($last_date_components[
'wdom'] - 1).
') OR (end_date_wdom BETWEEN '.($now[
'wdom'] + 1).
' AND '.($last_date_components[
'wdom'] - 1).
')))';
406 $monthly_conditions[] =
'((frequency = '.MatrixDAL::quote(
'MFW').
') AND ('.implode(
' OR ', $mfw_conditions).
"\n\n ))";
409 $mrw_conditions = Array();
410 if ($last_date_components[
'rwdom'] == $now[
'rwdom']) {
413 $mrw_conditions[] =
'((MOD('.$now[
'ms'].
' - start_date_ms, period) = 0)
414 AND (start_date_rwdom = '.$now[
'rwdom'].
')
415 AND (start_date_rmday - (7 * (start_date_rwdom - 1)) BETWEEN '.($now[
'rmday'] - (7 * ($now[
'rwdom'] - 1))).
' AND '.($last_date_components[
'rmday'] - (7 * ($last_date_components[
'rwdom'] - 1))).
'))';
416 $mrw_conditions[] =
'((MOD('.$now[
'ms'].
' - end_date_ms, period) = 0)
417 AND (end_date_rwdom = '.$now[
'rwdom'].
')
418 AND (end_date_rmday - (7 * (end_date_rwdom - 1)) BETWEEN '.($now[
'rmday'] - (7 * ($now[
'rwdom'] - 1))).
' AND '.($last_date_components[
'rmday'] - (7 * ($last_date_components[
'rwdom'] - 1))).
'))';
423 $mrw_conditions[] =
'(((start_date_rwdom = '.$now[
'rwdom'].
') AND (start_date_rmday - ((start_date_rwdom - 1) * 7) >= '.($now[
'rmday'] - (($now[
'rwdom'] - 1) * 7)).
')) OR (((end_date_rwdom = '.$now[
'rwdom'].
') AND (end_date_rmday - ((end_date_rwdom - 1) * 7) <= '.($now[
'rmday'] - (($now[
'rwdom'] - 1) * 7)).
'))))';
426 $mrw_conditions[] =
'(((start_date_rwdom = '.$last_date_components[
'rwdom'].
') AND (start_date_rmday - ((start_date_rwdom - 1) * 7) <= '.($last_date_components[
'rmday'] - (($last_date_components[
'rwdom'] - 1) * 7)).
')) OR (((end_date_rwdom = '.$last_date_components[
'rwdom'].
') AND (end_date_rmday - ((end_date_rwdom - 1) * 7) >= '.($last_date_components[
'rmday'] - (($last_date_components[
'rwdom'] - 1) * 7)).
'))))';
429 $mrw_conditions[] =
'((start_date_rwdom >= '.$now[
'rwdom'].
') AND (end_date_rwdom <= '.$last_date_components[
'rwdom'].
') AND (start_date_rmday - ((start_date_rwdom - 1) * 7) > '.($now[
'rmday'] - (($now[
'rwdom'] - 1) * 7)).
') AND (end_date_rmday - ((end_date_rwdom - 1) * 7) < '.($last_date_components[
'rmday'] - (($last_date_components[
'rwdom'] - 1) * 7)).
'))';
431 if ($last_date_components[
'rwdom'] - $now[
'rwdom'] != 1) {
434 $mrw_conditions[] =
'((start_date_rwdom BETWEEN '.($now[
'rwdom'] - 1).
' AND '.($last_date_components[
'rwdom'] + 1).
') OR (end_date_rwdom BETWEEN '.($now[
'rwdom'] - 1).
' AND '.($last_date_components[
'rwdom'] + 1).
'))';
437 $monthly_conditions[] =
'((frequency = '.MatrixDAL::quote(
'MRW').
') AND ('.implode(
' OR ', $mrw_conditions).
'))';
441 /* begin month options */
442 (frequency LIKE '.MatrixDAL::quote(
'M%').
')
445 (end_date_ms IS NOT NULL)
446 AND (end_date_ms - start_date_ms >= 3)
447 AND MOD('.$now[
'ms'].
' - start_date_ms, period)
448 BETWEEN 1 AND (end_date_ms - start_date_ms - 1)
451 MOD('.$now[
'ms'].
' - start_date_ms, period) = 0
452 OR MOD('.$now[
'ms'].
' - end_date_ms, period) = 0
454 '.implode(
"\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t OR ", $monthly_conditions).
'
458 /* end month options */
464 $sql_freqs[] =
'((frequency = '.MatrixDAL::quote(
'MFN').
') AND (((MOD('.$now[
'ms'].
' - start_date_ms, period) = 0) AND (start_date_mday >= '.$now[
'mday'].
')) OR ((MOD('.$now[
'ms'].
' - end_date_ms, period) = 0) AND (end_date_mday >= '.$now[
'mday'].
'))))';
466 $sql_freqs[] =
'((frequency = '.MatrixDAL::quote(
'MRN').
') AND (((MOD('.$now[
'ms'].
' - start_date_ms, period) = 0) AND (start_date_rmday <= '.$now[
'rmday'].
')) OR ((MOD('.$now[
'ms'].
' - end_date_ms, period) = 0) AND (end_date_rmday <= '.$now[
'rmday'].
'))))';
468 $sql_freqs[] =
'((frequency = '.MatrixDAL::quote(
'MFW').
') AND (MOD('.$now[
'ms'].
' - start_date_ms, period) = 0) AND ((start_date_wdom > '.$now[
'wdom'].
') OR (((start_date_wdom = '.$now[
'wdom'].
') AND (start_date_mday - ((start_date_wdom - 1) * 7) >= '.($now[
'mday'] - (($now[
'wdom'] - 1) * 7)).
')))))';
470 $sql_freqs[] =
'((frequency = '.MatrixDAL::quote(
'MFW').
') AND (MOD('.$now[
'ms'].
' - end_date_ms, period) = 0) AND ((end_date_wdom > '.$now[
'wdom'].
') OR (((end_date_wdom = '.$now[
'wdom'].
') AND (end_date_mday - ((end_date_wdom - 1) * 7) >= '.($now[
'mday'] - (($now[
'wdom'] - 1) * 7)).
')))))';
472 $sql_freqs[] =
'((frequency = '.MatrixDAL::quote(
'MRW').
') AND (MOD('.$now[
'ms'].
' - start_date_ms, period) = 0) AND ((start_date_rwdom < '.$now[
'rwdom'].
') OR (((start_date_rwdom = '.$now[
'rwdom'].
') AND (start_date_rmday - ((start_date_rwdom - 1) * 7) <= '.($now[
'rmday'] - (($now[
'rwdom'] - 1) * 7)).
')))))';
474 $sql_freqs[] =
'((frequency = '.MatrixDAL::quote(
'MRW').
') AND (MOD('.$now[
'ms'].
' - end_date_ms, period) = 0) AND ((end_date_rwdom > '.$now[
'rwdom'].
') OR (((end_date_rwdom = '.$now[
'rwdom'].
') AND (end_date_rmday - ((end_date_rwdom - 1) * 7) <= '.($now[
'rmday'] - (($now[
'rwdom'] - 1) * 7)).
')))))';
477 $sql_freqs[] =
'((frequency = '.MatrixDAL::quote(
'MFN').
') AND (((MOD('.$last_date_components[
'ms'].
' - start_date_ms, period) = 0) AND (start_date_mday <= '.$last_date_components[
'mday'].
')) OR ((MOD('.$last_date_components[
'ms'].
' - end_date_ms, period) = 0) AND (end_date_mday <= '.$last_date_components[
'mday'].
'))))';
479 $sql_freqs[] =
'((frequency = '.MatrixDAL::quote(
'MRN').
') AND (((MOD('.$last_date_components[
'ms'].
' - start_date_ms, period) = 0) AND (start_date_rmday >= '.$last_date_components[
'rmday'].
')) OR ((MOD('.$last_date_components[
'ms'].
' - end_date_ms, period) = 0) AND (end_date_rmday >= '.$last_date_components[
'rmday'].
'))))';
481 $sql_freqs[] =
'((frequency = '.MatrixDAL::quote(
'MFW').
') AND (MOD('.$now[
'ms'].
' - start_date_ms, period) = 0) AND ((start_date_wdom < '.$now[
'wdom'].
') OR (((start_date_wdom = '.$now[
'wdom'].
') AND (start_date_mday - ((start_date_wdom - 1) * 7) <= '.($now[
'mday'] - (($now[
'wdom'] - 1) * 7)).
')))))';
483 $sql_freqs[] =
'((frequency = '.MatrixDAL::quote(
'MFW').
') AND (MOD('.$now[
'ms'].
' - end_date_ms, period) = 0) AND ((end_date_wdom < '.$now[
'wdom'].
') OR (((end_date_wdom = '.$now[
'wdom'].
') AND (end_date_mday - ((end_date_wdom - 1) * 7) <= '.($now[
'mday'] - (($now[
'wdom'] - 1) * 7)).
')))))';
485 $sql_freqs[] =
'((frequency = '.MatrixDAL::quote(
'MRW').
') AND (MOD('.$now[
'ms'].
' - start_date_ms, period) = 0) AND ((start_date_rwdom > '.$now[
'rwdom'].
') OR (((start_date_rwdom = '.$now[
'rwdom'].
') AND (start_date_rmday - ((start_date_rwdom - 1) * 7) >= '.($now[
'rmday'] - (($now[
'rwdom'] - 1) * 7)).
')))))';
487 $sql_freqs[] =
'((frequency = '.MatrixDAL::quote(
'MRW').
') AND (MOD('.$now[
'ms'].
' - end_date_ms, period) = 0) AND ((end_date_rwdom > '.$now[
'rwdom'].
') OR (((end_date_rwdom = '.$now[
'rwdom'].
') AND (end_date_rmday - ((end_date_rwdom - 1) * 7) <= '.($now[
'rmday'] - (($now[
'rwdom'] - 1) * 7)).
')))))';
489 if ($last_date_components[
'ms'] - $now[
'ms'] > 1) {
491 $sql_freqs[] =
'((frequency LIKE '.MatrixDAL::quote(
'M%').
') AND ((MOD('.$last_date_components[
'ms'].
' - start_date_ms, period) BETWEEN 1 AND '.($last_date_components[
'ms'] - $now[
'ms'] - 1).
') OR (MOD('.$last_date_components[
'ms'].
' - end_date_ms, period) BETWEEN 1 AND '.($last_date_components[
'ms'] - $now[
'ms'] - 1).
')))';
498 $sql .=
"(/* begin sql freqs */\n\n".implode(
"\n\n\nOR\n\n\n ", $sql_freqs).
"\n/* end sql freqs */\n)\n\n";
501 $sql .=
' AND ('.$last_day.
' >= start_date_ds)';
502 $sql .=
' AND (stop_date = '.MatrixDAL::quote(
'----------').
' OR stop_date >= '.
MatrixDAL::quote($iso).
')';
504 $sql .=
' AND '.Calendar_Common::_getTreeClause($root_ids);
524 foreach ($raw as $r) {
525 if (!isset($res[$r[
'assetid']])) {
526 $r[
'treeid'] = Array($r[
'treeid']);
527 $res[$r[
'assetid']] = $r;
529 $res[$r[
'assetid']][
'treeid'][] = $r[
'treeid'];
553 function &
expandEventList(&$events, $first_day, $last_day, $day_start_time=
'00:00', $day_end_time=
'23:59')
556 foreach (array_keys($events) as $key) {
557 $event =& $events[$key];
558 $recurring_descandants = $GLOBALS[
'SQ_SYSTEM']->am->getTypeDescendants(
'calendar_event_recurring');
559 if (($event[
'type_code'] ==
'calendar_event_recurring') || in_array($event[
'type_code'], $recurring_descandants)) {
584 $exception_ids = Array();
585 foreach ($res as $id => $details) {
586 if (in_array($details[
'type_code'], Array(
'calendar_event_cancellation',
'calendar_event_modification'))) {
587 $id_bits = explode(
':', $id);
588 $real_id = $id_bits[0];
592 if (empty($exception_ids))
return FALSE;
594 $exception_links = Array();
596 $query =
MatrixDAL::preparePdoQuery(
'SELECT majorid, minorid FROM '.SQ_TABLE_RUNNING_PREFIX.
'ast_lnk WHERE minorid IN ('.implode(
', ', $exception_ids).
')');
598 }
catch (Exception $e) {
599 throw new Exception($e->getMessage());
605 if (empty($exception_links))
return FALSE;
609 $events_n_exceptions = Array();
611 foreach ($exception_links as $event => $exceptions) {
612 foreach ($exceptions as $index => $exception) {
613 $exception_event = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($exception[0]);
614 $events_n_exceptions[$event][] = $exception_event;
618 foreach ($events_n_exceptions as $event_id => $exceptions) {
619 foreach ($exceptions as $i => $exception) {
620 foreach ($res as $key => $details) {
621 $id = explode(
':', $key);
623 if ($event_id == $id) {
629 $exception_date = $exception->attr(
'start_date');
630 if (strpos($exception_date, date(
'Y-m-d', $details[
'start_date_ts'])) !== FALSE) {
636 if ($details[
'type_code'] ==
'calendar_event_cancellation') {
657 return ($a[
'start_date_ts'] < $b[
'start_date_ts']) ? -1 : 1;
673 return ($b[
'end_date_ts'] - $b[
'start_date_ts']) - ($a[
'end_date_ts'] - $a[
'start_date_ts']);
692 if ($iso_24h = (iso8601_time_component($iso) ==
'24:00:00')) {
693 $iso = substr($iso,0,10).
' 00:00:00';
697 if (($ts = strtotime($iso)) != FALSE) {
698 $this_date = getdate($ts);
699 $this_date[
'yday']++;
702 unset($this_date[
'weekday']);
703 unset($this_date[
'month']);
706 $this_date[
'ts'] = strtotime($iso.
' Z') - strtotime(
'1970-01-01 00:00:00 Z');
707 unset($this_date[0]);
714 $this_date[
'ts'] += 86400;
715 $this_date[
'hours'] = 24;
720 $this_date = Array();
722 list($this_date[
'year'], $this_date[
'mon'], $this_date[
'mday'], $this_date[
'hours'], $this_date[
'minutes'], $this_date[
'seconds']) = sscanf($iso,
'%4s-%2s-%2s %2s:%2s:%2s');
725 foreach (array_keys($this_date) as $iso_part) {
726 if ($this_date[$iso_part] == str_repeat(
'-', ($iso_part ==
'year' ? 4 : 2))) {
727 $this_date[$iso_part] = NULL;
731 $date_only_ts = strtotime(substr($iso,0,10));
734 if (is_null($this_date[
'mday']) || is_null($this_date[
'mon']) || is_null($this_date[
'year']) || ($date_only_ts == 1)) {
735 $this_date[
'wday'] = NULL;
736 $this_date[
'yday'] = NULL;
737 $this_date[
'ts'] = NULL;
738 $this_date[
'ds'] = NULL;
740 if (is_null($this_date[
'hours']) && (!is_null($this_date[
'minutes']) || !is_null($this_date[
'seconds']))) {
742 $this_date[
'ts'] = NULL;
743 }
else if (is_null($this_date[
'minutes']) && !is_null($this_date[
'seconds'])) {
745 $this_date[
'ts'] = NULL;
748 $this_date[
'ts'] = strtotime(substr($iso,0,10).
' '.str_replace(
'-',
'0',substr($iso,11)).
' Z') - strtotime(
'1970-01-01 00:00:00 Z');
753 $this_date[
'wday'] = date(
'w', $date_only_ts);
754 $this_date[
'yday'] = date(
'z', $date_only_ts) + 1;
761 if (is_null($this_date[
'mon']) || is_null($this_date[
'year'])) {
762 $this_date[
'ms'] = NULL;
767 if (is_null($this_date[
'mday']) || is_null($this_date[
'mon']) || is_null($this_date[
'year'])) {
768 $this_date[
'ryday'] = NULL;
769 $this_date[
'rmday'] = NULL;
770 $this_date[
'wdom'] = NULL;
771 $this_date[
'rwdom'] = NULL;
774 $this_date[
'ryday'] = 365 + (is_leap_year($this_date[
'year']) ? 1 : 0) - $this_date[
'yday'] + 1;
775 $this_date[
'rmday'] = days_in_month($this_date[
'mon'], $this_date[
'year']) - $this_date[
'mday'] + 1;
778 $wdom = weekday_dates_in_month($this_date[
'wday'], $this_date[
'mon'], $this_date[
'year']);
780 $this_date[
'wdom'] = array_search($this_date[
'mday'], $wdom) + 1;
781 $this_date[
'rwdom'] = count($wdom) - $this_date[
'wdom'] + 1;
786 foreach (array_keys($this_date) as $iso_part) {
787 $res[$prefix.$iso_part] = is_null($this_date[$iso_part]) ? NULL : intval($this_date[$iso_part]);
802 $base = ($GLOBALS[
'SQ_SYSTEM']->userSystemAdmin() || $GLOBALS[
'SQ_SYSTEM']->userRoot()) ? SQ_TABLE_RUNNING_PREFIX.
'ast a' :
'('.SQ_TABLE_RUNNING_PREFIX.
'ast a JOIN '.SQ_TABLE_RUNNING_PREFIX.
'vw_ast_perm p ON a.assetid = p.assetid)';
803 return '('.$base.
' JOIN ('.SQ_TABLE_RUNNING_PREFIX.
'ast_lnk l JOIN '.SQ_TABLE_RUNNING_PREFIX.
'ast_lnk_tree t ON l.linkid = t.linkid) ON l.minorid = a.assetid) JOIN '.SQ_TABLE_RUNNING_PREFIX.
'cal_date_val cd ON a.assetid = cd.assetid';
821 function _ts2Sql($y, $m, $d, $h=
'00', $i=
'00', $s=
'00')
829 $sql =
"(86400 * (DATE (($y) || '-' || ($m) || '-' || ($d)) - DATE '1970-01-01') + ($h) * 3600 + ($i) * 60 + ($s))";
836 $sql =
'(86400 * ('.db_extras_todate(
MatrixDAL::getDbType(),
"(($y) || '-' || ($m) || '-' || ($d) || ' ' || ($h) || ':' || ($i) || ':' || ($s))", FALSE).
' - '.db_extras_todate(
MatrixDAL::getDbType(),
'1970-01-01 00:00:00').
'))';
855 $treeids = $GLOBALS[
'SQ_SYSTEM']->am->getAssetTreeids($root_ids);
857 $tree_options = Array();
858 foreach ($treeids as $assetid => $treeids) {
859 foreach ($treeids as $treeid) {
860 $tree_options[] .=
'(t.treeid LIKE '.MatrixDAL::quote($treeid[0].
'%').
')';
863 return '('.implode(
' OR ', $tree_options).
')';
883 function _expandSingleEvent(&$result_list, &$event, $assetid, $first_day, $last_day, $day_start_time, $day_end_time)
885 list($day_start_hours, $day_start_minutes) = sscanf($day_start_time,
'%02d:%02d');
886 list($day_end_hours, $day_end_minutes) = sscanf($day_end_time,
'%02d:%02d');
887 $day_start_dts = ($day_start_hours * 60) + $day_start_minutes;
888 $day_end_dts = ($day_end_hours * 60) + $day_end_minutes;
890 $event_start_date = sprintf(
'%04d-%02d-%02d %02d:%02d:00', $event[
'start_date_year'], $event[
'start_date_mon'], $event[
'start_date_mday'], $event[
'start_date_hours'], $event[
'start_date_minutes']);
891 $event_start_dts = ($event[
'start_date_hours'] * 60) + $event[
'start_date_minutes'];
895 if (!is_null($event[
'end_date_ds'])) {
897 $event_end_date = sprintf(
'%04d-%02d-%02d %02d:%02d:00', $event[
'end_date_year'], $event[
'end_date_mon'], $event[
'end_date_mday'], $event[
'end_date_hours'], $event[
'end_date_minutes']);
898 $event_end_dts = (($event[
'end_date_hours'] * 60) + $event[
'end_date_minutes']);
900 if ($event[
'start_date_ds'] != $event[
'end_date_ds']) {
902 $event[
'expanded'] = 1;
904 if (($first_day <= substr($event_start_date, 0, 10)) && ($event_start_dts < $day_end_dts)) {
906 $result_list[$assetid.
':'.$i] = Array();
907 for (reset($event); NULL !== ($k = key($event)); next($event)) {
908 $result_list[$assetid.
':'.$i][$k] = $event[$k];
911 $result_list[$assetid.
':'.$i] = array_merge($result_list[$assetid.
':'.$i],
919 $inter_date = date(
'Y-m-d', strtotime(max(add_days_to_iso($event_start_date, 1), $first_day)));
920 $last_inter_date = min($last_day, add_days_to_iso($event_end_date, -1));
921 while ($inter_date <= $last_inter_date) {
922 $result_list[$assetid.
':'.$i] = Array();
923 for (reset($event); NULL !== ($k = key($event)); next($event)) {
924 $result_list[$assetid.
':'.$i][$k] = $event[$k];
927 $result_list[$assetid.
':'.$i] = array_merge($result_list[$assetid.
':'.$i],
931 $inter_date = add_days_to_iso($inter_date, 1);
935 if ((substr($event_end_date, 0, 10) <= $last_day) && ($event_end_dts > $day_start_dts)) {
937 $result_list[$assetid.
':'.$i] = Array();
938 for (reset($event); NULL !== ($k = key($event)); next($event)) {
939 $result_list[$assetid.
':'.$i][$k] = $event[$k];
942 $result_list[$assetid.
':'.$i] = array_merge($result_list[$assetid.
':'.$i],
951 if (($event_end_dts < $day_start_dts) || ($event_start_dts > $day_end_dts)) {
955 $start_time = gmmktime(0, 0, 0, $event[
'start_date_mon'], $event[
'start_date_mday'], $event[
'start_date_year']);
956 $result_list[$assetid.
':'.$i] = Array();
957 for (reset($event); NULL !== ($k = key($event)); next($event)) {
958 $result_list[$assetid.
':'.$i][$k] = $event[$k];
961 $result_list[$assetid.
':'.$i] = array_merge($result_list[$assetid.
':'.$i],
969 if (!is_null($event[
'start_date_hours'])) {
971 if (($event_start_dts < $day_start_dts) || ($event_start_dts > $day_end_dts)) {
976 $start_time = gmmktime(0, 0, 0, $event[
'start_date_mon'], $event[
'start_date_mday'], $event[
'start_date_year']);
977 $result_list[$assetid.
':'.$i] = Array();
978 for (reset($event); NULL !== ($k = key($event)); next($event)) {
979 $result_list[$assetid.
':'.$i][$k] = $event[$k];
982 if (!is_null($event[
'start_date_hours'])) {
983 $result_list[$assetid.
':'.$i] = array_merge($result_list[$assetid.
':'.$i],
988 $start_date = gmdate(
'Y-m-d', $start_time);
989 $result_list[$assetid.
':'.$i] = array_merge($result_list[$assetid.
':'.$i],
1018 require_once dirname(__FILE__).
'/../calendar_events/calendar_event_recurring/calendar_event_recurring.inc';
1019 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'calendar_event_recurring');
1021 $event[
'expanded'] = 1;
1023 $day_start_bits = explode(
':', $day_start_time);
1024 $day_start_dts = ($day_start_bits[0] * 60) + $day_start_bits[1];
1025 $day_end_bits = explode(
':', $day_end_time);
1026 $day_end_dts = ($day_end_bits[0] * 60) + $day_end_bits[1];
1028 $no_evt_end_date_ds = is_null($event[
'end_date_ds']) || empty($event[
'end_date_ds']);
1029 $no_evt_start_date_hours = is_null($event[
'start_date_hours']) || empty($event[
'start_date_hours']);
1030 $no_evt_end_date_hours = is_null($event[
'end_date_hours']) || empty($event[
'end_date_hours']);
1031 $no_evt_start_date_hours = is_null($event[
'start_date_hours']) || empty($event[
'start_date_hours']);
1033 $event_start_date = sprintf(
'%04d-%02d-%02d %02d:%02d:00', $event[
'start_date_year'], $event[
'start_date_mon'], $event[
'start_date_mday'], $event[
'start_date_hours'], $event[
'start_date_minutes']);
1034 $event_end_date = $no_evt_end_date_ds ? NULL : sprintf(
'%04d-%02d-%02d %02d:%02d:00', $event[
'end_date_year'], $event[
'end_date_mon'], $event[
'end_date_mday'], $event[
'end_date_hours'], $event[
'end_date_minutes']);
1036 $event_start_dts = $no_evt_start_date_hours ? NULL : (($event[
'start_date_hours'] * 60) + $event[
'start_date_minutes']);
1037 $event_end_dts = $no_evt_end_date_hours ? NULL : (($event[
'end_date_hours'] * 60) + $event[
'end_date_minutes']);
1040 if ($no_evt_end_date_ds && !$no_evt_start_date_hours) {
1042 if (($event_start_dts < $day_start_dts) || ($event_start_dts > $day_end_dts)) {
1049 if (($event[
'stop_date'] !=
'----------') && ($event[
'stop_date'] < $last_day)) {
1051 $event_last_day = $event[
'stop_date'];
1054 $event_last_day = $last_day;
1058 switch ($event[
'frequency']{0}) {
1060 $first_occur = add_days_to_iso($first_day, -$event[
'period']);
1064 $first_occur = add_days_to_iso($first_day, -$event[
'period']*7);
1068 $first_occur = add_months_to_iso($first_day, -$event[
'period']).
'-'.date(
'd', strtotime($first_day));
1074 if (!is_null($event[
'end_date_ds'])) {
1075 $first_occur = add_days_to_iso($first_day, -($event[
'end_date_ds']-$event[
'start_date_ds']));
1082 while (($next_occur !== FALSE) && (substr($next_occur, 0, 10) <= $last_day)) {
1086 $difference = $next_occur_ds - $event[
'start_date_ds'];
1088 if (!$no_evt_end_date_ds) {
1090 if ($event[
'start_date_ds'] != $event[
'end_date_ds']) {
1093 $start_time = gmmktime(0, 0, 0, $event[
'start_date_mon'], $event[
'start_date_mday'], $event[
'start_date_year'])+$difference*86400;
1094 $start_date = date(
'Y-m-d', $start_time);
1096 $end_time = gmmktime(0, 0, 0, $event[
'end_date_mon'], $event[
'end_date_mday'], $event[
'end_date_year'])+$difference*86400;
1097 $end_date = date(
'Y-m-d', $end_time);
1099 if (($start_date >= $first_day) && ($start_date <= $last_day) && (is_null($event_start_dts) || ($event_start_dts <= $day_end_dts))) {
1101 $result_list[$assetid.
':'.$i] = Array();
1102 for (reset($event); NULL !== ($k = key($event)); next($event)) {
1103 $result_list[$assetid.
':'.$i][$k] = $event[$k];
1106 $result_list[$assetid.
':'.$i] = array_merge($result_list[$assetid.
':'.$i],
1115 for ($inter_time = $start_time + 86400; $inter_time < $end_time; $inter_time += 86400) {
1116 $inter_date = date(
'Y-m-d', $inter_time);
1117 if (($inter_date >= $first_day) && ($inter_date <= $last_day)) {
1119 $result_list[$assetid.
':'.$i] = Array();
1120 for (reset($event); NULL !== ($k = key($event)); next($event)) {
1121 $result_list[$assetid.
':'.$i][$k] = $event[$k];
1124 $result_list[$assetid.
':'.$i] = array_merge($result_list[$assetid.
':'.$i],
1128 $result_list[$assetid.
':'.$i][
'partition_start'] = $day_start_time;
1134 if (($end_date >= $first_day) && ($end_date <= $last_day) && (is_null($event_end_dts) || ($event_end_dts > $day_start_dts))) {
1136 $result_list[$assetid.
':'.$i] = Array();
1137 for (reset($event); NULL !== ($k = key($event)); next($event)) {
1138 $result_list[$assetid.
':'.$i][$k] = $event[$k];
1141 $result_list[$assetid.
':'.$i] = array_merge($result_list[$assetid.
':'.$i],
1145 $result_list[$assetid.
':'.$i][
'partition_start'] = $day_start_time;
1153 $start_time = gmmktime(0, 0, 0, $event[
'start_date_mon'], $event[
'start_date_mday'], $event[
'start_date_year'])+$difference*86400;
1154 $start_date = date(
'Y-m-d', $start_time);
1155 if (($start_date >= $first_day) && ($start_date <= $last_day) &&
1156 (is_null($event_start_dts) ||
1157 (($event_start_dts >= $day_start_dts) && ($event_start_dts < $day_end_dts)) ||
1158 (($event_end_dts <= $day_end_dts) && ($event_end_dts > $day_start_dts)) ||
1159 (($event_start_dts < $day_start_dts) && ($event_end_dts > $day_end_dts))
1162 $result_list[$assetid.
':'.$i] = Array();
1163 for (reset($event); NULL !== ($k = key($event)); next($event)) {
1164 $result_list[$assetid.
':'.$i][$k] = $event[$k];
1174 'start_date_hours',
'start_date_minutes',
'start_date_seconds',
1175 'end_date_hours',
'end_date_minutes',
'end_date_seconds',
1178 foreach ($date_vars as $date_var) {
1179 if (is_null($event[$date_var]) || empty($event[$date_var])) {
1180 $event_date_components[$date_var] = NULL;
1184 $result_list[$assetid.
':'.$i] = array_merge($result_list[$assetid.
':'.$i], $event_date_components);
1187 $event_date_components = NULL;
1196 $start_time = gmmktime(0, 0, 0, $event[
'start_date_mon'], $event[
'start_date_mday'], $event[
'start_date_year'])+$difference*86400;
1197 $start_date = date(
'Y-m-d', $start_time);
1199 if (($start_date >= $first_day) && ($start_date <= $last_day)) {
1200 $result_list[$assetid.
':'.$i] = Array();
1201 for (reset($event); NULL !== ($k = key($event)); next($event)) {
1202 $result_list[$assetid.
':'.$i][$k] = $event[$k];
1205 if (!is_null($event[
'start_date_hours'])) {
1206 $result_list[$assetid.
':'.$i] = array_merge($result_list[$assetid.
':'.$i],
1210 $result_list[$assetid.
':'.$i] = array_merge($result_list[$assetid.
':'.$i],
1237 if (is_numeric($value)) {
1239 return floor($value / 86400);
1242 $value = strtotime(substr($value, 0, 10).
' 00:00:00 Z') - strtotime(
'1970-01-01 00:00:00 Z');
1243 return floor($value / 86400);
1259 if (is_numeric($value)) {
1261 $value = ts_iso8601($value);
1263 list($year, $month) = sscanf($value,
'%04d-%02d');
1264 return ($year * 12) + $month;
1283 $type_codes = $GLOBALS[
'SQ_SYSTEM']->am->getTypeDescendants($type_code);
1284 $type_codes[] = $type_code;
1286 foreach ($type_codes as $tc_key => $tc_value) {
1307 $frequency_threshold = $GLOBALS[
'SQ_SYSTEM']->getUserPrefs(
'calendar_event_recurring',
'SQ_CALENDAR_EVENTS_FREQUENCY');
1309 if ($frequency_threshold <= 0) {
1312 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'calendar_event_recurring');
1314 $limit_events = Array();
1315 foreach($events as $event) {
1316 $recurring_descandants = $GLOBALS[
'SQ_SYSTEM']->am->getTypeDescendants(
'calendar_event_recurring');
1317 if (($event[
'type_code'] ==
'calendar_event_recurring') || in_array($event[
'type_code'], $recurring_descandants)) {
1319 if ($event_frequency < $frequency_threshold) {
1320 $limit_events[$event[
'assetid']] = 1;
1327 foreach($instances as $key => $instance) {
1329 if (isset($limit_events[$instance[
'assetid']]) && $key != $instance[
'assetid'].
':0') {
1330 unset($instances[$key]);
1353 $frequency_limit = $GLOBALS[
'SQ_SYSTEM']->getUserPrefs(
'calendar_event_recurring',
'SQ_CALENDAR_EVENTS_FREQUENCY');
1355 $multi_date_event = Array();
1356 foreach ($events as $key => $event) {
1357 $assetid = $event[
'assetid'];
1361 foreach ($events as $key => $event) {
1362 $assetid = $event[
'assetid'];
1363 $recurring_descandants = $GLOBALS[
'SQ_SYSTEM']->am->getTypeDescendants(
'calendar_event_recurring');
1364 if (($event[
'type_code'] ==
'calendar_event_recurring') || in_array($event[
'type_code'], $recurring_descandants)) {
1393 if (empty($limit_start_day ) || empty($limit_end_day)) {
1398 $start_day = $event[
'start_date_year'].
'-'.$event[
'start_date_mon'].
'-'.$event[
'start_date_mday'];
1399 $start_time = (is_null($event[
'start_date_hours']) ?
'00' : $event[
'start_date_hours']).
':'.(is_null($event[
'start_date_minutes']) ?
'00' : $event[
'start_date_minutes']).
':00';
1400 $start_ts = strtotime($start_day.
' '.$start_time);
1401 $start_time_offset = ((is_null($event[
'start_date_hours']) ? 0 : $event[
'start_date_hours'])*3600)+((is_null($event[
'start_date_minutes']) ? 0 : $event[
'start_date_minutes'])*60);
1404 if (!is_null($event[
'end_date_year'])) {
1405 $end_day = $event[
'end_date_year'].
'-'.$event[
'end_date_mon'].
'-'.$event[
'end_date_mday'];
1406 $end_time = (is_null($event[
'end_date_hours']) ?
'23' : $event[
'end_date_hours']).
':'.(is_null($event[
'end_date_minutes']) ?
'59' : $event[
'end_date_minutes']).
':00';
1407 if (strpos($end_time,
'24:') !== FALSE) $end_time =
'23:59:00';
1408 $end_ts = strtotime($end_day.
' '.$end_time);
1410 $end_day = $start_day;
1411 $end_time =
'23:59:00';
1412 $end_ts = $start_ts;
1415 $limit_start_ts = strtotime($limit_start_day.
' '.$limit_start_time);
1416 $limit_end_ts = strtotime($limit_end_day.
' '.$limit_end_time);
1419 if (($start_ts >= $limit_start_ts && $start_ts <= $limit_end_ts ) || ($start_ts <= $limit_start_ts && $end_ts >= $limit_start_ts )) {
1421 $type_code = $event[
'type_code'];
1424 $result[$event[
'assetid'].
':start_date_ts='.$start_ts.
',end_date_ts='.$end_ts.
',type_code='.$type_code] = $start_ts;
1426 $result_list = array_merge($result_list, $result);
1451 $start_day = $event[
'start_date_year'].
'-'.str_pad($event[
'start_date_mon'],2,
'0',STR_PAD_LEFT).
'-'.str_pad($event[
'start_date_mday'],2,
'0',STR_PAD_LEFT);
1452 $start_time = (is_null($event[
'start_date_hours']) ?
'00' : $event[
'start_date_hours']).
':'.(is_null($event[
'start_date_minutes']) ?
'00' : $event[
'start_date_minutes']).
':00';
1455 if (!is_null($event[
'end_date_year'])) {
1456 $end_day = $event[
'end_date_year'].
'-'.str_pad($event[
'end_date_mon'],2,
'0',STR_PAD_LEFT).
'-'.str_pad($event[
'end_date_mday'],2,
'0',STR_PAD_LEFT);
1457 $end_time = (is_null($event[
'end_date_hours']) ?
'23' : $event[
'end_date_hours']).
':'.(is_null($event[
'end_date_minutes']) ?
'59' : $event[
'end_date_minutes']).
':00';
1458 if (strpos($end_time,
'24:') !== FALSE) $end_time =
'23:59:00';
1460 $end_day = $start_day;
1461 $end_time =
'23:59:00';
1464 $event_period_ts = strtotime($end_day.
' '.$end_time) - strtotime($start_day.
' '.$start_time);
1465 if ($event_period_ts < 0)
return;
1467 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'calendar_event_recurring');
1469 $after = $first_day;
1470 $type_code = $event[
'type_code'];
1474 $stop_day = $event[
'stop_date'] ==
'----------' ? $last_day : $event[
'stop_date'];
1478 if ($next_occurance > $last_day)
break;
1480 if ($next_occurance) {
1481 $i_start_ts = strtotime($next_occurance.
' '.$start_time);
1482 $i_end_ts = $i_start_ts+$event_period_ts;
1483 $result_list[$assetid.
':start_date_ts='.$i_start_ts.
',end_date_ts='.$i_end_ts.
',type_code='.$type_code] = $i_start_ts;
1486 if ($event_frequency < $frequency_threshold) {
1490 $after = add_days_to_iso($next_occurance);
1492 }
while($next_occurance);