View source
<?php
function recommender_views_data() {
$views_data = array();
$recommender_data = recommender_fetch_data();
$tables = array();
foreach ($recommender_data as $name => $def) {
$raw_structure = $def['data structure'];
$default_structure = recommender_prepare_data_structure($raw_structure);
$item_entity_info = entity_get_info(@$default_structure['item entity type']);
$user_entity_info = entity_get_info(@$default_structure['user entity type']);
if (isset($raw_structure['preference']) && $default_structure['preference']['type'] == 'table' && !@$raw_structure['preference']['no views']) {
$table_structure = $default_structure['preference'];
$tables[$table_structure['name']][] = $def['title'];
$views_data[$table_structure['name']] = _recommender_views_template_table($table_structure, 'preference', implode(', ', $tables[$table_structure['name']]), $user_entity_info, $item_entity_info);
}
if (isset($raw_structure['user similarity']) && $default_structure['user similarity']['type'] == 'table' && !@$raw_structure['user similarity']['no views']) {
$table_structure = $default_structure['user similarity'];
$tables[$table_structure['name']][] = $def['title'];
$views_data[$table_structure['name']] = _recommender_views_template_table($table_structure, 'user similarity', implode(', ', $tables[$table_structure['name']]), $user_entity_info, $item_entity_info);
}
if (isset($raw_structure['item similarity']) && $default_structure['item similarity']['type'] == 'table' && !@$raw_structure['item similarity']['no views']) {
$table_structure = $default_structure['item similarity'];
$tables[$table_structure['name']][] = $def['title'];
$views_data[$table_structure['name']] = _recommender_views_template_table($table_structure, 'item similarity', implode(', ', $tables[$table_structure['name']]), $user_entity_info, $item_entity_info);
}
if (isset($raw_structure['prediction']) && $default_structure['prediction']['type'] == 'table' && !@$raw_structure['prediction']['no views']) {
$table_structure = $default_structure['prediction'];
$tables[$table_structure['name']][] = $def['title'];
$views_data[$table_structure['name']] = _recommender_views_template_table($table_structure, 'prediction', implode(', ', $tables[$table_structure['name']]), $user_entity_info, $item_entity_info);
}
}
return $views_data;
}
function _recommender_views_template_table($table_structure, $table_type, $table_human_readable_name, $user_entity_info, $item_entity_info) {
$data = array();
$data['table'] = array(
'group' => t('Recommender'),
'help' => "{$table_human_readable_name}: {$table_type}",
'base' => array(
'field' => 'id',
'title' => "{$table_human_readable_name}: {$table_type}",
),
);
switch ($table_type) {
case 'preference':
case 'prediction':
$data[$table_structure['user field']] = _recommender_views_template_user_field(t('!name: user in !type table', array(
'!name' => $table_human_readable_name,
'!type' => $table_type,
)), t('The user field in !type table', array(
'!type' => $table_type,
)), $user_entity_info);
break;
case 'user similarity':
$data[$table_structure['user1 field']] = _recommender_views_template_user_field(t('!name: user 1 in user similarity table', array(
'!name' => $table_human_readable_name,
)), t('The user 1 field in user similarity table'), $user_entity_info);
break;
case 'item similarity':
$data[$table_structure['item1 field']] = _recommender_views_template_item_field(t('!name: item 1 in item similarity table', array(
'!name' => $table_human_readable_name,
)), t('The item 1 field in item similarity table'), $item_entity_info);
break;
}
switch ($table_type) {
case 'preference':
case 'prediction':
$data[$table_structure['item field']] = _recommender_views_template_item_field(t('!name: item in !type table', array(
'!name' => $table_human_readable_name,
'!type' => $table_type,
)), t('The item field in !type table', array(
'!type' => $table_type,
)), $item_entity_info);
break;
case 'user similarity':
$data[$table_structure['user2 field']] = _recommender_views_template_user_field(t('!name: user 2 in user similarity table', array(
'!name' => $table_human_readable_name,
)), t('The user 2 field in user similarity table'), $user_entity_info);
break;
case 'item similarity':
$data[$table_structure['item2 field']] = _recommender_views_template_item_field(t('!name: item 2 in item similarity table', array(
'!name' => $table_human_readable_name,
)), t('The item 2 field in item similarity table'), $item_entity_info);
break;
}
$data[$table_structure['score field']] = _recommender_views_template_score_field(t('!name: score in !type table', array(
'!name' => $table_human_readable_name,
'!type' => $table_type,
)), t('The score field in !type table', array(
'!type' => $table_type,
)));
$data[$table_structure['timestamp field']] = _recommender_views_template_timestamp_field(t('!name: timestamp in !type table', array(
'!name' => $table_human_readable_name,
'!type' => $table_type,
)), t('The timestamp field in !type table', array(
'!type' => $table_type,
)));
return $data;
}
function _recommender_views_template_user_field($title, $help, $user_entity_info) {
$return = array(
'title' => $title,
'help' => $help,
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'relationship' => array(
'handler' => 'views_handler_relationship',
'base' => $user_entity_info['base table'],
'base field' => $user_entity_info['entity keys']['id'],
'label' => $title,
),
);
if ($user_entity_info['base table'] == 'users') {
$return['filter'] = array(
'handler' => 'views_handler_filter_user_name',
);
$return['argument'] = array(
'handler' => 'views_handler_argument_user_uid',
);
}
else {
$return['filter'] = array(
'handler' => 'views_handler_filter_numeric',
);
$return['argument'] = array(
'handler' => 'views_handler_argument_numeric',
);
}
return $return;
}
function _recommender_views_template_item_field($title, $help, $item_entity_info) {
$return = array(
'title' => $title,
'help' => $help,
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'relationship' => array(
'handler' => 'views_handler_relationship',
'base' => $item_entity_info['base table'],
'base field' => $item_entity_info['entity keys']['id'],
'label' => $title,
),
);
if ($item_entity_info['base table'] == 'node') {
$return['field'] = array(
'handler' => 'views_handler_field_node',
'click sortable' => TRUE,
);
$return['argument'] = array(
'handler' => 'views_handler_argument_node_nid',
'name field' => 'title',
'numeric' => TRUE,
'validate type' => 'nid',
);
}
else {
$return['field'] = array(
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
);
$return['argument'] = array(
'handler' => 'views_handler_argument_numeric',
);
}
return $return;
}
function _recommender_views_template_score_field($title, $help) {
return array(
'title' => $title,
'help' => $help,
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
}
function _recommender_views_template_timestamp_field($title, $help) {
return array(
'title' => $title,
'help' => $help,
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
);
}