View source
<?php
abstract class FacetapiWidget {
protected $id;
protected $realm;
protected $facet;
protected $settings;
protected $build = array();
protected $key;
protected $jsSettings = array();
public function __construct($id, array $realm, FacetapiFacet $facet, stdClass $settings) {
$this->id = $id;
$this->realm = $realm;
$this->settings = $settings;
$this->settings->settings += $this
->getDefaultSettings();
$this->facet = $facet;
$this->key = $facet['field alias'];
}
public function init() {
$searcher = $this->facet
->getAdapter()
->getSearcher();
$classes = implode(' ', array(
"facetapi-" . $this->id,
"facetapi-facet-{$this->facet['name']}",
));
$this->build = array(
'#title' => $this->facet['label'],
'#description' => $this->facet['description'],
'#weight' => $this->facet['weight'],
'#adapter' => $this->facet
->getAdapter(),
'#realm_name' => $this->realm['name'],
'#facet' => $this->facet
->getFacet(),
'#settings' => $this->settings,
$this->facet['field alias'] => $this->facet
->getBuild(),
'#attributes' => array(
'class' => $classes,
'id' => 'facetapi-' . facetapi_hash_delta("facet-{$searcher}-{$this->realm['name']}-{$this->facet['name']}"),
),
);
$this
->sortFacet($this->build);
$this->jsSettings += array(
'id' => $this->build['#attributes']['id'],
'searcher' => $searcher,
'realmName' => $this->realm['name'],
'facetName' => $this->facet['name'],
'queryType' => $this->facet['query type'],
'widget' => $this->settings->settings['widget'],
);
drupal_add_css(drupal_get_path('module', 'facetapi') . '/facetapi.css');
return $this;
}
public abstract function execute();
public function settingsForm(&$form, &$form_state) {
}
public function getDefaultSettings() {
return array();
}
public function getId() {
return $this->id;
}
public function getBuild() {
return $this->build;
}
public function getKey() {
return $this->key;
}
public function getJavaScriptSettings() {
return $this->jsSettings;
}
function sortFacet(array &$build) {
$settings = $build['#settings']->settings;
$this->sorts = array_intersect_key(facetapi_get_sort_info(), array_filter($settings['active_sorts']));
foreach ($this->sorts as $name => &$info) {
$info['weight'] = $settings['sort_weight'][$name];
$info['order'] = $settings['sort_order'][$name];
}
if ($this->sorts) {
uasort($this->sorts, 'facetapi_sort_weight');
$this
->applySorts($build[$this->facet['field alias']]);
}
}
protected function applySorts(&$build) {
foreach (element_children($build) as $value) {
if (!empty($build[$value]['#item_children'])) {
$this
->applySorts($build[$value]['#item_children']);
}
}
uasort($build, array(
$this,
'sortCallback',
));
}
protected function sortCallback(array $a, array $b) {
$return = 0;
foreach ($this->sorts as $sort) {
if ($return = $sort['callback']($a, $b)) {
if (SORT_DESC == $sort['order']) {
$return *= -1;
}
break;
}
}
return $return;
}
}
function facetapi_sort_active(array $a, array $b) {
$a_active = isset($a['#active']) ? $a['#active'] : 0;
$b_active = isset($b['#active']) ? $b['#active'] : 0;
if ($a_active == $b_active) {
return 0;
}
return $a_active < $b_active ? -1 : 1;
}
function facetapi_sort_count(array $a, array $b) {
$a_count = isset($a['#count']) ? $a['#count'] : 0;
$b_count = isset($b['#count']) ? $b['#count'] : 0;
if ($a_count == $b_count) {
return 0;
}
return $a_count < $b_count ? -1 : 1;
}
function facetapi_sort_indexed(array $a, array $b) {
$a_value = isset($a['#indexed_value']) ? $a['#indexed_value'] : '';
$b_value = isset($b['#indexed_value']) ? $b['#indexed_value'] : '';
if ($a_value == $b_value) {
return 0;
}
return $a_value < $b_value ? -1 : 1;
}
function facetapi_sort_display(array $a, array $b) {
$a_count = isset($a['#value']) ? $a['#value'] : '';
$b_count = isset($b['#value']) ? $b['#value'] : '';
return strcasecmp($a['#value'], $b['#value']);
}