17 require_once SQ_CORE_PACKAGE_PATH.
'/page/page.inc';
18 require_once SQ_FUDGE_PATH.
'/general/text.inc';
19 require_once SQ_FUDGE_PATH.
'/general/datetime.inc';
20 require_once SQ_PACKAGES_PATH.
'/calendar/lib/calendar_common.inc';
42 var $bodycopies = Array(
44 'name' =>
'Page Contents',
46 'content' =>
'%event_list%',
50 'name' =>
'Time Unit Format',
52 'content' =>
"<h3>%time_unit_start_datetime_Y-m-d%</h3>\n%time_unit_event_list%",
66 $this->_ser_attrs = TRUE;
67 parent::__construct($assetid);
83 if (!parent::_createAdditional($link))
return FALSE;
85 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
89 'value' =>
'type_formats',
90 'link_type' => SQ_LINK_TYPE_2,
95 $am->includeAsset(
'folder');
96 $am->includeAsset(
'bodycopy');
99 $type_formats_folder =
new Folder();
100 $type_formats_folder->setAttrValue(
'name',
'Type Formats');
101 if (!$type_formats_folder->create($link))
return FALSE;
104 $tf_link = Array(
'asset' => &$type_formats_folder,
'link_type' => SQ_LINK_TYPE_2,
'is_dependant' => 1,
'is_exclusive' => 0);
105 foreach (Array(
'calendar_event_single',
'calendar_event_recurring') as $event_type) {
106 $tf_link[
'value'] = $event_type;
108 'content' =>
'%asset_name% (%event_start_date%)',
111 $format_bc->setAttrValue(
'name', $am->getTypeInfo($event_type,
'name').
' Format');
112 if (!$format_bc->create($tf_link, $content)) {
119 foreach ($this->bodycopies as $value => $details) {
122 'link_type' => SQ_LINK_TYPE_2,
129 $bodycopy->setAttrValue(
'name', array_get_index($details,
'name'));
130 if (!$bodycopy->create($copy_link, array_get_index($details,
'content'))) {
149 $page_links = parent::_getAllowedLinks();
150 $page_links[SQ_LINK_TYPE_2][
'bodycopy'] = Array(
'card' => 3,
'exclusive' => FALSE);
151 $page_links[SQ_LINK_TYPE_3][
'bodycopy'] = Array(
'card' => 1,
'exclusive' => FALSE);
169 if (!isset($vars[
'keywords'])) {
170 $vars[
'keywords'] = Array();
173 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
175 $links = $am->getLinks($this->
id, SQ_LINK_TYPE_2, Array(
'bodycopy',
'folder'), TRUE,
'major', NULL, TRUE);
176 $broadcaster_parents = $GLOBALS[
'SQ_SYSTEM']->am->getParents($broadcaster->id);
179 $tf_folder = $this->
getFolder(
'type_formats');
180 if (isset($broadcaster_parents[$tf_folder->id])) {
181 $tf_links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($tf_folder->id, SQ_LINK_TYPE_2,
'bodycopy', TRUE,
'major');
183 foreach ($tf_links as $link) {
184 $tf_bcs[$link[
'minorid']] = $link[
'value'];
186 $bc_id = reset(array_intersect(array_keys($tf_bcs), array_keys($broadcaster_parents)));
188 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset($tf_bcs[$bc_id]);
189 $dummy_asset =
new $tf_bcs[$bc_id];
190 $asset_keywords = $dummy_asset->getAvailableKeywords();
191 foreach ($asset_keywords as $kw => $desc) {
192 $vars[
'keywords'][$kw] = $desc;
201 $format_bodycopy = FALSE;
202 $format_bodycopies_links = $am->getLinks($this->
id, SQ_LINK_TYPE_2,
'bodycopy', TRUE);
203 foreach ($format_bodycopies_links as $link) {
204 if (isset($broadcaster_parents[$link[
'minorid']])) {
205 $format_bodycopy = $link[
'value'];
209 switch ($format_bodycopy) {
211 $vars[
'keywords'][
'event_list'] = translate(
'cal_rolling_keyword_event_list');
215 $vars[
'keywords'][
'time_unit_event_list'] = translate(
'cal_rolling_keyword_time_unit_event_list', $this->
attr(
'time_unit'));
216 $vars[
'keywords'][
'time_unit_start_datetime_xxxx'] = translate(
'cal_rolling_keyword_time_unit_start_datetime_xxxx');
217 $vars[
'keywords'][
'time_unit_end_datetime_xxxx'] = translate(
'cal_rolling_keyword_time_unit_end_datetime_xxxx');
236 $cached_contents =
'';
238 if (!empty($cache_key)) {
239 $cm = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'cache_manager');
240 $cached_contents = $cm->loadFromCache($this->
id, $this->
type(), $cache_key);
243 if ($cached_contents !== FALSE) {
244 echo $cached_contents;
247 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
249 $page_links = $am->getLinks($this->
id, SQ_LINK_TYPE_2, Array(
'folder',
'bodycopy'), TRUE,
'major', NULL, TRUE);
250 $tf_folder_id = NULL;
251 $pb_bodycopy_id = NULL;
253 foreach ($page_links as $link) {
254 switch ($link[
'minor_type_code']) {
256 $this->_tmp[
'tf_folder_id'] = $link[
'minorid'];
259 if ($link[
'value'] ==
'') {
260 $pb_bodycopy_id = $link[
'minorid'];
265 if (is_null($this->_tmp[
'tf_folder_id'])) {
266 trigger_localised_error(
'CAL0059', E_USER_WARNING, $this->
id);
269 if (is_null($pb_bodycopy_id)) {
270 trigger_localised_error(
'CAL0060', E_USER_WARNING, $this->
id);
275 $pb = $am->getAsset($pb_bodycopy_id,
'bodycopy');
277 $calendar_replacements = $replacements;
278 $keywords = $pb->getKeywords();
279 foreach ($keywords as $word) {
280 if (isset($calendar_replacements[$word]) && !empty($calendar_replacements[$word])) {
281 $replacements[$word] = $calendar_replacements[$word];
286 $pb->setKeywordReplacements($replacements);
289 if (!empty($cache_key)) {
290 $cm->saveToCache($this->
id, $this->
type(), $cache_key, ob_get_contents());
317 return parent::getKeywordReplacement($keyword);
331 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
335 if ($this->
type() ==
'page_calendar_rolling') {
336 $from_date = $this->
attr(
'duration_from');
337 $to_date = $this->
attr(
'duration_to');
338 $day_filter = $this->
attr(
'day_filter');
339 $time_unit = $this->
attr(
'time_unit');
342 $cache_key .= $from_date;
343 $cache_key .=
':'.$to_date;
344 $cache_key .=
':'.$time_unit;
345 $cache_key .=
':'.$day_filter;
346 }
else if ($this->
type() ==
'page_calendar_restricted') {
347 $from_date = $this->
attr(
'start_date');
348 $to_date = $this->
attr(
'end_date');
350 $cache_key .= $from_date;
353 $end_date = substr($to_date, 0, 10);
354 if (!($end_date ==
'----------')) {
355 $cache_key .=
':'.$to_date;
359 if ($cache_key !==
'') {
360 $cache_key .=
'-ctx'.$contextid;
376 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
379 $type_format_links = $am->getLinks($this->_tmp[
'tf_folder_id'], SQ_LINK_TYPE_2,
'bodycopy', TRUE,
'major', NULL, TRUE);
380 $type_formats = Array();
381 $needs_asset = Array();
382 $somebody_doesnt_need_asset = FALSE;
383 $metadata_keywords = Array();
384 $asset_info_fields = Array(
401 foreach ($type_format_links as $link) {
402 if (!empty($link[
'value'])) {
403 $needs_asset[$link[
'value']] = FALSE;
404 $metadata_keywords[$link[
'value']] = Array();
405 $bc = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid']);
406 $type_formats[$link[
'value']] = $bc->getRawBodycopyContent();
407 $required_keywords[$link[
'value']] = $bc->getKeywords();
408 foreach ($required_keywords[$link[
'value']] as $keyword) {
409 if (strpos($keyword,
'asset_') !== 0) {
410 $needs_asset[$link[
'value']] = TRUE;
413 if ((strpos($keyword,
'asset_metadata_') !== 0) && (!in_array(substr($keyword, 6), $asset_info_fields))) {
414 $needs_asset[$link[
'value']] = TRUE;
417 if (strpos($keyword,
'asset_metadata_') === 0) {
418 $metadata_keywords[$link[
'value']][] = $keyword;
421 if (!$needs_asset[$link[
'value']]) {
422 $somebody_doesnt_need_asset = TRUE;
429 $all_events = $this->
_getEvents(NULL, NULL, $time);
432 if (!empty($all_events)) {
434 foreach ($all_events as $day => $day_event_list) {
435 $all_ids = array_merge($all_ids, array_keys($day_event_list));
437 if ($somebody_doesnt_need_asset) {
438 $asset_infos = $am->getAssetInfo($all_ids);
442 $events_by_time_unit = Array();
443 $start_date = reset(array_keys($all_events));
444 $start_time = iso8601_ts($start_date);
445 switch ($this->
attr(
'time_unit')) {
447 $current_year = date(
'Y', $time);
448 $current_month = date(
'm', $time);
450 foreach ($all_events as $event_date => $events) {
451 $event_time = iso8601_ts($event_date);
452 $event_year = date(
'Y', $event_time);
453 $event_month = date(
'm', $event_time);
455 $difference = ($event_month - $current_month) + (($event_year - $current_year) * 12);
456 $events_by_time_unit[$difference][$event_date] = $events;
462 $current_year = date(
'Y', $time);
466 $current_week = date(
'W', strtotime(date(
'Y-m-d', $time).
' +1 day'));
468 foreach ($all_events as $event_date => $events) {
469 $event_time = iso8601_ts($event_date);
470 $event_year = date(
'Y', $event_time);
473 $event_week = date(
'W', strtotime(date(
'Y-m-d', $event_time).
' +1 day'));
475 $difference = ($event_week - $current_week) + (($event_year - $current_year) * 52);
476 $events_by_time_unit[$difference][$event_date] = $events;
482 foreach ($all_events as $event_date => $events) {
483 $difference = days_between_isos($event_date, date(
'Y-m-d', $time));
484 $events_by_time_unit[$difference][$event_date] = $events;
490 $unit_bodycopy_link = $am->getLink($this->
id, SQ_LINK_TYPE_2,
'bodycopy', TRUE,
'time_unit');
491 if (empty($unit_bodycopy_link))
return FALSE;
493 $unit_bodycopy = $am->getAsset($unit_bodycopy_link[
'minorid'], $unit_bodycopy_link[
'minor_type_code']);
494 if (is_null($unit_bodycopy))
return FALSE;
496 $single_event_descendants = $am->getTypeDescendants(
'calendar_event_single', TRUE);
497 $recurring_event_descendants = $am->getTypeDescendants(
'calendar_event_recurring', TRUE);
501 $orig_date_param =
'';
502 if (isset($_REQUEST[
'SQ_CALENDAR_DATE'])) {
503 $orig_date_param = $_REQUEST[
'SQ_CALENDAR_DATE'];
506 foreach ($events_by_time_unit as $unit_count => $events_by_date) {
510 foreach ($events_by_date as $date => $events) {
511 $_REQUEST[
'SQ_CALENDAR_DATE'] = $date;
514 foreach ($events as $id => $event) {
516 $format_type = $event[
'type_code'];
520 if (!isset($type_formats[$format_type])) {
521 if (in_array($format_type, $single_event_descendants)) {
522 $format_type =
'calendar_event_single';
523 }
else if (in_array($format_type, $recurring_event_descendants) || $format_type ==
'calendar_event_modification') {
524 $format_type =
'calendar_event_recurring';
530 if (!isset($type_formats[$format_type]))
continue;
533 if ($needs_asset[$format_type]) {
534 $asset = $am->getAsset($id);
535 foreach ($required_keywords[$format_type] as $keyword) {
536 if ($keyword ==
'asset_contents') {
539 $replacements[$keyword] = ob_get_contents();
542 $replacements[$keyword] = $asset->getKeywordReplacement($keyword);
547 foreach ($asset_infos[$id] as $field => $value) {
548 $replacements[
'asset_'.$field] = $value;
550 $replacements[
'asset_assetid'] = $id;
554 if (!empty($metadata_keywords[$event[
'type_code']])) {
556 $mm = $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
559 $metadata_values = $mm->getMetadataFieldValues($id, array_values($metadata_keywords[$event[
'type_code']]));
561 foreach ($metadata_values as $field => $value) {
562 $replacements[
'asset_metadata_'.$field] = $value;
566 $type_format = $type_formats[$format_type];
567 $unit_contents .= replace_keywords($type_format, $replacements);
573 $unit_keywords = $unit_bodycopy->getKeywords();
574 foreach ($unit_keywords as $keyword) {
576 case 'time_unit_event_list':
577 $replacements[$keyword] = $unit_contents;
583 preg_match(
'/^time_unit_(start|end)_datetime_(.+)/', $keyword, $matches);
584 if ($matches != NULL) {
585 $start_end = $matches[1];
586 $date_format = $matches[2];
588 $time_unit = $this->
attr(
'time_unit');
591 $replacements[$keyword] = date($date_format, strtotime($start_end_dates[$start_end]));
599 $unit_bodycopy->setKeywordReplacements($replacements);
600 $unit_bodycopy->printBody();
601 $event_list .= ob_get_clean();
606 if ($orig_date_param !=
'') {
607 $_REQUEST[
'SQ_CALENDAR_DATE'] = $orig_date_param;
609 unset($_REQUEST[
'SQ_CALENDAR_DATE']);
612 $no_result_link = $am->getLink($this->
id, SQ_LINK_TYPE_2,
'bodycopy', TRUE,
'no_results');
614 if (empty($no_result_link))
return FALSE;
616 $no_result_bc = $am->getAsset($no_result_link[
'minorid'], $no_result_link[
'minor_type_code']);
618 if (is_null($no_result_bc))
return FALSE;
620 $keywords = $no_result_bc->getKeywords();
622 $replacements = Array();
625 foreach ($keywords as $keyword) {
629 $no_result_bc->setKeywordReplacements($replacements);
631 $no_result_bc->printBody();
632 $event_list = ob_get_clean();
636 return Array(
'event_list' => $event_list);
655 if (!is_int($time))
return FALSE;
658 $time = strtotime(date(
'Y-m-d', $time).
' '.($modifier < 0 ? $modifier :
'+'.$modifier).
' '.$time_unit);
661 preg_match(
'/(day|week|month)([s]?)/', $time_unit, $matches);
662 if (empty($matches))
return FALSE;
663 $unit = array_get_index($matches, 1);
669 $tmp_start = mktime(0, 0, 0, date(
'm', $time), 1, date(
'y', $time));
670 $start = date(
'Y-m-d', $tmp_start);
671 $end = date(
'Y-m-d', strtotime($start.
' +1 month -1 day'));
676 $day_of_week = date(
'w', $time);
677 $start = date(
'Y-m-d', strtotime(date(
'Y-m-d', $time).
' -'.$day_of_week.
' days'));
678 $end = date(
'Y-m-d', strtotime($start.
' +6 days'));
684 $start = date(
'Y-m-d', $time);
708 function _getEvents($start_date=NULL, $end_date=NULL, $time=NULL)
711 if (is_null($time)) $time = time();
712 if (is_null($start_date)) {
713 $unit = $this->
attr(
'time_unit');
714 $from = $this->
attr(
'duration_from');
715 $to = $this->
attr(
'duration_to');
727 $start_date = $from_dates[
'start'];
728 $end_date = $to_dates[
'end'];
730 $length = round((iso8601_ts($end_date) - iso8601_ts($start_date)) / (60 * 60 * 24));
733 $root_nodes = $this->
attr(
'root_nodes');
734 if (empty($root_nodes)) {
735 trigger_localised_error(
'CAL0028', E_USER_WARNING);
740 $bind_vars = Array();
742 $end_date_stamp = $start_date_stamp + $length;
743 $date_sql =
'(cd.start_date_ds BETWEEN :start_date_ds AND :end_date_ds) OR (cd.start_date_ds < :start_date_ds_1 AND cd.end_date_ds >= :start_date_ds_2)';
746 $single_result = Array();
749 foreach($bind_vars as $bind_value => $bind_var) {
752 MatrixDAL::bindValueToPdO($query,
'start_date_ds', $start_date_stamp, PDO::PARAM_INT);
753 MatrixDAL::bindValueToPdO($query,
'start_date_ds_1', $start_date_stamp, PDO::PARAM_INT);
754 MatrixDAL::bindValueToPdO($query,
'start_date_ds_2', $start_date_stamp, PDO::PARAM_INT);
755 MatrixDAL::bindValueToPdO($query,
'end_date_ds', $end_date_stamp, PDO::PARAM_INT);
757 }
catch (Exception $e) {
758 throw new Exception($e->getMessage());
764 $bind_vars = Array();
767 $recur_result = Array();
770 foreach($bind_vars as $bind_value => $bind_var) {
774 }
catch (Exception $e) {
775 throw new Exception($e->getMessage());
780 $all_events = $single_result + $recur_result;
786 $index = $start_date;
787 $res[$index] = Array();
788 while ($index < $end_date) {
789 $index = date(
'Y-m-d', strtotime($index.
' +1 day'));
790 $res[$index] = Array();
793 foreach ($all_events as $id => $event) {
794 $event_date = sprintf(
'%04d-%02d-%02d', $event[
'start_date_year'], $event[
'start_date_mon'], $event[
'start_date_mday']);
795 $res[$event_date][strtok($id,
':')] = $event;
799 $day_filter = explode(
'|', $this->
attr(
'day_filter'));
800 foreach ($res as $day => $day_events) {
801 if (!in_array(date(
'D', strtotime($day)), $day_filter)) {
805 uasort($res[$day], Array(
'Calendar_Common',
'compareStartDates'));
826 if (!isset($this->_tmp[
'formats'][$type])) {
828 $format_links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($folder->id, SQ_LINK_TYPE_2,
'bodycopy', TRUE);
829 if (empty($format_links))
return Array();
832 foreach ($format_links as $link_data) {
833 $formats[$link_data[
'minorid']] = $link_data[
'value'];
835 $this->_tmp[
'formats'][$type] = $formats;
837 return $this->_tmp[
'formats'][$type];
853 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($this->
id, SQ_LINK_TYPE_2,
'folder', TRUE, $type);
854 if (empty($link))
return $null;
856 $folder = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid'], $link[
'minor_type_code']);
857 if (is_null($folder))
return $null;
879 $bodycopy_links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($this->
id, SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3,
'bodycopy', FALSE,
'major',
'no_results');
880 $bodycopy_link = reset($bodycopy_links);
883 if ($bodycopy_link) {
886 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'bodycopy');
888 $link_type = ($enable_on_create ? SQ_LINK_TYPE_2 : SQ_LINK_TYPE_3);
893 'value' =>
'no_results',
894 'link_type' => $link_type,
899 $asset->setAttrValue(
'name',
'Page Contents (No Results)');
900 $args = Array(
'content' =>
'No events found');
901 if (!$asset->create($copy_link, $args))
return FALSE;
903 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($asset);
926 if ($only_if_enabled) {
927 $link_types = SQ_LINK_TYPE_2;
929 $link_types = SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3;
932 $tmp_bodycopy_link = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($this->
id, $link_types,
'bodycopy', FALSE,
'major',
'no_results');
933 $bodycopy_link = reset($tmp_bodycopy_link);
935 if ($bodycopy_link) {
936 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($bodycopy_link[
'minorid'],
'bodycopy');
952 $link_types = SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3;
954 $bodycopy_links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($this->
id, $link_types,
'bodycopy', FALSE,
'major',
'no_results');
955 $bodycopy_link = reset($bodycopy_links);
957 if ($bodycopy_link) {
958 return ($bodycopy_link[
'link_type'] == SQ_LINK_TYPE_2);
978 $tmp_body_link = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($this->
id, SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3,
'bodycopy', FALSE,
'major',
'no_results');
979 $bodycopy_link = reset($tmp_body_link);
981 if (!$bodycopy_link) {
989 $new_link_type = ($enabled) ? SQ_LINK_TYPE_2 : SQ_LINK_TYPE_3;
990 if ($bodycopy_link[
'link_type'] != $new_link_type) {
991 $GLOBALS[
'SQ_SYSTEM']->am->updateLink($bodycopy_link[
'linkid'], $new_link_type);