Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e02900355e
Fetching contributors…

Cannot retrieve contributors at this time

file 129 lines (123 sloc) 4.852 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
<?php

/**
* Build gRaphael graph parameters from a search facet.
*
* @param string $pie
* The gRaphael method to use. Either 'pie', 'bar', 'line', or 'dot'.
* @param string $datasource
* The Searchlight datasource to use for faceting.
* @param string $environment
* The Searchlight environment to use.
* @param string $facet
* The name of the facet to build.
* @param int $items
* The number of items to return for this facet.
* @return
* An array representing parameters that can be passed to theme('graphael').
*/
function searchlight_graphael_build($method = 'pie', $datasource, $environment = NULL, $facet, $items = 5) {
  $environment = !empty($environment) ? searchlight_environment_load($environment) : FALSE;
  $active = searchlight_environment_active();

  if ($environment && $active && ($environment->name === $active->name) && isset($active->plugins[$facet])) {
    $facets = $plugin->render($active->query, 'facets');
  }
  elseif ($environment) {
    $environment->initView();
    if ($query = $environment->query) {
      $plugin = searchlight_get_facet($datasource, $facet);
      $plugin->construct(NULL, $datasource->fields[$facet], NULL, array('items' => $items));
      $plugin->query($query);
      $facets = $plugin->render($query, 'facets');
    }
  }

  // If we're unable to get any environment working, just act on the data source.
  if (!isset($facets)) {
    $query = searchlight_get_query($datasource->base_table, $datasource->base_field);
    $query->set_search_buildmode('search');
    $plugin = searchlight_get_facet($datasource, $facet);
    $plugin->construct(NULL, $datasource->fields[$facet], NULL, array('items' => $items));
    $plugin->query($query);
    $query->build(new stdClass());
    $facets = $plugin->render($query, 'facets');
  }

  // Build gRaphael values
  $graph = array('method' => $method, 'values' => array(), 'params' => array(), 'extend' => array());
  switch ($method) {
    case 'bar':
      foreach ($facets as $item) {
        $graph['values'][] = (int) $item['count'];
        $graph['extend']['label']['values'][] = truncate_utf8($item['title'], 30, TRUE, TRUE) . "\n" . $item['count'];
        // Generate link.
        if ($environment) {
          $graph['extend']['url']['values'][] = url($environment->getURLPath(), $environment->getURLOptions('add', $facet, $item['id']));
        }
      }
      break;
    case 'pie':
      foreach ($facets as $item) {
        $graph['values'][] = (int) $item['count'];
        $graph['params']['opts']['legend'][] = truncate_utf8($item['title'], 30, TRUE, TRUE) ;
        $graph['extend']['label']['values'][] = truncate_utf8($item['title'], 30, TRUE, TRUE) . "\n" . $item['count'];
        // Generate link.
        if ($environment) {
          $graph['extend']['url']['values'][] = url($environment->getURLPath(), $environment->getURLOptions('add', $facet, $item['id']));
        }
      }
      break;
    case 'line':
      $i = 0;
      foreach ($facets as $item) {
        $graph['values'][0][] = $i;
        $graph['values'][1][] = (int) $item['count'];
        $graph['extend']['label']['values'][] = truncate_utf8($item['title'], 30, TRUE, TRUE) . "\n" . $item['count'];
        $i++;
        // Generate link.
        if ($environment) {
          $graph['extend']['url']['values'][] = url($environment->getURLPath(), $environment->getURLOptions('add', $facet, $item['id']));
        }
      }
      break;
    case 'dot':
      $i = 0;
      foreach ($facets as $item) {
        $graph['values'][0][] = $i;
        $graph['values'][1][] = 0;
        $graph['values'][2][] = (int) $item['count'];
        $graph['extend']['label']['values'][] = truncate_utf8($item['title'], 30, TRUE, TRUE) . "\n" . $item['count'];
        $i++;
        // Generate link.
        if ($environment) {
          $graph['extend']['url']['values'][] = url($environment->getURLPath(), $environment->getURLOptions('add', $facet, $item['id']));
        }
      }
      break;
  }
  return $graph;
}

/**
* Implementation of hook_ctools_api().
*/
function searchlight_graphael_ctools_api($module = NULL, $api = NULL) {
  if ($module === 'boxes' && $api === 'plugins') {
    return TRUE;
  }
  return FALSE;
}

/**
* Implementation of hook_boxes_plugins().
*/
function searchlight_graphael_boxes_plugins() {
  $info = array();
  foreach (searchlight_get_datasource() as $datasource) {
    $info['searchlight_graphael_' . $datasource->base_table] = array(
      'title' => t('Searchlight graph (@base_table)', array('@base_table' => $datasource->base_table)),
      'handler' => array(
        'class' => 'searchlight_graphael_box',
        'file' => 'searchlight_graphael_box.inc',
        'path' => drupal_get_path('module', 'searchlight_graphael') .'/plugins',
        'parent' => 'box',
      ),
    );
  }
  return $info;
}
Something went wrong with that request. Please try again.