Reworked init system, updated patches
[project/i18n.git] / i18n_node.inc
CommitLineData
d469e0bb
JR
1<?php
2// $Id$
3
4/**
5 * Internationalization (i18n) module
6 *
7 * @author Jose A. Reyero, 2004
8 *
9 * Multilingual Nodes support
10 */
11
12/**
c6713f3d
JR
13 * Implementation of hook_node_rewrite_sql()
14 */
265bd97e 15function i18n_node_rewrite_sql($query,$node_alias){
c6713f3d 16 $result = array();
265bd97e
JR
17 // Check for some special cases, no hints anymore
18 if ($query == '') {
19 // No query, search case
20 // $result['where'] = "$node_alias.language='".i18n_get_lang()."'";
21 } elseif (!strpos($query,'{node}')) {
22 // Node table is not included, join it
c6713f3d 23 $result['join'] = "INNER JOIN {node} USING(nid)";
265bd97e 24 $result['where'] = "{node}.language ='".i18n_get_lang()."' or {node}.language = ''" ;
c6713f3d 25 } else {
265bd97e
JR
26 // Default case, simple where condition
27 $result['where'] = "$node_alias.language ='".i18n_get_lang()."' OR $node_alias.language = ''" ;
c6713f3d
JR
28 }
29 return $result;
30}
31/**
d469e0bb
JR
32 * This is the callback for the tab 'translations' for nodes
33 */
34function i18n_node_translation() {
35 $op = $_POST['op'];
36 $edit = $_POST['edit'];
37
38 if (empty($op)) {
39 $op = arg(2);
40 }
41
42 switch ($op) {
43 case 'translation':
44 $output = i18n_node_translation_status(arg(1));
45 break;
46 }
47 print theme('page', $output);
48}
49
50function i18n_node_translation_status($nid) {
51 $languages = i18n_supported_languages();
52 $translations = i18n_node_get_translations($nid, true);
53 if (!$translations) {
54 $node = node_load(array('nid' => $nid));
55 $translations[$node->language]= $node;
56 }
57 foreach ($languages as $lang => $name) {
58 if (array_key_exists($lang, $translations)) {
59 $node = $translations[$lang];
60 $rows[] = array($name, l($node->title, 'node/'.$node->nid), l(t('edit'), 'node/'.$node->nid.'/edit'));
61 if ($node->nid == $nid) {
62 drupal_set_title(t('Translations of ').$node->title);
63 }
64 }
65 else {
66 $rows[] = array($name, t('Not translated'), l(t('create translation'), 'translation/node/'.$nid.'/'.$lang));
67 }
68 }
69 $header = array(t('Language'), t('Title'), t('Actions'));
70 $output .= t('<h2>Translation Status</h2>') .'<br />';
71 $output .= theme('table', $header, $rows);
72 return $output;
73}
74
75/**
76 * Hook nodeapi
77 */
78function i18n_nodeapi(&$node, $op, $arg = 0) {
79
80 if (variable_get("i18n_node_$node->type", 0)) {
81 switch ($op) {
82 case 'fields':
83 return array('language');
84 break;
85
86 case 'validate':
87 break;
88
89 case 'insert':
90 if ( arg(0) == 'translation' ) {
91 i18n_node_create_translation($node->nid, $node->language, $node->i18n_snid);
92 }
93 break;
94
95 case 'delete':
96 db_query('DELETE FROM {i18n_node} WHERE nid=%d', $node->nid);
97 break;
98
99 case 'update':
100 // Check language changed?
101 break;
102
103 case 'form pre':
104 $languages = i18n_supported_languages();
105 if ($node->nid) {
106 $translations = i18n_node_get_translations($node->nid);
107 }
108 elseif (arg(0) == 'translation') {
109 // We are translating a node
110 $snid = arg(2);
111 $node->language = arg(3);
112 $form = form_hidden('i18n_snid', $node->i18n_snid ? $node->i18n_snid : $snid);
113 // Load the node to be translated
114 $trans = node_load(array('nid' => $snid));
115 foreach ($trans as $field=>$value) {
116 if ($field != 'nid' && empty($node->$field)) {
117 $node->$field = $value;
118 }
119 }
120 $translations = i18n_node_get_translations($snid, true);
121
122 if (!$translations) {
123 $translations[$trans->language]= $trans;
124 }
125 }
126
127 if ($translations) {
128 foreach ($languages as $lang => $langname) {
129 if ($trans = $translations[$lang]) {
130 $rows[] = array($langname, l($trans->title, 'node/'.$trans->nid.'/edit'));
131 }
132 else {
133 $langselect[$lang]= $langname;
134 }
135 }
136 }
137 else {
138 $langselect = $languages;
139 }
140
141 $langselect = array_merge(array('' => ''), $langselect);
142 $form .= form_select(t('Language'), 'language', $node->language ? $node->language : i18n_get_lang(), $langselect);
143 if ($rows) {
144 $header = array(t('Language'), t('Title'));
145 $form .= form_group(t('translations'), theme('table', $header, $rows));
146 }
147
148 return $form;
149 break;
150 }
151 }
152}
153
154function i18n_node_available_langs($node) {
155 $languages = i18n_supported_languages();
156 if ($node->nid) {
157 $usedlangs = i18n_node_get_translations($node->nid);
158 }
159 elseif ($node->i18n_snid) {
160 $usedlangs = i18n_node_get_translations($node->i18n_snid, true);
161 }
162 else {
163 return $languages;
164 }
165
166 foreach ($languages as $lang=>$name) {
167 if (!array_key_exists($lang, $usedlangs)) {
168 $newlangs[$lang] = $name;
169 }
170 }
171
172 return $newlangs;
173
174}
175
176function i18n_node_get_lang($nid, $default = '') {
177 $lang = db_result(db_query('SELECT language FROM {node} WHERE nid=%d',$nid));
178 return $lang ? $lang : $default ;
179}
180
181/**
182 * getall = true to get the also node itself
183 */
184
185function i18n_node_get_translations($nid, $getall = false) {
186 $sql = 'SELECT n.nid, n.language, n.title, a.trid FROM {node} n INNER JOIN {i18n_node} a ON n.nid = a.nid INNER JOIN {i18n_node} b ON a.trid = b.trid AND b.nid =%d';
187 if ($getall) {
188 $result = db_query($sql, $nid);
189 }
190 else {
191 $result = db_query($sql . ' WHERE n.nid != %d', $nid, $nid);
192 }
193 $items = array();
194 while ($node = db_fetch_object($result)) {
195 $items[$node->language] = $node;
196 }
197 //debug($items);
198 return $items;
199}
200
201// snid = source nid
202function i18n_node_create_translation($nid, $lang, $snid) {
203 $trid = db_result(db_query('SELECT trid FROM {i18n_node} WHERE nid=%d', $snid));
204 if (!$trid) {
205 $trid = db_next_id('{i18n_node}_trid');
206 db_query('INSERT INTO {i18n_node} (trid, nid) VALUES (%d, %d)', $trid, $snid);
207 }
208 db_query('INSERT INTO {i18n_node}(trid, nid) VALUES (%d, %d)', $trid, $nid);
209}
210
211function i18n_node_where_sql() {
212 return " AND ( n.language='" . i18n_get_lang(). "' OR n.language='')";
213}
214
215function i18n_get_node_type($nid) {
216 return db_result(db_query('SELECT type FROM {node} WHERE nid=%d', $nid));
217}
218
219?>