| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Implementation of hook_views_style_plugins()
|
| 5 |
*/
|
| 6 |
function nodecloud_views_style_plugins() {
|
| 7 |
$plugins = array();
|
| 8 |
$plugins['nodecloud'] = array(
|
| 9 |
'name' => t('Node Cloud'),
|
| 10 |
'theme' => 'nodecloud_display',
|
| 11 |
'summary_theme' => 'nodecloud_display',
|
| 12 |
'needs_fields' => true,
|
| 13 |
);
|
| 14 |
return $plugins;
|
| 15 |
}
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Implementation of the display theme
|
| 19 |
*/
|
| 20 |
function theme_nodecloud_display(&$view, &$items, $type) {
|
| 21 |
// detect if the second sort field is something that we'll get back
|
| 22 |
if (isset($view->sort[1])) {
|
| 23 |
$sort_field = strtr($view->sort[1]['field'], '.', '_');
|
| 24 |
$is = (array) $items[0];
|
| 25 |
if (array_key_exists($sort_field, $is)) {
|
| 26 |
$size_by = $sort_field;
|
| 27 |
$order_by = $view->sort[1]['sortorder'];
|
| 28 |
}
|
| 29 |
}
|
| 30 |
|
| 31 |
// if we couldn't set $size_by, see if there is a numeric result in the first item's fields
|
| 32 |
if (!isset($size_by)) {
|
| 33 |
foreach($items[0] as $key => $field) {
|
| 34 |
if (is_numeric($field)) {
|
| 35 |
$size_by = $key;
|
| 36 |
$order_by = 'DESC';
|
| 37 |
break;
|
| 38 |
}
|
| 39 |
}
|
| 40 |
}
|
| 41 |
|
| 42 |
if (!isset($size_by)) {
|
| 43 |
$size_by = 'nid'; // guaranteed to be in every view
|
| 44 |
$order_by = 'DESC';
|
| 45 |
}
|
| 46 |
|
| 47 |
$fields = _views_get_fields();
|
| 48 |
|
| 49 |
$total = count($items);
|
| 50 |
$half_way = ceil($total/2);
|
| 51 |
|
| 52 |
// find the min and max values of the $size_by field
|
| 53 |
foreach($items as $key => $item) {
|
| 54 |
$sizes[] = $item->$size_by;
|
| 55 |
}
|
| 56 |
|
| 57 |
if ($order_by == 'DESC') {
|
| 58 |
$realmax = max($sizes);
|
| 59 |
$realmin = min($sizes);
|
| 60 |
} else {
|
| 61 |
$realmax = min($sizes);
|
| 62 |
$realmin = max($sizes);
|
| 63 |
}
|
| 64 |
|
| 65 |
if (!isset($realmax)) {
|
| 66 |
$realmax = 0;
|
| 67 |
}
|
| 68 |
|
| 69 |
if (!isset($realmin)) {
|
| 70 |
$realmin = 0;
|
| 71 |
}
|
| 72 |
|
| 73 |
if (($realmax - $realmin) == 0) {
|
| 74 |
$realmax += 1; // fudge factor incase things go badly....
|
| 75 |
}
|
| 76 |
|
| 77 |
// resize max and min on to the range of text sizes we've defined
|
| 78 |
$localmax = variable_get('nodecloud_max', NODECLOUD_MAX);
|
| 79 |
$localmin = variable_get('nodecloud_min', NODECLOUD_MIN);
|
| 80 |
|
| 81 |
$strengths = array();
|
| 82 |
|
| 83 |
foreach($items as $key => $item) {
|
| 84 |
$strength = ((($localmax - $localmin) * (($item->$size_by - $realmin) / ($realmax - $realmin))) / $localmin) + $localmin ;
|
| 85 |
$strengths[$key]['item'] = $item;
|
| 86 |
$strengths[$key]['em'] = $strength;
|
| 87 |
}
|
| 88 |
|
| 89 |
$clouds = '';
|
| 90 |
foreach ($strengths as $unit) {
|
| 91 |
$clouds .= theme('nodecloud_item', $view, $unit['item'], $unit['em']);
|
| 92 |
}
|
| 93 |
|
| 94 |
if ($clouds) {
|
| 95 |
drupal_add_css(drupal_get_path('module', 'nodecloud') . '/nodecloud.css');
|
| 96 |
return "<div style=\"line-height: ". ($localmax) . "em;\" class=\"nodecloud-cloud\">" . $clouds . "</div>";
|
| 97 |
}
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Formats individual cloud items.
|
| 102 |
*/
|
| 103 |
function theme_nodecloud_item($view, $item, $size) {
|
| 104 |
foreach ($view->field as $field) {
|
| 105 |
if ($fields[$field['id']]['visible'] !== FALSE) {
|
| 106 |
if ($field['label']) {
|
| 107 |
$cloud .= "<span class=\"view-label ". views_css_safe('view-label-'. $field['queryname']) ."\">" . $field['label'] . "</span>\n";
|
| 108 |
}
|
| 109 |
$cloud .= "<span class=\"view-field ". views_css_safe('view-data-'. $field['queryname']) ."\">" . views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $item, $view) . "</span>\n";
|
| 110 |
}
|
| 111 |
}
|
| 112 |
if (!trim($cloud)) {
|
| 113 |
$node = node_load($item->nid);
|
| 114 |
$cloud = l($node->title, 'node/' . $node->nid);
|
| 115 |
}
|
| 116 |
return "<span style=\"font-size: ". $size ."em;\" class=\"view-item ". views_css_safe('view-item-'. $view->name) ."\">". $cloud ."</span>\n";
|
| 117 |
}
|