[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/station/schedule/ -> station_schedule.install (source)

   1  <?php
   2  
   3  // $Id: station_schedule.install,v 1.30 2010/07/09 21:04:36 timplunkett Exp $
   4  
   5  /**
   6   * Implementation of hook_schema().
   7   */
   8  function station_schedule_install() {
   9    drupal_install_schema('station_schedule');
  10  
  11    // To deal with the possibility that we're being installed at the same time
  12    // as CCK and the field modules we depend on, we need to manually enable the
  13    // the modules to ensure they're available before we create our fields.
  14    module_enable(array('content', 'userreference'));
  15  
  16    $roles = user_roles(TRUE);
  17    $dj_role = variable_get('station_schedule_dj_role', DRUPAL_AUTHENTICATED_RID);
  18    $dj_title = variable_get('station_schedule_dj_title', 'DJs');
  19  
  20    # var_export(content_fields('field_station_program_dj', 'station_program'));
  21    $dj_field = array(
  22      'field_name' => 'field_station_program_dj',
  23      'type_name' => 'station_program',
  24      'display_settings' =>
  25      array(
  26        'label' =>
  27        array(
  28          'format' => 'above',
  29        ),
  30        'teaser' =>
  31        array(
  32          'format' => 'default',
  33        ),
  34        'full' =>
  35        array(
  36          'format' => 'default',
  37        ),
  38        0 =>
  39        array(
  40          'format' => 'above',
  41        ),
  42        'token' =>
  43        array(
  44          'format' => 'default',
  45        ),
  46      ),
  47      'widget_active' => '1',
  48      'type' => 'userreference',
  49      'required' => '0',
  50      'multiple' => '1',
  51      'db_storage' => '0',
  52      'module' => 'userreference',
  53      'active' => '1',
  54      'locked' => '1',
  55      'columns' =>
  56      array(
  57        'uid' =>
  58        array(
  59          'type' => 'int',
  60          'unsigned' => TRUE,
  61          'not null' => FALSE,
  62        ),
  63      ),
  64      'referenceable_roles' =>
  65      array(
  66        $dj_role => $dj_role,
  67      ),
  68      'referenceable_status' =>
  69      array(
  70        1 => 1,
  71        0 => 0,
  72      ),
  73      'advanced_view' => '',
  74      'advanced_view_args' => '',
  75      'widget' =>
  76      array(
  77        'reverse_link' => 0,
  78        'default_value' =>
  79        array(
  80          0 =>
  81          array(
  82            'uid' => NULL,
  83            '_error_element' => 'default_value_widget][field_station_program_dj][0][uid][uid',
  84          ),
  85        ),
  86        'default_value_php' => NULL,
  87        'label' => $dj_title,
  88        'weight' => '1',
  89        'description' => st('Enter the names of users you\'d like to add as @dj-title. The users must be members of the %role-name role.', array('@dj-title' => $dj_title, '%role-name' => $roles[$dj_role])),
  90        'type' => 'userreference_autocomplete',
  91        'module' => 'userreference',
  92      ),
  93    );
  94  
  95    // Create the fields.
  96    module_load_include('inc', 'content', 'includes/content.crud');
  97    content_field_instance_create($dj_field);
  98  }
  99  
 100  /**
 101   * Implementation of hook_uninstall().
 102   */
 103  function station_schedule_uninstall() {
 104    if (module_exists('content')) {
 105      module_load_include('inc', 'content', 'includes/content.crud');
 106      content_field_instance_delete('field_station_program_genre', 'station_program');
 107      content_field_instance_delete('field_station_program_link', 'station_program');
 108    }
 109  
 110    drupal_uninstall_schema('station_schedule');
 111  
 112    variable_del('station_schedule_redirect_old_urls');
 113    variable_del('station_schedule_default');
 114  
 115    // Remove any stream files.
 116    file_scan_directory(file_create_path('station'), '.*\.m3u', array('.', '..', 'CVS'), 'file_delete', FALSE);
 117  }
 118  
 119  /**
 120   * Implementation of hook_schema().
 121   */
 122  function station_schedule_schema() {
 123    $schema['station_schedule'] = array(
 124      'description' => t('Information about station schedules.'),
 125      'fields' => array(
 126        'nid' => array(
 127          'description' => t("The schedule's {node}.nid."),
 128          'type' => 'int',
 129          'unsigned' => TRUE,
 130          'not null' => TRUE,
 131          'default' => 0,
 132        ),
 133        'increment' => array(
 134          'description' => t('Increment of the schedule block size in minutes.'),
 135          'type' => 'int',
 136          'unsigned' => TRUE,
 137          'not null' => TRUE,
 138          'default' => 0,
 139        ),
 140        'start_hour' => array(
 141          'description' => t('Programming start time in hours.'),
 142          'type' => 'int',
 143          'unsigned' => TRUE,
 144          'not null' => TRUE,
 145          'default' => 0,
 146        ),
 147        'end_hour' => array(
 148          'description' => t('Programming end time in hours.'),
 149          'type' => 'int',
 150          'unsigned' => TRUE,
 151          'not null' => TRUE,
 152          'default' => 24,
 153        ),
 154        'streams' => array(
 155          'description' => t('Web stream URLs.'),
 156          'type' => 'text',
 157          'not null' => TRUE,
 158          'size' => 'big',
 159          'serialize' => TRUE,
 160        ),
 161        'unscheduled_message' => array(
 162          'description' => t('Message to display during unscheduled periods.'),
 163          'type' => 'varchar',
 164          'length' => 255,
 165          'not null' => TRUE,
 166          'default' => '',
 167        ),
 168      ),
 169      'primary key' => array('nid'),
 170    );
 171  
 172    $schema['station_schedule_item'] = array(
 173      'description' => t('Information about station schedules.'),
 174      'fields' => array(
 175        'iid' => array(
 176          'description' => t('The primary identifier a station schedule item.'),
 177          'type' => 'serial',
 178          'unsigned' => TRUE,
 179          'not null' => TRUE,
 180        ),
 181        'schedule_nid' => array(
 182          'description' => t("The schedule's {node}.nid."),
 183          'type' => 'int',
 184          'unsigned' => TRUE,
 185          'not null' => TRUE,
 186          'default' => 0,
 187        ),
 188        'program_nid' => array(
 189          'description' => t("The program's {node}.nid."),
 190          'type' => 'int',
 191          'unsigned' => TRUE,
 192          'not null' => TRUE,
 193          'default' => 0,
 194        ),
 195        'start' => array(
 196          'description' => t("Starting minute from Sunday midnight."),
 197          'type' => 'int',
 198          'unsigned' => TRUE,
 199          'not null' => TRUE,
 200          'default' => 0,
 201        ),
 202        'finish' => array(
 203          'description' => t("Finishing minute from Sunday midnight."),
 204          'type' => 'int',
 205          'unsigned' => TRUE,
 206          'not null' => TRUE,
 207          'default' => 0,
 208        ),
 209        'may_archive' => array(
 210          'description' => t("Can this item be scaved in the archive?"),
 211          'type' => 'int',
 212          'unsigned' => TRUE,
 213          'not null' => TRUE,
 214          'size' => 'tiny',
 215          'default' => 0,
 216        ),
 217      ),
 218      'indexes' => array(
 219        'station_schedule_start'   => array('schedule_nid', 'start'),
 220        'station_schedule_program' => array('schedule_nid', 'program_nid'),
 221      ),
 222      'primary key' => array('iid'),
 223    );
 224  
 225    return $schema;
 226  }
 227  
 228  /**
 229   * Implementation of hook_update_last_removed().
 230   */
 231  function station_schedule_update_last_removed() {
 232    // We've removed the 5.x-1.x version of mymodule, including database updates.
 233    // The next update function is mymodule_update_5200().
 234    return 101;
 235  }
 236  
 237  /**
 238   * Rename station_schedule to station_schedule_item and rename sid to iid. Add
 239   * a new schedule_nid field that refers to the schedule. Then create a
 240   * station_schedule table and update all schedule items to be a member of it.
 241   */
 242  function station_schedule_update_5200() {
 243    $ret = array();
 244    switch ($GLOBALS['db_type']) {
 245      case 'mysql':
 246      case 'mysqli':
 247        // Rename {station_schedule} to {station_schedule_item}, sid to iid and
 248        // add schedule_nid so we can join to the new {station_schedule}.
 249        $ret[] = update_sql('ALTER TABLE {station_schedule} RENAME TO {station_schedule_item}');
 250        $ret[] = update_sql('ALTER TABLE {station_schedule_item} CHANGE COLUMN `sid` `iid` INTEGER UNSIGNED NOT NULL DEFAULT 0');
 251        $ret[] = update_sql('ALTER TABLE {station_schedule_item} ADD COLUMN `schedule_nid` INTEGER UNSIGNED NOT NULL DEFAULT 0 FIRST');
 252  
 253        // Move the sequence.
 254        db_query('LOCK TABLES {sequences} WRITE');
 255        $ret[] = update_sql("DELETE FROM {sequences} WHERE name = '{station_schedule_item}_iid'");
 256        $ret[] = update_sql("UPDATE {sequences} SET name = '{station_schedule_item}_iid' WHERE name = '{station_schedule}_sid'");
 257        db_query('UNLOCK TABLES');
 258  
 259        // Create the table for schedule nodes.
 260        $ret[] = update_sql("
 261          CREATE TABLE {station_schedule} (
 262            `nid` int unsigned NOT NULL default '0',
 263            `increment` int unsigned NOT NULL default '0',
 264            `streams` longtext,
 265            PRIMARY KEY(`nid`)
 266          ) /*!40100 DEFAULT CHARACTER SET utf8 */;
 267        ");
 268  
 269        // Create a new node for the existing schedule. We can't do it the easy
 270        // with node_save() because it'll call station_schedule_insert() which
 271        // references fields added in later updates.
 272        $node = new stdClass();
 273        $node->nid = db_next_id('{node}_nid');
 274        $node->vid = db_next_id('{node_revisions}_vid');
 275        $node->created = time();
 276        $node->changed = time();
 277        $node->uid = 1;
 278        $node->title = t('Default');
 279        db_query("INSERT INTO {node} (nid, vid, title, type, uid, status, created, changed, comment, promote, sticky)
 280          VALUES (%d, %d, '%s', 'station_schedule', 1, %d, %d, %d, 0, 0, 0)",
 281          $node->nid, $node->vid, $node->title, $node->uid, $node->created, $node->changed
 282        );
 283        db_query("INSERT INTO {node_revisions} (nid, vid, title, body, teaser, timestamp, uid, format, log)
 284          VALUES (%d, %d, '%s', '', '', %d, %d, 0, '')",
 285          $node->nid, $node->vid, $node->title, $node->created, $node->uid
 286        );
 287        db_query("INSERT INTO {station_schedule} (nid, increment) VALUES (%d, %d)", $node->nid, variable_get('station_schedule_increment', 60));
 288  
 289        // Don't need this anymore.
 290        variable_del('station_schedule_increment');
 291  
 292        // Make the new schedule the default.
 293        variable_set('station_schedule_default', $node->nid);
 294        $ret[] = update_sql("UPDATE {station_schedule_item} SET schedule_nid = ". (int) $node->nid);
 295  
 296        // Fix the keys.
 297        $ret[] = update_sql('ALTER TABLE {station_schedule_item} DROP INDEX `station_schedule_start`');
 298        $ret[] = update_sql('ALTER TABLE {station_schedule_item} DROP INDEX `station_schedule_program`');
 299        $ret[] = update_sql('ALTER TABLE {station_schedule_item} ADD INDEX `station_schedule_start` (`schedule_nid`, `start`)');
 300        $ret[] = update_sql('ALTER TABLE {station_schedule_item} ADD INDEX `station_schedule_program` (`schedule_nid`, `program_nid`)');
 301      break;
 302    }
 303    return $ret;
 304  }
 305  
 306  /**
 307   * Existing sites will want to preserve the old URLs by default.
 308   */
 309  function station_schedule_update_5201() {
 310    variable_set('station_schedule_redirect_old_urls', 1);
 311    return array();
 312  }
 313  
 314  /**
 315   * Move the unscheduled message from variable into the schedule table.
 316   */
 317  function station_schedule_update_5202() {
 318    $ret = array();
 319    switch ($GLOBALS['db_type']) {
 320      case 'mysql':
 321      case 'mysqli':
 322        $ret[] = update_sql("ALTER TABLE {station_schedule} ADD COLUMN `unscheduled_message` VARCHAR(255) NOT NULL default '' AFTER `increment`");
 323  
 324        $unscheduled_message = variable_get('station_block_unschedule',  t("We're on autopilot."));
 325        db_query("UPDATE {station_schedule} SET unscheduled_message = '%s'", $unscheduled_message);
 326        variable_del('station_block_unschedule');
 327  
 328        break;
 329    }
 330    return $ret;
 331  }
 332  
 333  /**
 334   * Move the streams from variables into the schedule table.
 335   */
 336  function station_schedule_update_5203() {
 337    $ret = array();
 338    switch ($GLOBALS['db_type']) {
 339      case 'mysql':
 340      case 'mysqli':
 341        $ret[] = update_sql("ALTER TABLE {station_schedule} ADD COLUMN `streams` longtext AFTER `increment`");
 342  
 343        $streams = array();
 344        if ($high = variable_get('station_stream_high_url', '')) {
 345          $streams['high'] = array(
 346             'name' => t('High'),
 347             'description' => t('High bandwidth stream'),
 348             'urls' => $high,
 349          );
 350        }
 351        if ($low = variable_get('station_stream_low_url', '')) {
 352          $streams['low'] = array(
 353            'name' => t('Low'),
 354            'description' => t('Low bandwidth stream'),
 355            'urls' => $low,
 356          );
 357        }
 358        if (count($streams)) {
 359          db_query("UPDATE {station_schedule} SET streams = '%s'", serialize($streams));
 360        }
 361        variable_del('station_stream_high_url');
 362        variable_del('station_stream_low_url');
 363  
 364        break;
 365    }
 366    return $ret;
 367  }
 368  
 369  function station_schedule_update_6000() {
 370    $ret = array();
 371  
 372    // Change the iid field to a serial type
 373    // FROM `iid` int unsigned NOT NULL default '0' to
 374    $iid_field = array(
 375      'description' => t('The primary identifier a station schedule item.'),
 376      'type' => 'serial',
 377      'unsigned' => TRUE,
 378      'not null' => TRUE,
 379    );
 380    db_change_field($ret, 'station_schedule_item', 'iid', 'iid', $iid_field);
 381  
 382    return $ret;
 383  }
 384  
 385  /**
 386   * Move the program's DJ list into a node reference field.
 387   */
 388  function station_schedule_update_6001() {
 389    $ret = array();
 390  
 391    // Make sure they've enabled CCK and link modules
 392    if (!module_exists('content') || !module_exists('userreference')) {
 393      $ret['#abort'] = array('success' => FALSE, 'query' => t('Station Playlist now requires CCK and User Reference modules. Some updates are still pending.<br/>Please re-run the update script.'));
 394      return $ret;
 395    }
 396    // Make sure there's no pending CCK updates
 397    drupal_load('module', 'content');
 398    if ($abort = content_check_update('userreference')) {
 399      return $abort;
 400    }
 401  
 402    $roles = user_roles(TRUE);
 403    $dj_role = variable_get('station_schedule_dj_role', DRUPAL_AUTHENTICATED_RID);
 404    $dj_title = variable_get('station_schedule_dj_title', 'DJs');
 405  
 406    # var_export(content_fields('field_station_program_dj', 'station_program'));
 407    $dj_field = array(
 408      'field_name' => 'field_station_program_dj',
 409      'type_name' => 'station_program',
 410      'display_settings' =>
 411      array(
 412        'label' =>
 413        array(
 414          'format' => 'above',
 415        ),
 416        'teaser' =>
 417        array(
 418          'format' => 'default',
 419        ),
 420        'full' =>
 421        array(
 422          'format' => 'default',
 423        ),
 424        0 =>
 425        array(
 426          'format' => 'above',
 427        ),
 428        'token' =>
 429        array(
 430          'format' => 'default',
 431        ),
 432      ),
 433      'widget_active' => '1',
 434      'type' => 'userreference',
 435      'required' => '0',
 436      'multiple' => '1',
 437      'db_storage' => '0',
 438      'module' => 'userreference',
 439      'active' => '1',
 440      'locked' => '1',
 441      'columns' =>
 442      array(
 443        'uid' =>
 444        array(
 445          'type' => 'int',
 446          'unsigned' => TRUE,
 447          'not null' => FALSE,
 448        ),
 449      ),
 450      'referenceable_roles' =>
 451      array(
 452        $dj_role => $dj_role,
 453      ),
 454      'referenceable_status' =>
 455      array(
 456        1 => 1,
 457        0 => 0,
 458      ),
 459      'advanced_view' => '',
 460      'advanced_view_args' => '',
 461      'widget' =>
 462      array(
 463        'reverse_link' => 0,
 464        'default_value' =>
 465        array(
 466          0 =>
 467          array(
 468            'uid' => NULL,
 469            '_error_element' => 'default_value_widget][field_station_program_dj][0][uid][uid',
 470          ),
 471        ),
 472        'default_value_php' => NULL,
 473        'label' => $dj_title,
 474        'weight' => '1',
 475        'description' => t('Enter the names of users you\'d like to add as @dj-title. The users must be members of the %role-name role.', array('@dj-title' => $dj_title, '%role-name' => $roles[$dj_role])),
 476        'type' => 'userreference_autocomplete',
 477        'module' => 'userreference',
 478      ),
 479    );
 480  
 481    // Create the fields.
 482    module_load_include('inc', 'content', 'includes/content.crud');
 483    content_field_instance_create($dj_field);
 484  
 485  
 486    // Now the tricky part is to migrate the data.
 487  
 488    // NOTE: This differes from the copy/pasted version of this update because
 489    // we need to ensure the delta is unique
 490  
 491    // Might be needlessly escaping the names here but it shouldn't hurt
 492    // anything.
 493    $db_info    = content_database_info($dj_field);
 494    $to_table   = db_escape_table($db_info['table']);
 495    $to_col     = db_escape_table($db_info['columns']['uid']['column']);
 496  
 497    // We've got to assume that CCK's table might only have records for half of
 498    // the nodes we're migrating so the strategy is to INSERT then UPDATE.
 499    $ret[] = update_sql("INSERT INTO {{$to_table}} (nid, vid, delta)
 500      SELECT n.nid, n.vid, f.uid FROM {station_dj} f INNER JOIN {node} n ON f.program_nid = n.nid
 501      WHERE NOT EXISTS ( SELECT * FROM {{$to_table}} t WHERE t.nid = f.program_nid AND t.delta = f.uid)");
 502    $ret[] = update_sql("UPDATE {{$to_table}} t INNER JOIN {station_dj} f ON t.nid = f.program_nid AND t.delta = f.uid
 503      SET t.$to_col = f.uid");
 504  
 505    // Since these are now on the field we don't really need them any more..
 506    variable_del('station_schedule_dj_role');
 507    variable_del('station_schedule_dj_title');
 508  
 509  #  db_drop_table($ret, 'station_dj');
 510  
 511    return $ret;
 512  }
 513  
 514  /**
 515   * Resave the schedules so that the webstream links are saved as files.
 516   */
 517  function station_schedule_update_6002() {
 518    $result = db_query("SELECT n.nid FROM {node} n INNER JOIN {station_schedule} ss ON n.nid = ss.nid WHERE n.type = 'station_schedule'");
 519    while ($node = db_fetch_array($result)) {
 520      $node = node_load($node);
 521      if ($node->nid) {
 522        $node = node_validate($node);
 523        $node = node_submit($node);
 524        node_save($node);
 525      }
 526    }
 527  
 528    return array();
 529  }
 530  
 531  /**
 532   * Adds programming start times and end times to station schedules.
 533   */
 534  function station_schedule_update_6003() {
 535    $ret = array();
 536    db_add_field($ret, 'station_schedule', 'start_hour', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
 537    db_add_field($ret, 'station_schedule', 'end_hour', array('type' => 'int', 'not null' => TRUE, 'default' => 24));
 538    return $ret;
 539  }


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