| 1 |
<?php
|
| 2 |
// $Id: i18nblocks.module,v 1.6 2008/02/19 02:16:12 jareyero Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Internationalization (i18n) submodule: Multilingual meta-blocks
|
| 6 |
*
|
| 7 |
* @author Jose A. Reyero, 2005
|
| 8 |
*
|
| 9 |
* @ TODO Add strings on block update
|
| 10 |
*/
|
| 11 |
|
| 12 |
// Tag for localizable block, cannot be any language
|
| 13 |
define('I18N_BLOCK_LOCALIZE', '__LOCALIZE__');
|
| 14 |
|
| 15 |
// Block type: localizable
|
| 16 |
define('I18N_BLOCK_LOCALIZABLE', 1);
|
| 17 |
// Block type: block with language
|
| 18 |
define('I18N_BLOCK_LANGUAGE', 0);
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Block types
|
| 22 |
*/
|
| 23 |
function _block_types() {
|
| 24 |
return array(
|
| 25 |
I18N_BLOCK_LOCALIZE => t('Localizable block'),
|
| 26 |
I18N_BLOCK_METABLOCK => t('Multilingual block (Metablock)'),
|
| 27 |
);
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Implementation of hook_help()
|
| 32 |
*/
|
| 33 |
function i18nblocks_help($section, $arg) {
|
| 34 |
switch ($section) {
|
| 35 |
case 'admin/help#i18nblocks':
|
| 36 |
$output = '<p>'.t('This module provides support for multilingual blocks.').'</p>';
|
| 37 |
$output .= '<p>'.t('You can set up a language for a block or define it as translatable:').'</p>';
|
| 38 |
$output .= '<ul>';
|
| 39 |
$output .= '<li>'.t('Blocks with a language will be displayed only in pages with that language.').'</li>';
|
| 40 |
$output .= '<li>'.t('For translatable blocks, a new [Translatable] version of the block will be created.').'</li>';
|
| 41 |
$output .= '</ul>';
|
| 42 |
return $output;
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
/**
|
| 47 |
* Implementation of hook_db_rewrite_sql()
|
| 48 |
*/
|
| 49 |
function i18nblocks_db_rewrite_sql($query, $primary_table, $primary_key) {
|
| 50 |
global $language;
|
| 51 |
if ($primary_table == 'b' && $primary_key == 'bid') {
|
| 52 |
$return['join'] = 'LEFT JOIN {i18n_blocks} i18n ON b.module = i18n.module AND b.delta = i18n.delta';
|
| 53 |
$return['where'] = i18n_db_rewrite_where('i18n', 'block', 'simple');
|
| 54 |
return $return;
|
| 55 |
}
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* Implementation of hook_block()
|
| 60 |
*
|
| 61 |
* Blocks in i18n_blocks with no language are suppossed to be translatable
|
| 62 |
*/
|
| 63 |
function i18nblocks_block($op = 'list', $delta = 0, $edit = array()) {
|
| 64 |
switch($op) {
|
| 65 |
case 'list':
|
| 66 |
$blocks = array();
|
| 67 |
$result = db_query("SELECT i.*, b.info FROM {i18n_blocks} i INNER JOIN {boxes} b ON i.delta = b.bid WHERE i.module = 'block' AND i.language = ''");
|
| 68 |
while ($data = db_fetch_object($result)) {
|
| 69 |
$blocks[$data->ibid]['info'] = $data->info . t('[Translatable]');
|
| 70 |
}
|
| 71 |
return $blocks;
|
| 72 |
case 'view':
|
| 73 |
return i18nblocks_get_block($delta, i18n_get_lang());
|
| 74 |
break;
|
| 75 |
case 'configure':
|
| 76 |
$form['i18nblocks']['#value'] = t('This is a localizable block');
|
| 77 |
return $form;
|
| 78 |
}
|
| 79 |
}
|
| 80 |
|
| 81 |
/**
|
| 82 |
* Implementation of hook_locale().
|
| 83 |
*/
|
| 84 |
function i18nblocks_locale($op = 'groups') {
|
| 85 |
switch ($op) {
|
| 86 |
case 'groups':
|
| 87 |
return array('blocks' => t('Blocks'));
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
/**
|
| 92 |
* Implementation of block form_alter().
|
| 93 |
*
|
| 94 |
* Remove block title for multilingual blocks.
|
| 95 |
*/
|
| 96 |
function i18nblocks_form_alter(&$form, $form_state, $form_id) {
|
| 97 |
if (($form_id == 'block_admin_configure' || $form_id == 'block_box_form' || $form_id == 'block_add_block_form')) {
|
| 98 |
$module = $form['module']['#value'];
|
| 99 |
$delta = $form['delta']['#value'];
|
| 100 |
$form['i18n'] = array(
|
| 101 |
'#type' => 'fieldset',
|
| 102 |
'#title' => t('Multilingual settings'),
|
| 103 |
'#collapsible' => TRUE,
|
| 104 |
'#weight' => -1,
|
| 105 |
);
|
| 106 |
// For i18nblocks, just a help text
|
| 107 |
if ($module == 'i18nblocks') {
|
| 108 |
$form['i18n']['text'] = array('#value' => t('This is a translatable block.'));
|
| 109 |
// Unset block title
|
| 110 |
unset($form['block_settings']['title']);
|
| 111 |
$form['block_settings']['title'] = array('#type' => 'value', '#value' => '');
|
| 112 |
} else {
|
| 113 |
$i18nblock = i18nblocks_load($module, $delta);
|
| 114 |
$form['i18n'] = array(
|
| 115 |
'#type' => 'fieldset',
|
| 116 |
'#title' => t('Multilingual settings'),
|
| 117 |
'#collapsible' => TRUE,
|
| 118 |
'#weight' => 0,
|
| 119 |
);
|
| 120 |
// Language options will depend on block type
|
| 121 |
$options = array('' => t('All languages'));
|
| 122 |
if ($module == 'block') {
|
| 123 |
$options[I18N_BLOCK_LOCALIZE] = t('All languages (Translatable)');
|
| 124 |
}
|
| 125 |
$options += locale_language_list('name');
|
| 126 |
|
| 127 |
$form['i18n']['language'] = array(
|
| 128 |
'#type' => 'radios',
|
| 129 |
'#title' => t('Language'),
|
| 130 |
'#default_value' => $i18nblock->language,
|
| 131 |
'#options' => $options,
|
| 132 |
);
|
| 133 |
// Pass i18ndelta value
|
| 134 |
$form['i18n']['ibid'] = array('#type' => 'value', '#value' => $i18nblock->ibid);
|
| 135 |
$form['#submit'][] = 'i18nblocks_form_submit';
|
| 136 |
}
|
| 137 |
}
|
| 138 |
}
|
| 139 |
|
| 140 |
/**
|
| 141 |
* Forms api callback. Submit function
|
| 142 |
*/
|
| 143 |
function i18nblocks_form_submit($form, &$form_state) {
|
| 144 |
$values = $form_state['values'];
|
| 145 |
// Dirty trick to act on new created blocks
|
| 146 |
if (!$values['delta']) {
|
| 147 |
// The last insert id will return a different value in mysql
|
| 148 |
//$values['delta'] = db_last_insert_id('boxes', 'bid');
|
| 149 |
$values['delta'] = db_result(db_query("SELECT MAX(bid) FROM {boxes}"));
|
| 150 |
}
|
| 151 |
switch ($values['language']) {
|
| 152 |
case I18N_BLOCK_LOCALIZE: // Translatable block
|
| 153 |
$values['language'] = '';
|
| 154 |
i18nblocks_save($values);
|
| 155 |
break;
|
| 156 |
case '': // No language, delete all i18n information
|
| 157 |
if ($values['ibid']) {
|
| 158 |
db_query("DELETE FROM {i18n_blocks} WHERE ibid = %d", $values['ibid']);
|
| 159 |
}
|
| 160 |
break;
|
| 161 |
default: // The block has a language
|
| 162 |
i18nblocks_save($values);
|
| 163 |
}
|
| 164 |
}
|
| 165 |
|
| 166 |
/**
|
| 167 |
* Get block language data
|
| 168 |
*/
|
| 169 |
function i18nblocks_load($module, $delta) {
|
| 170 |
$block = db_fetch_object(db_query("SELECT * FROM {i18n_blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
|
| 171 |
// If no result, return default settings
|
| 172 |
if ($block && !$block->language) {
|
| 173 |
$block->language = I18N_BLOCK_LOCALIZE;
|
| 174 |
}
|
| 175 |
return $block ? $block : (object)array('language' => '', 'ibid' => 0);
|
| 176 |
}
|
| 177 |
|
| 178 |
/**
|
| 179 |
* Set block language data
|
| 180 |
*/
|
| 181 |
function i18nblocks_save($block) {
|
| 182 |
// Update strings for localizable blocks
|
| 183 |
if ($block['ibid']) {
|
| 184 |
db_query("UPDATE {i18n_blocks} SET language = '%s' WHERE ibid = %d", $block['ibid']);
|
| 185 |
} else {
|
| 186 |
db_query("INSERT INTO {i18n_blocks}(module, delta, language) VALUES('%s', '%s', '%s')", $block['module'], $block['delta'], $block['language']);
|
| 187 |
$block['ibid'] = db_last_insert_id('i18n_blocks', 'ibid');
|
| 188 |
}
|
| 189 |
if (!$block['language']) {
|
| 190 |
$delta = $block['ibid'];
|
| 191 |
tt("blocks:block:$delta:title", $block['title'], NULL, TRUE);
|
| 192 |
tt("blocks:block:$delta:content", $block['body'], NULL, TRUE);
|
| 193 |
}
|
| 194 |
}
|
| 195 |
|
| 196 |
|
| 197 |
/**
|
| 198 |
* Load and translate block data
|
| 199 |
*
|
| 200 |
* @param $delta
|
| 201 |
* Block id
|
| 202 |
* @param $language
|
| 203 |
* Language to localize the block
|
| 204 |
*/
|
| 205 |
function i18nblocks_get_block($delta, $language){
|
| 206 |
// Get block metadata
|
| 207 |
$meta = db_fetch_object(db_query("SELECT b.* FROM {blocks} b INNER JOIN {i18n_blocks} i ON i.delta = b.delta AND i.module = b.module WHERE i.ibid = '%d'", $delta));
|
| 208 |
// Get block data from module
|
| 209 |
$block = module_invoke($meta->module, 'block', 'view', $meta->delta);
|
| 210 |
|
| 211 |
if ($block) {
|
| 212 |
// Override the default title
|
| 213 |
if ($meta->title) {
|
| 214 |
// Check plain here to allow module generated titles to keep any markup.
|
| 215 |
$block['subject'] = $meta->title == '<none>' ? '' : check_plain(tt("blocks:block:$delta:title", $meta->title, $language));
|
| 216 |
} elseif (!empty($block['subject'])) {
|
| 217 |
$block['subject'] = tt("blocks:block:$delta:title", $block['subject'], $language);
|
| 218 |
}
|
| 219 |
$block['content'] = tt("blocks:block:$delta:content", $block['content'], $language);
|
| 220 |
return $block;
|
| 221 |
}
|
| 222 |
}
|