source: sipes/modules_contrib/term_fields/term_fields.install

stableversion-3.0
Last change on this file was 8337b9c, checked in by lhernandez <lhernandez@…>, 8 años ago

se agrego el modulo de campos para la taxonomia

  • Propiedad mode establecida a 100644
File size: 11.7 KB
Línea 
1<?php
2/**
3 * @file
4 * Installation file for the Term Fields module
5 */
6
7/**
8 * Implements hook_install().
9 */
10function term_fields_install() {
11  drupal_install_schema('term_fields');
12}
13
14/**
15 * Implements hook_uninstall().
16 */
17function term_fields_uninstall() {
18  drupal_uninstall_schema('term_fields');
19}
20
21/**
22 * Implements hook_schema().
23 */
24function term_fields_schema() {
25  $schema['term_fields'] = array(
26    'fields' => array(
27      'fid' => array(
28        'type' => 'varchar',
29        'length' => 32,
30        'not null' => TRUE,
31        'description' => 'The unique field identifier.',
32      ),
33      'module' => array(
34        'type' => 'varchar',
35        'length' => 127,
36        'not null' => TRUE,
37        'default' => '',
38        'description' => 'The module thats handles this type of fields.',
39      ),
40      'title' => array(
41        'type' => 'varchar',
42        'length' => 255,
43        'not null' => TRUE,
44        'description' => 'The human readable name of this field.',
45      ),
46      'description' => array(
47        'type' => 'text',
48        'size' => 'medium',
49        'description' => 'A description of the field for users.',
50      ),
51      'type' => array(
52        'type' => 'varchar',
53        'length' => 32,
54        'not null' => TRUE,
55        'description' => 'The type of field.',
56      ),
57      'widget' => array(
58        'type' => 'varchar',
59        'length' => 32,
60        'not null' => TRUE,
61        'description' => 'The widget related to the type of the field.',
62      ),
63      'options' => array(
64        'type' => 'text',
65        'size' => 'big',
66        'description' => 'Serialized array of options used for select type fields.',
67        'serialize' => TRUE,
68      ),
69      'instantiated' => array(
70        'type' => 'int',
71        'size' => 'tiny',
72        'not null' => TRUE,
73        'description' => 'This flag specifies if the columns of the {term_fields_term} table have already been created.',
74        'default' => 0,
75      ),
76    ),
77    'primary key' => array('fid'),
78  );
79
80  $schema['term_fields_instance'] = array(
81    'fields' => array(
82      'fid' => array(
83        'type' => 'varchar',
84        'length' => 32,
85        'not null' => TRUE,
86        'description' => 'The {term_fields} primary key.',
87      ),
88      'vid' => array(
89        'type' => 'int',
90        'unsigned' => TRUE,
91        'not null' => TRUE,
92        'description' => 'The vocabulary id of which field values can be set.',
93      ),
94      'required' => array(
95        'type' => 'int',
96        'size' => 'tiny',
97        'not null' => TRUE,
98        'default' => 0,
99      ),
100      'weight' => array(
101        'type' => 'int',
102        'size' => 'tiny',
103        'unsigned' => FALSE,
104        'not null' => TRUE,
105        'description' => 'The weight of the field.',
106      ),
107    ),
108    'primary key' => array('fid', 'vid'),
109  );
110 
111  $schema['term_fields_term'] = array(
112    'fields' => array(
113      'tid' => array(
114        'type' => 'int',
115        'unsigned' => TRUE,
116        'not null' => TRUE,
117      ),
118      'vid' => array(
119        'type' => 'int',
120        'unsigned' => TRUE,
121        'not null' => TRUE,
122      ),
123    ),
124    'primary key' => array('tid'),
125    'indexes' => array(
126      'vid' => array('vid'),
127    ),
128  );
129
130  return $schema;
131}
132
133/**
134 * Implements hook_update_N().
135 *
136 * Removes abandoned entries of deleted taxonomy terms.
137 */
138function term_fields_update_6100() {
139  $ret = array();
140  $ret[] = update_sql("DELETE FROM {term_fields_term} WHERE tid NOT IN (SELECT tid FROM {term_data})");
141  return $ret;
142}
143
144/**
145 * Implements hook_update_N().
146 */
147function term_fields_update_6101() {
148  $ret = array();
149 
150  if (!db_table_exists('term_fields_instance')) {
151    $term_fields_instance_schema = array(
152      'fields' => array(
153        'fid' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'description' => 'The {term_fields} primary key.'),
154        'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The vocabulary id of which field values can be set.'),
155        'required' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0),
156        'weight' => array('type' => 'int', 'size' => 'tiny', 'unsigned' => FALSE, 'not null' => TRUE, 'description' => 'The weight of the field.'),
157      ),
158      'primary key' => array('fid', 'vid'),
159    );
160   
161    // Creates {term_fields_instance} table.
162    db_create_table($ret, 'term_fields_instance', $term_fields_instance_schema);
163   
164    // Moves field instances to the new created table.
165    $result = db_query("SELECT fid, vid, weight FROM {term_fields}");
166    while ($row = db_fetch_object($result)) {
167      db_query("INSERT INTO {term_fields_instance} (`fid`, `vid`, `weight`) VALUES ('%s', %d, %d)", $row->fid, $row->vid, $row->weight);
168    }
169   
170    // Removes deprecated 'vid' and 'weight' columns in {term_fields} table.
171    db_drop_index($ret, 'term_fields', 'vid');
172    db_drop_field($ret, 'term_fields', 'vid');
173    db_drop_field($ret, 'term_fields', 'weight');
174   
175    // Adds 'module', 'widget' and 'instantiated' columns.
176    db_add_field($ret, 'term_fields', 'module', array('type' => 'varchar', 'length' => 127, 'not null' => TRUE, 'default' => '', 'description' => 'The module thats handles this type of fields.'));
177    db_add_field($ret, 'term_fields', 'widget', array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'description' => 'The widget related to the type of the field.'));
178    db_add_field($ret, 'term_fields', 'instantiated', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'description' => 'This flag specifies if the columns of the {term_fields_term} table have already been created.', 'default' => 0));
179   
180    // Update the value of 'module' column.
181    db_query("UPDATE {term_fields} SET module ='%s'", 'term_fields');
182   
183    // Add the 'vid' column to . This allows to easily reset the values
184    // when a field is removed from a specific vocabulary.
185    db_add_field($ret, 'term_fields_term', 'vid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE), array('indexes' => array('vid' => array('vid'))));
186   
187    // Cheat with cache to avoid a partial update of the database that would
188    // causes errors. We are only concerned by the 'serialize' option yet.
189    cache_set('term_fields_storage', array(), 'cache');
190   
191    // In order to have the new created 'serialize' attribute added to the 'options',
192    // column, we need to rebuild the database schema. Otherwise drupal_write_record()
193    // will not serialize the values as expected.
194    drupal_get_schema(NULL, TRUE);
195   
196    // Update term_fields tables (columns and/or values).
197    term_fields_database_update_6101($ret);
198   
199    // Removes deprecated entries in {variables} table.
200    db_query("DELETE FROM {variable} WHERE name LIKE 'term_fields_%'");
201   
202    // Add permission for uploading files to all roles, if there were any
203    // field of type "file" previously.
204    if (db_result(db_query("SELECT COUNT(*) FROM {term_fields} WHERE type = 'file'"))) {
205      $result = db_query('SELECT p.pid, r.rid, p.perm FROM {role} r LEFT JOIN {permission} p ON r.rid = p.rid');
206     
207      while ($perm = db_fetch_object($result)) {
208        if (! empty($perm->perm)) {
209          $perm->perm .= ', upload term files';
210        }
211        else {
212          $perm->perm = 'upload term files';
213        }
214       
215        drupal_write_record('permission', $perm, array('pid'));
216      }
217    }
218  }
219   
220  return $ret;
221}
222
223/**
224 * Helper for updating the databse to 2.x version.
225 */
226function term_fields_database_update_6101(&$ret) {
227  module_load_include('module', 'term_fields');
228  $result = db_query("SELECT * FROM {term_fields}");
229 
230  while ($field = db_fetch_object($result)) {
231    $field->instantiated = 1;
232    $old_options = ! empty($field->options) ? unserialize($field->options) : array();
233    $field->options = array();
234   
235    switch ($field->type) {
236      case 'checkbox':
237        $field->type = 'choice';
238        $field->widget = 'checkbox';
239        term_fields_term_fields_term_update_6101($ret, $field);
240        break;
241       
242      case 'textfield':
243      case 'textarea':
244        $field->widget = $field->type;
245        $field->type = 'text';
246        term_fields_term_fields_term_update_6101($ret, $field);
247        break;
248
249      case 'numeric':
250        $field->type = 'numeric';
251        $field->widget = 'textfield';
252        $field->options['format'] = 'integer';
253        term_fields_term_fields_term_update_6101($ret, $field);
254        break;
255     
256      case 'select':
257        $field->type = 'list';
258        $field->widget = 'select';
259        $field->options['options'] = $old_options;
260        term_fields_term_fields_term_update_6101($ret, $field);
261        break;
262     
263      case 'date':
264        $field->type = 'date';
265        $field->widget = 'datetime';
266        $field->options['todate'] = '';
267        $field->options['tz_handling'] = '';
268        $field->options['granularity'] = array('year', 'month', 'day');
269        term_fields_term_fields_term_update_6101($ret, $field);
270        break;
271
272      case 'file':
273        $field->type = 'file';
274        $field->widget = 'file';
275        $field->options['allowed_extensions'] = 'txt';
276        $field->options['max_size'] = 1;
277       
278        if (! empty($old_options['file_allowed_exts'])) {
279          $exts = array();
280         
281          foreach ($old_options['file_allowed_exts'] as $ext) {
282            $exts[] = trim($ext, ' ,.');
283          }
284         
285          $field->options['allowed_extensions'] = implode('|', $exts);
286        }
287       
288        if (isset($old_options['file_max_size']) && is_numeric($old_options['file_max_size']) && $old_options['file_max_size'] >= 0) {
289          $field->options['max_size'] = (int) $old_options['file_max_size'];
290        }       
291       
292        term_fields_term_fields_term_update_6101($ret, $field, 'fid');
293        break;
294    }
295   
296    drupal_write_record('term_fields', $field, array('fid'));
297  }
298 
299  // Now we can clear all caches and get the fields and schema as expected.
300  term_fields_get_fields(NULL, TRUE);
301  term_fields_get_storage(NULL, TRUE);
302  drupal_get_schema(NULL, TRUE);
303 
304  // Clear Views cache.
305  if (module_exists('views')) {
306    views_invalidate_cache();
307  }
308}
309
310/**
311 * Helper for updating the {term_fields_term} columns.
312 */
313function term_fields_term_fields_term_update_6101(&$ret, &$field, $default_column = 'value') {
314  $t = get_t();
315 
316  if ($new_columns = module_invoke($field->module, 'term_fields_api', 'storage', $field)) {
317    $new_field = $field->fid .'_'. $default_column;
318   
319    if (array_key_exists($new_field, $new_columns)) {
320      db_change_field($ret, 'term_fields_term', $field->fid, $new_field, $new_columns[$new_field]);
321     
322      unset($new_columns[$new_field]);
323     
324      foreach ($new_columns as $field => $spec) {
325        db_add_field($ret, 'term_fields_term', $field, $spec);
326      }
327    }
328    else {
329      $message = 'Term fields: an error occured while changing the column definition for field %fid. Column was not modified and the field was skipped.';
330      $t_args = array('%fid' => $field->fid);
331      drupal_set_message($t($message, $t_args), 'error');
332      watchdog('term_fields', $message, $t_args, WATCHDOG_ALERT);
333      $field-> instantiated = 0;
334      db_query("DELETE FROM {term_fields_instance} WHERE fid = '%s'", $field->fid);
335    }
336  }
337}
338
339/**
340 * Implements hook_update_N().
341 */
342function term_fields_update_6200() {
343  // We now have to set the vid for each row, otherwise they'll be 0
344  // see https://drupal.org/node/1356140
345  $ret = array();
346  $result = db_query("SELECT tf.tid, tf.vid, td.vid AS real_vid FROM {term_fields_term} tf LEFT JOIN {term_data} td ON td.tid = tf.tid");
347 
348  while ($row = db_fetch_object($result)) {
349    // If the vid is 0, it is incorrect and needs to be updated.
350    if (! $row->vid) {
351      $ret[] = update_sql("UPDATE {term_fields_term} SET vid = '". $row->real_vid ."' WHERE tid = '". $row->tid ."'");
352    }
353  }
354 
355  return $ret;
356}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.