View source
<?php
class views_handler_filter_heartbeat_access extends views_handler_filter_numeric {
public $heartbeat_access = -1;
public $heartbeat_relations = array();
public $whoisuser = array(
0 => 'Acting user',
1 => 'Requested user (from url)',
);
function construct() {
parent::construct();
$this->value_value = t('Are restricted to access type');
$this->value['whoisuser'] = 0;
$this->value['access'] = 0;
}
function option_definition() {
$options = parent::option_definition();
return $options;
}
function options(&$options) {
parent::options($options);
$options['value']['access'] = array();
$options['value']['whoisuser'] = array();
}
function operators() {
$operators = array(
'=' => array(
'title' => t('Is equal to'),
'method' => 'op_simple',
'short' => t('='),
'values' => 1,
),
'!=' => array(
'title' => t('Is not equal to'),
'method' => 'op_simple',
'short' => t('!='),
'values' => 1,
),
);
if (!empty($this->definition['allow empty'])) {
$operators += array(
'empty' => array(
'title' => t('Is empty (NULL)'),
'method' => 'op_empty',
'short' => t('empty'),
'values' => 0,
),
'not empty' => array(
'title' => t('Is not empty (NULL)'),
'method' => 'op_empty',
'short' => t('not empty'),
'values' => 0,
),
);
}
return $operators;
}
function admin_summary() {
$options = _heartbeat_access_type_options();
$key = (int) (isset($this->value['access']) ? $this->value['access'] : HEARTBEAT_PRIVATE);
$return = $options[$key];
if (isset($this->whoisuser[$this->value['whoisuser']])) {
$return .= ': ' . $this->whoisuser[$this->value['whoisuser']];
}
return $return;
}
function query() {
global $user;
if ($this->value['whoisuser'] == 1) {
if (arg(0) == 'heartbeat' && arg(1) == 'user' && is_numeric(arg(2))) {
$uid = arg(2);
}
elseif (arg(0) == 'user' && is_numeric(arg(1)) && arg(2) != 'edit') {
$uid = arg(1);
}
else {
$uid = $user->uid;
}
}
else {
$uid = $user->uid;
}
$this
->ensure_my_table();
$info = $this
->operators();
$access = $this->value['access'];
$field = "{$this->table_alias}.{$this->real_field}";
$this->heartbeat_access = $access;
switch ($access) {
case HEARTBEAT_PUBLIC_TO_CONNECTED:
$uids = heartbeat_get_related_uids($uid);
$this->heartbeat_relations = $uids;
$this->query
->set_where_group('OR', 'orgroup');
$sql = "{$field} in( " . implode(',', $uids) . " ) AND {$this->table_alias}.access >= 0 ";
$this->query
->add_where('orgroup', $sql);
$sql = "{$this->table_alias}.uid_target = %d AND {$this->table_alias}.nid_target <> 0";
$this->query
->add_where('orgroup', $sql, $uid);
break;
case HEARTBEAT_PRIVATE:
$this->query
->set_where_group('OR', 'orgroup');
$uids = heartbeat_get_related_uids($uid);
$this->heartbeat_relations = $uids;
$sql = "{$field} = %d AND {$this->table_alias}.access = %d ";
$this->query
->add_where('orgroup', $sql, $uid, $access);
$sql = "({$this->table_alias}.uid_target = %d AND {$this->table_alias}.access > 0)";
$this->query
->add_where('orgroup', $sql, $uid);
break;
case HEARTBEAT_PUBLIC_TO_ALL:
$sql = "{$field} > %d AND {$this->table_alias}.access > 0";
$this->query
->add_where('andgroup', $sql, 0);
break;
default:
$sql = "{$field} = %d";
$this->query
->add_where('andgroup', $sql, $uid);
}
}
function value_form(&$form, &$form_state) {
if (empty($form_state['exposed'])) {
$form['value']['access'] = array(
'#type' => 'radios',
'#title' => t('Access type'),
'#options' => _heartbeat_access_type_options(),
'#default_value' => !empty($this->value['access']) ? $this->value['access'] : HEARTBEAT_PRIVATE,
);
$form['value']['whoisuser'] = array(
'#type' => 'radios',
'#title' => t('Who is targetuser'),
'#options' => $this->whoisuser,
'#default_value' => !empty($this->value['whoisuser']) ? $this->value['whoisuser'] : 0,
);
}
parent::value_form($form, $form_state);
}
}