You are here

support_pm.install in Support Ticketing System 7

Same filename and directory in other branches
  1. 6 support_pm/support_pm.install

File

support_pm/support_pm.install
View source
<?php

/**
 * Helpdesk / support ticket project management database schema.
 * Copyright (c) 2010-11 Tag1 Consulting, Inc <jeremy@tag1consulting.com>.
 */

/**
 * Support project management database schema definition.
 */
function support_pm_schema() {
  $schema['support_plan'] = array(
    'description' => 'Allows planning of hours required.',
    'fields' => array(
      'clid' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
      ),
      'day' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'hours' => array(
        'type' => 'float',
        'not null' => FALSE,
        'unsigned' => TRUE,
        'default' => 0.0,
      ),
      'comment' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'uid',
      'clid',
      'day',
    ),
  );
  $schema['support_project'] = array(
    'description' => 'Assign projects to support clients.',
    'fields' => array(
      'projid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'project' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'path' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'disabled' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => FALSE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'projid',
    ),
    'keys' => array(
      'project' => array(
        'project',
        'disabled',
        'weight',
      ),
    ),
    'unique keys' => array(
      'path' => array(
        'path',
      ),
    ),
  );
  $schema['support_project_client'] = array(
    'fields' => array(
      'projid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
      ),
      'clid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'projid',
      'clid',
    ),
  );
  $schema['support_project_ticket'] = array(
    'description' => 'Stores which projects are assigned to tickets.',
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
      ),
      'projid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'nid',
      'projid',
    ),
  );
  return $schema;
}

/**
 * Add project tables.
 */
function support_pm_update_6001() {
  $schema['support_project'] = array(
    'description' => 'Defines projects that can be assigned to client tickets.',
    'fields' => array(
      'projid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'project' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'disabled' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => FALSE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'projid',
    ),
    'keys' => array(
      'project' => array(
        'project',
        'disabled',
        'weight',
      ),
    ),
  );
  $schema['support_project_client'] = array(
    'description' => 'Stores which projects can be assigned to which clients.',
    'fields' => array(
      'projid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
      ),
      'clid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'projid',
      'clid',
    ),
  );
  $schema['support_project_ticket'] = array(
    'description' => 'Stores which projects are assigned to tickets.',
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
      ),
      'projid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'nid',
      'projid',
    ),
  );
  db_create_table('support_project', $schema['support_project']);
  db_create_table('support_project_client', $schema['support_project_client']);
  db_create_table('support_project_ticket', $schema['support_project_ticket']);

  // We've added new menus, flush caches and rebuild menus.
  drupal_flush_all_caches();
  menu_rebuild();
}

/**
 * Add path to {support_project}.
 */
function support_pm_update_6002() {
  db_add_field('support_project', 'path', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
    'description' => 'Project path.',
  ));
  $result = db_query('SELECT projid, project FROM {support_project}');
  foreach ($result as $project) {
    $path = strtolower(preg_replace('/[^0-9a-zA-Z]/', '', $project->project));
    db_update('support_project')
      ->fields(array(
      'path' => $path,
    ))
      ->condition('projid', $project->projid)
      ->execute();
  }

  // Add index after conversion, as otherwise it will fail as all paths will be
  // the same, ''.  If there'd been an actual release of this module already,
  // we'd add more error checking here to handle multiple projects of the same
  // name.
  db_add_index('support_project', 'path', array(
    'path',
  ));
}

/**
 * Remove {support_plan}.pid, it's useless and gets in the way of doing merge queries.
 */
function support_pm_update_7000() {
  db_drop_index('support_plan', 'uid_clid_day');

  //  db_drop_primary_key('support_plan');
  db_drop_field('support_plan', 'pid');
  db_add_primary_key('support_plan', array(
    'uid',
    'clid',
    'day',
  ));
}

/**
 * Ensure clid 0 row exists for "All Clients" in {support_project_client}.
 */
function support_pm_update_7001() {
  $result = db_query('SELECT p.projid FROM {support_project} p LEFT JOIN {support_project_client} c ON p.projid = c.projid WHERE c.projid IS NULL');
  foreach ($result as $project) {
    db_insert('support_project_client')
      ->fields(array(
      'projid' => $project->projid,
      'clid' => 0,
    ))
      ->execute();
  }
}

Functions

Namesort descending Description
support_pm_schema Support project management database schema definition.
support_pm_update_6001 Add project tables.
support_pm_update_6002 Add path to {support_project}.
support_pm_update_7000 Remove {support_plan}.pid, it's useless and gets in the way of doing merge queries.
support_pm_update_7001 Ensure clid 0 row exists for "All Clients" in {support_project_client}.