| 1 |
<?php
|
| 2 |
// $Id: translatable.database.inc,v 1.2 2008/04/10 16:57:26 sun Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Returns database definitions for a given object type.
|
| 6 |
*
|
| 7 |
* @param string $type
|
| 8 |
* An object type, either 'node' or 'translation'.
|
| 9 |
*
|
| 10 |
* @return array
|
| 11 |
* An array containing table name and key of the object, optionally including
|
| 12 |
* a parent key and additional table columns.
|
| 13 |
*/
|
| 14 |
function translatable_get_object_definition($type) {
|
| 15 |
switch ($type) {
|
| 16 |
case 'node':
|
| 17 |
return array(
|
| 18 |
'table' => 'node_translatable',
|
| 19 |
'key' => 'nid',
|
| 20 |
'parentkey' => 'tnid',
|
| 21 |
'language' => 'language',
|
| 22 |
'any' => 'any',
|
| 23 |
);
|
| 24 |
|
| 25 |
case 'translation':
|
| 26 |
return array(
|
| 27 |
'table' => 'translatable_object',
|
| 28 |
'key' => 'tid',
|
| 29 |
'object_name' => 'object_name',
|
| 30 |
'object_key' => 'object_key',
|
| 31 |
'object_field' => 'object_field',
|
| 32 |
'translation' => 'translation',
|
| 33 |
'locale' => 'locale',
|
| 34 |
);
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
/**
|
| 39 |
* Find a content translation in translation index.
|
| 40 |
*
|
| 41 |
* @param string $type
|
| 42 |
* The object type to search for.
|
| 43 |
* @param string $conditions
|
| 44 |
* A SQL where clause to search for.
|
| 45 |
* @param bool $all
|
| 46 |
* Whether to return one or all query result rows.
|
| 47 |
* @param bool $force
|
| 48 |
* Whether to omit cached query results.
|
| 49 |
* @param array $args
|
| 50 |
* Additional query arguments, supplied as keys 'fields', 'group by', and
|
| 51 |
* 'having'.
|
| 52 |
*
|
| 53 |
* @return array
|
| 54 |
* Either a single (see $all) or an array containing all query result(s).
|
| 55 |
*/
|
| 56 |
function translatable_find($type, $conditions = '', $all = TRUE, $force = FALSE, $args = array()) {
|
| 57 |
static $objects = array();
|
| 58 |
|
| 59 |
$cid = $type .':'. $conditions .':'. (int)$all;
|
| 60 |
if (isset($objects[$cid]) && !$force) {
|
| 61 |
return $objects[$cid];
|
| 62 |
}
|
| 63 |
|
| 64 |
// Get schema definition for object type.
|
| 65 |
$object = translatable_get_object_definition($type);
|
| 66 |
|
| 67 |
$fields = '*'. (isset($args['fields']) ? ', '. implode(', ', (array)$args['fields']) : '');
|
| 68 |
$sql = 'SELECT '. $fields .' FROM {'. $object['table'] .'}';
|
| 69 |
if ($conditions) {
|
| 70 |
$sql .= ' WHERE '. $conditions;
|
| 71 |
}
|
| 72 |
if (isset($args['group by'])) {
|
| 73 |
$sql .= ' GROUP BY '. implode(', ', (array)$args['group by']);
|
| 74 |
}
|
| 75 |
if (isset($args['having'])) {
|
| 76 |
$sql .= ' HAVING '. implode(' AND ', (array)$args['having']);
|
| 77 |
}
|
| 78 |
$result = $all ? db_query($sql) : db_query_range($sql, 0, 1);
|
| 79 |
|
| 80 |
// A single item will be directly returned, while multiple items are stored
|
| 81 |
// as collection.
|
| 82 |
if ($all) {
|
| 83 |
$items = array();
|
| 84 |
while ($item = db_fetch_array($result)) {
|
| 85 |
$key = $item[$object['key']];
|
| 86 |
$items[$key] = $item;
|
| 87 |
}
|
| 88 |
$objects[$cid] = $items;
|
| 89 |
}
|
| 90 |
else {
|
| 91 |
$objects[$cid] = db_fetch_array($result);
|
| 92 |
}
|
| 93 |
|
| 94 |
return $objects[$cid];
|
| 95 |
}
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Fetch a single content from translation index.
|
| 99 |
*/
|
| 100 |
function translatable_findbyid($type, $id, $force = FALSE) {
|
| 101 |
$object = translatable_get_object_definition($type);
|
| 102 |
return translatable_find($type, $object['key'] .' = '. (int)$id, FALSE, $force);
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Save a content to translation index.
|
| 107 |
*/
|
| 108 |
function translatable_save($type, $item) {
|
| 109 |
if (!is_array($item)) {
|
| 110 |
return FALSE;
|
| 111 |
}
|
| 112 |
$object = translatable_get_object_definition($type);
|
| 113 |
|
| 114 |
if ($type == 'translation' && empty($item[$object['key']])) {
|
| 115 |
// @doc When will the item's key not be set?
|
| 116 |
$translation = translatable_find('translation', "object_name = '". $item['object_name'] ."' AND object_key = '". $item['object_key'] ."' AND object_field = '". $item['object_field'] ."' AND locale = '". $item['locale'] ."'", FALSE);
|
| 117 |
$item[$object['key']] = $translation[$object['key']];
|
| 118 |
}
|
| 119 |
|
| 120 |
if (translatable_findbyid($type, $item[$object['key']])) {
|
| 121 |
return translatable_update($type, $item);
|
| 122 |
}
|
| 123 |
else {
|
| 124 |
if (isset($object['parentkey']) && empty($item[$object['parentkey']])) {
|
| 125 |
// If no source content id is given, set source content id to current
|
| 126 |
// content id.
|
| 127 |
$item[$object['parentkey']] = $item[$object['key']];
|
| 128 |
}
|
| 129 |
return translatable_insert($type, $item);
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
| 133 |
/**
|
| 134 |
* Insert a new content into translation index.
|
| 135 |
*/
|
| 136 |
function translatable_insert($type, $item) {
|
| 137 |
if (!is_array($item)) {
|
| 138 |
return FALSE;
|
| 139 |
}
|
| 140 |
$object = translatable_get_object_definition($type);
|
| 141 |
|
| 142 |
if ($type == 'translation') {
|
| 143 |
$item[$object['key']] = db_next_id('{translatable_object}_'. $object['key']);
|
| 144 |
}
|
| 145 |
|
| 146 |
$table = $object['table'];
|
| 147 |
unset($object['table']);
|
| 148 |
|
| 149 |
$columns = $types = $values = array();
|
| 150 |
foreach ($object as $key => $column) {
|
| 151 |
$columns[] = $column;
|
| 152 |
if ($key == 'key' || $key == 'parentkey') {
|
| 153 |
$types[] = '%d';
|
| 154 |
}
|
| 155 |
else {
|
| 156 |
$types[] = "'%s'";
|
| 157 |
}
|
| 158 |
$values[] = $item[$column];
|
| 159 |
}
|
| 160 |
db_query('INSERT INTO {'. $table .'} ('. implode(', ', $columns) .') VALUES ('. implode(', ', $types) .')', $values);
|
| 161 |
return $item[$object['key']];
|
| 162 |
}
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Update a content in translation index.
|
| 166 |
*/
|
| 167 |
function translatable_update($type, $item) {
|
| 168 |
if (!is_array($item)) {
|
| 169 |
return FALSE;
|
| 170 |
}
|
| 171 |
$object = translatable_get_object_definition($type);
|
| 172 |
|
| 173 |
$table = $object['table'];
|
| 174 |
unset($object['table']);
|
| 175 |
|
| 176 |
$assigns = $values = array();
|
| 177 |
foreach ($object as $key => $column) {
|
| 178 |
if ($key == 'key') {
|
| 179 |
$where = $column .' = '. $item[$column];
|
| 180 |
continue;
|
| 181 |
}
|
| 182 |
if ($key == 'parentkey') {
|
| 183 |
$assigns[] = $column .' = %d';
|
| 184 |
}
|
| 185 |
else {
|
| 186 |
$assigns[] = $column ." = '%s'";
|
| 187 |
}
|
| 188 |
$values[] = $item[$column];
|
| 189 |
}
|
| 190 |
return db_query('UPDATE {'. $table .'} SET '. implode(', ', $assigns) .' WHERE '. $where, $values);
|
| 191 |
}
|
| 192 |
|
| 193 |
/**
|
| 194 |
* Delete a translation reference in translation index.
|
| 195 |
*
|
| 196 |
* @todo If a source content is deleted, all translations of this node have to
|
| 197 |
* be deleted, too.
|
| 198 |
*/
|
| 199 |
function translatable_delete($type, $id) {
|
| 200 |
if ($id) {
|
| 201 |
$object = translatable_get_object_definition($type);
|
| 202 |
db_query('DELETE FROM {%s} WHERE %s = %d', $object['table'], $object['key'], $id);
|
| 203 |
}
|
| 204 |
}
|
| 205 |
|
| 206 |
/**
|
| 207 |
* Delete all translations for a given condition.
|
| 208 |
*/
|
| 209 |
function translatable_delete_all($type, $conditions) {
|
| 210 |
if ($conditions) {
|
| 211 |
$object = translatable_get_object_definition($type);
|
| 212 |
db_query('DELETE FROM {%s} WHERE '. $conditions, $object['table']);
|
| 213 |
}
|
| 214 |
}
|
| 215 |
|