| 1 |
<?php
|
| 2 |
|
| 3 |
function sphinx_facet_menu() {
|
| 4 |
if (!$may_cache) {
|
| 5 |
$items[] = array(
|
| 6 |
'path' => 'admin/settings/sphinx/facet',
|
| 7 |
'title' => t('Facets'),
|
| 8 |
'description' => t('Configure the Sphinx facets'),
|
| 9 |
'callback' => 'drupal_get_form',
|
| 10 |
'callback arguments' => array('sphinx_facet_administration'),
|
| 11 |
'access' => user_access('administer sphinx search'),
|
| 12 |
'type' => MENU_LOCAL_TASK,
|
| 13 |
'weight' => 4,
|
| 14 |
);
|
| 15 |
}
|
| 16 |
return $items;
|
| 17 |
}
|
| 18 |
|
| 19 |
function sphinx_facet_administration(){
|
| 20 |
$options = array();
|
| 21 |
for($n=1;$n<=20;$n++) { $options[$n] = $n;}
|
| 22 |
$form['sphinx_facet']['no_facets'] = array(
|
| 23 |
'#type' => 'select',
|
| 24 |
'#title' => t('Number of facets'),
|
| 25 |
'#default_value' => variable_get('sphinx_facet_number_of_facets', 5),
|
| 26 |
'#description' => t(""),
|
| 27 |
'#options' => $options,
|
| 28 |
);
|
| 29 |
return system_settings_form($form);
|
| 30 |
|
| 31 |
|
| 32 |
}
|
| 33 |
function sphinx_facet_administration_submit($form_id, $form_values){
|
| 34 |
variable_set('sphinx_facet_number_of_facets', $form_values['no_facets']);
|
| 35 |
drupal_set_message(t('Facet settings updated!'));
|
| 36 |
}
|
| 37 |
|
| 38 |
function sphinx_facet_block($op = 'list', $delta = 0, $edit = array()){
|
| 39 |
if ($op == 'list') {
|
| 40 |
$blocks['facet'] = array(
|
| 41 |
'info' => t('Sphinx - facets'),
|
| 42 |
'weight' => 0,
|
| 43 |
'enabled' => 1,
|
| 44 |
'region' => 'sidebar_wide'
|
| 45 |
);
|
| 46 |
return $blocks;
|
| 47 |
}
|
| 48 |
else if ($op == 'view') {
|
| 49 |
if($delta=='facet') {
|
| 50 |
$index_path = arg(1);
|
| 51 |
if(arg(0)=='search' && !empty($index_path)){
|
| 52 |
$index = _sphinx_get_index_by_path(arg(1));
|
| 53 |
$attributes = _sphinx_facet_get_attributes_by_iid($index);
|
| 54 |
$facets = _sphinx_facet_build_facets($index, $attributes);
|
| 55 |
return array('subject' => 'Facets', 'content' => $facets);
|
| 56 |
}
|
| 57 |
}
|
| 58 |
}
|
| 59 |
}
|
| 60 |
|
| 61 |
function _sphinx_facet_get_attributes_by_iid($index) {
|
| 62 |
if (!empty($index)) {
|
| 63 |
$res = db_query('SELECT aid, attribute_name, display_name, type FROM {sphinx_attributes} WHERE iid=%d && facet=1' , $index);
|
| 64 |
$attributes = array();
|
| 65 |
while($attr = db_fetch_object($res)) {
|
| 66 |
$attributes[] = $attr;
|
| 67 |
}
|
| 68 |
return $attributes;
|
| 69 |
}
|
| 70 |
}
|
| 71 |
|
| 72 |
function _sphinx_facet_build_facets($index, $attributes){
|
| 73 |
//TODO: Don't display currently active filters
|
| 74 |
|
| 75 |
$response = '';
|
| 76 |
$url = explode('?', $_SERVER['REQUEST_URI']);
|
| 77 |
$query = _sphinx_facet_build_query($index);
|
| 78 |
$index_name = _sphinx_facet_get_index_name_by_id($index);
|
| 79 |
$client = new SphinxClient();
|
| 80 |
$connect = $client->_Connect();
|
| 81 |
$display_names = array();
|
| 82 |
if (!empty($_GET['as_filter'])) {
|
| 83 |
$filter_codes = explode('-', $_GET['as_filter']);
|
| 84 |
$filters = array();
|
| 85 |
$active_filters = array();
|
| 86 |
$filter_list = array();
|
| 87 |
for($z=0; $z<count($filter_codes); $z++){
|
| 88 |
$parts = explode('_',$filter_codes[$z]);
|
| 89 |
|
| 90 |
if(!empty($parts[0]) && !empty($parts[1])) {
|
| 91 |
$attribute = _sphinx_facet_get_attribute_data_by_aid($parts[0]);
|
| 92 |
$filters[$attribute->attribute_name] = array($parts[1]);
|
| 93 |
if($attribute->type == "term"){
|
| 94 |
$term = taxonomy_get_term($parts[1]);
|
| 95 |
$name = $term->name;
|
| 96 |
}
|
| 97 |
else if ($attribute->type == "user") {
|
| 98 |
$u = user_load(array('uid' => $parts[1]));
|
| 99 |
$name = $u->profile_navn;
|
| 100 |
}
|
| 101 |
|
| 102 |
if(count($filter_codes)>2){
|
| 103 |
$query_string = str_replace($filter_codes[$z].'-', '',$url[1]);
|
| 104 |
} else {
|
| 105 |
$query_string = str_replace('as_filter='.$filter_codes[$z].'-', '',$url[1]);
|
| 106 |
}
|
| 107 |
$filter_list[] = $attribute->display_name.': '.$name. ' ('.l('x', substr($url[0],1), null, $query_string).')';
|
| 108 |
}
|
| 109 |
}
|
| 110 |
$response .= '<h2>'. t('Active filters') .'</h2>'. theme('item_list', $filter_list);
|
| 111 |
}
|
| 112 |
$limit = variable_get('sphinx_facet_number_of_facets', 5);
|
| 113 |
for($n=0; $n<count($attributes); $n++) {
|
| 114 |
if(is_array($filters)){
|
| 115 |
foreach($filters as $attribute => $values){
|
| 116 |
$client->SetFilter( $attribute, $values );
|
| 117 |
}
|
| 118 |
}
|
| 119 |
$client->SetGroupBy($attributes[$n]->attribute_name,SPH_GROUPBY_ATTR, "@count desc");
|
| 120 |
$client->SetMatchMode(SPH_MATCH_EXTENDED);
|
| 121 |
$client->SetLimits(0,$limit);
|
| 122 |
$client->AddQuery($query, $index_name);
|
| 123 |
$display_names[$n]= $attributes[$n]->display_name;
|
| 124 |
}
|
| 125 |
$results = $client->RunQueries();
|
| 126 |
for($n=0; $n<count($results); $n++) {
|
| 127 |
if(count($results[$n]['matches'])) {
|
| 128 |
$response .= '<h2>'.$display_names[$n].'</h2>';
|
| 129 |
|
| 130 |
$lines = array();
|
| 131 |
foreach($results[$n]['matches'] as $tid => $value) {
|
| 132 |
|
| 133 |
if($attributes[$n]->type == "term"){
|
| 134 |
$term = taxonomy_get_term($value['attrs']['@groupby']);
|
| 135 |
$link = $term->name;
|
| 136 |
}
|
| 137 |
else if ($attributes[$n]->type == "user") {
|
| 138 |
$u = user_load(array('uid' => $value['attrs']['@groupby']));
|
| 139 |
$link = $u->profile_navn;
|
| 140 |
}
|
| 141 |
else if ($attributes[$n]->type == "node") {
|
| 142 |
|
| 143 |
}
|
| 144 |
if(empty($_GET['as_filter'])) {
|
| 145 |
$qstring = 'as_filter='.$attributes[$n]->aid.'_'.$value['attrs']['@groupby'].'-';
|
| 146 |
$query_string = (empty($url[1]))?$qstring:$url[1].'&'.$qstring;
|
| 147 |
} else {
|
| 148 |
$pieces = explode('&',$url[1]);
|
| 149 |
for($x=0; $x<count($pieces); $x++) {
|
| 150 |
$frag = explode('=', $pieces[$x]);
|
| 151 |
//print_r($frag);
|
| 152 |
if($frag[0]=='as_filter') {
|
| 153 |
$pieces[$x] = $frag[0].'='.$frag[1].$attributes[$n]->aid.'_'.$value['attrs']['@groupby'].'-';
|
| 154 |
}
|
| 155 |
}
|
| 156 |
$query_string = implode('&', $pieces);
|
| 157 |
}
|
| 158 |
|
| 159 |
$lines[] = l($link. ' ('.$value['attrs']['@count'].') ', substr($url[0],1), null, $query_string).' <br />';
|
| 160 |
}
|
| 161 |
|
| 162 |
$response .= theme('item_list', $lines);
|
| 163 |
}
|
| 164 |
}
|
| 165 |
return $response;
|
| 166 |
|
| 167 |
}
|
| 168 |
|
| 169 |
function _sphinx_facet_build_query($index) {
|
| 170 |
|
| 171 |
$fields = _sphinx_get_active_fields_by_iid($index);
|
| 172 |
$needle = arg(2).' ';
|
| 173 |
foreach ($fields as $fid => $name) {
|
| 174 |
$term = $_GET['as_f'. $fid];
|
| 175 |
if (!empty($term)) {
|
| 176 |
$needle .= '@'. $name .' '. $term .' ';
|
| 177 |
}
|
| 178 |
}
|
| 179 |
if (!empty($needle)) {
|
| 180 |
return $needle;
|
| 181 |
}
|
| 182 |
return;
|
| 183 |
}
|
| 184 |
|
| 185 |
function _sphinx_facet_get_attribute_data_by_aid($aid = null) {
|
| 186 |
if (!empty($aid)) {
|
| 187 |
$res = db_query('SELECT * FROM {sphinx_attributes} WHERE aid=%d', $aid);
|
| 188 |
$index = db_fetch_object($res);
|
| 189 |
return $index;
|
| 190 |
}
|
| 191 |
}
|
| 192 |
function _sphinx_facet_get_index_name_by_id($iid) {
|
| 193 |
if (!empty($iid)) {
|
| 194 |
$res = db_query('SELECT index_name FROM {sphinx_indexes} WHERE iid="%d"', $iid);
|
| 195 |
$index = db_fetch_object($res);
|
| 196 |
return $index->index_name;
|
| 197 |
}
|
| 198 |
}
|