summaryrefslogtreecommitdiffstats
path: root/i18n_node.inc
blob: 7592e1495d812fa94dc8a1e3c574a14a74d41101 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
// $Id$

/**
 * Internationalization (i18n) module
 *
 * @author Jose A. Reyero, 2004
 * 
 * Multilingual Nodes support
 */

// translation/node/id/lang
// translation/term/id

function i18n_translation_page() {
  $op = $_POST['op'] ? $_POST['op'] : arg(1);
  $edit = $_POST['edit'];
  
  switch ($op) {
    case 'node':
      print theme('page', i18n_translation_add(arg(2), arg(3)));
      break;
      
     case t('Preview'):
      $edit = node_validate($edit);
      print theme('page', node_preview($edit), t('Preview'));
      break;
      
    case t('Submit'):
      drupal_set_title(t('Submit'));
      print theme('page', node_submit($edit));
      break;
      
    case t('Delete'):
      print theme('page', node_delete($edit), t('Delete'));
      break;
      
    default:
      // print theme('page', node_page_default(), '');
  }      
}

function i18n_translation_add($nid, $lang) {
  $type = i18n_get_node_type($nid);
  return node_add($type);
}

/**
 * This is the callback for the tab 'translations' for nodes
 */
function i18n_node_translation() {
  $op = $_POST['op'];
  $edit = $_POST['edit'];

  if (empty($op)) {
    $op = arg(2);
  }
  
  switch ($op) {
    case 'translation':
      $output = i18n_node_translation_status(arg(1));
      break;
  }
  print theme('page', $output);  
}

function i18n_node_translation_status($nid) {
  $languages = i18n_supported_languages();
  $translations = i18n_node_get_translations($nid, true);
  if (!$translations) {
    $node = node_load(array('nid' => $nid));
    $translations[$node->language]= $node;
  }
  foreach ($languages as $lang => $name) {
    if (array_key_exists($lang, $translations)) {
      $node = $translations[$lang];
      $rows[] = array($name, l($node->title, 'node/'.$node->nid), l(t('edit'), 'node/'.$node->nid.'/edit'));
      if ($node->nid == $nid) {
        drupal_set_title(t('Translations of ').$node->title);
      }
    }
    else {
      $rows[] = array($name, t('Not translated'), l(t('create translation'), 'translation/node/'.$nid.'/'.$lang));
  }
  }
  $header = array(t('Language'), t('Title'), t('Actions'));
  $output .= t('<h2>Translation Status</h2>') .'<br />';
  $output .= theme('table', $header, $rows);
  return $output;
}

/**
 * Hook nodeapi
 */
function i18n_nodeapi(&$node, $op, $arg = 0) {

  if (variable_get("i18n_node_$node->type", 0)) {
    switch ($op) {
      case 'fields':
        return array('language');
        break;
        
      case 'validate':
        break;
        
      case 'insert':
        if ( arg(0) == 'translation' ) {
          i18n_node_create_translation($node->nid, $node->language, $node->i18n_snid);
        }
        break;
  
      case 'delete':
        db_query('DELETE FROM {i18n_node} WHERE nid=%d', $node->nid);
        break;
        
      case 'update':
        // Check language changed?
        break;
        
      case 'form pre':
        $languages = i18n_supported_languages();
        if ($node->nid) {
          $translations = i18n_node_get_translations($node->nid);
        }
        elseif (arg(0) == 'translation') {
          // We are translating a node
          $snid = arg(2);
          $node->language = arg(3);
          $form = form_hidden('i18n_snid', $node->i18n_snid ? $node->i18n_snid : $snid);
          // Load the node to be translated  
          $trans = node_load(array('nid' => $snid));
          foreach ($trans as $field=>$value) {
            if ($field != 'nid' && empty($node->$field)) {
              $node->$field = $value;
            }
          }          
          $translations = i18n_node_get_translations($snid, true);

          if (!$translations) {
            $translations[$trans->language]= $trans;
          }
        }

        if ($translations) {
          foreach ($languages as $lang => $langname) {
            if ($trans = $translations[$lang]) {
              $rows[] = array($langname, l($trans->title, 'node/'.$trans->nid.'/edit'));
            }
            else {
              $langselect[$lang]= $langname;
            }
          }
        }
        else {
          $langselect = $languages;
        }
        
        $langselect = array_merge(array('' => ''), $langselect);
        $form .= form_select(t('Language'), 'language', $node->language ? $node->language : i18n_get_lang(), $langselect);
        if ($rows) {
           $header = array(t('Language'), t('Title'));
           $form .= form_group(t('translations'), theme('table', $header, $rows));
        }
    
        return $form;
        break;
    }
  }
}

function i18n_node_available_langs($node) {
  $languages = i18n_supported_languages();
  if ($node->nid) {
    $usedlangs = i18n_node_get_translations($node->nid);
  }
  elseif ($node->i18n_snid) {
    $usedlangs = i18n_node_get_translations($node->i18n_snid, true);
  }
  else {
    return $languages;
  }
  
  foreach ($languages as $lang=>$name) {
    if (!array_key_exists($lang, $usedlangs)) {
       $newlangs[$lang] = $name;
    }
  }
   
  return $newlangs;
    
}

function i18n_node_get_lang($nid, $default = '') {
  $lang = db_result(db_query('SELECT language FROM {node} WHERE nid=%d',$nid));
  return $lang ? $lang : $default ;
}

/**
	*	getall = true to get the also node itself
	*/

function i18n_node_get_translations($nid, $getall = false) {
  $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';
  if ($getall) {
    $result = db_query($sql, $nid);
  }
  else {
    $result = db_query($sql . ' WHERE n.nid != %d', $nid, $nid);
  }
  $items = array();
  while ($node = db_fetch_object($result)) {
      $items[$node->language] = $node;
  }
  //debug($items);
  return $items;
}

// snid = source nid
function i18n_node_create_translation($nid, $lang, $snid) {
  $trid = db_result(db_query('SELECT trid FROM {i18n_node} WHERE nid=%d', $snid));
  if (!$trid) {
    $trid = db_next_id('{i18n_node}_trid');
    db_query('INSERT INTO {i18n_node} (trid, nid) VALUES (%d, %d)', $trid, $snid);
  }
  db_query('INSERT INTO {i18n_node}(trid, nid) VALUES (%d, %d)', $trid, $nid);
}

function i18n_node_where_sql() {
  return " AND ( n.language='" . i18n_get_lang(). "' OR n.language='')";
}

function i18n_get_node_type($nid) {
  return db_result(db_query('SELECT type FROM {node} WHERE nid=%d', $nid));
}

?>