[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/menu_per_role/ -> menu_per_role.install (source)

   1  <?php
   2  // $Id: menu_per_role.install,v 1.3.2.7 2010/10/19 08:53:14 alexiswilke Exp $
   3  
   4  /*
   5   * Implementation of hook_install();
   6   */
   7  function menu_per_role_install() {
   8    drupal_install_schema('menu_per_role');
   9  }
  10  
  11  /*
  12   * Implementation of hook_uninstall();
  13   */
  14  function menu_per_role_uninstall() {
  15    drupal_uninstall_schema('menu_per_role');
  16  }
  17  
  18  /*
  19   * Implementation of hook_schema()
  20   */
  21  function menu_per_role_schema() {
  22    $schema['menu_per_role'] = array(
  23      'fields' => array(
  24        'mlid' => array(
  25          'description' => t('The menu identifier.'),
  26          'type' => 'int',
  27          'unsigned' => TRUE,
  28          'not null' => TRUE,
  29        ),
  30        'rids' => array(
  31          'description' => t('The role identifiers separated by commas. Show to those roles.'),
  32          'type' => 'text',
  33          'not null' => TRUE,
  34        ),
  35        'hrids' => array(
  36          'description' => t('The role identifiers separated by commas. Hide from those roles.'),
  37          'type' => 'text',
  38          'not null' => TRUE,
  39        ),
  40      ),
  41      'primary key' => array('mlid'),
  42    );
  43    return $schema;
  44  }
  45  
  46  /**
  47   * hook_update()
  48   */
  49  function menu_per_role_update_6000() {
  50    $ret = array();
  51  
  52    // already using new schema?
  53    if (db_column_exists('menu_per_role', 'mlid')) {
  54      return $ret;
  55    }
  56  
  57    db_query('BEGIN');
  58  
  59    // read the old data in memory (assuming it is not extra large, we should be fine)
  60    $result = db_query('SELECT mid, rid FROM {menu_per_role} WHERE mid IS NOT NULL');
  61    while($row = db_fetch_array($result)) {
  62      $mid[$row['mid']][] = $row['rid'];
  63    }
  64  
  65    db_query('DELETE FROM {menu_per_role}');
  66  
  67    // create new fields right there
  68    db_add_field($ret, 'menu_per_role', 'mlid', array(
  69      'description' => t('The menu identifier.'),
  70      'type' => 'int',
  71      'unsigned' => TRUE,
  72      'not null' => TRUE,
  73      'default' => 0,
  74    ));
  75    db_add_field($ret, 'menu_per_role', 'rids', array(
  76      'description' => t('The role identifiers separated by commas.'),
  77      'type' => 'text',
  78      'not null' => TRUE,
  79    ));
  80  
  81    // delete old schema fields and set new primary key
  82    db_drop_field($ret, 'menu_per_role', 'mid');
  83    db_drop_field($ret, 'menu_per_role', 'rid');
  84    db_add_primary_key($ret, 'menu_per_role', array('mlid'));
  85  
  86    // save the old data in the new table
  87    foreach ($mid as $key => $value) {
  88      if (count($value) > 0) {
  89        $list = implode(',', $value);
  90        $ret[] = update_sql("INSERT INTO {menu_per_role} (mlid, rids) VALUES (" . $key . ", '" . $list . "')");
  91      }
  92    }
  93  
  94    // we're done
  95    db_query('COMMIT');
  96  
  97    return $ret;
  98  }
  99  
 100  /**
 101   * hook_update()
 102   */
 103  function menu_per_role_update_6001() {
 104    $ret = array();
 105  
 106    // already using new schema?
 107    if (db_column_exists('menu_per_role', 'hrids')) {
 108      return $ret;
 109    }
 110  
 111    // create new field
 112    db_add_field($ret, 'menu_per_role', 'hrids', array(
 113      'description' => t('The role identifiers separated by commas. Hide from those roles.'),
 114      'type' => 'text',
 115      'not null' => TRUE,
 116      'default' => '',
 117    ));
 118  
 119    return $ret;
 120  }
 121  
 122  /**
 123   * hook_update_N();
 124   */
 125  function menu_per_role_update_6002() {
 126    $ret = array();
 127  
 128    // This default creates problems with MySQL and really it should not be there!
 129    db_field_set_no_default($ret, 'menu_per_role', 'mlid');
 130  
 131    return $ret;
 132  }
 133  
 134  /**
 135   * hook_update_N();
 136   */
 137  function menu_per_role_update_6003() {
 138    $ret = array();
 139  
 140    // We do not need to have empty entries, this will accelerate total
 141    // processing time on every page load
 142    db_query("DELETE FROM {menu_per_role} WHERE rids = '' AND hrids = ''");
 143  
 144    return $ret;
 145  }
 146  
 147  // vim: ts=2 sw=2 et syntax=php


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