Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
page_calendar_rolling.inc
1 <?php
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';
21 
35 {
36 
37 
42  var $bodycopies = Array(
43  '' => Array(
44  'name' => 'Page Contents',
45  'content' => Array(
46  'content' => '%event_list%',
47  ),
48  ),
49  'time_unit' => Array(
50  'name' => 'Time Unit Format',
51  'content' => Array(
52  'content' => "<h3>%time_unit_start_datetime_Y-m-d%</h3>\n%time_unit_event_list%",
53  ),
54  ),
55  );
56 
57 
64  function __construct($assetid=0)
65  {
66  $this->_ser_attrs = TRUE;
67  parent::__construct($assetid);
68  }//end constructor
69 
70 
81  function _createAdditional(&$link)
82  {
83  if (!parent::_createAdditional($link)) return FALSE;
84 
85  $am =& $GLOBALS['SQ_SYSTEM']->am;
86 
87  $link = Array(
88  'asset' => &$this,
89  'value' => 'type_formats',
90  'link_type' => SQ_LINK_TYPE_2,
91  'is_dependant' => 1,
92  'is_exclusive' => 1,
93  );
94 
95  $am->includeAsset('folder');
96  $am->includeAsset('bodycopy');
97 
98  // create a type formats folder
99  $type_formats_folder = new Folder();
100  $type_formats_folder->setAttrValue('name', 'Type Formats');
101  if (!$type_formats_folder->create($link)) return FALSE;
102 
103  // create type format bodycopies
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;
107  $content = Array(
108  'content' => '%asset_name% (%event_start_date%)',
109  );
110  $format_bc = new Bodycopy();
111  $format_bc->setAttrValue('name', $am->getTypeInfo($event_type, 'name').' Format');
112  if (!$format_bc->create($tf_link, $content)) {
113  return FALSE;
114  }
115  $format_bc = NULL;
116  }
117 
118  // create the page contents and time_unit bodycopies
119  foreach ($this->bodycopies as $value => $details) {
120  $copy_link = Array(
121  'asset' => &$this,
122  'link_type' => SQ_LINK_TYPE_2,
123  'is_dependant' => 1,
124  'is_exclusive' => 1,
125  'value' => $value,
126  );
127 
128  $bodycopy = new Bodycopy();
129  $bodycopy->setAttrValue('name', array_get_index($details, 'name'));
130  if (!$bodycopy->create($copy_link, array_get_index($details, 'content'))) {
131  return FALSE;
132  }
133  }
134 
135  return TRUE;
136 
137  }//end _createAdditional()
138 
139 
147  function _getAllowedLinks()
148  {
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);
152  return $page_links;
153 
154  }//end _getAllowedLinks()
155 
156 
167  function onRequestKeywords(&$broadcaster, $vars=Array())
168  {
169  if (!isset($vars['keywords'])) {
170  $vars['keywords'] = Array();
171  }
172 
173  $am =& $GLOBALS['SQ_SYSTEM']->am;
174 
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);
177 
178  // check if the broadcaster is in the type formats folder
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');
182  $tf_bcs = Array();
183  foreach ($tf_links as $link) {
184  $tf_bcs[$link['minorid']] = $link['value'];
185  }
186  $bc_id = reset(array_intersect(array_keys($tf_bcs), array_keys($broadcaster_parents)));
187  if ($bc_id) {
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;
193  }
194  return TRUE;
195  }
196  return FALSE;
197  }
198 
199 
200  // get the bodycopy parent of the broadcaster
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'];
206  }
207  }
208 
209  switch ($format_bodycopy) {
210  case '':
211  $vars['keywords']['event_list'] = translate('cal_rolling_keyword_event_list');
212  return TRUE;
213  break;
214  case 'time_unit':
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');
218  return TRUE;
219  break;
220  }
221 
222  return FALSE;
223 
224  }//end onRequestKeywords()
225 
226 
233  function printBody()
234  {
235  $cache_key = $this->_getCacheKey();
236  $cached_contents = '';
237 
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);
241  }
242 
243  if ($cached_contents !== FALSE) {
244  echo $cached_contents;
245  } else {
246  // No cached version, so carry out the calculations
247  $am =& $GLOBALS['SQ_SYSTEM']->am;
248 
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;
252 
253  foreach ($page_links as $link) {
254  switch ($link['minor_type_code']) {
255  case 'folder':
256  $this->_tmp['tf_folder_id'] = $link['minorid'];
257  break;
258  case 'bodycopy':
259  if ($link['value'] == '') {
260  $pb_bodycopy_id = $link['minorid'];
261  }
262  break;
263  }
264  }
265  if (is_null($this->_tmp['tf_folder_id'])) {
266  trigger_localised_error('CAL0059', E_USER_WARNING, $this->id);
267  return;
268  }
269  if (is_null($pb_bodycopy_id)) {
270  trigger_localised_error('CAL0060', E_USER_WARNING, $this->id);
271  return;
272  }
273 
274  ob_start();
275  $pb = $am->getAsset($pb_bodycopy_id, 'bodycopy');
276  $replacements = $this->_getPageContentsReplacements();
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];
282  } else {
283  $replacements[$word] = $this->getKeywordReplacement($word);
284  }
285  }
286  $pb->setKeywordReplacements($replacements);
287  $pb->printBody();
288 
289  if (!empty($cache_key)) {
290  $cm->saveToCache($this->id, $this->type(), $cache_key, ob_get_contents());
291  }
292  ob_end_flush();
293  }//end else
294 
295  }//end printBody()
296 
297 
312  function getKeywordReplacement($keyword)
313  {
314  $prefix = $this->getPrefix();
315 
316  // No keywords here, just go and get the global keywords
317  return parent::getKeywordReplacement($keyword);
318 
319  }//end getKeywordReplacement()
320 
321 
328  function _getCacheKey()
329  {
330  $cache_key = '';
331  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
332 
333  // Hmmm, page_calendar_restricted call this function too.
334  // So must make sure, we can tell the difference between the 2 entities
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');
340 
341  // All attributes will have some contents of no need to check for empty
342  $cache_key .= $from_date; // First entry so no colon out front like a little soldier
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');
349 
350  $cache_key .= $from_date; // First entry so no colon out front like a little soldier
351 
352  // If the end date is set use it in the cache key
353  $end_date = substr($to_date, 0, 10);
354  if (!($end_date == '----------')) {
355  $cache_key .= ':'.$to_date;
356  }
357  }
358 
359  if ($cache_key !== '') {
360  $cache_key .= '-ctx'.$contextid;
361  }
362 
363  return $cache_key;
364 
365  }//end _getCacheKey()
366 
367 
375  {
376  $am =& $GLOBALS['SQ_SYSTEM']->am;
377 
378  // prepare keywords for the type format replacement we're going to perform later
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(
385  'assetid',
386  'type_code',
387  'version',
388  'name',
389  'short_name',
390  'status',
391  'languages',
392  'charset',
393  'force_secure',
394  'created',
395  'created_userid',
396  'updated',
397  'updated_userid',
398  'published',
399  'published_userid',
400  );
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;
411  } else {
412  // the keyword starts with 'asset_' but it might still require the asset
413  if ((strpos($keyword, 'asset_metadata_') !== 0) && (!in_array(substr($keyword, 6), $asset_info_fields))) {
414  $needs_asset[$link['value']] = TRUE;
415  }
416  }
417  if (strpos($keyword, 'asset_metadata_') === 0) {
418  $metadata_keywords[$link['value']][] = $keyword;
419  }
420  }
421  if (!$needs_asset[$link['value']]) {
422  $somebody_doesnt_need_asset = TRUE;
423  }
424  }
425  }
426 
427  // get the events to display
428  $time = time();
429  $all_events = $this->_getEvents(NULL, NULL, $time);
430 
431  $event_list = '';
432  if (!empty($all_events)) {
433  $all_ids = Array();
434  foreach ($all_events as $day => $day_event_list) {
435  $all_ids = array_merge($all_ids, array_keys($day_event_list));
436  }
437  if ($somebody_doesnt_need_asset) {
438  $asset_infos = $am->getAssetInfo($all_ids);
439  }
440 
441  // break up events into months, weeks, or days
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')) {
446  case 'months':
447  $current_year = date('Y', $time);
448  $current_month = date('m', $time);
449 
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);
454 
455  $difference = ($event_month - $current_month) + (($event_year - $current_year) * 12);
456  $events_by_time_unit[$difference][$event_date] = $events;
457  }
458  break;
459 
460  case 'weeks':
461  // assumes week starts on sunday; further development required in order to have weeks start on a different day
462  $current_year = date('Y', $time);
463 
464  // hack required - php 'W' assumes week starts on monday, 'w' assumes week starts on sunday
465  // "correct" so that weeks start on sunday
466  $current_week = date('W', strtotime(date('Y-m-d', $time).' +1 day'));
467 
468  foreach ($all_events as $event_date => $events) {
469  $event_time = iso8601_ts($event_date);
470  $event_year = date('Y', $event_time);
471 
472  // hack required - same as above
473  $event_week = date('W', strtotime(date('Y-m-d', $event_time).' +1 day'));
474 
475  $difference = ($event_week - $current_week) + (($event_year - $current_year) * 52);
476  $events_by_time_unit[$difference][$event_date] = $events;
477  }
478  break;
479 
480  case 'days':
481  default:
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;
485  }
486  break;
487  }//end switch ($this->attr('time_unit'))
488 
489 
490  $unit_bodycopy_link = $am->getLink($this->id, SQ_LINK_TYPE_2, 'bodycopy', TRUE, 'time_unit');
491  if (empty($unit_bodycopy_link)) return FALSE;
492 
493  $unit_bodycopy = $am->getAsset($unit_bodycopy_link['minorid'], $unit_bodycopy_link['minor_type_code']);
494  if (is_null($unit_bodycopy)) return FALSE;
495 
496  $single_event_descendants = $am->getTypeDescendants('calendar_event_single', TRUE);
497  $recurring_event_descendants = $am->getTypeDescendants('calendar_event_recurring', TRUE);
498  $mm = NULL;
499 
500  // Store the date supplied in the URL (if supplied) so it can be restored after use
501  $orig_date_param = '';
502  if (isset($_REQUEST['SQ_CALENDAR_DATE'])) {
503  $orig_date_param = $_REQUEST['SQ_CALENDAR_DATE'];
504  }
505 
506  foreach ($events_by_time_unit as $unit_count => $events_by_date) {
507 
508  // get all the events for this time_unit (day/week/month), stuff them into $event_list
509  $unit_contents = '';
510  foreach ($events_by_date as $date => $events) {
511  $_REQUEST['SQ_CALENDAR_DATE'] = $date;
512 
513  // loop through the events for this date
514  foreach ($events as $id => $event) {
515 
516  $format_type = $event['type_code'];
517 
518  // Firstly, check whether a specific asset Display Format has been defined.
519  // If not, we should default to its parent type
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';
525  }
526  }
527 
528  // Now if a Display Format is not set for either the asset type or its parent type, then
529  // we can't print this event
530  if (!isset($type_formats[$format_type])) continue;
531 
532  // get asset keywords
533  if ($needs_asset[$format_type]) {
534  $asset = $am->getAsset($id);
535  foreach ($required_keywords[$format_type] as $keyword) {
536  if ($keyword == 'asset_contents') {
537  ob_start();
538  $asset->printBody();
539  $replacements[$keyword] = ob_get_contents();
540  ob_end_clean();
541  } else {
542  $replacements[$keyword] = $asset->getKeywordReplacement($keyword);
543  }
544  }
545  } else {
546  // we can do this without the asset
547  foreach ($asset_infos[$id] as $field => $value) {
548  $replacements['asset_'.$field] = $value;
549  }
550  $replacements['asset_assetid'] = $id;
551  }
552 
553  // get metadata keywords
554  if (!empty($metadata_keywords[$event['type_code']])) {
555  if (is_null($mm)) {
556  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
557  }
558  // get all the metadata keywords for this asset
559  $metadata_values = $mm->getMetadataFieldValues($id, array_values($metadata_keywords[$event['type_code']]));
560 
561  foreach ($metadata_values as $field => $value) {
562  $replacements['asset_metadata_'.$field] = $value;
563  }
564  }
565 
566  $type_format = $type_formats[$format_type];
567  $unit_contents .= replace_keywords($type_format, $replacements);
568 
569  }//end foreach events in day
570  }//end foreach ($events_by_date)
571 
572 
573  $unit_keywords = $unit_bodycopy->getKeywords();
574  foreach ($unit_keywords as $keyword) {
575  switch ($keyword) {
576  case 'time_unit_event_list':
577  $replacements[$keyword] = $unit_contents;
578  break;
579 
580  default:
581  // if the keyword at this point starts with event_start_datetime_ or event_end_datetime_
582  // it means this is a dynamic keyword to display a date
583  preg_match('/^time_unit_(start|end)_datetime_(.+)/', $keyword, $matches);
584  if ($matches != NULL) {
585  $start_end = $matches[1];
586  $date_format = $matches[2];
587 
588  $time_unit = $this->attr('time_unit');
589  // construct a string in the format "YEAR/MONTH/DATE +UNIT_COUNT TIME_UNITs", and pass it to strtotime() to calculate
590  $start_end_dates = $this->_getStartEndDates($time_unit, $time, $unit_count);
591  $replacements[$keyword] = date($date_format, strtotime($start_end_dates[$start_end]));
592  }
593  break;
594  }//end switch ($keyword)
595  }//end foreach ($unit_keywords)
596 
597  ob_start();
598  // print the contents of page - replacing the global keywords
599  $unit_bodycopy->setKeywordReplacements($replacements);
600  $unit_bodycopy->printBody();
601  $event_list .= ob_get_clean();
602 
603  }//end foreach ($events_by_time_unit)
604 
605  // Restore supplied date value
606  if ($orig_date_param != '') {
607  $_REQUEST['SQ_CALENDAR_DATE'] = $orig_date_param;
608  } else {
609  unset($_REQUEST['SQ_CALENDAR_DATE']);
610  }
611  } else {
612  $no_result_link = $am->getLink($this->id, SQ_LINK_TYPE_2, 'bodycopy', TRUE, 'no_results');
613  // no bodycopy link found? return false
614  if (empty($no_result_link)) return FALSE;
615 
616  $no_result_bc = $am->getAsset($no_result_link['minorid'], $no_result_link['minor_type_code']);
617  // no bodycopy found? return false
618  if (is_null($no_result_bc)) return FALSE;
619 
620  $keywords = $no_result_bc->getKeywords();
621 
622  $replacements = Array();
623  // don't worry about our keyword modifiers here our parent
624  // function getKeywordReplacement is doing that for us
625  foreach ($keywords as $keyword) {
626  $replacements[$keyword] = $this->getKeywordReplacement($keyword);
627  }
628 
629  $no_result_bc->setKeywordReplacements($replacements);
630  ob_start();
631  $no_result_bc->printBody();
632  $event_list = ob_get_clean();
633 
634  }//end !empty($all_events)
635 
636  return Array('event_list' => $event_list);
637 
638  }//end _getPageContentsReplacements()
639 
640 
653  function _getStartEndDates($time_unit, $time, $modifier=0)
654  {
655  if (!is_int($time)) return FALSE;
656 
657  // use the modifier, calculate the new date
658  $time = strtotime(date('Y-m-d', $time).' '.($modifier < 0 ? $modifier : '+'.$modifier).' '.$time_unit);
659 
660  // check for day/week/month, bail out if other
661  preg_match('/(day|week|month)([s]?)/', $time_unit, $matches);
662  if (empty($matches)) return FALSE;
663  $unit = array_get_index($matches, 1);
664 
665  // calculate the start and end times for the time unit
666  switch ($unit) {
667  case 'month':
668  // construct a date that looks like: YEAR/MONTH/01 00:00.00
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'));
672  break;
673 
674  case 'week':
675  // get current day, diff to monday/sunday
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'));
679  break;
680 
681  case 'day':
682  default:
683  // just snip off anything beyond the day
684  $start = date('Y-m-d', $time);
685  $end = $start;
686  break;
687  }
688 
689  return Array(
690  'start' => $start,
691  'end' => $end,
692  );
693 
694  }//end _getStartEndDates()
695 
696 
708  function _getEvents($start_date=NULL, $end_date=NULL, $time=NULL)
709  {
710  $db = MatrixDAL::getDb();
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');
716 
717  // switch if the values are the wrong way around
718  if ($from > $to) {
719  $tmp = $from;
720  $from = $to;
721  $to = $tmp;
722  }
723 
724  $from_dates = $this->_getStartEndDates($unit, $time, $from);
725  $to_dates = $this->_getStartEndDates($unit, $time, $to);
726 
727  $start_date = $from_dates['start'];
728  $end_date = $to_dates['end'];
729  }
730  $length = round((iso8601_ts($end_date) - iso8601_ts($start_date)) / (60 * 60 * 24));
731 
732  // build sql to restrict the tree locations
733  $root_nodes = $this->attr('root_nodes');
734  if (empty($root_nodes)) {
735  trigger_localised_error('CAL0028', E_USER_WARNING);
736  return Array();
737  }
738 
739  // get the single events
740  $bind_vars = Array();
741  $start_date_stamp = Calendar_Common::_getDayStamp($start_date);
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)';
744  $sql = Calendar_Common::getSingleEventQueryBase(array_keys($root_nodes), 'calendar_event_single', TRUE, $bind_vars).' AND ('.$date_sql.') ORDER BY cd.start_date_ts';
745 
746  $single_result = Array();
747  try {
748  $query = MatrixDAL::preparePdoQuery($sql);
749  foreach($bind_vars as $bind_value => $bind_var) {
750  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
751  }
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);
756  $single_result = MatrixDAL::executePdoAll($query);
757  } catch (Exception $e) {
758  throw new Exception($e->getMessage());
759  }//end
760 
761  $single_result = Calendar_Common::condenseResultTreeids($single_result);
762 
763  // get the recurring events
764  $bind_vars = Array();
765  $sql = Calendar_Common::getRecurringEventQueryBase(array_keys($root_nodes), 'period', $start_date, (int) $length, 'calendar_event_recurring', $bind_vars);
766 
767  $recur_result = Array();
768  try {
769  $query = MatrixDAL::preparePdoQuery($sql);
770  foreach($bind_vars as $bind_value => $bind_var) {
771  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
772  }
773  $recur_result = MatrixDAL::executePdoAll($query);
774  } catch (Exception $e) {
775  throw new Exception($e->getMessage());
776  }//end
777 
778  $recur_result = Calendar_Common::condenseResultTreeids($recur_result);
779 
780  $all_events = $single_result + $recur_result;
781  $all_events = Calendar_Common::expandEventList($all_events, $start_date, $end_date);
783 
784  $res = Array();
785  if (!$this->isNoResultsBodycopyEnabled() || !empty($all_events)) {
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();
791  }
792 
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;
796  }
797 
798  // finally, filter by weekdays
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)) {
802  unset($res[$day]);
803  continue;
804  }
805  uasort($res[$day], Array('Calendar_Common', 'compareStartDates'));
806  }
807  }
808 
809  return $res;
810 
811  }//end _getEvents()
812 
813 
824  function getFormats($type='type_formats')
825  {
826  if (!isset($this->_tmp['formats'][$type])) {
827  $folder = $this->getFolder($type);
828  $format_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($folder->id, SQ_LINK_TYPE_2, 'bodycopy', TRUE);
829  if (empty($format_links)) return Array();
830 
831  $formats = Array();
832  foreach ($format_links as $link_data) {
833  $formats[$link_data['minorid']] = $link_data['value'];
834  }
835  $this->_tmp['formats'][$type] = $formats;
836  }
837  return $this->_tmp['formats'][$type];
838 
839  }//end getFormats()
840 
841 
850  function &getFolder($type='type_formats')
851  {
852  $null = NULL;
853  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_TYPE_2, 'folder', TRUE, $type);
854  if (empty($link)) return $null;
855 
856  $folder = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
857  if (is_null($folder)) return $null;
858 
859  return $folder;
860 
861  }//end getFolder()
862 
863 
864 //-- NO RESULTS BODYCOPY --//
865 
866 
877  function createNoResultsBodycopy($enable_on_create=TRUE)
878  {
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);
881 
882  // we already have a bodycopy link?!
883  if ($bodycopy_link) {
884  return FALSE;
885  } else {
886  $GLOBALS['SQ_SYSTEM']->am->includeAsset('bodycopy');
887 
888  $link_type = ($enable_on_create ? SQ_LINK_TYPE_2 : SQ_LINK_TYPE_3);
889 
890  $asset = new Bodycopy();
891  $copy_link = Array(
892  'asset' => &$this,
893  'value' => 'no_results',
894  'link_type' => $link_type,
895  'is_dependant' => 1,
896  'is_exclusive' => 1,
897  );
898 
899  $asset->setAttrValue('name', 'Page Contents (No Results)');
900  $args = Array('content' => 'No events found');
901  if (!$asset->create($copy_link, $args)) return FALSE;
902 
903  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
904  unset($asset);
905  }
906 
907  return TRUE;
908 
909  }//end createNoResultsBodycopy()
910 
911 
922  function &getNoResultsBodycopy($only_if_enabled=TRUE)
923  {
924  $asset = NULL;
925 
926  if ($only_if_enabled) {
927  $link_types = SQ_LINK_TYPE_2;
928  } else {
929  $link_types = SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3;
930  }
931 
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);
934 
935  if ($bodycopy_link) {
936  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($bodycopy_link['minorid'], 'bodycopy');
937  }
938 
939  return $asset;
940 
941  }//end getNoResultsBodycopy()
942 
943 
951  {
952  $link_types = SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3;
953 
954  $bodycopy_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, $link_types, 'bodycopy', FALSE, 'major', 'no_results');
955  $bodycopy_link = reset($bodycopy_links);
956 
957  if ($bodycopy_link) {
958  return ($bodycopy_link['link_type'] == SQ_LINK_TYPE_2);
959  } else {
960  return FALSE;
961  }
962 
963 
964  }//end isNoResultsBodycopyEnabled()
965 
966 
976  function setUseNoResultsBodycopy($enabled)
977  {
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);
980 
981  if (!$bodycopy_link) {
982  // no bodycopy yet? If we're trying to set to disabled, then we don't
983  // need to do anything - if not then we need to create it
984  if ($enabled) {
985  if (!$this->createNoResultsBodycopy()) return FALSE;
986  }
987  } else {
988  // set link type to either TYPE_2 if enabled or TYPE_3 if disabled
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);
992  }
993  }
994 
995  return TRUE;
996 
997  }//end setUseNoResultsBodycopy()
998 
999 
1000 }//end class
1001 ?>