| 1 |
<?php
|
| 2 |
// $Id: views_calc_field_handler.inc,v 1.2 2009/04/20 18:36:57 karens Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Copied from the basic 'node' field handler.
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Field handler to provide simple renderer that allows linking to a node.
|
| 10 |
*/
|
| 11 |
class views_calc_field_handler extends views_handler_field {
|
| 12 |
/**
|
| 13 |
* Constructor to provide additional field to add.
|
| 14 |
*/
|
| 15 |
function construct() {
|
| 16 |
parent::construct();
|
| 17 |
$this->additional_fields['nid'] = 'nid';
|
| 18 |
}
|
| 19 |
|
| 20 |
function option_definition() {
|
| 21 |
$options = parent::option_definition();
|
| 22 |
$options['link_to_node'] = array('default' => FALSE);
|
| 23 |
return $options;
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Provide link to node option
|
| 28 |
*/
|
| 29 |
function options_form(&$form, &$form_state) {
|
| 30 |
parent::options_form($form, $form_state);
|
| 31 |
$form['link_to_node'] = array(
|
| 32 |
'#title' => t('Link this field to its node'),
|
| 33 |
'#type' => 'checkbox',
|
| 34 |
'#default_value' => !empty($this->options['link_to_node']),
|
| 35 |
);
|
| 36 |
}
|
| 37 |
|
| 38 |
/**
|
| 39 |
* Render whatever the data is as a link to the node.
|
| 40 |
*
|
| 41 |
* Data should be made XSS safe prior to calling this function.
|
| 42 |
*/
|
| 43 |
function render_link($data, $values) {
|
| 44 |
if (!empty($this->options['link_to_node'])) {
|
| 45 |
return l($data, "node/" . $values->{$this->aliases['nid']}, array('html' => TRUE));
|
| 46 |
}
|
| 47 |
else {
|
| 48 |
return $data;
|
| 49 |
}
|
| 50 |
}
|
| 51 |
|
| 52 |
/**
|
| 53 |
* Find the right calculation and add it to the query as
|
| 54 |
* an aliased field.
|
| 55 |
*/
|
| 56 |
function query() {
|
| 57 |
$results = _views_calc_fields();
|
| 58 |
while ($calc_field = db_fetch_array($results)) {
|
| 59 |
if ($this->definition['cid'] == $calc_field['cid']) {
|
| 60 |
foreach (explode(',', $calc_field['tablelist']) as $table) {
|
| 61 |
$this->view->query->add_table($table);
|
| 62 |
}
|
| 63 |
$this->view->query->add_field(NULL, "({$calc_field['calc']})", "cid". $calc_field['cid']);
|
| 64 |
return;
|
| 65 |
}
|
| 66 |
}
|
| 67 |
}
|
| 68 |
|
| 69 |
function pre_query() {
|
| 70 |
$this->field_alias = "cid{$this->definition['cid']}";
|
| 71 |
parent::pre_query();
|
| 72 |
}
|
| 73 |
|
| 74 |
/**
|
| 75 |
* Use the requested format function to render the raw alias value.
|
| 76 |
*/
|
| 77 |
function render($values) {
|
| 78 |
$field_alias = "cid{$this->definition['cid']}";
|
| 79 |
|
| 80 |
$value = $values->$field_alias;
|
| 81 |
$formats = _views_calc_format_options();
|
| 82 |
$format = $formats[$this->definition['format']];
|
| 83 |
$tmp = explode(':', $format);
|
| 84 |
$function = trim($tmp[0]);
|
| 85 |
$vars = count($tmp) == 2 ? $tmp[1] : '';
|
| 86 |
if ($function == 'custom') {
|
| 87 |
$tmp = explode(':', $this->definition['custom']);
|
| 88 |
$function = trim($tmp[0]);
|
| 89 |
$vars = count($tmp) == 2 ? $tmp[1] : '';
|
| 90 |
}
|
| 91 |
if (empty($function) || $function == 'none') {
|
| 92 |
$function = 'check_plain';
|
| 93 |
}
|
| 94 |
$raw = $function($value, $vars);
|
| 95 |
|
| 96 |
// This needs to be set for the $this->render_link() to work. It would
|
| 97 |
// have been set in the query, if we hadn't bypassed the normal query.
|
| 98 |
// TODO there may be a better way to do this.
|
| 99 |
$this->aliases['nid'] = 'nid';
|
| 100 |
|
| 101 |
return $this->render_link($raw, $values);
|
| 102 |
}
|
| 103 |
}
|