[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/station/program/ -> station_program.install (source)

   1  <?php
   2  // $Id: station_program.install,v 1.13 2009/08/29 08:01:42 drewish Exp $
   3  
   4  /**
   5   * Implementation of hook_schema().
   6   */
   7  function station_program_install() {
   8    // To deal with the possibility that we're being installed at the same time
   9    // as CCK and the field modules we depend on, we need to manually enable the
  10    // the modules to ensure they're available before we create our fields.
  11    module_enable(array('content', 'text', 'link'));
  12  
  13    # var_export(content_fields('field_station_program_genre', 'station_program'));
  14    $genre_field = array(
  15      'field_name' => 'field_station_program_genre',
  16      'type_name' => 'station_program',
  17      'display_settings' =>
  18      array(
  19        'label' =>
  20        array(
  21          'format' => 'above',
  22          'exclude' => 0,
  23        ),
  24        'teaser' =>
  25        array(
  26          'format' => 'default',
  27          'exclude' => 0,
  28        ),
  29        'full' =>
  30        array(
  31          'format' => 'default',
  32          'exclude' => 0,
  33        ),
  34        4 =>
  35        array(
  36          'format' => 'default',
  37          'exclude' => 0,
  38        ),
  39        2 =>
  40        array(
  41          'format' => 'default',
  42          'exclude' => 0,
  43        ),
  44        3 =>
  45        array(
  46          'format' => 'default',
  47          'exclude' => 0,
  48        ),
  49        'token' =>
  50        array(
  51          'format' => 'default',
  52          'exclude' => 0,
  53        ),
  54      ),
  55      'widget_active' => '1',
  56      'type' => 'text',
  57      'required' => '1',
  58      'multiple' => '0',
  59      'db_storage' => '1',
  60      'module' => 'text',
  61      'active' => '1',
  62      'locked' => '0',
  63      'columns' =>
  64      array(
  65        'value' =>
  66        array(
  67          'type' => 'text',
  68          'size' => 'big',
  69          'not null' => FALSE,
  70          'sortable' => TRUE,
  71        ),
  72      ),
  73      'text_processing' => '0',
  74      'max_length' => '',
  75      'allowed_values' => '',
  76      'allowed_values_php' => '',
  77      'widget' =>
  78      array(
  79        'rows' => 5,
  80        'size' => '60',
  81        'default_value' =>
  82        array(
  83          0 =>
  84          array(
  85            'value' => '',
  86            '_error_element' => 'default_value_widget][field_station_program_genre][0][value',
  87          ),
  88        ),
  89        'default_value_php' => NULL,
  90        'label' => 'Genre',
  91        'weight' => '-3',
  92        'description' => 'A free-form description of the musical genres played on the program.',
  93        'type' => 'text_textfield',
  94        'module' => 'text',
  95      ),
  96    );
  97  
  98  
  99    # var_export(content_fields('field_station_program_link', 'station_program'));
 100    $link_field = array(
 101      'field_name' => 'field_station_program_link',
 102      'type_name' => 'station_program',
 103      'display_settings' =>
 104      array(
 105        'weight' => '-2',
 106        'parent' => '',
 107        'label' =>
 108        array(
 109          'format' => 'above',
 110        ),
 111        'teaser' =>
 112        array(
 113          'format' => 'hidden',
 114          'exclude' => 0,
 115        ),
 116        'full' =>
 117        array(
 118          'format' => 'default',
 119          'exclude' => 0,
 120        ),
 121        4 =>
 122        array(
 123          'format' => 'default',
 124          'exclude' => 0,
 125        ),
 126        2 =>
 127        array(
 128          'format' => 'default',
 129          'exclude' => 0,
 130        ),
 131        3 =>
 132        array(
 133          'format' => 'default',
 134          'exclude' => 0,
 135        ),
 136        'token' =>
 137        array(
 138          'format' => 'default',
 139          'exclude' => 0,
 140        ),
 141      ),
 142      'widget_active' => '1',
 143      'type' => 'link',
 144      'required' => '0',
 145      'multiple' => '0',
 146      'db_storage' => '1',
 147      'module' => 'link',
 148      'active' => '1',
 149      'locked' => '0',
 150      'columns' =>
 151      array(
 152        'url' =>
 153        array(
 154          'type' => 'varchar',
 155          'length' => 255,
 156          'not null' => FALSE,
 157          'sortable' => TRUE,
 158        ),
 159        'title' =>
 160        array(
 161          'type' => 'varchar',
 162          'length' => 255,
 163          'not null' => FALSE,
 164          'sortable' => TRUE,
 165        ),
 166        'attributes' =>
 167        array(
 168          'type' => 'text',
 169          'size' => 'medium',
 170          'not null' => FALSE,
 171        ),
 172      ),
 173      'attributes' =>
 174      array(
 175        'target' => 'default',
 176        'rel' => '',
 177        'class' => '',
 178      ),
 179      'display' =>
 180      array(
 181        'url_cutoff' => '80',
 182      ),
 183      'url' => 0,
 184      'title' => 'none',
 185      'title_value' => '',
 186      'enable_tokens' => 0,
 187      'widget' =>
 188      array(
 189        'default_value' =>
 190        array(
 191          0 =>
 192          array(
 193            'title' => '',
 194            'url' => '',
 195          ),
 196        ),
 197        'default_value_php' => NULL,
 198        'label' => 'Homepage URL',
 199        'weight' => '-2',
 200        'description' => 'Link to the program\'s website.',
 201        'type' => 'link',
 202        'module' => 'link',
 203      ),
 204    );
 205  
 206  
 207    // Create the fields.
 208    module_load_include('inc', 'content', 'includes/content.crud');
 209    content_field_instance_create($link_field);
 210    content_field_instance_create($genre_field);
 211  }
 212  
 213  /**
 214   * Implementation of hook_uninstall().
 215   */
 216  function station_program_uninstall() {
 217    if (module_exists('content')) {
 218      module_load_include('inc', 'content', 'includes/content.crud');
 219      content_field_instance_delete('field_station_program_genre', 'station_program');
 220      content_field_instance_delete('field_station_program_link', 'station_program');
 221    }
 222  }
 223  
 224  /**
 225   * Implementation of hook_update_last_removed().
 226   */
 227  function station_program_update_last_removed() {
 228    // We've removed the 5.x-1.x version of mymodule, including database updates.
 229    // The next update function is mymodule_update_5200().
 230    return 5100;
 231  }
 232  
 233  /**
 234   * Change the program node name to station_program.
 235   */
 236  function station_program_update_5200() {
 237    $ret = array();
 238    switch ($GLOBALS['db_type']) {
 239      case 'mysql':
 240      case 'mysqli':
 241        $ret[] = update_sql("UPDATE {node} SET type = 'station_program' WHERE type='program';");
 242        $ret[] = update_sql("UPDATE {node_type} SET type = 'station_program' WHERE type='program';");
 243      break;
 244    }
 245    return $ret;
 246  }
 247  
 248  /**
 249   * Create CCK fields and migrate the data to them.
 250   *
 251   * @return unknown
 252   */
 253  function station_program_update_6000() {
 254    $ret = array();
 255  
 256    // Make sure they've enabled CCK and link modules
 257    if (!module_exists('content') || !module_exists('text') || !module_exists('link')) {
 258      $ret['#abort'] = array('success' => FALSE, 'query' => t('Station Program now requires CCK, Text and Link modules. Some updates are still pending.<br/>Please re-run the update script.'));
 259      return $ret;
 260    }
 261    // Make sure there's no pending CCK updates
 262    drupal_load('module', 'content');
 263    if ($abort = content_check_update('text')) {
 264      return $abort;
 265    }
 266    if ($abort = content_check_update('link')) {
 267      return $abort;
 268    }
 269  
 270    # var_export(content_fields('field_station_program_genre', 'station_program'));
 271    $genre_field = array(
 272      'field_name' => 'field_station_program_genre',
 273      'type_name' => 'station_program',
 274      'display_settings' =>
 275      array(
 276        'label' =>
 277        array(
 278          'format' => 'above',
 279          'exclude' => 0,
 280        ),
 281        'teaser' =>
 282        array(
 283          'format' => 'default',
 284          'exclude' => 0,
 285        ),
 286        'full' =>
 287        array(
 288          'format' => 'default',
 289          'exclude' => 0,
 290        ),
 291        4 =>
 292        array(
 293          'format' => 'default',
 294          'exclude' => 0,
 295        ),
 296        2 =>
 297        array(
 298          'format' => 'default',
 299          'exclude' => 0,
 300        ),
 301        3 =>
 302        array(
 303          'format' => 'default',
 304          'exclude' => 0,
 305        ),
 306        'token' =>
 307        array(
 308          'format' => 'default',
 309          'exclude' => 0,
 310        ),
 311      ),
 312      'widget_active' => '1',
 313      'type' => 'text',
 314      'required' => '1',
 315      'multiple' => '0',
 316      'db_storage' => '1',
 317      'module' => 'text',
 318      'active' => '1',
 319      'locked' => '0',
 320      'columns' =>
 321      array(
 322        'value' =>
 323        array(
 324          'type' => 'text',
 325          'size' => 'big',
 326          'not null' => FALSE,
 327          'sortable' => TRUE,
 328        ),
 329      ),
 330      'text_processing' => '0',
 331      'max_length' => '',
 332      'allowed_values' => '',
 333      'allowed_values_php' => '',
 334      'widget' =>
 335      array(
 336        'rows' => 5,
 337        'size' => '60',
 338        'default_value' =>
 339        array(
 340          0 =>
 341          array(
 342            'value' => '',
 343            '_error_element' => 'default_value_widget][field_station_program_genre][0][value',
 344          ),
 345        ),
 346        'default_value_php' => NULL,
 347        'label' => 'Genre',
 348        'weight' => '-3',
 349        'description' => 'A free-form description of the musical genres played on the program.',
 350        'type' => 'text_textfield',
 351        'module' => 'text',
 352      ),
 353    );
 354  
 355  
 356    # var_export(content_fields('field_station_program_link', 'station_program'));
 357    $link_field = array(
 358      'field_name' => 'field_station_program_link',
 359      'type_name' => 'station_program',
 360      'display_settings' =>
 361      array(
 362        'weight' => '-2',
 363        'parent' => '',
 364        'label' =>
 365        array(
 366          'format' => 'above',
 367        ),
 368        'teaser' =>
 369        array(
 370          'format' => 'hidden',
 371          'exclude' => 0,
 372        ),
 373        'full' =>
 374        array(
 375          'format' => 'default',
 376          'exclude' => 0,
 377        ),
 378        4 =>
 379        array(
 380          'format' => 'default',
 381          'exclude' => 0,
 382        ),
 383        2 =>
 384        array(
 385          'format' => 'default',
 386          'exclude' => 0,
 387        ),
 388        3 =>
 389        array(
 390          'format' => 'default',
 391          'exclude' => 0,
 392        ),
 393        'token' =>
 394        array(
 395          'format' => 'default',
 396          'exclude' => 0,
 397        ),
 398      ),
 399      'widget_active' => '1',
 400      'type' => 'link',
 401      'required' => '0',
 402      'multiple' => '0',
 403      'db_storage' => '1',
 404      'module' => 'link',
 405      'active' => '1',
 406      'locked' => '0',
 407      'columns' =>
 408      array(
 409        'url' =>
 410        array(
 411          'type' => 'varchar',
 412          'length' => 255,
 413          'not null' => FALSE,
 414          'sortable' => TRUE,
 415        ),
 416        'title' =>
 417        array(
 418          'type' => 'varchar',
 419          'length' => 255,
 420          'not null' => FALSE,
 421          'sortable' => TRUE,
 422        ),
 423        'attributes' =>
 424        array(
 425          'type' => 'text',
 426          'size' => 'medium',
 427          'not null' => FALSE,
 428        ),
 429      ),
 430      'attributes' =>
 431      array(
 432        'target' => 'default',
 433        'rel' => '',
 434        'class' => '',
 435      ),
 436      'display' =>
 437      array(
 438        'url_cutoff' => '80',
 439      ),
 440      'url' => 0,
 441      'title' => 'none',
 442      'title_value' => '',
 443      'enable_tokens' => 0,
 444      'widget' =>
 445      array(
 446        'default_value' =>
 447        array(
 448          0 =>
 449          array(
 450            'title' => '',
 451            'url' => '',
 452          ),
 453        ),
 454        'default_value_php' => NULL,
 455        'label' => 'Homepage URL',
 456        'weight' => '-2',
 457        'description' => 'Link to the program\'s website.',
 458        'type' => 'link',
 459        'module' => 'link',
 460      ),
 461    );
 462  
 463  
 464    // Create the fields.
 465    module_load_include('inc', 'content', 'includes/content.crud');
 466    content_field_instance_create($link_field);
 467    content_field_instance_create($genre_field);
 468  
 469  
 470    // Now the tricky part is to migrate the data.
 471    // The procedure I'm using here is a bit overkill for migrating two fields
 472    // but I'm planning on re-using it so it makes sense to go ahead and just be
 473    // robust.
 474    // We've got to assume that CCK's table might only have records for half of
 475    // the nodes we're migrating so the strategy is to INSERT then UPDATE.
 476    $migrations = array(
 477      array(
 478        'cck_field' => $genre_field,
 479        'from_table' => 'station_program',
 480        'from_col' => 'genre',
 481        'from_join' => 'vid',
 482        'to_col' => 'value',
 483        'to_join' => 'vid',
 484      ),
 485      array(
 486        'cck_field' => $link_field,
 487        'from_table' => 'station_program',
 488        'from_col' => 'url',
 489        'from_join' => 'vid',
 490        'to_col' => 'url',
 491        'to_join' => 'vid',
 492      ),
 493    );
 494    foreach ($migrations as $migration) {
 495      // Might be needlessly escaping the names here but it shouldn't hurt
 496      // anything.
 497      $from_table = db_escape_table($migration['from_table']);
 498      $from_col   = db_escape_table($migration['from_col']);
 499      $from_join  = db_escape_table($migration['from_join']);
 500  
 501      $db_info    = content_database_info($migration['cck_field']);
 502      $to_table   = db_escape_table($db_info['table']);
 503      $to_col     = db_escape_table($db_info['columns'][$migration['to_col']]['column']);
 504      $to_join    = db_escape_table($migration['to_join']);
 505  
 506      // Make sure there's a record in CCK's table for every one in the source
 507      // table, then do an update of all the records.
 508      $ret[] = update_sql("INSERT INTO {{$to_table}} (nid, vid) SELECT n.nid, n.vid FROM {{$from_table}} f INNER JOIN {node} n ON f.$from_join = n.nid WHERE NOT EXISTS ( SELECT * FROM {{$to_table}} t WHERE f.$from_join = t.$to_join)");
 509      $ret[] = update_sql("UPDATE {{$to_table}} t INNER JOIN {{$from_table}} f ON t.$to_join = f.$from_join SET t.$to_col = f.$from_col");
 510    }
 511  
 512  #  db_drop_table($ret, 'station_program');
 513  
 514    return $ret;
 515  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7