/** * Overrides FacetapiWidget::settingsForm(). */ function settingsForm(&$form, &$form_state) { parent::settingsForm($form, $form_state); unset($form['widget']['widget_settings']['links'][$this->id]['soft_limit']); unset($form['widget']['widget_settings']['links'][$this->id]['show_expanded']); $form['widget']['widget_settings']['date_ranges'] = array('#type' => 'fieldset', '#title' => t('Configured Date Ranges'), '#tree' => TRUE, '#attributes' => array('class' => array('clearfix')), '#states' => array('visible' => array(array(array('select[name="widget"]' => array('value' => 'date_range')), 'or', array('select[name="widget"]' => array('value' => 'date_range_checkboxes')))))); $form['widget']['widget_settings']['ranges'] = array('#type' => 'container', '#title' => t('Configured Date Ranges'), '#tree' => TRUE); if (!isset($this->settings->settings['ranges'])) { $this->settings->settings['ranges'] = date_facets_default_ranges(); } uasort($this->settings->settings['ranges'], 'drupal_sort_weight'); foreach ($this->settings->settings['ranges'] as $range_data) { $form['widget']['widget_settings']['ranges'][$range_data['machine_name']]['label'] = array('#type' => 'textfield', '#title' => t('Label'), '#default_value' => isset($range_data['label']) ? $range_data['label'] : NULL, '#size' => 40, '#tree' => TRUE); $form['widget']['widget_settings']['ranges'][$range_data['machine_name']]['machine_name'] = array('#type' => 'machine_name', '#default_value' => isset($range_data['machine_name']) ? $range_data['machine_name'] : NULL, '#maxlength' => 20, '#machine_name' => array('exists' => 'date_facets_date_range_exists', 'source' => array('widget', 'widget_settings', 'ranges', $range_data['machine_name'], 'label')), '#tree' => TRUE); $form['widget']['widget_settings']['ranges'][$range_data['machine_name']]['date_range_start_op'] = array('#prefix' => t('FROM'), '#type' => 'select', '#title' => t('Date Range'), '#default_value' => isset($range_data['date_range_start_op']) ? $range_data['date_range_start_op'] : NULL, '#options' => array('NOW' => t('today'), '-' => t('past'), '+' => t('future')), '#tree' => TRUE); $form['widget']['widget_settings']['ranges'][$range_data['machine_name']]['date_range_start_amount'] = array('#type' => 'textfield', '#title' => t('Date Range'), '#default_value' => isset($range_data['date_range_start_amount']) ? $range_data['date_range_start_amount'] : NULL, '#size' => 5, '#tree' => TRUE, '#states' => array('invisible' => array(':input[name*="' . $range_data['machine_name'] . '][date_range_start_op]"]' => array('value' => 'NOW')))); $form['widget']['widget_settings']['ranges'][$range_data['machine_name']]['date_range_start_unit'] = array('#type' => 'select', '#title' => t('Date Range'), '#default_value' => isset($range_data['date_range_start_unit']) ? $range_data['date_range_start_unit'] : NULL, '#options' => array('HOUR' => t('hour'), 'DAY' => t('day'), 'MONTH' => t('month'), 'YEAR' => t('year')), '#states' => array('invisible' => array(':input[name*="' . $range_data['machine_name'] . '][date_range_start_op]"]' => array('value' => 'NOW'))), '#tree' => TRUE); $form['widget']['widget_settings']['ranges'][$range_data['machine_name']]['date_range_end_op'] = array('#prefix' => t('TO'), '#type' => 'select', '#title' => t('Date Range'), '#default_value' => isset($range_data['date_range_end_op']) ? $range_data['date_range_end_op'] : NULL, '#options' => array('NOW' => t('today'), '-' => t('past'), '+' => t('future')), '#tree' => TRUE); $form['widget']['widget_settings']['ranges'][$range_data['machine_name']]['date_range_end_amount'] = array('#type' => 'textfield', '#title' => t('Date Range'), '#default_value' => isset($range_data['date_range_end_amount']) ? $range_data['date_range_end_amount'] : NULL, '#size' => 5, '#tree' => TRUE, '#states' => array('invisible' => array(':input[name*="' . $range_data['machine_name'] . '][date_range_end_op]"]' => array('value' => 'NOW')))); $form['widget']['widget_settings']['ranges'][$range_data['machine_name']]['date_range_end_unit'] = array('#type' => 'select', '#title' => t('Date Range'), '#default_value' => isset($range_data['date_range_end_unit']) ? $range_data['date_range_end_unit'] : NULL, '#options' => array('HOUR' => t('hour'), 'DAY' => t('day'), 'MONTH' => t('month'), 'YEAR' => t('year')), '#states' => array('invisible' => array(':input[name*="' . $range_data['machine_name'] . '][date_range_end_op]"]' => array('value' => 'NOW'))), '#tree' => TRUE); $form['widget']['widget_settings']['ranges'][$range_data['machine_name']]['weight'] = array('#type' => 'weight', '#title' => t('Weight'), '#default_value' => isset($range_data['weight']) ? $range_data['weight'] : NULL, '#delta' => 10, '#attributes' => array('class' => array('date-range-weight')), '#tree' => TRUE); $form['widget']['widget_settings']['ranges'][$range_data['machine_name']]['delete'] = array('#type' => 'checkbox', '#title' => t('Delete'), '#tree' => TRUE); } // The new range button if (isset($form_state['add_new_range']) && $form_state['add_new_range']) { $form_state['add_new_range'] = FALSE; $form['widget']['widget_settings']['ranges']['temp']['label'] = array('#type' => 'textfield', '#title' => t('Label'), '#size' => 40, '#tree' => TRUE); $form['widget']['widget_settings']['ranges']['temp']['machine_name'] = array('#type' => 'machine_name', '#maxlength' => 20, '#machine_name' => array('exists' => 'date_facets_date_range_exists', 'source' => array('widget', 'widget_settings', 'ranges', 'temp', 'label')), '#tree' => TRUE); $form['widget']['widget_settings']['ranges']['temp']['date_range_start_op'] = array('#type' => 'select', '#title' => t('Date Range'), '#options' => array('NOW' => t('today'), '-' => t('past'), '+' => t('future')), '#tree' => TRUE); $form['widget']['widget_settings']['ranges']['temp']['date_range_start_amount'] = array('#type' => 'textfield', '#title' => t('Date Range'), '#size' => 5, '#tree' => TRUE, '#states' => array('invisible' => array(':input[name*="temp][date_range_start_op]"]' => array('value' => 'NOW')))); $form['widget']['widget_settings']['ranges']['temp']['date_range_start_unit'] = array('#type' => 'select', '#title' => t('Date Range'), '#options' => array('HOUR' => t('hour'), 'DAY' => t('day'), 'MONTH' => t('month'), 'YEAR' => t('year')), '#states' => array('invisible' => array(':input[name*="temp][date_range_start_op]"]' => array('value' => 'NOW'))), '#tree' => TRUE); $form['widget']['widget_settings']['ranges']['temp']['date_range_end_op'] = array('#type' => 'select', '#title' => t('Date Range'), '#options' => array('NOW' => t('today'), '-' => t('past'), '+' => t('future')), '#tree' => TRUE); $form['widget']['widget_settings']['ranges']['temp']['date_range_end_amount'] = array('#type' => 'textfield', '#title' => t('Date Range'), '#size' => 5, '#tree' => TRUE, '#states' => array('invisible' => array(':input[name*="temp][date_range_end_op]"]' => array('value' => 'NOW')))); $form['widget']['widget_settings']['ranges']['temp']['date_range_end_unit'] = array('#type' => 'select', '#title' => t('Date Range'), '#options' => array('HOUR' => t('hour'), 'DAY' => t('day'), 'MONTH' => t('month'), 'YEAR' => t('year')), '#states' => array('invisible' => array(':input[name*="temp][date_range_end_op]"]' => array('value' => 'NOW'))), '#tree' => TRUE); $form['widget']['widget_settings']['ranges']['temp']['weight'] = array('#type' => 'weight', '#title' => t('Weight'), '#delta' => 10, '#attributes' => array('class' => array('date-range-weight')), '#tree' => TRUE); $form['widget']['widget_settings']['ranges']['temp']['delete'] = array('#type' => 'checkbox', '#title' => t('Delete'), '#tree' => TRUE, '#states' => array('checked' => array(':input[name*="temp][label]"]' => array('value' => '')))); } else { $form['widget']['widget_settings']['date_ranges']['add_range'] = array('#type' => 'button', '#executes_submit_callback' => FALSE, '#value' => t('Add a new date range'), '#limit_validation_errors' => array(), '#ajax' => array('callback' => 'date_facets_tabledrag_form_new_range', 'wrapper' => 'date_facets_facet_config_form', 'method' => 'replace', 'effect' => 'fade'), '#weight' => 10); } $form['#validate'] = empty($form['#validate']) ? array() : $form['#validate']; $form['#submit'] = empty($form['#submit']) ? array() : $form['#submit']; $form['#validate'][] = 'date_facets_tabledrag_form_validate'; $form['#submit'][] = 'date_facets_facet_settings_form_submit'; $form['#theme'] = 'date_facets_tabledrag_form'; $form['#prefix'] = '<div id="date_facets_facet_config_form">'; $form['#suffix'] = '</div>'; }
/** * Implements FacetapiQueryTypeInterface::build(). */ public function build() { // Get the configured date ranges for the facet, or defaults if none have // been set up. $realm = facetapi_realm_load('block'); $settings = $this->adapter->getFacetSettings($this->facet, $realm); $ranges = isset($settings->settings['ranges']) && !empty($settings->settings['ranges']) ? $settings->settings['ranges'] : date_facets_default_ranges(); // Build the markup for the facet's date ranges. $build = date_facets_get_ranges($ranges); if ($this->adapter->searchExecuted()) { $facet_global_settings = $this->adapter->getFacet($this->facet)->getSettings(); // Iterate over each date range to get a count of results for that item. foreach ($ranges as $range) { // Generate our conditions for this date range. list($start, $end) = $this->generateRange($range); // We need to clone the query so we can get a count for each of our // date ranges. $facet_query = clone $this->adapter->getFacetQueryExtender(); $query_info = $this->adapter->getQueryInfo($this->facet); $facet_query->addFacetField($query_info); // Add the appropriate joins to our query, and add our date range // conditions. foreach ($query_info['fields'] as $field_info) { $facet_query->addFacetJoin($query_info, $field_info['table_alias']); $field = $field_info['table_alias'] . '.' . $this->facet['field']; $facet_query->condition($field, $start, '>='); $facet_query->condition($field, $end, '<'); } // Executes query, iterates over results. $result = $facet_query->execute()->fetchAll(); // If the result is 0, and the mincount is set to more than 0, remove // the facet option. if (empty($result) && $facet_global_settings->settings['facet_mincount'] > 0) { unset($build[$range['machine_name']]); } // Add the facet option counts to the build array. foreach ($result as $record) { $build[$range['machine_name']]['#count'] = $record->count; } } } return $build; }
/** * Implements FacetapiQueryTypeInterface::build(). * * Unlike normal facets, we provide a static list of options. */ public function build() { // Get facet's configured date ranges, or defaults if none have been set up. $settings = $this->adapter->getFacetSettings($this->facet, facetapi_realm_load('block')); $ranges = isset($settings->settings['ranges']) && !empty($settings->settings['ranges']) ? $settings->settings['ranges'] : date_facets_default_ranges(); // Build the markup for the date ranges. $build = date_facets_get_ranges_render_arrays($ranges); if ($response = apachesolr_static_response_cache($this->adapter->getSearcher())) { $facet_global_settings = $this->adapter->getFacet($this->facet)->getSettings(); $values = (array) $response->facet_counts->facet_queries; // Add result counts from the facet queries added in execute(). foreach ($ranges as $range) { list($start, $end) = $this->generateRange($range); $build[$range['machine_name']]['#count'] = $values[$this->facet['field'] . ":[{$start} TO {$end}]"]; } // Unset options with fewer results than the minimum count setting. foreach ($build as $name => $item) { if ($item['#count'] < $facet_global_settings->settings['facet_mincount']) { unset($build[$name]); } } } return $build; }
/** * Implements FacetapiQueryTypeInterface::build(). * * Unlike normal facets, we provide a static list of options. */ public function build() { $facet = $this->adapter->getFacet($this->facet); $search_ids = drupal_static('search_api_facetapi_active_facets', array()); if (empty($search_ids[$facet['name']]) || !search_api_current_search($search_ids[$facet['name']])) { return array(); } $search_id = $search_ids[$facet['name']]; $build = array(); $search = search_api_current_search($search_id); $results = $search[1]; if (!$results['result count']) { return array(); } // Executes query, iterates over results. if (isset($results['search_api_facets']) && isset($results['search_api_facets'][$this->facet['field']])) { $values = $results['search_api_facets'][$this->facet['field']]; $realm = facetapi_realm_load('block'); $settings = $this->adapter->getFacetSettings($this->facet, $realm); $ranges = isset($settings->settings['ranges']) && !empty($settings->settings['ranges']) ? $settings->settings['ranges'] : date_facets_default_ranges(); // Build the markup for the facet's date ranges. $build = date_facets_get_ranges_render_arrays($ranges); // Calculate values by facet. foreach ($values as $value) { $value['filter'] = str_replace('"', '', $value['filter']); foreach ($ranges as $key => $item) { list($start, $end) = $this->generateRange($item); $future_interval = $item['date_range_end_op'] == '+' && $start <= $value['filter'] && $value['filter'] <= $end; $past_interval = $item['date_range_start_op'] == '-' && $start <= $value['filter'] && $value['filter'] <= $end; if ($future_interval || $past_interval) { $build[$key]['#count'] += $value['count']; } } } } // Unset empty items. foreach ($build as $key => $item) { if ($item['#count'] === NULL) { unset($build[$key]); } } // Gets total number of documents matched in search. $total = $results['result count']; $keys_of_active_facets = array(); // Gets active facets, starts building hierarchy. foreach ($this->adapter->getActiveItems($this->facet) as $key => $item) { // If the item is active, the count is the result set count. $build[$key]['#count'] = $total; $keys_of_active_facets[] = $key; } // If we have active item, unset other items. $settings = $facet->getSettings()->settings; if (isset($settings['operator']) && $settings['operator'] !== FACETAPI_OPERATOR_OR) { if (!empty($keys_of_active_facets)) { foreach ($build as $key => $item) { if (!in_array($key, $keys_of_active_facets)) { unset($build[$key]); } } } } return $build; }