17 require_once SQ_PACKAGES_PATH.
'/search/search_page/search_page.inc';
18 require_once SQ_PACKAGES_PATH.
'/calendar/lib/calendar_common.inc';
36 var $fuzzy_date_types = Array(
42 'previous_weekend' => 0,
46 'this_fortnight' => 0,
47 'next_fortnight' => 0,
48 'previous_fortnight' => 0,
51 'previous_month' => 1,
69 $this->_ser_attrs = TRUE;
70 parent::__construct($assetid);
83 if (isset($_REQUEST[$this->
getPrefix().
'_submit_button']) && empty($_REQUEST[
'mode'])) {
84 $_REQUEST[
'mode'] =
'results';
87 $mode = array_get_index($_REQUEST,
'mode',
'');
93 $queries = $this->_getEventSearchQuery();
94 foreach ($queries as $field_name => $value) {
99 unset($queries[$field_name]);
102 $this->_tmp[
'searched_fqueries'] = $queries;
105 if (empty($_SESSION[
'SQ_LAST_EVENT_SEARCH'][$this->
id])) {
106 $_SESSION[
'SQ_LAST_EVENT_SEARCH'][$this->id] = Array();
107 }
else if ($mode==
'results') {
109 $this->_tmp[
'potential_searched_fqueries'] = $_SESSION[
'SQ_LAST_EVENT_SEARCH'][$this->id];
112 parent::printContents();
132 $event_search = Array();
133 foreach($search as $key => $val) {
134 if (strpos($key,
'f:') === 0) {
135 unset($search[$key]);
136 $event_search[substr($key, 2)] = $val;
140 $sm_result = parent::processSearch($search);
144 $multi_date_assets = Array();
146 foreach($root_nodes as $root_node) {
147 $multi_date_assets += $GLOBALS[
'SQ_SYSTEM']->am->getChildren($root_node,
'calendar_event_multi_date', TRUE);
151 $multi_date_events = Array();
152 foreach($multi_date_assets as $event_assetid => $val) {
154 $multi_events = array_keys($GLOBALS[
'SQ_SYSTEM']->am->getChildren($event_assetid,
'calendar_event', FALSE, TRUE));
155 foreach($multi_events as $multi_event_id) {
156 if (isset($sm_result[$event_assetid])) {
157 if (!isset($sm_result[$multi_event_id])) {
158 $sm_result[$multi_event_id] = 1;
161 $multi_date_events[$multi_event_id] = $event_assetid;
165 $sm_result_assetids = array_keys($sm_result);
167 $date_filter_result = $this->processEventSearch($event_search);
171 if ($this->
attr(
'main_logic') ==
'AND') {
172 foreach($date_filter_result as $event => $start_ts) {
173 $assetid = substr($event, 0, strpos($event,
':'));
174 if (isset($multi_date_events[$assetid]) && strpos($event,
',type_code=') !== FALSE) {
175 $event = substr($event, 0, strpos($event,
',type_code=')).
',type_code=calendar_event_multi_date';
177 if (!isset($result[$event]) && (empty($sm_queries) || in_array($assetid, $sm_result_assetids))) {
178 $result[$event] = array_get_index($sm_result, $assetid, 1);
183 $sm_fields_result = $this->getEventInstances($sm_result_assetids);
184 $combined_result = array_merge($sm_fields_result, $date_filter_result);
185 foreach($combined_result as $event => $start_ts) {
186 $assetid = substr($event, 0, strpos($event,
':'));
187 if (isset($multi_date_events[$assetid]) && strpos($event,
',type_code=') !== FALSE) {
188 $event = substr($event, 0, strpos($event,
',type_code=')).
',type_code=calendar_event_multi_date';
190 $result[$event] = array_get_index($sm_result, $assetid, 1);
207 function getEventInstances($event_assetids)
209 $query = $this->_getEventSearchQuery();
210 $events = $this->_getEvents();
212 $relevant_events = Array();
213 foreach($events as $event) {
214 if (isset($event[
'assetid']) && in_array($event[
'assetid'], $event_assetids)) {
215 $relevant_events[] = $event;
218 if (empty($relevant_events)) {
222 $grouping_logic = $this->
attr(
'events_filter_logic');
225 if ($this->isEmptyQuery($query)) {
228 foreach($query as $query_field => $query_data) {
229 if (empty($query_data)) {
233 $field_result = Array();
234 foreach($query_data as $query_date) {
235 if (empty($query_date[
'from']) || empty($query_date[
'to'])) {
242 if ($instances === FALSE) {
243 $instances = $field_result;
245 $instances = $grouping_logic ==
'AND' ? array_intersect($result, $field_result) : array_merge($result, $field_result);
250 return $instances ? $instances : Array();
263 function processEventSearch($search=Array())
265 $query = $this->_getEventSearchQuery($search);
268 $cache_key = md5(serialize($query).$this->
attr(
'main_logic').$this->
attr(
'events_filter_logic'));
269 $cm = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'cache_manager');
270 $result = $cm->loadFromCache($this->
id, $this->
type(), $cache_key);
271 if ($result !== FALSE) {
272 return unserialize($result);
276 $events = $this->_getEvents();
280 $_SESSION[
'SQ_LAST_EVENT_SEARCH'][$this->id] = $query;
284 if ($this->
attr(
'events_filter_allow_empty_search') && $this->isEmptyQuery($query)) {
286 'default_field' => Array(
288 'from' =>
'1970-01-01',
289 'to' =>
'2030-12-31',
295 if (empty($events) || empty($query)) {
300 $grouping_logic = $this->
attr(
'events_filter_logic');
304 foreach($query as $query_field => $query_data) {
305 if (empty($query_data)) {
310 $field_result = Array();
311 foreach($query_data as $query_date) {
312 if (empty($query_date[
'from']) || empty($query_date[
'to'])) {
321 if ($result === FALSE) {
322 $result = $field_result;
324 $result = $grouping_logic ==
'AND' ? array_intersect($result, $field_result) : array_merge($result, $field_result);
328 if (is_array($result)) {
329 $cm->saveToCache($this->
id, $this->
type(), $cache_key, serialize($result));
332 return $result ? $result : Array();
345 function isEmptyQuery($query)
348 $grouping_logic = $this->
attr(
'events_filter_logic');
350 $empty = $grouping_logic ==
'AND' ? TRUE : FALSE;
351 foreach($query as $field => $val) {
352 foreach($val as $query_data) {
353 if ($grouping_logic ==
'AND' && !empty($query_data[
'from']) && !empty($query_data[
'to'])) {
356 }
else if ($grouping_logic ==
'OR' && empty($query_data[
'from']) && empty($query_data[
'to'])) {
374 function _getEventSearchQuery($search=Array())
376 if (isset($this->_tmp[
'searched_fqueries'])) {
377 return $this->_tmp[
'searched_fqueries'];
380 $filters = $this->
attr(
'events_filters');
384 $pmap_params = $pmap->getParameters();
386 foreach ($filters as $field_name => $field_details) {
387 $queries[$field_name] = Array();
388 $query_var =
'queries_'.$field_name.
'_fquery';
389 $clean_query_var = str_replace(
'.',
'_', $query_var);
391 $queries[$field_name] = $this->
_processDateFilter($query_var, $field_details[
'type']);
392 if (!isset($queries[$field_name][0][
'from'])) {
393 $queries[$field_name] = $this->_getStoredQueryValue($field_name, $filters[$field_name][
'type']);
400 if (empty($queries) && !empty($this->_tmp[
'potential_searched_fqueries'])) {
401 $queries = $this->_tmp[
'potential_searched_fqueries'];
415 function _getEvents()
420 $bind_vars = Array();
422 $single_result = Array();
425 foreach($bind_vars as $bind_value => $bind_var) {
430 }
catch (Exception $e) {
431 throw new Exception($e->getMessage());
436 $bind_vars = Array();
438 $recurring_result = Array();
441 foreach($bind_vars as $bind_value => $bind_var) {
445 }
catch (Exception $e) {
446 throw new Exception($e->getMessage());
450 return array_merge($single_result, $recurring_result);
467 if (strpos($keyword,
'event_filter_') !== FALSE) {
470 $replacement = parent::getGeneralReplacement($keyword);
487 function _getStoredQueryValue($filter_name, $filter_type)
493 $pmap_params = $pmap->getParameters();
495 if (is_array($pmap_params) && !empty($pmap_params)) {
497 if (in_array(
'f:'.$filter_name, $pmap_params)) {
498 $key_field = array_search(
'f:'.$filter_name, $pmap_params);
499 $raw_val = $pmap->getParameterValue($pmap_params[$key_field]);
500 replace_global_keywords($raw_val);
509 switch ($filter_type) {
512 'from' => trim($raw_val),
513 'to' => trim($raw_val),
518 $pq_data = explode(
',', $raw_val);
519 list($from, $to) = $pq_data;
521 'from' => trim($from),
526 case 'fuzzy_dropdown':
527 $raw_val = trim($raw_val);
528 if (isset($this->fuzzy_date_types[$raw_val])) {
533 case 'fuzzy_checkboxes':
534 $fuzzy_types = explode(
',', $raw_val);
535 foreach($fuzzy_types as $fuzzy_type) {
536 $fuzzy_type = trim($fuzzy_type);
537 if (isset($this->fuzzy_date_types[$fuzzy_type])) {
561 $query_var = $filter_name.
'_fquery';
562 $filters = $this->
attr(
'events_filters');
563 if (!isset($filters[$filter_name][
'type'])) {
569 if (isset($_SESSION[
'SQ_LAST_EVENT_SEARCH'][$this->
id])) {
570 $query = array_get_index($_SESSION[
'SQ_LAST_EVENT_SEARCH'][$this->
id], $filter_name, Array());
574 $current_value = $this->
_processDateFilter(
'queries_'.$query_var, $filters[$filter_name][
'type']);
575 if (!isset($current_value[0][
'from'])) {
576 $current_value = $this->_getStoredQueryValue($filter_name, $filters[$filter_name][
'type']);
579 $fuzzy_date_options = array_get_index($filters[$filter_name],
'fuzzy_options', Array());
580 foreach($fuzzy_date_options as $key => $val) {
581 $fuzzy_date_options[$val] = str_replace(
'_',
' ', ucfirst($val));
582 unset($fuzzy_date_options[$key]);
587 switch($filters[$filter_name][
'type']) {
589 $this->
_printDateRange(
'queries_'.$query_var, array_get_index($current_value, 0, Array()), TRUE, $filters[$filter_name][
'use_picker']);
593 $this->
_printDateRange(
'queries_'.$query_var, array_get_index($current_value, 0, Array()), FALSE, $filters[$filter_name][
'use_picker']);
596 case 'fuzzy_dropdown':
597 $fuzzy_date_options = array_reverse($fuzzy_date_options, TRUE);
598 $fuzzy_date_options[
'none'] =
'';
599 $fuzzy_date_options = array_reverse($fuzzy_date_options, TRUE);
600 combo_box(
'queries_'.$query_var, $fuzzy_date_options, FALSE, isset($current_value[0]) ? array_get_index($current_value[0],
'fuzzy_type',
'') :
'');
603 case 'fuzzy_checkboxes':
604 $selected_opts = Array();
605 foreach($current_value as $selected_val) {
606 if (isset($selected_val[
'fuzzy_type'])) {
607 $selected_opts[] = $selected_val[
'fuzzy_type'];
610 foreach($fuzzy_date_options as $key => $val) {
612 check_box(
'queries_'.$query_var.
'_raw[]', $key, in_array($key, $selected_opts));
623 $replacement = ob_get_contents();
644 function _printDateRange($field_name, $current_value=Array(), $single_field=TRUE, $use_picker=TRUE, $read_only=FALSE)
646 if (!isset($current_value[
'from'])) {
647 $current_value[
'from'] =
'---------- --:--:--';
650 if (!isset($current_value[
'to'])) {
651 $current_value[
'to'] =
'---------- --:--:--';
655 require_once SQ_LIB_PATH.
'/js_calendar/js_calendar.inc';
658 ?><script src=
"<?php echo sq_web_path('lib'); ?>/html_form/html_form.js"
659 type=
"text/javascript"></script><?php
664 <table border=
"0" cellspacing=
"0" cellpadding=
"0">
667 if (!$single_field) {
668 echo
'<td align="right">'.ucfirst(translate(
'from')).
':</td>';
673 if ($read_only && $current_value[
'from'] !=
'---------- --:--:--') {
674 $from = iso8601_ts($current_value[
'from']);
675 echo date(
'j M Y', $from);
678 if (!$field->processField() || $field->value ==
'---------- --:--:--') {
679 $field->setValue($current_value[
'from']);
683 $field->printField();
688 if ($use_picker && (!$read_only || $current_value[
'from'] ==
'---------- --:--:--')) {
695 $calendar->changeSetting(
'onDayClick',
'datetime_set_date');
696 $calendar->paint($field_name.
'_from',
'', TRUE);
704 if (!$single_field) {
707 <td align=
"right"><?php echo ucfirst(translate(
'to')) ?>:</td>
710 if ($read_only && $current_value[
'to'] !=
'---------- --:--:--') {
711 $to = iso8601_ts($current_value[
'to']);
712 echo date(
'j M Y', $to);
715 if (!$field->processField() || $field->value ==
'---------- --:--:--') {
716 $field->setValue($current_value[
'to']);
718 $field->printField();
723 if ($use_picker && (!$read_only || $current_value[
'to'] ==
'---------- --:--:--')) {
730 $calendar->changeSetting(
'onDayClick',
'datetime_set_date');
731 $calendar->paint($field_name.
'_to',
'', TRUE);
758 'min' =>
'0000-01-01 00:00:00',
759 'max' =>
'9999-12-31 23:59:59',
760 'allow_circa' =>
'0',
761 'print_format' =>
'',
762 'show' => Array(
'y',
'm',
'd'),
763 'null' => Array(
'y',
'm',
'd'),
771 $value =
'---------- --:--:--';
772 $field =
new DateTime_Field($field_name, $value, $parameters, TRUE);
788 if (!isset($current_value[
'from'])) {
789 $current_value[
'from'] =
'---------- --:--:--';
792 if (!isset($current_value[
'to'])) {
793 $current_value[
'to'] =
'---------- --:--:--';
797 if ($current_value[
'from'] !=
'---------- --:--:--') {
798 $from = iso8601_ts($current_value[
'from']);
799 $from_string = date(
'j M Y', $from);
803 if ($current_value[
'to'] !=
'---------- --:--:--') {
804 $to = iso8601_ts($current_value[
'to']);
805 $to_string = date(
'j M Y', $to);
808 if (empty($from_string) && empty($to_string)) {
810 }
else if (empty($from_string)) {
811 echo translate(
'sch_page_date_range_prior_to', $to_string);
812 }
else if (empty($to_string)) {
813 echo translate(
'sch_page_date_range_onwards', $from_string);
814 }
else if ($from_string == $to_string) {
817 echo $from_string.
' - '.$to_string;
840 if ($field->processField()) {
841 $result[0][
'from'] = rtrim($field->value,
"-: ");
845 if ($field->processField()) {
846 $result[0][
'to'] = rtrim($field->value,
"-: ");
849 if (isset($result[0][
'from']) && !isset($result[0][
'to'])) {
850 $result[0][
'to'] = $result[0][
'from'];
854 case 'fuzzy_dropdown':
855 if (isset($_REQUEST[$field_name])) {
860 case 'fuzzy_checkboxes':
861 $fuzzy_types = Array();
862 if (isset($_REQUEST[$field_name.
'_raw'])) {
864 $fuzzy_types = $_REQUEST[$field_name.
'_raw'];
865 }
else if (isset($_REQUEST[$field_name])) {
867 $fuzzy_types = explode(
',', $_REQUEST[$field_name]);
870 if (!empty($fuzzy_types)) {
871 foreach($fuzzy_types as $index => $selected_date_type) {
895 $today = date(
"Y-m-d");
904 $from = date(
"Y-m-d", strtotime($today.
" +1 day"));
905 $to = date(
"Y-m-d", strtotime($today.
" +1 day"));
909 $from = date(
"Y-m-d", strtotime($today.
" -1 day"));
910 $to = date(
"Y-m-d", strtotime($today.
" -1 day"));
914 $from = date(
"N") == 7 ? $today : date(
"Y-m-d", strtotime(
'next Saturday'));
915 $to = date(
"N") == 7 ? $today : date(
"Y-m-d", strtotime(
'next Sunday'));
919 $from = date(
"N") == 7 ? date(
"Y-m-d", strtotime(
'next Saturday')) : date(
"Y-m-d", strtotime(
'next Saturday +1 week'));
920 $to = date(
"Y-m-d", strtotime(
'this Sunday +1 week'));
923 case 'previous_weekend':
924 $from = date(
"N") == 7 ? date(
"Y-m-d", strtotime(
'last Saturday -1 week')) : date(
"Y-m-d", strtotime(
'last Saturday'));
925 $to = date(
"Y-m-d", strtotime(
'last Sunday -1 week'));
930 $from = date(
"N") == 2 ? $today : date(
"Y-m-d", strtotime(
'last Monday'));
931 $to = date(
"Y-m-d", strtotime($from.
" +1 week"));
935 $from = date(
"Y-m-d", strtotime(
'next Monday'));
936 $to = date(
"Y-m-d", strtotime($from.
" +1 week"));
939 case 'previous_week':
940 $from = date(
"N") == 2 ? date(
"Y-m-d", strtotime(
'last Monday')) : date(
"Y-m-d", strtotime(
'last Monday -1 week'));
941 $to = date(
"Y-m-d", strtotime($from.
" +1 week"));
944 case 'this_fortnight':
945 $from = date(
"N") == 2 ? $today : date(
"Y-m-d", strtotime(
'last Monday'));
946 $to = date(
"Y-m-d", strtotime($from.
" +2 weeks"));
949 case 'next_fortnight':
950 $from = date(
"Y-m-d", strtotime(
'next Monday +1 week'));
951 $to = date(
"Y-m-d", strtotime($from.
" +2 weeks"));
954 case 'previous_fortnight':
955 $from = date(
"N") == 2 ? date(
"Y-m-d", strtotime(
'last Monday -1 week')) : date(
"Y-m-d", strtotime(
'last Monday -2 weeks'));
956 $to = date(
"Y-m-d", strtotime($from.
" +2 weeks"));
960 $from = date(
"Y-m-d", strtotime(date(
'm').
'/01/'.date(
'Y')));
961 $to = date(
"Y-m-d", strtotime((date(
'm')+1).
'/01/'.date(
'Y'))-86400);
965 if (date(
'm') == 12) {
969 $month = date(
'm')+1;
972 $from = date(
"Y-m-d", strtotime($month.
'/01/'.$year));
973 $to = date(
"Y-m-d", strtotime(($month+1).
'/01/'.$year)-86400);
976 case 'previous_month':
977 if (date(
'm') == 1) {
984 $from = date(
"Y-m-d", strtotime($month.
'/01/'.$year));
985 $to = date(
"Y-m-d", strtotime(($month == 12 ? 1 : $month+1).
'/01/'.$year)-86400);
990 $from = date(
"Y-m-d", strtotime(
'01/01/'.$year));
991 $to = date(
"Y-m-d", strtotime(
'12/31/'.$year));
996 $from = date(
"Y-m-d", strtotime(
'01/01/'.$year));
997 $to = date(
"Y-m-d", strtotime(
'12/31/'.$year));
1000 case 'previous_year':
1001 $year = date(
'Y')-1;
1002 $from = date(
"Y-m-d", strtotime(
'01/01/'.$year));
1003 $to = date(
"Y-m-d", strtotime(
'12/31/'.$year));
1007 $from =
'1970-01-01';
1021 "fuzzy_type" => $type,
1043 $vars[
'keywords'] = isset($vars[
'keywords']) ? $vars[
'keywords'] : Array();
1045 $parents = $GLOBALS[
'SQ_SYSTEM']->am->getParents($broadcaster->id,
'bodycopy', TRUE);
1046 $bodycopy_links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($this->
id, SQ_LINK_TYPE_2,
'bodycopy');
1051 if (!empty($sis_bodycopy_link)) {
1052 $bodycopy_links[] = $sis_bodycopy_link;
1058 if (!empty($search_bodycopy_link)) {
1059 $bodycopy_links[] = $search_bodycopy_link;
1062 $bodycopies = Array();
1063 $type_formats = Array();
1064 $keywords = Array();
1066 foreach ($bodycopy_links as $link_info) {
1067 if (isset($parents[$link_info[
'minorid']])) {
1068 $bodycopies[] = $link_info[
'value'];
1075 if (empty($bodycopies)) {
1076 parent::onRequestKeywords($broadcaster, $vars);
1082 $search_customised = !empty($search_bodycopy_link) && $search_bodycopy_link[
'link_type'] != SQ_LINK_TYPE_3;
1083 $sis_customised = !empty($sis_bodycopy_link) && $sis_bodycopy_link[
'link_type'] != SQ_LINK_TYPE_3;
1085 foreach ($bodycopies as $bodycopy) {
1086 if ($bodycopy ==
'initial' || $bodycopy ==
'sis_form' || $bodycopy ==
'search_form') {
1088 $fields = $this->
attr(
'fields');
1089 foreach ($fields as $field => $field_data) {
1090 $keywords[$field.
'_query'] = translate(
'sch_page_keyword_search_field', $field);
1091 if ($bodycopy ==
'initial') {
1092 $keywords[$field.
'_query_logic'] = translate(
'sch_page_keyword_search_field_logic', $field);
1096 $filters = $this->
attr(
'events_filters');
1097 foreach ($filters as $filter => $filter_data) {
1098 if (isset($filter_data[
'type'])) {
1099 $keywords[
'event_filter_'.$filter] = translate(
'page_events_search_filter_field', $filter);
1103 foreach ($this->
attr(
'categories') as $cat_name => $cat_details) {
1104 $keywords[$cat_name.
'_category_chooser'] =
'Dropdown box for category set '.$cat_name;
1109 if ($bodycopy ==
'search_form') {
1110 $keywords[
'submit_button'] = translate(
'cms_listing_keyword_submit_button');
1112 if ($bodycopy ==
'sis_form') {
1113 $keywords[
'search_in_search_button'] = translate(
'sch_page_keyword_sis_button');
1116 if ($bodycopy !=
'sis_form' && $bodycopy !=
'search_form') {
1117 foreach ($this->keywords[$bodycopy] as $keyword) {
1118 $keywords[$keyword] = ucwords(str_replace(
'_',
' ', $keyword));
1122 if ($search_customised) {
1127 if ($sis_customised && $bodycopy ==
'results') {
1133 $vars[
'keywords'] = array_merge($vars[
'keywords'], $keywords);