| 1 |
<?php
|
| 2 |
// $Id: i18nviews.module,v 1.3 2008/02/15 22:51:41 jareyero Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Views support for Internationalization (i18n) package
|
| 7 |
*
|
| 8 |
* This module uses i18n module namespace to add some views hooks
|
| 9 |
*
|
| 10 |
* @author Jose A. Reyero, 2007
|
| 11 |
*/
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Views integration
|
| 15 |
*/
|
| 16 |
function i18n_views_tables() {
|
| 17 |
$tables['i18n'] = array(
|
| 18 |
'name' => 'i18n_node',
|
| 19 |
'join' => array(
|
| 20 |
'left' => array(
|
| 21 |
'table' => 'node',
|
| 22 |
'field' => 'nid'
|
| 23 |
),
|
| 24 |
'right' => array(
|
| 25 |
'field' => 'nid'
|
| 26 |
)
|
| 27 |
)
|
| 28 |
);
|
| 29 |
$tables['i18n']['fields']['language'] = array(
|
| 30 |
'name' => t('Internationalization: Language'),
|
| 31 |
'sortable' => true,
|
| 32 |
'handler' => 'i18n_views_handler_language',
|
| 33 |
);
|
| 34 |
$tables['i18n']['filters']['language'] = array(
|
| 35 |
'name' => t('Internationalization: Language'),
|
| 36 |
'list-type' => 'list',
|
| 37 |
'value-type' => 'array',
|
| 38 |
'list' => 'i18n_views_language_list',
|
| 39 |
'operator' => 'views_handler_operator_or',
|
| 40 |
'help' => t('Enabled languages for content.'),
|
| 41 |
);
|
| 42 |
$tables['i18n']['filters']['extlanguage'] = array(
|
| 43 |
'field' => 'language',
|
| 44 |
'name' => t('Internationalization: Language (extended)'),
|
| 45 |
'list-type' => 'list',
|
| 46 |
'value-type' => 'array',
|
| 47 |
'list' => 'i18n_views_language_list_all',
|
| 48 |
'operator' => 'views_handler_operator_or',
|
| 49 |
'help' => t('All defined languages for content.'),
|
| 50 |
);
|
| 51 |
$tables['i18n']['filters']['selection'] = array(
|
| 52 |
'name' => t('Internationalization: Selection'),
|
| 53 |
'handler' => 'i18n_views_filter_handler',
|
| 54 |
'list-type' => 'select',
|
| 55 |
'operator' => array('=' => t('Is')),
|
| 56 |
'list' => '_i18n_selection_mode',
|
| 57 |
'help' => t('Content language.'),
|
| 58 |
'cacheable' => 'no' // This query condition cannot be cached
|
| 59 |
);
|
| 60 |
return $tables;
|
| 61 |
}
|
| 62 |
|
| 63 |
function i18n_views_language_list($a, $b){
|
| 64 |
return locale_language_list();
|
| 65 |
}
|
| 66 |
function i18n_views_language_list_all($a, $b){
|
| 67 |
return locale_language_list('name', TRUE);
|
| 68 |
}
|
| 69 |
function i18n_views_handler_language($fieldinfo, $fielddata, $value, $data) {
|
| 70 |
$languages = locale_language_list('name', TRUE);
|
| 71 |
return $languages[$value];
|
| 72 |
}
|
| 73 |
|
| 74 |
/**
|
| 75 |
* Filter handler callback. Manages language selection mode
|
| 76 |
*/
|
| 77 |
function i18n_views_filter_handler($filterdata, $filterinfo, &$query) {
|
| 78 |
if ($filterdata == 'handler' && $filterinfo['field'] == 'i18n.selection' && $mode = $filterinfo['value']) {
|
| 79 |
// If this filter is used, rollback value set in pre_query and set the new one.
|
| 80 |
i18n_selection_mode('reset');
|
| 81 |
i18n_selection_mode($mode);
|
| 82 |
}
|
| 83 |
}
|
| 84 |
/**
|
| 85 |
* Implementation of hook_views_pre_query().
|
| 86 |
*
|
| 87 |
* Disable language conditions for views. This is called before filter handlers
|
| 88 |
*/
|
| 89 |
function i18n_views_pre_query(&$view) {
|
| 90 |
// If any language filter, the language selection mode will be 'off'
|
| 91 |
foreach($view->exposed_filter as $filter) {
|
| 92 |
if($filter['field'] == 'i18n.language') {
|
| 93 |
i18n_selection_mode('off');
|
| 94 |
return;
|
| 95 |
}
|
| 96 |
}
|
| 97 |
// If no filter, sets the one from settings for this view
|
| 98 |
i18n_selection_mode(variable_get('i18n_selection_mode', 'simple'));
|
| 99 |
}
|
| 100 |
/**
|
| 101 |
* Implementation of hook_views_pre_view().
|
| 102 |
*
|
| 103 |
* If used language selection filter, reset query rewriting again
|
| 104 |
*/
|
| 105 |
function i18n_views_pre_view(&$view, &$items) {
|
| 106 |
i18n_selection_mode('reset');
|
| 107 |
}
|
| 108 |
|
| 109 |
/**
|
| 110 |
* Implementation of hook_form_alter()
|
| 111 |
*
|
| 112 |
* @ TODO Copied from i18ntaxonomy, update all this
|
| 113 |
*/
|
| 114 |
function i18nviews_form_alter(&$form, $form_state, $form_id) {
|
| 115 |
|
| 116 |
if ($form_id == 'views_filters' && $translate = variable_get('i18ntaxonomy_vocabularies', array())) {
|
| 117 |
// We only translate exposed filters here
|
| 118 |
$view = $form['view']['#value'];
|
| 119 |
if($view->exposed_filter) {
|
| 120 |
foreach($view->exposed_filter as $index => $filter) {
|
| 121 |
$matches = array();
|
| 122 |
if($filter['field'] == 'term_node.tid') {
|
| 123 |
// That's a full taxonomy box. Translate options: arary(tid => "Vocabulary: Term")
|
| 124 |
// First, we get a translated list. Then we replace on the options array
|
| 125 |
$replace = _i18ntaxonomy_vocabulary_terms(array_keys($translate));
|
| 126 |
foreach($replace as $tid => $name) {
|
| 127 |
if(isset($form["filter$index"]['#options'][$tid])) {
|
| 128 |
$form["filter$index"]['#options'][$tid] = $name;
|
| 129 |
}
|
| 130 |
}
|
| 131 |
} elseif(preg_match("/term_node_(\d+)\.tid/", $filter['field'], $matches)) {
|
| 132 |
$vid = $matches[1];
|
| 133 |
if ($translate[$vid]) {
|
| 134 |
// Translate this vocabulary terms, field name is filter$index vid = $matches[1]
|
| 135 |
foreach ($form["filter$index"]['#options'] as $value => $option) {
|
| 136 |
if ($value != '**ALL**') { // ALL option should be already localized
|
| 137 |
// This may be an object with an option property being an array (tid => name)
|
| 138 |
if (is_object($option) && is_array($option->option)) {
|
| 139 |
foreach (array_keys($option->option) as $tid) {
|
| 140 |
$option->option[$tid] = t($option->option[$tid]);
|
| 141 |
}
|
| 142 |
$form["filter$index"]['#options'][$value] = $option;
|
| 143 |
// But it used to be a plain string, so let's keep this just in case...
|
| 144 |
} elseif(is_string($option)) {
|
| 145 |
$form["filter$index"]['#options'][$value] = t($option);
|
| 146 |
}
|
| 147 |
}
|
| 148 |
}
|
| 149 |
}
|
| 150 |
}
|
| 151 |
}
|
| 152 |
}
|
| 153 |
}
|
| 154 |
}
|