17 require_once SQ_CORE_PACKAGE_PATH.
'/page/page.inc';
18 require_once SQ_FUDGE_PATH.
'/general/text.inc';
19 require_once SQ_PACKAGES_PATH.
'/calendar/lib/calendar_common.inc';
44 $this->_ser_attrs = TRUE;
45 parent::__construct($assetid);
61 if (!parent::_createAdditional($link))
return FALSE;
63 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
66 $am->includeAsset(
'bodycopy');
68 $page_contents_bc->setAttrValue(
'name',
'Page Contents');
69 $link = Array(
'asset' => &$this,
'value' =>
'page_contents',
'link_type' => SQ_LINK_TYPE_2,
'is_dependant' => 1,
'is_exclusive' => 1);
70 if (!$page_contents_bc->create($link))
return FALSE;
73 $am->includeAsset(
'folder');
74 $type_formats_folder =
new Folder();
75 $type_formats_folder->setAttrValue(
'name',
'Type Formats');
76 $link = Array(
'asset' => &$this,
'value' =>
'type_formats',
'link_type' => SQ_LINK_TYPE_2,
'is_dependant' => 1,
'is_exclusive' => 1);
77 if (!$type_formats_folder->create($link))
return FALSE;
80 $tf_link = Array(
'asset' => &$type_formats_folder,
'link_type' => SQ_LINK_TYPE_2,
'is_dependant' => 1,
'is_exclusive' => 0);
81 foreach (Array(
'calendar_event_single',
'calendar_event_recurring') as $event_type) {
82 $tf_link[
'value'] = $event_type;
84 $format_bc->setAttrValue(
'name', $am->getTypeInfo($event_type,
'name').
' Format');
85 if (!$format_bc->create($tf_link))
return FALSE;
103 $page_links = parent::_getAllowedLinks();
104 $page_links[SQ_LINK_TYPE_2][
'bodycopy'] = Array(
'card' => 1,
'exclusive' => TRUE);
122 if (!isset($vars[
'keywords'])) {
123 $vars[
'keywords'] = Array();
125 $tf_folder = $this->
getFolder(
'type_formats');
126 $broadcaster_parents = $GLOBALS[
'SQ_SYSTEM']->am->getParents($broadcaster->id);
127 if (isset($broadcaster_parents[$tf_folder->id])) {
128 $tf_links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($tf_folder->id, SQ_LINK_TYPE_2,
'bodycopy', TRUE,
'major');
130 foreach ($tf_links as $link) {
131 $tf_bcs[$link[
'minorid']] = $link[
'value'];
133 $bc_id = reset(array_intersect(array_keys($tf_bcs), array_keys($broadcaster_parents)));
135 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset($tf_bcs[$bc_id]);
136 $dummy_asset =
new $tf_bcs[$bc_id];
137 $asset_keywords = $dummy_asset->getAvailableKeywords();
138 foreach ($asset_keywords as $kw => $desc) {
139 $vars[
'keywords'][$kw] = $desc;
143 }
else if (isset($broadcaster_parents[$this->
id])) {
145 $vars[
'keywords'][
'event_list'] =
'List of Events';
162 $cached_contents =
'';
164 $_REQUEST[
'SQ_CALENDAR_DATE'] = date(
'Y-m-d');
166 if (!empty($cache_key)) {
167 $cm = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'cache_manager');
168 $cached_contents = $cm->loadFromCache($this->
id, $this->
type(), $cache_key);
171 if ($cached_contents !== FALSE) {
172 echo $cached_contents;
175 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
177 $page_links = $am->getLinks($this->
id, SQ_LINK_TYPE_2, Array(
'folder',
'bodycopy'), TRUE,
'major', NULL, TRUE);
178 $tf_folder_id = NULL;
179 $pb_bodycopy_id = NULL;
180 foreach ($page_links as $link) {
181 switch ($link[
'minor_type_code']) {
183 $this->_tmp[
'tf_folder_id'] = $link[
'minorid'];
186 $pb_bodycopy_id = $link[
'minorid'];
190 if (is_null($this->_tmp[
'tf_folder_id'])) {
191 trigger_localised_error(
'CAL0059', E_USER_WARNING, $this->
id);
194 if (is_null($pb_bodycopy_id)) {
195 trigger_localised_error(
'CAL0060', E_USER_WARNING, $this->
id);
200 $pb = $am->getAsset($pb_bodycopy_id,
'bodycopy');
202 $calendar_replacements = $replacements;
203 $keywords = $pb->getKeywords();
204 foreach ($keywords as $word) {
205 if (isset($calendar_replacements[$word]) && !empty($calendar_replacements[$word])) {
206 $replacements[$word] = $calendar_replacements[$word];
211 $pb->setKeywordReplacements($replacements);
214 if (!empty($cache_key)) {
215 $cm->saveToCache($this->
id, $this->
type(), $cache_key, ob_get_contents());
242 return parent::getKeywordReplacement($keyword);
256 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
258 $num_events = $this->
attr(
'num_events');
259 $horizon = $this->
attr(
'horizon');
262 $cache_key .= $num_events;
263 $cache_key .=
':'.$horizon;
265 $cache_key .=
'-ctx'.$contextid;
280 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
281 $type_format_links = $am->getLinks($this->_tmp[
'tf_folder_id'], SQ_LINK_TYPE_2,
'bodycopy', TRUE,
'major', NULL, TRUE);
282 $type_formats = Array();
283 $needs_asset = Array();
284 $somebody_doesnt_need_asset = FALSE;
285 $metadata_keywords = Array();
286 $asset_info_fields = Array(
303 foreach ($type_format_links as $link) {
304 if (!empty($link[
'value'])) {
305 $needs_asset[$link[
'value']] = FALSE;
306 $metadata_keywords[$link[
'value']] = Array();
307 $bc = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid']);
308 $type_formats[$link[
'value']] = $bc->getRawBodycopyContent();
309 $required_keywords[$link[
'value']] = $bc->getKeywords();
310 foreach ($required_keywords[$link[
'value']] as $keyword) {
311 if (strpos($keyword,
'asset_') !== 0) {
312 $needs_asset[$link[
'value']] = TRUE;
315 if ((strpos($keyword,
'asset_metadata_') !== 0) && (!in_array(substr($keyword, 6), $asset_info_fields))) {
316 $needs_asset[$link[
'value']] = TRUE;
319 if (strpos($keyword,
'asset_metadata_') === 0) {
320 $metadata_keywords[$link[
'value']][] = $keyword;
323 if (!$needs_asset[$link[
'value']]) {
324 $somebody_doesnt_need_asset = TRUE;
333 $tf_folder = $this->
getFolder(
'type_formats');
334 $tf_links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($tf_folder->id, SQ_LINK_TYPE_2,
'bodycopy', TRUE,
'major');
336 $types_available = Array();
337 foreach ($tf_links as $link) {
338 $type = $link[
'value'];
340 $parent_types = $GLOBALS[
'SQ_SYSTEM']->am->getTypeAncestors($type, FALSE);
341 foreach ($parent_types as $parent_type) {
342 $types_available[$parent_type][] = $type;
346 $multi_date_events = Array();
347 $multi_date_types = Array(
'calendar_event_multi_date');
348 if (isset($types_available[
'calendar_event_multi_date'])) {
349 foreach ($types_available[
'calendar_event_multi_date'] as $multi_date_type) {
350 $multi_date_types[] = $multi_date_type;
354 foreach ($events as $event) {
355 $event_type_code = $event[
'type_code'];
356 if (($event_type_code ==
'calendar_event_single') || ($event_type_code ==
'calendar_event_recurring')) {
357 $event_parents = $GLOBALS[
'SQ_SYSTEM']->am->getParents($event[
'assetid'], $multi_date_types);
358 foreach ($event_parents as $parent_id => $event_parent) {
359 if (!isset($multi_date_events[$parent_id])) {
360 $multi_date_events[$parent_id] = 1;
367 $events_data = Array();
371 foreach ($multi_date_events as $multi_date_event_id => $value_set) {
372 $asset = $am->getAsset($multi_date_event_id);
374 $type_code = $asset->type();
375 $format_type = $asset->type();
377 if (isset($required_keywords[$format_type])) {
378 foreach ($required_keywords[$format_type] as $keyword) {
379 if ($keyword ==
'asset_contents') {
382 $replacements[$keyword] = ob_get_contents();
385 $replacements[$keyword] = $asset->getKeywordReplacement($keyword);
391 if (!empty($metadata_keywords[$type_code])) {
392 if (!empty($metadata_keywords)) {
394 $mm = $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
397 $metadata_values = $mm->getMetadataFieldValues($id, $metadata_keywords[$type_code]);
399 foreach ($metadata_values as $field => $value) {
400 $replacements[
'asset_metadata_'.$field] = $value;
405 if (isset($required_keywords[$format_type])) {
406 $entry = $type_formats[$format_type];
408 replace_keywords($entry, $replacements);
409 $event_list .= $entry;
415 if ($somebody_doesnt_need_asset) {
417 foreach ($events as $key => $value) {
418 $key_assetid = strtok($key,
':');
419 $keys[$key_assetid] = $key_assetid;
421 $asset_infos = $am->getAssetInfo($keys);
424 $recurring_event_descendants = array_merge($am->getTypeDescendants(
'calendar_event_recurring', TRUE), Array(
'calendar_event_modification'));
426 foreach($events as $id => $event) {
427 $events[$id][
'current_date'] = $_REQUEST[
'SQ_CALENDAR_DATE'];
430 foreach ($events as $id => $event) {
432 $id = strtok($id,
':');
433 $replacements = Array();
438 if (in_array($event[
'type_code'], $types_available)) {
439 $format_type = $event_type_code;
441 $format_type =
'calendar_event_single';
443 if (in_array($event[
'type_code'], $recurring_event_descendants)) {
444 $format_type =
'calendar_event_recurring';
448 $asset = $am->getAsset($id);
449 $event_occurance = $event[
'start_date_ts'];
450 if ($asset && $asset->type() ==
'calendar_event_recurring') {
452 $current_date = $events[$shadow_id][
'current_date'];
455 if ($asset->hasOccurrenceOnDate($current_date)) {
456 $current_date = add_days_to_iso($current_date, 1);
458 $next_occurance = $asset->getFirstOccurrenceAfter($current_date);
459 if ($next_occurance === FALSE)
continue;
461 foreach($events as $temp_id => $temp_event) {
462 $original_id = strtok($temp_id,
':');
463 if ($original_id == $id) {
464 $events[$temp_id][
'current_date'] = $next_occurance;
465 $_REQUEST[
'SQ_CALENDAR_DATE'] = $next_occurance;
468 $event_occurance = strtotime($next_occurance);
470 }
else if ($asset && $asset->type() ==
'calendar_event_modification') {
472 $pattern =
'/( [0-9\-]{2}:[0-9\-]{2}:[0-9\-]{2})/';
474 $occurance_date = preg_replace($pattern,
'', $asset->attr(
'start_date'));
477 $link = $GLOBALS[
'SQ_SYSTEM']->am->getParents($id,
'calendar_event_recurring', NULL, NULL, TRUE, 1, 1);
479 foreach($events as $temp_id => $temp_event) {
480 $original_id = strtok($temp_id,
':');
481 if (array_key_exists($original_id, $link)) {
482 $events[$temp_id][
'current_date'] = $occurance_date;
483 $recuring_event_last_occurance_date = $_REQUEST[
'SQ_CALENDAR_DATE'];
484 $_REQUEST[
'SQ_CALENDAR_DATE'] = $occurance_date;
487 $event_occurance = strtotime($occurance_date);
491 if ($asset && $needs_asset[$format_type] ) {
492 foreach ($required_keywords[$format_type] as $keyword) {
493 if ($keyword ==
'asset_contents') {
496 $replacements[$keyword] = ob_get_contents();
499 $replacements[$keyword] = $asset->getKeywordReplacement($keyword);
504 foreach ($asset_infos[$id] as $field => $value) {
505 $replacements[
'asset_'.$field] = $value;
507 $replacements[
'asset_assetid'] = $id;
512 if (!empty($metadata_keywords[$event[
'type_code']])) {
513 if (!empty($metadata_keywords)) {
515 $mm = $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
518 $metadata_values = $mm->getMetadataFieldValues($id, $metadata_keywords[$event[
'type_code']]);
520 foreach ($metadata_values as $field => $value) {
521 $replacements[
'asset_metadata_'.$field] = $value;
526 $entry = $type_formats[$format_type];
527 replace_keywords($entry, $replacements);
528 $events_data[] = Array(
'data' => $entry,
'start_date_ts' => $event_occurance);
530 if ($asset && $asset->type() ==
'calendar_event_modification')
531 $_REQUEST[
'SQ_CALENDAR_DATE'] = $recuring_event_last_occurance_date;
536 uasort($events_data, Array(
'Calendar_Common',
'compareStartDates'));
537 foreach($events_data as $event_data) {
538 $event_list .= $event_data[
'data'];
541 if (isset($_REQUEST[
'SQ_CALENDAR_DATE'])) {
542 unset($_REQUEST[
'SQ_CALENDAR_DATE']);
545 return Array(
'event_list' => $event_list);
559 $raw_dynamic_root_nodes = $parameter_map->getParameterValue(
'replacement_root_node');
560 if (!empty($raw_dynamic_root_nodes)) {
561 $dynamic_root_nodes = Array();
564 if (!is_array($raw_dynamic_root_nodes)) {
565 $raw_dynamic_root_nodes = explode(
',', $raw_dynamic_root_nodes);
568 $original_root_ids = array_keys($this->
attr(
'root_nodes'));
569 foreach ($raw_dynamic_root_nodes as $dynamic_root) {
570 if (!strlen($dynamic_root))
continue;
573 $dynamic_parents = $GLOBALS[
'SQ_SYSTEM']->am->getParents($dynamic_root);
574 $matching_root_ids = array_intersect(array_keys($dynamic_parents), $original_root_ids);
575 if (empty($matching_root_ids)) {
576 trigger_localised_error(
'CAL0067', E_USER_WARNING, $dynamic_root, $this->
id);
578 $dynamic_root_nodes[$dynamic_root] = 1;
582 if (!empty($dynamic_root_nodes)) {
583 return $dynamic_root_nodes;
604 if (empty($root_nodes)) {
605 $root_nodes = $this->
attr(
'root_nodes');
607 if (empty($root_nodes)) {
608 trigger_localised_error(
'CAL0028', E_USER_WARNING);
613 $horizon_ts = strtotime(
'+'.$this->
attr(
'horizon').
'days');
614 $all_events = Array();
616 while ((count($all_events) < $this->
attr(
'num_events')) && ($start_ts < $horizon_ts)) {
618 $end_ts = strtotime(
'+'.$this->
attr(
'horizon').
' days', $start_ts);
619 if ($end_ts > $horizon_ts)
break;
620 $single_result = Array();
621 $recur_result = Array();
624 $bind_vars = Array();
625 $date_sql =
'(cd.start_date_ts BETWEEN :start_date_ts AND :end_date_ts) OR (cd.start_date_ts < :start_date_ts_1 AND cd.end_date_ts >= :start_date_ts_2)';
628 $single_result = Array();
631 foreach($bind_vars as $bind_value => $bind_var) {
639 }
catch (Exception $e) {
640 throw new Exception($e->getMessage());
645 $bind_vars = Array();
648 $recur_result = Array();
651 foreach($bind_vars as $bind_value => $bind_var) {
655 }
catch (Exception $e) {
656 throw new Exception($e->getMessage());
660 foreach ($single_result as $event_id => $single_event) {
663 if ($single_event[
'type_code'] ==
'calendar_event_single')
continue;
664 $recur_result[$event_id] = $single_event;
665 unset($single_result[$event_id]);
667 $recur_events = $recur_result;
674 $new_events = $single_result + $recur_result;
677 $all_events += $new_events;
679 $start_ts = $end_ts - 1;
683 uasort($all_events, Array(
'Calendar_Common',
'compareStartDates'));
686 foreach ($all_events as $id => $event) {
687 if ($i >= $this->
attr(
'num_events'))
break;
708 if (!isset($this->_tmp[
'formats'][$type])) {
710 $format_links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($folder->id, SQ_LINK_TYPE_2,
'bodycopy', TRUE);
711 if (empty($format_links))
return Array();
714 foreach ($format_links as $link_data) {
715 $formats[$link_data[
'minorid']] = $link_data[
'value'];
717 $this->_tmp[
'formats'][$type] = $formats;
719 return $this->_tmp[
'formats'][$type];
735 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($this->
id, SQ_LINK_TYPE_2,
'folder', TRUE, $type);
736 if (empty($link))
return $null;
738 $folder = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid'], $link[
'minor_type_code']);
739 if (is_null($folder))
return $null;